summaryrefslogtreecommitdiffabout
path: root/pwmanager/pwmanager/pwmdocui.cpp
Unidiff
Diffstat (limited to 'pwmanager/pwmanager/pwmdocui.cpp') (more/less context) (show whitespace changes)
-rw-r--r--pwmanager/pwmanager/pwmdocui.cpp436
1 files changed, 436 insertions, 0 deletions
diff --git a/pwmanager/pwmanager/pwmdocui.cpp b/pwmanager/pwmanager/pwmdocui.cpp
new file mode 100644
index 0000000..66a1b59
--- a/dev/null
+++ b/pwmanager/pwmanager/pwmdocui.cpp
@@ -0,0 +1,436 @@
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 "pwmdocui.h"
21#include "setmasterpwwndimpl.h"
22#include "getmasterpwwndimpl.h"
23#include "pwmexception.h"
24#include "getkeycardwnd.h"
25#include "pwm.h"
26#include "globalstuff.h"
27#include "spinforsignal.h"
28
29#include <qlineedit.h>
30#include <qtabwidget.h>
31
32#include <kmessagebox.h>
33#include <kfiledialog.h>
34
35#ifndef PWM_EMBEDDED
36#include <kwin.h>
37#else
38#endif
39
40
41#ifdef CONFIG_KEYCARD
42# include "pwmkeycard.h"
43#endif
44
45
46PwMDocUi::PwMDocUi(QObject *parent, const char *name)
47 : QObject(parent, name)
48{
49 currentView = 0;
50 keyCard = 0;
51}
52
53PwMDocUi::~PwMDocUi()
54{
55}
56
57QString PwMDocUi::requestMpw(bool chipcard)
58{
59 QString pw;
60
61 if (chipcard) {
62#ifdef CONFIG_KEYCARD
63 PWM_ASSERT(keyCard);
64 uint32_t id;
65 string ret;
66 SpinForSignal *spinner = keyCard->getSpinner();
67 connect(keyCard, SIGNAL(keyAvailable(uint32_t, const string &)),
68 spinner, SLOT(u32_str_slot(uint32_t, const string &)));
69 keyCard->getKey();
70 spinner->spin(&id, &ret);
71 disconnect(keyCard, SIGNAL(keyAvailable(uint32_t, const string &)),
72 spinner, SLOT(u32_str_slot(uint32_t, const string &)));
73 if (ret == "")
74 return "";
75 pw = ret.c_str();
76#else // CONFIG_KEYCARD
77 no_keycard_support_msg_box(currentView);
78#endif // CONFIG_KEYCARD
79 } else {
80 GetMasterPwWndImpl pwWnd;
81#ifndef PWM_EMBEDDED
82 KWin::setState(pwWnd.winId(), NET::StaysOnTop);
83#endif
84 if (pwWnd.exec() != 1)
85 return "";
86 pw = pwWnd.pwLineEdit->text();
87 }
88
89 return pw;
90}
91
92QString PwMDocUi::requestNewMpw(bool *chipcard)
93{
94 QString pw;
95 SetMasterPwWndImpl pwWnd(currentView);
96 pwWnd.setPwMKeyCard(keyCard);
97#ifndef PWM_EMBEDDED
98 if (!chipcard) {
99 pwWnd.mainTab->removePage(pwWnd.mainTab->page(1));
100 }
101#else
102 qDebug("PwMDocUi::requestNewMpw must be implemented");
103#endif
104
105 if (pwWnd.exec() != 1)
106 return "";
107 pw = pwWnd.getPw(chipcard).c_str();
108
109 return pw;
110}
111
112QString PwMDocUi::requestMpwChange(const QString *currentPw, bool *chipcard)
113{
114 QString pw(requestMpw(*chipcard));
115 if (pw == "")
116 return "";
117 if (pw != *currentPw) {
118 wrongMpwMsgBox(*chipcard);
119 return "";
120 }
121
122 pw = requestNewMpw(chipcard);
123 if (pw == "")
124 return "";
125 return pw;
126}
127
128void PwMDocUi::wrongMpwMsgBox(bool chipcard, QString prefix, QString postfix)
129{
130 QString msg;
131 if (prefix != "") {
132 msg += prefix;
133 msg += "\n";
134 }
135
136 if (chipcard) {
137 msg += i18n("Wrong key-card!\n"
138 "Please try again with the "
139 "correct key-card.");
140 } else {
141 msg += i18n("Wrong master-password!\n"
142 "Please try again.");
143 }
144
145 if (postfix != "") {
146 msg += "\n";
147 msg += postfix;
148 }
149 KMessageBox::error(currentView, msg,
150 (chipcard) ? (i18n("wrong chipcard"))
151 : (i18n("password error")));
152}
153
154void PwMDocUi::noMpwMsgBox(bool chipcard, QString prefix, QString postfix)
155{
156 QString msg;
157 if (prefix != "") {
158 msg += prefix;
159 msg += "\n";
160 }
161
162 if (chipcard) {
163 msg += i18n("No key-card found!\n"
164 "Please insert the "
165 "correct key-card.");
166 } else {
167 msg += i18n("No master-password given!");
168 }
169
170 if (postfix != "") {
171 msg += "\n";
172 msg += postfix;
173 }
174 KMessageBox::error(currentView, msg,
175 (chipcard) ? (i18n("no chipcard"))
176 : (i18n("password error")));
177}
178
179void PwMDocUi::rootAlertMsgBox()
180{
181 KMessageBox::error(currentView,
182 i18n("This feature is not available, "
183 "if you execute PwM with \"root\" "
184 "UID 0 privileges, for security reasons!"),
185 i18n("not allowed as root!"));
186}
187
188void PwMDocUi::cantDeeplock_notSavedMsgBox()
189{
190 KMessageBox::error(currentView,
191 i18n("Can't deep-lock, because the document "
192 "hasn't been saved, yet. Please save "
193 "to a file and try again."),
194 i18n("not saved, yet"));
195}
196
197void PwMDocUi::gpmPwLenErrMsgBox()
198{
199 KMessageBox::error(currentView,
200 i18n("GPasman does not support passwords "
201 "shorter than 4 characters! Please try "
202 "again with a longer password."),
203 i18n("password too short"));
204}
205
206int PwMDocUi::dirtyAskSave(const QString &docTitle)
207{
208 int ret;
209#ifndef PWM_EMBEDDED
210 ret = KMessageBox::questionYesNoCancel(currentView,
211 i18n("The list \"") +
212 docTitle +
213 i18n
214 ("\" has been modified.\n"
215 "Do you want to save it?"),
216 i18n("save?"));
217 if (ret == KMessageBox::Yes) {
218 return 0;
219 } else if (ret == KMessageBox::No) {
220 return 1;
221 }
222#else
223 ret = KMessageBox::warningYesNoCancel(currentView,
224 i18n("The list \"") +
225 docTitle +
226 i18n
227 ("\" has been modified.\n"
228 "Do you want to save it?"),
229 i18n("save?"));
230 if (ret == KMessageBox::Yes) {
231 return 0;
232 } else if (ret == KMessageBox::No) {
233 return 1;
234 }
235
236#endif
237
238 // cancel
239 return -1;
240}
241
242bool PwMDocUi::saveDocUi(PwMDoc *doc)
243{
244 PWM_ASSERT(doc);
245 doc->timer()->getLock(DocTimer::id_autoLockTimer);
246 if (doc->isDocEmpty()) {
247 KMessageBox::information(currentView,
248 i18n
249 ("Sorry, there's nothing to save.\n"
250 "Please first add some passwords."),
251 i18n("nothing to do"));
252 doc->timer()->putLock(DocTimer::id_autoLockTimer);
253 return true;
254 }
255 PwMerror ret = doc->saveDoc(conf()->confGlobCompression());
256 if (ret == e_filename) {
257 doc->timer()->putLock(DocTimer::id_autoLockTimer);
258 return saveAsDocUi(doc);
259 } else if (ret == e_weakPw) {
260 KMessageBox::error(currentView,
261 i18n("Error: This is a weak password.\n"
262 "Please select another password."),
263 i18n("weak password"));
264 doc->timer()->putLock(DocTimer::id_autoLockTimer);
265 return false;
266 } else if (ret == e_fileBackup) {
267 KMessageBox::error(currentView,
268 i18n("Error: Couldn't make backup-file!"),
269 i18n("backup failed"));
270 doc->timer()->putLock(DocTimer::id_autoLockTimer);
271 return false;
272 } else if (ret != e_success) {
273 KMessageBox::error(currentView,
274 i18n("Error: Couldn't write to file.\n"
275 "Please check if you have permission to "
276 "write to the file in that directory."),
277 i18n("error while writing"));
278 doc->timer()->putLock(DocTimer::id_autoLockTimer);
279 return false;
280 }
281 doc->timer()->putLock(DocTimer::id_autoLockTimer);
282 return true;
283}
284
285bool PwMDocUi::saveAsDocUi(PwMDoc *doc)
286{
287 PWM_ASSERT(doc);
288 doc->timer()->getLock(DocTimer::id_autoLockTimer);
289 if (doc->isDocEmpty()) {
290 KMessageBox::information(currentView,
291 i18n
292 ("Sorry, there's nothing to save.\n"
293 "Please first add some passwords."),
294 i18n("nothing to do"));
295 doc->timer()->putLock(DocTimer::id_autoLockTimer);
296 return true;
297 }
298 QString fn(KFileDialog::getSaveFileName(QString::null,
299 i18n("*.pwm|PwManager Password file"),
300 currentView));
301 if (fn == "") {
302 doc->timer()->putLock(DocTimer::id_autoLockTimer);
303 return false;
304 }
305 if (fn.right(4) != ".pwm")
306 fn += ".pwm";
307
308 PwMerror ret = doc->saveDoc(conf()->confGlobCompression(), &fn);
309 if (ret != e_success) {
310 KMessageBox::error(currentView,
311 i18n("Error: Couldn't write to file.\n"
312 "Please check if you have permission to "
313 "write to the file in that directory."),
314 i18n("error while writing"));
315 doc->timer()->putLock(DocTimer::id_autoLockTimer);
316 return false;
317 }
318 doc->timer()->putLock(DocTimer::id_autoLockTimer);
319 return true;
320}
321
322bool PwMDocUi::openDocUi(PwMDoc *doc,
323 QString filename,
324 bool openDeepLocked)
325{
326 if (filename.isEmpty())
327 filename = KFileDialog::getOpenFileName(QString::null,
328 i18n("*.pwm|PwManager Password file\n"
329 "*|All files"), getCurrentView());
330 if (filename.isEmpty())
331 goto cancelOpen;
332 PwMerror ret;
333 while (true) {
334 int lockStat = -1;
335 if (openDeepLocked) {
336 lockStat = 2;
337 } else {
338 if (conf()->confGlobUnlockOnOpen()) {
339 lockStat = 0;
340 } else {
341 lockStat = 1;
342 }
343 }
344 ret = doc->openDoc(&filename, lockStat);
345 if (ret != e_success) {
346 if (ret == e_readFile || ret == e_openFile) {
347 KMessageBox::error(getCurrentView(),
348 i18n("Could not read file!")
349 + "\n"
350 + filename,
351 i18n("file error"));
352 goto cancelOpen;
353 }
354 if (ret == e_alreadyOpen) {
355 KMessageBox::error(getCurrentView(),
356 i18n("This file is already open."),
357 i18n("already open"));
358 goto cancelOpen;
359 }
360 if (ret == e_fileVer) {
361 KMessageBox::error(getCurrentView(),
362 i18n
363 ("File-version is not supported!\n"
364 "Did you create this file with an older or newer version of PwM?"),
365 i18n
366 ("incompatible version"));
367 goto cancelOpen;
368 }
369 if (ret == e_wrongPw) {
370 continue;
371 }
372 if (ret == e_noPw) {
373 goto cancelOpen;
374 }
375 if (ret == e_fileFormat) {
376 KMessageBox::error(getCurrentView(),
377 i18n
378 ("Sorry, this file has not been recognized "
379 "as a PwM Password file.\n"
380 "Probably you have selected the wrong file."),
381 i18n
382 ("no PwM password-file"));
383 goto cancelOpen;
384 }
385 if (ret == e_fileCorrupt) {
386 KMessageBox::error(getCurrentView(),
387 i18n
388 ("File corrupt!\n"
389 "Maybe the media, you stored this file on, "
390 "had bad sectors?"),
391 i18n
392 ("checksum error"));
393 goto cancelOpen;
394 }
395 }
396 break;
397 }
398 return true;
399
400 cancelOpen:
401 return false;
402}
403
404QString PwMDocUi::string_defaultCategory()
405{
406 return i18n("Default");
407}
408
409QString PwMDocUi::string_locked()
410{
411 return i18n("<LOCKED>");
412}
413
414QString PwMDocUi::string_deepLockedShort()
415{
416 return i18n("DEEP-LOCKED");
417}
418
419QString PwMDocUi::string_deepLockedLong()
420{
421 return i18n("This file is DEEP-LOCKED!\n"
422 "That means all data has been encrypted "
423 "and written out to the file. If you want "
424 "to see the entries, please UNLOCK the file. "
425 "While unlocking, you will be prompted for the "
426 "master-password or the key-card.");
427}
428
429QString PwMDocUi::string_defaultTitle()
430{
431 return i18n("Untitled");
432}
433
434#ifndef PWM_EMBEDDED
435#include "pwmdocui.moc"
436#endif