File: MW_menu.cpp

package info (click to toggle)
xca 2.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,328 kB
  • sloc: cpp: 30,584; sh: 341; xml: 74; makefile: 56; python: 34
file content (265 lines) | stat: -rw-r--r-- 7,403 bytes parent folder | download | duplicates (2)
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
/* vi: set sw=4 ts=4:
 *
 * Copyright (C) 2001 - 2014 Christian Hohnstaedt.
 *
 * All rights reserved.
 */


#include "MainWindow.h"
#include "XcaApplication.h"
#include "PwDialog.h"
#include "Options.h"
#include "lib/load_obj.h"
#include "lib/pass_info.h"
#include "lib/pkcs11.h"
#include "lib/pki_evp.h"
#include "lib/pki_scard.h"
#include "lib/func.h"
#include "lib/db_x509super.h"
#include "lib/database_model.h"
#include "ui_Options.h"
#include "hashBox.h"
#include "OidResolver.h"
#include "OpenDb.h"
#include "Help.h"
#include <QApplication>
#include <QClipboard>
#include <QMenuBar>
#include <QMessageBox>
#include <QFileDialog>
#include <QActionGroup>

QAction *MainWindow::languageMenuEntry(const QStringList &sl)
{
	QString lang, tooltip;
	QLocale locale;

	if (sl[0].isEmpty()) {
		locale = QLocale::system();
		lang = MainWindow::tr("System");
	} else {
		locale = QLocale(sl[0]);
		lang = QString("%1 (%2)").arg(sl[1])
			.arg(QLocale::languageToString(locale.language()));
	}
	tooltip = locale.nativeLanguageName();

	if (sl.length() > 2)
		tooltip += " - " + sl[2];

	QAction *a = new QAction(lang, this);
	a->setToolTip(tooltip);
	a->setData(QVariant(locale));
	a->setDisabled(!XcaApplication::languageAvailable(locale));

	a->setCheckable(true);
	if (locale == QLocale())
		a->setChecked(true);
	return a;
}

void MainWindow::init_menu()
{
	static QMenu *file = NULL, *help = NULL, *import = NULL,
			*token = NULL, *languageMenu = NULL, *extra = NULL;
	static QActionGroup * langGroup = NULL;
	QAction *a, *options;

	delete file;
	delete help;
	delete import;
	delete token;
	delete extra;
	delete languageMenu;
	delete historyMenu;
	delete langGroup;

	wdMenuList.clear();
	scardList.clear();
	acList.clear();
	setMenuBar(new QMenuBar(nullptr));

	langGroup = new QActionGroup(this);

	languageMenu = new tipMenu(tr("Language"), this);
	connect(languageMenu, SIGNAL(triggered(QAction*)),
		qApp, SLOT(switchLanguage(QAction*)));

	foreach(const QStringList &sl, getTranslators()) {
		a = languageMenuEntry(sl);
		langGroup->addAction(a);
		languageMenu->addAction(a);
	}


#ifndef APPSTORE_COMPLIANT
	historyMenu = new tipMenu(tr("Recent DataBases") + " ...", this);
	update_history_menu();

	connect(historyMenu, SIGNAL(triggered(QAction*)),
	        this, SLOT(open_database(QAction*)));

	file = menuBar()->addMenu(tr("&File"));

	a = file->addAction(tr("New DataBase"), this, SLOT(new_database()));
	a->setShortcut(QKeySequence::New);
	a->setEnabled(OpenDb::hasSqLite());

	a = file->addAction(tr("Open DataBase"), this, SLOT(load_database()));
	a->setShortcut(QKeySequence::Open);
	a->setEnabled(OpenDb::hasSqLite());

	file->addAction(tr("Open Remote DataBase"),
			this, SLOT(openRemoteSqlDB()))->
			setEnabled(OpenDb::hasRemoteDrivers());
	file->addMenu(historyMenu);
	file->addAction(tr("Set as default DataBase"), this,
				SLOT(default_database()));
	a = file->addAction(tr("Close DataBase"), this, SLOT(close_database()));
	a->setShortcut(QKeySequence::Close);
	acList += a;

#endif
	options = new QAction(tr("Options"), this);
	connect(options, SIGNAL(triggered()), this, SLOT(setOptions()));
	options->setMenuRole(QAction::PreferencesRole);
#ifndef APPSTORE_COMPLIANT
	file->addAction(options);
#endif
	acList += options;

	a = new QAction(tr("Exit"), this);
	connect(a, SIGNAL(triggered()),
		qApp, SLOT(quit()), Qt::QueuedConnection);
	a->setMenuRole(QAction::QuitRole);
	a->setShortcut(QKeySequence::Quit);
#ifndef APPSTORE_COMPLIANT
	file->addMenu(languageMenu);
	file->addSeparator();
	file->addAction(a);
#endif

	import = menuBar()->addMenu(tr("I&mport"));
	import->addAction(tr("Keys"), keyView, SLOT(load()) );
	import->addAction(tr("Requests"), reqView, SLOT(load()) );
	import->addAction(tr("Certificates"), certView, SLOT(load()) );
	import->addAction(tr("PKCS#12"), certView, SLOT(loadPKCS12()) );
	import->addAction(tr("PKCS#7"), certView, SLOT(loadPKCS7()) );
	import->addAction(tr("Template"), tempView, SLOT(load()) );
	import->addAction(tr("Revocation list"), crlView, SLOT(load()));
	import->addAction(tr("PEM file"), this, SLOT(loadPem()) );
	import->addAction(tr("Paste PEM file"), this, SLOT(pastePem()))->
			setShortcut(QKeySequence::Paste);

#ifndef APPSTORE_COMPLIANT
	token = menuBar()->addMenu(tr("Token"));
	token->addAction(tr("&Manage Security token"), this,
				SLOT(manageToken()));
	token->addAction(tr("&Init Security token"),  this,
				SLOT(initToken()));
	token->addAction(tr("&Change PIN"), this,
				SLOT(changePin()) );
	token->addAction(tr("Change &SO PIN"), this,
				SLOT(changeSoPin()) );
	token->addAction(tr("Init PIN"), this,
				SLOT(initPin()) );
#endif
	extra = menuBar()->addMenu(tr("Extra"));
	acList += extra->addAction(tr("&Dump DataBase"), this,
				SLOT(dump_database()));
	acList += extra->addAction(tr("&Export Certificate Index"), this,
				SLOT(exportIndex()));
	acList += extra->addAction(tr("Export Certificate &Index hierarchy"), this,
				SLOT(exportIndexHierarchy()));
	acList += extra->addAction(tr("C&hange DataBase password"), this,
				SLOT(changeDbPass()));
#if 0
	acList += extra->addAction(tr("&Undelete items"), this,
				SLOT(undelete()));
#endif
	extra->addAction(tr("Generate DH parameter"), this,
				 SLOT(generateDHparam()));
	extra->addAction(tr("OID Resolver"), resolver, SLOT(show()));

#ifdef APPSTORE_COMPLIANT
	extra->addSeparator();
	extra->addMenu(languageMenu);
	extra->addAction(options);
#endif
	help = menuBar()->addMenu(tr("&Help") );
	help->addAction(tr("Content"), helpdlg, SLOT(content()))->
			setShortcut(QKeySequence::HelpContents);
	a = new QAction(tr("About"), this);
	connect(a, SIGNAL(triggered()), this, SLOT(about()));
	a->setMenuRole(QAction::AboutRole);
	a->setShortcut(QKeySequence::WhatsThis);
	help->addAction(a);
	wdMenuList += import;
#ifndef APPSTORE_COMPLIANT
	scardList += token;
#endif

	setItemEnabled(Database.isOpen());
}

void MainWindow::update_history_menu()
{
	QStringList hist = history.get();
	if (!historyMenu)
		return;
	historyMenu->clear();
	for (int i = 0, j = 0; i < hist.size(); i++) {
		QAction *a;
		QString txt = hist[i];
		if (!QFile::exists(txt) && !database_model::isRemoteDB(txt))
			continue;
		if (txt.size() > 33)
			txt = QString("...") + txt.mid(txt.size() - 30);
		a = historyMenu->addAction(QString("%1 %2").arg(j++).arg(txt));
		a->setData(QVariant(hist[i]));
		a->setToolTip(hist[i]);
	}
}

void MainWindow::open_database(QAction* a)
{
	init_database(a->data().toString());
}

void MainWindow::new_database()
{
	load_db l;
	QString selectedFilter;
	QString fname = QFileDialog::getSaveFileName(this, l.caption, homedir,
			l.filter, &selectedFilter, QFileDialog::DontConfirmOverwrite);
	// make sure that, if the 3 letter extension was left selected
	// in Qt's OS X file open dialog,
	// the filename actually ends with that extension.
	// Otherwise usability breaks in jarring ways.
	init_database(getFullFilename(fname, selectedFilter));
}

void MainWindow::load_database()
{
	load_db l;
	QString fname = QFileDialog::getOpenFileName(this, l.caption, homedir,
			l.filter);
	init_database(fname);
}

void MainWindow::setOptions()
{
	if (!QSqlDatabase::database().isOpen())
		return;

	Options *opt = new Options(this);
	if (opt->exec()) {
		reqView->showHideSections();
		certView->showHideSections();
	}
	delete opt;

	pkcs11::libraries.load(Settings["pkcs11path"]);
	enableTokenMenu(pkcs11::libraries.loaded());
}