accounts.cpp 12.6 KB
Newer Older
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
/*
 * Copyright (C) 2014-2016 CZ.NIC
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of portions of this program with the
 * OpenSSL library under certain conditions as described in each
 * individual source file, and distribute linked combinations including
 * the two.
 */

#include <QDebug>
#include <QMessageBox>
#include <QSettings>
#include <QUrl>

#include "src/accounts.h"
#include "src/common.h"
#include "src/net/isds_wrapper.h"
#include "src/net/isds_session.h"
#include "src/settings.h"
#include "src/sqlite/account_db.h"
#include "src/models/accountmodel.h"
#include "src/sqlite/message_db_container.h"
#include "src/sqlite/file_db_container.h"

Accounts::Accounts(QObject *parent) : QObject(parent)
{
}


/* ========================================================================= */
/*
 * Slot: Update new message counter.
 */
void Accounts::updateNewMessageCounter(const QString &userName)
/* ========================================================================= */
{
	qDebug("%s()", __func__);

	MessageDb *msgDb = NULL;
54
55
	msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation, userName,
	    AccountListModel::globAccounts[userName].storeToDisk());
56
57
	if (msgDb == NULL) {
		qDebug() << "ERROR: Message database cannot open!";
58
		return;
59
60
61
	}

	globAccountsModelPtr->updateCounters(userName,
62
63
	    msgDb->getCntOfNewMsgs(RECEIVED_MESSAGE), -1,
	    msgDb->getCntOfNewMsgs(SENT_MESSAGE), -1);
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
}


/* ========================================================================= */
/*
 * Slot: Remove account.
 */
void Accounts::getAccountData(const QString &userName)
/* ========================================================================= */
{
	qDebug("%s()", __func__);

	if (!AccountListModel::globAccounts.contains(userName)) {
		return;
	}

	const AcntData &acntData(AccountListModel::globAccounts[userName]);

	/* Set login method model index for combobox in QML page.
	 * See AccountSettingsPage.qml for logim method order. Default is:
	 *    0 .. Username + Password
	 *    1 .. Certificate + Password
	 *    2 .. Password + Security code
	 *    3 .. Password + Security SMS
	 */
	int qmlLoginMethodIndex = 0;
	if (acntData.loginMethod() == USERNAME_PWD_CERT) {
		qmlLoginMethodIndex = 1;
	} else if (acntData.loginMethod() == USERNAME_PWD_HOTP) {
		qmlLoginMethodIndex = 2;
	} else if (acntData.loginMethod() == USERNAME_PWD_TOTP) {
		qmlLoginMethodIndex = 3;
	}

	emit sendAccountData(acntData.accountName(), userName,
	    qmlLoginMethodIndex, acntData.password(), acntData.isTestAccount(),
	    acntData.rememberPwd(), acntData.storeToDisk(), acntData.certPath());
}


/* ========================================================================= */
/*
 * Slot: Remove account.
 */
108
bool Accounts::removeAccount(const QString &userName, bool showDialog)
109
110
111
112
/* ========================================================================= */
{
	qDebug("%s()", __func__);

113
114
115
116
117
118
119
120
121
122
	if (showDialog) {
		QMessageBox msgBox;
		msgBox.setWindowTitle(tr("Remove account: %1").arg(userName));
		msgBox.setIcon(QMessageBox::Question);
		msgBox.setText(tr("Do you want to remove the account '%1'?").arg(userName));
		msgBox.setInformativeText(tr("Note: It will also remove all related "
		    "local databases and account information."));
		msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
		msgBox.setDefaultButton(QMessageBox::No);
		if (msgBox.exec() == QMessageBox::No) {
123
			return false;
124
		}
125
126
127
	}

	if (!AccountListModel::globAccounts.contains(userName)) {
128
		return false;
129
130
131
132
	}

	/* Delete file database */
	FileDb *fDb = NULL;
133
134
	fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation, userName,
	    AccountListModel::globAccounts[userName].storeToDisk());
135
136
	if (fDb == NULL) {
		qDebug() << "ERROR: File database open error!";
137
		return false;
138
139
140
	}
	if (!globFileDbsPtr->deleteDb(fDb)) {
		qDebug() << "ERROR: File database could not delete!";
141
		return false;
142
143
144
145
	}

	/* Delete message database */
	MessageDb *msgDb = NULL;
146
147
	msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation, userName,
	    AccountListModel::globAccounts[userName].storeToDisk());
148
149
	if (msgDb == NULL) {
		qDebug() << "ERROR: Message database open error!";
150
		return false;
151
152
153
	}
	if (!globMessageDbsPtr->deleteDb(msgDb)) {
		qDebug() << "ERROR: Message database could not delete!";
154
		return false;
155
156
157
158
159
	}

	/* Delete account info from account database */
	if (!globAccountDbPtr->deleteAccountInfoFromDb(userName)) {
		qDebug() << "ERROR: Account info could not delete!";
160
		return false;
161
162
	}

163
164
165
	/* Delete user info from account database */
	if (!globAccountDbPtr->deleteUserInfoFromDb(userName)) {
		qDebug() << "ERROR: User info could not delete!";
166
		return false;
167
168
	}

169
170
171
172
173
174
	/* Delete isds session context */
	emit removeIsdsCtx(userName);

	/* Delete account from settings */
	globAccountsModelPtr->deleteAccount(userName);
	saveAllSettings();
175
176

	return true;
177
178
179
180
181
182
183
184
185
}


/* ========================================================================= */
/*
 * Slot: Update account.
 */
void Accounts::updateAccount(const QString &loginMetod,
    const QString &acntName, const QString &userName,
186
    const QString &pwd, bool isTestAccount, bool rememberPwd,
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
    bool storeToDisk, const QString &certPath)
/* ========================================================================= */
{
	qDebug("%s()", __func__);

	if (acntName.isEmpty()) {
		showErrorMessageBox(tr("Problem while updating account"),
		    tr("Account name has not been specified!"),
		    tr("This field must be filled in."),
		    true);
		return;
	}

	if (!AccountListModel::globAccounts.contains(userName)) {
		return;
	}

	AcntData &acntData(AccountListModel::globAccounts[userName]);

	/* update account map */
	acntData.setLoginMethod(loginMetod);
	acntData.setAccountName(acntName.trimmed());
	acntData.setPassword(pwd.trimmed());
	acntData.setTestAccount(isTestAccount);
	acntData.setRememberPwd(rememberPwd);
212

213
	bool oldStoreToDisk = acntData.storeToDisk();
214
	/* Reopen database if storeToDisk changed. */
215
216
	if (acntData.storeToDisk() != storeToDisk) {
		MessageDb *mDb = globMessageDbsPtr->accessMessageDb(
217
		    globSet.dbsLocation, userName, oldStoreToDisk);
218
219
220
		if (mDb != NULL) {
			QString mDbFileName(mDb->fileName());
			/* Reopen database. */
221
222
223
224
			globMessageDbsPtr->deleteDb(mDb);
			globMessageDbsPtr->accessMessageDb(globSet.dbsLocation,
			    userName, storeToDisk);
			if (oldStoreToDisk) {
225
226
227
228
				/* Delete databases from storage. */
				QFile::remove(mDbFileName);
			}
		}
229
		FileDb *fDb = globFileDbsPtr->accessFileDb(
230
		    globSet.dbsLocation, userName, oldStoreToDisk);
231
232
233
		if (fDb != NULL) {
			QString fDbFileName(fDb->fileName());
			/* Reopen database. */
234
235
236
237
			globFileDbsPtr->deleteDb(fDb);
			globFileDbsPtr->accessFileDb(globSet.dbsLocation,
			    userName, storeToDisk);
			if (oldStoreToDisk) {
238
239
240
				/* Delete databases from storage. */
				QFile::remove(fDbFileName);
			}
241
242
		}

243
244
		/* Inform the model that message counters have changed. */
		globAccountsModelPtr->updateCounters(userName, 0, 0, 0, 0);
245
	}
246

247
	acntData.setStoreToDisk(storeToDisk);
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
	QUrl url(certPath, QUrl::TolerantMode);
	url.toLocalFile();
	acntData.setCertPath(url.toLocalFile());

	/* Model data have changed. */
	emit AccountListModel::globAccounts.accountDataChanged(userName);

	/* Delete isds session context */
	emit removeIsdsCtx(userName);

	/* update account data in settings */
	saveAllSettings();
}


/* ========================================================================= */
/*
 * Slot: Create account.
 */
267
bool Accounts::createAccount(const QString &loginMetod,
268
269
270
271
272
273
274
275
276
277
278
279
    const QString &acntName, const QString &userName,
    const QString &pwd, bool isTestAccount,  bool rememberPwd,
    bool storeToDisk, const QString &certPath)
/* ========================================================================= */
{
	qDebug("%s()", __func__);

	if (userName.isEmpty() || acntName.isEmpty()) {
		showErrorMessageBox(tr("Problem while creating account"),
		    tr("User name or account name has not been specified!"),
		    tr("These fields must be filled in."),
		    true);
280
		return false;
281
282
283
284
285
286
287
288
289
	}

	AcntData acntData;
	acntData.setAccountName(acntName.trimmed());
	acntData.setUserName(userName.trimmed());
	acntData.setPassword(pwd.trimmed());
	acntData.setLoginMethod(loginMetod);
	acntData.setTestAccount(isTestAccount);
	acntData.setRememberPwd(rememberPwd);
290
	acntData.setStoreToDisk(storeToDisk);
291
292
293
294
295
296
297
298
299
	QUrl url(certPath, QUrl::TolerantMode);
	url.toLocalFile();
	acntData.setCertPath(url.toLocalFile());

	if (globAccountsModelPtr->addAccount(acntData) == -2) {
		showErrorMessageBox(tr("Creating account: %1").arg(userName),
		    tr("Account '%1' could not be created.").arg(acntName),
		    tr("Account with user name '%1' already exists.").arg(userName),
		    true);
300
		return false;
301
	}
302
303

	return true;
304
305
}

306

307
308
309
310
311
312
313
/* ========================================================================= */
/*
 * Slot: Load account info from databse and fill QML page.
 */
void Accounts::saveAccountToSettings(void)
/* ========================================================================= */
{
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
	saveAllSettings();
}


/* ========================================================================= */
/*
 * Slot: Load account info from databse and fill QML page.
 */
void Accounts::fillAccountInfo(const QString &userName)
/* ========================================================================= */
{
	 qDebug("%s()", __func__);

	QString html = globAccountDbPtr->getAccountDetail(userName);
	if (!html.isEmpty()) {
		emit accountDetailChanged(html);
	}
}


/* ========================================================================= */
/*
 * Slot: Load model counters.
 */
Karel Slaný's avatar
Karel Slaný committed
338
void Accounts::loadModelCounters(void)
339
340
341
342
343
344
345
346
347
348
349
350
351
/* ========================================================================= */
{
	 qDebug("%s()", __func__);

	for (int row = 0; row < globAccountsModelPtr->rowCount(); ++row) {
		const QString userName(globAccountsModelPtr->userName(
		    globAccountsModelPtr->index(row, 0)));
		if (userName.isEmpty()) {
			Q_ASSERT(0);
			continue;
		}

		MessageDb *msgDb = NULL;
352
353
		msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation,
		    userName, AccountListModel::globAccounts[userName].storeToDisk());
354
355
356
357
358
359
360
		if (msgDb == NULL) {
			qWarning("Cannot open message database for '%s'.",
			    userName.toUtf8().constData());
			continue;
		}

		globAccountsModelPtr->updateCounters(userName,
361
		    msgDb->getCntOfNewMsgs(RECEIVED_MESSAGE),
362
		    msgDb->getMessageCount(RECEIVED_MESSAGE),
363
		    msgDb->getCntOfNewMsgs(SENT_MESSAGE),
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
		    msgDb->getMessageCount(SENT_MESSAGE));
	}
}


/* ========================================================================= */
/*
 * Func: Show error message box.
 */
void Accounts::showErrorMessageBox(const QString &title,
    const QString &text, const QString &informatinText, bool isError)
/* ========================================================================= */
{
	QMessageBox msgBox;
	msgBox.setWindowTitle(title);
	if (isError) {
		msgBox.setIcon(QMessageBox::Critical);
	} else {
		msgBox.setIcon(QMessageBox::Warning);
	}
	msgBox.setText(text);
	msgBox.setInformativeText(informatinText);
	msgBox.setStandardButtons(QMessageBox::Ok);
	msgBox.setDefaultButton(QMessageBox::Ok);
	msgBox.exec();
}


/* ========================================================================= */
/*
 * Func: Return string of account storage.
 */
const QString Accounts::getStorageText(bool toDisk)
/* ========================================================================= */
{

	if (toDisk) return tr("local storage");
	return tr("data in memory");
}


/* ========================================================================= */
/*
 * Func: Return string of account type.
 */
const QString Accounts::getAccountTypeText(bool isTest)
/* ========================================================================= */
{

	if (isTest) return tr("Test account");
	return tr("Standard account");
}
416
417
418
419
420
421
422
423
424
425
426
427


/* ========================================================================= */
/*
 * Slot: Received save account request from QML.
 *	TODO - Must be replaced via better solution in the future.
 */
void Accounts::saveAccountRequest(void)
/* ========================================================================= */
{
	emit saveAccountResponse();
}