aboutsummaryrefslogtreecommitdiffstats
path: root/src/ui/main_window/MainWindowFileSlotFunction.cpp
blob: e3d2957cc8205a6e8386b3bd7cd71e9f54a46e23 (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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
/**
 * Copyright (C) 2021 Saturneric <[email protected]>
 *
 * 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.
 *
 * GpgFrontend 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 GpgFrontend. If not, see <https://www.gnu.org/licenses/>.
 *
 * The initial version of the source code is inherited from
 * the gpg4usb project, which is under GPL-3.0-or-later.
 *
 * All the source code of GpgFrontend was modified and released by
 * Saturneric <[email protected]> starting on May 12, 2021.
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 */

#include <boost/format.hpp>

#include "MainWindow.h"
#include "core/function/ArchiveFileOperator.h"
#include "core/function/GlobalSettingStation.h"
#include "core/function/gpg/GpgFileOpera.h"
#include "core/function/gpg/GpgKeyGetter.h"
#include "core/function/result_analyse/GpgDecryptResultAnalyse.h"
#include "core/function/result_analyse/GpgEncryptResultAnalyse.h"
#include "core/function/result_analyse/GpgSignResultAnalyse.h"
#include "core/function/result_analyse/GpgVerifyResultAnalyse.h"
#include "core/thread/Task.h"
#include "core/utils/GpgUtils.h"
#include "ui/UserInterfaceUtils.h"
#include "ui/dialog/SignersPicker.h"

namespace GpgFrontend::UI {

void MainWindow::SlotFileEncrypt(std::filesystem::path path) {
  auto* file_tree_view = edit_->SlotCurPageFileTreeView();
  auto check_result = TargetFilePreCheck(path, true);
  if (!std::get<0>(check_result)) {
    QMessageBox::critical(
        this, _("Error"),
        QString(_("Cannot read from file: %1")).arg(path.filename().c_str()));
    return;
  }

  // check selected keys
  auto key_ids = m_key_list_->GetChecked();

  bool const non_ascii_when_export =
      GlobalSettingStation::GetInstance().LookupSettings(
          "general.non_ascii_when_export", true);
  auto out_path =
      SetExtensionOfOutputFile(path, kENCRYPT, !non_ascii_when_export);

  if (QFile::exists(out_path)) {
    auto out_file_name = boost::format(_("The target file %1% already exists, "
                                         "do you need to overwrite it?")) %
                         out_path.filename();
    auto ret =
        QMessageBox::warning(this, _("Warning"), out_file_name.str().c_str(),
                             QMessageBox::Ok | QMessageBox::Cancel);

    if (ret == QMessageBox::Cancel) return;
  }

  check_result = TargetFilePreCheck(out_path, false);
  if (!std::get<0>(check_result)) {
    QMessageBox::critical(this, _("Error"),
                          QString(_("Cannot write to file: %1"))
                              .arg(out_path.filename().c_str()));
    return;
  }

  if (key_ids->empty()) {
    // Symmetric Encrypt
    auto ret = QMessageBox::information(
        this, _("Symmetric Encryption"),
        _("No Key Selected. Do you want to encrypt with a "
          "symmetric cipher using a passphrase?"),
        QMessageBox::Ok | QMessageBox::Cancel);
    if (ret == QMessageBox::Cancel) return;

    CommonUtils::WaitForOpera(
        this, _("Symmetrically Encrypting"), [=](const OperaWaitingHd& op_hd) {
          GpgFileOpera::GetInstance().EncryptFileSymmetric(
              path, !non_ascii_when_export, out_path,
              [=](GpgError err, const DataObjectPtr& data_obj) {
                // stop waiting
                op_hd();

                if (data_obj == nullptr ||
                    !data_obj->Check<GpgEncryptResult>()) {
                  throw std::runtime_error("data object doesn't pass checking");
                }

                auto result = ExtractParams<GpgEncryptResult>(data_obj, 0);
                auto result_analyse = GpgEncryptResultAnalyse(err, result);
                result_analyse.Analyse();

                process_result_analyse(edit_, info_board_, result_analyse);
                file_tree_view->update();
              });
        });

    return;
  }

  auto p_keys = GpgKeyGetter::GetInstance().GetKeys(key_ids);

  // check key abilities
  for (const auto& key : *p_keys) {
    bool const key_can_encrypt = key.IsHasActualEncryptionCapability();

    if (!key_can_encrypt) {
      QMessageBox::critical(
          nullptr, _("Invalid KeyPair"),
          QString(_("The selected keypair cannot be used for encryption.")) +
              "<br/><br/>" + _("For example the Following Key:") + " <br/>" +
              QString::fromStdString(key.GetUIDs()->front().GetUID()));
      return;
    }
  }

  CommonUtils::WaitForOpera(
      this, _("Encrypting"), [=](const OperaWaitingHd& op_hd) {
        GpgFileOpera::GetInstance().EncryptFile(
            {p_keys->begin(), p_keys->end()}, path, !non_ascii_when_export,
            out_path, [=](GpgError err, const DataObjectPtr& data_obj) {
              // stop waiting
              op_hd();

              if (data_obj == nullptr || !data_obj->Check<GpgEncryptResult>()) {
                throw std::runtime_error("data object doesn't pass checking");
              }

              auto result = ExtractParams<GpgEncryptResult>(data_obj, 0);
              auto result_analyse = GpgEncryptResultAnalyse(err, result);
              result_analyse.Analyse();

              process_result_analyse(edit_, info_board_, result_analyse);
              file_tree_view->update();
            });
      });
}

void MainWindow::SlotDirectoryEncrypt(std::filesystem::path path) {
  auto* file_tree_view = edit_->SlotCurPageFileTreeView();
  auto check_result = TargetFilePreCheck(path, true);
  if (!std::get<0>(check_result)) {
    QMessageBox::critical(
        this, _("Error"),
        QString(_("Cannot read from file: %1")).arg(path.filename().c_str()));
    return;
  }

  // check selected keys
  auto key_ids = m_key_list_->GetChecked();
  bool const non_ascii_when_export =
      GlobalSettingStation::GetInstance().LookupSettings(
          "general.non_ascii_when_export", true);
  auto out_path = SetExtensionOfOutputFileForArchive(path, kENCRYPT,
                                                     !non_ascii_when_export);

  if (QFile::exists(out_path)) {
    auto out_file_name = boost::format(_("The target file %1% already exists, "
                                         "do you need to overwrite it?")) %
                         out_path.filename();
    auto ret =
        QMessageBox::warning(this, _("Warning"), out_file_name.str().c_str(),
                             QMessageBox::Ok | QMessageBox::Cancel);

    if (ret == QMessageBox::Cancel) return;
  }

  check_result = TargetFilePreCheck(out_path, false);
  if (!std::get<0>(check_result)) {
    QMessageBox::critical(this, _("Error"),
                          QString(_("Cannot write to file: %1"))
                              .arg(out_path.filename().c_str()));
    return;
  }

  if (key_ids->empty()) {
    // TODO
    return;
  }

  auto p_keys = GpgKeyGetter::GetInstance().GetKeys(key_ids);

  // check key abilities
  for (const auto& key : *p_keys) {
    bool const key_can_encrypt = key.IsHasActualEncryptionCapability();

    if (!key_can_encrypt) {
      QMessageBox::critical(
          nullptr, _("Invalid KeyPair"),
          QString(_("The selected keypair cannot be used for encryption.")) +
              "<br/><br/>" + _("For example the Following Key:") + " <br/>" +
              QString::fromStdString(key.GetUIDs()->front().GetUID()));
      return;
    }
  }

  CommonUtils::WaitForOpera(
      this, _("Archiving & Encrypting"), [=](const OperaWaitingHd& op_hd) {
        GpgFileOpera::GetInstance().EncryptDirectory(
            {p_keys->begin(), p_keys->end()}, path, !non_ascii_when_export,
            out_path, [=](GpgError err, const DataObjectPtr& data_obj) {
              // stop waiting
              op_hd();

              if (data_obj == nullptr || !data_obj->Check<GpgEncryptResult>()) {
                throw std::runtime_error("data object doesn't pass checking");
              }

              auto result = ExtractParams<GpgEncryptResult>(data_obj, 0);
              auto result_analyse = GpgEncryptResultAnalyse(err, result);
              result_analyse.Analyse();

              process_result_analyse(edit_, info_board_, result_analyse);
              file_tree_view->update();
            });
      });
}

void MainWindow::SlotFileDecrypt(std::filesystem::path path) {
  auto* file_tree_view = edit_->SlotCurPageFileTreeView();
  auto check_result = TargetFilePreCheck(path, true);
  if (!std::get<0>(check_result)) {
    QMessageBox::critical(
        this, _("Error"),
        QString(_("Cannot read from file: %1")).arg(path.filename().c_str()));
    return;
  }

  auto out_path = SetExtensionOfOutputFile(path, kDECRYPT, true);
  if (exists(out_path)) {
    auto ret = QMessageBox::warning(
        this, _("Warning"),
        _("The target file already exists, do you need to overwrite it?"),
        QMessageBox::Ok | QMessageBox::Cancel);

    if (ret == QMessageBox::Cancel) return;
  }

  check_result = TargetFilePreCheck(out_path, false);
  if (!std::get<0>(check_result)) {
    QMessageBox::critical(this, _("Error"),
                          QString(_("Cannot write to file: %1"))
                              .arg(out_path.filename().c_str()));
    return;
  }

  CommonUtils::WaitForOpera(
      this, _("Decrypting"), [=](const OperaWaitingHd& op_hd) {
        GpgFileOpera::GetInstance().DecryptFile(
            path, out_path, [=](GpgError err, const DataObjectPtr& data_obj) {
              // stop waiting
              op_hd();

              if (data_obj == nullptr || !data_obj->Check<GpgDecryptResult>()) {
                throw std::runtime_error("data object doesn't pass checking");
              }

              auto result = ExtractParams<GpgDecryptResult>(data_obj, 0);

              auto result_analyse = GpgDecryptResultAnalyse(err, result);
              result_analyse.Analyse();

              process_result_analyse(edit_, info_board_, result_analyse);
              file_tree_view->update();
            });
      });
}

void MainWindow::SlotArchiveDecrypt(std::filesystem::path path) {
  auto* file_tree_view = edit_->SlotCurPageFileTreeView();
  auto check_result = TargetFilePreCheck(path, true);
  if (!std::get<0>(check_result)) {
    QMessageBox::critical(
        this, _("Error"),
        QString(_("Cannot read from file: %1")).arg(path.filename().c_str()));
    return;
  }

  auto out_path = SetExtensionOfOutputFileForArchive(path, kDECRYPT, true);
  if (exists(out_path)) {
    auto ret = QMessageBox::warning(
        this, _("Warning"),
        _("The target file already exists, do you need to overwrite it?"),
        QMessageBox::Ok | QMessageBox::Cancel);

    if (ret == QMessageBox::Cancel) return;
  }

  check_result = TargetFilePreCheck(out_path, false);
  if (!std::get<0>(check_result)) {
    QMessageBox::critical(this, _("Error"),
                          QString(_("Cannot write to file: %1"))
                              .arg(out_path.filename().c_str()));
    return;
  }

  CommonUtils::WaitForOpera(
      this, _("Decrypting & Extrating"), [=](const OperaWaitingHd& op_hd) {
        GpgFileOpera::GetInstance().DecryptArchive(
            path, out_path, [=](GpgError err, const DataObjectPtr& data_obj) {
              // stop waiting
              op_hd();

              if (data_obj == nullptr || !data_obj->Check<GpgDecryptResult>()) {
                throw std::runtime_error("data object doesn't pass checking");
              }

              auto result = ExtractParams<GpgDecryptResult>(data_obj, 0);

              auto result_analyse = GpgDecryptResultAnalyse(err, result);
              result_analyse.Analyse();

              process_result_analyse(edit_, info_board_, result_analyse);
              file_tree_view->update();
            });
      });
}

void MainWindow::SlotFileSign() {
  auto* file_tree_view = edit_->SlotCurPageFileTreeView();
  auto path_qstr = file_tree_view->GetSelected();
  auto path = ConvertPathByOS(path_qstr);
  auto check_result = TargetFilePreCheck(path, true);
  if (!std::get<0>(check_result)) {
    QMessageBox::critical(
        this, _("Error"),
        QString(_("Cannot read from file: %1")).arg(path.filename().c_str()));
    return;
  }

  auto key_ids = m_key_list_->GetChecked();
  auto keys = GpgKeyGetter::GetInstance().GetKeys(key_ids);

  if (keys->empty()) {
    QMessageBox::critical(
        this, _("No Key Checked"),
        _("Please check the key in the key toolbox on the right."));
    return;
  }

  for (const auto& key : *keys) {
    if (!key.IsHasActualSigningCapability()) {
      QMessageBox::information(
          this, _("Invalid Operation"),
          QString(_("The selected key contains a key that does not actually "
                    "have a sign usage.")) +
              "<br/><br/>" + _("for example the Following Key:") + " <br/>" +
              QString::fromStdString(key.GetUIDs()->front().GetUID()));
      return;
    }
  }

  bool const non_ascii_when_export =
      GlobalSettingStation::GetInstance().LookupSettings(
          "general.non_ascii_when_export", true);
  auto sig_file_path =
      SetExtensionOfOutputFile(path, kSIGN, !non_ascii_when_export);

  if (exists(sig_file_path)) {
    auto ret = QMessageBox::warning(
        this, _("Warning"),
        QString(_("The signature file \"%1\" exists, "
                  "do you need to overwrite it?"))
            .arg(sig_file_path.filename().u8string().c_str()),
        QMessageBox::Ok | QMessageBox::Cancel);

    if (ret == QMessageBox::Cancel) return;
  }

  CommonUtils::WaitForOpera(
      this, _("Signing"), [=](const OperaWaitingHd& op_hd) {
        GpgFileOpera::GetInstance().SignFile(
            {keys->begin(), keys->end()}, path, !non_ascii_when_export,
            sig_file_path, [=](GpgError err, const DataObjectPtr& data_obj) {
              // stop waiting
              op_hd();

              if (data_obj == nullptr || !data_obj->Check<GpgSignResult>()) {
                throw std::runtime_error("data object doesn't pass checking");
              }

              auto result = ExtractParams<GpgSignResult>(data_obj, 0);
              auto result_analyse = GpgSignResultAnalyse(err, result);
              result_analyse.Analyse();

              process_result_analyse(edit_, info_board_, result_analyse);
              file_tree_view->update();
            });
      });
}

void MainWindow::SlotFileVerify() {
  auto* file_tree_view = edit_->SlotCurPageFileTreeView();
  auto path_qstr = file_tree_view->GetSelected();
  auto path = ConvertPathByOS(path_qstr);
  auto check_result = TargetFilePreCheck(path, true);
  if (!std::get<0>(check_result)) {
    QMessageBox::critical(
        this, _("Error"),
        QString(_("Cannot read from file: %1")).arg(path.filename().c_str()));
    return;
  }

  std::filesystem::path sign_file_path = path;
  std::filesystem::path data_file_path;

  if (path.extension() == ".gpg") {
    swap(data_file_path, sign_file_path);
  } else if (path.extension() == ".sig" || path.extension() == ".asc") {
    data_file_path = sign_file_path.parent_path() / sign_file_path.stem();
  }

  SPDLOG_DEBUG("sign_file_path: {} {}", sign_file_path.u8string(),
               sign_file_path.extension().u8string());

  if (path.extension() != ".gpg") {
    bool ok;
    QString const text = QInputDialog::getText(
        this, _("Origin file to verify"), _("Filepath"), QLineEdit::Normal,
        data_file_path.u8string().c_str(), &ok);
    if (ok && !text.isEmpty()) {
      data_file_path = text.toStdString();
    } else {
      return;
    }
  }

  if (!is_regular_file(data_file_path) ||
      (!sign_file_path.empty() && !is_regular_file(sign_file_path))) {
    QMessageBox::critical(
        this, _("Error"),
        _("Please select the appropriate origin file or signature file. "
          "Ensure that both are in this directory."));
    return;
  }

  SPDLOG_DEBUG("data path: {}", data_file_path.u8string());
  SPDLOG_DEBUG("sign path: {}", sign_file_path.u8string());

  CommonUtils::WaitForOpera(
      this, _("Verifying"), [=](const OperaWaitingHd& op_hd) {
        GpgFileOpera::GetInstance().VerifyFile(
            data_file_path.u8string(), sign_file_path.u8string(),
            [=](GpgError err, const DataObjectPtr& data_obj) {
              // stop waiting
              op_hd();

              if (data_obj == nullptr || !data_obj->Check<GpgVerifyResult>()) {
                throw std::runtime_error("data object doesn't pass checking");
              }

              auto result = ExtractParams<GpgVerifyResult>(data_obj, 0);
              auto result_analyse = GpgVerifyResultAnalyse(err, result);
              result_analyse.Analyse();

              process_result_analyse(edit_, info_board_, result_analyse);
              if (result_analyse.GetStatus() == -2) {
                import_unknown_key_from_keyserver(this, result_analyse);
              }
              if (result_analyse.GetStatus() >= 0) {
                show_verify_details(this, info_board_, err, result);
              }

              file_tree_view->update();
            });
      });
}

void MainWindow::SlotFileEncryptSign() {
  auto* file_tree_view = edit_->SlotCurPageFileTreeView();
  auto path_qstr = file_tree_view->GetSelected();
  auto path = ConvertPathByOS(path_qstr);
  auto check_result = TargetFilePreCheck(path, true);
  if (!std::get<0>(check_result)) {
    QMessageBox::critical(
        this, _("Error"),
        QString(_("Cannot read from file: %1")).arg(path.filename().c_str()));
    return;
  }

  // check selected keys
  auto key_ids = m_key_list_->GetChecked();
  auto p_keys = GpgKeyGetter::GetInstance().GetKeys(key_ids);

  if (p_keys->empty()) {
    QMessageBox::critical(
        this, _("No Key Checked"),
        _("Please check the key in the key toolbox on the right."));
    return;
  }

  // check key abilities
  for (const auto& key : *p_keys) {
    bool const key_can_encrypt = key.IsHasActualEncryptionCapability();

    if (!key_can_encrypt) {
      QMessageBox::critical(
          nullptr, _("Invalid KeyPair"),
          QString(_("The selected keypair cannot be used for encryption.")) +
              "<br/><br/>" + _("For example the Following Key:") + " <br/>" +
              QString::fromStdString(key.GetUIDs()->front().GetUID()));
      return;
    }
  }

  bool const non_ascii_when_export =
      GlobalSettingStation::GetInstance().LookupSettings(
          "general.non_ascii_when_export", true);

  // get file info
  QFileInfo const file_info(path);
  if (file_info.isDir()) {
    path = path.replace_extension(".tar");
  }

  const auto* extension = ".asc";
  if (non_ascii_when_export || file_info.isDir()) {
    extension = ".gpg";
  }

  auto out_path = path;
  out_path = out_path.replace_extension(path.extension().string() + extension);
  if (QFile::exists(out_path)) {
    auto ret = QMessageBox::warning(
        this, _("Warning"),
        _("The target file already exists, do you need to overwrite it?"),
        QMessageBox::Ok | QMessageBox::Cancel);

    if (ret == QMessageBox::Cancel) return;
  }

  auto* signers_picker = new SignersPicker(this);
  QEventLoop loop;
  connect(signers_picker, &SignersPicker::finished, &loop, &QEventLoop::quit);
  loop.exec();

  // return when canceled
  if (!signers_picker->GetStatus()) return;

  auto signer_key_ids = signers_picker->GetCheckedSigners();
  auto p_signer_keys = GpgKeyGetter::GetInstance().GetKeys(signer_key_ids);

  CommonUtils::WaitForOpera(
      this, _("Encrypting and Signing"), [=](const OperaWaitingHd& op_hd) {
        GpgFileOpera::GetInstance().EncryptSignFile(
            {p_keys->begin(), p_keys->end()},
            {p_signer_keys->begin(), p_signer_keys->end()}, path,
            !non_ascii_when_export, out_path,
            [=](GpgError err, const DataObjectPtr& data_obj) {
              // stop waiting
              op_hd();

              if (data_obj == nullptr ||
                  !data_obj->Check<GpgEncryptResult, GpgSignResult>()) {
                throw std::runtime_error("data object doesn't pass checking");
              }
              auto encrypt_result =
                  ExtractParams<GpgEncryptResult>(data_obj, 0);
              auto sign_result = ExtractParams<GpgSignResult>(data_obj, 1);

              auto encrypt_result_analyse =
                  GpgEncryptResultAnalyse(err, encrypt_result);
              encrypt_result_analyse.Analyse();

              auto sign_result_analyse = GpgSignResultAnalyse(err, sign_result);
              sign_result_analyse.Analyse();

              process_result_analyse(edit_, info_board_, encrypt_result_analyse,
                                     sign_result_analyse);

              file_tree_view->update();
            });
      });
}

void MainWindow::SlotFileDecryptVerify() {
  auto* file_tree_view = edit_->SlotCurPageFileTreeView();
  auto path_qstr = file_tree_view->GetSelected();
  auto path = ConvertPathByOS(path_qstr);
  auto check_result = TargetFilePreCheck(path, true);
  if (!std::get<0>(check_result)) {
    QMessageBox::critical(
        this, _("Error"),
        QString(_("Cannot read from file: %1")).arg(path.filename().c_str()));
    return;
  }

  std::filesystem::path out_path = path;
  if (path.extension() == ".asc" || path.extension() == ".gpg") {
    out_path = path.parent_path() / out_path.stem();
  } else {
    out_path += ".out";
  }
  SPDLOG_DEBUG("out path: {}", out_path.u8string());

  if (QFile::exists(out_path.u8string().c_str())) {
    auto ret =
        QMessageBox::warning(this, _("Warning"),
                             QString(_("The output file %1 already exists, do "
                                       "you need to overwrite it?"))
                                 .arg(out_path.filename().u8string().c_str()),
                             QMessageBox::Ok | QMessageBox::Cancel);

    if (ret == QMessageBox::Cancel) return;
  }

  CommonUtils::WaitForOpera(
      this, _("Decrypting and Verifying"), [=](const OperaWaitingHd& op_hd) {
        GpgFileOpera::GetInstance().DecryptVerifyFile(
            path, out_path, [=](GpgError err, const DataObjectPtr& data_obj) {
              // stop waiting
              op_hd();

              if (data_obj == nullptr ||
                  !data_obj->Check<GpgDecryptResult, GpgVerifyResult>()) {
                throw std::runtime_error("data object doesn't pass checking");
              }
              auto decrypt_result =
                  ExtractParams<GpgDecryptResult>(data_obj, 0);
              auto verify_result = ExtractParams<GpgVerifyResult>(data_obj, 1);

              auto decrypt_result_analyse =
                  GpgDecryptResultAnalyse(err, decrypt_result);
              decrypt_result_analyse.Analyse();

              auto verify_result_analyse =
                  GpgVerifyResultAnalyse(err, verify_result);
              verify_result_analyse.Analyse();

              process_result_analyse(edit_, info_board_, decrypt_result_analyse,
                                     verify_result_analyse);
              if (verify_result_analyse.GetStatus() == -2) {
                import_unknown_key_from_keyserver(this, verify_result_analyse);
              }
              if (verify_result_analyse.GetStatus() >= 0) {
                show_verify_details(this, info_board_, err, verify_result);
              }
              file_tree_view->update();
            });
      });
}

}  // namespace GpgFrontend::UI