1 //  This file is part of Qt Bitcoin Trader
2 //      https://github.com/JulyIGHOR/QtBitcoinTrader
3 //  Copyright (C) 2013-2021 July Ighor <julyighor@gmail.com>
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, either version 3 of the License, or
8 //  (at your option) any later version.
9 //
10 //  In addition, as a special exception, the copyright holders give
11 //  permission to link the code of portions of this program with the
12 //  OpenSSL library under certain conditions as described in each
13 //  individual source file, and distribute linked combinations including
14 //  the two.
15 //
16 //  You must obey the GNU General Public License in all respects for all
17 //  of the code used other than OpenSSL. If you modify file(s) with this
18 //  exception, you may extend this exception to your version of the
19 //  file(s), but you are not obligated to do so. If you do not wish to do
20 //  so, delete this exception statement from your version. If you delete
21 //  this exception statement from all source files in the program, then
22 //  also delete it here.
23 //
24 //  This program is distributed in the hope that it will be useful,
25 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
26 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27 //  GNU General Public License for more details.
28 //
29 //  You should have received a copy of the GNU General Public License
30 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
31 
32 #include "settingsgeneral.h"
33 #include "main.h"
34 #include "translationmessage.h"
35 #include "charts/chartsview.h"
36 #include <QDir>
37 
SettingsGeneral(QWidget * parent)38 SettingsGeneral::SettingsGeneral(QWidget* parent)
39     : QWidget(parent)
40 {
41     ui.setupUi(this);
42     iniSettings = new QSettings(baseValues.iniFileName, QSettings::IniFormat, this);
43     mainSettings = new QSettings(appDataDir + "/QtBitcoinTrader.cfg", QSettings::IniFormat, this);
44     hiDpiSettings = new QSettings("Centrabit", "Qt Bitcoin Trader");
45 
46     loadLanguage();
47     loadOther();
48     loadUpdates();
49     loadTime();
50 
51     ui.revertChangesButton->setEnabled(false);
52     ui.saveButton->setEnabled(false);
53     ui.forceSyncPairsButton->setEnabled(!QDir(appDataDir + "/cache").entryList(QStringList("*.cache"), QDir::Files).empty());
54 
55 #ifdef Q_OS_MAC
56     ui.closeToTrayLabel->setVisible(false);
57     ui.closeToTrayCheckBox->setVisible(false);
58 #endif
59 }
60 
~SettingsGeneral()61 SettingsGeneral::~SettingsGeneral()
62 {
63     delete iniSettings;
64     delete mainSettings;
65     delete hiDpiSettings;
66 }
67 
loadLanguage()68 void SettingsGeneral::loadLanguage()
69 {
70     QStringList langList;
71     QFile resLanguage(":/Resources/Language/LangList.ini");
72     resLanguage.open(QIODevice::ReadOnly);
73     QStringList resourceLanguages = QString(resLanguage.readAll().replace("\r", "")).split("\n");
74 
75     for (int n = 0; n < resourceLanguages.size(); n++)
76         if (!resourceLanguages.at(n).isEmpty())
77             langList << ":/Resources/Language/" + resourceLanguages.at(n);
78 
79     QStringList folderLangList = QDir(appDataDir + "/Language", "*.lng").entryList();
80     folderLangList.sort();
81 
82     for (int n = 0; n < folderLangList.size(); n++)
83         langList << appDataDir + "/Language/" + folderLangList.at(n);
84 
85     int selectedLangId = -1;
86 
87     QString preferedLangFile = julyTranslator.lastFile();
88 
89     if (!QFile::exists(preferedLangFile))
90         preferedLangFile.clear();
91 
92     if (preferedLangFile.isEmpty())
93         preferedLangFile = baseValues.defaultLangFile;
94 
95     ui.languageComboBox->clear();
96 
97     for (int n = 0; n < langList.size(); n++)
98     {
99         JulyTranslator translateName;
100         translateName.loadFromFile(langList.at(n));
101         QString langName = translateName.translateString("LANGUAGE_NAME", "");
102 
103         if (langName.isEmpty())
104             continue;
105 
106         if (preferedLangFile == langList.at(n))
107             selectedLangId = n;
108 
109         ui.languageComboBox->insertItem(ui.languageComboBox->count(), langName, langList.at(n));
110     }
111 
112     if (selectedLangId > -1)
113         ui.languageComboBox->setCurrentIndex(selectedLangId);
114 }
115 
saveLanguage()116 void SettingsGeneral::saveLanguage()
117 {
118     QString loadFromFile = ui.languageComboBox->itemData(ui.languageComboBox->currentIndex(), Qt::UserRole).toString();
119 
120     if (!loadFromFile.isEmpty())
121     {
122         julyTranslator.loadFromFile(loadFromFile);
123         mainSettings->setValue("LanguageFile", loadFromFile);
124         baseValues.mainWindow_->chartsView->refreshCharts();
125     }
126 }
127 
loadOther()128 void SettingsGeneral::loadOther()
129 {
130     ui.confirmOpenOrderCheckBox->setChecked(iniSettings->value("UI/ConfirmOpenOrder", true).toBool());
131     ui.closeToTrayCheckBox->setChecked(iniSettings->value("UI/CloseToTray", false).toBool());
132     ui.optimizeInterfaceCheckBox->setChecked(iniSettings->value("UI/OptimizeInterface", false).toBool());
133     ui.useAltDomainYobitBox->setChecked(mainSettings->value("UseAlternateDomainForYobit", false).toBool());
134     ui.hiDpiCheckBox->setChecked(hiDpiSettings->value("HiDPI", baseValues.defaultEnableHiDPI).toBool());
135 }
136 
saveOther()137 void SettingsGeneral::saveOther()
138 {
139     iniSettings->setValue("UI/ConfirmOpenOrder", ui.confirmOpenOrderCheckBox->isChecked());
140     baseValues.mainWindow_->confirmOpenOrder = ui.confirmOpenOrderCheckBox->isChecked();
141 
142 #ifdef Q_OS_MAC
143     ui.closeToTrayCheckBox->setChecked(false);
144 #endif
145     iniSettings->setValue("UI/CloseToTray", ui.closeToTrayCheckBox->isChecked());
146     baseValues.mainWindow_->closeToTray = ui.closeToTrayCheckBox->isChecked();
147 
148     iniSettings->setValue("UI/OptimizeInterface", ui.optimizeInterfaceCheckBox->isChecked());
149     mainSettings->setValue("UseAlternateDomainForYobit", ui.useAltDomainYobitBox->isChecked());
150 
151     if (ui.hiDpiCheckBox->isChecked() != hiDpiSettings->value("HiDPI", baseValues.defaultEnableHiDPI).toBool())
152     {
153         hiDpiSettings->setValue("HiDPI", ui.hiDpiCheckBox->isChecked());
154         mainSettings->remove("RowHeight");
155     }
156 }
157 
loadUpdates()158 void SettingsGeneral::loadUpdates()
159 {
160     ui.checkForUpdatesCheckBox->setChecked(mainSettings->value("CheckForUpdates", true).toBool());
161     ui.checkForUpdatesBetaCheckBox->setChecked(mainSettings->value("CheckForUpdatesBeta", false).toBool());
162     ui.autoUpdateCheckBox->setChecked(mainSettings->value("AutoUpdate", false).toBool());
163     ui.disablePairSyncCheckBox->setChecked(mainSettings->value("DisablePairSynchronization", false).toBool());
164 }
165 
saveUpdates()166 void SettingsGeneral::saveUpdates()
167 {
168     mainSettings->setValue("CheckForUpdates", ui.checkForUpdatesCheckBox->isChecked());
169     mainSettings->setValue("CheckForUpdatesBeta", ui.checkForUpdatesBetaCheckBox->isChecked());
170     mainSettings->setValue("AutoUpdate", ui.autoUpdateCheckBox->isChecked());
171     mainSettings->setValue("DisablePairSynchronization", ui.disablePairSyncCheckBox->isChecked());
172 }
173 
loadTime()174 void SettingsGeneral::loadTime()
175 {
176     ui.use24HourTimeFormatCheckBox->setChecked(mainSettings->value("Use24HourTimeFormat", true).toBool());
177     ui.timeSynchronizationCheckBox->setChecked(mainSettings->value("TimeSynchronization", true).toBool());
178 }
179 
saveTime()180 void SettingsGeneral::saveTime()
181 {
182     mainSettings->setValue("Use24HourTimeFormat", ui.use24HourTimeFormatCheckBox->isChecked());
183     mainSettings->setValue("TimeSynchronization", ui.timeSynchronizationCheckBox->isChecked());
184 
185     baseValues_->use24HourTimeFormat = ui.use24HourTimeFormatCheckBox->isChecked();
186 }
187 
on_revertChangesButton_clicked()188 void SettingsGeneral::on_revertChangesButton_clicked()
189 {
190     loadLanguage();
191     loadOther();
192     loadUpdates();
193     loadTime();
194 
195     ui.revertChangesButton->setEnabled(false);
196     ui.saveButton->setEnabled(false);
197 }
198 
on_restoreDefaultsButton_clicked()199 void SettingsGeneral::on_restoreDefaultsButton_clicked()
200 {
201     ui.languageComboBox->setCurrentIndex(ui.languageComboBox->findData(baseValues.defaultLangFile, Qt::UserRole,
202                                          Qt::MatchExactly | Qt::MatchCaseSensitive));
203     ui.confirmOpenOrderCheckBox->setChecked(true);
204     ui.closeToTrayCheckBox->setChecked(false);
205     ui.optimizeInterfaceCheckBox->setChecked(false);
206     ui.useAltDomainYobitBox->setChecked(false);
207     ui.hiDpiCheckBox->setChecked(baseValues.defaultEnableHiDPI);
208     ui.checkForUpdatesCheckBox->setChecked(true);
209     ui.checkForUpdatesBetaCheckBox->setChecked(false);
210     ui.autoUpdateCheckBox->setChecked(false);
211     ui.disablePairSyncCheckBox->setChecked(false);
212     ui.use24HourTimeFormatCheckBox->setChecked(true);
213     ui.timeSynchronizationCheckBox->setChecked(true);
214 
215     ui.restoreDefaultsButton->setEnabled(false);
216 }
217 
on_saveButton_clicked()218 void SettingsGeneral::on_saveButton_clicked()
219 {
220     saveLanguage();
221     saveOther();
222     saveUpdates();
223     saveTime();
224 
225     ui.revertChangesButton->setEnabled(false);
226     ui.saveButton->setEnabled(false);
227 }
228 
anyValueChanged()229 void SettingsGeneral::anyValueChanged()
230 {
231     if (!ui.revertChangesButton->isEnabled())
232         ui.revertChangesButton->setEnabled(true);
233 
234     if (!ui.restoreDefaultsButton->isEnabled())
235         ui.restoreDefaultsButton->setEnabled(true);
236 
237     if (!ui.saveButton->isEnabled())
238         ui.saveButton->setEnabled(true);
239 }
240 
on_showTranslationButton_clicked()241 void SettingsGeneral::on_showTranslationButton_clicked()
242 {
243     this->parentWidget()->parentWidget()->close();
244 
245     TranslationMessage translationMessage;
246     translationMessage.exec();
247 }
248 
on_forceSyncPairsButton_clicked()249 void SettingsGeneral::on_forceSyncPairsButton_clicked()
250 {
251     QStringList cacheFiles = QDir(appDataDir + "/cache").entryList(QStringList("*.cache"), QDir::Files);
252 
253     for (int i = 0; i < cacheFiles.size(); ++i)
254         QFile(appDataDir + "/cache/" + cacheFiles.at(i)).remove();
255 
256     ui.forceSyncPairsButton->setEnabled(false);
257 }
258 
on_checkForUpdatesCheckBox_stateChanged(int state)259 void SettingsGeneral::on_checkForUpdatesCheckBox_stateChanged(int state)
260 {
261     if (!state && ui.autoUpdateCheckBox->isChecked())
262         ui.autoUpdateCheckBox->setChecked(false);
263 }
264 
on_autoUpdateCheckBox_stateChanged(int state)265 void SettingsGeneral::on_autoUpdateCheckBox_stateChanged(int state)
266 {
267     if (state && !ui.checkForUpdatesCheckBox->isChecked())
268         ui.checkForUpdatesCheckBox->setChecked(true);
269 }
270