Initial commit
[keepassx] / src / dialogs / AutoTypeDlg.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2008 by Felix Geyer                                *
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; version 2 of the License.               *
7  *                                                                         *
8  *   This program is distributed in the hope that it will be useful,       *
9  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
10  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
11  *   GNU General Public License for more details.                          *
12  *                                                                         *
13  *   You should have received a copy of the GNU General Public License     *
14  *   along with this program; if not, write to the                         *
15  *   Free Software Foundation, Inc.,                                       *
16  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
17  ***************************************************************************/
18
19 #include <QDesktopWidget>
20 #include "AutoTypeDlg.h"
21
22 bool AutoTypeDlg::dialogVisible = false;
23
24 AutoTypeDlg::AutoTypeDlg(QList<IEntryHandle*> entries, QList<int> numbers, bool wasLocked) : pWasLocked(wasLocked){
25         Q_ASSERT(!dialogVisible);
26         dialogVisible = true;
27         setupUi(this);
28         
29         setAttribute(Qt::WA_DeleteOnClose);
30         setWindowFlags(windowFlags()|Qt::WindowStaysOnTopHint);
31         setGeometry( QRect(QApplication::desktop()->screenGeometry(QCursor::pos()).center() - rect().center(), size()) );
32         setWindowIcon(getIcon("keepassx_small"));
33         entryList->setAlternatingRowColors(config->alternatingRowColors());
34         
35         bool hideUsernames = config->hideUsernames();
36         if (hideUsernames)
37                 entryList->setHeaderLabels(QStringList() << tr("Group") << tr("Title"));
38         else
39                 entryList->setHeaderLabels(QStringList() << tr("Group") << tr("Title") << tr("Username"));
40         
41         QList<QTreeWidgetItem*> itemList;
42         AutoTypeEntry autoTypeEntry;
43         for (int i=0; i<entries.size(); i++){
44                 QStringList cols;
45                 cols << entries[i]->group()->title() << entries[i]->title();
46                 if (!hideUsernames)
47                         cols << entries[i]->username();
48                 
49                 QTreeWidgetItem* widgetItem = new QTreeWidgetItem(cols);
50                 itemList.append(widgetItem);
51                 autoTypeEntry.dbHandle = entries[i];
52                 autoTypeEntry.nr = numbers[i];
53                 itemToEntry.insert(widgetItem, autoTypeEntry);
54         }
55         
56         qSort(itemList.begin(), itemList.end(), itemLessThan);
57         for (int i=0; i<itemList.size(); i++)
58                 entryList->addTopLevelItem(itemList[i]);
59         entryList->setCurrentItem(itemList[0]);
60         
61         entryList->resizeColumnToContents(0);
62         entryList->resizeColumnToContents(1);
63         if (!hideUsernames)
64                 entryList->resizeColumnToContents(2);
65         
66         entryList->setColumnWidth(0, entryList->columnWidth(0)+10);
67         if (!hideUsernames)
68                 entryList->setColumnWidth(1, entryList->columnWidth(1)+10);
69         
70         connect(ButtonBox, SIGNAL(rejected()), SLOT(close()));
71         connect(entryList, SIGNAL(itemClicked(QTreeWidgetItem*,int)), SLOT(itemSelected(QTreeWidgetItem*)));
72         connect(entryList, SIGNAL(returnPressed(QTreeWidgetItem*)), SLOT(itemSelected(QTreeWidgetItem*)));
73 }
74
75 bool AutoTypeDlg::itemLessThan(QTreeWidgetItem* i1, QTreeWidgetItem* i2){
76         if (i1->text(0) != i2->text(0))
77                 return i1->text(0) < i2->text(0);
78         else if (i1->text(1) != i2->text(1))
79                 return i1->text(1) < i2->text(1);
80         else
81                 return i1->text(2) < i2->text(2);
82 }
83
84
85 void AutoTypeDlg::paintEvent(QPaintEvent* event){
86         QWidget::paintEvent(event);
87         QPainter painter(this);
88         painter.setClipRegion(event->region());
89         painter.drawPixmap(QPoint(0,0),BannerPixmap);
90 }
91
92 void AutoTypeDlg::resizeEvent(QResizeEvent* event){
93         createBanner(&BannerPixmap,getPixmap("keepassx"),tr("Auto-Type"),width());
94         QWidget::resizeEvent(event);
95 }
96
97 void AutoTypeDlg::closeEvent(QCloseEvent* event) {
98         Q_ASSERT(dialogVisible);
99         dialogVisible = false;
100         QWidget::closeEvent(event);
101 }
102
103 bool AutoTypeDlg::event(QEvent* event){
104         if (!EventOccurred){
105                 int t = event->type();
106                 if ( (t>=QEvent::MouseButtonPress && t<=QEvent::KeyRelease) || (t>=QEvent::HoverEnter && t<=QEvent::HoverMove) )
107                         EventOccurred = true;
108         }
109         return QWidget::event(event);
110 }
111
112 void AutoTypeDlg::itemSelected(QTreeWidgetItem* item){
113         close();
114         autoType->perform(itemToEntry[item].dbHandle, pWasLocked, itemToEntry[item].nr, pWasLocked);
115 }