f63284fa7a4b0bf8b1b087a90f6d6d95e4946e1b
[keepassx] / src / dialogs / SettingsDlg.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005 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  *   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, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
18  ***************************************************************************/
19
20 #include <QFileDialog>
21 #include <QColorDialog>
22 #include "dialogs/SettingsDlg.h"
23 #include "dialogs/CustomizeDetailViewDlg.h"
24
25 //bool CSettingsDlg::PluginsModified=false;
26
27
28 CSettingsDlg::CSettingsDlg(QWidget* parent):QDialog(parent,Qt::Dialog)
29 {
30         setupUi(this);
31         connect(listWidget, SIGNAL( currentRowChanged(int) ), stackedWidget, SLOT( setCurrentIndex(int) ) );
32         
33         connect(DialogButtons, SIGNAL( accepted() ), this, SLOT( OnOK() ) );
34         connect(DialogButtons, SIGNAL( rejected() ), this, SLOT( OnCancel() ) );
35         connect(DialogButtons, SIGNAL( clicked(QAbstractButton*)), this, SLOT(OnOtherButton(QAbstractButton*)));
36         
37         connect(CheckBox_ShowSysTrayIcon, SIGNAL( toggled(bool) ), CheckBox_MinimizeTray, SLOT( setEnabled(bool) ) );
38         connect(CheckBox_OpenLast, SIGNAL( toggled(bool) ), CheckBox_RememberLastKey, SLOT( setEnabled(bool) ) );
39         connect(CheckBox_OpenLast, SIGNAL( toggled(bool) ), CheckBox_StartMinimized, SLOT( setEnabled(bool) ) );
40         connect(CheckBox_OpenLast, SIGNAL( toggled(bool) ), CheckBox_StartLocked, SLOT( setEnabled(bool) ) );
41         
42         connect(listSelectLanguage, SIGNAL( currentRowChanged(int) ), SLOT( OnSelectLanguage(int) ) );
43         
44         connect(Button_ClearFileDlgHistory, SIGNAL(clicked()), &fileDlgHistory, SLOT(clear()));
45         connect(ButtonColor1, SIGNAL( clicked() ), this, SLOT( OnColor1() ) );
46         connect(ButtonColor2, SIGNAL( clicked() ), this, SLOT( OnColor2() ) );
47         connect(ButtonTextColor, SIGNAL( clicked() ), this, SLOT( OnTextColor() ) );
48         connect(Button_MountDirBrowse,SIGNAL(clicked()),this,SLOT(OnMountDirBrowse()));
49         connect(Button_BrowserCmdBrowse,SIGNAL(clicked()),this,SLOT(OnBrowserCmdBrowse()));
50
51         //connect(Radio_IntPlugin_None,SIGNAL(toggled(bool)),this,SLOT(OnIntPluginNone()));
52         //connect(Radio_IntPlugin_Gnome,SIGNAL(toggled(bool)),this,SLOT(OnIntPluginGnome()));
53         //connect(Radio_IntPlugin_Kde,SIGNAL(toggled(bool)),this,SLOT(OnIntPluginKde()));
54
55         connect(Button_CustomizeEntryDetails,SIGNAL(clicked()),this,SLOT(OnCustomizeEntryDetails()));
56         connect(CheckBox_InactivityLock, SIGNAL(toggled(bool)), SLOT(OnInactivityLockChange(bool)));
57         
58         connect(CheckBox_Backup, SIGNAL( toggled(bool) ), CheckBox_BackupDelete, SLOT( setEnabled(bool) ) );
59         connect(CheckBox_Backup, SIGNAL( toggled(bool) ), SLOT( OnBackupDeleteChange() ) );
60         connect(CheckBox_BackupDelete, SIGNAL( toggled(bool) ), SLOT( OnBackupDeleteChange() ) );
61         connect(CheckBox_AutoSave, SIGNAL(toggled(bool)), SLOT(OnAutoSaveToggle(bool)));
62         connect(CheckBox_AutoSaveChange, SIGNAL(toggled(bool)), SLOT(OnAutoSaveChangeToggle(bool)));
63         
64 #if !defined(AUTOTYPE)
65         Box_AutoType->setVisible(false);
66 #endif
67 #if !defined(GLOBAL_AUTOTYPE)
68         Label_GlobalShortcut->setVisible(false);
69         Edit_GlobalShortcut->setVisible(false);
70         CheckBox_EntryTitlesMatch->setVisible(false);
71 #endif
72 #ifdef Q_WS_MAC
73         CheckBox_AlwaysOnTop->setVisible(false);
74 #endif
75         
76 #ifdef GLOBAL_AUTOTYPE
77         pShortcut = autoType->getShortcut();
78         connect(this,SIGNAL(rejected()),SLOT(resetGlobalShortcut()));
79 #endif
80         
81         listWidget->setCurrentRow(0);
82
83         //General (1)
84         CheckBox_OpenLast->setChecked(config->openLastFile());
85         CheckBox_RememberLastKey->setChecked(config->rememberLastKey());
86         CheckBox_ShowSysTrayIcon->setChecked(config->showSysTrayIcon());
87         CheckBox_MinimizeTray->setChecked(config->minimizeTray());
88         CheckBox_StartMinimized->setChecked(config->startMinimized());
89         CheckBox_StartLocked->setChecked(config->startLocked());
90         checkBox_SaveFileDlgHistory->setChecked(config->saveFileDlgHistory());
91         checkBox_AskBeforeDelete->setChecked(config->askBeforeDelete());
92         
93         //General (2)
94         CheckBox_Backup->setChecked(config->backup());
95         CheckBox_BackupDelete->setChecked(config->backupDelete());
96         SpinBox_BackupDeleteAfter->setValue(config->backupDeleteAfter());
97         CheckBox_AutoSave->setChecked(config->autoSave());
98         CheckBox_AutoSaveChange->setChecked(config->autoSaveChange());
99
100         switch(config->groupTreeState()){
101                 case KpxConfig::RestoreLast:
102                         Radio_GroupTreeRestore->setChecked(true);
103                         break;
104                 case KpxConfig::ExpandAll:
105                         Radio_GroupTreeExpand->setChecked(true);
106                         break;
107                 default:
108                         Radio_GroupTreeDoNothing->setChecked(true);
109         }
110
111         //Appearance
112         CheckBox_AlwaysOnTop->setChecked(config->alwaysOnTop());
113         QPixmap *pxt=new QPixmap(pixmTextColor->width(),pixmTextColor->height());
114         pxt->fill(config->bannerTextColor());
115         pixmTextColor->clear();
116         pixmTextColor->setPixmap(*pxt);
117
118         QPixmap *px1=new QPixmap(pixmColor1->width(),pixmColor1->height());
119         px1->fill(config->bannerColor1());
120         pixmColor1->clear();
121         pixmColor1->setPixmap(*px1);
122
123         QPixmap *px2=new QPixmap(pixmColor2->width(),pixmColor2->height());
124         px2->fill(config->bannerColor2());
125         pixmColor2->clear();
126         pixmColor2->setPixmap(*px2);
127
128         color1=config->bannerColor1();
129         color2=config->bannerColor2();
130         textcolor=config->bannerTextColor();
131         CheckBox_AlternatingRowColors->setChecked(config->alternatingRowColors());
132         
133         //Language
134         translations = getAllTranslations();
135         initLanguageList();
136         QString currentLang = config->language();
137         bool foundCurrent = false;
138         for (int i=0; i<translations.size(); i++){
139                 if (translations[i].nameCode==currentLang){
140                         listSelectLanguage->setCurrentRow(i+2);
141                         foundCurrent = true;
142                 }
143         }
144         if (!foundCurrent){
145                 if (currentLang=="en_US")
146                         listSelectLanguage->setCurrentRow(1);
147                 else
148                         listSelectLanguage->setCurrentRow(0);
149         }
150         
151         //Security
152         SpinBox_ClipboardTime->setValue(config->clipboardTimeOut());
153         CheckBox_ShowPasswords->setChecked(config->showPasswords());
154         CheckBox_ShowPasswords_PasswordDlg->setChecked(config->showPasswordsPasswordDlg());
155         CheckBox_LockMinimize->setChecked(config->lockOnMinimize());
156         CheckBox_InactivityLock->setChecked(config->lockOnInactivity());
157         SpinBox_InacitivtyTime->setValue(config->lockAfterSec());
158         
159         //Features
160         stackedWidget->removeWidget(pageFeatures);
161         //CheckBox_FeatureBookmarks->setChecked(config->featureBookmarks());
162
163
164         // TODO Desktop Integration
165         stackedWidget->removeWidget(pageDesktop);
166         /*if(PluginLoadError==QString())
167                 Label_IntPlugin_Error->hide();
168         else
169                 Label_IntPlugin_Error->setText(QString("<html><p style='font-weight:600; color:#8b0000;'>%1</p></body></html>")
170                                 .arg(tr("Error: %1")).arg(PluginLoadError));
171
172         switch(config->integrPlugin()){
173                 case KpxConfig::KDE:
174                         Radio_IntPlugin_Kde->setChecked(true);
175                         break;
176                 case KpxConfig::Gnome:
177                         Radio_IntPlugin_Gnome->setChecked(true);
178                         break;
179                 default:
180                         Radio_IntPlugin_None->setChecked(true);
181         }
182         if(!PluginsModified)
183                 Label_IntPlugin_Info->hide();*/
184
185
186         //Advanced
187         Box_BrowserCmd->setChecked(!config->urlCmdDef());
188         Edit_BrowserCmd->setText(config->urlCmd());
189
190         Edit_MountDir->setText(config->mountDir());
191         CheckBox_SaveRelativePaths->setChecked(config->saveRelativePaths());
192 #ifdef AUTOTYPE
193         SpinBox_AutoTypePreGap->setValue(config->autoTypePreGap());
194         SpinBox_AutoTypeKeyStrokeDelay->setValue(config->autoTypeKeyStrokeDelay());
195 #endif
196 #ifdef GLOBAL_AUTOTYPE
197         Edit_GlobalShortcut->setShortcut(config->globalShortcut());
198         CheckBox_EntryTitlesMatch->setChecked(config->entryTitlesMatch());
199 #endif
200         
201         adjustSize();
202         resize( size() + QSize(20,20) );
203 }
204
205 CSettingsDlg::~CSettingsDlg(){
206 }
207
208 void CSettingsDlg::paintEvent(QPaintEvent *event){
209         QDialog::paintEvent(event);
210         QPainter painter(this);
211         painter.setClipRegion(event->region());
212         painter.drawPixmap(QPoint(0,0),BannerPixmap);
213 }
214
215 void CSettingsDlg::resizeEvent(QResizeEvent* event){
216         createBanner(&BannerPixmap,getPixmap("appsettings"),tr("Settings"),width());
217         QDialog::resizeEvent(event);
218 }
219
220 void CSettingsDlg::OnOK()
221 {
222         apply();
223         accept();
224 }
225
226 void CSettingsDlg::OnCancel()
227 {
228         reject();
229 }
230
231 void CSettingsDlg::OnOtherButton(QAbstractButton* button){
232         if(DialogButtons->buttonRole(button)==QDialogButtonBox::ApplyRole)
233                 apply();
234 }
235
236 void CSettingsDlg::apply(){
237         
238         //General (1)
239         config->setShowSysTrayIcon(CheckBox_ShowSysTrayIcon->isChecked());
240         config->setMinimizeTray(CheckBox_MinimizeTray->isChecked());
241         config->setStartMinimized(CheckBox_StartMinimized->isChecked());
242         config->setStartLocked(CheckBox_StartLocked->isChecked());
243         config->setSaveFileDlgHistory(checkBox_SaveFileDlgHistory->isChecked());
244         if(Radio_GroupTreeRestore->isChecked())config->setGroupTreeState(KpxConfig::RestoreLast);
245         else if(Radio_GroupTreeExpand->isChecked())config->setGroupTreeState(KpxConfig::ExpandAll);
246         else config->setGroupTreeState(KpxConfig::DoNothing);
247         config->setOpenLastFile(CheckBox_OpenLast->isChecked());
248         config->setRememberLastKey(CheckBox_RememberLastKey->isChecked());
249         config->setAskBeforeDelete(checkBox_AskBeforeDelete->isChecked());
250         
251         //General (2)
252         config->setBackup(CheckBox_Backup->isChecked());
253         config->setBackupDelete(CheckBox_BackupDelete->isChecked());
254         config->setBackupDeleteAfter(SpinBox_BackupDeleteAfter->value());
255         config->setAutoSave(CheckBox_AutoSave->isChecked());
256         config->setAutoSaveChange(CheckBox_AutoSaveChange->isChecked());
257
258         //Appearence
259         config->setAlwaysOnTop(CheckBox_AlwaysOnTop->isChecked());
260         config->setBannerColor1(color1);
261         config->setBannerColor2(color2);
262         config->setBannerTextColor(textcolor);
263         config->setAlternatingRowColors(CheckBox_AlternatingRowColors->isChecked());
264
265         //Language
266         int langIndex = listSelectLanguage->currentRow();
267         QString oldLang = config->language();
268         if (langIndex==0)
269                 config->setLanguage("auto");
270         else if (langIndex==1)
271                 config->setLanguage("en_US");
272         else
273                 config->setLanguage(translations[langIndex-2].nameCode);
274         if (config->language() != oldLang){
275                 installTranslator();
276                 retranslateUi(this);
277                 initLanguageList();
278                 listSelectLanguage->setCurrentRow(langIndex);
279         }
280
281         //Security
282         config->setClipboardTimeOut(SpinBox_ClipboardTime->value());
283         config->setShowPasswords(CheckBox_ShowPasswords->isChecked());
284         config->setShowPasswordsPasswordDlg(CheckBox_ShowPasswords_PasswordDlg->isChecked());
285         config->setLockOnMinimize(CheckBox_LockMinimize->isChecked());
286         config->setLockOnInactivity(CheckBox_InactivityLock->isChecked());
287         config->setLockAfterSec(SpinBox_InacitivtyTime->value());
288
289         //Features
290         //config->setFeatureBookmarks(CheckBox_FeatureBookmarks->isChecked());
291
292         //TODO Desktop Integration
293         /*PluginsModified=Label_IntPlugin_Info->isVisible();
294         if(Radio_IntPlugin_Kde->isChecked())config->setIntegrPlugin(KpxConfig::KDE);
295         else if(Radio_IntPlugin_Gnome->isChecked())config->setIntegrPlugin(KpxConfig::Gnome);
296         else config->setIntegrPlugin(KpxConfig::NoIntegr);*/
297
298         //Advanced
299         config->setUrlCmdDef(!Box_BrowserCmd->isChecked());
300         config->setUrlCmd(Edit_BrowserCmd->text());
301         config->setMountDir(Edit_MountDir->text());
302         if(!config->mountDir().isEmpty() && config->mountDir().right(1)!="/")
303                 config->setMountDir(config->mountDir()+"/");
304         config->setSaveRelativePaths(CheckBox_SaveRelativePaths->isChecked());
305 #ifdef AUTOTYPE
306         config->setAutoTypePreGap(SpinBox_AutoTypePreGap->value());
307         config->setAutoTypeKeyStrokeDelay(SpinBox_AutoTypeKeyStrokeDelay->value());
308 #endif
309 #ifdef GLOBAL_AUTOTYPE
310         config->setGlobalShortcut(Edit_GlobalShortcut->shortcut());
311         config->setEntryTitlesMatch(CheckBox_EntryTitlesMatch->isChecked());
312 #endif
313 }
314
315 void CSettingsDlg::OnTextColor()
316 {
317         QColor c=QColorDialog::getColor(textcolor,this);
318         if(c.isValid()){
319         textcolor=c;
320         QPixmap *px=new QPixmap(pixmTextColor->width(),pixmTextColor->height());
321         px->fill(c);
322         pixmTextColor->clear();
323         pixmTextColor->setPixmap(*px);
324         createBanner(&BannerPixmap,getPixmap("appsettings"),tr("Settings"),width(),color1,color2,textcolor);
325         }
326 }
327
328
329 void CSettingsDlg::OnColor2()
330 {
331         QColor c=QColorDialog::getColor(color2,this);
332         if(c.isValid()){
333                 color2=c;
334                 QPixmap *px=new QPixmap(pixmColor2->width(),pixmColor2->height());
335                 px->fill(c);
336                 pixmColor2->clear();
337                 pixmColor2->setPixmap(*px);
338                 createBanner(&BannerPixmap,getPixmap("appsettings"),tr("Settings"),width(),color1,color2,textcolor);
339         }
340 }
341
342
343 void CSettingsDlg::OnColor1()
344 {
345         QColor c=QColorDialog::getColor(color1,this);
346         if(c.isValid()){
347                 color1=c;
348                 QPixmap *px=new QPixmap(pixmColor1->width(),pixmColor1->height());
349                 px->fill(c);
350                 pixmColor1->clear();
351                 pixmColor1->setPixmap(*px);
352                 createBanner(&BannerPixmap,getPixmap("appsettings"),tr("Settings"),width(),color1,color2,textcolor);
353         }
354 }
355
356 void CSettingsDlg::OnMountDirBrowse(){
357         QString dir=QFileDialog::getExistingDirectory(this,tr("Select a directory..."));
358         if(!dir.isEmpty()){
359                 Edit_MountDir->setText(dir);
360         }
361 }
362
363 void CSettingsDlg::OnBrowserCmdBrowse(){
364         QString filename=QFileDialog::getOpenFileName(this,tr("Select an executable..."));
365         if(!filename.isEmpty()){
366                 Edit_BrowserCmd->setText(filename);
367         }
368 }
369
370 /*void CSettingsDlg::OnIntPluginNone(){
371         Label_IntPlugin_Info->show();
372 }
373
374 void CSettingsDlg::OnIntPluginGnome(){
375         Label_IntPlugin_Info->show();
376 }
377
378 void CSettingsDlg::OnIntPluginKde(){
379         Label_IntPlugin_Info->show();
380 }*/
381
382 void CSettingsDlg::OnCustomizeEntryDetails(){
383         CustomizeDetailViewDialog dlg(this);
384         dlg.exec();
385 }
386
387 void CSettingsDlg::OnInactivityLockChange(bool checked){
388         SpinBox_InacitivtyTime->setEnabled(checked);
389 }
390
391 void CSettingsDlg::OnAutoSaveToggle(bool checked){
392         CheckBox_AutoSaveChange->setEnabled(!checked);
393 }
394
395 void CSettingsDlg::OnAutoSaveChangeToggle(bool checked){
396         CheckBox_AutoSave->setEnabled(!checked);
397 }
398
399 void CSettingsDlg::OnBackupDeleteChange(){
400         SpinBox_BackupDeleteAfter->setEnabled(CheckBox_Backup->isChecked() && CheckBox_BackupDelete->isChecked());
401 }
402
403 void CSettingsDlg::OnSelectLanguage(int index){
404         if (index == -1)
405                 return;
406         
407         if (index==0){
408                 labelLang->clear();
409                 labelAuthor->clear();
410         }
411         else if (index==1){
412                 labelLang->setText("English (United States)");
413                 labelAuthor->setText("KeePassX Development Team");
414         }
415         else{
416                 if (translations[index-2].nameLong != translations[index-2].nameEnglish)
417                         labelLang->setText(QString("%1 / %2").arg(translations[index-2].nameLong).arg(translations[index-2].nameEnglish));
418                 else
419                         labelLang->setText(translations[index-2].nameEnglish);
420                 labelAuthor->setText(translations[index-2].author);
421         }
422 }
423
424 void CSettingsDlg::initLanguageList() {
425         listSelectLanguage->clear(); // completely rebuild the list because of a bug in Qt 4.3
426         listSelectLanguage->addItem(tr("System Language"));
427         listSelectLanguage->addItem("English (United States)"); // Don't translate this string
428         for (int i=0; i<translations.size(); i++)
429                 listSelectLanguage->addItem(translations[i].nameLong);
430 }
431
432 #ifdef GLOBAL_AUTOTYPE
433 void CSettingsDlg::resetGlobalShortcut(){
434         autoType->unregisterGlobalShortcut();
435         autoType->registerGlobalShortcut(pShortcut);
436 }
437 #endif