kpilot

main-test.cc

Go to the documentation of this file.
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 
00033 #include "options.h"
00034 
00035 #include <stdlib.h>
00036 #include <time.h>
00037 #include <iostream>
00038 
00039 #include <qpushbutton.h>
00040 #include <qhbox.h>
00041 #include <qtimer.h>
00042 
00043 #include <kapplication.h>
00044 #include <klocale.h>
00045 #include <kaboutdata.h>
00046 #include <kcmdlineargs.h>
00047 #include <kservice.h>
00048 #include <kservicetype.h>
00049 #include <kuserprofile.h>
00050 
00051 #include <pi-version.h>
00052 
00053 #include "actionQueue.h"
00054 #include "actions.h"
00055 #include "kpilotdevicelink.h"
00056 #include "kpilotlocallink.h"
00057 #include "pilot.h"
00058 
00059 #include "kpilotConfig.h"
00060 #include "hotSync.h"
00061 
00062 
00063 static KCmdLineOptions generalOptions[] = {
00064     {"p",0,0},
00065     {"port <device>",
00066         I18N_NOOP("Path to Pilot device node"),
00067         "/dev/pilot"},
00068     {"l",0,0},
00069     {"list", I18N_NOOP("List DBs"), 0},
00070     {"b",0,0},
00071     {"backup <dest dir>", I18N_NOOP("Backup Pilot to <dest dir>"), 0},
00072     {"r",0,0},
00073     {"restore <src dir>", I18N_NOOP("Restore Pilot from backup"), 0},
00074     {"e",0,0},
00075     { "exec <filename>",
00076         I18N_NOOP("Run conduit from desktop file <filename>"),
00077         0 },
00078     {"c",0,0},
00079     { "check <what>",
00080         I18N_NOOP("Run a specific check (with the device)"), "help"},
00081     {"s",0,0},
00082     { "show <what>",
00083         I18N_NOOP("Show KPilot configuration information"), "help"},
00084 #ifdef DEBUG
00085     { "debug <level>",
00086         I18N_NOOP("Set the debug level"), "1" },
00087 #endif
00088     KCmdLineLastOption
00089 } ;
00090 
00091 static KCmdLineOptions conduitOptions[] = {
00092     { "T",0,0},
00093     { "notest",
00094         I18N_NOOP("*Really* run the conduit, not in test mode."),
00095         0 } ,
00096     { "F",0,0},
00097     { "local",
00098         I18N_NOOP("Run the conduit in file-test mode."),
00099         0 } ,
00100     { "HHtoPC",
00101         I18N_NOOP("Copy Pilot to Desktop."),
00102         0 } ,
00103     { "PCtoHH",
00104         I18N_NOOP("Copy Desktop to Pilot."),
00105         0 } ,
00106     { "loop",
00107         I18N_NOOP("Repeated perform action - only useful for --list"),
00108         0 } ,
00109     KCmdLineLastOption
00110 } ;
00111 
00118 KPilotLink *createLink( bool local )
00119 {
00120     FUNCTIONSETUP;
00121     if (!local)
00122     {
00123         return new KPilotDeviceLink(0, "deviceLink");
00124     }
00125     else
00126     {
00127         return new KPilotLocalLink(0, "localLink");
00128     }
00129 }
00130 
00134 void connectStack( KPilotLink *l, ActionQueue *a, bool loop = false )
00135 {
00136     FUNCTIONSETUP;
00137 
00138     if (l && a)
00139     {
00140         QObject::connect(a, SIGNAL(syncDone(SyncAction *)),
00141             l, SLOT(close()));
00142         if (!loop)
00143         {
00144             QObject::connect(a, SIGNAL(syncDone(SyncAction *)),
00145                 kapp, SLOT(quit()));
00146         }
00147         else
00148         {
00149             QObject::connect(a, SIGNAL(syncDone(SyncAction *)),
00150                 l, SLOT(reset()));
00151         }
00152         QObject::connect(l, SIGNAL(deviceReady(KPilotLink*)),
00153             a, SLOT(execConduit()));
00154     }
00155 }
00156 
00157 
00158 
00159 int exec(const QString &device, const QString &what, KCmdLineArgs *p)
00160 {
00161     FUNCTIONSETUP;
00162 
00163     // get --exec-conduit value
00164     if (what.isEmpty()) return 1;
00165     QStringList l;
00166     l.append(what);
00167 
00168     SyncAction::SyncMode::Mode syncMode = SyncAction::SyncMode::eHotSync;
00169     if (p->isSet("HHtoPC")) syncMode = SyncAction::SyncMode::eCopyHHToPC;
00170     if (p->isSet("PCtoHH")) syncMode = SyncAction::SyncMode::eCopyPCToHH;
00171     SyncAction::SyncMode mode(syncMode,p->isSet("test"),p->isSet("local"));
00172 
00173     KPilotLink *link = createLink( p->isSet("local") );
00174     ActionQueue *syncStack = new ActionQueue( link );
00175     syncStack->queueInit();
00176     syncStack->addAction(new CheckUser( link ));
00177     syncStack->queueConduits(l,mode);
00178     syncStack->queueCleanup();
00179     connectStack(link,syncStack);
00180     link->reset(device);
00181     return kapp->exec();
00182 }
00183 
00184 int backup(const QString &device, const QString &what, KCmdLineArgs *p)
00185 {
00186     FUNCTIONSETUP;
00187     KPilotLink *link = createLink( p->isSet("local") );
00188     ActionQueue *syncStack = new ActionQueue( link );
00189     syncStack->queueInit();
00190     BackupAction *ba = new BackupAction( link, true /* full backup */ );
00191     ba->setDirectory( what );
00192     syncStack->addAction( ba );
00193     syncStack->queueCleanup();
00194     connectStack(link,syncStack);
00195     link->reset(device);
00196     return kapp->exec();
00197 }
00198 
00199 int restore(const QString &device, const QString &what, KCmdLineArgs *p)
00200 {
00201     FUNCTIONSETUP;
00202     KPilotLink *link = createLink( p->isSet("local") );
00203     ActionQueue *syncStack = new ActionQueue( link );
00204     syncStack->queueInit();
00205     RestoreAction *ra = new RestoreAction( link );
00206     ra->setDirectory( what );
00207     syncStack->addAction( ra );
00208     syncStack->queueCleanup();
00209     connectStack(link,syncStack);
00210     link->reset(device);
00211     return kapp->exec();
00212 }
00213 
00214 int listDB(const QString &device, KCmdLineArgs *p)
00215 {
00216     FUNCTIONSETUP;
00217     KPilotLink *link = createLink( p->isSet("local") );
00218     ActionQueue *syncStack = new ActionQueue( link );
00219     syncStack->queueInit();
00220     syncStack->addAction( new TestLink( link ) );
00221     syncStack->queueCleanup();
00222     connectStack(link,syncStack, p->isSet("loop") );
00223     link->reset(device);
00224     return kapp->exec();
00225 }
00226 
00227 int check( const QString &device, const QString &what, KCmdLineArgs *p )
00228 {
00229     FUNCTIONSETUP;
00230 
00231     if ( "help" == what )
00232     {
00233         std::cout <<
00234 "You can use the --check option to kpilotTest to run various\n"
00235 "small checks that require the use of the device. These are:\n"
00236 "\thelp - show this help\n"
00237 "\tuser - check the user name on the handheld\n"
00238         << std::endl;
00239         return 0;
00240     }
00241 
00242     if ( "user" == what )
00243     {
00244         KPilotLink *link = createLink( p->isSet("local") );
00245         ActionQueue *syncStack = new ActionQueue( link );
00246         syncStack->queueInit();
00247         syncStack->addAction( new CheckUser( link ) );
00248         syncStack->queueCleanup();
00249         connectStack(link,syncStack);
00250         link->reset(device);
00251         return kapp->exec();
00252     }
00253 
00254     return 0;
00255 }
00256 
00257 void listConduits()
00258 {
00259     FUNCTIONSETUP;
00260 
00261     KServiceTypeProfile::OfferList offers =
00262         KServiceTypeProfile::offers(CSL1("KPilotConduit"));
00263 
00264     // Now actually fill the two list boxes, just make
00265     // sure that nothing gets listed in both.
00266     //
00267     //
00268     QValueListIterator < KServiceOffer > availList(offers.begin());
00269     while (availList != offers.end())
00270     {
00271         KSharedPtr < KService > o = (*availList).service();
00272 
00273         std::cout << "File:   " << o->desktopEntryName() << std::endl;
00274         std::cout << "  Desc: " << o->name()  << std::endl;
00275         if (!o->library().isEmpty())
00276         {
00277             std::cout << "  Lib : "
00278                 << o->library()
00279                 << std::endl;
00280         }
00281 
00282         ++availList;
00283     }
00284 }
00285 
00286 int show( const QString &what )
00287 {
00288     FUNCTIONSETUP;
00289 
00290     if ( "help" == what )
00291     {
00292         std::cout <<
00293 "Displays various bits of KPilot's internal settings. This\n"
00294 "does not require a device connection or a running KDE desktop.\n"
00295 "No change to data takes place. The following options are available\n"
00296 "for display:\n"
00297 "\thelp     - displays this help\n"
00298 "\tconduits - displays the list of available conduits\n"
00299 "\tuser     - displays the user name KPilot expects\n"
00300 "\tdevice   - displays the device settings in KPilot\n"
00301 "\tdebug    - displays internal numbers\n"
00302         << std::endl;
00303         return 0;
00304     }
00305 
00306     if ( "conduits" == what )
00307     {
00308         listConduits();
00309         return 0;
00310     }
00311 
00312     if ( "user" == what )
00313     {
00314         std::cout << "User: " << KPilotSettings::userName() << std::endl;
00315         return 0;
00316     }
00317 
00318     if ( "device" == what )
00319     {
00320         std::cout << "Device:   " << KPilotSettings::pilotDevice()
00321             << "\nSpeed:    " << KPilotSettings::pilotSpeed()
00322             << "\nEncoding: " << KPilotSettings::encoding()
00323             << "\nQuirks:   " << KPilotSettings::workarounds()
00324             << std::endl;
00325         return 0;
00326     }
00327 
00328     if ( "debug" == what )
00329     {
00330         std::cout << "Debug:  " << KPilotSettings::debug()
00331             << "\nConfig: " << KPilotSettings::configVersion()
00332             << std::endl;
00333         return 0;
00334     }
00335 
00336     std::cerr << "Unknown --show argument, use --show help for help.\n";
00337     return 1;
00338 }
00339 
00340 int main(int argc, char **argv)
00341 {
00342 #ifdef DEBUG
00343     debug_level = 1;
00344 #endif
00345     FUNCTIONSETUP;
00346     KAboutData about("kpilotTest",
00347         I18N_NOOP("KPilotTest"),
00348         KPILOT_VERSION,
00349         "KPilot Tester",
00350         KAboutData::License_GPL, "(C) 2001-2004, Adriaan de Groot");
00351     about.addAuthor("Adriaan de Groot",
00352         I18N_NOOP("KPilot Maintainer"),
00353         "groot@kde.org", "http://www.kpilot.org/");
00354 
00355     KCmdLineArgs::init(argc, argv, &about);
00356     KCmdLineArgs::addCmdLineOptions(generalOptions,
00357         I18N_NOOP("General"));
00358     KCmdLineArgs::addCmdLineOptions(conduitOptions,
00359         I18N_NOOP("Conduit Actions"),"conduit");
00360 
00361     KApplication::addCmdLineOptions();
00362 
00363     KCmdLineArgs *p = KCmdLineArgs::parsedArgs();
00364 
00365     bool needGUI = false;
00366 
00367     // Some versions need a GUI
00368     needGUI |= (p->isSet("check"));
00369     needGUI |= (p->isSet("exec")); // assume worst wrt. conduits
00370     needGUI |= (p->isSet("restore"));
00371 
00372     KApplication a(needGUI,needGUI);
00373 #ifdef DEBUG
00374     KPilotConfig::getDebugLevel(p);
00375     DEBUGKPILOT  << fname << "Created KApplication." << endl;
00376 #endif
00377 
00378     Pilot::setupPilotCodec(KPilotSettings::encoding());
00379 
00380     QString device( "/dev/pilot" );
00381 
00382     if ( p->isSet("port") )
00383     {
00384         device = p->getOption("port");
00385     }
00386 
00387     if ( p->isSet("check") )
00388     {
00389         return check( device, p->getOption("check"),
00390             KCmdLineArgs::parsedArgs("conduit") );
00391     }
00392 
00393     if ( p->isSet("show") )
00394     {
00395         return show( p->getOption("show") );
00396     }
00397 
00398     if ( p->isSet("exec") )
00399     {
00400         return exec( device, p->getOption("exec"),
00401             KCmdLineArgs::parsedArgs("conduit") );
00402     }
00403 
00404     if ( p->isSet("list") )
00405     {
00406         return listDB( device,
00407             KCmdLineArgs::parsedArgs("conduit") );
00408     }
00409 
00410     if ( p->isSet("backup") )
00411     {
00412         return backup( device, p->getOption("backup"),
00413             KCmdLineArgs::parsedArgs("conduit") );
00414     }
00415 
00416     if ( p->isSet("restore") )
00417     {
00418         return restore( device, p->getOption("restore"),
00419             KCmdLineArgs::parsedArgs("conduit") );
00420     }
00421 
00422 
00423 
00424     std::cout <<
00425 "Usage: kpilotTest [--port devicename] action\n\n"
00426 "Where action can be one of:\n"
00427 "\t--list - list the databases on the handheld\n"
00428 "\t--show (help | conduits | ...) - show configuration\n"
00429 "\t--check (help | user | ...) - check device\n"
00430 "\t--exec conduit - run a single conduit\n"
00431 "\t--backup - backup the device\n"
00432 "\t--restore - restore the device from backup\n"
00433     << std::endl;
00434     return 1;
00435 }
00436 
00437