From f12c3ca8aae5eae6b7a7ada4b923441b576d4ef7 Mon Sep 17 00:00:00 2001 From: Christian Hohnstaedt Date: Fri, 14 Jul 2017 17:22:21 +0200 Subject: [PATCH] Improve Copy&Paste behavior: Accept Ctrl-V and MousePaste on MainWindow --- lib/main.cpp | 16 +++++++++++++++- lib/pki_multi.cpp | 19 +++++++------------ widgets/MainWindow.cpp | 12 +++++++++--- 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/lib/main.cpp b/lib/main.cpp index 58f7344f..01424e42 100644 --- a/lib/main.cpp +++ b/lib/main.cpp @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -146,9 +147,9 @@ bool XCA_application::eventFilter(QObject *watched, QEvent *ev) static int mctr; QMouseEvent *me; QStringList l; + XcaTreeView *treeview; int key; - (void)watched; switch (ev->type()) { case QEvent::FileOpen: l << static_cast(ev)->file(); @@ -169,6 +170,19 @@ bool XCA_application::eventFilter(QObject *watched, QEvent *ev) entropy.add(key); } break; + case QEvent::MouseButtonPress: + me = static_cast(ev); + treeview = watched ? + dynamic_cast(watched->parent()) : NULL; + + if ((watched == mainw || treeview) && + me->button() == Qt::MidButton && + QApplication::clipboard()->supportsSelection()) + { + mainw->pastePem(); + return true; + } + break; default: break; } diff --git a/lib/pki_multi.cpp b/lib/pki_multi.cpp index d253761d..b92a9420 100644 --- a/lib/pki_multi.cpp +++ b/lib/pki_multi.cpp @@ -46,18 +46,13 @@ pki_base *pki_multi::pull() /* General PEM loader */ static pki_base *pkiByPEM(QString text, int *skip) { - int pos; - pos = text.indexOf(BEGIN); - if (pos <0) { - if (skip) - *skip = text.length() - (sizeof(BEGIN)-1); - return NULL; - } - if (skip) { + int pos = text.indexOf(BEGIN); + + if (skip) *skip = pos; - if (pos) /* if we are not at the beginning, retry */ - return NULL; - } + + if (pos < 0) + return NULL; text = text.remove(0, pos + sizeof(BEGIN)-1); if (text.startsWith(PEM_STRING_X509_OLD D5) || @@ -113,7 +108,7 @@ void pki_multi::fromPEMbyteArray(QByteArray &ba, QString name) for (;;) { try { item = pkiByPEM(QString::fromLatin1(ba), &startpos); - if (!item || startpos < 0) + if (!item) break; ba.remove(0, startpos); item->fromPEMbyteArray(ba, name); diff --git a/widgets/MainWindow.cpp b/widgets/MainWindow.cpp index f3111899..1dfe92a1 100644 --- a/widgets/MainWindow.cpp +++ b/widgets/MainWindow.cpp @@ -390,12 +390,13 @@ void MainWindow::pastePem() ui.button->setText(tr("Import PEM data")); input->setWindowTitle(XCA_TITLE); textbox->setPlainText(text); - if (input->exec()) + if (input->exec()) { text = textbox->toPlainText(); + if (!text.isEmpty()) + pastePem(text); + } delete input; - if (!text.isEmpty()) - pastePem(text); } void MainWindow::initToken() @@ -1018,6 +1019,11 @@ void MainWindow::keyPressEvent(QKeyEvent *e) XCA_application::tableFont.setPointSize(siz -1); } break; + case Qt::Key_V: + if (e->modifiers() == Qt::ControlModifier) { + pastePem(); + break; + } default: QMainWindow::keyPressEvent(e); return;