kpilot Library API Documentation

conduitConfigDialog.cc

00001 /* conduitConfigDialog.cc                KPilot
00002 **
00003 ** Copyright (C) 2001 by Dan Pilone
00004 **
00005 ** This file defines a .ui-based configuration dialog for conduits.
00006 */
00007 
00008 /*
00009 ** This program is free software; you can redistribute it and/or modify
00010 ** it under the terms of the GNU General Public License as published by
00011 ** the Free Software Foundation; either version 2 of the License, or
00012 ** (at your option) any later version.
00013 **
00014 ** This program is distributed in the hope that it will be useful,
00015 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00017 ** GNU General Public License for more details.
00018 **
00019 ** You should have received a copy of the GNU General Public License
00020 ** along with this program in a file called COPYING; if not, write to
00021 ** the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00022 ** MA 02111-1307, USA.
00023 */
00024 
00025 /*
00026 ** Bug reports and questions can be sent to kde-pim@kde.org
00027 */
00028 
00029 static const char *conduitconfigdialog_id =
00030         "$Id: conduitConfigDialog.cc,v 1.9.4.5 2003/03/12 23:31:13 adridg Exp $";
00031 
00032 #include "options.h"
00033 
00034 #include <qlistview.h>
00035 #include <qlabel.h>
00036 #include <qtooltip.h>
00037 #include <qfile.h>
00038 #include <qpushbutton.h>
00039 
00040 #include <kservice.h>
00041 #include <kservicetype.h>
00042 #include <kuserprofile.h>
00043 #include <kprocess.h>
00044 #include <kmessagebox.h>
00045 #include <kglobal.h>
00046 #include <kstandarddirs.h>
00047 #include <klibloader.h>
00048 
00049 #include "plugin.h"
00050 #include "kpilotConfig.h"
00051 
00052 #include "conduitConfigDialog_base.h"
00053 #include "conduitConfigDialog.moc"
00054 
00055 #define CONDUIT_NAME    (0)
00056 #define CONDUIT_COMMENT (1)
00057 #define CONDUIT_DESKTOP (2)
00058 #define CONDUIT_LIBRARY (3)
00059 
00060 class ConduitTip : public QToolTip
00061 {
00062 public:
00063         ConduitTip(QListView *parent);
00064         virtual ~ConduitTip();
00065 
00066 protected:
00067         virtual void maybeTip(const QPoint &);
00068 
00069         QListView *fListView;
00070 } ;
00071 
00072 
00073 ConduitTip::ConduitTip(QListView *p) :
00074         QToolTip(p->viewport(),0L),
00075         fListView(p)
00076 {
00077         FUNCTIONSETUP;
00078 }
00079 
00080 ConduitTip::~ConduitTip()
00081 {
00082         FUNCTIONSETUP;
00083 }
00084 
00085 /* virtual */ void ConduitTip::maybeTip(const QPoint &p)
00086 {
00087         FUNCTIONSETUP;
00088 
00089         QListViewItem *l = fListView->itemAt(p);
00090 
00091         if (!l) return;
00092 
00093         // ConduitListItem *q = static_cast<ConduitListItem *>(l);
00094 
00095 #ifdef DEBUG
00096         DEBUGKPILOT << fname
00097                 << ": Tip over "
00098                 << l->text(CONDUIT_NAME)
00099                 << " with text "
00100                 << l->text(CONDUIT_COMMENT)
00101                 << endl;
00102 #endif
00103 
00104         QString s = l->text(CONDUIT_COMMENT);
00105 
00106         if (s.isEmpty()) return;
00107         if (s.find(CSL1("<qt>"),0,false) == -1)
00108         {
00109                 s.prepend(CSL1("<qt>"));
00110                 s.append(CSL1("</qt>"));
00111         }
00112 
00113         tip(fListView->itemRect(l),s);
00114 }
00115 
00116 
00117 ConduitConfigDialog::ConduitConfigDialog(QWidget * _w, const char *n,
00118         bool m) : UIDialog(_w, n, m)
00119 {
00120         FUNCTIONSETUP;
00121 
00122         fConfigWidget = new ConduitConfigWidget(widget());
00123 
00124         fillLists();
00125 
00126         fConfigWidget->active->adjustSize();
00127         fConfigWidget->available->adjustSize();
00128 
00129         int w = QMAX(fConfigWidget->active->width(),
00130                 fConfigWidget->available->width());
00131 
00132 
00133         fConfigWidget->available->resize(w,fConfigWidget->available->height());
00134         fConfigWidget->active->resize(w,fConfigWidget->active->height());
00135         fConfigWidget->available->setColumnWidth(0,w);
00136         fConfigWidget->active->setColumnWidth(0,w);
00137         fConfigWidget->available->setColumnWidthMode(0,QListView::Manual);
00138         fConfigWidget->active->setColumnWidthMode(0,QListView::Manual);
00139 
00140         QObject::connect(fConfigWidget->active,
00141                 SIGNAL(selectionChanged(QListViewItem *)),
00142                 this,SLOT(selected(QListViewItem *)));
00143         QObject::connect(fConfigWidget->available,
00144                 SIGNAL(selectionChanged(QListViewItem *)),
00145                 this,SLOT(selected(QListViewItem *)));
00146         QObject::connect(fConfigWidget->active,
00147                 SIGNAL(doubleClicked(QListViewItem *)),
00148                 this,SLOT(configureConduit()));
00149 
00150         QObject::connect(fConfigWidget->enableButton,
00151                 SIGNAL(clicked()),
00152                 this,SLOT(enableConduit()));
00153         QObject::connect(fConfigWidget->disableButton,
00154                 SIGNAL(clicked()),
00155                 this,SLOT(disableConduit()));
00156         QObject::connect(fConfigWidget->configButton,
00157                 SIGNAL(clicked()),
00158                 this,SLOT(configureConduit()));
00159 
00160         fConfigWidget->adjustSize();
00161 
00162         (void) new ConduitTip(fConfigWidget->active);
00163         (void) new ConduitTip(fConfigWidget->available);
00164 
00165         selected(0L);
00166 
00167         (void) conduitconfigdialog_id;
00168 }
00169 
00170 ConduitConfigDialog::~ConduitConfigDialog()
00171 {
00172         FUNCTIONSETUP;
00173 }
00174 
00175 void ConduitConfigDialog::fillLists()
00176 {
00177         FUNCTIONSETUP;
00178 
00179         QStringList potentiallyInstalled =
00180                 KPilotConfig::getConfig().setConduitGroup().
00181                 getInstalledConduits();
00182         KServiceTypeProfile::OfferList offers =
00183                 KServiceTypeProfile::offers(CSL1("KPilotConduit"));
00184 
00185         // Now actually fill the two list boxes, just make
00186         // sure that nothing gets listed in both.
00187         //
00188         //
00189         QValueListIterator < KServiceOffer > availList(offers.begin());
00190         while (availList != offers.end())
00191         {
00192                 KSharedPtr < KService > o = (*availList).service();
00193 
00194 #ifdef DEBUG
00195                 DEBUGKPILOT << fname << ": "
00196                         << o->desktopEntryName()
00197                         << " = " << o->name() << endl;
00198 #endif
00199 
00200                 QListViewItem *p = 0L;
00201 
00202                 if (!o->exec().isEmpty())
00203                 {
00204                         kdWarning() << k_funcinfo
00205                                 << ": Old-style conduit found "
00206                                 << o->name()
00207                                 << endl;
00208                 }
00209 
00210                 if (potentiallyInstalled.contains(o->desktopEntryName()) == 0)
00211                 {
00212                         p = new QListViewItem(fConfigWidget->available,
00213                                 o->name(),
00214                                 o->comment(),
00215                                 o->desktopEntryName(),
00216                                 o->library());
00217                 }
00218                 else
00219                 {
00220                         p = new QListViewItem(fConfigWidget->active,
00221                                 o->name(),
00222                                 o->comment(),
00223                                 o->desktopEntryName(),
00224                                 o->library());
00225                 }
00226 
00227                 ++availList;
00228         }
00229 }
00230 
00231 void ConduitConfigDialog::selected(QListViewItem *p)
00232 {
00233         FUNCTIONSETUP;
00234 
00235         if (!p)
00236         {
00237                 fConfigWidget->configButton->setEnabled(false);
00238                 fConfigWidget->enableButton->setEnabled(false);
00239                 fConfigWidget->disableButton->setEnabled(false);
00240                 return;
00241         }
00242 
00243         if (p->listView() == fConfigWidget->active)
00244         {
00245                 fConfigWidget->configButton->setEnabled(true);
00246                 fConfigWidget->enableButton->setEnabled(false);
00247                 fConfigWidget->disableButton->setEnabled(true);
00248                 fConfigWidget->available->clearSelection();
00249         }
00250         else
00251         {
00252                 fConfigWidget->configButton->setEnabled(false);
00253                 fConfigWidget->enableButton->setEnabled(true);
00254                 fConfigWidget->disableButton->setEnabled(false);
00255                 fConfigWidget->active->clearSelection();
00256         }
00257 }
00258 
00259 void ConduitConfigDialog::enableConduit()
00260 {
00261         FUNCTIONSETUP;
00262 
00263         QListViewItem *l = fConfigWidget->available->currentItem();
00264         if (!l) return;
00265 
00266         fConfigWidget->available->takeItem(l);
00267         fConfigWidget->active->clearSelection();
00268         fConfigWidget->active->insertItem(l);
00269         fConfigWidget->active->setSelected(l,true);
00270         selected(l);
00271 }
00272 
00273 void ConduitConfigDialog::disableConduit()
00274 {
00275         FUNCTIONSETUP;
00276 
00277         QListViewItem *l = fConfigWidget->active->currentItem();
00278         if (!l) return;
00279 
00280         fConfigWidget->active->takeItem(l);
00281         fConfigWidget->available->clearSelection();
00282         fConfigWidget->available->insertItem(l);
00283         fConfigWidget->available->setSelected(l,true);
00284         selected(l);
00285         fConfigWidget->available->setFocus();
00286 }
00287 
00288 
00289 void ConduitConfigDialog::configureConduit()
00290 {
00291         FUNCTIONSETUP;
00292 
00293         QListViewItem *p = fConfigWidget->active->currentItem();
00294 
00295         if (!p)
00296         {
00297 #ifdef DEBUG
00298                 DEBUGKPILOT << fname
00299                         << ": Executed NULL conduit?"
00300                         << endl;
00301 #endif
00302                 return;
00303         }
00304 
00305 #ifdef DEBUG
00306         DEBUGKPILOT << fname
00307                 << ": Executing conduit "
00308                 << p->text(CONDUIT_NAME)
00309                 << endl;
00310 #endif
00311 
00312         if (p->text(CONDUIT_LIBRARY).isEmpty())
00313         {
00314                 warnNoExec(p);
00315                 return;
00316         }
00317 
00318         QCString library = QFile::encodeName(p->text(CONDUIT_LIBRARY));
00319 
00320         KLibFactory *f = KLibLoader::self()->
00321                 factory(library);
00322         if (!f)
00323         {
00324 #ifdef DEBUG
00325                 DEBUGKPILOT << fname
00326                         << ": No conduit library "
00327                         << library
00328                         << " found."
00329                         << endl;
00330 #endif
00331                 warnNoLibrary(p);
00332                 return;
00333         }
00334 
00335         QStringList a;
00336         a.append(CSL1("modal"));
00337 
00338         QObject *o = f->create(this, 0L, "ConduitConfig",a);
00339 
00340 
00341         if (!o)
00342         {
00343 #ifdef DEBUG
00344                 DEBUGKPILOT << fname
00345                         << ": Can't create object."
00346                         << endl;
00347 #endif
00348 
00349                 KLibLoader::self()->unloadLibrary(
00350                         library);
00351                 warnNoLibrary(p);
00352                 return;
00353         }
00354 
00355         ConduitConfig *d = dynamic_cast<ConduitConfig *>(o);
00356 
00357         if (!d)
00358         {
00359 #ifdef DEBUG
00360                 DEBUGKPILOT << fname
00361                         << ": Can't cast to dialog."
00362                         << endl;
00363 #endif
00364 
00365                 delete o;
00366                 KLibLoader::self()->unloadLibrary(
00367                         library);
00368                 warnNoLibrary(p);
00369                 return;
00370         }
00371 
00372         d->setConfig(&KPilotConfig::getConfig());
00373         d->readSettings();
00374         d->exec();
00375 
00376         delete d;
00377         KLibLoader::self()->unloadLibrary(
00378                 library);
00379 }
00380 
00381 
00382 void ConduitConfigDialog::warnNoExec(const QListViewItem * p)
00383 {
00384         FUNCTIONSETUP;
00385 
00386         QString msg = i18n("<qt>No library could be "
00387                 "found for the conduit %1. This means that the "
00388                 "conduit was not installed properly.</qt>")
00389                 .arg(p->text(CONDUIT_NAME));
00390 
00391 #ifdef DEBUG
00392         DEBUGKPILOT << fname << ": " << msg << endl;
00393 #endif
00394 
00395         KMessageBox::error(this, msg, i18n("Conduit Error"));
00396 }
00397 
00398 void ConduitConfigDialog::warnNoLibrary(const QListViewItem *p)
00399 {
00400         FUNCTIONSETUP;
00401 
00402         QString msg = i18n("<qt>There was a problem loading the library "
00403                 "for the conduit %1. This means that the "
00404                 "conduit was not installed properly.</qt>")
00405                 .arg(p->text(CONDUIT_NAME));
00406 
00407         KMessageBox::error(this, msg, i18n("Conduit Error"));
00408 }
00409 
00410 /* virtual */ void ConduitConfigDialog::commitChanges()
00411 {
00412         FUNCTIONSETUP;
00413 
00414         QStringList activeConduits;
00415         const QListViewItem *p = fConfigWidget->active->firstChild();
00416         KPilotConfigSettings & config = KPilotConfig::getConfig();
00417 
00418 
00419 
00420         while (p)
00421         {
00422                 activeConduits.append(p->text(CONDUIT_DESKTOP));
00423                 p = p->nextSibling();
00424         }
00425         config.setConduitGroup().setInstalledConduits(activeConduits);
00426         config.sync();
00427 }
00428 
00429 
00430 
00431 
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Feb 15 11:40:43 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2001