kpilot/lib

syncAction.cc

00001 /* KPilot
00002 **
00003 ** Copyright (C) 1998-2001 by Dan Pilone
00004 ** Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
00005 ** Copyright (C) 2001 by Waldo Bastian (code in questionYesNo)
00006 **
00007 */
00008 
00009 /*
00010 ** This program is free software; you can redistribute it and/or modify
00011 ** it under the terms of the GNU Lesser General Public License as published by
00012 ** the Free Software Foundation; either version 2.1 of the License, or
00013 ** (at your option) any later version.
00014 **
00015 ** This program is distributed in the hope that it will be useful,
00016 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00018 ** GNU Lesser General Public License for more details.
00019 **
00020 ** You should have received a copy of the GNU Lesser General Public License
00021 ** along with this program in a file called COPYING; if not, write to
00022 ** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00023 ** MA 02110-1301, USA.
00024 */
00025 
00026 /*
00027 ** Bug reports and questions can be sent to kde-pim@kde.org
00028 */
00029 
00030 #include "options.h"
00031 
00032 #include <time.h>
00033 
00034 #include <pi-socket.h>
00035 #include <pi-dlp.h>
00036 
00037 #include <qtimer.h>
00038 #include <qvbox.h>
00039 #include <qlayout.h>
00040 #include <qcheckbox.h>
00041 #include <qlabel.h>
00042 #include <qmessagebox.h>
00043 #include <qdir.h>
00044 #include <qfile.h>
00045 #include <qfileinfo.h>
00046 #include <qtl.h>
00047 #include <qstyle.h>
00048 
00049 #include <kdialogbase.h>
00050 #include <kglobal.h>
00051 #include <kstandarddirs.h>
00052 #include <kconfig.h>
00053 #include <kmessagebox.h>
00054 
00055 #include "syncAction.moc"
00056 #include "kpilotlibSettings.h"
00057 
00058 SyncAction::SyncAction(KPilotLink  *p,
00059     const char *name) :
00060     QObject(p, name),
00061     fHandle(p),
00062     fParent(0L)
00063 {
00064     FUNCTIONSETUP;
00065 }
00066 
00067 SyncAction::SyncAction(KPilotLink *p,
00068     QWidget * visibleparent,
00069     const char *name) :
00070     QObject(p, name),
00071     fHandle(p),
00072     fParent(visibleparent)
00073 {
00074     FUNCTIONSETUP;
00075 }
00076 
00077 SyncAction::~SyncAction()
00078 {
00079 }
00080 
00081 /* virtual */ QString SyncAction::statusString() const
00082 {
00083     FUNCTIONSETUP;
00084     QString s = CSL1("status=");
00085 
00086     s.append(QString::number(status()));
00087     return s;
00088 }
00089 
00090 /* slot */ void SyncAction::execConduit()
00091 {
00092     FUNCTIONSETUP;
00093 
00094 #ifdef DEBUG
00095     DEBUGLIBRARY << fname
00096         << ": Running conduit " << name() << endl;
00097 #endif
00098 
00099     bool r = this->exec();
00100 
00101 #ifdef DEBUG
00102     DEBUGLIBRARY << fname << ": Exec returned " << r << endl;
00103 #endif
00104 
00105     if (!r)
00106     {
00107         emit logError(i18n("The conduit %1 could not be executed.")
00108             .arg(QString::fromLatin1(name())));
00109         delayDone();
00110     }
00111 }
00112 
00113 /* slot */ void SyncAction::delayedDoneSlot()
00114 {
00115     emit syncDone(this);
00116 }
00117 
00118 bool SyncAction::delayDone()
00119 {
00120     QTimer::singleShot(0,this,SLOT(delayedDoneSlot()));
00121     return true;
00122 }
00123 
00124 static struct
00125 {
00126     SyncAction::SyncMode::Mode mode;
00127     const char *name;
00128 } maps[] =
00129 {
00130     { SyncAction::SyncMode::eHotSync, "--hotsync" },
00131     { SyncAction::SyncMode::eFullSync, "--full" },
00132     { SyncAction::SyncMode::eCopyPCToHH, "--copyPCToHH" },
00133     { SyncAction::SyncMode::eCopyHHToPC, "--copyHHToPC" },
00134     { SyncAction::SyncMode::eBackup, "--backup" },
00135     { SyncAction::SyncMode::eRestore, "--restore" },
00136     { SyncAction::SyncMode::eFullSync, "--fullsync" },
00137     { SyncAction::SyncMode::eHotSync, (const char *)0 }
00138 }
00139 ;
00140 
00141 SyncAction::SyncMode::SyncMode(const QStringList &args) :
00142     fMode(eHotSync),
00143     fTest(args.contains("--test")),
00144     fLocal(args.contains("--local"))
00145 {
00146     int i = 0;
00147     while(maps[i].name)
00148     {
00149         if (args.contains(QString::fromLatin1(maps[i].name)))
00150         {
00151             fMode = maps[i].mode;
00152             break;
00153         }
00154         i++;
00155     }
00156 
00157     if (!maps[i].name)
00158     {
00159         kdError() << k_funcinfo << "No mode set by arguments "
00160             << args << ", defaulting to HotSync." << endl;
00161     }
00162 }
00163 
00164 SyncAction::SyncMode::SyncMode(Mode m, bool test, bool local) :
00165     fMode(m),
00166     fTest(test),
00167     fLocal(local)
00168 {
00169     if ( ((int)m<(int)eHotSync) || ((int)m>(int)eRestore) )
00170     {
00171         kdError() << k_funcinfo << "Mode value " << (int)m << " is illegal"
00172             ", defaulting to HotSync." << endl;
00173         fMode = eHotSync;
00174     }
00175 }
00176 
00177 QStringList SyncAction::SyncMode::list() const
00178 {
00179     FUNCTIONSETUPL(3);
00180 
00181     QStringList l;
00182     int i=0;
00183 
00184     while(maps[i].name)
00185     {
00186         if ( fMode == maps[i].mode )
00187         {
00188             l.append(QString::fromLatin1(maps[i].name));
00189             break;
00190         }
00191         i++;
00192     }
00193     if ( !maps[i].name )
00194     {
00195         kdError() << k_funcinfo << "Mode " << fMode << " does not have a name." << endl;
00196         l.append(QString::fromLatin1(maps[0].name));
00197     }
00198 
00199     if (isTest()) l.append(CSL1("--test"));
00200     if (isLocal()) l.append(CSL1("--local"));
00201     return l;
00202 }
00203 
00204 /* static */ QString SyncAction::SyncMode::name(SyncAction::SyncMode::Mode e)
00205 {
00206     switch(e)
00207     {
00208     case eHotSync : return i18n("HotSync");
00209     case eFullSync : return i18n("Full Synchronization");
00210     case eCopyPCToHH : return i18n("Copy PC to Handheld");
00211     case eCopyHHToPC : return i18n("Copy Handheld to PC");
00212     case eBackup : return i18n("Backup");
00213     case eRestore : return i18n("Restore From Backup");
00214     }
00215     return CSL1("<unknown>");
00216 }
00217 
00218 QString SyncAction::SyncMode::name() const
00219 {
00220     QString s = name(fMode);
00221     if (isTest())
00222     {
00223 
00224         s.append(CSL1(" [%1]").arg(TODO_I18N("Test Sync")));
00225     }
00226     if (isLocal())
00227     {
00228         s.append(CSL1(" [%1]").arg(TODO_I18N("Local Sync")));
00229     }
00230     return s;
00231 }
00232 
00233 bool SyncAction::SyncMode::setMode(int mode)
00234 {
00235     // Resets test and local flags too
00236     fTest = fLocal = false;
00237 
00238     if ( (mode>0) && (mode<=eRestore) )
00239     {
00240         fMode = (SyncAction::SyncMode::Mode) mode;
00241         return true;
00242     }
00243     else
00244     {
00245         kdWarning() << k_funcinfo << ": Bad sync mode " << mode << " requested." << endl ;
00246         fMode = eHotSync;
00247         return false;
00248     }
00249 }
00250 
00251 bool SyncAction::SyncMode::setMode(SyncAction::SyncMode::Mode m)
00252 {
00253     int i=0;
00254     while ( maps[i].name )
00255     {
00256         if ( maps[i].mode == m )
00257         {
00258             fMode = m;
00259             return true;
00260         }
00261         i++;
00262     }
00263 
00264     kdWarning() << k_funcinfo << ": Bad sync mode " << m << " requested." << endl ;
00265     fMode = eHotSync;
00266     return false;
00267 }
00268 
00269 void SyncAction::startTickle(unsigned timeout)
00270 {
00271     FUNCTIONSETUP;
00272 
00273     if (!deviceLink())
00274     {
00275         kdWarning() << k_funcinfo << ": Trying to tickle without a device." << endl;
00276     }
00277     else
00278     {
00279         connect(deviceLink(),SIGNAL(timeout()),this,SIGNAL(timeout()));
00280         deviceLink()->startTickle(timeout);
00281     }
00282 }
00283 
00284 void SyncAction::stopTickle()
00285 {
00286     FUNCTIONSETUP;
00287     if (!deviceLink())
00288     {
00289         kdWarning() << k_funcinfo << ": Trying to tickle without a device." << endl;
00290     }
00291     else
00292     {
00293         disconnect(deviceLink(),SIGNAL(timeout()),this,SIGNAL(timeout()));
00294         deviceLink()->stopTickle();
00295     }
00296 }
00297 
00298 
00299 int SyncAction::questionYesNo(const QString & text,
00300     const QString & caption,
00301     const QString & key,
00302     unsigned timeout,
00303     const QString & yes,
00304     const QString &no )
00305 {
00306     FUNCTIONSETUP;
00307 
00308     bool checkboxReturn = false;
00309     int r;
00310     KMessageBox::ButtonCode result;
00311     if (!key.isEmpty())
00312     {
00313         if (!KMessageBox::shouldBeShownYesNo(key,result))
00314         {
00315             return result;
00316         }
00317     }
00318 
00319     KDialogBase *dialog =
00320         new KDialogBase(caption.isNull()? i18n("Question") : caption,
00321         KDialogBase::Yes | KDialogBase::No,
00322         KDialogBase::Yes, KDialogBase::No,
00323         fParent, "questionYesNo", true, true,
00324         yes.isEmpty() ? KStdGuiItem::yes() : yes,
00325         no.isEmpty() ? KStdGuiItem::no() : no);
00326 
00327     if ( (timeout > 0) && ( deviceLink() ) )
00328     {
00329         QObject::connect(deviceLink(), SIGNAL(timeout()),
00330             dialog, SLOT(slotCancel()));
00331         startTickle(timeout);
00332     }
00333 
00334 #if KDE_IS_VERSION(3,3,0)
00335     r = (KMessageBox::ButtonCode) KMessageBox::createKMessageBox(dialog,
00336         QMessageBox::Question,
00337         text,
00338         QStringList(),
00339         (key.isEmpty() ? QString::null : i18n("&Do not ask again")),
00340         &checkboxReturn,
00341         0);
00342 
00343 #else
00344     // The following code is taken from KDialogBase.cc,
00345     // part of the KDE 2.2 libraries. Copyright 2001
00346     // by Waldo Bastian.
00347     //
00348     //
00349     QVBox *topcontents = new QVBox(dialog);
00350 
00351     topcontents->setSpacing(KDialog::spacingHint() * 2);
00352     topcontents->setMargin(KDialog::marginHint() * 2);
00353 
00354     QWidget *contents = new QWidget(topcontents);
00355     QHBoxLayout *lay = new QHBoxLayout(contents);
00356 
00357     lay->setSpacing(KDialog::spacingHint() * 2);
00358 
00359     lay->addStretch(1);
00360     QLabel *label1 = new QLabel( contents);
00361     label1->setPixmap(QMessageBox::standardIcon(QMessageBox::Information));
00362     lay->add( label1 );
00363     QLabel *label2 = new QLabel( text, contents);
00364     label2->setMinimumSize(label2->sizeHint());
00365     lay->add(label2);
00366     lay->addStretch(1);
00367 
00368     QSize extraSize = QSize(50, 30);
00369 
00370     QCheckBox *checkbox = 0L;
00371     if (!key.isEmpty())
00372     {
00373         checkbox = new QCheckBox(i18n("Do not ask again"),topcontents);
00374         extraSize = QSize(50,0);
00375     }
00376 
00377     dialog->setMainWidget(topcontents);
00378     dialog->enableButtonSeparator(false);
00379     dialog->incInitialSize(extraSize);
00380 
00381     r = dialog->exec();
00382     if (checkbox)
00383     {
00384         checkboxReturn = checkbox->isChecked();
00385     }
00386 #endif
00387 
00388     switch(r)
00389     {
00390     case KDialogBase::Yes : result=KMessageBox::Yes ; break;
00391     case KDialogBase::No  : result=KMessageBox::No; break;
00392     case KDialogBase::Cancel : result=KMessageBox::Cancel; break;
00393     default : break;
00394     }
00395 
00396     stopTickle();
00397 
00398     if (!key.isEmpty() && checkboxReturn)
00399     {
00400         KMessageBox::saveDontShowAgainYesNo(key,result);
00401     }
00402 
00403     return result;
00404 }
00405 
00406 
00407 int SyncAction::questionYesNoCancel(const QString & text,
00408     const QString & caption,
00409     const QString & key,
00410     unsigned timeout,
00411     const QString &yes,
00412     const QString &no)
00413 {
00414     FUNCTIONSETUP;
00415 
00416     bool checkboxReturn = false;
00417     int r;
00418     KMessageBox::ButtonCode result;
00419 
00420     if (!key.isEmpty())
00421     {
00422         if (!KMessageBox::shouldBeShownYesNo(key,result))
00423         {
00424             if (result != KMessageBox::Cancel)
00425             {
00426                 return result;
00427             }
00428         }
00429     }
00430 
00431     KDialogBase *dialog =
00432         new KDialogBase(caption.isNull()? i18n("Question") : caption,
00433         KDialogBase::Yes | KDialogBase::No | KDialogBase::Cancel,
00434         KDialogBase::Yes, KDialogBase::Cancel,
00435         fParent, "questionYesNoCancel", true, true,
00436         (yes.isEmpty() ? KStdGuiItem::yes() : yes),
00437         (no.isEmpty() ? KStdGuiItem::no() : no),
00438         KStdGuiItem::cancel());
00439 
00440     if ( (timeout > 0) && (deviceLink()) )
00441     {
00442         QObject::connect(deviceLink(), SIGNAL(timeout()),
00443             dialog, SLOT(slotCancel()));
00444         startTickle(timeout);
00445     }
00446 
00447 #if KDE_IS_VERSION(3,3,0)
00448     r = KMessageBox::createKMessageBox(dialog,
00449         QMessageBox::Question,
00450         text,
00451         QStringList(),
00452         (key.isEmpty() ? QString::null : i18n("&Do not ask again")),
00453         &checkboxReturn,
00454         0);
00455 #else
00456     // The following code is taken from KDialogBase.cc,
00457     // part of the KDE 2.2 libraries. Copyright 2001
00458     // by Waldo Bastian.
00459     //
00460     //
00461     QVBox *topcontents = new QVBox(dialog);
00462 
00463     topcontents->setSpacing(KDialog::spacingHint() * 2);
00464     topcontents->setMargin(KDialog::marginHint() * 2);
00465 
00466     QWidget *contents = new QWidget(topcontents);
00467     QHBoxLayout *lay = new QHBoxLayout(contents);
00468 
00469     lay->setSpacing(KDialog::spacingHint() * 2);
00470 
00471     lay->addStretch(1);
00472     QLabel *label1 = new QLabel( contents);
00473     label1->setPixmap(QMessageBox::standardIcon(QMessageBox::Information));
00474     lay->add( label1 );
00475     QLabel *label2 = new QLabel( text, contents);
00476     label2->setMinimumSize(label2->sizeHint());
00477     lay->add(label2);
00478     lay->addStretch(1);
00479 
00480     QSize extraSize = QSize(50, 30);
00481 
00482     QCheckBox *checkbox = 0L;
00483     if (!key.isEmpty())
00484     {
00485         checkbox = new QCheckBox(i18n("Do not ask again"),topcontents);
00486         extraSize = QSize(50,0);
00487     }
00488 
00489     dialog->setMainWidget(topcontents);
00490     dialog->enableButtonSeparator(false);
00491     dialog->incInitialSize(extraSize);
00492 
00493     r = dialog->exec();
00494     if (checkbox)
00495     {
00496         checkboxReturn = checkbox->isChecked();
00497     }
00498 #endif
00499 
00500     switch(r)
00501     {
00502     case KDialogBase::Yes : result=KMessageBox::Yes ; break;
00503     case KDialogBase::No  : result=KMessageBox::No; break;
00504     case KDialogBase::Cancel : result=KMessageBox::Cancel; break;
00505     default : break;
00506     }
00507     stopTickle();
00508 
00509     if (!key.isEmpty() && checkboxReturn)
00510     {
00511         KMessageBox::saveDontShowAgainYesNo(key,result);
00512     }
00513 
00514     return result;
00515 }
00516 
KDE Home | KDE Accessibility Home | Description of Access Keys