summaryrefslogtreecommitdiffabout
path: root/pwmanager/pwmanager/pwmprint.cpp
blob: 1d26b10cf7f9dc0ced4c24253ecf55c626e64975 (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
/***************************************************************************
 *                                                                         *
 *   copyright (C) 2003, 2004 by Michael Buesch                            *
 *   email: mbuesch@freenet.de                                             *
 *                                                                         *
 *   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 "pwmprint.h"
#include "pwmexception.h"
#include "pwm.h"
#include "pwmdoc.h"
#include "configuration.h"
#include "printtext.h"
#include "globalstuff.h"
#include "listobjselectwnd.h"

#include <kprinter.h>
#include <kmessagebox.h>
#include <kdialogbase.h>
#include <qdatetime.h>


PwMPrint::PwMPrint(PwMDoc *_doc, QWidget *_parent)
{
	PWM_ASSERT(_doc);
	PWM_ASSERT(_parent);
	doc = _doc;
	parent = _parent;
}

PwMPrint::~PwMPrint()
{
}

void PwMPrint::printNow()
{
	QString docTitle(doc->getTitle());
	if (!doc->unlockAll_tempoary())
		return;
	if (doc->isDocEmpty()) {
		KMessageBox::information(parent,
					 i18n("Sorry, there are no entries to print "
					      "in the document \"") +
					 docTitle + "\".",
					 i18n("nothing to do"));
		doc->unlockAll_tempoary(true);
		return;
	}
	QStringList printCategories(selCategories());
	if (printCategories.isEmpty()) {
		doc->unlockAll_tempoary(true);
		return;
	}
	doc->ensureLvp();

	PrintText prn;
	if (!prn.setup(parent)) {
		doc->unlockAll_tempoary(true);
		return;
	}
	prn.setFullPage(false);
	prn.setCreator(PROG_NAME);
	prn.setDocName(docTitle);

	prn.setHeader(genDateString(), docTitle);
	prn.beginPrint();

	prn.printLine("");
	prn.printLine(QString(PROG_NAME " v" PACKAGE_VER " ") + i18n("password table:"));
	prn.printLine("");

	vector<PwMCategoryItem> tmp;
	PwMCategoryItem catItem;
	catItem.clear();
	PwMDataItem item;
	item.clear();
	unsigned int numEntr, j, i = 0;
	QStringList::iterator catI = printCategories.begin(),
			      catEnd = printCategories.end();
	// Sort items on lvp and store them in tempoary "tmp".
	while (catI != catEnd) {
		catItem.clear();
		catItem.name = (*catI).latin1();
		tmp.push_back(catItem);
		item.clear();
		numEntr = doc->numEntries(*catI);
		tmp[i].d.insert(tmp[i].d.begin(), numEntr, item);
		for (j = 0; j < numEntr; ++j) {
			doc->getEntry(*catI, j, &item);
			tmp[i].d[numEntr - item.listViewPos - 1] = item;
		}
		++catI;
		++i;
	}
	doc->unlockAll_tempoary(true);

	QString currLine;
	vector<PwMCategoryItem>::iterator cat_it = tmp.begin(),
					  cat_end = tmp.end();
	unsigned int size;
	while (cat_it != cat_end) {
		i = 0;
		size = cat_it->d.size();
		prn.printLine("");
		currLine = "== ";
		currLine += i18n("Category: ");
		currLine += cat_it->name.c_str();
		currLine += " ==";
		prn.printLine(currLine);
		prn.printLine("");
		while (true) {
			item = cat_it->d[i];
			currLine = "-- ";
			currLine += item.desc.c_str();
			currLine += " --";
			prn.printLine(currLine);
			if (item.name.size()) {
				currLine = i18n("Username");
				currLine += ": ";
				currLine += item.name.c_str();
				prn.printLine(currLine);
			}
			if (item.pw.size()) {
				currLine = i18n("Password");
				currLine += ": ";
				currLine += item.pw.c_str();
				prn.printLine(currLine);
			}
			if (item.comment.size()) {
				currLine = i18n("Comment");
				currLine += ": ";
				currLine += item.comment.c_str();
				prn.printLine(currLine);
			}
			if (item.url.size()) {
				currLine = i18n("URL");
				currLine += ": ";
				currLine += item.url.c_str();
				prn.printLine(currLine);
			}
			if (item.launcher.size()) {
				currLine = i18n("Launcher");
				currLine += ": ";
				currLine += item.launcher.c_str();
				prn.printLine(currLine);
			}

			++i;
			if (i >= size)
				break;
			prn.printLine("");
		}
		++cat_it;
	}
}

QString PwMPrint::genDateString()
{
	QDateTime dt = QDateTime::currentDateTime();
	QString ret(dt.toString(Qt::LocalDate));
	return ret;
}

QStringList PwMPrint::selCategories()
{
	ListObjSelectWnd selWnd(i18n("Print categories"),
				i18n("Please select the categories "
				     "you want to print."),
				parent, true);
	QStringList catList;
	doc->getCategoryList(&catList);
	selWnd.setList(catList);
	if (selWnd.exec() != 1)
		return QStringList();
	return selWnd.getSelectedList();
}