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