From 7dc36a024d2525494a75ee23ae64e6c232d552ce Mon Sep 17 00:00:00 2001 From: Janosch Knack Date: Sat, 19 Nov 2016 23:55:26 +0100 Subject: mainwindow now uses the new QtPassSettings --- mainwindow.cpp | 283 ++++++++++++++++++++--------------------------------- mainwindow.h | 3 - qtpasssettings.cpp | 30 ++++-- qtpasssettings.h | 5 +- 4 files changed, 133 insertions(+), 188 deletions(-) diff --git a/mainwindow.cpp b/mainwindow.cpp index ebdd025b..1825d188 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -103,26 +103,6 @@ MainWindow::~MainWindow() { #endif } -/** - * @brief MainWindow::getSettings make sure to only have one set of settings. - * @return QScopedPointer settings - */ -QSettings &MainWindow::getSettings() { - if (!settings) { - QString portable_ini = QCoreApplication::applicationDirPath() + - QDir::separator() + "qtpass.ini"; - // qDebug() << "Settings file: " + portable_ini; - if (QFile(portable_ini).exists()) { - // qDebug() << "Settings file exists, loading it in"; - settings.reset(new QSettings(portable_ini, QSettings::IniFormat)); - } else { - // qDebug() << "Settings file does not exist, use defaults"; - settings.reset(new QSettings("IJHack", "QtPass")); - } - } - return *settings; -} - /** * @brief MainWindow::changeEvent sets focus to the search box * @param event @@ -203,95 +183,77 @@ void MainWindow::mountWebDav() { * possible */ bool MainWindow::checkConfig() { - QSettings &settings(getSettings()); - QString version = QtPassSettings::getVersion(); if (freshStart) { - settings.beginGroup(SettingsConstants::groupMainwindow); - restoreGeometry(settings.value(SettingsConstants::geometry, saveGeometry()).toByteArray()); - restoreState(settings.value(SettingsConstants::savestate, saveState()).toByteArray()); - move(settings.value(SettingsConstants::pos, pos()).toPoint()); - resize(settings.value(SettingsConstants::size, size()).toSize()); + QByteArray geometry = QtPassSettings::getGeometry(saveGeometry()); + restoreGeometry(geometry); + QByteArray savestate = QtPassSettings::getSavestate(saveState()); + restoreState(savestate); + QPoint position = QtPassSettings::getPos(pos()); + move(position); + QSize newSize = QtPassSettings::getSize(size()); + resize(newSize); QList splitter = ui->splitter->sizes(); - int left = settings.value(SettingsConstants::splitterLeft, splitter[0]).toInt(); - int right = settings.value(SettingsConstants::splitterRight, splitter[1]).toInt(); + int left = QtPassSettings::getSplitterLeft(splitter[0]); + int right = QtPassSettings::getSplitterRight(splitter[1]); if (left > 0 || right > 0) { splitter[0] = left; splitter[1] = right; ui->splitter->setSizes(splitter); } - if (settings.value(SettingsConstants::maximized, isMaximized()).toBool()) + if (QtPassSettings::isMaximized(isMaximized())){ showMaximized(); - settings.endGroup(); + } } - usePass = (settings.value(SettingsConstants::usePass) == "true"); - - useAutoclear = (settings.value(SettingsConstants::useAutoclear) == "true"); - autoclearSeconds = settings.value(SettingsConstants::autoclearSeconds).toInt(); - useAutoclearPanel = (settings.value(SettingsConstants::useAutoclearPanel) == "true"); - autoclearPanelSeconds = settings.value(SettingsConstants::autoclearPanelSeconds).toInt(); - hidePassword = (settings.value(SettingsConstants::hidePassword) == "true"); - hideContent = (settings.value(SettingsConstants::hideContent) == "true"); - addGPGId = (settings.value(SettingsConstants::addGPGId) != "false"); + usePass = QtPassSettings::isUsePass(); - passStore = settings.value(SettingsConstants::passStore).toString(); - if (passStore.isEmpty()) { - passStore = Util::findPasswordStore(); - settings.setValue(SettingsConstants::passStore, passStore); - } - // ensure directory exists if never used pass or misconfigured. - // otherwise process->setWorkingDirectory(passStore); will fail on execution. - QDir().mkdir(passStore); + useAutoclear = QtPassSettings::isUseAutoclear(); + autoclearSeconds = QtPassSettings::getAutoclearSeconds(); + useAutoclearPanel = QtPassSettings::isUseAutoclearPanel(); + autoclearPanelSeconds = QtPassSettings::getAutoclearPanelSeconds(); + hidePassword = QtPassSettings::isHidePassword(); + hideContent = QtPassSettings::isHideContent(); + addGPGId = QtPassSettings::isAddGPGId(true); + passStore = QtPassSettings::getPassStore(Util::findPasswordStore()); passStore = Util::normalizeFolderPath(passStore); + QtPassSettings::setPassStore(passStore); + + passExecutable = QtPassSettings::getPassExecutable(Util::findBinaryInPath("pass")); + + gitExecutable = QtPassSettings::getGitExecutable(Util::findBinaryInPath("git")); - passExecutable = settings.value(SettingsConstants::passExecutable).toString(); - if (passExecutable.isEmpty()) - passExecutable = Util::findBinaryInPath("pass"); - - gitExecutable = settings.value(SettingsConstants::gitExecutable).toString(); - if (gitExecutable.isEmpty()) - gitExecutable = Util::findBinaryInPath("git"); - - gpgExecutable = settings.value(SettingsConstants::gpgExecutable).toString(); - if (gpgExecutable.isEmpty()) - gpgExecutable = Util::findBinaryInPath("gpg2"); - - pwgenExecutable = settings.value(SettingsConstants::pwgenExecutable).toString(); - if (pwgenExecutable.isEmpty()) - pwgenExecutable = Util::findBinaryInPath("pwgen"); - - gpgHome = settings.value(SettingsConstants::gpgHome).toString(); - - useWebDav = (settings.value(SettingsConstants::useWebDav) == "true"); - webDavUrl = settings.value(SettingsConstants::webDavUrl).toString(); - webDavUser = settings.value(SettingsConstants::webDavUser).toString(); - webDavPassword = settings.value(SettingsConstants::webDavPassword).toString(); - - profile = settings.value(SettingsConstants::profile).toString(); - settings.beginGroup(SettingsConstants::groupProfiles); - QStringList keys = settings.childKeys(); - foreach (QString key, keys) - profiles[key] = settings.value(key).toString(); - settings.endGroup(); - - useGit = (settings.value(SettingsConstants::useGit) == "true"); - usePwgen = (settings.value(SettingsConstants::usePwgen) == "true"); - avoidCapitals = settings.value(SettingsConstants::avoidCapitals).toBool(); - avoidNumbers = settings.value(SettingsConstants::avoidNumbers).toBool(); - lessRandom = settings.value(SettingsConstants::lessRandom).toBool(); - useSymbols = (settings.value(SettingsConstants::useSymbols) == "true"); - pwdConfig.selected = settings.value(SettingsConstants::passwordCharsSelected).toInt(); - pwdConfig.length = settings.value(SettingsConstants::passwordLength).toInt(); - pwdConfig.selected = settings.value(SettingsConstants::passwordCharsselection).toInt(); - pwdConfig.Characters[3] = settings.value(SettingsConstants::passwordChars).toString(); - - useTrayIcon = settings.value(SettingsConstants::useTrayIcon).toBool(); - hideOnClose = settings.value(SettingsConstants::hideOnClose).toBool(); - startMinimized = settings.value(SettingsConstants::startMinimized).toBool(); - alwaysOnTop = settings.value(SettingsConstants::alwaysOnTop).toBool(); + gpgExecutable = QtPassSettings::getGpgExecutable(Util::findBinaryInPath("gpg2")); + + pwgenExecutable = QtPassSettings::getPwgenExecutable(Util::findBinaryInPath("pwgen")); + + gpgHome = QtPassSettings::getGpgHome(); + + useWebDav = QtPassSettings::isUseWebDav(); + webDavUrl = QtPassSettings::getWebDavUrl(); + webDavUser = QtPassSettings::getWebDavUser(); + webDavPassword = QtPassSettings::getWebDavPassword(); + + profile = QtPassSettings::getProfile(); + profiles = QtPassSettings::getProfiles(); + + useGit = QtPassSettings::isUseGit(); + usePwgen = QtPassSettings::isUsePwgen(); + avoidCapitals = QtPassSettings::isAvoidCapitals(); + avoidNumbers = QtPassSettings::isAvoidNumbers(); + lessRandom = QtPassSettings::isLessRandom(); + useSymbols = QtPassSettings::isUseSymbols(); + pwdConfig.selected = QtPassSettings::getPasswordCharsSelected(); + pwdConfig.length = QtPassSettings::getPasswordLength(); + pwdConfig.selected = QtPassSettings::getPasswordCharsselection(); + pwdConfig.Characters[3] = QtPassSettings::getPasswordChars(); + + useTrayIcon = QtPassSettings::isUseTrayIcon(); + hideOnClose = QtPassSettings::isHideOnClose(); + startMinimized = QtPassSettings::isStartMinimized(); + alwaysOnTop = QtPassSettings::isAlwaysOnTop(); if (alwaysOnTop) { Qt::WindowFlags flags = windowFlags(); @@ -299,8 +261,8 @@ bool MainWindow::checkConfig() { this->show(); } - autoPull = settings.value(SettingsConstants::autoPull).toBool(); - autoPush = settings.value(SettingsConstants::autoPush).toBool(); + autoPull = QtPassSettings::isAutoPull(); + autoPush = QtPassSettings::isAutoPush(); if (useTrayIcon && tray == NULL) { initTrayIcon(); @@ -312,9 +274,9 @@ bool MainWindow::checkConfig() { destroyTrayIcon(); } - passTemplate = settings.value(SettingsConstants::passTemplate).toString(); - useTemplate = settings.value(SettingsConstants::useTemplate).toBool(); - templateAllFields = settings.value(SettingsConstants::templateAllFields).toBool(); + passTemplate = QtPassSettings::getPassTemplate(); + useTemplate = QtPassSettings::isUseTemplate(); + templateAllFields = QtPassSettings::isTemplateAllFields(); // qDebug() << version; @@ -340,7 +302,7 @@ bool MainWindow::checkConfig() { passTemplate = "login\nurl"; } - settings.setValue(SettingsConstants::version, VERSION); + QtPassSettings::setVersion(VERSION); if (Util::checkConfig(passStore, passExecutable, gpgExecutable)) { config(); @@ -504,52 +466,39 @@ void MainWindow::config() { autoPull = d->autoPull(); alwaysOnTop = d->alwaysOnTop(); - QSettings &settings(getSettings()); - - settings.setValue(SettingsConstants::version, VERSION); - settings.setValue(SettingsConstants::passExecutable, passExecutable); - settings.setValue(SettingsConstants::gitExecutable, gitExecutable); - settings.setValue(SettingsConstants::gpgExecutable, gpgExecutable); - settings.setValue(SettingsConstants::passStore, passStore); - settings.setValue(SettingsConstants::usePass, usePass ? "true" : "false"); - switch (useClipboard) { - case Enums::CLIPBOARD_ALWAYS: - settings.setValue(SettingsConstants::useClipboard, "true"); - break; - case Enums::CLIPBOARD_ON_DEMAND: - settings.setValue(SettingsConstants::useClipboard, "2"); - break; - default: - settings.setValue(SettingsConstants::useClipboard, "false"); - break; - } - settings.setValue(SettingsConstants::useAutoclear, useAutoclear ? "true" : "false"); - settings.setValue(SettingsConstants::autoclearSeconds, autoclearSeconds); - settings.setValue(SettingsConstants::useAutoclearPanel, - useAutoclearPanel ? "true" : "false"); - settings.setValue(SettingsConstants::autoclearPanelSeconds, autoclearPanelSeconds); - settings.setValue(SettingsConstants::hidePassword, hidePassword ? "true" : "false"); - settings.setValue(SettingsConstants::hideContent, hideContent ? "true" : "false"); - settings.setValue(SettingsConstants::addGPGId, addGPGId ? "true" : "false"); - settings.setValue(SettingsConstants::useTrayIcon, useTrayIcon ? "true" : "false"); - settings.setValue(SettingsConstants::hideOnClose, hideOnClose ? "true" : "false"); - settings.setValue(SettingsConstants::startMinimized, startMinimized ? "true" : "false"); - settings.setValue(SettingsConstants::useGit, useGit ? "true" : "false"); - settings.setValue(SettingsConstants::pwgenExecutable, pwgenExecutable); - settings.setValue(SettingsConstants::usePwgen, usePwgen ? "true" : "false"); - settings.setValue(SettingsConstants::avoidCapitals, avoidCapitals ? "true" : "false"); - settings.setValue(SettingsConstants::avoidNumbers, avoidNumbers ? "true" : "false"); - settings.setValue(SettingsConstants::lessRandom, lessRandom ? "true" : "false"); - settings.setValue(SettingsConstants::useSymbols, useSymbols ? "true" : "false"); - settings.setValue(SettingsConstants::passwordLength, pwdConfig.length); - settings.setValue(SettingsConstants::passwordCharsselection, pwdConfig.selected); - settings.setValue(SettingsConstants::passwordChars, pwdConfig.Characters[3]); - settings.setValue(SettingsConstants::useTemplate, useTemplate); - settings.setValue(SettingsConstants::passTemplate, passTemplate); - settings.setValue(SettingsConstants::templateAllFields, templateAllFields); - settings.setValue(SettingsConstants::autoPull, autoPull ? "true" : "false"); - settings.setValue(SettingsConstants::autoPush, autoPush ? "true" : "false"); - settings.setValue(SettingsConstants::alwaysOnTop, alwaysOnTop ? "true" : "false"); + QtPassSettings::setVersion(VERSION); + QtPassSettings::setPassExecutable(passExecutable); + QtPassSettings::setGitExecutable(gitExecutable); + QtPassSettings::setGpgExecutable(gpgExecutable); + QtPassSettings::setPassStore(passStore); + QtPassSettings::setUsePass(usePass); + QtPassSettings::setClipBoardType(useClipboard); + QtPassSettings::setUseAutoclear(useAutoclear); + QtPassSettings::setAutoclearSeconds(autoclearSeconds); + QtPassSettings::setUseAutoclearPanel(useAutoclearPanel); + QtPassSettings::setAutoclearPanelSeconds(autoclearPanelSeconds); + QtPassSettings::setHidePassword(hidePassword); + QtPassSettings::setHideContent(hideContent); + QtPassSettings::setAddGPGId(addGPGId); + QtPassSettings::setUseTrayIcon(useTrayIcon); + QtPassSettings::setHideOnClose(hideOnClose); + QtPassSettings::setStartMinimized(startMinimized); + QtPassSettings::setUseGit(useGit); + QtPassSettings::setPwgenExecutable(pwgenExecutable); + QtPassSettings::setUsePwgen(usePwgen); + QtPassSettings::setAvoidCapitals(avoidCapitals); + QtPassSettings::setAvoidNumbers(avoidNumbers); + QtPassSettings::setLessRandom(lessRandom); + QtPassSettings::setUseSymbols(useSymbols); + QtPassSettings::setPasswordLength(pwdConfig.length); + QtPassSettings::setPasswordCharsselection(pwdConfig.selected); + QtPassSettings::setPasswordChars(pwdConfig.Characters[3]); + QtPassSettings::setUseTemplate(useTemplate); + QtPassSettings::setPassTemplate(passTemplate); + QtPassSettings::setTemplateAllFields(templateAllFields); + QtPassSettings::setAutoPull(autoPull); + QtPassSettings::setAutoPush(autoPush); + QtPassSettings::setAlwaysOnTop(alwaysOnTop); if (alwaysOnTop) { Qt::WindowFlags flags = windowFlags(); @@ -560,26 +509,7 @@ void MainWindow::config() { this->show(); } - if (!profiles.isEmpty()) { - settings.beginGroup("profiles"); - settings.remove(""); - bool profileExists = false; - QHashIterator i(profiles); - while (i.hasNext()) { - i.next(); - // qDebug() << i.key() + "|" + i.value(); - if (i.key() == profile) - profileExists = true; - settings.setValue(i.key(), i.value()); - } - if (!profileExists) { - // just take the last one - profile = i.key(); - } - settings.endGroup(); - } else { - settings.remove("profiles"); - } + QtPassSettings::setProfiles(profiles); updateProfileBox(); ui->treeView->setRootIndex( proxyModel.mapFromSource(model.setRootPath(passStore))); @@ -1644,10 +1574,8 @@ void MainWindow::on_profileBox_currentIndexChanged(QString name) { passStore = profiles[name]; ui->statusBar->showMessage(tr("Profile changed to %1").arg(name), 2000); - QSettings &settings(getSettings()); - - settings.setValue(SettingsConstants::profile, profile); - settings.setValue(SettingsConstants::passStore, passStore); + QtPassSettings::setProfile(profile); + QtPassSettings::setPassStore(passStore); // qDebug() << env; QStringList store = env.filter("PASSWORD_STORE_DIR"); @@ -1705,19 +1633,18 @@ void MainWindow::closeEvent(QCloseEvent *event) { event->ignore(); } else { clearClipboard(); - settings->beginGroup(SettingsConstants::groupMainwindow); - settings->setValue(SettingsConstants::geometry, saveGeometry()); - settings->setValue(SettingsConstants::savestate, saveState()); - settings->setValue(SettingsConstants::maximized, isMaximized()); + QtPassSettings::setGeometry(saveGeometry()); + QtPassSettings::setSavestate(saveState()); + QtPassSettings::setMaximized(isMaximized()); if (!isMaximized()) { - settings->setValue(SettingsConstants::pos, pos()); - settings->setValue(SettingsConstants::size, size()); + QtPassSettings::setPos(pos()); + QtPassSettings::setSize(size()); } - settings->setValue(SettingsConstants::splitterLeft, ui->splitter->sizes()[0]); - settings->setValue(SettingsConstants::splitterRight, ui->splitter->sizes()[1]); - settings->endGroup(); + QtPassSettings::setSplitterLeft(ui->splitter->sizes()[0]); + QtPassSettings::setSplitterRight(ui->splitter->sizes()[1]); event->accept(); } + QtPassSettings::saveAllSettings(); } /** diff --git a/mainwindow.h b/mainwindow.h index d64f099f..69c8fc86 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -8,7 +8,6 @@ #include #include #include -#include #include #include @@ -155,7 +154,6 @@ private: QAction *actionAddFolder; QApplication *QtPass; - QScopedPointer settings; QScopedPointer ui; QFileSystemModel model; StoreModel proxyModel; @@ -221,7 +219,6 @@ private: QString getDir(const QModelIndex &, bool); QString getFile(const QModelIndex &, bool); void setPassword(QString, bool, bool); - QSettings &getSettings(); QList listKeys(QString keystring = "", bool secret = false); QStringList getRecipientList(QString for_file); QString getRecipientString(QString for_file, QString separator = " ", diff --git a/qtpasssettings.cpp b/qtpasssettings.cpp index 646e9cee..bfe2ca48 100644 --- a/qtpasssettings.cpp +++ b/qtpasssettings.cpp @@ -238,7 +238,11 @@ void QtPassSettings::setAddGPGId(const bool &addGPGId) QString QtPassSettings::getPassStore(const QString &defaultValue) { - return getStringValue("passStore", defaultValue); + QString returnValue = getStringValue("passStore", defaultValue); + // ensure directory exists if never used pass or misconfigured. + // otherwise process->setWorkingDirectory(passStore); will fail on execution. + QDir().mkdir(returnValue); + return returnValue; } void QtPassSettings::setPassStore(const QString &passStore) @@ -553,16 +557,25 @@ QHash QtPassSettings::getProfiles() return profiles; } +void QtPassSettings::setProfiles(QHash &profiles) +{ + getSettings().remove("profiles"); + beginSettingsGroup("profiles"); + QHash::iterator i=profiles.begin(); + for(; i !=profiles.end(); ++i) + { + setSetting(i.key() ,i.value()); + } + endSettingsGroup(); +} + QSettings &QtPassSettings::getSettings() { if (!QtPassSettings::initialized) { QString portable_ini = QCoreApplication::applicationDirPath() + QDir::separator() + "qtpass.ini"; - // qDebug() << "Settings file: " + portable_ini; if (QFile(portable_ini).exists()) { - // qDebug() << "Settings file exists, loading it in"; settings.reset(new QSettings(portable_ini, QSettings::IniFormat)); } else { - // qDebug() << "Settings file does not exist, use defaults"; settings.reset(new QSettings("IJHack", "QtPass")); } } @@ -701,7 +714,12 @@ void QtPassSettings::beginProfilesGroup() } -QVariant QtPassSettings::getSetting(const QString &key, const QVariant &defalutValue) +QVariant QtPassSettings::getSetting(const QString &key, const QVariant &defaultValue) +{ + return getSettings().value(key, defaultValue); +} + +void QtPassSettings::setSetting(const QString &key, const QVariant &value) { - return getSettings().value(key, defalutValue); + getSettings().setValue(key, value); } diff --git a/qtpasssettings.h b/qtpasssettings.h index 2eec2726..06518545 100644 --- a/qtpasssettings.h +++ b/qtpasssettings.h @@ -165,7 +165,9 @@ public: static bool isTemplateAllFields(const bool &defaultValue = QVariant().toBool()); static void setTemplateAllFields(const bool &templateAllFields); - QHash getProfiles(); + static QHash getProfiles(); + static void setProfiles(QHash &profiles); + signals: public slots: @@ -211,6 +213,7 @@ private: static void beginProfilesGroup(); static QVariant getSetting(const QString &key, const QVariant &defaultValue = QVariant()); + static void setSetting(const QString &key, const QVariant &value); }; #endif // QTPASSSETTINGS_H -- cgit v1.2.3