summaryrefslogtreecommitdiffabout
path: root/pwmanager/pwmanager/pwm.cpp
Unidiff
Diffstat (limited to 'pwmanager/pwmanager/pwm.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--pwmanager/pwmanager/pwm.cpp1192
1 files changed, 1192 insertions, 0 deletions
diff --git a/pwmanager/pwmanager/pwm.cpp b/pwmanager/pwmanager/pwm.cpp
new file mode 100644
index 0000000..0e57650
--- a/dev/null
+++ b/pwmanager/pwmanager/pwm.cpp
@@ -0,0 +1,1192 @@
1/***************************************************************************
2 * *
3 * copyright (C) 2003, 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 <klocale.h>
21#include <klistview.h>
22#include <ktoolbar.h>
23#include <kfiledialog.h>
24#include <kiconloader.h>
25#include <kmessagebox.h>
26
27#ifndef PWM_EMBEDDED
28#include <kmenubar.h>
29#include <kstatusbar.h>
30#include <dcopclient.h>
31#else
32#include <qmenubar.h>
33#endif
34
35#include <qpixmap.h>
36#include <qcheckbox.h>
37#include <qspinbox.h>
38#include <qlineedit.h>
39#include <qfileinfo.h>
40#include <qclipboard.h>
41
42
43#include <stdio.h>
44
45#include "pwm.h"
46#include "pwminit.h"
47#include "configwndimpl.h"
48#include "pwmprint.h"
49#include "addentrywndimpl.h"
50#include "globalstuff.h"
51#include "findwndimpl.h"
52#include "configuration.h"
53
54#ifdef CONFIG_KWALLETIF
55# include "kwalletif.h"
56# include "kwalletemu.h"
57#endif
58#ifdef CONFIG_KEYCARD
59# include "pwmkeycard.h"
60#endif
61
62
63 #define DEFAULT_SIZE (QSize(700, 400))
64
65// Button IDs for "file" popup menu
66enum {
67 BUTTON_POPUP_FILE_NEW = 0,
68 BUTTON_POPUP_FILE_OPEN,
69 BUTTON_POPUP_FILE_CLOSE,
70 BUTTON_POPUP_FILE_SAVE,
71 BUTTON_POPUP_FILE_SAVEAS,
72 BUTTON_POPUP_FILE_EXPORT,
73 BUTTON_POPUP_FILE_IMPORT,
74 BUTTON_POPUP_FILE_PRINT,
75 BUTTON_POPUP_FILE_QUIT
76};
77// Button IDs for "manage" popup menu
78enum {
79 BUTTON_POPUP_MANAGE_ADD = 0,
80 BUTTON_POPUP_MANAGE_EDIT,
81 BUTTON_POPUP_MANAGE_DEL,
82 BUTTON_POPUP_MANAGE_CHANGEMP
83};
84// Button IDs for chipcard popup menu
85enum {
86#ifdef CONFIG_KEYCARD
87 BUTTON_POPUP_CHIPCARD_GENNEW = 0,
88 BUTTON_POPUP_CHIPCARD_DEL,
89 BUTTON_POPUP_CHIPCARD_READID,
90 BUTTON_POPUP_CHIPCARD_SAVEBACKUP,
91 BUTTON_POPUP_CHIPCARD_REPLAYBACKUP
92#else // CONFIG_KEYCARD
93 BUTTON_POPUP_CHIPCARD_NO = 0
94#endif // CONFIG_KEYCARD
95};
96// Button IDs for "view" popup menu
97enum {
98 BUTTON_POPUP_VIEW_FIND = 0,
99 BUTTON_POPUP_VIEW_LOCK,
100 BUTTON_POPUP_VIEW_DEEPLOCK,
101 BUTTON_POPUP_VIEW_UNLOCK
102};
103// Button IDs for "options" popup menu
104enum {
105 BUTTON_POPUP_OPTIONS_CONFIG = 0
106};
107// Button IDs for "export" popup menu (in "file" popup menu)
108enum {
109 BUTTON_POPUP_EXPORT_TEXT = 0,
110 BUTTON_POPUP_EXPORT_GPASMAN
111#ifdef CONFIG_KWALLETIF
112 ,BUTTON_POPUP_EXPORT_KWALLET
113#endif
114};
115// Button IDs for "import" popup menu (in "file" popup menu)
116enum {
117 BUTTON_POPUP_IMPORT_TEXT = 0,
118 BUTTON_POPUP_IMPORT_GPASMAN
119#ifdef CONFIG_KWALLETIF
120 ,BUTTON_POPUP_IMPORT_KWALLET
121#endif
122};
123// Button IDs for toolbar
124enum {
125 BUTTON_TOOL_NEW = 0,
126 BUTTON_TOOL_OPEN,
127 BUTTON_TOOL_SAVE,
128 BUTTON_TOOL_SAVEAS,
129 BUTTON_TOOL_PRINT,
130 BUTTON_TOOL_ADD,
131 BUTTON_TOOL_EDIT,
132 BUTTON_TOOL_DEL,
133 BUTTON_TOOL_FIND,
134 BUTTON_TOOL_LOCK,
135 BUTTON_TOOL_DEEPLOCK,
136 BUTTON_TOOL_UNLOCK
137};
138
139
140PwM::PwM(PwMInit *_init, PwMDoc *doc,
141 bool virginity,
142 QWidget *parent, const char *name)
143 : KMainWindow(parent, name)
144 , forceQuit (false)
145 , forceMinimizeToTray (false)
146{
147 init = _init;
148 connect(doc, SIGNAL(docClosed(PwMDoc *)),
149 this, SLOT(docClosed(PwMDoc *)));
150 initMenubar();
151 initToolbar();
152 initMetrics();
153 setVirgin(virginity);
154 setFocusPolicy(QWidget::WheelFocus);
155#ifndef PWM_EMBEDDED
156 statusBar()->show();
157#endif
158 view = makeNewListView(doc);
159 setCentralWidget(view);
160 updateCaption();
161 showStatMsg(i18n("Ready."));
162}
163
164PwM::~PwM()
165{
166 disconnect(curDoc(), SIGNAL(docClosed(PwMDoc *)),
167 this, SLOT(docClosed(PwMDoc *)));
168 conf()->confWndMainWndSize(size());
169 emit closed(this);
170 delete view;
171}
172
173void PwM::initMenubar()
174{
175 KIconLoader icons;
176
177 filePopup = new KPopupMenu(this);
178 importPopup = new KPopupMenu(filePopup);
179 exportPopup = new KPopupMenu(filePopup);
180 managePopup = new KPopupMenu(this);
181#ifdef CONFIG_KEYCARD
182 chipcardPopup = new KPopupMenu(this);
183#endif // CONFIG_KEYCARD
184 viewPopup = new KPopupMenu(this);
185 optionsPopup = new KPopupMenu(this);
186
187// "file" popup menu
188 filePopup->insertItem(QIconSet(icons.loadIcon("filenew", KIcon::Small)),
189 i18n("&New"), this,
190 SLOT(new_slot()), 0, BUTTON_POPUP_FILE_NEW);
191 filePopup->insertItem(QIconSet(icons.loadIcon("fileopen", KIcon::Small)),
192 i18n("&Open"), this,
193 SLOT(open_slot()), 0, BUTTON_POPUP_FILE_OPEN);
194 filePopup->insertItem(QIconSet(icons.loadIcon("fileclose", KIcon::Small)),
195 i18n("&Close"), this,
196 SLOT(close_slot()), 0, BUTTON_POPUP_FILE_CLOSE);
197 filePopup->insertSeparator();
198 filePopup->insertItem(QIconSet(icons.loadIcon("filesave", KIcon::Small)),
199 i18n("&Save"), this,
200 SLOT(save_slot()), 0, BUTTON_POPUP_FILE_SAVE);
201 filePopup->insertItem(QIconSet(icons.loadIcon("filesaveas", KIcon::Small)),
202 i18n("Save &as..."),
203 this, SLOT(saveAs_slot()), 0,
204 BUTTON_POPUP_FILE_SAVEAS);
205 filePopup->insertSeparator();
206 // "file/export" popup menu
207 exportPopup->insertItem(i18n("&Text-file..."), this,
208 SLOT(exportToText()), 0, BUTTON_POPUP_EXPORT_TEXT);
209 exportPopup->insertItem(i18n("&Gpasman / Kpasman ..."), this,
210 SLOT(exportToGpasman()), 0, BUTTON_POPUP_EXPORT_GPASMAN);
211#ifdef CONFIG_KWALLETIF
212 exportPopup->insertItem(i18n("&KWallet..."), this,
213 SLOT(exportToKWallet()), 0, BUTTON_POPUP_EXPORT_KWALLET);
214#endif
215 filePopup->insertItem(QIconSet(icons.loadIcon("fileexport", KIcon::Small)),
216 i18n("E&xport"), exportPopup,
217 BUTTON_POPUP_FILE_EXPORT);
218 // "file/import" popup menu
219 importPopup->insertItem(i18n("&Text-file..."), this,
220 SLOT(importFromText()), 0, BUTTON_POPUP_IMPORT_TEXT);
221 importPopup->insertItem(i18n("&Gpasman / Kpasman ..."), this,
222 SLOT(importFromGpasman()), 0, BUTTON_POPUP_IMPORT_GPASMAN);
223#ifdef CONFIG_KWALLETIF
224 importPopup->insertItem(i18n("&KWallet..."), this,
225 SLOT(importKWallet()), 0, BUTTON_POPUP_IMPORT_KWALLET);
226#endif
227 filePopup->insertItem(QIconSet(icons.loadIcon("fileimport", KIcon::Small)),
228 i18n("I&mport"), importPopup,
229 BUTTON_POPUP_FILE_IMPORT);
230 filePopup->insertSeparator();
231 filePopup->insertItem(QIconSet(icons.loadIcon("fileprint", KIcon::Small)),
232 i18n("&Print..."), this,
233 SLOT(print_slot()), 0, BUTTON_POPUP_FILE_PRINT);
234 filePopup->insertSeparator();
235 filePopup->insertItem(QIconSet(icons.loadIcon("exit", KIcon::Small)),
236 i18n("&Quit"), this,
237 SLOT(quitButton_slot()), 0, BUTTON_POPUP_FILE_QUIT);
238 menuBar()->insertItem(i18n("&File"), filePopup);
239// "manage" popup menu
240 managePopup->insertItem(QIconSet(icons.loadIcon("pencil", KIcon::Small)),
241 i18n("&Add password"), this,
242 SLOT(addPwd_slot()), 0,
243 BUTTON_POPUP_MANAGE_ADD);
244 managePopup->insertItem(QIconSet(icons.loadIcon("edit", KIcon::Small)),
245 i18n("&Edit"), this, SLOT(editPwd_slot()), 0,
246 BUTTON_POPUP_MANAGE_EDIT);
247 managePopup->insertItem(QIconSet(icons.loadIcon("editdelete", KIcon::Small)),
248 i18n("&Delete"), this, SLOT(deletePwd_slot()),
249 0, BUTTON_POPUP_MANAGE_DEL);
250 managePopup->insertSeparator();
251 managePopup->insertItem(QIconSet(icons.loadIcon("rotate", KIcon::Small)),
252 i18n("Change &Master Password"), this,
253 SLOT(changeMasterPwd_slot()), 0,
254 BUTTON_POPUP_MANAGE_CHANGEMP);
255 menuBar()->insertItem(i18n("&Manage"), managePopup);
256// "chipcard" popup menu
257#ifdef CONFIG_KEYCARD
258 chipcardPopup->insertItem(QIconSet(icons.loadIcon("filenew", KIcon::Small)),
259 i18n("&Generate new key-card"), this,
260 SLOT(genNewCard_slot()), 0,
261 BUTTON_POPUP_CHIPCARD_GENNEW);
262 chipcardPopup->insertItem(QIconSet(icons.loadIcon("editdelete", KIcon::Small)),
263 i18n("&Erase key-card"), this,
264 SLOT(eraseCard_slot()), 0,
265 BUTTON_POPUP_CHIPCARD_DEL);
266 chipcardPopup->insertItem(QIconSet(icons.loadIcon("", KIcon::Small)),
267 i18n("Read card-&ID"), this,
268 SLOT(readCardId_slot()), 0,
269 BUTTON_POPUP_CHIPCARD_READID);
270 chipcardPopup->insertSeparator();
271 chipcardPopup->insertItem(QIconSet(icons.loadIcon("2rightarrow", KIcon::Small)),
272 i18n("&Make card backup-image"), this,
273 SLOT(makeCardBackup_slot()), 0,
274 BUTTON_POPUP_CHIPCARD_SAVEBACKUP);
275 chipcardPopup->insertItem(QIconSet(icons.loadIcon("2leftarrow", KIcon::Small)),
276 i18n("&Replay card backup-image"), this,
277 SLOT(replayCardBackup_slot()), 0,
278 BUTTON_POPUP_CHIPCARD_REPLAYBACKUP);
279 menuBar()->insertItem(i18n("&Chipcard manager"), chipcardPopup);
280#endif // CONFIG_KEYCARD
281// "view" popup menu
282 viewPopup->insertItem(QIconSet(icons.loadIcon("find", KIcon::Small)),
283 i18n("&Find"), this,
284 SLOT(find_slot()), 0, BUTTON_POPUP_VIEW_FIND);
285 viewPopup->insertSeparator();
286 viewPopup->insertItem(QIconSet(icons.loadIcon("halfencrypted", KIcon::Small)),
287 i18n("&Lock all entries"), this,
288 SLOT(lockWnd_slot()), 0,
289 BUTTON_POPUP_VIEW_LOCK);
290 viewPopup->insertItem(QIconSet(icons.loadIcon("encrypted", KIcon::Small)),
291 i18n("&Deep-lock all entries"), this,
292 SLOT(deepLockWnd_slot()), 0,
293 BUTTON_POPUP_VIEW_DEEPLOCK);
294 viewPopup->insertItem(QIconSet(icons.loadIcon("decrypted", KIcon::Small)),
295 i18n("&Unlock all entries"), this,
296 SLOT(unlockWnd_slot()), 0,
297 BUTTON_POPUP_VIEW_UNLOCK);
298 menuBar()->insertItem(i18n("&View"), viewPopup);
299// "options" popup menu
300 optionsPopup->insertItem(QIconSet(icons.loadIcon("configure", KIcon::Small)),
301 i18n("&Configure..."), this,
302 SLOT(config_slot()),
303 BUTTON_POPUP_OPTIONS_CONFIG);
304 menuBar()->insertItem(i18n("&Options"), optionsPopup);
305// "help" popup menu
306#ifndef PWM_EMBEDDED
307 helpPopup = helpMenu(QString::null, false);
308 menuBar()->insertItem(i18n("&Help"), helpPopup);
309#endif
310}
311
312void PwM::initToolbar()
313{
314 KIconLoader icons;
315
316 toolBar()->insertButton(icons.loadIcon("filenew", KIcon::Toolbar),
317 BUTTON_TOOL_NEW, SIGNAL(clicked(int)), this,
318 SLOT(new_slot()), true, i18n("New"));
319 toolBar()->insertButton(icons.loadIcon("fileopen", KIcon::Toolbar),
320 BUTTON_TOOL_OPEN, SIGNAL(clicked(int)), this,
321 SLOT(open_slot()), true, i18n("Open"));
322 toolBar()->insertSeparator();
323 toolBar()->insertButton(icons.loadIcon("filesave", KIcon::Toolbar),
324 BUTTON_TOOL_SAVE, SIGNAL(clicked(int)), this,
325 SLOT(save_slot()), true, i18n("Save"));
326 toolBar()->insertButton(icons.loadIcon("filesaveas", KIcon::Toolbar),
327 BUTTON_TOOL_SAVEAS, SIGNAL(clicked(int)), this,
328 SLOT(saveAs_slot()), true, i18n("Save as"));
329 toolBar()->insertButton(icons.loadIcon("fileprint", KIcon::Toolbar),
330 BUTTON_TOOL_PRINT, SIGNAL(clicked(int)), this,
331 SLOT(print_slot()), true, i18n("Print..."));
332 toolBar()->insertSeparator();
333 toolBar()->insertButton(icons.loadIcon("pencil", KIcon::Toolbar),
334 BUTTON_TOOL_ADD, SIGNAL(clicked(int)), this,
335 SLOT(addPwd_slot()), true,
336 i18n("Add password"));
337 toolBar()->insertButton(icons.loadIcon("edit", KIcon::Toolbar),
338 BUTTON_TOOL_EDIT, SIGNAL(clicked(int)), this,
339 SLOT(editPwd_slot()), true,
340 i18n("Edit password"));
341 toolBar()->insertButton(icons.loadIcon("editdelete", KIcon::Toolbar),
342 BUTTON_TOOL_DEL, SIGNAL(clicked(int)), this,
343 SLOT(deletePwd_slot()), true,
344 i18n("Delete password"));
345 toolBar()->insertSeparator();
346 toolBar()->insertButton(icons.loadIcon("find", KIcon::Toolbar),
347 BUTTON_TOOL_FIND, SIGNAL(clicked(int)), this,
348 SLOT(find_slot()), true, i18n("Find entry"));
349 toolBar()->insertSeparator();
350 toolBar()->insertButton(icons.loadIcon("halfencrypted", KIcon::Toolbar),
351 BUTTON_TOOL_LOCK, SIGNAL(clicked(int)), this,
352 SLOT(lockWnd_slot()), true,
353 i18n("Lock all entries"));
354 toolBar()->insertButton(icons.loadIcon("encrypted", KIcon::Toolbar),
355 BUTTON_TOOL_DEEPLOCK, SIGNAL(clicked(int)), this,
356 SLOT(deepLockWnd_slot()), true,
357 i18n("Deep-Lock all entries"));
358 toolBar()->insertButton(icons.loadIcon("decrypted", KIcon::Toolbar),
359 BUTTON_TOOL_UNLOCK, SIGNAL(clicked(int)), this,
360 SLOT(unlockWnd_slot()), true,
361 i18n("Unlock all entries"));
362}
363
364void PwM::initMetrics()
365{
366 QSize s = conf()->confWndMainWndSize();
367 if (s.isValid())
368 resize(s);
369 else
370 resize(DEFAULT_SIZE);
371}
372
373void PwM::updateCaption()
374{
375 setPlainCaption(curDoc()->getTitle() + " - " PROG_NAME " " PACKAGE_VER);
376}
377
378void PwM::hideEvent(QHideEvent *)
379{
380 if (isMinimized()) {
381 if (init->tray()) {
382 forceMinimizeToTray = true;
383 close();
384 }
385 int mmlock = conf()->confGlobMinimizeLock();
386 switch (mmlock) {
387 case 0: // don't lock anything
388 break;
389 case 1: {// normal lock
390 curDoc()->lockAll(true);
391 break;
392 } case 2: {// deep-lock
393 curDoc()->deepLock();
394 break;
395 } default:
396 WARN();
397 }
398 }
399}
400
401void PwM::setVirgin(bool v)
402{
403 if (virgin == v)
404 return;
405 virgin = v;
406 filePopup->setItemEnabled(BUTTON_POPUP_FILE_SAVE, !v);
407 filePopup->setItemEnabled(BUTTON_POPUP_FILE_SAVEAS, !v);
408 filePopup->setItemEnabled(BUTTON_POPUP_FILE_EXPORT, !v);
409 filePopup->setItemEnabled(BUTTON_POPUP_FILE_PRINT, !v);
410 managePopup->setItemEnabled(BUTTON_POPUP_MANAGE_EDIT, !v);
411 managePopup->setItemEnabled(BUTTON_POPUP_MANAGE_DEL, !v);
412 managePopup->setItemEnabled(BUTTON_POPUP_MANAGE_CHANGEMP, !v);
413 viewPopup->setItemEnabled(BUTTON_POPUP_VIEW_LOCK, !v);
414 viewPopup->setItemEnabled(BUTTON_POPUP_VIEW_DEEPLOCK, !v);
415 viewPopup->setItemEnabled(BUTTON_POPUP_VIEW_UNLOCK, !v);
416 viewPopup->setItemEnabled(BUTTON_POPUP_VIEW_FIND, !v);
417 toolBar()->setItemEnabled(BUTTON_TOOL_SAVE, !v);
418 toolBar()->setItemEnabled(BUTTON_TOOL_SAVEAS, !v);
419 toolBar()->setItemEnabled(BUTTON_TOOL_PRINT, !v);
420 toolBar()->setItemEnabled(BUTTON_TOOL_EDIT, !v);
421 toolBar()->setItemEnabled(BUTTON_TOOL_DEL, !v);
422 toolBar()->setItemEnabled(BUTTON_TOOL_LOCK, !v);
423 toolBar()->setItemEnabled(BUTTON_TOOL_DEEPLOCK, !v);
424 toolBar()->setItemEnabled(BUTTON_TOOL_UNLOCK, !v);
425 toolBar()->setItemEnabled(BUTTON_TOOL_FIND, !v);
426}
427
428void PwM::new_slot()
429{
430 init->createMainWnd();
431}
432
433//US ENH
434void PwM::open_slot()
435{
436 open_slot("");
437}
438
439void PwM::open_slot(QString fn)
440{
441 openDoc(fn);
442}
443
444PwMDoc * PwM::openDoc(QString filename, bool openDeepLocked)
445{
446 if (!isVirgin()) {
447 // open the document in a new window.
448 PwM *newInstance = init->createMainWnd();
449 PwMDoc *newDoc = newInstance->openDoc(filename, openDeepLocked);
450 if (!newDoc) {
451 newInstance->setForceQuit(true);
452 delete_and_null(newInstance);
453 }
454 return newDoc;
455 }
456
457 if (!curDoc()->openDocUi(curDoc(), filename, openDeepLocked))
458 return 0;
459 showStatMsg(i18n("Successfully opened file."));
460 updateCaption();
461 setVirgin(false);
462 return curDoc();
463}
464
465PwMView * PwM::makeNewListView(PwMDoc *doc)
466{
467 PwMView *ret = new PwMView(this, this, doc);
468 ret->setFont(conf()->confGlobEntryFont());
469 ret->show();
470 return ret;
471}
472
473void PwM::close_slot()
474{
475 close();
476}
477
478void PwM::quitButton_slot()
479{
480 init->shutdownApp(0);
481}
482
483void PwM::save_slot()
484{
485 save();
486}
487
488bool PwM::save()
489{
490 if (!curDoc()->saveDocUi(curDoc()))
491 return false;
492 showStatMsg(i18n("Successfully saved data."));
493 updateCaption();
494 return true;
495}
496
497void PwM::saveAs_slot()
498{
499 saveAs();
500}
501
502bool PwM::saveAs()
503{
504 if (!curDoc()->saveAsDocUi(curDoc()))
505 return false;
506 showStatMsg(i18n("Successfully saved data."));
507 updateCaption();
508 return true;
509}
510
511//US ENH : changed code to run with older MOC
512void PwM::addPwd_slot()
513{
514 addPwd_slot(0, 0);
515}
516
517void PwM::addPwd_slot(QString *pw, PwMDoc *_doc)
518{
519 PwMDoc *doc;
520 if (_doc) {
521 doc = _doc;
522 } else {
523 doc = curDoc();
524 }
525 PWM_ASSERT(doc);
526 doc->timer()->getLock(DocTimer::id_autoLockTimer);
527 AddEntryWndImpl w;
528 vector<string> catList;
529 doc->getCategoryList(&catList);
530 unsigned i, size = catList.size();
531 for (i = 0; i < size; ++i) {
532 w.addCategory(catList[i].c_str());
533 }
534 w.setCurrCategory(view->getCurrentCategory());
535 if (pw)
536 w.pwLineEdit->setText(*pw);
537
538 tryAgain:
539 if (w.exec() == 1) {
540 PwMDataItem d;
541 d.desc = w.getDescription().latin1();
542 d.name = w.getUsername().latin1();
543 d.pw = w.getPassword().latin1();
544 d.comment = w.getComment().latin1();
545 d.url = w.getUrl().latin1();
546 d.launcher = w.getLauncher().latin1();
547 PwMerror ret = doc->addEntry(w.getCategory(), &d);
548 if (ret == e_entryExists) {
549 KMessageBox::error(this,
550 i18n
551 ("An entry with this \"Description\", "
552 "does already exist.\n"
553 "Please select another description."),
554 i18n("entry already exists."));
555 goto tryAgain;
556 } else if (ret == e_maxAllowedEntr) {
557 KMessageBox::error(this, i18n("The maximum possible number of entries "
558 "has been reached. You can't add more entries."),
559 i18n("maximum number of entries"));
560 doc->timer()->putLock(DocTimer::id_autoLockTimer);
561 return;
562 }
563 }
564 setVirgin(false);
565 doc->timer()->putLock(DocTimer::id_autoLockTimer);
566}
567
568//US ENH : changed code to run with older MOC
569void PwM::editPwd_slot()
570{
571 editPwd_slot(0,0,0);
572}
573
574void PwM::editPwd_slot(const QString *category)
575{
576 editPwd_slot(category, 0, 0);
577}
578
579void PwM::editPwd_slot(const QString *category, const int *index,
580 PwMDoc *_doc)
581{
582 PwMDoc *doc;
583 if (_doc) {
584 doc = _doc;
585 } else {
586 doc = curDoc();
587 }
588 PWM_ASSERT(doc);
589 if (doc->isDocEmpty())
590 return;
591 if (doc->isDeepLocked())
592 return;
593 doc->timer()->getLock(DocTimer::id_autoLockTimer);
594 unsigned int curEntryIndex;
595 if (index) {
596 curEntryIndex = *index;
597 } else {
598 if (!(view->getCurEntryIndex(&curEntryIndex))) {
599 printDebug("couldn't get index. Maybe we have a binary entry here.");
600 doc->timer()->putLock(DocTimer::id_autoLockTimer);
601 return;
602 }
603 }
604 QString curCategory;
605 if (category) {
606 curCategory = *category;
607 } else {
608 curCategory = view->getCurrentCategory();
609 }
610 PwMDataItem currItem;
611 if (!doc->getEntry(curCategory, curEntryIndex, &currItem, true)) {
612 doc->timer()->putLock(DocTimer::id_autoLockTimer);
613 return;
614 }
615 BUG_ON(currItem.binary);
616
617 AddEntryWndImpl w;
618 vector<string> catList;
619 doc->getCategoryList(&catList);
620 unsigned i, size = catList.size();
621 for (i = 0; i < size; ++i) {
622 w.addCategory(catList[i].c_str());
623 }
624 w.setCurrCategory(curCategory);
625 w.setDescription(currItem.desc.c_str());
626 w.setUsername(currItem.name.c_str());
627 w.setPassword(currItem.pw.c_str());
628 w.setUrl(currItem.url.c_str());
629 w.setLauncher(currItem.launcher.c_str());
630 w.setComment(currItem.comment.c_str());
631 if (w.exec() == 1) {
632 currItem.desc = w.getDescription().latin1();
633 currItem.name = w.getUsername().latin1();
634 currItem.pw = w.getPassword().latin1();
635 currItem.comment = w.getComment().latin1();
636 currItem.url = w.getUrl().latin1();
637 currItem.launcher = w.getLauncher().latin1();
638 if (!doc->editEntry(curCategory, w.getCategory(),
639 curEntryIndex, &currItem)) {
640 KMessageBox::error(this,
641 i18n("Couldn't edit the entry.\n"
642 "Maybe you changed the category and "
643 "this entry is already present in the new "
644 "category?"),
645 i18n("couldn't edit entry."));
646 doc->timer()->putLock(DocTimer::id_autoLockTimer);
647 return;
648 }
649 }
650 doc->timer()->putLock(DocTimer::id_autoLockTimer);
651}
652
653void PwM::deletePwd_slot()
654{
655 PWM_ASSERT(curDoc());
656 if (curDoc()->isDocEmpty())
657 return;
658 if (curDoc()->isDeepLocked())
659 return;
660 curDoc()->timer()->getLock(DocTimer::id_autoLockTimer);
661 unsigned int curEntryIndex = 0;
662 if (!(view->getCurEntryIndex(&curEntryIndex))) {
663 printDebug("couldn't get index");
664 curDoc()->timer()->putLock(DocTimer::id_autoLockTimer);
665 return;
666 }
667
668 PwMDataItem currItem;
669 QString curCategory = view->getCurrentCategory();
670 if (!curDoc()->getEntry(curCategory, curEntryIndex, &currItem)) {
671 printDebug("couldn't get entry");
672 curDoc()->timer()->putLock(DocTimer::id_autoLockTimer);
673 return;
674 }
675 if (KMessageBox::
676 questionYesNo(this,
677 i18n
678 ("Do you really want to delete the selected entry") +
679 " \"" + QString(currItem.desc.c_str())
680 + "\" ?", i18n("delete?"))
681 == KMessageBox::Yes) {
682
683 curDoc()->delEntry(curCategory, curEntryIndex);
684 }
685 curDoc()->timer()->putLock(DocTimer::id_autoLockTimer);
686}
687
688void PwM::changeMasterPwd_slot()
689{
690 PWM_ASSERT(curDoc());
691 curDoc()->changeCurrentPw();
692}
693
694void PwM::lockWnd_slot()
695{
696 PWM_ASSERT(curDoc());
697 curDoc()->lockAll(true);
698}
699
700void PwM::deepLockWnd_slot()
701{
702 PWM_ASSERT(curDoc());
703 curDoc()->deepLock();
704}
705
706void PwM::unlockWnd_slot()
707{
708 PWM_ASSERT(curDoc());
709 curDoc()->lockAll(false);
710}
711
712void PwM::config_slot()
713{
714 Configuration *conf = Configuration::obj();
715 int oldStyle = conf->confWndMainViewStyle();
716
717 // display the configuration window (modal mode)
718 if (!conf->showConfWnd(this))
719 return;
720
721 int newStyle = conf->confWndMainViewStyle();
722 // reinitialize tray
723 init->initTray();
724 // reinitialize KWallet emulation
725 init->initKWalletEmu();
726
727 PwMDocList *_dl = PwMDoc::getOpenDocList();
728 const vector<PwMDocList::listItem> *dl = _dl->getList();
729 vector<PwMDocList::listItem>::const_iterator i = dl->begin(),
730 end = dl->end();
731 PwMDoc *doc;
732 while (i != end) {
733 doc = (*i).doc;
734 // unlock-without-mpw timeout
735 doc->timer()->start(DocTimer::id_mpwTimer);
736 // auto-lock timeout
737 doc->timer()->start(DocTimer::id_autoLockTimer);
738 ++i;
739 }
740
741 const QValueList<PwM *> *ml = init->mainWndList();
742#ifndef PWM_EMBEDDED
743 QValueList<PwM *>::const_iterator i2 = ml->begin(),
744 end2 = ml->end();
745#else
746 QValueList<PwM *>::ConstIterator i2 = ml->begin(),
747 end2 = ml->end();
748#endif
749 PwM *pwm;
750 while (i2 != end2) {
751 pwm = *i2;
752 // reinitialize the window style.
753 if (oldStyle != newStyle)
754 pwm->curView()->initStyle(newStyle);
755 // set the new font
756 pwm->curView()->setFont(conf->confGlobEntryFont());
757 ++i2;
758 }
759}
760
761void PwM::activateMpButton(bool activate)
762{
763 managePopup->setItemEnabled(BUTTON_POPUP_MANAGE_CHANGEMP, activate);
764}
765
766void PwM::closeEvent(QCloseEvent *e)
767{
768 e->accept();
769}
770
771void PwM::docClosed(PwMDoc *doc)
772{
773 PARAM_UNUSED(doc);
774 PWM_ASSERT(doc == curDoc());
775 close();
776}
777
778void PwM::find_slot()
779{
780 PWM_ASSERT(curDoc());
781 if (curDoc()->isDocEmpty())
782 return;
783 if (curDoc()->isDeepLocked())
784 return;
785 curDoc()->timer()->getLock(DocTimer::id_autoLockTimer);
786 FindWndImpl findWnd(view);
787 findWnd.exec();
788 curDoc()->timer()->putLock(DocTimer::id_autoLockTimer);
789}
790
791void PwM::exportToText()
792{
793 PWM_ASSERT(curDoc());
794 if (curDoc()->isDocEmpty()) {
795 KMessageBox::information(this,
796 i18n
797 ("Sorry, there's nothing to export.\n"
798 "Please first add some passwords."),
799 i18n("nothing to do"));
800 return;
801 }
802 curDoc()->timer()->getLock(DocTimer::id_autoLockTimer);
803 QString fn(KFileDialog::getSaveFileName(QString::null,
804 i18n("*|plain-text file"),
805 this));
806 if (fn == "") {
807 curDoc()->timer()->putLock(DocTimer::id_autoLockTimer);
808 return;
809 }
810
811 PwMerror ret = curDoc()->exportToText(&fn);
812 if (ret != e_success) {
813 KMessageBox::error(this,
814 i18n("Error: Couldn't write to file.\n"
815 "Please check if you have permission to write "
816 "to the file in that directory."),
817 i18n("error while writing"));
818 } else
819 showStatMsg(i18n("Successfully exported data."));
820 curDoc()->timer()->putLock(DocTimer::id_autoLockTimer);
821}
822
823bool PwM::importFromText()
824{
825 if (!isVirgin()) {
826 if (KMessageBox::questionYesNo(this,
827 i18n("Do you want to import the data "
828 "into the current document? (If you "
829 "select \"no\", a new document will be "
830 "opened.)"),
831 i18n("import into this document?"))
832 == KMessageBox::No) {
833 // import the data to a new window.
834 PwM *newInstance = init->createMainWnd();
835 bool ok = newInstance->importFromText();
836 if (!ok) {
837 newInstance->setForceQuit(true);
838 delete_and_null(newInstance);
839 }
840 return ok;
841 }
842 }
843
844 curDoc()->timer()->getLock(DocTimer::id_autoLockTimer);
845 PwMerror ret;
846 QString path(KFileDialog::getOpenFileName(QString::null,
847 i18n("*|PWM-exported text file"),
848 this));
849 if (path == "")
850 goto cancelImport;
851
852 ret = curDoc()->importFromText(&path, 0);
853 if (ret == e_fileFormat) {
854 KMessageBox::error(this,
855 i18n("Could not read file-format.\n"
856 "This seems to be _not_ a valid file "
857 "exported by PwM."),
858 i18n("invalid file-format"));
859 goto cancelImport;
860 } else if (ret == e_invalidArg) {
861 BUG();
862 goto cancelImport;
863 } else if (ret != e_success) {
864 KMessageBox::error(this,
865 i18n("Could not import file!\n"
866 "Do you have permission to read this file? "
867 "Do you have enough free memory?"),
868 i18n("import failed"));
869 goto cancelImport;
870 }
871 setVirgin(false);
872 curDoc()->timer()->putLock(DocTimer::id_autoLockTimer);
873 return true;
874
875cancelImport:
876 curDoc()->timer()->putLock(DocTimer::id_autoLockTimer);
877 return false;
878}
879
880void PwM::exportToGpasman()
881{
882 PWM_ASSERT(curDoc());
883 if (curDoc()->isDocEmpty()) {
884 KMessageBox::information(this,
885 i18n
886 ("Sorry, there's nothing to export.\n"
887 "Please first add some passwords."),
888 i18n("nothing to do"));
889 return;
890 }
891 curDoc()->timer()->getLock(DocTimer::id_autoLockTimer);
892 QString fn(KFileDialog::getSaveFileName(QString::null,
893 i18n("*|Gpasman or Kpasman file"),
894 this));
895 if (fn == "") {
896 curDoc()->timer()->putLock(DocTimer::id_autoLockTimer);
897 return;
898 }
899
900 PwMerror ret = curDoc()->exportToGpasman(&fn);
901 if (ret != e_success) {
902 if (ret == e_noPw) {
903 curDoc()->timer()->putLock(DocTimer::id_autoLockTimer);
904 return;
905 }
906 KMessageBox::error(this,
907 i18n("Error: Couldn't write to file.\n"
908 "Please check if you have permission to write "
909 "to the file in that directory."),
910 i18n("error while writing"));
911 } else
912 showStatMsg(i18n("Successfully exported data."));
913 curDoc()->timer()->putLock(DocTimer::id_autoLockTimer);
914}
915
916void PwM::exportToKWallet()
917{
918#ifdef CONFIG_KWALLETIF
919 if (!checkAndAskForKWalletEmu())
920 return;
921 PWM_ASSERT(curDoc());
922 if (curDoc()->isDocEmpty()) {
923 KMessageBox::information(this,
924 i18n
925 ("Sorry, there's nothing to export.\n"
926 "Please first add some passwords."),
927 i18n("nothing to do"));
928 init->initKWalletEmu();
929 return;
930 }
931 curDoc()->timer()->getLock(DocTimer::id_autoLockTimer);
932 KWalletIf walletIf(this);
933 if (walletIf.kwalletExport(curDoc())) {
934 KMessageBox::information(this,
935 i18n("Successfully exported the data of the current "
936 "document to KWallet."),
937 i18n("Successfully exported data."));
938 showStatMsg(i18n("Successfully exported data."));
939 }
940 init->initKWalletEmu();
941 curDoc()->timer()->putLock(DocTimer::id_autoLockTimer);
942#endif // CONFIG_KWALLETIF
943}
944
945bool PwM::importFromGpasman()
946{
947 if (!isVirgin()) {
948 if (KMessageBox::questionYesNo(this,
949 i18n("Do you want to import the data "
950 "into the current document? (If you "
951 "select \"no\", a new document will be "
952 "opened.)"),
953 i18n("import into this document?"))
954 == KMessageBox::No) {
955 // import the data to a new window.
956 PwM *newInstance = init->createMainWnd();
957 bool ok = newInstance->importFromGpasman();
958 if (!ok) {
959 newInstance->setForceQuit(true);
960 delete_and_null(newInstance);
961 }
962 return ok;
963 }
964 }
965
966 curDoc()->timer()->getLock(DocTimer::id_autoLockTimer);
967 PwMerror ret;
968 QString path(KFileDialog::getOpenFileName(QString::null,
969 i18n("*|Gpasman or Kpasman file"), this));
970 if (path == "")
971 goto cancelImport;
972 ret = curDoc()->importFromGpasman(&path);
973 if (ret == e_wrongPw) {
974 if (KMessageBox::questionYesNo(this,
975 i18n
976 ("This is probably the wrong master-password"
977 "you have typed in.\n"
978 "There is no real way to determine the "
979 "correctness of the password in the Gpasman "
980 "file-format. But I think this "
981 "password ist wrong.\n"
982 "Do you want to continue nevertheless?"),
983 i18n("password error"))
984 == KMessageBox::No) {
985 goto cancelImport;
986 }
987 } else if (ret != e_success) {
988 KMessageBox::error(this,
989 i18n("Could not import file!\n"
990 "Do you have permission to read this file?"),
991 i18n("import failed"));
992 goto cancelImport;
993 }
994 setVirgin(false);
995 curDoc()->timer()->putLock(DocTimer::id_autoLockTimer);
996 return true;
997
998cancelImport:
999 curDoc()->timer()->putLock(DocTimer::id_autoLockTimer);
1000 return false;
1001}
1002
1003#ifdef CONFIG_KWALLETIF
1004bool PwM::checkAndAskForKWalletEmu()
1005{
1006 if (init->kwalletEmu()) {
1007 /* KWallet emulation is enabled. We can't import/export
1008 * data from/to it, while emulation is active.
1009 */
1010 if (KMessageBox::questionYesNo(this,
1011 i18n("KWallet emulation is enabled.\n"
1012 "You can't import or export data from/to "
1013 "the original KWallet, while the emulation "
1014 "is active.\n"
1015 "Do you want to tempoarly disable the KWallet emulation?"),
1016 i18n("Tempoarly disable KWallet emulation?"))
1017 == KMessageBox::Yes) {
1018 init->initKWalletEmu(true);
1019 PWM_ASSERT(!init->kwalletEmu());
1020 return true;
1021 }
1022 return false;
1023 }
1024 return true;
1025}
1026#endif // CONFIG_KWALLETIF
1027
1028bool PwM::importKWallet()
1029{
1030#ifdef CONFIG_KWALLETIF
1031 if (!checkAndAskForKWalletEmu())
1032 return false;
1033 KWalletIf walletIf(this);
1034 if (!isVirgin()) {
1035 if (KMessageBox::questionYesNo(this,
1036 i18n("Do you want to import the data "
1037 "into the current document? (If you "
1038 "select \"no\", a new document will be "
1039 "opened.)"),
1040 i18n("import into this document?"))
1041 == KMessageBox::No) {
1042 // import the data to a new window.
1043 PwM *newInstance = init->createMainWnd();
1044 bool ok = newInstance->importKWallet();
1045 if (!ok) {
1046 newInstance->setForceQuit(true);
1047 delete_and_null(newInstance);
1048 goto exit_fail;
1049 } else {
1050 goto exit_ok;
1051 }
1052 }
1053 }
1054 curDoc()->timer()->getLock(DocTimer::id_autoLockTimer);
1055 if (!walletIf.kwalletImport(curDoc())) {
1056 curDoc()->timer()->putLock(DocTimer::id_autoLockTimer);
1057 showStatMsg(i18n("KWallet import failed"));
1058 goto exit_fail;
1059 }
1060 KMessageBox::information(this,
1061 i18n("Successfully imported the KWallet data "
1062 "into the current document."),
1063 i18n("successfully imported"));
1064 showStatMsg(i18n("successfully imported"));
1065 setVirgin(false);
1066 curDoc()->timer()->putLock(DocTimer::id_autoLockTimer);
1067
1068exit_ok:
1069 init->initKWalletEmu();
1070 return true;
1071
1072exit_fail:
1073 init->initKWalletEmu();
1074#endif // CONFIG_KWALLETIF
1075 return false;
1076}
1077
1078void PwM::print_slot()
1079{
1080 curDoc()->timer()->getLock(DocTimer::id_autoLockTimer);
1081#ifndef PWM_EMBEDDED
1082 PwMPrint p(curDoc(), this);
1083 p.printNow();
1084#else
1085 qDebug("PwM::print_slot , PRINTING IS NOT IMPLEMENTED");
1086#endif
1087 curDoc()->timer()->putLock(DocTimer::id_autoLockTimer);
1088}
1089
1090void PwM::genNewCard_slot()
1091{
1092#ifdef CONFIG_KEYCARD
1093 init->keycard()->genNewCard();
1094#endif
1095}
1096
1097void PwM::eraseCard_slot()
1098{
1099#ifdef CONFIG_KEYCARD
1100 init->keycard()->eraseCard();
1101#endif
1102}
1103
1104void PwM::readCardId_slot()
1105{
1106#ifdef CONFIG_KEYCARD
1107 init->keycard()->displayKey();
1108#endif
1109}
1110
1111void PwM::makeCardBackup_slot()
1112{
1113#ifdef CONFIG_KEYCARD
1114 init->keycard()->makeBackupImage();
1115#endif
1116}
1117
1118void PwM::replayCardBackup_slot()
1119{
1120#ifdef CONFIG_KEYCARD
1121 init->keycard()->replayBackupImage();
1122#endif
1123}
1124
1125void PwM::execLauncher_slot()
1126{
1127 PWM_ASSERT(curDoc());
1128 if (curDoc()->isDeepLocked())
1129 return;
1130 unsigned int curEntryIndex;
1131 if (!view->getCurEntryIndex(&curEntryIndex))
1132 return;
1133 bool ret = curDoc()->execLauncher(view->getCurrentCategory(),
1134 curEntryIndex);
1135 if (ret)
1136 showStatMsg(i18n("Executed the \"Launcher\"."));
1137 else
1138 showStatMsg(i18n("ERROR: Couldn't execute the \"Launcher\"!"));
1139}
1140
1141void PwM::goToURL_slot()
1142{
1143 PWM_ASSERT(curDoc());
1144 if (curDoc()->isDeepLocked())
1145 return;
1146 unsigned int curEntryIndex;
1147 if (!view->getCurEntryIndex(&curEntryIndex))
1148 return;
1149 bool ret = curDoc()->goToURL(view->getCurrentCategory(),
1150 curEntryIndex);
1151 if (ret)
1152 showStatMsg(i18n("started browser with current URL."));
1153 else
1154 showStatMsg(i18n("ERROR: Couldn't start browser! Maybe invalid URL?"));
1155}
1156
1157void PwM::copyToClipboard(const QString &s)
1158{
1159 QClipboard *cb = QApplication::clipboard();
1160#ifndef PWM_EMBEDDED
1161 if (cb->supportsSelection())
1162 cb->setText(s, QClipboard::Selection);
1163 cb->setText(s, QClipboard::Clipboard);
1164#else
1165 cb->setText(s);
1166
1167#endif
1168
1169}
1170
1171void PwM::showStatMsg(const QString &msg)
1172{
1173#ifndef PWM_EMBEDDED
1174 KStatusBar *statBar = statusBar();
1175 statBar->message(msg, STATUSBAR_MSG_TIMEOUT * 1000);
1176#else
1177 qDebug("Statusbar : %s",msg.latin1());
1178#endif
1179}
1180
1181void PwM::focusInEvent(QFocusEvent *e)
1182{
1183 if (e->gotFocus()) {
1184 emit gotFocus(this);
1185 } else if (e->lostFocus()) {
1186 emit lostFocus(this);
1187 }
1188}
1189
1190#ifndef PWM_EMBEDDED
1191#include "pwm.moc"
1192#endif