From dccb0a99515b2f5d4c0b0c4d1a0c53be3b35887c Mon Sep 17 00:00:00 2001 From: Dubslow Date: Thu, 4 Dec 2014 10:55:14 -0600 Subject: [PATCH] finishing touches --- src/historykeeper.cpp | 18 +++++----- src/historykeeper.h | 2 +- src/widget/form/settings/privacyform.cpp | 42 ++++++++---------------- 3 files changed, 24 insertions(+), 38 deletions(-) diff --git a/src/historykeeper.cpp b/src/historykeeper.cpp index 78a2a05c0..d72f6a09d 100644 --- a/src/historykeeper.cpp +++ b/src/historykeeper.cpp @@ -138,12 +138,6 @@ HistoryKeeper::~HistoryKeeper() delete db; } -void HistoryKeeper::reencrypt(QString newpw) -{ - // TODO: this needs to appropriately set the core password as well - // if newpw.isEmpty(), then use the other core password -} - qint64 HistoryKeeper::addChatEntry(const QString& chat, const QString& message, const QString& sender, const QDateTime &dt, bool isSent) { QList cmds = generateAddChatEntryCmd(chat, message, sender, dt, isSent); @@ -406,10 +400,18 @@ bool HistoryKeeper::isFileExist() bool HistoryKeeper::removeHistory(int encrypted) { - Q_UNUSED(encrypted); resetInstance(); - QString path = getHistoryPath(); + QString path = getHistoryPath(QString(), encrypted); QFile DbFile(path); return DbFile.remove(); } + +QList HistoryKeeper::exportMessagesDeleteFile(int encrypted) +{ + auto msgs = getInstance()->exportMessages(); + qDebug() << "HistoryKeeper: count" << msgs.size() << "messages exported"; + if (!removeHistory(encrypted)) + qWarning() << "HistoryKeeper: couldn't delete old log file!"; + return msgs; +} diff --git a/src/historykeeper.h b/src/historykeeper.h index 96b92212b..ad80b536f 100644 --- a/src/historykeeper.h +++ b/src/historykeeper.h @@ -52,12 +52,12 @@ public: static bool isFileExist(); static void renameHistory(QString from, QString to); static bool removeHistory(int encrypted = -1); + static QList exportMessagesDeleteFile(int encrypted = -1); qint64 addChatEntry(const QString& chat, const QString& message, const QString& sender, const QDateTime &dt, bool isSent); qint64 addGroupChatEntry(const QString& chat, const QString& message, const QString& sender, const QDateTime &dt); QList getChatHistory(ChatType ct, const QString &chat, const QDateTime &time_from, const QDateTime &time_to); void markAsSent(int m_id); - void reencrypt(QString newpw); QList exportMessages(); void importMessages(const QList &lst); diff --git a/src/widget/form/settings/privacyform.cpp b/src/widget/form/settings/privacyform.cpp index d5f34bb29..6ee6624bb 100644 --- a/src/widget/form/settings/privacyform.cpp +++ b/src/widget/form/settings/privacyform.cpp @@ -77,24 +77,23 @@ bool PrivacyForm::setChatLogsPassword() if (int r = dialog->exec()) { + QList oldMessages = HistoryKeeper::exportMessagesDeleteFile(); + QString newpw = dialog->getPassword(); delete dialog; - if (!HistoryKeeper::checkPassword(true)) - if (checkContinue(tr("Old encrypted chat logs", "title"), - tr("Would you like to re-encrypt your old chat logs?\nOtherwise they will be deleted.", "body"))) - { - HistoryKeeper::getInstance()->reencrypt(r == 2 ? QString() : newpw); - // will set core and reset itself - return true; - } - // @apprb you resetInstance() in the old code but wouldn't that wipe out the current unencrypted history? - // current history should of course just become encrypted if (r == 2) core->useOtherPassword(Core::ptHistory); else core->setPassword(newpw, Core::ptHistory); - // HistoryKeeper::encryptPlain(); + + //if (!HistoryKeeper::checkPassword(true)) + // if (checkContinue(tr("Old encrypted chat logs", "title"), + // tr("Would you like to re-encrypt your old chat logs?\nOtherwise they will be deleted.", "body"))) + // for now, don't bother asking. Why wouldn't you want to reencrypt? + + HistoryKeeper::getInstance()->importMessages(oldMessages); + return true; } else @@ -106,19 +105,7 @@ bool PrivacyForm::setChatLogsPassword() void PrivacyForm::onEncryptLogsUpdated() { - auto getOldMessages = []() - { - auto msgs = HistoryKeeper::getInstance()->exportMessages(); - qDebug() << "Loaded messages:" << msgs.size(); - bool delRet = HistoryKeeper::removeHistory(); - if (!delRet) - qWarning() << "HistoryKeeper::removeHistory() returned FALSE"; - HistoryKeeper::resetInstance(); // HistoryKeeper::removeHistory() invokes HistoryKeeper::removeHistory() but logic may be changed - return msgs; - }; - Core* core = Core::getInstance(); - QList oldMessages; if (bodyUI->cbEncryptHistory->isChecked()) { @@ -126,14 +113,11 @@ void PrivacyForm::onEncryptLogsUpdated() { if (setChatLogsPassword()) { - oldMessages = getOldMessages(); - Settings::getInstance().setEncryptLogs(true); bodyUI->cbEncryptHistory->setChecked(true); // not logically necessary, but more consistent (esp. if the logic changes) bodyUI->changeLogsPwButton->setEnabled(true); - HistoryKeeper::getInstance()->importMessages(oldMessages); return; } } @@ -142,14 +126,14 @@ void PrivacyForm::onEncryptLogsUpdated() { if (checkContinue(tr("Old encrypted chat logs", "title"), tr("Would you like to un-encrypt your chat logs?\nOtherwise they will be deleted."))) { - oldMessages = getOldMessages(); - + QList oldMessages = HistoryKeeper::exportMessagesDeleteFile(true); + core->clearPassword(Core::ptHistory); Settings::getInstance().setEncryptLogs(false); HistoryKeeper::getInstance()->importMessages(oldMessages); } else { - HistoryKeeper::resetInstance(); + HistoryKeeper::removeHistory(true); } }