aboutsummaryrefslogtreecommitdiffstats
path: root/src/ui/KeyMgmt.cpp
blob: 0c3d6a14afbf4408ecf83685c7edc1f3f7abdee2 (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
/**
 * 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/KeyMgmt.h"

#include <utility>

#include "gpg/function/GpgKeyGetter.h"
#include "gpg/function/GpgKeyImportExportor.h"
#include "gpg/function/GpgKeyOpera.h"
#include "ui/SignalStation.h"

namespace GpgFrontend::UI {
KeyMgmt::KeyMgmt(QWidget* parent)
    : QMainWindow(parent),
      appPath(qApp->applicationDirPath()),
      settings(RESOURCE_DIR(appPath) + "/conf/gpgfrontend.ini",
               QSettings::IniFormat) {
  /* the list of Keys available*/
  mKeyList = new KeyList();
  mKeyList->setColumnWidth(2, 250);
  mKeyList->setColumnWidth(3, 250);
  setCentralWidget(mKeyList);
  mKeyList->setDoubleClickedAction([this](const GpgKey& key, QWidget* parent) {
    new KeyDetailsDialog(key, parent);
  });

  createActions();
  createMenus();
  createToolBars();
  connect(this, SIGNAL(signalStatusBarChanged(QString)), this->parent(),
          SLOT(slotSetStatusBarText(QString)));

  /* Restore the iconstyle */
  this->settings.sync();

  QSize iconSize = settings.value("toolbar/iconsize", QSize(24, 24)).toSize();
  settings.setValue("toolbar/iconsize", iconSize);

  Qt::ToolButtonStyle buttonStyle = static_cast<Qt::ToolButtonStyle>(
      settings.value("toolbar/iconstyle", Qt::ToolButtonTextUnderIcon)
          .toUInt());
  this->setIconSize(iconSize);
  this->setToolButtonStyle(buttonStyle);

  // state sets pos & size of dock-widgets
  this->restoreState(this->settings.value("keymgmt/windowState").toByteArray());

  qDebug() << "windows/windowSave"
           << this->settings.value("window/windowSave").toBool();

  // Restore window size & location
  if (this->settings.value("keymgmt/setWindowSize").toBool()) {
    QPoint pos = settings.value("keymgmt/pos", QPoint(100, 100)).toPoint();
    QSize size = settings.value("keymgmt/size", QSize(900, 600)).toSize();
    qDebug() << "Settings size" << size << "pos" << pos;
    this->setMinimumSize(size);
    this->move(pos);
  } else {
    qDebug() << "Use default min windows size and pos";
    QPoint defaultPoint(100, 100);
    QSize defaultMinSize(900, 600);
    this->setMinimumSize(defaultMinSize);
    this->move(defaultPoint);
    this->settings.setValue("keymgmt/pos", defaultPoint);
    this->settings.setValue("keymgmt/size", defaultMinSize);
    this->settings.setValue("keymgmt/setWindowSize", true);
  }

  setWindowTitle(_("Key Pair Management"));
  mKeyList->addMenuAction(deleteSelectedKeysAct);
  mKeyList->addMenuAction(showKeyDetailsAct);

  connect(this, SIGNAL(signalKeyStatusUpdated()), SignalStation::GetInstance(),
          SIGNAL(KeyDatabaseRefresh()));
}

void KeyMgmt::createActions() {
  openKeyFileAct = new QAction(_("Open"), this);
  openKeyFileAct->setShortcut(QKeySequence(_("Ctrl+O")));
  openKeyFileAct->setToolTip(_("Open Key File"));
  connect(openKeyFileAct, SIGNAL(triggered()), this,
          SLOT(slotImportKeyFromFile()));

  closeAct = new QAction(_("Close"), this);
  closeAct->setShortcut(QKeySequence(_("Ctrl+Q")));
  closeAct->setIcon(QIcon(":exit.png"));
  closeAct->setToolTip(_("Close"));
  connect(closeAct, SIGNAL(triggered()), this, SLOT(close()));

  generateKeyPairAct = new QAction(_("New Keypair"), this);
  generateKeyPairAct->setShortcut(QKeySequence(_("Ctrl+N")));
  generateKeyPairAct->setIcon(QIcon(":key_generate.png"));
  generateKeyPairAct->setToolTip(_("Generate KeyPair"));
  connect(generateKeyPairAct, SIGNAL(triggered()), this,
          SLOT(slotGenerateKeyDialog()));

  generateSubKeyAct = new QAction(_("New Subkey"), this);
  generateSubKeyAct->setShortcut(QKeySequence(_("Ctrl+Shift+N")));
  generateSubKeyAct->setIcon(QIcon(":key_generate.png"));
  generateSubKeyAct->setToolTip(_("Generate Subkey For Selected KeyPair"));
  connect(generateSubKeyAct, SIGNAL(triggered()), this,
          SLOT(slotGenerateSubKey()));

  importKeyFromFileAct = new QAction(_("File"), this);
  importKeyFromFileAct->setIcon(QIcon(":import_key_from_file.png"));
  importKeyFromFileAct->setToolTip(_("Import New Key From File"));
  connect(importKeyFromFileAct, SIGNAL(triggered()), this,
          SLOT(slotImportKeyFromFile()));

  importKeyFromClipboardAct = new QAction(_("Clipboard"), this);
  importKeyFromClipboardAct->setIcon(QIcon(":import_key_from_clipboard.png"));
  importKeyFromClipboardAct->setToolTip(_("Import New Key From Clipboard"));
  connect(importKeyFromClipboardAct, SIGNAL(triggered()), this,
          SLOT(slotImportKeyFromClipboard()));

  importKeyFromKeyServerAct = new QAction(_("Keyserver"), this);
  importKeyFromKeyServerAct->setIcon(QIcon(":import_key_from_server.png"));
  importKeyFromKeyServerAct->setToolTip(_("Import New Key From Keyserver"));
  connect(importKeyFromKeyServerAct, SIGNAL(triggered()), this,
          SLOT(slotImportKeyFromKeyServer()));

  exportKeyToClipboardAct = new QAction(_("Export To Clipboard"), this);
  exportKeyToClipboardAct->setIcon(QIcon(":export_key_to_clipboard.png"));
  exportKeyToClipboardAct->setToolTip(_("Export Selected Key(s) To Clipboard"));
  connect(exportKeyToClipboardAct, SIGNAL(triggered()), this,
          SLOT(slotExportKeyToClipboard()));

  exportKeyToFileAct = new QAction(_("Export To File"), this);
  exportKeyToFileAct->setIcon(QIcon(":export_key_to_file.png"));
  exportKeyToFileAct->setToolTip(_("Export Selected Key(s) To File"));
  connect(exportKeyToFileAct, SIGNAL(triggered()), this,
          SLOT(slotExportKeyToFile()));

  deleteSelectedKeysAct = new QAction(_("Delete Selected Key(s)"), this);
  deleteSelectedKeysAct->setToolTip(_("Delete the Selected keys"));
  connect(deleteSelectedKeysAct, SIGNAL(triggered()), this,
          SLOT(slotDeleteSelectedKeys()));

  deleteCheckedKeysAct = new QAction(_("Delete Checked Key(s)"), this);
  deleteCheckedKeysAct->setToolTip(_("Delete the Checked keys"));
  deleteCheckedKeysAct->setIcon(QIcon(":button_delete.png"));
  connect(deleteCheckedKeysAct, SIGNAL(triggered()), this,
          SLOT(slotDeleteCheckedKeys()));

  showKeyDetailsAct = new QAction(_("Show Key Details"), this);
  showKeyDetailsAct->setToolTip(_("Show Details for this Key"));
  connect(showKeyDetailsAct, SIGNAL(triggered()), this,
          SLOT(slotShowKeyDetails()));
}

void KeyMgmt::createMenus() {
  fileMenu = menuBar()->addMenu(_("File"));
  fileMenu->addAction(openKeyFileAct);
  fileMenu->addAction(closeAct);

  keyMenu = menuBar()->addMenu(_("Key"));
  generateKeyMenu = keyMenu->addMenu(_("Generate Key"));
  generateKeyMenu->addAction(generateKeyPairAct);
  generateKeyMenu->addAction(generateSubKeyAct);

  importKeyMenu = keyMenu->addMenu(_("Import Key"));
  importKeyMenu->addAction(importKeyFromFileAct);
  importKeyMenu->addAction(importKeyFromClipboardAct);
  importKeyMenu->addAction(importKeyFromKeyServerAct);
  keyMenu->addAction(exportKeyToFileAct);
  keyMenu->addAction(exportKeyToClipboardAct);
  keyMenu->addSeparator();
  keyMenu->addAction(deleteCheckedKeysAct);
}

void KeyMgmt::createToolBars() {
  QToolBar* keyToolBar = addToolBar(_("Key"));
  keyToolBar->setObjectName("keytoolbar");

  // add button with popup menu for import
  auto* generateToolButton = new QToolButton(this);
  generateToolButton->setMenu(generateKeyMenu);
  generateToolButton->setPopupMode(QToolButton::InstantPopup);
  generateToolButton->setIcon(QIcon(":key_generate.png"));
  generateToolButton->setText(_("Generate"));
  generateToolButton->setToolTip(_("Generate A New Keypair or Subkey"));
  generateToolButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
  keyToolBar->addWidget(generateToolButton);

  // add button with popup menu for import
  auto* toolButton = new QToolButton(this);
  toolButton->setMenu(importKeyMenu);
  toolButton->setPopupMode(QToolButton::InstantPopup);
  toolButton->setIcon(QIcon(":key_import.png"));
  toolButton->setToolTip(_("Import key"));
  toolButton->setText(_("Import Key"));
  toolButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
  keyToolBar->addWidget(toolButton);

  keyToolBar->addSeparator();
  keyToolBar->addAction(deleteCheckedKeysAct);
  keyToolBar->addSeparator();
  keyToolBar->addAction(exportKeyToFileAct);
  keyToolBar->addAction(exportKeyToClipboardAct);
}

void KeyMgmt::slotImportKeys(const std::string& in_buffer) {
  GpgImportInformation result = GpgKeyImportExportor::GetInstance().ImportKey(
      std::make_unique<ByteArray>(in_buffer));
  emit signalKeyStatusUpdated();
  new KeyImportDetailDialog(result, false, this);
}

void KeyMgmt::slotImportKeyFromFile() {
  QString file_name = QFileDialog::getOpenFileName(
      this, _("Open Key"), QString(),
      QString(_("Key Files")) + " (*.asc *.txt);;" + _("Keyring files") +
          " (*.gpg);;All Files (*)");
  if (!file_name.isNull()) {
    slotImportKeys(read_all_data_in_file(file_name.toStdString()));
  }
}

void KeyMgmt::slotImportKeyFromKeyServer() {
  importDialog = new KeyServerImportDialog(mKeyList, false, this);
  importDialog->show();
}

void KeyMgmt::slotImportKeyFromClipboard() {
  QClipboard* cb = QApplication::clipboard();
  slotImportKeys(cb->text(QClipboard::Clipboard).toUtf8().toStdString());
}

void KeyMgmt::slotDeleteSelectedKeys() {
  deleteKeysWithWarning(mKeyList->getSelected());
}

void KeyMgmt::slotDeleteCheckedKeys() {
  deleteKeysWithWarning(mKeyList->getChecked());
}

void KeyMgmt::deleteKeysWithWarning(KeyIdArgsListPtr key_ids) {
  /**
   * TODO: Different Messages for private/public key, check if
   * more than one selected... compare to seahorse "delete-dialog"
   */

  LOG(INFO) << "KeyMgmt::deleteKeysWithWarning Called";

  if (key_ids->empty()) return;
  QString keynames;
  for (const auto& key_id : *key_ids) {
    auto key = GpgKeyGetter::GetInstance().GetKey(key_id);
    if (!key.good()) continue;
    keynames.append(QString::fromStdString(key.name()));
    keynames.append("<i> &lt;");
    keynames.append(QString::fromStdString(key.email()));
    keynames.append("&gt; </i><br/>");
  }

  int ret = QMessageBox::warning(
      this, _("Deleting Keys"),
      "<b>" +
          QString(
              _("Are you sure that you want to delete the following keys?")) +
          "</b><br/><br/>" + keynames + +"<br/>" +
          _("The action can not be undone."),
      QMessageBox::No | QMessageBox::Yes);

  if (ret == QMessageBox::Yes) {
    GpgKeyOpera::GetInstance().DeleteKeys(std::move(key_ids));
    emit signalKeyStatusUpdated();
  }
}

void KeyMgmt::slotShowKeyDetails() {
  auto keys_selected = mKeyList->getSelected();
  if (keys_selected->empty()) return;

  auto key = GpgKeyGetter::GetInstance().GetKey(keys_selected->front());

  if (!key.good()) {
    QMessageBox::critical(nullptr, _("Error"), _("Key Not Found."));
    return;
  }

  new KeyDetailsDialog(key);
}

void KeyMgmt::slotExportKeyToFile() {
  ByteArrayPtr key_export_data = nullptr;
  auto keys_checked = mKeyList->getChecked();
  if (!GpgKeyImportExportor::GetInstance().ExportKeys(keys_checked,
                                                      key_export_data)) {
    return;
  }
  auto key =
      GpgKeyGetter::GetInstance().GetKey(mKeyList->getSelected()->front());
  if (!key.good()) {
    QMessageBox::critical(nullptr, _("Error"), _("Key Not Found."));
    return;
  }
  QString fileString = QString::fromStdString(key.name() + " " + key.email() +
                                              "(" + key.id() + ")_pub.asc");

  QString file_name = QFileDialog::getSaveFileName(
      this, _("Export Key To File"), fileString,
      QString(_("Key Files")) + " (*.asc *.txt);;All Files (*)");

  write_buffer_to_file(file_name.toStdString(), *key_export_data);

  emit signalStatusBarChanged(QString(_("key(s) exported")));
}

void KeyMgmt::slotExportKeyToClipboard() {
  ByteArrayPtr key_export_data = nullptr;
  auto keys_checked = mKeyList->getChecked();
  if (!GpgKeyImportExportor::GetInstance().ExportKeys(keys_checked,
                                                      key_export_data)) {
    return;
  }
  QApplication::clipboard()->setText(QString::fromStdString(*key_export_data));
}

void KeyMgmt::slotGenerateKeyDialog() {
  auto* keyGenDialog = new KeyGenDialog(this);
  keyGenDialog->show();
}

void KeyMgmt::closeEvent(QCloseEvent* event) { QMainWindow::closeEvent(event); }

void KeyMgmt::slotGenerateSubKey() {
  auto keys_selected = mKeyList->getSelected();
  if (keys_selected->empty()) {
    QMessageBox::information(
        nullptr, _("Invalid Operation"),
        _("Please select one KeyPair before doing this operation."));
    return;
  }
  const auto key = GpgKeyGetter::GetInstance().GetKey(keys_selected->front());
  if (!key.good()) {
    QMessageBox::critical(nullptr, _("Error"), _("Key Not Found."));
    return;
  }
  if (!key.is_private_key()) {
    QMessageBox::critical(nullptr, _("Invalid Operation"),
                          _("If a key pair does not have a private key then "
                            "it will not be able to generate sub-keys."));
    return;
  }

  auto dialog = new SubkeyGenerateDialog(key.id(), this);
  dialog->show();
}

}  // namespace GpgFrontend::UI