diff --git a/configdialog.cpp b/configdialog.cpp index f00b0be17..690449088 100644 --- a/configdialog.cpp +++ b/configdialog.cpp @@ -22,6 +22,12 @@ ConfigDialog::ConfigDialog(MainWindow *parent) : ui->profileTable->verticalHeader()->hide(); ui->profileTable->horizontalHeader()->setStretchLastSection(true); ui->label->setText(ui->label->text() + VERSION); + ui->comboBoxClipboard->clear(); + + ui->comboBoxClipboard->addItem(tr("No Clipboard")); + ui->comboBoxClipboard->addItem(tr("Always copy to clipboard")); + ui->comboBoxClipboard->addItem(tr("On-demand copy to clipboard")); + ui->comboBoxClipboard->setCurrentIndex(0); } /** @@ -234,11 +240,11 @@ void ConfigDialog::on_toolButtonStore_clicked() } /** - * @brief ConfigDialog::on_checkBoxClipboard_clicked + * @brief ConfigDialog::on_comboBoxClipboard_activated */ -void ConfigDialog::on_checkBoxClipboard_clicked() +void ConfigDialog::on_comboBoxClipboard_activated() { - if (ui->checkBoxClipboard->isChecked()) { + if (ui->comboBoxClipboard->currentIndex() > 0) { ui->checkBoxAutoclear->setEnabled(true); ui->checkBoxHidePassword->setEnabled(true); ui->checkBoxHideContent->setEnabled(true); @@ -275,10 +281,10 @@ void ConfigDialog::on_checkBoxAutoclearPanel_clicked() /** * @brief ConfigDialog::useClipboard */ -void ConfigDialog::useClipboard(bool useClipboard) +void ConfigDialog::useClipboard(MainWindow::clipBoardType useClipboard) { - ui->checkBoxClipboard->setChecked(useClipboard); - on_checkBoxClipboard_clicked(); + ui->comboBoxClipboard->setCurrentIndex(static_cast(useClipboard)); + on_comboBoxClipboard_activated(); } /** @@ -323,9 +329,9 @@ void ConfigDialog::setAutoclearPanel(int seconds) * @brief ConfigDialog::useClipboard * @return */ -bool ConfigDialog::useClipboard() +MainWindow::clipBoardType ConfigDialog::useClipboard() { - return ui->checkBoxClipboard->isChecked(); + return static_cast(ui->comboBoxClipboard->currentIndex()); } /** @@ -351,7 +357,7 @@ int ConfigDialog::getAutoclear() */ void ConfigDialog::on_checkBoxAutoclear_clicked() { - on_checkBoxClipboard_clicked(); + on_comboBoxClipboard_activated(); } /** diff --git a/configdialog.h b/configdialog.h index b9a1938b6..d18cd0ab5 100644 --- a/configdialog.h +++ b/configdialog.h @@ -27,7 +27,7 @@ class ConfigDialog : public QDialog void setStorePath(QString); void setProfiles(QHash, QString); void usePass(bool); - void useClipboard(bool); + void useClipboard(MainWindow::clipBoardType); void useAutoclear(bool); void setAutoclear(int); void useAutoclearPanel(bool); @@ -41,7 +41,7 @@ class ConfigDialog : public QDialog QString getStorePath(); QHash getProfiles(); bool usePass(); - bool useClipboard(); + MainWindow::clipBoardType useClipboard(); bool useAutoclear(); int getAutoclear(); bool useAutoclearPanel(); @@ -91,7 +91,7 @@ private slots: void on_toolButtonPwgen_clicked(); void on_toolButtonPass_clicked(); void on_toolButtonStore_clicked(); - void on_checkBoxClipboard_clicked(); + void on_comboBoxClipboard_activated(); void on_checkBoxAutoclear_clicked(); void on_checkBoxAutoclearPanel_clicked(); void on_addButton_clicked(); diff --git a/configdialog.ui b/configdialog.ui index c4eec6e34..8ed498432 100644 --- a/configdialog.ui +++ b/configdialog.ui @@ -245,13 +245,6 @@ - - - - Use clipboard - - - @@ -404,6 +397,9 @@ + + + @@ -589,7 +585,6 @@ url toolButtonPwgen passPath toolButtonPass - checkBoxClipboard checkBoxAutoclear spinBoxAutoclearSeconds checkBoxAutoclearPanel diff --git a/mainwindow.cpp b/mainwindow.cpp index bade6c94d..aac760416 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -48,10 +48,11 @@ MainWindow::MainWindow(QWidget *parent) : // no working config QApplication::quit(); } + ui->copyPasswordButton->setEnabled(false); + setClippedPassword(""); QtPass = NULL; autoclearTimer = NULL; QTimer::singleShot(10, this, SLOT(focusInput())); - } void MainWindow::focusInput() { @@ -172,7 +173,13 @@ bool MainWindow::checkConfig() { usePass = (settings.value("usePass") == "true"); - useClipboard = (settings.value("useClipboard") == "true"); + useClipboard = CLIPBOARD_NEVER; + if (settings.value("useClipboard") == "true" || + settings.value("useClipboard") == "1") { + useClipboard = CLIPBOARD_ALWAYS; + } else if (settings.value("useClipboard") == "2") { + useClipboard = CLIPBOARD_ON_DEMAND; + } useAutoclear = (settings.value("useAutoclear") == "true"); autoclearSeconds = settings.value("autoclearSeconds").toInt(); useAutoclearPanel = (settings.value("useAutoclearPanel") == "true"); @@ -323,6 +330,9 @@ bool MainWindow::checkConfig() { this, SLOT(showContextMenu(const QPoint&))); ui->textBrowser->setOpenExternalLinks(true); + ui->textBrowser->setContextMenuPolicy(Qt::CustomContextMenu); + connect(ui->textBrowser, SIGNAL(customContextMenuRequested(const QPoint&)), + this, SLOT(showBrowserContextMenu(const QPoint&))); updateProfileBox(); @@ -439,7 +449,17 @@ void MainWindow::config() { settings.setValue("gpgExecutable", gpgExecutable); settings.setValue("passStore", passStore); settings.setValue("usePass", usePass ? "true" : "false"); - settings.setValue("useClipboard", useClipboard ? "true" : "false"); + switch (useClipboard) { + case CLIPBOARD_ALWAYS: + settings.setValue("useClipboard", "true"); + break; + case CLIPBOARD_ON_DEMAND: + settings.setValue("useClipboard", "2"); + break; + default: + settings.setValue("useClipboard", "false"); + break; + } settings.setValue("useAutoclear", useAutoclear ? "true" : "false"); settings.setValue("autoclearSeconds", autoclearSeconds); settings.setValue("useAutoclearPanel", useAutoclearPanel ? "true" : "false"); @@ -572,6 +592,7 @@ void MainWindow::on_treeView_clicked(const QModelIndex &index) { currentDir = getDir(ui->treeView->currentIndex(), false); lastDecrypt = "Could not decrypt"; + setClippedPassword(""); QString file = getFile(index, usePass); if (!file.isEmpty()){ currentAction = GPG; @@ -665,13 +686,14 @@ void MainWindow::readyRead(bool finished = false) { if (finished && currentAction == GPG) { lastDecrypt = output; QStringList tokens = output.split("\n"); - if (useClipboard && !output.isEmpty()) { - QClipboard *clip = QApplication::clipboard(); - clip->setText(tokens[0]); - ui->statusBar->showMessage(tr("Password copied to clipboard"), 3000); - if (useAutoclear) { - clippedPass = tokens[0]; - QTimer::singleShot(1000*autoclearSeconds, this, SLOT(clearClipboard())); + + if (useClipboard != CLIPBOARD_NEVER && !output.isEmpty()) { + setClippedPassword(tokens[0]); + if (useClipboard == CLIPBOARD_ALWAYS) copyPasswordToClipboard(); + if (useAutoclearPanel) { + QTimer::singleShot(1000*autoclearPanelSeconds, \ + this, \ + SLOT(clearPanel())); } if (hidePassword && !useTemplate) { tokens[0] = "***" + tr("Password hidden") + "***"; @@ -771,9 +793,8 @@ void MainWindow::readyRead(bool finished = false) { void MainWindow::clearClipboard() { QClipboard *clipboard = QApplication::clipboard(); - if (clipboard->text() == clippedPass) { + if (clipboard->text() == getClippedPassword()) { clipboard->clear(); - clippedPass = ""; ui->statusBar->showMessage(tr("Clipboard cleared"), 3000); } else { ui->statusBar->showMessage(tr("Clipboard not cleared"), 3000); @@ -1560,6 +1581,11 @@ void MainWindow::closeEvent(QCloseEvent *event) } } +void MainWindow::on_copyPasswordButton_clicked() +{ + copyPasswordToClipboard(); +} + /** * @brief MainWindow::showContextMenu * @param pos @@ -1595,12 +1621,41 @@ void MainWindow::showContextMenu(const QPoint& pos) connect(edit, SIGNAL(triggered()), this, SLOT(editPassword())); } if (selected) { + //if (useClipboard != CLIPBOARD_NEVER) { + // contextMenu.addSeparator(); + // QAction* copyItem = contextMenu.addAction(tr("Copy Password")); + // if (getClippedPassword().length() == 0) copyItem->setEnabled(false); + // connect(copyItem, SIGNAL(triggered()), this, SLOT(copyPasswordToClipboard())); + //} + contextMenu.addSeparator(); QAction* deleteItem = contextMenu.addAction(tr("Delete")); connect(deleteItem, SIGNAL(triggered()), this, SLOT(on_deleteButton_clicked())); } contextMenu.exec(globalPos); - } +} + +/** + * @brief MainWindow::showContextMenu + * @param pos + */ +void MainWindow::showBrowserContextMenu(const QPoint& pos) +{ + QMenu *contextMenu = ui->textBrowser->createStandardContextMenu(pos); + + if (useClipboard != CLIPBOARD_NEVER) { + contextMenu->addSeparator(); + QAction* copyItem = contextMenu->addAction(tr("Copy Password")); + if (getClippedPassword().length() == 0) copyItem->setEnabled(false); + connect(copyItem, \ + SIGNAL(triggered()), \ + this, \ + SLOT(copyPasswordToClipboard())); + } + QPoint globalPos = ui->textBrowser->viewport()->mapToGlobal(pos); + + contextMenu->exec(globalPos); +} /** * @brief MainWindow::addFolder @@ -1713,3 +1768,39 @@ void MainWindow::clearTemplateWidgets() } ui->verticalLayoutPassword->setSpacing(0); } + +/* + * @brief Mainwindow::copyPasswordToClipboard - copy the clipped password (if not "") to the clipboard + * @return + */ +void MainWindow::copyPasswordToClipboard() +{ + if (clippedPass.length() > 0) { + QClipboard *clip = QApplication::clipboard(); + clip->setText(clippedPass); + ui->statusBar->showMessage(tr("Password copied to clipboard"), 3000); + if (useAutoclear) { + QTimer::singleShot(1000*autoclearSeconds, \ + this, \ + SLOT(clearClipboard())); + } + } +} + +/** + * @brief Mainwindow::setClippedPassword - set the stored clipped password + * @return none + */ +void MainWindow::setClippedPassword(const QString & pass) +{ + clippedPass = pass; + if (clippedPass.length() == 0) { + ui->copyPasswordButton->setEnabled(false); + } else { + ui->copyPasswordButton->setEnabled(true); + } +} + +const QString &MainWindow::getClippedPassword() { + return clippedPass; +} diff --git a/mainwindow.h b/mainwindow.h index e63d9a412..587d2a749 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -35,6 +35,10 @@ class MainWindow : public QMainWindow enum actionType { GPG, GIT, EDIT, DELETE, GPG_INTERNAL, PWGEN }; public: + enum clipBoardType { CLIPBOARD_NEVER = 0, + CLIPBOARD_ALWAYS = 1, + CLIPBOARD_ON_DEMAND = 2 }; + explicit MainWindow(QWidget *parent = 0); ~MainWindow(); void setPassExecutable(QString); @@ -73,10 +77,13 @@ private slots: void on_usersButton_clicked(); void messageAvailable(QString message); void on_profileBox_currentIndexChanged(QString); + void on_copyPasswordButton_clicked(); void showContextMenu(const QPoint& pos); + void showBrowserContextMenu(const QPoint& pos); void addFolder(); void editPassword(); void focusInput(); + void copyPasswordToClipboard(); private: QApplication *QtPass; @@ -87,7 +94,7 @@ private slots: QScopedPointer selectionModel; QScopedPointer process; bool usePass; - bool useClipboard; + clipBoardType useClipboard; bool useAutoclear; bool useAutoclearPanel; bool hidePassword; @@ -155,6 +162,8 @@ private slots: bool removeDir(const QString & dirName); void waitFor(int); void clearTemplateWidgets(); + void setClippedPassword(const QString & pass); + const QString &getClippedPassword(); }; #endif // MAINWINDOW_H diff --git a/mainwindow.ui b/mainwindow.ui index 87c931026..f36363d47 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -85,6 +85,16 @@ + + + + Copy Password + + + Password + + +