Initial commit
[keepassx] / src / dialogs / CollectEntropyDlg.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2006 by Tarek Saidi                                *
3  *   tarek.saidi@arcor.de                                                  *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; version 2 of the License.               *
8
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21
22 #include "CollectEntropyDlg.h"
23
24
25 CollectEntropyDlg::CollectEntropyDlg(QWidget* parent):QDialog(parent){
26         setupUi(this);
27         resize(layout()->closestAcceptableSize(this,QSize(0,0)));
28         setMinimumSize(size());
29         setMaximumSize(size());
30         createBanner(&BannerPixmap,getPixmap("dice"),tr("Entropy Collection"),width());
31         KeyEntropyBuffer=new unsigned char[105];
32         MouseEntropyBuffer=new quint16[210];
33         KeyCounter=0;
34         MouseCounter=0;
35         QTimer* timer=new QTimer(this);
36         connect(timer,SIGNAL(timeout()),this,SLOT(trackMousePos()));
37         timer->setInterval(50);
38         timer->start();
39         ReseedDone=false;
40 }
41
42 CollectEntropyDlg::~CollectEntropyDlg(){
43         delete [] KeyEntropyBuffer;
44         delete [] MouseEntropyBuffer;
45 }
46
47 void CollectEntropyDlg::paintEvent(QPaintEvent *event){
48         QDialog::paintEvent(event);
49         QPainter painter(this);
50         painter.setClipRegion(event->region());
51         painter.drawPixmap(QPoint(0,0),BannerPixmap);
52 }
53
54 void CollectEntropyDlg::trackMousePos(){
55         QPoint p=QCursor::pos();
56         if(LastPos-p==QPoint(0,0))return;
57         LastPos=p;
58         if(MouseCounter==105 || ReseedDone)return;
59         MouseEntropyBuffer[2*MouseCounter]=p.x();
60         MouseEntropyBuffer[2*MouseCounter+1]=p.y();
61         MouseCounter++;
62         updateProgress();
63 }
64
65 void CollectEntropyDlg::keyReleaseEvent(QKeyEvent* event ){
66         QDialog::keyReleaseEvent(event);
67         if(KeyCounter==105 || ReseedDone)return;
68         KeyEntropyBuffer[KeyCounter]=event->key();
69         KeyCounter++;
70         updateProgress();
71 }
72
73 void CollectEntropyDlg::updateProgress(){
74         if(4*KeyCounter+4*MouseCounter>=420){
75                 progressBar->setValue(420);
76                 ReseedDone=true;
77                 reseedStrongPool((quint8*)MouseEntropyBuffer,4*MouseCounter,KeyEntropyBuffer,KeyCounter);
78                 Animation->stop();
79                 stackedWidget->setCurrentIndex(1);
80         }
81         else
82                 progressBar->setValue(4*KeyCounter+4*MouseCounter);
83
84 }
85
86 void CollectEntropyDlg::showEvent(QShowEvent* event){
87         if(!event->spontaneous()){
88                 Animation->start();
89         }
90 }