diff options
-rw-r--r-- | datahelpers.h | 96 | ||||
-rw-r--r-- | enums.h | 13 | ||||
-rw-r--r-- | mainwindow.cpp | 51 | ||||
-rw-r--r-- | mainwindow.h | 37 | ||||
-rw-r--r-- | pass.cpp | 5 | ||||
-rw-r--r-- | pass.h | 5 | ||||
-rw-r--r-- | passworddialog.cpp | 15 | ||||
-rw-r--r-- | passworddialog.h | 10 | ||||
-rw-r--r-- | qtpass.pro | 7 | ||||
-rw-r--r-- | qtpasssettings.cpp | 9 | ||||
-rw-r--r-- | qtpasssettings.h | 4 | ||||
-rw-r--r-- | settingsconstants.cpp | 2 | ||||
-rw-r--r-- | settingsconstants.h | 1 | ||||
-rw-r--r-- | usersdialog.h | 55 |
14 files changed, 141 insertions, 169 deletions
diff --git a/datahelpers.h b/datahelpers.h new file mode 100644 index 00000000..5daef810 --- /dev/null +++ b/datahelpers.h @@ -0,0 +1,96 @@ +#ifndef DATAHELPERS_H +#define DATAHELPERS_H + +#include <QDateTime> +#include <QString> + +/*! + \struct passwordConfiguration + \brief holds the Password configuration settings + */ +struct passwordConfiguration { + /** + * @brief passwordConfiguration::selected character set. + */ + enum characterSet { + ALLCHARS = 0, + ALPHABETICAL, + ALPHANUMERIC, + CUSTOM, + CHARSETS_COUNT // have to be last, for easier initialization of arrays + } selected; + /** + * @brief passwordConfiguration::length of password. + */ + int length; + /** + * @brief passwordConfiguration::Characters the different character sets. + */ + QString Characters[CHARSETS_COUNT]; + passwordConfiguration() : selected(ALLCHARS), length(16) { + Characters[ALLCHARS] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890~!@#$%^&" + "*()_-+={}[]|:;<>,.?"; /*AllChars*/ + Characters[ALPHABETICAL] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstu" + "vwxyz"; /*Only Alphabetical*/ + Characters[ALPHANUMERIC] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstu" + "vwxyz1234567890"; /*Alphabetical and Numerical*/ + Characters[CUSTOM] = Characters[ALLCHARS]; // this may be redefined by user + } +}; + +/*! + \struct UserInfo + \brief Stores key info lines including validity, creation date and more. + */ +struct UserInfo { + UserInfo() : validity('-'), have_secret(false), enabled(false) {} + + /** + * @brief UserInfo::fullyValid when validity is f or u. + * http://git.gnupg.org/cgi-bin/gitweb.cgi?p=gnupg.git;a=blob_plain;f=doc/DETAILS + */ + bool fullyValid() { return validity == 'f' || validity == 'u'; } + /** + * @brief UserInfo::marginallyValid when validity is m. + * http://git.gnupg.org/cgi-bin/gitweb.cgi?p=gnupg.git;a=blob_plain;f=doc/DETAILS + */ + bool marginallyValid() { return validity == 'm'; } + /** + * @brief UserInfo::isValid when fullyValid or marginallyValid. + */ + bool isValid() { return fullyValid() || marginallyValid(); } + + /** + * @brief UserInfo::name full name + */ + QString name; + /** + * @brief UserInfo::key_id hexadecimal representation + */ + QString key_id; + /** + * @brief UserInfo::validity GnuPG representation of validity + * http://git.gnupg.org/cgi-bin/gitweb.cgi?p=gnupg.git;a=blob_plain;f=doc/DETAILS + */ + char validity; + /** + * @brief UserInfo::have_secret secret key is available + * (can decrypt with this key) + */ + bool have_secret; + /** + * @brief UserInfo::enabled + */ + bool enabled; + /** + * @brief UserInfo::expiry date/time key expires + */ + QDateTime expiry; + /** + * @brief UserInfo::created date/time key was created + */ + QDateTime created; +}; + +#endif // DATAHELPERS_H @@ -14,19 +14,6 @@ public: CLIPBOARD_ALWAYS = 1, CLIPBOARD_ON_DEMAND = 2 }; - /** - * @brief Enums::characterSet enum - * 0 All character - * 1 Alphabetical - * 2 Alphanumeric - * 3 Custon (from config) - */ - enum characterSet { - ALLCHARS = 0, - ALPHABETICAL = 1, - ALPHANUMERIC = 2, - CUSTOM = 3 - }; }; #endif // ENUMS_H diff --git a/mainwindow.cpp b/mainwindow.cpp index d7165348..5c9d8996 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -78,7 +78,7 @@ MainWindow::MainWindow(QWidget *parent) clearClipboardTimer.setSingleShot(true); connect(&clearClipboardTimer, SIGNAL(timeout()), this, SLOT(clearClipboard())); - pwdConfig.selected = 0; + pwdConfig.selected = passwordConfiguration::ALLCHARS; if (!checkConfig()) { // no working config QApplication::quit(); @@ -258,10 +258,11 @@ bool MainWindow::checkConfig() { QtPassSettings::getPwgenExecutable(Util::findBinaryInPath("pwgen")); QtPassSettings::setPwgenExecutable(pwgenExecutable); - pwdConfig.selected = QtPassSettings::getPasswordCharsSelected(); pwdConfig.length = QtPassSettings::getPasswordLength(); - pwdConfig.selected = QtPassSettings::getPasswordCharsselection(); - pwdConfig.Characters[3] = QtPassSettings::getPasswordChars(); + pwdConfig.selected = static_cast<passwordConfiguration::characterSet>( + QtPassSettings::getPasswordCharsselection()); + pwdConfig.Characters[passwordConfiguration::CUSTOM] = + QtPassSettings::getPasswordChars(); if (QtPassSettings::isAlwaysOnTop()) { Qt::WindowFlags flags = windowFlags(); @@ -418,7 +419,7 @@ void MainWindow::config() { d->useSymbols(QtPassSettings::isUseSymbols()); d->setPasswordLength(pwdConfig.length); d->setPwdTemplateSelector(pwdConfig.selected); - if (pwdConfig.selected != 3) + if (pwdConfig.selected != passwordConfiguration::CUSTOM) d->setLineEditEnabled(false); d->setPasswordChars(pwdConfig.Characters[pwdConfig.selected]); d->useTemplate(QtPassSettings::isUseTemplate()); @@ -461,8 +462,10 @@ void MainWindow::config() { QtPassSettings::setLessRandom(d->lessRandom()); QtPassSettings::setUseSymbols(d->useSymbols()); pwdConfig.length = d->getPasswordLength(); - pwdConfig.selected = d->getPwdTemplateSelector(); - pwdConfig.Characters[3] = d->getPasswordChars(); + pwdConfig.selected = static_cast<passwordConfiguration::characterSet>( + d->getPwdTemplateSelector()); + pwdConfig.Characters[passwordConfiguration::CUSTOM] = + d->getPasswordChars(); QtPassSettings::setUseTemplate(d->useTemplate()); QtPassSettings::setPassTemplate(d->getTemplate()); QtPassSettings::setTemplateAllFields(d->templateAllFields()); @@ -473,7 +476,8 @@ void MainWindow::config() { QtPassSettings::setVersion(VERSION); QtPassSettings::setPasswordLength(pwdConfig.length); QtPassSettings::setPasswordCharsselection(pwdConfig.selected); - QtPassSettings::setPasswordChars(pwdConfig.Characters[3]); + QtPassSettings::setPasswordChars( + pwdConfig.Characters[passwordConfiguration::CUSTOM]); if (QtPassSettings::isAlwaysOnTop()) { Qt::WindowFlags flags = windowFlags(); @@ -938,17 +942,16 @@ void MainWindow::setPassword(QString file, bool overwrite, bool isNew = false) { // warn? return; } - PasswordDialog d(this); + PasswordDialog d(pwdConfig, *pass, this); d.setFile(file); d.usePwgen(QtPassSettings::isUsePwgen()); d.setTemplate(QtPassSettings::getPassTemplate()); d.useTemplate(QtPassSettings::isUseTemplate()); d.templateAll(QtPassSettings::isTemplateAllFields()); d.setPassword(lastDecrypt); - d.setLength(pwdConfig.length); - d.setPasswordCharTemplate(pwdConfig.selected); + currentAction = PWGEN; if (!d.exec()) { - d.setPassword(NULL); + d.setPassword(QString()); return; } QString newValue = d.getPassword(); @@ -1429,30 +1432,6 @@ void MainWindow::editPassword() { } /** - * @brief MainWindow::generatePassword use either pwgen or internal password - * generator - * @param length of the desired password - * @param selection character set to use for generation - * @return the password - */ -QString MainWindow::generatePassword(int length, - Enums::characterSet selection) { - currentAction = PWGEN; - // TODO(bezet): move checks inside and catch exceptions - - if (!QtPassSettings::isUsePwgen()) { - int charsetLength = pwdConfig.Characters[selection].length(); - if (charsetLength <= 0) - QMessageBox::critical( - this, tr("No characters chosen"), - tr("Can't generate password, there are no characters to choose from " - "set in the configuration!")); - return QString(); - } - return pass->Generate(length, pwdConfig.Characters[selection]); -} - -/** * @brief MainWindow::clearTemplateWidgets empty the template widget fields in * the UI */ diff --git a/mainwindow.h b/mainwindow.h index afd30c15..64abe785 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -1,6 +1,7 @@ #ifndef MAINWINDOW_H_ #define MAINWINDOW_H_ +#include "datahelpers.h" #include "enums.h" #include "imitatepass.h" #include "pass.h" @@ -24,41 +25,6 @@ namespace Ui { class MainWindow; } -struct UserInfo; - -/*! - \struct passwordConfiguration - \brief holds the Password configuration settings - */ -struct passwordConfiguration { - /** - * @brief passwordConfiguration::selected character set. - */ - int selected; - /** - * @brief passwordConfiguration::length of password. - */ - int length; - /** - * @brief passwordConfiguration::Characters the different character sets. - */ - QString Characters[4]; - passwordConfiguration() { - length = 16; - selected = 0; - enum selected { ALLCHARS, ALPHABETICAL, ALPHANUMERIC, CUSTOM }; - Characters[ALLCHARS] = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890~!@#$%^&" - "*()_-+={}[]|:;<>,.?"; /*AllChars*/ - Characters[ALPHABETICAL] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstu" - "vwxyz"; /*Only Alphabetical*/ - Characters[ALPHANUMERIC] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstu" - "vwxyz1234567890"; /*Alphabetical and Numerical*/ - Characters[CUSTOM] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1" - "234567890~!@#$%^&*()_-+={}[]|:;<>,.?"; /*Custom*/ - } -}; - /*! \class MainWindow \brief The MainWindow class does way too much, not only is it a switchboard, @@ -80,7 +46,6 @@ public: QStringList getSecretKeys(); void generateKeyPair(QString, QDialog *); void userDialog(QString = ""); - QString generatePassword(int length, Enums::characterSet selection); void config(); void executePassGitInit(); @@ -68,6 +68,11 @@ QString Pass::Generate(int length, const QString &charset) { QChar nextChar = charset.at(index); passwd.append(nextChar); } + } else { + emit critical( + tr("No characters chosen"), + tr("Can't generate password, there are no characters to choose from " + "set in the configuration!")); } } return passwd; @@ -1,13 +1,12 @@ #ifndef PASS_H #define PASS_H +#include "datahelpers.h" +#include "enums.h" #include <QList> #include <QProcess> #include <QQueue> #include <QString> -// TODO(bezet): extract UserInfo somewhere -#include "enums.h" -#include "usersdialog.h" /*! \struct execQueueItem diff --git a/passworddialog.cpp b/passworddialog.cpp index efaff428..708d955f 100644 --- a/passworddialog.cpp +++ b/passworddialog.cpp @@ -8,12 +8,14 @@ * @brief PasswordDialog::PasswordDialog basic constructor. * @param parent */ -PasswordDialog::PasswordDialog(MainWindow *parent) - : QDialog(parent), ui(new Ui::PasswordDialog) { - mainWindow = parent; +PasswordDialog::PasswordDialog(const passwordConfiguration &passConfig, + Pass &pass, QWidget *parent) + : QDialog(parent), ui(new Ui::PasswordDialog), m_passConfig(passConfig), m_pass(pass) { templating = false; allFields = false; ui->setupUi(this); + setLength(m_passConfig.length); + setPasswordCharTemplate(m_passConfig.selected); } /** @@ -39,9 +41,12 @@ void PasswordDialog::on_checkBoxShow_stateChanged(int arg1) { */ void PasswordDialog::on_createPasswordButton_clicked() { ui->widget->setEnabled(false); - ui->lineEditPassword->setText(mainWindow->generatePassword( + QString newPass = m_pass.Generate( ui->spinBox_pwdLength->value(), - (Enums::characterSet)ui->passwordTemplateSwitch->currentIndex())); + m_passConfig.Characters[(passwordConfiguration::characterSet) + ui->passwordTemplateSwitch->currentIndex()]); + if (newPass.length() > 0) + ui->lineEditPassword->setText(newPass); ui->widget->setEnabled(true); } diff --git a/passworddialog.h b/passworddialog.h index 7781c9be..1deb1543 100644 --- a/passworddialog.h +++ b/passworddialog.h @@ -1,8 +1,10 @@ #ifndef PASSWORDDIALOG_H_ #define PASSWORDDIALOG_H_ -#include "mainwindow.h" +#include "datahelpers.h" +#include "pass.h" #include <QDialog> +#include <QWidget> namespace Ui { class PasswordDialog; @@ -18,7 +20,8 @@ class PasswordDialog : public QDialog { Q_OBJECT public: - explicit PasswordDialog(MainWindow *parent = 0); + explicit PasswordDialog(const passwordConfiguration &passConfig, Pass &pass, + QWidget *parent = 0); ~PasswordDialog(); /*! Sets content in the password field in the interface. @@ -55,7 +58,8 @@ private slots: private: Ui::PasswordDialog *ui; - MainWindow *mainWindow; + const passwordConfiguration &m_passConfig; + Pass &m_pass; QString passTemplate; QStringList fields; bool templating; @@ -53,9 +53,10 @@ HEADERS += mainwindow.h \ qtpasssettings.h \ enums.h \ settingsconstants.h \ - pass.h \ - realpass.h \ - imitatepass.h + pass.h \ + realpass.h \ + imitatepass.h \ + datahelpers.h FORMS += mainwindow.ui \ configdialog.ui \ diff --git a/qtpasssettings.cpp b/qtpasssettings.cpp index 529de11d..8ef85547 100644 --- a/qtpasssettings.cpp +++ b/qtpasssettings.cpp @@ -332,15 +332,6 @@ void QtPassSettings::setUseSymbols(const bool &useSymbols) { setBoolValue(SettingsConstants::useSymbols, useSymbols); } -int QtPassSettings::getPasswordCharsSelected(const int &defaultValue) { - return getIntValue(SettingsConstants::passwordCharsSelected, defaultValue); -} - -void QtPassSettings::setPasswordCharsSelected( - const int &passwordCharsSelected) { - setIntValue(SettingsConstants::passwordCharsSelected, passwordCharsSelected); -} - int QtPassSettings::getPasswordLength(const int &defaultValue) { return getIntValue(SettingsConstants::passwordLength, defaultValue); } diff --git a/qtpasssettings.h b/qtpasssettings.h index 399b57a4..0d15030c 100644 --- a/qtpasssettings.h +++ b/qtpasssettings.h @@ -136,10 +136,6 @@ public: static bool isUseSymbols(const bool &defaultValue = QVariant().toBool()); static void setUseSymbols(const bool &useSymbols); - static int - getPasswordCharsSelected(const int &defaultValue = QVariant().toInt()); - static void setPasswordCharsSelected(const int &passwordCharsSelected); - static int getPasswordLength(const int &defaultValue = QVariant().toInt()); static void setPasswordLength(const int &passwordLength); diff --git a/settingsconstants.cpp b/settingsconstants.cpp index e284412e..6a870f2e 100644 --- a/settingsconstants.cpp +++ b/settingsconstants.cpp @@ -39,8 +39,6 @@ const QString SettingsConstants::avoidCapitals = "avoidCapitals"; const QString SettingsConstants::avoidNumbers = "avoidNumbers"; const QString SettingsConstants::lessRandom = "lessRandom"; const QString SettingsConstants::useSymbols = "useSymbols"; -const QString SettingsConstants::passwordCharsSelected = - "passwordCharsSelected"; const QString SettingsConstants::passwordLength = "passwordLength"; const QString SettingsConstants::passwordCharsselection = "passwordCharsselection"; diff --git a/settingsconstants.h b/settingsconstants.h index fc178d87..a67d66b1 100644 --- a/settingsconstants.h +++ b/settingsconstants.h @@ -42,7 +42,6 @@ public: const static QString avoidNumbers; const static QString lessRandom; const static QString useSymbols; - const static QString passwordCharsSelected; const static QString passwordLength; const static QString passwordCharsselection; const static QString passwordChars; diff --git a/usersdialog.h b/usersdialog.h index 521bc935..de0c0b1b 100644 --- a/usersdialog.h +++ b/usersdialog.h @@ -1,6 +1,7 @@ #ifndef USERSDIALOG_H_ #define USERSDIALOG_H_ +#include "datahelpers.h" #include <QCloseEvent> #include <QDateTime> #include <QDialog> @@ -14,60 +15,6 @@ class UsersDialog; class QListWidgetItem; /*! - \struct UserInfo - \brief Stores key info lines including validity, creation date and more. - */ -struct UserInfo { - UserInfo() : validity('-'), have_secret(false), enabled(false) {} - - /** - * @brief UserInfo::fullyValid when validity is f or u. - * http://git.gnupg.org/cgi-bin/gitweb.cgi?p=gnupg.git;a=blob_plain;f=doc/DETAILS - */ - bool fullyValid() { return validity == 'f' || validity == 'u'; } - /** - * @brief UserInfo::marginallyValid when validity is m. - * http://git.gnupg.org/cgi-bin/gitweb.cgi?p=gnupg.git;a=blob_plain;f=doc/DETAILS - */ - bool marginallyValid() { return validity == 'm'; } - /** - * @brief UserInfo::isValid when fullyValid or marginallyValid. - */ - bool isValid() { return fullyValid() || marginallyValid(); } - - /** - * @brief UserInfo::name full name - */ - QString name; - /** - * @brief UserInfo::key_id hexadecimal representation - */ - QString key_id; - /** - * @brief UserInfo::validity GnuPG representation of validity - * http://git.gnupg.org/cgi-bin/gitweb.cgi?p=gnupg.git;a=blob_plain;f=doc/DETAILS - */ - char validity; - /** - * @brief UserInfo::have_secret secret key is available - * (can decrypt with this key) - */ - bool have_secret; - /** - * @brief UserInfo::enabled - */ - bool enabled; - /** - * @brief UserInfo::expiry date/time key expires - */ - QDateTime expiry; - /** - * @brief UserInfo::created date/time key was created - */ - QDateTime created; -}; - -/*! \class UsersDialog \brief Handles listing and editing of GPG users. |