aboutsummaryrefslogtreecommitdiffstats
path: root/src/ui/function/GpgOperaHelper.cpp
blob: 35c4ba3591e13ec7434924c4f0f7ad7278408882 (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
/**
 * Copyright (C) 2021-2024 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 "GpgOperaHelper.h"

#include "core/function/gpg/GpgFileOpera.h"
#include "core/function/result_analyse/GpgDecryptResultAnalyse.h"
#include "core/function/result_analyse/GpgEncryptResultAnalyse.h"
#include "core/function/result_analyse/GpgSignResultAnalyse.h"
#include "core/model/GpgDecryptResult.h"
#include "core/model/GpgEncryptResult.h"
#include "core/model/GpgSignResult.h"
#include "core/utils/GpgUtils.h"
#include "ui/dialog/WaitingDialog.h"

namespace GpgFrontend::UI {

void GpgOperaHelper::BuildOperas(QSharedPointer<GpgOperaContextBasement>& base,
                                 int category, int channel,
                                 const GpgOperaFactory& f) {
  assert(base != nullptr);

  auto context = GetGpgOperaContextFromBasement(base, category);
  if (context == nullptr) return;

  assert(context->paths.size() == context->o_paths.size());

  for (int i = 0; i < context->paths.size(); i++) {
    context->base->operas.push_back(f(context, channel, i));
  }
}

template <typename AnalyseType>
void HandleExtraLogicIfNeeded(const QSharedPointer<GpgOperaContext>& context,
                              const AnalyseType& analyse) {}

template <>
void HandleExtraLogicIfNeeded<GpgVerifyResultAnalyse>(
    const QSharedPointer<GpgOperaContext>& context,
    const GpgVerifyResultAnalyse& analyse) {
  context->base->unknown_fprs.append(analyse.GetUnknownSignatures());
}

template <typename ResultType, typename AnalyseType, typename OperaFunc>
auto GpgOperaHelper::BuildSimpleGpgFileOperasHelper(
    QSharedPointer<GpgOperaContext>& context, int channel, int index,
    OperaFunc opera_func) -> OperaWaitingCb {
  const auto& path = context->paths[index];
  const auto& o_path = context->o_paths[index];
  auto& opera_results = context->base->opera_results;

  return [=, &opera_results](const OperaWaitingHd& op_hd) {
    opera_func(
        path, o_path,
        [=, &opera_results](GpgError err, const DataObjectPtr& data_obj) {
          // stop waiting
          op_hd();

          if (CheckGpgError(err) == GPG_ERR_USER_1 || data_obj == nullptr ||
              !data_obj->Check<ResultType>()) {
            opera_results.append(
                {-1, "# " + tr("Critical Error"), QFileInfo(path).fileName()});
            return;
          }

          auto result = ExtractParams<ResultType>(data_obj, 0);
          auto result_analyse = AnalyseType(channel, err, result);
          result_analyse.Analyse();

          HandleExtraLogicIfNeeded(context, result_analyse);

          opera_results.append(
              {result_analyse.GetStatus(), result_analyse.GetResultReport(),
               QFileInfo(path.isEmpty() ? o_path : path).fileName()});
        });
  };
}

template <typename ResultTypeA, typename AnalyseTypeA, typename ResultTypeB,
          typename AnalyseTypeB, typename OperaFunc>
auto GpgOperaHelper::BuildComplexGpgFileOperasHelper(
    QSharedPointer<GpgOperaContext>& context, int channel, int index,
    OperaFunc opera_func) -> OperaWaitingCb {
  const auto& path = context->paths[index];
  const auto& o_path = context->o_paths[index];
  auto& opera_results = context->base->opera_results;

  return [=, &opera_results](const OperaWaitingHd& op_hd) {
    opera_func(
        path, o_path,
        [=, &opera_results](GpgError err, const DataObjectPtr& data_obj) {
          // stop waiting
          op_hd();

          if (CheckGpgError(err) == GPG_ERR_USER_1 || data_obj == nullptr ||
              !data_obj->Check<ResultTypeA, ResultTypeB>()) {
            opera_results.append(
                {-1, "# " + tr("Critical Error"), QFileInfo(path).fileName()});
            return;
          }

          auto result_1 = ExtractParams<ResultTypeA>(data_obj, 0);
          auto result_2 = ExtractParams<ResultTypeB>(data_obj, 1);

          auto result_analyse_1 = AnalyseTypeA(channel, err, result_1);
          result_analyse_1.Analyse();

          HandleExtraLogicIfNeeded(context, result_analyse_1);

          auto result_analyse_2 = AnalyseTypeB(channel, err, result_2);
          result_analyse_2.Analyse();

          HandleExtraLogicIfNeeded(context, result_analyse_2);

          opera_results.append(
              {std::min(result_analyse_1.GetStatus(),
                        result_analyse_2.GetStatus()),
               result_analyse_1.GetResultReport() +
                   result_analyse_2.GetResultReport(),
               QFileInfo(path.isEmpty() ? o_path : path).fileName()});
        });
  };
}

template <typename EncryptFuncSymmetric, typename EncryptFuncKeys>
auto BuildOperasEncryptHelper(
    QSharedPointer<GpgOperaContext>& context, int channel, int index,
    EncryptFuncSymmetric encrypt_symmetric,
    EncryptFuncKeys encrypt_with_keys) -> OperaWaitingCb {
  if (context->base->keys.isEmpty()) {
    return GpgOperaHelper::BuildSimpleGpgFileOperasHelper<
        GpgEncryptResult, GpgEncryptResultAnalyse>(
        context, channel, index,
        [=](const QString& path, const QString& o_path, const auto& callback) {
          encrypt_symmetric(path, o_path, callback);
        });
  }

  return GpgOperaHelper::BuildSimpleGpgFileOperasHelper<
      GpgEncryptResult, GpgEncryptResultAnalyse>(
      context, channel, index,
      [=](const QString& path, const QString& o_path, const auto& callback) {
        encrypt_with_keys(path, o_path, callback);
      });
}

auto GpgOperaHelper::BuildOperasFileEncrypt(
    QSharedPointer<GpgOperaContext>& context, int channel,
    int index) -> OperaWaitingCb {
  return BuildOperasEncryptHelper(
      context, channel, index,
      [context, channel](const QString& path, const QString& o_path,
                         const auto& callback) {
        GpgFileOpera::GetInstance(channel).EncryptFileSymmetric(
            path, context->base->ascii, o_path, callback);
      },
      [context, channel](const QString& path, const QString& o_path,
                         const auto& callback) {
        GpgFileOpera::GetInstance(channel).EncryptFile(
            context->base->keys, path, context->base->ascii, o_path, callback);
      });
}

auto GpgOperaHelper::BuildOperasDirectoryEncrypt(
    QSharedPointer<GpgOperaContext>& context, int channel,
    int index) -> OperaWaitingCb {
  return BuildOperasEncryptHelper(
      context, channel, index,
      [context, channel](const QString& path, const QString& o_path,
                         const auto& callback) {
        GpgFileOpera::GetInstance(channel).EncryptDirectorySymmetric(
            path, context->base->ascii, o_path, callback);
      },
      [context, channel](const QString& path, const QString& o_path,
                         const auto& callback) {
        GpgFileOpera::GetInstance(channel).EncryptDirectory(
            context->base->keys, path, context->base->ascii, o_path, callback);
      });
}

auto GpgOperaHelper::BuildOperasFileDecrypt(
    QSharedPointer<GpgOperaContext>& context, int channel,
    int index) -> OperaWaitingCb {
  return BuildSimpleGpgFileOperasHelper<GpgDecryptResult,
                                        GpgDecryptResultAnalyse>(
      context, channel, index,
      [channel](const QString& path, const QString& o_path,
                const auto& callback) {
        GpgFileOpera::GetInstance(channel).DecryptFile(path, o_path, callback);
      });
}

auto GpgOperaHelper::BuildOperasArchiveDecrypt(
    QSharedPointer<GpgOperaContext>& context, int channel,
    int index) -> OperaWaitingCb {
  return BuildSimpleGpgFileOperasHelper<GpgDecryptResult,
                                        GpgDecryptResultAnalyse>(
      context, channel, index,
      [channel](const QString& path, const QString& o_path,
                const auto& callback) {
        GpgFileOpera::GetInstance(channel).DecryptArchive(path, o_path,
                                                          callback);
      });
}

auto GpgOperaHelper::BuildOperasFileSign(
    QSharedPointer<GpgOperaContext>& context, int channel,
    int index) -> OperaWaitingCb {
  return BuildSimpleGpgFileOperasHelper<GpgSignResult, GpgSignResultAnalyse>(
      context, channel, index,
      [channel, context](const QString& path, const QString& o_path,
                         const auto& callback) {
        GpgFileOpera::GetInstance(channel).SignFile(
            context->base->keys, path, context->base->ascii, o_path, callback);
      });
}

auto GpgOperaHelper::BuildOperasFileVerify(
    QSharedPointer<GpgOperaContext>& context, int channel,
    int index) -> OperaWaitingCb {
  return BuildSimpleGpgFileOperasHelper<GpgVerifyResult,
                                        GpgVerifyResultAnalyse>(
      context, channel, index,
      [channel](const QString& path, const QString& o_path,
                const auto& callback) {
        GpgFileOpera::GetInstance(channel).VerifyFile(o_path, path, callback);
      });
}

auto GpgOperaHelper::BuildOperasFileEncryptSign(
    QSharedPointer<GpgOperaContext>& context, int channel,
    int index) -> OperaWaitingCb {
  return BuildComplexGpgFileOperasHelper<GpgEncryptResult,
                                         GpgEncryptResultAnalyse, GpgSignResult,
                                         GpgSignResultAnalyse>(
      context, channel, index,
      [channel, context](const QString& path, const QString& o_path,
                         const auto& callback) {
        GpgFileOpera::GetInstance(channel).EncryptSignFile(
            context->base->keys, context->base->singer_keys, path,
            context->base->ascii, o_path, callback);
      });
}

auto GpgOperaHelper::BuildOperasDirectoryEncryptSign(
    QSharedPointer<GpgOperaContext>& context, int channel,
    int index) -> OperaWaitingCb {
  return BuildComplexGpgFileOperasHelper<GpgEncryptResult,
                                         GpgEncryptResultAnalyse, GpgSignResult,
                                         GpgSignResultAnalyse>(
      context, channel, index,
      [channel, context](const QString& path, const QString& o_path,
                         const auto& callback) {
        GpgFileOpera::GetInstance(channel).EncryptSignDirectory(
            context->base->keys, context->base->singer_keys, path,
            context->base->ascii, o_path, callback);
      });
}

auto GpgOperaHelper::BuildOperasFileDecryptVerify(
    QSharedPointer<GpgOperaContext>& context, int channel,
    int index) -> OperaWaitingCb {
  return BuildComplexGpgFileOperasHelper<
      GpgDecryptResult, GpgDecryptResultAnalyse, GpgVerifyResult,
      GpgVerifyResultAnalyse>(
      context, channel, index,
      [channel](const QString& path, const QString& o_path,
                const auto& callback) {
        GpgFileOpera::GetInstance(channel).DecryptVerifyFile(path, o_path,
                                                             callback);
      });
}

auto GpgOperaHelper::BuildOperasArchiveDecryptVerify(
    QSharedPointer<GpgOperaContext>& context, int channel,
    int index) -> OperaWaitingCb {
  return BuildComplexGpgFileOperasHelper<
      GpgDecryptResult, GpgDecryptResultAnalyse, GpgVerifyResult,
      GpgVerifyResultAnalyse>(
      context, channel, index,
      [channel](const QString& path, const QString& o_path,
                const auto& callback) {
        GpgFileOpera::GetInstance(channel).DecryptVerifyArchive(path, o_path,
                                                                callback);
      });
}

void GpgOperaHelper::WaitForMultipleOperas(
    QWidget* parent, const QString& title,
    const QContainer<OperaWaitingCb>& operas) {
  QEventLoop looper;
  QPointer<WaitingDialog> const dialog = new WaitingDialog(title, true, parent);
  connect(dialog, &QDialog::finished, &looper, &QEventLoop::quit);
  connect(dialog, &QDialog::finished, dialog, &QDialog::deleteLater);
  dialog->show();

  std::atomic<int> remaining_tasks(static_cast<int>(operas.size()));
  const auto tasks_count = operas.size();

  for (const auto& opera : operas) {
    QTimer::singleShot(64, parent, [=, &remaining_tasks]() {
      opera([dialog, &remaining_tasks, tasks_count]() {
        if (dialog == nullptr) return;

        const auto pg_value =
            static_cast<double>(tasks_count - remaining_tasks + 1) * 100.0 /
            static_cast<double>(tasks_count);
        emit dialog->SignalUpdateValue(static_cast<int>(pg_value));
        QCoreApplication::processEvents();

        if (--remaining_tasks == 0) {
          dialog->close();
          dialog->accept();
        }
      });
    });
  }

  looper.exec();
}
}  // namespace GpgFrontend::UI