Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified the clipboard logic to allow for on-demand copy to clipboard. #119

Merged
merged 2 commits into from
Nov 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions configdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<int>(useClipboard));
on_comboBoxClipboard_activated();
}

/**
Expand Down Expand Up @@ -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<MainWindow::clipBoardType>(ui->comboBoxClipboard->currentIndex());
}

/**
Expand All @@ -351,7 +357,7 @@ int ConfigDialog::getAutoclear()
*/
void ConfigDialog::on_checkBoxAutoclear_clicked()
{
on_checkBoxClipboard_clicked();
on_comboBoxClipboard_activated();
}

/**
Expand Down
6 changes: 3 additions & 3 deletions configdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ConfigDialog : public QDialog
void setStorePath(QString);
void setProfiles(QHash<QString, QString>, QString);
void usePass(bool);
void useClipboard(bool);
void useClipboard(MainWindow::clipBoardType);
void useAutoclear(bool);
void setAutoclear(int);
void useAutoclearPanel(bool);
Expand All @@ -41,7 +41,7 @@ class ConfigDialog : public QDialog
QString getStorePath();
QHash<QString,QString> getProfiles();
bool usePass();
bool useClipboard();
MainWindow::clipBoardType useClipboard();
bool useAutoclear();
int getAutoclear();
bool useAutoclearPanel();
Expand Down Expand Up @@ -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();
Expand Down
11 changes: 3 additions & 8 deletions configdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,6 @@
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QCheckBox" name="checkBoxClipboard">
<property name="text">
<string>Use clipboard</string>
</property>
</widget>
</item>
<item row="0" column="3">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
Expand Down Expand Up @@ -404,6 +397,9 @@
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QComboBox" name="comboBoxClipboard"/>
</item>
</layout>
</item>
</layout>
Expand Down Expand Up @@ -589,7 +585,6 @@ url</string>
<tabstop>toolButtonPwgen</tabstop>
<tabstop>passPath</tabstop>
<tabstop>toolButtonPass</tabstop>
<tabstop>checkBoxClipboard</tabstop>
<tabstop>checkBoxAutoclear</tabstop>
<tabstop>spinBoxAutoclearSeconds</tabstop>
<tabstop>checkBoxAutoclearPanel</tabstop>
Expand Down
117 changes: 104 additions & 13 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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") + "***";
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1560,6 +1581,11 @@ void MainWindow::closeEvent(QCloseEvent *event)
}
}

void MainWindow::on_copyPasswordButton_clicked()
{
copyPasswordToClipboard();
}

/**
* @brief MainWindow::showContextMenu
* @param pos
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
11 changes: 10 additions & 1 deletion mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -87,7 +94,7 @@ private slots:
QScopedPointer<QItemSelectionModel> selectionModel;
QScopedPointer<QProcess> process;
bool usePass;
bool useClipboard;
clipBoardType useClipboard;
bool useAutoclear;
bool useAutoclearPanel;
bool hidePassword;
Expand Down Expand Up @@ -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
10 changes: 10 additions & 0 deletions mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="copyPasswordButton">
<property name="toolTip">
<string>Copy Password</string>
</property>
<property name="text">
<string>Password</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
Expand Down