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 }
00251
00252 void listConduits()
00253 {
00254 FUNCTIONSETUP;
00255
00256 KServiceTypeProfile::OfferList offers =
00257 KServiceTypeProfile::offers(CSL1("KPilotConduit"));
00258
00259
00260
00261
00262
00263 QValueListIterator < KServiceOffer > availList(offers.begin());
00264 while (availList != offers.end())
00265 {
00266 KSharedPtr < KService > o = (*availList).service();
00267
00268 std::cout << "File: " << o->desktopEntryName().latin1() << std::endl;
00269 std::cout << " Desc: " << o->name().latin1() << std::endl;
00270 if (!o->library().isEmpty())
00271 {
00272 std::cout << " Lib : "
00273 << o->library().latin1()
00274 << std::endl;
00275 }
00276
00277 ++availList;
00278 }
00279 }
00280
00281 int show( const QString &what )
00282 {
00283 FUNCTIONSETUP;
00284
00285 if ( "help" == what )
00286 {
00287 std::cout <<
00288 "Displays various bits of KPilot's internal settings. This\n"
00289 "does not require a device connection or a running KDE desktop.\n"
00290 "No change to data takes place. The following options are available\n"
00291 "for display:\n"
00292 "\thelp - displays this help\n"
00293 "\tconduits - displays the list of available conduits\n"
00294 "\tuser - displays the user name KPilot expects\n"
00295 "\tdevice - displays the device settings in KPilot\n"
00296 "\tdebug - displays internal numbers\n"
00297 << std::endl;
00298 return 0;
00299 }
00300
00301 if ( "conduits" == what )
00302 {
00303 listConduits();
00304 return 0;
00305 }
00306
00307 if ( "user" == what )
00308 {
00309 std::cout << "User: " << KPilotSettings::userName() << std::endl;
00310 return 0;
00311 }
00312
00313 if ( "device" == what )
00314 {
00315 std::cout << "Device: " << KPilotSettings::pilotDevice()
00316 << "\nSpeed: " << KPilotSettings::pilotSpeed()
00317 << "\nEncoding: " << KPilotSettings::encoding()
00318 << "\nQuirks: " << KPilotSettings::workarounds()
00319 << std::endl;
00320 return 0;
00321 }
00322
00323 if ( "debug" == what )
00324 {
00325 std::cout << "Debug: " << KPilotSettings::debug()
00326 << "\nConfig: " << KPilotSettings::configVersion()
00327 << std::endl;
00328 return 0;
00329 }
00330
00331 std::cerr << "Unknown --show argument, use --show help for help.\n";
00332 return 1;
00333 }
00334
00335 int main(int argc, char **argv)
00336 {
00337 #ifdef DEBUG
00338 debug_level = 1;
00339 #endif
00340 FUNCTIONSETUP;
00341 KAboutData about("kpilotTest",
00342 I18N_NOOP("KPilotTest"),
00343 KPILOT_VERSION,
00344 "KPilot Tester",
00345 KAboutData::License_GPL, "(C) 2001-2004, Adriaan de Groot");
00346 about.addAuthor("Adriaan de Groot",
00347 I18N_NOOP("KPilot Maintainer"),
00348 "groot@kde.org", "http://www.kpilot.org/");
00349
00350 KCmdLineArgs::init(argc, argv, &about);
00351 KCmdLineArgs::addCmdLineOptions(generalOptions,
00352 I18N_NOOP("General"));
00353 KCmdLineArgs::addCmdLineOptions(conduitOptions,
00354 I18N_NOOP("Conduit Actions"),"conduit");
00355
00356 KApplication::addCmdLineOptions();
00357
00358 KCmdLineArgs *p = KCmdLineArgs::parsedArgs();
00359
00360 bool needGUI = false;
00361
00362
00363 needGUI |= (p->isSet("check"));
00364 needGUI |= (p->isSet("exec"));
00365 needGUI |= (p->isSet("restore"));
00366
00367 KApplication a(needGUI,needGUI);
00368 #ifdef DEBUG
00369 KPilotConfig::getDebugLevel(p);
00370 DEBUGKPILOT << fname << "Created KApplication." << endl;
00371 #endif
00372
00373
00374 unsigned int count = 0;
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