PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/kdepim-4.3.4/kpilot/lib/syncAction.cc

#
C++ | 460 lines | 368 code | 65 blank | 27 comment | 38 complexity | 8437d33f724d0d2c1b454f1f37afba6a MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0
  1. /* KPilot
  2. **
  3. ** Copyright (C) 1998-2001 by Dan Pilone <dan@kpilot.org>
  4. ** Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
  5. ** Copyright (C) 2001 by Waldo Bastian (code in questionYesNo) <waldo@kpilot.org>
  6. **
  7. */
  8. /*
  9. ** This program is free software; you can redistribute it and/or modify
  10. ** it under the terms of the GNU Lesser General Public License as published by
  11. ** the Free Software Foundation; either version 2.1 of the License, or
  12. ** (at your option) any later version.
  13. **
  14. ** This program is distributed in the hope that it will be useful,
  15. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ** GNU Lesser General Public License for more details.
  18. **
  19. ** You should have received a copy of the GNU Lesser General Public License
  20. ** along with this program in a file called COPYING; if not, write to
  21. ** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  22. ** MA 02110-1301, USA.
  23. */
  24. /*
  25. ** Bug reports and questions can be sent to kde-pim@kde.org
  26. */
  27. #include "options.h"
  28. #include <time.h>
  29. #include <pi-socket.h>
  30. #include <pi-dlp.h>
  31. #include <QtCore/QDir>
  32. #include <QtCore/QFile>
  33. #include <QtCore/QFileInfo>
  34. #include <QtCore/QTimer>
  35. #include <QtGui/QCheckBox>
  36. #include <QtGui/QLabel>
  37. #include <QtGui/QLayout>
  38. #include <QtGui/QMessageBox>
  39. #include <QtGui/QStyle>
  40. #include <kdialog.h>
  41. #include <kglobal.h>
  42. #include <kstandarddirs.h>
  43. #include <kconfig.h>
  44. #include <kmessagebox.h>
  45. #include "syncAction.moc"
  46. #include "kpilotlibSettings.h"
  47. SyncAction::SyncAction(KPilotLink *p,
  48. const char * name)
  49. : QObject(p),
  50. fHandle(p),
  51. fParent(0L)
  52. {
  53. FUNCTIONSETUP;
  54. setObjectName(name);
  55. }
  56. SyncAction::SyncAction(KPilotLink *p,
  57. QWidget * visibleparent,
  58. const char *name) :
  59. QObject(p),
  60. fHandle(p),
  61. fParent(visibleparent)
  62. {
  63. FUNCTIONSETUP;
  64. setObjectName(name);
  65. }
  66. SyncAction::~SyncAction()
  67. {
  68. FUNCTIONSETUPL(5);
  69. }
  70. /* virtual */ QString SyncAction::statusString() const
  71. {
  72. FUNCTIONSETUP;
  73. QString s = CSL1("status=");
  74. s.append(QString::number(status()));
  75. return s;
  76. }
  77. /* slot */ void SyncAction::execConduit()
  78. {
  79. FUNCTIONSETUP;
  80. bool r = this->exec();
  81. DEBUGKPILOT << "Exec, " << objectName()
  82. << (r ? " is running" : " failed to start");
  83. if (!r)
  84. {
  85. emit logError(i18n("The conduit %1 could not be executed.",
  86. objectName()));
  87. delayDone();
  88. }
  89. }
  90. /* slot */ void SyncAction::delayedDoneSlot()
  91. {
  92. emit syncDone(this);
  93. }
  94. bool SyncAction::delayDone()
  95. {
  96. QTimer::singleShot(0,this,SLOT(delayedDoneSlot()));
  97. return true;
  98. }
  99. static struct
  100. {
  101. SyncAction::SyncMode::Mode mode;
  102. const char *name;
  103. } maps[] =
  104. {
  105. { SyncAction::SyncMode::eHotSync, "--hotsync" },
  106. { SyncAction::SyncMode::eFullSync, "--full" },
  107. { SyncAction::SyncMode::eCopyPCToHH, "--copyPCToHH" },
  108. { SyncAction::SyncMode::eCopyHHToPC, "--copyHHToPC" },
  109. { SyncAction::SyncMode::eBackup, "--backup" },
  110. { SyncAction::SyncMode::eRestore, "--restore" },
  111. { SyncAction::SyncMode::eFullSync, "--fullsync" },
  112. { SyncAction::SyncMode::eHotSync, (const char *)0 }
  113. }
  114. ;
  115. SyncAction::SyncMode::SyncMode(const QStringList &args) :
  116. fMode(eHotSync),
  117. fTest(args.contains("--test")),
  118. fLocal(args.contains("--local"))
  119. {
  120. FUNCTIONSETUP;
  121. DEBUGKPILOT << "args passed in: [" << args.join(",") << "]";
  122. int i = 0;
  123. while(maps[i].name)
  124. {
  125. if (args.contains(QString::fromLatin1(maps[i].name)))
  126. {
  127. fMode = maps[i].mode;
  128. break;
  129. }
  130. i++;
  131. }
  132. DEBUGKPILOT << "using 'i' of: [" << i << "]";
  133. if (!maps[i].name)
  134. {
  135. WARNINGKPILOT << "No mode set by arguments ("
  136. << args.join(",") << ") defaulting to HotSync.";
  137. }
  138. }
  139. SyncAction::SyncMode::SyncMode(Mode m, bool test, bool local) :
  140. fMode(m),
  141. fTest(test),
  142. fLocal(local)
  143. {
  144. if ( ((int)m<(int)eHotSync) || ((int)m>(int)eRestore) )
  145. {
  146. WARNINGKPILOT << "Mode value" << (int)m << " is illegal"
  147. ", defaulting to HotSync.";
  148. fMode = eHotSync;
  149. }
  150. }
  151. QStringList SyncAction::SyncMode::list() const
  152. {
  153. FUNCTIONSETUPL(3);
  154. QStringList l;
  155. int i=0;
  156. while(maps[i].name)
  157. {
  158. if ( fMode == maps[i].mode )
  159. {
  160. l.append(QString::fromLatin1(maps[i].name));
  161. break;
  162. }
  163. i++;
  164. }
  165. if ( !maps[i].name )
  166. {
  167. WARNINGKPILOT << "Mode" << fMode << " does not have a name.";
  168. l.append(QString::fromLatin1(maps[0].name));
  169. }
  170. if (isTest()) l.append(CSL1("--test"));
  171. if (isLocal()) l.append(CSL1("--local"));
  172. return l;
  173. }
  174. QVariantList SyncAction::SyncMode::variantList() const
  175. {
  176. FUNCTIONSETUPL(3);
  177. QVariantList l;
  178. int i=0;
  179. while(maps[i].name)
  180. {
  181. if ( fMode == maps[i].mode )
  182. {
  183. l.append(QString::fromLatin1(maps[i].name));
  184. break;
  185. }
  186. i++;
  187. }
  188. if ( !maps[i].name )
  189. {
  190. WARNINGKPILOT << "Mode" << fMode << " does not have a name.";
  191. l.append(QString::fromLatin1(maps[0].name));
  192. }
  193. if (isTest()) l.append(CSL1("--test"));
  194. if (isLocal()) l.append(CSL1("--local"));
  195. return l;
  196. }
  197. /* static */ QString SyncAction::SyncMode::name(SyncAction::SyncMode::Mode e)
  198. {
  199. switch(e)
  200. {
  201. case eHotSync : return i18n("HotSync");
  202. case eFullSync : return i18n("Full Synchronization");
  203. case eCopyPCToHH : return i18n("Copy PC to Handheld");
  204. case eCopyHHToPC : return i18n("Copy Handheld to PC");
  205. case eBackup : return i18n("Backup");
  206. case eRestore : return i18n("Restore From Backup");
  207. }
  208. return CSL1("<unknown>");
  209. }
  210. QString SyncAction::SyncMode::name() const
  211. {
  212. QString s = name(fMode);
  213. if (isTest())
  214. {
  215. s.append(CSL1(" [%1]").arg(i18n("Test Sync")));
  216. }
  217. if (isLocal())
  218. {
  219. s.append(CSL1(" [%1]").arg(i18n("Local Sync")));
  220. }
  221. return s;
  222. }
  223. bool SyncAction::SyncMode::setMode(int mode)
  224. {
  225. // Resets test and local flags too
  226. fTest = fLocal = false;
  227. if ( (mode>0) && (mode<=eRestore) )
  228. {
  229. fMode = (SyncAction::SyncMode::Mode) mode;
  230. return true;
  231. }
  232. else
  233. {
  234. WARNINGKPILOT << "Bad sync mode" << mode << " requested.";
  235. fMode = eHotSync;
  236. return false;
  237. }
  238. }
  239. bool SyncAction::SyncMode::setMode(SyncAction::SyncMode::Mode m)
  240. {
  241. int i=0;
  242. while ( maps[i].name )
  243. {
  244. if ( maps[i].mode == m )
  245. {
  246. fMode = m;
  247. return true;
  248. }
  249. i++;
  250. }
  251. WARNINGKPILOT << "Bad sync mode" << m << " requested.";
  252. fMode = eHotSync;
  253. return false;
  254. }
  255. void SyncAction::startTickle(unsigned timeout)
  256. {
  257. FUNCTIONSETUP;
  258. if (!deviceLink())
  259. {
  260. WARNINGKPILOT << "Trying to tickle without a device.";
  261. }
  262. else
  263. {
  264. connect(deviceLink(),SIGNAL(timeout()),this,SIGNAL(timeout()));
  265. deviceLink()->startTickle(timeout);
  266. }
  267. }
  268. void SyncAction::stopTickle()
  269. {
  270. FUNCTIONSETUP;
  271. if (!deviceLink())
  272. {
  273. WARNINGKPILOT << "Trying to tickle without a device.";
  274. }
  275. else
  276. {
  277. disconnect(deviceLink(),SIGNAL(timeout()),this,SIGNAL(timeout()));
  278. deviceLink()->stopTickle();
  279. }
  280. }
  281. int SyncAction::questionYesNo(const QString & text,
  282. const QString & caption,
  283. const QString & key,
  284. unsigned timeout,
  285. const QString & yes,
  286. const QString &no )
  287. {
  288. FUNCTIONSETUP;
  289. bool checkboxReturn = false;
  290. int r;
  291. KMessageBox::ButtonCode result;
  292. if (!key.isEmpty())
  293. {
  294. if (!KMessageBox::shouldBeShownYesNo(key,result))
  295. {
  296. return result;
  297. }
  298. }
  299. KDialog *dialog = new KDialog(fParent);
  300. dialog->setCaption(caption.isNull()? i18n("Question") : caption);
  301. dialog->setButtons(KDialog::Yes | KDialog::No);
  302. dialog->setDefaultButton(KDialog::Yes);
  303. dialog->setEscapeButton(KDialog::Cancel);
  304. dialog->setObjectName("questionYesNo");
  305. dialog->showButtonSeparator(true);
  306. dialog->setButtonText(KDialog::Yes, yes.isEmpty() ?
  307. KStandardGuiItem::yes().text() : yes);
  308. dialog->setButtonText(KDialog::No, no.isEmpty() ?
  309. KStandardGuiItem::no().text() : no);
  310. if ( (timeout > 0) && ( deviceLink() ) )
  311. {
  312. QObject::connect(deviceLink(), SIGNAL(timeout()),
  313. dialog, SLOT(slotCancel()));
  314. startTickle(timeout);
  315. }
  316. r = (KMessageBox::ButtonCode) KMessageBox::createKMessageBox(dialog,
  317. QMessageBox::Question,
  318. text,
  319. QStringList(),
  320. (key.isEmpty() ? QString::null : i18n("&Do not ask again")), //krazy:exclude=nullstrassign for old broken gcc
  321. &checkboxReturn,
  322. 0);
  323. switch(r)
  324. {
  325. case KDialog::Yes : result=KMessageBox::Yes ; break;
  326. case KDialog::No : result=KMessageBox::No; break;
  327. case KDialog::Cancel : result=KMessageBox::Cancel; break;
  328. default : break;
  329. }
  330. stopTickle();
  331. if (!key.isEmpty() && checkboxReturn)
  332. {
  333. KMessageBox::saveDontShowAgainYesNo(key,result);
  334. }
  335. return result;
  336. }
  337. int SyncAction::questionYesNoCancel(const QString & text,
  338. const QString & caption,
  339. const QString & key,
  340. unsigned timeout,
  341. const QString &yes,
  342. const QString &no)
  343. {
  344. FUNCTIONSETUP;
  345. bool checkboxReturn = false;
  346. int r;
  347. KMessageBox::ButtonCode result;
  348. if (!key.isEmpty())
  349. {
  350. if (!KMessageBox::shouldBeShownYesNo(key,result))
  351. {
  352. if (result != KMessageBox::Cancel)
  353. {
  354. return result;
  355. }
  356. }
  357. }
  358. KDialog *dialog = new KDialog(fParent);
  359. dialog->setCaption(caption.isNull()? i18n("Question") : caption);
  360. dialog->setButtons(KDialog::Yes | KDialog::No | KDialog::Cancel);
  361. dialog->setDefaultButton(KDialog::Yes);
  362. dialog->setEscapeButton(KDialog::Cancel);
  363. dialog->setObjectName("questionYesNoCancel");
  364. dialog->showButtonSeparator(true);
  365. dialog->setButtonText(KDialog::Yes, yes.isEmpty() ?
  366. KStandardGuiItem::yes().text() : yes);
  367. dialog->setButtonText(KDialog::No, no.isEmpty() ?
  368. KStandardGuiItem::no().text() : no);
  369. if ( (timeout > 0) && (deviceLink()) )
  370. {
  371. QObject::connect(deviceLink(), SIGNAL(timeout()),
  372. dialog, SLOT(slotCancel()));
  373. startTickle(timeout);
  374. }
  375. r = KMessageBox::createKMessageBox(dialog,
  376. QMessageBox::Question,
  377. text,
  378. QStringList(),
  379. (key.isEmpty() ? QString::null : i18n("&Do not ask again")), //krazy:exclude=nullstrassign for old broken gcc
  380. &checkboxReturn,
  381. 0);
  382. switch(r)
  383. {
  384. case KDialog::Yes : result=KMessageBox::Yes ; break;
  385. case KDialog::No : result=KMessageBox::No; break;
  386. case KDialog::Cancel : result=KMessageBox::Cancel; break;
  387. default : break;
  388. }
  389. stopTickle();
  390. if (!key.isEmpty() && checkboxReturn)
  391. {
  392. KMessageBox::saveDontShowAgainYesNo(key,result);
  393. }
  394. return result;
  395. }