| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "gui/dialogs/formsettings.h" |
| 4 | |
| 5 | #include "definitions/definitions.h" |
| 6 | #include "gui/guiutilities.h" |
| 7 | #include "gui/messagebox.h" |
| 8 | #include "gui/settings/settingsbrowsermail.h" |
| 9 | #include "gui/settings/settingsdatabase.h" |
| 10 | #include "gui/settings/settingsdownloads.h" |
| 11 | #include "gui/settings/settingsfeedsmessages.h" |
| 12 | #include "gui/settings/settingsgeneral.h" |
| 13 | #include "gui/settings/settingsgui.h" |
| 14 | #include "gui/settings/settingslocalization.h" |
| 15 | #include "gui/settings/settingsmediaplayer.h" |
| 16 | #include "gui/settings/settingsnodejs.h" |
| 17 | #include "gui/settings/settingsnotifications.h" |
| 18 | #include "gui/settings/settingsshortcuts.h" |
| 19 | #include "miscellaneous/application.h" |
| 20 | #include "miscellaneous/iconfactory.h" |
| 21 | #include "miscellaneous/settings.h" |
| 22 | |
| 23 | #include <QPainter> |
| 24 | #include <QScrollArea> |
| 25 | #include <QScrollBar> |
| 26 | |
| 27 | FormSettings::FormSettings(QWidget& parent) : QDialog(&parent), m_settings(*qApp->settings()) { |
| 28 | m_ui.setupUi(this); |
| 29 | |
| 30 | // Set flags and attributes. |
| 31 | GuiUtilities::applyDialogProperties(*this, |
| 32 | qApp->icons()->fromTheme(QSL("emblem-system"), QSL("applications-system"))); |
| 33 | |
| 34 | m_btnApply = m_ui.m_buttonBox->button(QDialogButtonBox::StandardButton::Apply); |
| 35 | |
| 36 | m_btnApply->setEnabled(false); |
| 37 | |
| 38 | // Establish needed connections. |
| 39 | connect(m_ui.m_buttonBox, &QDialogButtonBox::accepted, this, &FormSettings::saveSettings); |
| 40 | connect(m_ui.m_buttonBox, &QDialogButtonBox::rejected, this, &FormSettings::cancelSettings); |
| 41 | |
| 42 | connect(m_btnApply, &QPushButton::clicked, this, &FormSettings::applySettings); |
| 43 | connect(m_ui.m_listSettings, &QListWidget::currentRowChanged, this, &FormSettings::openSettingsCategory); |
| 44 | |
| 45 | addSettingsPanel(new SettingsGeneral(&m_settings, this)); |
| 46 | addSettingsPanel(new SettingsDatabase(&m_settings, this)); |
| 47 | addSettingsPanel(new SettingsGui(&m_settings, this)); |
| 48 | addSettingsPanel(new SettingsNotifications(&m_settings, this)); |
| 49 | addSettingsPanel(new SettingsLocalization(&m_settings, this)); |
| 50 | addSettingsPanel(new SettingsShortcuts(&m_settings, this)); |
| 51 | addSettingsPanel(new SettingsBrowserMail(&m_settings, this)); |
| 52 | addSettingsPanel(new SettingsNodejs(&m_settings, this)); |
| 53 | addSettingsPanel(new SettingsMediaPlayer(&m_settings, this)); |
| 54 | addSettingsPanel(new SettingsDownloads(&m_settings, this)); |
| 55 | addSettingsPanel(new SettingsFeedsMessages(&m_settings, this)); |
| 56 | |
| 57 | m_ui.m_listSettings->setMaximumWidth(m_ui.m_listSettings->sizeHintForColumn(0) + |
| 58 | 6 * m_ui.m_listSettings->frameWidth()); |
| 59 | m_ui.m_listSettings->setCurrentRow(0); |
| 60 | } |
| 61 | |
| 62 | FormSettings::~FormSettings() { |
| 63 | qDebugNN << LOGSEC_GUI << "Destroying FormSettings distance."; |
| 64 | } |
| 65 | |
| 66 | void FormSettings::reject() { |
| 67 | m_ui.m_buttonBox->button(QDialogButtonBox::StandardButton::Cancel)->click(); |
| 68 | } |
| 69 | |
| 70 | void FormSettings::openSettingsCategory(int category) { |
| 71 | if (category >= 0 && category < m_panels.size()) { |
| 72 | if (!m_panels.at(category)->isLoaded()) { |
| 73 | m_panels.at(category)->loadSettings(); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | m_ui.m_stackedSettings->setCurrentIndex(category); |
| 78 | } |
| 79 | |
| 80 | void FormSettings::saveSettings() { |
| 81 | applySettings(); |
| 82 | accept(); |
| 83 | } |
| 84 | |
| 85 | void FormSettings::applySettings() { |
| 86 | // Save all settings. |
| 87 | m_settings.checkSettings(); |
| 88 | QStringList panels_for_restart; |
| 89 | |
| 90 | for (SettingsPanel* panel : std::as_const(m_panels)) { |
| 91 | if (panel->isDirty() && panel->isLoaded()) { |
| 92 | panel->saveSettings(); |
| 93 | } |
| 94 | |
| 95 | if (panel->requiresRestart()) { |
| 96 | panels_for_restart.append(panel->title().toLower()); |
| 97 | panel->setRequiresRestart(false); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | if (!panels_for_restart.isEmpty()) { |
| 102 | const QStringList changed_settings_description = |
| 103 | panels_for_restart.replaceInStrings(QRegularExpression(QSL("^")), QString::fromUtf8(QByteArray(" • "))); |
| 104 | const QMessageBox::StandardButton clicked_button = |
| 105 | MsgBox::show(this, |
| 106 | QMessageBox::Icon::Question, |
| 107 | tr("Critical settings were changed"), |
| 108 | tr("Some critical settings were changed and will be applied after the application gets restarted. " |
| 109 | "\n\nYou have to restart manually."), |
| 110 | tr("Do you want to restart now?"), |
| 111 | tr("Changed categories of settings:\n%1.").arg(changed_settings_description.join(QSL(",\n"))), |
| 112 | QMessageBox::StandardButton::Yes | QMessageBox::StandardButton::No, |
| 113 | QMessageBox::StandardButton::Yes); |
| 114 | |
| 115 | if (clicked_button == QMessageBox::Yes) { |
| 116 | qApp->restart(); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | m_btnApply->setEnabled(false); |
| 121 | } |
| 122 | |
| 123 | void FormSettings::cancelSettings() { |
| 124 | QStringList changed_panels; |
| 125 | |
| 126 | for (SettingsPanel* panel : std::as_const(m_panels)) { |
| 127 | if (panel->isLoaded() && panel->isDirty()) { |
| 128 | changed_panels.append(panel->title().toLower()); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | if (changed_panels.isEmpty()) { |
| 133 | done(QDialog::DialogCode::Rejected); |
| 134 | } |
| 135 | else { |
| 136 | const QStringList changed_settings_description = |
| 137 | changed_panels.replaceInStrings(QRegularExpression(QSL("^")), QString::fromUtf8(QByteArray(" • "))); |
| 138 | |
| 139 | if (MsgBox::show(this, |
| 140 | QMessageBox::Icon::Critical, |
| 141 | tr("Some settings are changed and will be lost"), |
| 142 | tr("Some settings were changed and by cancelling this dialog, you would lose these changes."), |
| 143 | tr("Do you really want to close this dialog without saving any settings?"), |
| 144 | tr("Changed categories of settings:\n%1.").arg(changed_settings_description.join(QSL(",\n"))), |
| 145 | QMessageBox::StandardButton::Yes | QMessageBox::StandardButton::No, |
| 146 | QMessageBox::StandardButton::Yes) == QMessageBox::StandardButton::Yes) { |
| 147 | done(QDialog::DialogCode::Rejected); |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | void FormSettings::addSettingsPanel(SettingsPanel* panel) { |
| 153 | QListWidgetItem* itm = new QListWidgetItem(m_ui.m_listSettings); |
| 154 | |
| 155 | itm->setText(panel->title()); |
| 156 | itm->setIcon(panel->icon()); |
| 157 | |
| 158 | m_panels.append(panel); |
| 159 | |
| 160 | QScrollArea* scr = new QScrollArea(m_ui.m_stackedSettings); |
| 161 | |
| 162 | scr->setWidgetResizable(true); |
| 163 | scr->setFrameShape(QFrame::Shape::StyledPanel); |
| 164 | scr->setWidget(panel); |
| 165 | |
| 166 | m_ui.m_stackedSettings->addWidget(scr); |
| 167 | |
| 168 | connect(panel, &SettingsPanel::settingsChanged, this, [this]() { |
| 169 | m_btnApply->setEnabled(true); |
| 170 | }); |
| 171 | } |