1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
#include "settingsdialog.h"
#include "src/net/instance.h"
#include "src/settings_interface.h"
#include <QPushButton>
#include <QDialogButtonBox>
#include <QDesktopServices>
#include <QUrl>
#include <QMessageBox>
#include <QInputDialog>
#include <qnamespace.h>
SettingsDialog::SettingsDialog(QWidget* parent)
: QDialog(parent, Qt::Dialog), ui(new Ui::SettingsDialog)
{
ui->setupUi(this);
// Create connections for each dialog button box button, as this is the best way to do it
connect(ui->buttonBox->button(QDialogButtonBox::RestoreDefaults), &QPushButton::clicked, this, &SettingsDialog::defaults_button_clicked);
connect(ui->buttonBox->button(QDialogButtonBox::Apply), &QPushButton::clicked, this, &SettingsDialog::apply_button_clicked);
connect(ui->buttonBox->button(QDialogButtonBox::Cancel), &QPushButton::clicked, this, &SettingsDialog::cancel_button_clicked);
#ifndef MASTODONPP_BUILD
// Disable unusable settings
// TODO: This should be more precise if we ever add other Fedi APIs, as it would disable all of them and not only the Mastodon one.
ui->remoteInstanceGroupBox->setDisabled(true);
ui->remoteInstanceGroupBox->setToolTip(tr("Setting disabled as not built with mastodonpp support"));
#endif
update_ui();
}
SettingsDialog::~SettingsDialog() {
delete ui;
}
/* ----- General tab ----- */
void SettingsDialog::on_timezoneCombo_activated(int index) {
if (update_ui_in_progress) return;
settings_interface.write_setting("ui/timezone", index);
}
/* ----- Network tab ----- */
void SettingsDialog::on_connectToTheInternetCheckBox_clicked(bool checked) {
if (update_ui_in_progress) return;
settings_interface.write_setting("net/access_internet", checked);
}
void SettingsDialog::on_DownloadCustomEmojiCheckBox_clicked(bool checked) {
if (update_ui_in_progress) return;
settings_interface.write_setting("net/download_emoji", checked);
}
void SettingsDialog::on_downloadAttachmentsCheckBox_clicked(bool checked) {
if (update_ui_in_progress) return;
settings_interface.write_setting("net/download_attachments", checked);
}
void SettingsDialog::on_instanceTypeComboBox_activated(int index) {
if (update_ui_in_progress) return;
settings_interface.write_setting("net/instance/type", index);
}
void SettingsDialog::on_instanceAddressLineEdit_editingFinished() {
if (update_ui_in_progress) return;
settings_interface.write_setting("net/instance/address", ui->instanceAddressLineEdit->text());
}
void SettingsDialog::on_tokenLineEdit_editingFinished() {
if (update_ui_in_progress) return;
settings_interface.write_setting("net/instance/token", ui->tokenLineEdit->text());
}
void SettingsDialog::on_instanceActionsLabel_linkActivated(const QString& link) {
if (link == "action:fill-address") {
update_ui_in_progress = true;
ui->instanceAddressLineEdit->setText(instance_address);
update_ui_in_progress = false;
} else if (link == "action:request-token") {
InstancePtr instance = Instance::create_instance((AppSettingsTypes::InstanceType)ui->instanceTypeComboBox->currentIndex());
QString url = instance->oauth2_step1();
if (not QDesktopServices::openUrl(url))
QMessageBox::information(this, tr("Navigate to this URL"), tr("Please open the following URL in your browser: %1").arg(url));
Instance::OAuth2Step2 step2 = {.ok = false};
while (not step2.ok) {
bool ok;
QString code = QInputDialog::getText(this, tr("Enter authorization code"), tr("Enter the given authentification code:"), QLineEdit::Normal, "", &ok);
if (code.isEmpty()) return; // User canceled
if (ok) step2 = instance->oauth2_step2(code);
}
// Do not set update_ui_in_progress as we want the new text to be seen as new text by settings_interface (through on_tokenLineEdit_editingFinished())
ui->tokenLineEdit->setText(step2.token);
on_tokenLineEdit_editingFinished(); // Make sure that the change has been noticed as sometimes it isn't
}
}
/* ----- Dialog & setting saving/restoring ----- */
// OK button pressed, save and apply new settings
void SettingsDialog::apply_button_clicked() {
settings_interface.commit();
emit QDialog::accept(); // Closes the dialog with return code
}
// Cancel button pressed, discard new settings
void SettingsDialog::cancel_button_clicked() {
emit QDialog::reject(); // Closes the dialog with return code
}
// Restore defaults button pressed, reset settings
void SettingsDialog::defaults_button_clicked() {
settings_interface.clear_all();
update_ui();
}
// Update the UI's setting values with the values in settings_interface.
void SettingsDialog::update_ui() {
update_ui_in_progress = true;
// General tab
ui->timezoneCombo->setCurrentIndex(settings_interface.read_setting("ui/timezone").value<AppSettingsTypes::Timezone>());
// Network tab
ui->connectToTheInternetCheckBox->setChecked(settings_interface.read_setting("net/access_internet").value<bool>());
ui->DownloadCustomEmojiCheckBox->setChecked(settings_interface.read_setting("net/download_emoji").value<bool>());
ui->downloadAttachmentsCheckBox->setChecked(settings_interface.read_setting("net/download_attachments").value<bool>());
// Instance frame
ui->instanceTypeComboBox->setCurrentIndex(settings_interface.read_setting("net/instance/type").value<AppSettingsTypes::InstanceType>());
ui->instanceAddressLineEdit->setText(settings_interface.read_setting("net/instance/address").value<QString>());
ui->tokenLineEdit->setText(settings_interface.read_setting("net/instance/token").value<QString>());
update_ui_in_progress = false;
}
void SettingsDialog::set_instance_address(const QString &url) {
instance_address = url;
}
|