1 /*
2 * Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 or (at your option)
7 * version 3 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "AutoTypeSelectDialog.h"
19
20 #include <QApplication>
21 #include <QDesktopWidget>
22 #include <QDialogButtonBox>
23 #include <QHeaderView>
24 #include <QLabel>
25 #include <QVBoxLayout>
26
27 #include "autotype/AutoTypeSelectView.h"
28 #include "core/Config.h"
29 #include "core/FilePath.h"
30 #include "gui/entry/EntryModel.h"
31
AutoTypeSelectDialog(QWidget * parent)32 AutoTypeSelectDialog::AutoTypeSelectDialog(QWidget* parent)
33 : QDialog(parent)
34 , m_view(new AutoTypeSelectView(this))
35 , m_entryActivatedEmitted(false)
36 {
37 setAttribute(Qt::WA_DeleteOnClose);
38 // Places the window on the active (virtual) desktop instead of where the main window is.
39 setAttribute(Qt::WA_X11BypassTransientForHint);
40 setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
41 setWindowTitle(tr("Auto-Type - KeePassX"));
42 setWindowIcon(filePath()->applicationIcon());
43
44 QRect screenGeometry = QApplication::desktop()->availableGeometry(QCursor::pos());
45 QSize size = config()->get("GUI/AutoTypeSelectDialogSize", QSize(400, 250)).toSize();
46 size.setWidth(qMin(size.width(), screenGeometry.width()));
47 size.setHeight(qMin(size.height(), screenGeometry.height()));
48 resize(size);
49
50 // move dialog to the center of the screen
51 QPoint screenCenter = screenGeometry.center();
52 move(screenCenter.x() - (size.width() / 2), screenCenter.y() - (size.height() / 2));
53
54 QVBoxLayout* layout = new QVBoxLayout(this);
55
56 QLabel* descriptionLabel = new QLabel(tr("Select entry to Auto-Type:"), this);
57 layout->addWidget(descriptionLabel);
58
59 connect(m_view, SIGNAL(activated(QModelIndex)), SLOT(emitEntryActivated(QModelIndex)));
60 connect(m_view, SIGNAL(clicked(QModelIndex)), SLOT(emitEntryActivated(QModelIndex)));
61 connect(m_view->model(), SIGNAL(rowsRemoved(QModelIndex,int,int)), SLOT(entryRemoved()));
62 layout->addWidget(m_view);
63
64 QDialogButtonBox* buttonBox = new QDialogButtonBox(QDialogButtonBox::Cancel, Qt::Horizontal, this);
65 connect(buttonBox, SIGNAL(rejected()), SLOT(reject()));
66 layout->addWidget(buttonBox);
67 }
68
setEntries(const QList<Entry * > & entries,const QHash<Entry *,QString> & sequences)69 void AutoTypeSelectDialog::setEntries(const QList<Entry*>& entries, const QHash<Entry*, QString>& sequences)
70 {
71 m_sequences = sequences;
72 m_view->setEntryList(entries);
73
74 m_view->header()->resizeSections(QHeaderView::ResizeToContents);
75 }
76
done(int r)77 void AutoTypeSelectDialog::done(int r)
78 {
79 config()->set("GUI/AutoTypeSelectDialogSize", size());
80
81 QDialog::done(r);
82 }
83
emitEntryActivated(const QModelIndex & index)84 void AutoTypeSelectDialog::emitEntryActivated(const QModelIndex& index)
85 {
86 // make sure we don't emit the signal twice when both activated() and clicked() are triggered
87 if (m_entryActivatedEmitted) {
88 return;
89 }
90 m_entryActivatedEmitted = true;
91
92 Entry* entry = m_view->entryFromIndex(index);
93 accept();
94 Q_EMIT entryActivated(entry, m_sequences[entry]);
95 }
96
entryRemoved()97 void AutoTypeSelectDialog::entryRemoved()
98 {
99 if (m_view->model()->rowCount() == 0) {
100 reject();
101 }
102 }
103