1 //**********************************************************************************
2 //EncryptPad Copyright 2016 Evgeny Pokhilko
3 //<http://www.evpo.net/encryptpad>
4 //
5 //This file is part of EncryptPad
6 //
7 //EncryptPad is free software: you can redistribute it and/or modify
8 //it under the terms of the GNU General Public License as published by
9 //the Free Software Foundation, either version 2 of the License, or
10 //(at your option) any later version.
11 //
12 //EncryptPad is distributed in the hope that it will be useful,
13 //but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 //GNU General Public License for more details.
16 //
17 //You should have received a copy of the GNU General Public License
18 //along with EncryptPad.  If not, see <http://www.gnu.org/licenses/>.
19 //**********************************************************************************
20 #include "preferences_dialog.h"
21 #include "ui_preferences_dialog.h"
22 #include <QFontDialog>
23 #include "common_definitions.h"
24 #include "file_name_helper.h"
25 #include "file_properties_dialog.h"
26 
27 namespace
28 {
29     enum class BinderDirection
30     {
31         Set,
32         Get
33     };
34 
BindControl(QSpinBox * cnt,int & value,BinderDirection direction)35     void BindControl(QSpinBox *cnt, int &value, BinderDirection direction)
36     {
37         if(direction == BinderDirection::Get)
38         {
39             value = cnt->value();
40         }
41         else if(direction == BinderDirection::Set)
42         {
43             cnt->setValue(value);
44         }
45     }
46 
BindControl(QCheckBox * cnt,bool & value,BinderDirection direction)47     void BindControl(QCheckBox *cnt, bool &value, BinderDirection direction)
48     {
49         if(direction == BinderDirection::Get)
50         {
51             value = cnt->isChecked();
52         }
53         else if(direction == BinderDirection::Set)
54         {
55             cnt->setChecked(value);
56         }
57     }
58 
BindControl(QLineEdit * cnt,QString & value,BinderDirection direction)59     void BindControl(QLineEdit *cnt, QString &value, BinderDirection direction)
60     {
61         if(direction == BinderDirection::Get)
62         {
63             value = cnt->text();
64         }
65         else if(direction == BinderDirection::Set)
66         {
67             cnt->setText(value);
68         }
69     }
70 
BindControl(QComboBox * cnt,int & value,BinderDirection direction)71     void BindControl(QComboBox *cnt, int &value, BinderDirection direction)
72     {
73         if(direction == BinderDirection::Get)
74         {
75             value = cnt->currentData().toInt();
76         }
77         else if(direction == BinderDirection::Set)
78         {
79             int index = cnt->findData(value);
80             cnt->setCurrentIndex(index);
81         }
82     }
83 
84     struct ControlBinder
85     {
86         BinderDirection direction;
87         template<class L, class R>
Bind__anon51ebafc50111::ControlBinder88             void Bind(L *control, R &value)
89             {
90                 BindControl(control, value, direction);
91             }
ControlBinder__anon51ebafc50111::ControlBinder92         ControlBinder(BinderDirection d):direction(d){}
93     };
94 
Bind(Ui::PreferencesDialog & ui,ControlBinder & binder,PersistentPreferences & value)95     void Bind(Ui::PreferencesDialog &ui, ControlBinder &binder, PersistentPreferences &value)
96     {
97         binder.Bind(ui.uiRecentFiles, value.recentFiles);
98         binder.Bind(ui.uiWordWrap, value.wordWrap);
99         binder.Bind(ui.uiEnableFakeVim, value.enableFakeVim);
100         binder.Bind(ui.uiLastUsedDirectory, value.saveLastUsedDirectory);
101         binder.Bind(ui.uiEnableBakFiles, value.enableBakFiles);
102         binder.Bind(ui.uiLibcurl, value.libCurlPath);
103         binder.Bind(ui.uiLibcurlParameters, value.libCurlParameters);
104         binder.Bind(ui.uiWindowsEol, value.windowsEol);
105         binder.Bind(ui.uiS2KResultsPoolSize, value.s2kResultsPoolSize);
106         binder.Bind(ui.uiKeyFileLength, value.kfKeyLength);
107         binder.Bind(ui.uiTabSize, value.tabSize);
108     }
109 
ParseComboBoxData(QComboBox * combo)110     void ParseComboBoxData(QComboBox *combo)
111     {
112         for(int i = 0; i < combo->count(); i++)
113         {
114             bool ok = false;
115             int data = combo->itemText(i).toInt(&ok);
116             assert(ok);
117             combo->setItemData(i, data);
118         }
119     }
120 }
121 
PreferencesDialog(QWidget * parent)122 PreferencesDialog::PreferencesDialog(QWidget *parent) :
123     QDialog(parent, kDefaultWindowFlags),
124     defaultFilePropertiesChanged(false),
125     ui(new Ui::PreferencesDialog)
126 {
127     ui->setupUi(this);
128     setMinimumSize(sizeHint());
129     adjustSize();
130     ParseComboBoxData(ui->uiS2KResultsPoolSize);
131     ParseComboBoxData(ui->uiKeyFileLength);
132 }
133 
~PreferencesDialog()134 PreferencesDialog::~PreferencesDialog()
135 {
136     delete ui;
137 }
138 
getDefaultFilePropertiesChanged() const139 bool PreferencesDialog::getDefaultFilePropertiesChanged() const
140 {
141     return defaultFilePropertiesChanged;
142 }
143 
updateFont()144 void PreferencesDialog::updateFont()
145 {
146     QString fontText = font.family();
147     fontText += " " + tr("(Size: ");
148     fontText += QString::number(font.pointSize());
149     fontText += tr(")");
150     ui->uiFontFamily->setText(fontText);
151 }
152 
get(PersistentPreferences & value)153 void PreferencesDialog::get(PersistentPreferences &value)
154 {
155     value.defaultFileProperties = defaultFileProperties;
156     value.keyFileProperties = keyFileProperties;
157     value.font = font;
158     ControlBinder binder(BinderDirection::Get);
159     Bind(*ui, binder, value);
160 }
161 
set(PersistentPreferences & value)162 void PreferencesDialog::set(PersistentPreferences &value)
163 {
164     defaultFileProperties = value.defaultFileProperties;
165     keyFileProperties = value.keyFileProperties;
166     font = value.font;
167     updateFont();
168     ControlBinder binder(BinderDirection::Set);
169     Bind(*ui, binder, value);
170 }
171 
selectFont()172 void PreferencesDialog::selectFont()
173 {
174     bool ok;
175     QFont selected_font = QFontDialog::getFont(&ok, font, this, tr("Select Font"));
176     if(!ok)
177         return;
178 
179     font = selected_font;
180     updateFont();
181 }
182 
on_uiLibcurlBrowse_clicked()183 void PreferencesDialog::on_uiLibcurlBrowse_clicked()
184 {
185     FileRequestSelection selection = fileRequestService.RequestExistingFile(
186                 this, tr("Path to Libcurl"), ui->uiLibcurl->text(), GetLibcurlFilter());
187     if(selection.cancelled)
188         return;
189 
190     ui->uiLibcurl->setText(selection.file_name);
191 }
192 
editDefaultFileProperties()193 void PreferencesDialog::editDefaultFileProperties()
194 {
195     FilePropertiesDialog dlg(this);
196     dlg.SetUiFromMetadata(defaultFileProperties);
197     if(dlg.exec() == QDialog::Rejected || !dlg.GetIsDirty())
198         return;
199 
200     dlg.UpdateMetadataFromUi(defaultFileProperties);
201     if(dlg.GetIsDirty())
202         defaultFilePropertiesChanged = true;
203 }
204 
editKeyFileProperties()205 void PreferencesDialog::editKeyFileProperties()
206 {
207     FilePropertiesDialog dlg(this);
208     dlg.SetUiFromMetadata(keyFileProperties);
209     if(dlg.exec() == QDialog::Rejected || !dlg.GetIsDirty())
210         return;
211 
212     dlg.UpdateMetadataFromUi(keyFileProperties);
213 }
214