aboutsummaryrefslogtreecommitdiffstats
path: root/src/ui/main_window/MainWindowSlotFunction.cpp
blob: 9c5d500acd962875fed553edaa011baf2c8b1760 (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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/**
 * 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 "MainWindow.h"

void MainWindow::slotEncrypt() {

    if (edit->tabCount() == 0 || edit->slotCurPage() == nullptr) {
        return;
    }

    QVector<GpgKey> keys;
    mKeyList->getCheckedKeys(keys);

    if (keys.count() == 0) {
        QMessageBox::critical(nullptr, tr("No Key Selected"), tr("No Key Selected"));
        return;
    }

    for (const auto &key : keys) {
        if (!GpgME::GpgContext::checkIfKeyCanEncr(key)) {
            QMessageBox::information(nullptr,
                                     tr("Invalid Operation"),
                                     tr("The selected key contains a key that does not actually have a encrypt function.<br/>")
                                     + tr("<br/>For example the Following Key: <br/>") + key.uids.first().uid);
            return;

        }
    }

    auto *tmp = new QByteArray();

    gpgme_encrypt_result_t result = nullptr;
    auto error = mCtx->encrypt(keys, edit->curTextPage()->toPlainText().toUtf8(), tmp, &result);

    auto resultAnalyse = new EncryptResultAnalyse(error, result);
    auto &reportText = resultAnalyse->getResultReport();

    auto *tmp2 = new QString(*tmp);
    edit->slotFillTextEditWithText(*tmp2);
    infoBoard->associateTextEdit(edit->curTextPage());

    if (resultAnalyse->getStatus() < 0)
        infoBoard->slotRefresh(reportText, INFO_ERROR_CRITICAL);
    else if (resultAnalyse->getStatus() > 0)
        infoBoard->slotRefresh(reportText, INFO_ERROR_OK);
    else
        infoBoard->slotRefresh(reportText, INFO_ERROR_WARN);

    delete resultAnalyse;
}

void MainWindow::slotSign() {
    if (edit->tabCount() == 0 || edit->slotCurPage() == nullptr) {
        return;
    }

    QVector<GpgKey> keys;

    mKeyList->getPrivateCheckedKeys(keys);

    if (keys.isEmpty()) {
        QMessageBox::critical(nullptr, tr("No Key Selected"), tr("No Key Selected"));
        return;
    }

    for (const auto &key : keys) {
        if (!GpgME::GpgContext::checkIfKeyCanSign(key)) {
            QMessageBox::information(nullptr,
                                     tr("Invalid Operation"),
                                     tr("The selected key contains a key that does not actually have a signature function.<br/>")
                                     + tr("<br/>For example the Following Key: <br/>") + key.uids.first().uid);
            return;
        }
    }

    auto *tmp = new QByteArray();

    gpgme_sign_result_t result = nullptr;

    auto error = mCtx->sign(keys, edit->curTextPage()->toPlainText().toUtf8(), tmp, false, &result);
    infoBoard->associateTextEdit(edit->curTextPage());
    edit->slotFillTextEditWithText(QString::fromUtf8(*tmp));

    auto resultAnalyse = new SignResultAnalyse(error, result);

    auto &reportText = resultAnalyse->getResultReport();
    if (resultAnalyse->getStatus() < 0)
        infoBoard->slotRefresh(reportText, INFO_ERROR_CRITICAL);
    else if (resultAnalyse->getStatus() > 0)
        infoBoard->slotRefresh(reportText, INFO_ERROR_OK);
    else
        infoBoard->slotRefresh(reportText, INFO_ERROR_WARN);

    delete resultAnalyse;
}

void MainWindow::slotDecrypt() {
    if (edit->tabCount() == 0 || edit->slotCurPage() == nullptr) {
        return;
    }

    auto *decrypted = new QByteArray();
    QByteArray text = edit->curTextPage()->toPlainText().toUtf8();
    GpgME::GpgContext::preventNoDataErr(&text);

    gpgme_decrypt_result_t result = nullptr;
    // try decrypt, if fail do nothing, especially don't replace text
    auto error = mCtx->decrypt(text, decrypted, &result);
    infoBoard->associateTextEdit(edit->curTextPage());

    if(gpgme_err_code(error) == GPG_ERR_NO_ERROR)
        edit->slotFillTextEditWithText(QString::fromUtf8(*decrypted));

    auto resultAnalyse = new DecryptResultAnalyse(mCtx, error, result);

    auto &reportText = resultAnalyse->getResultReport();
    if (resultAnalyse->getStatus() < 0)
        infoBoard->slotRefresh(reportText, INFO_ERROR_CRITICAL);
    else if (resultAnalyse->getStatus() > 0)
        infoBoard->slotRefresh(reportText, INFO_ERROR_OK);
    else
        infoBoard->slotRefresh(reportText, INFO_ERROR_WARN);

    delete resultAnalyse;
}

void MainWindow::slotFind() {
    if (edit->tabCount() == 0 || edit->curTextPage() == nullptr) {
        return;
    }

    // At first close verifynotification, if existing
    edit->slotCurPage()->closeNoteByClass("findwidget");

    auto *fw = new FindWidget(this, edit->curTextPage());
    edit->slotCurPage()->showNotificationWidget(fw, "findWidget");

}

void MainWindow::slotVerify() {
    if (edit->tabCount() == 0 || edit->slotCurPage() == nullptr) {
        return;
    }

    // If an unknown key is found, enable the importfromkeyserveraction

    QByteArray text = edit->curTextPage()->toPlainText().toUtf8();
    GpgME::GpgContext::preventNoDataErr(&text);


    gpgme_verify_result_t result;

    auto error = mCtx->verify(&text, nullptr, &result);

    auto resultAnalyse = new VerifyResultAnalyse(mCtx, error, result);
    infoBoard->associateTextEdit(edit->curTextPage());

    auto &reportText = resultAnalyse->getResultReport();
    if (resultAnalyse->getStatus() < 0)
        infoBoard->slotRefresh(reportText, INFO_ERROR_CRITICAL);
    else if (resultAnalyse->getStatus() > 0)
        infoBoard->slotRefresh(reportText, INFO_ERROR_OK);
    else
        infoBoard->slotRefresh(reportText, INFO_ERROR_WARN);

    if (resultAnalyse->getStatus() >= 0) {
        infoBoard->resetOptionActionsMenu();
        infoBoard->addOptionalAction("Show Verify Details", [this, error, result]() {
            VerifyDetailsDialog(this, mCtx, mKeyList, error, result);
        });
    }

    delete resultAnalyse;
}

/*
 * Append the selected (not checked!) Key(s) To Textedit
 */
void MainWindow::slotAppendSelectedKeys() {
    if (edit->tabCount() == 0 || edit->slotCurPage() == nullptr) {
        return;
    }

    auto *keyArray = new QByteArray();
    mCtx->exportKeys(mKeyList->getSelected(), keyArray);
    edit->curTextPage()->append(*keyArray);
}

void MainWindow::slotCopyMailAddressToClipboard() {
    if (mKeyList->getSelected()->isEmpty()) {
        return;
    }
    auto &key = mCtx->getKeyById(mKeyList->getSelected()->first());
    QClipboard *cb = QApplication::clipboard();
    QString mail = key.email;
    cb->setText(mail);
}

void MainWindow::slotShowKeyDetails() {
    if (mKeyList->getSelected()->isEmpty()) {
        return;
    }
    auto &key = mCtx->getKeyById(mKeyList->getSelected()->first());
    if (key.good) {
        new KeyDetailsDialog(mCtx, key, this);
    }
}

void MainWindow::refreshKeysFromKeyserver() {
    if (mKeyList->getSelected()->isEmpty()) {
        return;
    }

    auto *dialog = new KeyServerImportDialog(mCtx, mKeyList, true, this);
    dialog->show();
    dialog->slotImport(*mKeyList->getSelected());

}

void MainWindow::uploadKeyToServer() {
    QVector<GpgKey> keys;
    keys.append(mKeyList->getSelectedKey());
    auto *dialog = new KeyUploadDialog(mCtx, keys, this);
    dialog->show();
    dialog->slotUpload();
}

void MainWindow::slotFileEncrypt() {
    QStringList *keyList;
    keyList = mKeyList->getChecked();
    new FileEncryptionDialog(mCtx, *keyList, FileEncryptionDialog::Encrypt, this);
}

void MainWindow::slotFileDecrypt() {
    QStringList *keyList;
    keyList = mKeyList->getChecked();
    new FileEncryptionDialog(mCtx, *keyList, FileEncryptionDialog::Decrypt, this);
}

void MainWindow::slotFileSign() {
    QStringList *keyList;
    keyList = mKeyList->getChecked();
    new FileEncryptionDialog(mCtx, *keyList, FileEncryptionDialog::Sign, this);
}

void MainWindow::slotFileVerify() {
    QStringList *keyList;
    keyList = mKeyList->getChecked();
    new FileEncryptionDialog(mCtx, *keyList, FileEncryptionDialog::Verify, this);
}

void MainWindow::slotEncryptSign() {

    if (edit->tabCount() == 0 || edit->slotCurPage() == nullptr) {
        return;
    }

    QVector<GpgKey> keys;
    mKeyList->getCheckedKeys(keys);

    if (keys.empty()) {
        QMessageBox::critical(nullptr, tr("No Key Selected"), tr("No Key Selected"));
        return;
    }

    for (const auto &key : keys) {
        if (!GpgME::GpgContext::checkIfKeyCanSign(key) || !GpgME::GpgContext::checkIfKeyCanEncr(key)) {
            QMessageBox::information(nullptr,
                                     tr("Invalid Operation"),
                                     tr("The selected key cannot be used for signing and encryption at the same time.<br/>")
                                     + tr("<br/>For example the Following Key: <br/>") + key.uids.first().uid);
            return;
        }
    }

    auto *tmp = new QByteArray();
    gpgme_encrypt_result_t encr_result = nullptr;
    gpgme_sign_result_t sign_result = nullptr;

    auto error = mCtx->encryptSign(keys, edit->curTextPage()->toPlainText().toUtf8(), tmp, &encr_result, &sign_result);
    auto *tmp2 = new QString(*tmp);
    edit->slotFillTextEditWithText(*tmp2);

    auto resultAnalyseEncr = new EncryptResultAnalyse(error, encr_result);
    auto resultAnalyseSign = new SignResultAnalyse(error, sign_result);
    int status = std::min(resultAnalyseEncr->getStatus(), resultAnalyseSign->getStatus());
    auto reportText = resultAnalyseEncr->getResultReport() + resultAnalyseSign->getResultReport();

    infoBoard->associateTextEdit(edit->curTextPage());

    if (status < 0)
        infoBoard->slotRefresh(reportText, INFO_ERROR_CRITICAL);
    else if (status > 0)
        infoBoard->slotRefresh(reportText, INFO_ERROR_OK);
    else
        infoBoard->slotRefresh(reportText, INFO_ERROR_WARN);

    delete resultAnalyseEncr;
    delete resultAnalyseSign;
}

void MainWindow::slotDecryptVerify() {

    if (edit->tabCount() == 0 || edit->slotCurPage() == nullptr) {
        return;
    }

    auto *decrypted = new QByteArray();
    QByteArray text = edit->curTextPage()->toPlainText().toUtf8();
    GpgME::GpgContext::preventNoDataErr(&text);

    gpgme_decrypt_result_t d_result = nullptr;
    gpgme_verify_result_t v_result = nullptr;
    // try decrypt, if fail do nothing, especially don't replace text
    auto error = mCtx->decryptVerify(text, decrypted, &d_result, &v_result);
    infoBoard->associateTextEdit(edit->curTextPage());

    if(gpgme_err_code(error) == GPG_ERR_NO_ERROR)
        edit->slotFillTextEditWithText(QString::fromUtf8(*decrypted));

    auto resultAnalyseDecrypt = new DecryptResultAnalyse(mCtx, error, d_result);
    auto resultAnalyseVerify = new VerifyResultAnalyse(mCtx, error, v_result);

    int status = std::min(resultAnalyseDecrypt->getStatus(), resultAnalyseVerify->getStatus());
    auto &reportText = resultAnalyseDecrypt->getResultReport() + resultAnalyseVerify->getResultReport();
    if (status < 0)
        infoBoard->slotRefresh(reportText, INFO_ERROR_CRITICAL);
    else if (status > 0)
        infoBoard->slotRefresh(reportText, INFO_ERROR_OK);
    else
        infoBoard->slotRefresh(reportText, INFO_ERROR_WARN);

    if (resultAnalyseVerify->getStatus() >= 0) {
        infoBoard->resetOptionActionsMenu();
        infoBoard->addOptionalAction("Show Verify Details", [this, error, v_result]() {
            VerifyDetailsDialog(this, mCtx, mKeyList, error, v_result);
        });
    }
    delete resultAnalyseDecrypt;
    delete resultAnalyseVerify;
}