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