summaryrefslogtreecommitdiffabout
path: root/pwmanager/pwmanager/pwminit.cpp
Unidiff
Diffstat (limited to 'pwmanager/pwmanager/pwminit.cpp') (more/less context) (show whitespace changes)
-rw-r--r--pwmanager/pwmanager/pwminit.cpp617
1 files changed, 617 insertions, 0 deletions
diff --git a/pwmanager/pwmanager/pwminit.cpp b/pwmanager/pwmanager/pwminit.cpp
new file mode 100644
index 0000000..b0a78c2
--- a/dev/null
+++ b/pwmanager/pwmanager/pwminit.cpp
@@ -0,0 +1,617 @@
1/***************************************************************************
2 * *
3 * copyright (C) 2004 by Michael Buesch *
4 * email: mbuesch@freenet.de *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License version 2 *
8 * as published by the Free Software Foundation. *
9 * *
10 ***************************************************************************/
11
12/***************************************************************************
13 * copyright (C) 2004 by Ulf Schenk
14 * This file is originaly based on version 1.0.1 of pwmanager
15 * and was modified to run on embedded devices that run microkde
16 *
17 * $Id$
18 **************************************************************************/
19
20#include "pwminit.h"
21#include "configuration.h"
22#include "randomizer.h"
23
24#ifndef PWM_EMBEDDED
25#include "selftest.h"
26#endif
27
28#include "pwm.h"
29#include "pwmexception.h"
30#include "pwmtray.h"
31#include "pwmdoc.h"
32
33#ifdef CONFIG_KWALLETIF
34# include "kwalletemu.h"
35#endif // CONFIG_KWALLETIF
36#ifdef CONFIG_KEYCARD
37# include "pwmkeycard.h"
38#endif // CONFIG_KEYCARD
39
40#include <qmessagebox.h>
41
42#include <kmessagebox.h>
43#ifndef PWM_EMBEDDED
44#include <kcmdlineargs.h>
45#include <kwin.h>
46#include <dcopclient.h>
47#endif
48
49#include <kapplication.h>
50#include <kiconloader.h>
51
52#include <signal.h>
53
54static PwMInit *sig_init_pointer;
55static NOREGPARM void sig_handler(int signum)
56{
57 switch (signum) {
58 case SIGINT:
59 case SIGTERM:
60 sig_init_pointer->shutdownApp(20 + signum);
61 break;
62 default:
63 printDebug(string("unhandled signal ")
64 + tostr(signum));
65 }
66}
67
68
69
70
71PwMInit::PwMInit(PwMApplication *_app)
72 : runStatus (unknown)
73 , _curWidget (0)
74 , _dcopClient (0)
75 , _kwalletEmu (0)
76 , _keycard (0)
77 , _tray (0)
78{
79 sig_init_pointer = this;
80 app = _app;
81}
82
83PwMInit::~PwMInit()
84{
85#ifndef PWM_EMBEDDED
86 SelfTest::cancel();
87 // close all open mainwnds
88 QValueList<PwM *>::iterator i = _mainWndList.begin(),
89 end = _mainWndList.end();
90
91#else
92 // close all open mainwnds
93 QValueList<PwM *>::Iterator i = _mainWndList.begin(),
94 end = _mainWndList.end();
95#endif
96 while (i != end) {
97 disconnect(*i, SIGNAL(closed(PwM *)),
98 this, SLOT(mainWndClosed(PwM *)));
99 delete *i;
100 ++i;
101 }
102 _mainWndList.clear();
103 // close all remaining open documents
104 PwMDocList *_dl = PwMDoc::getOpenDocList();
105 vector<PwMDocList::listItem> dl = *(_dl->getList());
106 vector<PwMDocList::listItem>::iterator i2 = dl.begin(),
107 end2 = dl.end();
108 while (i2 != end2) {
109 delete (*i2).doc;
110 ++i2;
111 }
112
113#ifdef CONFIG_KWALLETIF
114 delete_ifnot_null(_kwalletEmu);
115#endif // CONFIG_KWALLETIF
116#ifdef CONFIG_KEYCARD
117 delete_ifnot_null(_keycard);
118#endif // CONFIG_KEYCARD
119 delete_ifnot_null(_tray);
120
121 Randomizer::cleanup();
122 Configuration::cleanup();
123}
124
125void PwMInit::initializeApp()
126{
127 PWM_ASSERT(runStatus == unknown);
128 runStatus = init;
129 initPosixSignalHandler();
130 Randomizer::init();
131 Configuration::init();
132 initDCOP();
133 initKWalletEmu();
134 initKeycard();
135 initTray();
136 handleCmdLineArgs();
137
138 bool openDeeplocked = false;
139 if (conf()->confGlobAutostartDeepLocked() ||
140 savedCmd.open_deeplocked)
141 openDeeplocked = true;
142 if (conf()->confWndAutoMinimizeOnStart() ||
143 savedCmd.minToTray) {
144 PwMDoc *newDoc = createDoc();
145 if (!newDoc->openDocUi(newDoc,
146 conf()->confGlobAutoStart(),
147 openDeeplocked)) {
148 delete newDoc;
149 }
150 } else {
151 createMainWnd(conf()->confGlobAutoStart(),
152 openDeeplocked,
153 true,
154 0,
155 savedCmd.minimized);
156 }
157
158 runStatus = running;
159}
160
161void PwMInit::shutdownApp(int exitStatus)
162{
163 printDebug(string("PwMInit::shutdownApp(")
164 + tostr(exitStatus) + ") called.");
165 PWM_ASSERT((runStatus == running) || (runStatus == init));
166 runStatus = shutdown;
167 QApplication::exit(exitStatus);
168 /* The destructor of PwMInit is called when control
169 * leaves main()
170 */
171}
172
173void PwMInit::initPosixSignalHandler()
174{
175 signal(SIGINT, sig_handler);
176 signal(SIGTERM, sig_handler);
177}
178
179void PwMInit::initDCOP()
180{
181#ifndef PWM_EMBEDDED
182 _dcopClient = app->dcopClient();
183 _dcopClient->setNotifications(true);
184#else
185 qDebug("PwMInit::initDCOP() has to be implemented");
186#endif
187
188}
189
190void PwMInit::initKWalletEmu(bool forceDisable, bool forceReload)
191{
192#ifdef CONFIG_KWALLETIF
193 if (!conf()->confGlobKwalletEmu() ||
194 forceDisable) {
195 delete_ifnot_null(_kwalletEmu);
196 return;
197 }
198 try {
199 if (_kwalletEmu && forceReload)
200 delete_and_null(_kwalletEmu);
201 if (!_kwalletEmu)
202 _kwalletEmu = new KWalletEmu(this);
203 } catch (PwMException e) {
204 string errMsg("initializing KWallet emulation failed. ID: ");
205 errMsg += tostr(static_cast<int>(e.getId()));
206 errMsg += " err-message: ";
207 errMsg += e.getMessage();
208 printWarn(errMsg);
209 return;
210 }
211#else // CONFIG_KWALLETIF
212 PARAM_UNUSED(forceDisable);
213 PARAM_UNUSED(forceReload);
214#endif // CONFIG_KWALLETIF
215}
216
217void PwMInit::initKeycard()
218{
219#ifdef CONFIG_KEYCARD
220 PWM_ASSERT(!_keycard);
221 _keycard = new PwMKeyCard(this);
222#endif // CONFIG_KEYCARD
223}
224
225void PwMInit::initTray()
226{
227#ifdef PWM_EMBEDDED
228 //US ENH : embedded version does not support a tray
229 return;
230#endif
231
232 if (!conf()->confGlobTray()) {
233 if (!_tray)
234 return;
235 _tray->hide();
236 delete_and_null(_tray);
237 return;
238 }
239 if (_tray)
240 return;
241 _tray = new PwMTray(this);
242 connect(_tray, SIGNAL(quitSelected()),
243 this, SLOT(removeTrayAndQuit()));
244 connect(_tray, SIGNAL(closed(PwMTray *)),
245 this, SLOT(trayIconClosed(PwMTray *)));
246 KIconLoader icons;
247#ifndef PWM_EMBEDDED
248 _tray->setPixmap(icons.loadIcon(PACKAGE_NAME, KIcon::Small));
249#endif
250 _tray->show();
251 // connect the signals of all open documents.
252 const vector<PwMDocList::listItem> *dl = PwMDoc::getOpenDocList()->getList();
253 vector<PwMDocList::listItem>::const_iterator i = dl->begin(),
254 end = dl->end();
255 while (i != end) {
256 _tray->connectDocToTray((*i).doc);
257 ++i;
258 }
259}
260
261void PwMInit::removeTrayAndQuit()
262{
263 PWM_ASSERT(_tray);
264 // _tray is deleted in ~PwMInit
265 shutdownApp(0);
266}
267
268PwM * PwMInit::createMainWnd(const QString &loadFile,
269 bool loadFileDeepLocked,
270 bool virginity,
271 PwMDoc *doc,
272 bool minimized)
273{
274 PwM *newWnd;
275 if (!doc)
276 doc = createDoc();
277 newWnd = new PwM(this, doc, virginity);
278#ifndef PWM_EMBEDDED
279 _mainWndList.push_back(newWnd);
280#else
281 _mainWndList.append(newWnd);
282#endif
283 connect(newWnd, SIGNAL(closed(PwM *)),
284 this, SLOT(mainWndClosed(PwM *)));
285 connect(newWnd, SIGNAL(gotFocus(PwM *)),
286 this, SLOT(setCurWidget(PwM *)));
287 connect(newWnd, SIGNAL(lostFocus(PwM *)),
288 this, SLOT(resetCurWidget()));
289
290 //US ENH
291#ifndef PWM_EMBEDDED
292 if (minimized)
293 newWnd->showMinimized();
294 else
295 newWnd->show();
296
297#else //PWM_EMBEDDED
298
299#ifndef DESKTOP_VERSION
300 app->showMainWidget( newWnd );
301#else //DESKTOP_VERSION
302 app->setMainWidget( newWnd );
303 newWnd->resize (640, 480 );
304 newWnd->show();
305#endif //DESKTOP_VERSION
306
307#endif //PWM_EMBEDDED
308
309 if (loadFile != QString::null &&
310 loadFile != "") {
311 newWnd->openDoc(loadFile, loadFileDeepLocked);
312 }
313 return newWnd;
314}
315
316PwMDoc * PwMInit::createDoc()
317{
318 PwMDoc *doc = new PwMDoc(this);
319#ifdef CONFIG_KEYCARD
320 doc->setPwMKeyCard(keycard());
321#endif
322#ifdef CONFIG_KWALLETIF
323 if (kwalletEmu())
324 kwalletEmu()->connectDocSignals(doc);
325#endif
326
327 if (_tray)
328 _tray->connectDocToTray(doc);
329
330 return doc;
331
332}
333
334void PwMInit::mainWndClosed(PwM *wnd)
335{
336 bool doMinimizeToTray = false;
337 bool doDeleteDoc = false;
338#ifndef PWM_EMBEDDED
339 dcopClient()->suspend();
340 dcopClient()->setAcceptCalls(false);
341#endif
342again:
343
344 if (wnd->isForceMinimizeToTray()) {
345 if (unlikely(!_tray)) {
346 /* This should not happen! If we set forceMinimizeToTray ,
347 * we must be sure that _tray exists.
348 */
349 BUG();
350 wnd->setForceMinimizeToTray(false);
351 goto again;
352 }
353 doMinimizeToTray = true;
354 } else {
355 // Ask to minimize to tray. If not, delete doc.
356 if (_tray &&
357 runStatus != shutdown &&
358 !wnd->isForceQuit() &&
359 !wnd->curDoc()->isDeleted()) {
360 if (conf()->confWndClose())
361 doDeleteDoc = true;
362 else
363 doMinimizeToTray = true;
364 } else {
365 doDeleteDoc = true;
366 }
367 }
368
369 if (doMinimizeToTray) {
370
371 PWM_ASSERT(_tray);
372 int mmlock = conf()->confGlobMinimizeLock();
373 switch (mmlock) {
374 case 0: // don't lock anything
375 break;
376 case 1: // normal lock
377 wnd->curDoc()->lockAll(true);
378 break;
379 case 2: // deep-lock
380 wnd->curDoc()->deepLock();
381 break;
382 default:
383 WARN();
384 }
385 } else if (doDeleteDoc) {
386 if (!wnd->curDoc()->tryDelete()) {
387 /* We failed deleting the doc,
388 * so open a new window with it, again.
389 */
390 createMainWnd(QString::null, false,
391 false, wnd->curDoc());
392 }
393 }
394#ifndef PWM_EMBEDDED
395 // find the closed window in the "mainWndList" and delete it.
396 QValueList<PwM *>::iterator i = _mainWndList.begin(),
397 end = _mainWndList.end();
398#else
399 // find the closed window in the "mainWndList" and delete it.
400 QValueList<PwM *>::Iterator i = _mainWndList.begin(),
401 end = _mainWndList.end();
402#endif
403 while (i != end) {
404 if (*i == wnd) {
405#ifndef PWM_EMBEDDED
406 _mainWndList.erase(i);
407#else
408 _mainWndList.remove(i);
409#endif
410 goto out_success;
411 }
412 ++i;
413 }
414 BUG();
415out_success:
416#ifndef PWM_EMBEDDED
417 if (!_mainWndList.size())
418#else
419 if (!_mainWndList.count())
420#endif
421
422 {
423 /* If there's no main window and no tray icon
424 * left, we have no user interface, so we can
425 * shut down the application.
426 */
427 if (!_tray) {
428#ifndef PWM_EMBEDDED
429 dcopClient()->setAcceptCalls(true);
430 dcopClient()->resume();
431#endif
432 shutdownApp(0);
433 return;
434 }
435 /* There is no widget left, so set
436 * _curWidget to 0
437 */
438 resetCurWidget();
439 }
440#ifndef PWM_EMBEDDED
441 dcopClient()->setAcceptCalls(true);
442 dcopClient()->resume();
443#endif
444}
445
446void PwMInit::trayIconClosed(PwMTray *tray)
447{
448 if (runStatus != running)
449 return;
450 PARAM_UNUSED(tray);
451 PWM_ASSERT(tray == _tray);
452 /* If there's no main wnd left we have to
453 * shutdown the app (same as in mainWndClosed())
454 */
455#ifndef PWM_EMBEDDED
456 if (!_mainWndList.size())
457 shutdownApp(0);
458#else
459 if (!_mainWndList.count())
460 shutdownApp(0);
461#endif
462}
463
464void PwMInit::handleCmdLineArgs(bool initial)
465{
466#ifndef PWM_EMBEDDED
467 KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
468 PWM_ASSERT(args);
469 int i, numArgs = args->count();
470 const char *curArg;
471
472 // read all cmdline options
473 savedCmd.open_deeplocked = args->isSet("open-deeplocked");
474 savedCmd.minimized = args->isSet("minimized");
475 savedCmd.minToTray = args->isSet("mintray");
476 savedCmd.skipSelfTest = args->isSet("skip-self-test");
477 if (savedCmd.minimized &&
478 savedCmd.minToTray) {
479 printInfo(i18n("Commandline option \"--minimized\" and "
480 "\"--mintray\" selected. These are incompatible. "
481 "\"--mintray\" will be selected.").latin1());
482 }
483 /* Iterate through all non-option arguments.
484 * Every non-option arg is a filename to open.
485 */
486 for (i = 0; i < numArgs; ++i) {
487 curArg = args->arg(i);
488 PWM_ASSERT(curArg);
489 if (savedCmd.minToTray) {
490 PwMDoc *newDoc = createDoc();
491 if (!newDoc->openDocUi(newDoc,
492 curArg,
493 savedCmd.open_deeplocked)) {
494 delete newDoc;
495 }
496 } else {
497 PwM *newInstance = createMainWnd(QString::null,
498 false,
499 true,
500 0,
501 savedCmd.minimized);
502 PwMDoc *newDoc = newInstance->openDoc(curArg,
503 savedCmd.open_deeplocked);
504 if (!newDoc) {
505 newInstance->setForceQuit(true);
506 delete_and_null(newInstance);
507 }
508 }
509 }
510
511 if (savedCmd.minToTray) {
512 minimizeAllMainWnd(true);
513 } else if (savedCmd.minimized) {
514 minimizeAllMainWnd(false);
515 }
516 if (!savedCmd.skipSelfTest && initial) {
517 SelfTest::schedule();
518 }
519 args->clear();
520#endif
521}
522
523void PwMInit::minimizeAllMainWnd(bool toTray)
524{
525#ifndef PWM_EMBEDDED
526 if (!_mainWndList.size())
527 return;
528#else
529 if (!_mainWndList.count())
530 return;
531#endif
532 const QValueList<PwM *> *ml = mainWndList();
533#ifndef PWM_EMBEDDED
534 QValueList<PwM *>::const_iterator it = ml->begin(),
535 end = ml->end();
536#else
537 QValueList<PwM *>::ConstIterator it = ml->begin(),
538 end = ml->end();
539#endif
540 PwM *wnd;
541 if (toTray && _tray) {
542 /* minimize to tray.
543 * close all mainWnd.
544 */
545 while (it != end) {
546 wnd = *it;
547 wnd->setForceMinimizeToTray(true);
548 wnd->close_slot();
549 ++it;
550 }
551 } else {
552 // normal minimize
553 while (it != end) {
554 wnd = *it;
555 wnd->hide();
556 wnd->showMinimized();
557 ++it;
558 }
559 }
560}
561
562#ifdef PWM_EMBEDDED
563
564#ifndef DESKTOP_VERSION
565
566PwMApplication::PwMApplication(int & argc, char ** argv)
567 : QPEApplication( argc, argv )
568 , init (0)
569{
570 this->setKeepRunning ();
571}
572
573PwMApplication::~PwMApplication()
574{
575 delete_ifnot_null(init);
576}
577#else //DESKTOP_VERSION
578
579PwMApplication::PwMApplication(int & argc, char ** argv)
580 : QApplication( argc, argv )
581 , init (0)
582{
583 setStyle( new QPlatinumStyle ());
584 QString hdir = QDir::homeDirPath();
585 // there is a bug when creating dirs for WIN 98
586 // it is difficult to fix, because we have no WIN 98 runnung
587 // such that we try it to create the dirs at startup here
588 if ( hdir == "C:\\" )
589 {
590 // win 98 or ME
591 QDir app_dir;
592 if ( !app_dir.exists("C:\\kdepim") )
593 app_dir.mkdir ("C:\\kdepim");
594 if ( !app_dir.exists("C:\\kdepim\\apps") )
595 app_dir.mkdir ("C:\\kdepim\\apps");
596 if ( !app_dir.exists("C:\\kdepim\\config") )
597 app_dir.mkdir ("C:\\kdepim\\config");
598 if ( !app_dir.exists("C:\\kdepim\\apps\\pwmanager") )
599 app_dir.mkdir ("C:\\kdepim\\apps\\pwmanager");
600 }
601}
602
603PwMApplication::~PwMApplication()
604{
605 delete_ifnot_null(init);
606}
607
608#endif //DESKTOP_VERSION
609
610#endif //PWM_EMBEDDED
611
612
613
614
615#ifndef PWM_EMBEDDED
616#include "pwminit.moc"
617#endif