kpilot/kpilot

main-test.cc

00001 /* KPilot
00002 **
00003 ** Copyright (C) 2001 by Dan Pilone
00004 ** Copyright (C) 2001,2002,2003,2004 by Adriaan de Groot
00005 **
00006 ** This is the main program for kpilotTest, which shows a SyncLog and
00007 ** exercises the KPilotDeviceLink class. It's intended to test if the
00008 ** Palm hardware and the KPilot software are functioning correctly to
00009 ** some extent.
00010 */
00011 
00012 /*
00013 ** This program is free software; you can redistribute it and/or modify
00014 ** it under the terms of the GNU General Public License as published by
00015 ** the Free Software Foundation; either version 2 of the License, or
00016 ** (at your option) any later version.
00017 **
00018 ** This program is distributed in the hope that it will be useful,
00019 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00020 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00021 ** GNU General Public License for more details.
00022 **
00023 ** You should have received a copy of the GNU General Public License
00024 ** along with this program in a file called COPYING; if not, write to
00025 ** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00026 ** MA 02110-1301, USA.
00027 */
00028 
00029 /*
00030 ** Bug reports and questions can be sent to kde-pim@kde.org.
00031 */
00032 static const char *test_id =
00033     "$Id: main-test.cc 449698 2005-08-16 13:24:56Z adridg $";
00034 
00035 #include "options.h"
00036 
00037 #include <stdlib.h>
00038 #include <time.h>
00039 #include <iostream>
00040 
00041 #include <qpushbutton.h>
00042 #include <qhbox.h>
00043 #include <qtimer.h>
00044 
00045 #include <kapplication.h>
00046 #include <klocale.h>
00047 #include <kaboutdata.h>
00048 #include <kcmdlineargs.h>
00049 #include <kservice.h>
00050 #include <kservicetype.h>
00051 #include <kuserprofile.h>
00052 
00053 #include <pi-version.h>
00054 
00055 #include "logWidget.h"
00056 #include "kpilotConfig.h"
00057 #include "syncStack.h"
00058 #include "hotSync.h"
00059 #include "interactiveSync.h"
00060 
00061 static KCmdLineOptions kpilotoptions[] = {
00062     {"p",0,0},
00063     {"port <device>",
00064         I18N_NOOP("Path to Pilot device node"),
00065         "/dev/pilot"},
00066     {"l",0,0},
00067     {"list", I18N_NOOP("List DBs (default)"), 0},
00068     {"b",0,0},
00069     {"backup", I18N_NOOP("Backup instead of list DBs"), 0},
00070     {"r",0,0},
00071     {"restore", I18N_NOOP("Restore Pilot from backup"), 0},
00072     {"L",0,0},
00073     { "conduit-list", I18N_NOOP("List available conduits"), 0},
00074     {"E",0,0},
00075     { "conduit-exec <filename>",
00076         I18N_NOOP("Run conduit from desktop file <filename>"),
00077         0 },
00078     { "T",0,0},
00079     { "notest",
00080         I18N_NOOP("*Really* run the conduit, not in test mode."),
00081         0 } ,
00082     { "F",0,0},
00083     { "local",
00084         I18N_NOOP("Run the conduit in file-test mode."),
00085         0 } ,
00086     { "HHtoPC",
00087         I18N_NOOP("Copy Pilot to Desktop."),
00088         0 } ,
00089     { "PCtoHH",
00090         I18N_NOOP("Copy Desktop to Pilot."),
00091         0 } ,
00092     { "test-timeout",
00093         I18N_NOOP("Run conduit specially designed to timeout."),
00094         0 } ,
00095     { "test-usercheck",
00096         I18N_NOOP("Run conduit just for user check."),
00097         0 } ,
00098     { "dump-calendar",
00099         I18N_NOOP("Dump calendar to stdout."),
00100         0 } ,
00101 #ifdef DEBUG
00102     {"debug <level>", I18N_NOOP("Set debugging level"), "0"},
00103 #endif
00104     KCmdLineLastOption
00105 };
00106 
00107 
00108 static LogWidget *logWidget = 0L;
00109 static QPushButton *resetButton = 0L;
00110 
00111 
00112 
00117 class TimeoutAction : public SyncAction
00118 {
00119 public:
00120     TimeoutAction(KPilotDeviceLink *p) ;
00121 protected:
00122     virtual bool exec();
00123 } ;
00124 
00125 TimeoutAction::TimeoutAction(KPilotDeviceLink *p) :
00126     SyncAction(p)
00127 {
00128     FUNCTIONSETUP;
00129 }
00130 
00131 bool TimeoutAction::exec()
00132 {
00133     FUNCTIONSETUP;
00134 
00135     for (int i = 0; i<3; i++)
00136     {
00137         logMessage( CSL1("Hup two %1").arg(i) );
00138         fHandle->tickle();
00139         qApp->processEvents();
00140         sleep(1);
00141     }
00142 
00143     logMessage( CSL1("Now sleeping 65") );
00144     qApp->processEvents();
00145     sleep(65);
00146     return delayDone();
00147 }
00148 
00149 
00150 
00151 
00152 
00153 
00154 
00155 void createLogWidget()
00156 {
00157     LogWidget *w = new LogWidget(0L);
00158 
00159     w->resize(300, 300);
00160     w->show();
00161     w->setShowTime(true);
00162     kapp->setMainWidget(w);
00163     logWidget = w;
00164 
00165     resetButton = new QPushButton(i18n("Reset"),w->buttonBox());
00166 }
00167 
00168 static KPilotDeviceLink *deviceLink = 0L;
00169 
00170 void createLink()
00171 {
00172     FUNCTIONSETUP;
00173 
00174     deviceLink = new KPilotDeviceLink(0, "deviceLink");
00175 
00176     QObject::connect(deviceLink, SIGNAL(logError(const QString &)),
00177         logWidget, SLOT(addError(const QString &)));
00178     QObject::connect(deviceLink, SIGNAL(logMessage(const QString &)),
00179         logWidget, SLOT(addMessage(const QString &)));
00180     QObject::connect(deviceLink,SIGNAL(logProgress(const QString &,int)),
00181         logWidget, SLOT(addProgress(const QString &,int)));
00182 
00183 }
00184 
00185 static ActionQueue *syncStack = 0L;
00186 
00187 void connectStack()
00188 {
00189     FUNCTIONSETUP;
00190 
00191     QObject::connect(syncStack, SIGNAL(logError(const QString &)),
00192         logWidget, SLOT(addError(const QString &)));
00193     QObject::connect(syncStack, SIGNAL(logMessage(const QString &)),
00194         logWidget, SLOT(addMessage(const QString &)));
00195     QObject::connect(syncStack,SIGNAL(logProgress(const QString &,int)),
00196         logWidget, SLOT(addProgress(const QString &,int)));
00197     QObject::connect(syncStack, SIGNAL(syncDone(SyncAction *)),
00198         logWidget, SLOT(syncDone()));
00199 
00200     if (deviceLink)
00201     {
00202         QObject::connect(syncStack, SIGNAL(syncDone(SyncAction *)),
00203             deviceLink, SLOT(close()));
00204         QObject::connect(deviceLink, SIGNAL(deviceReady(KPilotDeviceLink*)), syncStack, SLOT(execConduit()));
00205         QObject::connect(resetButton,SIGNAL(clicked()),deviceLink,SLOT(reset()));
00206     }
00207 }
00208 
00209 void createConnection(KCmdLineArgs *p)
00210 {
00211     FUNCTIONSETUP;
00212 
00213     QString devicePath = p->getOption("port");
00214 
00215     if (devicePath.isEmpty())
00216     {
00217         devicePath = "/dev/pilot";
00218     }
00219 
00220     deviceLink->reset(devicePath);
00221 }
00222 
00223 int syncTest(KCmdLineArgs *p)
00224 {
00225     FUNCTIONSETUP;
00226 
00227     createLogWidget();
00228     createLink();
00229 
00230     syncStack = new ActionQueue(deviceLink);
00231 
00232     if (p->isSet("backup"))
00233     {
00234         syncStack->queueInit();
00235         syncStack->addAction(new BackupAction(deviceLink,true));
00236     }
00237     else if (p->isSet("restore"))
00238     {
00239         syncStack->queueInit(0);
00240         syncStack->addAction(new RestoreAction(deviceLink));
00241     }
00242     else if (p->isSet("test-timeout"))
00243     {
00244         syncStack->queueInit();
00245         syncStack->addAction( new TimeoutAction(deviceLink) );
00246         syncStack->addAction( new TimeoutAction(deviceLink) );
00247     }
00248     else
00249     {
00250         syncStack->queueInit(p->isSet("test-usercheck") /* whether to run usercheck */);
00251         syncStack->addAction(new TestLink(deviceLink));
00252     }
00253     syncStack->queueCleanup();
00254 
00255     connectStack();
00256     createConnection(p);
00257     return kapp->exec();
00258 }
00259 
00260 int execConduit(KCmdLineArgs *p)
00261 {
00262     FUNCTIONSETUP;
00263 
00264     // get --exec-conduit value
00265     QString s = p->getOption("conduit-exec");
00266     if (s.isEmpty()) return 1;
00267     QStringList l;
00268     l.append(s);
00269 
00270     createLogWidget();
00271 
00272     SyncAction::SyncMode::Mode syncMode = SyncAction::SyncMode::eHotSync;
00273     if (p->isSet("HHtoPC")) syncMode = SyncAction::SyncMode::eCopyHHToPC;
00274     if (p->isSet("PCtoHH")) syncMode = SyncAction::SyncMode::eCopyPCToHH;
00275     SyncAction::SyncMode mode(syncMode,p->isSet("test"),p->isSet("local"));
00276 
00277     if (!p->isSet("local"))
00278     {
00279         createLink();
00280 
00281         syncStack = new ActionQueue(deviceLink);
00282         syncStack->queueInit();
00283         syncStack->queueConduits(l,mode,false);
00284         syncStack->queueCleanup();
00285         connectStack();
00286         createConnection(p);
00287     }
00288     else
00289     {
00290         syncStack = new ActionQueue( 0L );
00291         syncStack->queueInit();
00292         syncStack->queueConduits(l,mode,false);
00293         syncStack->queueCleanup();
00294         connectStack();
00295         QTimer::singleShot(100,syncStack,SLOT(execConduit()));
00296     }
00297 
00298 
00299     return kapp->exec();
00300 }
00301 
00302 int listConduits(KCmdLineArgs *)
00303 {
00304     FUNCTIONSETUP;
00305 
00306     KServiceTypeProfile::OfferList offers =
00307         KServiceTypeProfile::offers(CSL1("KPilotConduit"));
00308 
00309     // Now actually fill the two list boxes, just make
00310     // sure that nothing gets listed in both.
00311     //
00312     //
00313     QValueListIterator < KServiceOffer > availList(offers.begin());
00314     while (availList != offers.end())
00315     {
00316         KSharedPtr < KService > o = (*availList).service();
00317 
00318         std::cout << o->desktopEntryName().latin1() << std::endl;
00319         std::cout << "\t" << o->name().latin1()  << std::endl;
00320         if (!o->library().isEmpty())
00321         {
00322             std::cout << "\tIn "
00323                 << o->library().latin1()
00324                 << std::endl;
00325         }
00326 
00327         ++availList;
00328     }
00329 
00330     return 0;
00331 }
00332 
00333 int main(int argc, char **argv)
00334 {
00335     FUNCTIONSETUP;
00336     KAboutData about("kpilotTest",
00337         I18N_NOOP("KPilotTest"),
00338         KPILOT_VERSION,
00339         "KPilot Tester",
00340         KAboutData::License_GPL, "(C) 2001-2004, Adriaan de Groot");
00341     about.addAuthor("Adriaan de Groot",
00342         I18N_NOOP("KPilot Maintainer"),
00343         "groot@kde.org", "http://www.cs.kun.nl/~adridg/kpilot/");
00344 
00345     KCmdLineArgs::init(argc, argv, &about);
00346     KCmdLineArgs::addCmdLineOptions(kpilotoptions, "kpilottest");
00347     KApplication::addCmdLineOptions();
00348 
00349     KCmdLineArgs *p = KCmdLineArgs::parsedArgs();
00350 
00351 
00352     KApplication a;
00353 #ifdef DEBUG
00354     KPilotConfig::getDebugLevel(p);
00355 #endif
00356 
00357     if ( p->isSet("backup") ||
00358         p->isSet("restore") ||
00359         p->isSet("list") ||
00360         p->isSet("test-timeout") ||
00361         p->isSet("test-usercheck") )
00362     {
00363         return syncTest(p);
00364     }
00365 
00366     if (p->isSet("conduit-list"))
00367     {
00368         return listConduits(p);
00369     }
00370 
00371     if (p->isSet("conduit-exec"))
00372     {
00373         return execConduit(p);
00374     }
00375 
00376     // The default is supposed to be "list"
00377     return syncTest(p);
00378     /* NOTREACHED */
00379     (void) test_id;
00380 }
00381 
00382 
KDE Home | KDE Accessibility Home | Description of Access Keys