aboutsummaryrefslogtreecommitdiffstats
path: root/src/ui/settings/SettingsDialog.cpp
blob: 33595b2583ccf38160124ef557f2af3f3089f50e (plain)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/**
 * This file is part of GPGFrontend.
 *
 * GPGFrontend is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Foobar is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Foobar.  If not, see <https://www.gnu.org/licenses/>.
 *
 * The initial version of the source code is inherited from gpg4usb-team.
 * Their source code version also complies with GNU General Public License.
 *
 * The source code version of this software was modified and released
 * by Saturneric<[email protected]> starting on May 12, 2021.
 *
 */

#include "ui/SettingsDialog.h"
#include "ui/WaitingDialog.h"

SettingsDialog::SettingsDialog(GpgFrontend::GpgContext *ctx, QWidget *parent)
        : QDialog(parent) {
    mCtx = ctx;
    tabWidget = new QTabWidget;
    generalTab = new GeneralTab(mCtx);
    appearanceTab = new AppearanceTab;
    sendMailTab = new SendMailTab;
    keyserverTab = new KeyserverTab;
    advancedTab = new AdvancedTab;
    gpgPathsTab = new GpgPathsTab;

    tabWidget->addTab(generalTab, tr("General"));
    tabWidget->addTab(appearanceTab, tr("Appearance"));
    tabWidget->addTab(sendMailTab, tr("Send Mail"));
    tabWidget->addTab(keyserverTab, tr("Key Server"));
    // tabWidget->addTab(gpgPathsTab, tr("Gpg paths"));
    tabWidget->addTab(advancedTab, tr("Advanced"));

    buttonBox =
            new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);

    connect(buttonBox, SIGNAL(accepted()), this, SLOT(slotAccept()));
    connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));

    auto *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(tabWidget);
    mainLayout->stretch(0);
    mainLayout->addWidget(buttonBox);
    mainLayout->stretch(0);
    setLayout(mainLayout);

    setWindowTitle(tr("Settings"));

    // slots for handling the restartneeded member
    this->slotSetRestartNeeded(false);
    connect(generalTab, SIGNAL(signalRestartNeeded(bool)), this,
            SLOT(slotSetRestartNeeded(bool)));
    connect(appearanceTab, SIGNAL(signalRestartNeeded(bool)), this,
            SLOT(slotSetRestartNeeded(bool)));
    connect(sendMailTab, SIGNAL(signalRestartNeeded(bool)), this,
            SLOT(slotSetRestartNeeded(bool)));
    connect(keyserverTab, SIGNAL(signalRestartNeeded(bool)), this,
            SLOT(slotSetRestartNeeded(bool)));
    connect(advancedTab, SIGNAL(signalRestartNeeded(bool)), this,
            SLOT(slotSetRestartNeeded(bool)));

    connect(this, SIGNAL(signalRestartNeeded(bool)), parent,
            SLOT(slotSetRestartNeeded(bool)));

    this->resize(480, 640);
    this->show();
}

bool SettingsDialog::getRestartNeeded() const { return this->restartNeeded; }

void SettingsDialog::slotSetRestartNeeded(bool needed) {
    this->restartNeeded = needed;
}

void SettingsDialog::slotAccept() {
    generalTab->applySettings();
    sendMailTab->applySettings();
    appearanceTab->applySettings();
    keyserverTab->applySettings();
    advancedTab->applySettings();
    gpgPathsTab->applySettings();
    if (getRestartNeeded()) {
        emit signalRestartNeeded(true);
    }
    close();
}

// http://www.informit.com/articles/article.aspx?p=1405555&seqNum=3
// http://developer.qt.nokia.com/wiki/How_to_create_a_multi_language_application
QHash<QString, QString> SettingsDialog::listLanguages() {
    QHash<QString, QString> languages;

    languages.insert("", tr("System Default"));

    QString appPath = qApp->applicationDirPath();
    QDir qmDir = QDir(RESOURCE_DIR(appPath) + "/ts/");
    QStringList fileNames = qmDir.entryList(QStringList("gpgfrontend_*.qm"));

    for (int i = 0; i < fileNames.size(); ++i) {
        QString locale = fileNames[i];
        locale.truncate(locale.lastIndexOf('.'));
        locale.remove(0, locale.indexOf('_') + 1);

        // this works in qt 4.8
        QLocale qloc(locale);
#if QT_VERSION < 0x040800
        QString language =
            QLocale::languageToString(qloc.language()) + " (" + locale +
            ")"; //+ " (" + QLocale::languageToString(qloc.language()) + ")";
#else
        QString language = qloc.nativeLanguageName() + " (" + locale + ")";
#endif
        languages.insert(locale, language);
    }
    return languages;
}

GpgPathsTab::GpgPathsTab(QWidget *parent)
        : QWidget(parent), appPath(qApp->applicationDirPath()),
          settings(RESOURCE_DIR(appPath) + "/conf/gpgfrontend.ini",
                   QSettings::IniFormat) {
    setSettings();

    /*****************************************
     * Keydb Box
     *****************************************/
    auto *keydbBox = new QGroupBox(tr("Relative path to keydb"));
    auto *keydbBoxLayout = new QGridLayout();

    // Label containing the current keydbpath relative to default keydb path
    keydbLabel = new QLabel(accKeydbPath, this);

    auto *keydbButton = new QPushButton("Change keydb path", this);
    connect(keydbButton, SIGNAL(clicked()), this, SLOT(chooseKeydbDir()));
    auto *keydbDefaultButton = new QPushButton("Set keydb to default path", this);
    connect(keydbDefaultButton, SIGNAL(clicked()), this,
            SLOT(setKeydbPathToDefault()));

    keydbBox->setLayout(keydbBoxLayout);
    keydbBoxLayout->addWidget(new QLabel(tr("Current keydb path: ")), 1, 1);
    keydbBoxLayout->addWidget(keydbLabel, 1, 2);
    keydbBoxLayout->addWidget(keydbButton, 1, 3);
    keydbBoxLayout->addWidget(keydbDefaultButton, 2, 3);
    keydbBoxLayout->addWidget(
            new QLabel(tr("<b>NOTE: </b> Gpg4usb will restart automatically if you "
                          "change the keydb path!")),
            3, 1, 1, 3);

    auto *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(keydbBox);
    mainLayout->addStretch(1);
    setLayout(mainLayout);
}

QString GpgPathsTab::getRelativePath(const QString &dir1, const QString &dir2) {
    QDir dir(dir1);
    QString s;

    s = dir.relativeFilePath(dir2);
    qDebug() << "relative path: " << s;
    if (s.isEmpty()) {
        s = ".";
    }
    return s;
}

void GpgPathsTab::setKeydbPathToDefault() {
    accKeydbPath = ".";
    keydbLabel->setText(".");
}

QString GpgPathsTab::chooseKeydbDir() {
    QString dir = QFileDialog::getExistingDirectory(
            this, tr("Choose keydb directory"), accKeydbPath,
            QFileDialog::ShowDirsOnly);

    accKeydbPath = getRelativePath(defKeydbPath, dir);
    keydbLabel->setText(accKeydbPath);
    return "";
}

void GpgPathsTab::setSettings() {
    defKeydbPath = qApp->applicationDirPath() + "/keydb";

    accKeydbPath = settings.value("gpgpaths/keydbpath").toString();
    if (accKeydbPath.isEmpty()) {
        accKeydbPath = ".";
    }
}

void GpgPathsTab::applySettings() {
    settings.setValue("gpgpaths/keydbpath", accKeydbPath);
}