summaryrefslogtreecommitdiffabout
path: root/pwmanager/pwmanager/pwmtray.cpp
blob: fe074ca80270221e0106fe71aba90b0ae710bfef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/***************************************************************************
 *                                                                         *
 *   copyright (C) 2003, 2004 by Michael Buesch                            *
 *   email: mbuesch@freenet.de                                             *
 *                                                                         *
 *   Original implementation of the tray-tree                              *
 *   (c) by Matt Scifo <mscifo@o1.com>                                     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License version 2        *
 *   as published by the Free Software Foundation.                         *
 *                                                                         *
 ***************************************************************************/

/***************************************************************************
 * copyright (C) 2004 by Ulf Schenk
 * This file is originaly based on version 1.0.1 of pwmanager
 * and was modified to run on embedded devices that run microkde
 *
 * $Id$
 **************************************************************************/  

#include "pwmtray.h"
#include "pwmexception.h"
#include "pwm.h"
#include "pwmdoc.h"
#include "pwminit.h"
#ifndef PWM_EMBEDDED
#include "configuration.h"
#else
#include "pwmprefs.h"
#endif

#include <klocale.h>


void ActiveTreeItem::execIt()
{
#ifndef PWM_EMBEDDED
	unsigned int entr = static_cast<unsigned int>(entry);
	unsigned int cat = static_cast<unsigned int>(category);
#else
	unsigned int entr = (unsigned int)(entry);
	unsigned int cat = (unsigned int)(category);
#endif
	switch (task) {
		case pwToClipboard: {
			PwMDataItem d;
			doc->getDataChangedLock();
			bool wasLocked = doc->isLocked(cat, entr);
			doc->getEntry(cat, entr, &d, true);
			if (wasLocked)
				doc->lockAt(cat, entr, true);
			doc->putDataChangedLock();
			PwM::copyToClipboard(d.pw.c_str());
			break;
		} case nameToClipboard: {
			PwMDataItem d;
			doc->getEntry(cat, entr, &d);
			PwM::copyToClipboard(d.name.c_str());
			break;
		} case descToClipboard: {
			PwMDataItem d;
			doc->getEntry(cat, entr, &d);
			PwM::copyToClipboard(d.desc.c_str());
			break;
		} case urlToClipboard: {
			PwMDataItem d;
			doc->getEntry(cat, entr, &d);
			PwM::copyToClipboard(d.url.c_str());
			break;
		} case launcherToClipboard: {
			PwMDataItem d;
			doc->getEntry(cat, entr, &d);
			PwM::copyToClipboard(d.launcher.c_str());
			break;
		} case commentToClipboard: {
			PwMDataItem d;
			doc->getEntry(cat, entr, &d);
			PwM::copyToClipboard(d.comment.c_str());
			break;
		} case execLauncher: {
			doc->execLauncher(cat, entr);
			break;
		} case goToURL: {
			doc->goToURL(cat, entr);
			break;
		} case openMainWnd: {
			// search if there is already an open window.
			const QValueList<PwM *> *wl = tray->init->mainWndList();
#ifndef PWM_EMBEDDED
			QValueList<PwM *>::const_iterator i = wl->begin(),
							  end = wl->end();
#else
			QValueList<PwM *>::ConstIterator i = wl->begin(),
							  end = wl->end();
#endif
			PwM *wnd;
			while (i != end) {
				wnd = *i;
				if (wnd->curDoc() == doc) {
					// now bring this window to the foreground.
					if (!wnd->hasFocus()) {
						wnd->hide();
						wnd->showNormal();
						wnd->setFocus();
					}
					return;
				}
				++i;
			}
			// there is no open window, so open a new one.
			tray->init->createMainWnd(QString::null, false, false, doc);
			break;
		} case closeDoc: {
			doc->tryDelete();
			break;
		} case lock: {
			doc->lockAll(true);
			break;
		} case deepLock: {
			doc->deepLock();
			break;
		} case unlock: {
			doc->lockAll(false);
			break;
		} default: {
			BUG();
		}
	}
}



#ifndef PWM_EMBEDDED
PwMTray::PwMTray(PwMInit *_init, QWidget * parent, const char *name)
 : KSystemTray(parent, name)
#else
PwMTray::PwMTray(PwMInit *_init, QWidget * parent, const char *name)
 : QWidget(parent, name)
#endif
{
	init = _init;
	buildMain();
}

PwMTray::~PwMTray()
{
	emit closed(this);
}

//US ENH for embedded tray class 
KPopupMenu* PwMTray::contextMenu()
{
  if (m_ctxMenu == 0)
    {
      m_ctxMenu = new KPopupMenu();
    }

  return m_ctxMenu;
}


void PwMTray::buildMain()
{
	KPopupMenu *ctxMenu = contextMenu();

	ctxMenu->insertSeparator();
	ctxMenu->insertItem(i18n("&New main window..."), this,
			    SLOT(newMainWnd()));
	ctxMenu->insertItem(i18n("&Open file..."), this,
			    SLOT(openDoc()));
	ctxMenu->insertSeparator();
	ctxMenu->insertItem(i18n("&Remove from tray"), this,
			    SLOT(undock()));
}

void PwMTray::insertActiveTreeItem(KPopupMenu *insertIn, const QString &text,
				   ActiveTreeItem::Task task, PwMDoc *doc,
				   int docCategory, int docEntry,
				   QValueList<int> *activeItemsList)
{
	ActiveTreeItem *activeItem;
	int id;

	activeItem = new ActiveTreeItem(text, insertIn->font(), task,
					doc, docCategory, docEntry,
					this);
	id = insertIn->insertItem(activeItem);
	insertIn->connectItem(id, activeItem, SLOT(execIt()));
#ifndef PWM_EMBEDDED
	activeItemsList->push_back(id);
#else
	activeItemsList->append(id);
#endif
}

void PwMTray::rebuildTree(KPopupMenu *popup, PwMDoc *doc,
			  QValueList<int> *activeItems)
{
	PWM_ASSERT(doc);
	PWM_ASSERT(popup);
	activeItems->clear();
	popup->clear();
#ifndef PWM_EMBEDDED
	popup->insertTitle(i18n("Categories:"));
#endif
	vector<string> catList;
	vector<string> entrList;
	doc->getCategoryList(&catList);
	KPopupMenu *newCatMenu;
	KPopupMenu *newEntrMenu;
	int i, size = catList.size();
	int j, entries;
	for (i = 0; i < size; ++i) {
		newCatMenu = new KPopupMenu(popup);
		popup->insertItem(catList[i].c_str(), newCatMenu);
		doc->getEntryList(i, &entrList);

		entries = entrList.size();
		for (j = 0; j < entries; ++j) {
			newEntrMenu = new KPopupMenu(newCatMenu);
			newCatMenu->insertItem(entrList[j].c_str(), newEntrMenu);

			if (doc->isBinEntry(i, j)) {
				/* This is a binary entry. Don't insert the usual
				 * menu items.
				 */
#ifndef PWM_EMBEDDED
				newEntrMenu->insertTitle(i18n("This is a binary entry.\n"
					    "It is not a normal password-entry, as it contains "
					    "binary data, which PwManager can't display here."));
#endif
				continue;
			}

			insertActiveTreeItem(newEntrMenu,
					     i18n("copy password to clipboard"),
					     ActiveTreeItem::pwToClipboard, doc, i, j,
					     activeItems);

			insertActiveTreeItem(newEntrMenu,
					     i18n("copy username to clipboard"),
					     ActiveTreeItem::nameToClipboard, doc, i, j,
					     activeItems);

			insertActiveTreeItem(newEntrMenu,
					     i18n("copy description to clipboard"),
					     ActiveTreeItem::descToClipboard, doc, i, j,
					     activeItems);

			insertActiveTreeItem(newEntrMenu,
					     i18n("copy url to clipboard"),
					     ActiveTreeItem::urlToClipboard, doc, i, j,
					     activeItems);

			insertActiveTreeItem(newEntrMenu,
					     i18n("copy launcher to clipboard"),
					     ActiveTreeItem::launcherToClipboard, doc, i, j,
					     activeItems);

			insertActiveTreeItem(newEntrMenu,
					     i18n("copy comment to clipboard"),
					     ActiveTreeItem::commentToClipboard, doc, i, j,
					     activeItems);

			newEntrMenu->insertSeparator();

			insertActiveTreeItem(newEntrMenu,
					     i18n("Execute \"Launcher\""),
					     ActiveTreeItem::execLauncher, doc, i, j,
					     activeItems);

			insertActiveTreeItem(newEntrMenu,
					     i18n("Go to \"URL\""),
					     ActiveTreeItem::goToURL, doc, i, j,
					     activeItems);
		}
	}
	insertMainWndCtrl(popup, doc);
}

void PwMTray::insertMainWndCtrl(KPopupMenu *menu, PwMDoc *doc)
{
	ActiveTreeItem *activeItem;
	int id;

#ifndef PWM_EMBEDDED
	menu->insertTitle(i18n("Manager:"));
#endif
	activeItem = new ActiveTreeItem(i18n("&Open main manager window ..."),
					menu->font(), ActiveTreeItem::openMainWnd,
					doc, 0, 0, this);
	id = menu->insertItem(activeItem);
	menu->connectItem(id, activeItem, SLOT(execIt()));

	activeItem = new ActiveTreeItem(i18n("&Close this document ..."),
					menu->font(), ActiveTreeItem::closeDoc,
					doc, 0, 0, this);
	id = menu->insertItem(activeItem);
	menu->connectItem(id, activeItem, SLOT(execIt()));

	menu->insertSeparator();

	activeItem = new ActiveTreeItem(i18n("&Lock all entries"),
					menu->font(), ActiveTreeItem::lock,
					doc, 0, 0, this);
	id = menu->insertItem(activeItem);
	menu->connectItem(id, activeItem, SLOT(execIt()));

	activeItem = new ActiveTreeItem(i18n("&Deep-lock all entries"),
					menu->font(), ActiveTreeItem::deepLock,
					doc, 0, 0, this);
	id = menu->insertItem(activeItem);
	menu->connectItem(id, activeItem, SLOT(execIt()));

	activeItem = new ActiveTreeItem(i18n("&Unlock all entries"),
					menu->font(), ActiveTreeItem::unlock,
					doc, 0, 0, this);
	id = menu->insertItem(activeItem);
	menu->connectItem(id, activeItem, SLOT(execIt()));
}

void PwMTray::updateTree(PwMDoc *document)
{
	PWM_ASSERT(document);
	KPopupMenu *treeEntry;
	int treeItemNum = -1;
	int id = findTreeEntry(document, &treeEntry, &treeItemNum);
	if (id == -1) {
		// tree-entry doesn't exist, yet.
		id = insertTreeEntry(document, &treeEntry);
	}
	if (treeItemNum != -1) {
		// delete all *old* active items
		KPopupMenu *ctxMenu = contextMenu();
		QValueList<int> *oldItems = &tree[treeItemNum].activeItems;
#ifndef PWM_EMBEDDED
		QValueList<int>::iterator i = oldItems->begin();
#else
		QValueList<int>::Iterator i = oldItems->begin();
#endif
		while (i != oldItems->end()) {
			ctxMenu->removeItem(*i);
			++i;
		}
		oldItems->clear();
		ctxMenu->changeItem(id, document->getTitle());
	}

	treeItem newTreeItem;
	if (document->isDeepLocked()) {
		treeEntry->clear();
#ifndef PWM_EMBEDDED		
		treeEntry->insertTitle(i18n("Categories:"));
#endif
		QString msg(IS_DEEPLOCKED_SHORTMSG);
		msg += "  ";
		msg += i18n("(Click here to unlock)");
		treeEntry->insertItem(msg, document,
				      SLOT(_deepUnlock()));
		insertMainWndCtrl(treeEntry, document);
	} else {
		rebuildTree(treeEntry, document, &newTreeItem.activeItems);
	}
	newTreeItem.menuId = id;
	newTreeItem.doc = document;
	newTreeItem.menu = treeEntry;
	if (treeItemNum == -1) {
#ifndef PWM_EMBEDDED
		tree.push_back(newTreeItem);
#else
		tree.append(newTreeItem);
#endif
	} else {
		tree[treeItemNum] = newTreeItem;
	}
}

void PwMTray::closeTreeEntry(PwMDoc *document)
{
	KPopupMenu *menu;
	int treeItem;
	int id = findTreeEntry(document, &menu, &treeItem);
	if (id != -1) {
		removeTreeEntry(id, treeItem, &menu);
	}
}

int PwMTray::findTreeEntry(PwMDoc *doc, KPopupMenu **menu, int *treeItem)
{
	PWM_ASSERT(doc);
#ifndef PWM_EMBEDDED
	int i, count = tree.size();
#else
	int i, count = tree.count();
#endif
	for (i = 0; i < count; ++i) {
		if (tree[i].doc == doc) {
			if (menu)
				*menu = tree[i].menu;
			if (treeItem)
				*treeItem = i;
			return tree[i].menuId;
		}
	}
	if (menu)
		*menu = 0;
	if (treeItem)
		*treeItem = -1;
	return -1;
}

int PwMTray::insertTreeEntry(PwMDoc *doc, KPopupMenu **popup)
{
	PWM_ASSERT(doc);
	PWM_ASSERT(popup);
	KPopupMenu *ctxMenu = contextMenu();
	*popup = new KPopupMenu(ctxMenu);
	return ctxMenu->insertItem(doc->getTitle(), *popup, -1, 1);
}

void PwMTray::removeTreeEntry(int menuId, int treeItem, KPopupMenu **menu)
{
	PWM_ASSERT(menu);
	KPopupMenu *ctxMenu = contextMenu();
	ctxMenu->removeItem(menuId);
	delete_and_null(*menu);
#ifndef PWM_EMBEDDED
	tree.erase(tree.at(treeItem));
#else
	tree.remove(tree.at(treeItem));
#endif
}

void PwMTray::mouseReleaseEvent(QMouseEvent *e)
{
	QWidget *curWnd = init->curWidget();
	KPopupMenu *ctxMenu = contextMenu();
	// give focus to init->curWidget()
	if (curWnd && !curWnd->hasFocus()) {
		curWnd->hide();
		curWnd->showNormal();
		curWnd->setFocus();
	}
	// popup the context menu
	ctxMenu->popup(e->globalPos());
	emit clickedIcon(this);
}

void PwMTray::connectDocToTray(PwMDoc *doc)
{
	connect(doc, SIGNAL(dataChanged(PwMDoc *)),
		this, SLOT(updateTree(PwMDoc *)));
	connect(doc, SIGNAL(docClosed(PwMDoc *)),
		this, SLOT(closeTreeEntry(PwMDoc *)));
	// fake a dataChanged signal for this doc to update the tray tree.
	updateTree(doc);
}

void PwMTray::openDoc()
{
	// open the document in a new window.
	PwM *newInstance = init->createMainWnd();
	PwMDoc *newDoc = newInstance->openDoc("");
	if (!newDoc) {
		newInstance->setForceQuit(true);
		delete_and_null(newInstance);
	}
}

void PwMTray::newMainWnd()
{
	init->createMainWnd();
}

void PwMTray::undock()
{
	conf()->confGlobTray(false);
	init->initTray();
	// Attention! "this" is already deleted here!
}

#ifndef PWM_EMBEDDED
#include "pwmtray.moc"
#endif