• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

IJHack / QtPass / 24124365613

08 Apr 2026 07:54AM UTC coverage: 21.044%. Remained the same
24124365613

push

github

web-flow
Merge pull request #929 from IJHack/fix/clang-format-docstrings

docs: add Doxygen docstrings to source files (clang-formatted)

1109 of 5270 relevant lines covered (21.04%)

7.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/mainwindow.cpp
1
// SPDX-FileCopyrightText: 2016 Anne Jan Brouwer
2
// SPDX-License-Identifier: GPL-3.0-or-later
3
#include "mainwindow.h"
4

5
#ifdef QT_DEBUG
6
#include "debughelper.h"
7
#endif
8

9
#include "configdialog.h"
10
#include "filecontent.h"
11
#include "passworddialog.h"
12
#include "qpushbuttonasqrcode.h"
13
#include "qpushbuttonshowpassword.h"
14
#include "qpushbuttonwithclipboard.h"
15
#include "qtpass.h"
16
#include "qtpasssettings.h"
17
#include "trayicon.h"
18
#include "ui_mainwindow.h"
19
#include "usersdialog.h"
20
#include "util.h"
21
#include <QCloseEvent>
22
#include <QDesktopServices>
23
#include <QDialog>
24
#include <QDirIterator>
25
#include <QFileInfo>
26
#include <QInputDialog>
27
#include <QLabel>
28
#include <QMenu>
29
#include <QMessageBox>
30
#include <QShortcut>
31
#include <QTimer>
32
#include <utility>
33

34
/**
35
 * @brief MainWindow::MainWindow handles all of the main functionality and also
36
 * the main window.
37
 * @param searchText for searching from cli
38
 * @param parent pointer
39
 */
40
MainWindow::MainWindow(const QString &searchText, QWidget *parent)
×
41
    : QMainWindow(parent), ui(new Ui::MainWindow), keygen(nullptr),
×
42
      tray(nullptr) {
×
43
#ifdef __APPLE__
44
  // extra treatment for mac os
45
  // see http://doc.qt.io/qt-5/qkeysequence.html#qt_set_sequence_auto_mnemonic
46
  qt_set_sequence_auto_mnemonic(true);
47
#endif
48
  ui->setupUi(this);
×
49

50
  m_qtPass = new QtPass(this);
×
51

52
  // register shortcut ctrl/cmd + Q to close the main window
53
  new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_Q), this, SLOT(close()));
×
54
  // register shortcut ctrl/cmd + C to copy the currently selected password
55
  new QShortcut(QKeySequence(QKeySequence::StandardKey::Copy), this,
×
56
                SLOT(copyPasswordFromTreeview()));
×
57

58
  model.setNameFilters(QStringList() << "*.gpg");
×
59
  model.setNameFilterDisables(false);
×
60

61
  /*
62
   * I added this to solve Windows bug but now on GNU/Linux the main folder,
63
   * if hidden, disappear
64
   *
65
   * model.setFilter(QDir::NoDot);
66
   */
67

68
  QString passStore = QtPassSettings::getPassStore(Util::findPasswordStore());
×
69

70
  QModelIndex rootDir = model.setRootPath(passStore);
×
71
  model.fetchMore(rootDir);
×
72

73
  proxyModel.setModelAndStore(&model, passStore);
×
74
  selectionModel.reset(new QItemSelectionModel(&proxyModel));
×
75

76
  ui->treeView->setModel(&proxyModel);
×
77
  ui->treeView->setRootIndex(proxyModel.mapFromSource(rootDir));
×
78
  ui->treeView->setColumnHidden(1, true);
×
79
  ui->treeView->setColumnHidden(2, true);
×
80
  ui->treeView->setColumnHidden(3, true);
×
81
  ui->treeView->setHeaderHidden(true);
×
82
  ui->treeView->setIndentation(15);
×
83
  ui->treeView->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
×
84
  ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu);
×
85
  ui->treeView->header()->setSectionResizeMode(0, QHeaderView::Stretch);
×
86
  ui->treeView->sortByColumn(0, Qt::AscendingOrder);
×
87
  connect(ui->treeView, &QWidget::customContextMenuRequested, this,
×
88
          &MainWindow::showContextMenu);
×
89
  connect(ui->treeView, &DeselectableTreeView::emptyClicked, this,
×
90
          &MainWindow::deselect);
×
91

92
  if (QtPassSettings::isUseMonospace()) {
×
93
    QFont monospace("Monospace");
×
94
    monospace.setStyleHint(QFont::Monospace);
×
95
    ui->textBrowser->setFont(monospace);
×
96
  }
×
97
  if (QtPassSettings::isNoLineWrapping()) {
×
98
    ui->textBrowser->setLineWrapMode(QTextBrowser::NoWrap);
×
99
  }
100
  ui->textBrowser->setOpenExternalLinks(true);
×
101
  ui->textBrowser->setContextMenuPolicy(Qt::CustomContextMenu);
×
102
  connect(ui->textBrowser, &QWidget::customContextMenuRequested, this,
×
103
          &MainWindow::showBrowserContextMenu);
×
104

105
  updateProfileBox();
×
106

107
  QtPassSettings::getPass()->updateEnv();
×
108
  clearPanelTimer.setInterval(MS_PER_SECOND *
×
109
                              QtPassSettings::getAutoclearPanelSeconds());
×
110
  clearPanelTimer.setSingleShot(true);
×
111
  connect(&clearPanelTimer, &QTimer::timeout, this, [this]() { clearPanel(); });
×
112

113
  searchTimer.setInterval(350);
×
114
  searchTimer.setSingleShot(true);
×
115

116
  connect(&searchTimer, &QTimer::timeout, this, &MainWindow::onTimeoutSearch);
×
117

118
  initToolBarButtons();
×
119
  initStatusBar();
×
120

121
  ui->lineEdit->setClearButtonEnabled(true);
×
122

123
  setUiElementsEnabled(true);
×
124

125
  QTimer::singleShot(10, this, SLOT(focusInput()));
126

127
  ui->lineEdit->setText(searchText);
×
128

129
  if (!m_qtPass->init()) {
×
130
    // no working config so this should just quit
131
    QApplication::quit();
×
132
  }
133
}
×
134

135
MainWindow::~MainWindow() { delete m_qtPass; }
×
136

137
/**
138
 * @brief MainWindow::focusInput selects any text (if applicable) in the search
139
 * box and sets focus to it. Allows for easy searching, called at application
140
 * start and when receiving empty message in MainWindow::messageAvailable when
141
 * compiled with SINGLE_APP=1 (default).
142
 */
143
void MainWindow::focusInput() {
×
144
  ui->lineEdit->selectAll();
×
145
  ui->lineEdit->setFocus();
×
146
}
×
147

148
/**
149
 * @brief MainWindow::changeEvent sets focus to the search box
150
 * @param event
151
 */
152
void MainWindow::changeEvent(QEvent *event) {
×
153
  QWidget::changeEvent(event);
×
154
  if (event->type() == QEvent::ActivationChange) {
×
155
    if (isActiveWindow()) {
×
156
      focusInput();
×
157
    }
158
  }
159
}
×
160

161
/**
162
 * @brief MainWindow::initToolBarButtons init main ToolBar and connect actions
163
 */
164
void MainWindow::initToolBarButtons() {
×
165
  connect(ui->actionAddPassword, &QAction::triggered, this,
×
166
          &MainWindow::addPassword);
×
167
  connect(ui->actionAddFolder, &QAction::triggered, this,
×
168
          &MainWindow::addFolder);
×
169
  connect(ui->actionEdit, &QAction::triggered, this, &MainWindow::onEdit);
×
170
  connect(ui->actionDelete, &QAction::triggered, this, &MainWindow::onDelete);
×
171
  connect(ui->actionPush, &QAction::triggered, this, &MainWindow::onPush);
×
172
  connect(ui->actionUpdate, &QAction::triggered, this, &MainWindow::onUpdate);
×
173
  connect(ui->actionUsers, &QAction::triggered, this, &MainWindow::onUsers);
×
174
  connect(ui->actionConfig, &QAction::triggered, this, &MainWindow::onConfig);
×
175
  connect(ui->actionOtp, &QAction::triggered, this, &MainWindow::onOtp);
×
176

177
  ui->actionAddPassword->setIcon(
×
178
      QIcon::fromTheme("document-new", QIcon(":/icons/document-new.svg")));
×
179
  ui->actionAddFolder->setIcon(
×
180
      QIcon::fromTheme("folder-new", QIcon(":/icons/folder-new.svg")));
×
181
  ui->actionEdit->setIcon(QIcon::fromTheme(
×
182
      "document-properties", QIcon(":/icons/document-properties.svg")));
×
183
  ui->actionDelete->setIcon(
×
184
      QIcon::fromTheme("edit-delete", QIcon(":/icons/edit-delete.svg")));
×
185
  ui->actionPush->setIcon(
×
186
      QIcon::fromTheme("go-up", QIcon(":/icons/go-top.svg")));
×
187
  ui->actionUpdate->setIcon(
×
188
      QIcon::fromTheme("go-down", QIcon(":/icons/go-bottom.svg")));
×
189
  ui->actionUsers->setIcon(QIcon::fromTheme(
×
190
      "x-office-address-book", QIcon(":/icons/x-office-address-book.svg")));
×
191
  ui->actionConfig->setIcon(QIcon::fromTheme(
×
192
      "applications-system", QIcon(":/icons/applications-system.svg")));
×
193
}
×
194

195
/**
196
 * @brief MainWindow::initStatusBar init statusBar with default message and logo
197
 */
198
void MainWindow::initStatusBar() {
×
199
  ui->statusBar->showMessage(tr("Welcome to QtPass %1").arg(VERSION), 2000);
×
200

201
  QPixmap logo = QPixmap::fromImage(QImage(":/artwork/icon.svg"))
×
202
                     .scaledToHeight(statusBar()->height());
×
203
  auto *logoApp = new QLabel(statusBar());
×
204
  logoApp->setPixmap(logo);
×
205
  statusBar()->addPermanentWidget(logoApp);
×
206
}
×
207

208
auto MainWindow::getCurrentTreeViewIndex() -> QModelIndex {
×
209
  return ui->treeView->currentIndex();
×
210
}
211

212
void MainWindow::cleanKeygenDialog() {
×
213
  this->keygen->close();
×
214
  this->keygen = nullptr;
×
215
}
×
216

217
/**
218
 * @brief Displays the given text in the main window text browser, optionally
219
 * marking it as an error and/or rendering it as HTML.
220
 * @example
221
 * MainWindow window;
222
 * window.flashText("Operation completed.", false, false);
223
 *
224
 * @param const QString &text - The text content to display.
225
 * @param const bool isError - If true, sets the text color to red before
226
 * displaying the text.
227
 * @param const bool isHtml - If true, treats the text as HTML and appends it to
228
 * the existing HTML content.
229
 * @return void - No return value.
230
 */
231
void MainWindow::flashText(const QString &text, const bool isError,
×
232
                           const bool isHtml) {
233
  if (isError) {
×
234
    ui->textBrowser->setTextColor(Qt::red);
×
235
  }
236

237
  if (isHtml) {
×
238
    QString _text = text;
239
    if (!ui->textBrowser->toPlainText().isEmpty()) {
×
240
      _text = ui->textBrowser->toHtml() + _text;
×
241
    }
242
    ui->textBrowser->setHtml(_text);
×
243
  } else {
244
    ui->textBrowser->setText(text);
×
245
    ui->textBrowser->setTextColor(Qt::black);
×
246
  }
247
}
×
248

249
/**
250
 * @brief MainWindow::config pops up the configuration screen and handles all
251
 * inter-window communication
252
 */
253
void MainWindow::applyTextBrowserSettings() {
×
254
  if (QtPassSettings::isUseMonospace()) {
×
255
    QFont monospace("Monospace");
×
256
    monospace.setStyleHint(QFont::Monospace);
×
257
    ui->textBrowser->setFont(monospace);
×
258
  } else {
×
259
    ui->textBrowser->setFont(QFont());
×
260
  }
261

262
  if (QtPassSettings::isNoLineWrapping()) {
×
263
    ui->textBrowser->setLineWrapMode(QTextBrowser::NoWrap);
×
264
  } else {
265
    ui->textBrowser->setLineWrapMode(QTextBrowser::WidgetWidth);
×
266
  }
267
}
×
268

269
void MainWindow::applyWindowFlagsSettings() {
×
270
  if (QtPassSettings::isAlwaysOnTop()) {
×
271
    Qt::WindowFlags flags = windowFlags();
272
    this->setWindowFlags(flags | Qt::WindowStaysOnTopHint);
×
273
  } else {
274
    this->setWindowFlags(Qt::Window);
×
275
  }
276
  this->show();
×
277
}
×
278

279
/**
280
 * @brief Opens and processes the application configuration dialog, then applies
281
 * any accepted settings.
282
 * @example
283
 * config();
284
 *
285
 * @return void - This function does not return a value.
286
 */
287
void MainWindow::config() {
×
288
  QScopedPointer<ConfigDialog> d(new ConfigDialog(this));
×
289
  d->setModal(true);
×
290
  // Automatically default to pass if it's available
291
  if (m_qtPass->isFreshStart() &&
×
292
      QFile(QtPassSettings::getPassExecutable()).exists()) {
×
293
    QtPassSettings::setUsePass(true);
×
294
  }
295

296
  if (m_qtPass->isFreshStart()) {
×
297
    d->wizard(); //  does shit
×
298
  }
299
  if (d->exec()) {
×
300
    if (d->result() == QDialog::Accepted) {
×
301
      applyTextBrowserSettings();
×
302
      applyWindowFlagsSettings();
×
303

304
      updateProfileBox();
×
305
      ui->treeView->setRootIndex(proxyModel.mapFromSource(
×
306
          model.setRootPath(QtPassSettings::getPassStore())));
×
307

308
      if (m_qtPass->isFreshStart() && !Util::configIsValid()) {
×
309
        config();
×
310
      }
311
      QtPassSettings::getPass()->updateEnv();
×
312
      clearPanelTimer.setInterval(MS_PER_SECOND *
×
313
                                  QtPassSettings::getAutoclearPanelSeconds());
×
314
      m_qtPass->setClipboardTimer();
×
315

316
      updateGitButtonVisibility();
×
317
      updateOtpButtonVisibility();
×
318
      if (QtPassSettings::isUseTrayIcon() && tray == nullptr) {
×
319
        initTrayIcon();
×
320
      } else if (!QtPassSettings::isUseTrayIcon() && tray != nullptr) {
×
321
        destroyTrayIcon();
×
322
      }
323
    }
324

325
    m_qtPass->setFreshStart(false);
×
326
  }
327
}
×
328

329
/**
330
 * @brief MainWindow::onUpdate do a git pull
331
 */
332
void MainWindow::onUpdate(bool block) {
×
333
  ui->statusBar->showMessage(tr("Updating password-store"), 2000);
×
334
  if (block) {
×
335
    QtPassSettings::getPass()->GitPull_b();
×
336
  } else {
337
    QtPassSettings::getPass()->GitPull();
×
338
  }
339
}
×
340

341
/**
342
 * @brief MainWindow::onPush do a git push
343
 */
344
void MainWindow::onPush() {
×
345
  if (QtPassSettings::isUseGit()) {
×
346
    ui->statusBar->showMessage(tr("Updating password-store"), 2000);
×
347
    QtPassSettings::getPass()->GitPush();
×
348
  }
349
}
×
350

351
/**
352
 * @brief MainWindow::getFile get the selected file path
353
 * @param index
354
 * @param forPass returns relative path without '.gpg' extension
355
 * @return path
356
 * @return
357
 */
358
auto MainWindow::getFile(const QModelIndex &index, bool forPass) -> QString {
×
359
  if (!index.isValid() ||
×
360
      !model.fileInfo(proxyModel.mapToSource(index)).isFile()) {
×
361
    return {};
362
  }
363
  QString filePath = model.filePath(proxyModel.mapToSource(index));
×
364
  if (forPass) {
×
365
    filePath = QDir(QtPassSettings::getPassStore()).relativeFilePath(filePath);
×
366
    filePath.replace(Util::endsWithGpg(), "");
×
367
  }
368
  return filePath;
369
}
370

371
/**
372
 * @brief MainWindow::on_treeView_clicked read the selected password file
373
 * @param index
374
 */
375
void MainWindow::on_treeView_clicked(const QModelIndex &index) {
×
376
  bool cleared = ui->treeView->currentIndex().flags() == Qt::NoItemFlags;
×
377
  currentDir =
378
      Util::getDir(ui->treeView->currentIndex(), false, model, proxyModel);
×
379
  // Clear any previously cached clipped text before showing new password
380
  m_qtPass->clearClippedText();
×
381
  QString file = getFile(index, true);
×
382
  ui->passwordName->setText(getFile(index, true));
×
383
  if (!file.isEmpty() && !cleared) {
×
384
    QtPassSettings::getPass()->Show(file);
×
385
  } else {
386
    clearPanel(false);
×
387
    ui->actionEdit->setEnabled(false);
×
388
    ui->actionDelete->setEnabled(true);
×
389
  }
390
}
×
391

392
/**
393
 * @brief MainWindow::on_treeView_doubleClicked when doubleclicked on
394
 * TreeViewItem, open the edit Window
395
 * @param index
396
 */
397
void MainWindow::on_treeView_doubleClicked(const QModelIndex &index) {
×
398
  QFileInfo fileOrFolder =
399
      model.fileInfo(proxyModel.mapToSource(ui->treeView->currentIndex()));
×
400

401
  if (fileOrFolder.isFile()) {
×
402
    editPassword(getFile(index, true));
×
403
  }
404
}
×
405

406
/**
407
 * @brief MainWindow::deselect clear the selection, password and copy buffer
408
 */
409
void MainWindow::deselect() {
×
410
  currentDir = "";
×
411
  m_qtPass->clearClipboard();
×
412
  ui->treeView->clearSelection();
×
413
  ui->actionEdit->setEnabled(false);
×
414
  ui->actionDelete->setEnabled(false);
×
415
  ui->passwordName->setText("");
×
416
  clearPanel(false);
×
417
}
×
418

419
void MainWindow::executeWrapperStarted() {
×
420
  clearTemplateWidgets();
×
421
  ui->textBrowser->clear();
×
422
  setUiElementsEnabled(false);
×
423
  clearPanelTimer.stop();
×
424
}
×
425

426
/**
427
 * @brief Handles displaying parsed password entry content in the main window.
428
 * @example
429
 * void result = MainWindow::passShowHandler(p_output);
430
 * // Updates the UI with parsed fields and emits
431
 * passShowHandlerFinished(output)
432
 *
433
 * @param p_output - The raw output text containing the password entry data.
434
 * @return void - This function does not return a value.
435
 */
436
void MainWindow::passShowHandler(const QString &p_output) {
×
437
  QStringList templ = QtPassSettings::isUseTemplate()
×
438
                          ? QtPassSettings::getPassTemplate().split("\n")
×
439
                          : QStringList();
×
440
  bool allFields =
441
      QtPassSettings::isUseTemplate() && QtPassSettings::isTemplateAllFields();
×
442
  FileContent fileContent = FileContent::parse(p_output, templ, allFields);
×
443
  QString output = p_output;
444
  QString password = fileContent.getPassword();
×
445

446
  // set clipped text
447
  m_qtPass->setClippedText(password, p_output);
×
448

449
  // first clear the current view:
450
  clearTemplateWidgets();
×
451

452
  // show what is needed:
453
  if (QtPassSettings::isHideContent()) {
×
454
    output = "***" + tr("Content hidden") + "***";
×
455
  } else if (!QtPassSettings::isDisplayAsIs()) {
×
456
    if (!password.isEmpty()) {
×
457
      // set the password, it is hidden if needed in addToGridLayout
458
      addToGridLayout(0, tr("Password"), password);
×
459
    }
460

461
    NamedValues namedValues = fileContent.getNamedValues();
×
462
    for (int j = 0; j < namedValues.length(); ++j) {
×
463
      const NamedValue &nv = namedValues.at(j);
464
      addToGridLayout(j + 1, nv.name, nv.value);
×
465
    }
466
    if (ui->gridLayout->count() == 0) {
×
467
      ui->verticalLayoutPassword->setSpacing(0);
×
468
    } else {
469
      ui->verticalLayoutPassword->setSpacing(6);
×
470
    }
471

472
    output = fileContent.getRemainingDataForDisplay();
×
473
  }
474

475
  if (QtPassSettings::isUseAutoclearPanel()) {
×
476
    clearPanelTimer.start();
×
477
  }
478

479
  emit passShowHandlerFinished(output);
×
480
  setUiElementsEnabled(true);
×
481
}
×
482

483
/**
484
 * @brief Handles the OTP output by displaying it, copying it to the clipboard,
485
 * and updating the UI state.
486
 * @example
487
 * void MainWindow::passOtpHandler(const QString &p_output);
488
 *
489
 * @param const QString &p_output - The OTP code text to process; if empty, an
490
 * error message is shown instead.
491
 * @return void - This function does not return a value.
492
 */
493
void MainWindow::passOtpHandler(const QString &p_output) {
×
494
  if (!p_output.isEmpty()) {
×
495
    addToGridLayout(ui->gridLayout->count() + 1, tr("OTP Code"), p_output);
×
496
    m_qtPass->copyTextToClipboard(p_output);
×
497
    showStatusMessage(tr("OTP code copied to clipboard"));
×
498
  } else {
499
    flashText(tr("No OTP code found in this password entry"), true);
×
500
  }
501
  if (QtPassSettings::isUseAutoclearPanel()) {
×
502
    clearPanelTimer.start();
×
503
  }
504
  setUiElementsEnabled(true);
×
505
}
×
506

507
/**
508
 * @brief MainWindow::clearPanel hide the information from shoulder surfers
509
 */
510
void MainWindow::clearPanel(bool notify) {
×
511
  while (ui->gridLayout->count() > 0) {
×
512
    QLayoutItem *item = ui->gridLayout->takeAt(0);
×
513
    delete item->widget();
×
514
    delete item;
×
515
  }
516
  if (notify) {
×
517
    QString output = "***" + tr("Password and Content hidden") + "***";
×
518
    ui->textBrowser->setHtml(output);
×
519
  } else {
520
    ui->textBrowser->setHtml("");
×
521
  }
522
}
×
523

524
/**
525
 * @brief MainWindow::setUiElementsEnabled enable or disable the relevant UI
526
 * elements
527
 * @param state
528
 */
529
void MainWindow::setUiElementsEnabled(bool state) {
×
530
  ui->treeView->setEnabled(state);
×
531
  ui->lineEdit->setEnabled(state);
×
532
  ui->lineEdit->installEventFilter(this);
×
533
  ui->actionAddPassword->setEnabled(state);
×
534
  ui->actionAddFolder->setEnabled(state);
×
535
  ui->actionUsers->setEnabled(state);
×
536
  ui->actionConfig->setEnabled(state);
×
537
  // is a file selected?
538
  state &= ui->treeView->currentIndex().isValid();
×
539
  ui->actionDelete->setEnabled(state);
×
540
  ui->actionEdit->setEnabled(state);
×
541
  updateGitButtonVisibility();
×
542
  updateOtpButtonVisibility();
×
543
}
×
544

545
/**
546
 * @brief Restores the main window geometry, state, position, size, and
547
 * tray/icon settings from saved application settings.
548
 * @example
549
 * MainWindow window;
550
 * window.restoreWindow();
551
 *
552
 * @return void - This function does not return a value.
553
 */
554
void MainWindow::restoreWindow() {
×
555
  QByteArray geometry = QtPassSettings::getGeometry(saveGeometry());
×
556
  restoreGeometry(geometry);
×
557
  QByteArray savestate = QtPassSettings::getSavestate(saveState());
×
558
  restoreState(savestate);
×
559
  QPoint position = QtPassSettings::getPos(pos());
×
560
  move(position);
×
561
  QSize newSize = QtPassSettings::getSize(size());
×
562
  resize(newSize);
×
563
  if (QtPassSettings::isMaximized(isMaximized())) {
×
564
    showMaximized();
×
565
  }
566

567
  if (QtPassSettings::isAlwaysOnTop()) {
×
568
    Qt::WindowFlags flags = windowFlags();
569
    setWindowFlags(flags | Qt::WindowStaysOnTopHint);
×
570
    show();
×
571
  }
572

573
  if (QtPassSettings::isUseTrayIcon() && tray == nullptr) {
×
574
    initTrayIcon();
×
575
    if (QtPassSettings::isStartMinimized()) {
×
576
      // since we are still in constructor, can't directly hide
577
      QTimer::singleShot(10, this, SLOT(hide()));
×
578
    }
579
  } else if (!QtPassSettings::isUseTrayIcon() && tray != nullptr) {
×
580
    destroyTrayIcon();
×
581
  }
582
}
×
583

584
/**
585
 * @brief MainWindow::on_configButton_clicked run Mainwindow::config
586
 */
587
void MainWindow::onConfig() { config(); }
×
588

589
/**
590
 * @brief Executes when the string in the search box changes, collapses the
591
 * TreeView
592
 * @param arg1
593
 */
594
void MainWindow::on_lineEdit_textChanged(const QString &arg1) {
×
595
  ui->statusBar->showMessage(tr("Looking for: %1").arg(arg1), 1000);
×
596
  ui->treeView->expandAll();
×
597
  clearPanel(false);
×
598
  ui->passwordName->setText("");
×
599
  ui->actionEdit->setEnabled(false);
×
600
  ui->actionDelete->setEnabled(false);
×
601
  searchTimer.start();
×
602
}
×
603

604
/**
605
 * @brief MainWindow::onTimeoutSearch Fired when search is finished or too much
606
 * time from two keypresses is elapsed
607
 */
608
void MainWindow::onTimeoutSearch() {
×
609
  QString query = ui->lineEdit->text();
×
610

611
  if (query.isEmpty()) {
×
612
    ui->treeView->collapseAll();
×
613
    deselect();
×
614
  }
615

616
  query.replace(QStringLiteral(" "), ".*");
×
617
  QRegularExpression regExp(query, QRegularExpression::CaseInsensitiveOption);
×
618
  proxyModel.setFilterRegularExpression(regExp);
×
619
  ui->treeView->setRootIndex(proxyModel.mapFromSource(
×
620
      model.setRootPath(QtPassSettings::getPassStore())));
×
621

622
  if (proxyModel.rowCount() > 0 && !query.isEmpty()) {
×
623
    selectFirstFile();
×
624
  } else {
625
    ui->actionEdit->setEnabled(false);
×
626
    ui->actionDelete->setEnabled(false);
×
627
  }
628
}
×
629

630
/**
631
 * @brief MainWindow::on_lineEdit_returnPressed get searching
632
 *
633
 * Select the first possible file in the tree
634
 */
635
void MainWindow::on_lineEdit_returnPressed() {
×
636
#ifdef QT_DEBUG
637
  dbg() << "on_lineEdit_returnPressed" << proxyModel.rowCount();
638
#endif
639

640
  if (proxyModel.rowCount() > 0) {
×
641
    selectFirstFile();
×
642
    on_treeView_clicked(ui->treeView->currentIndex());
×
643
  }
644
}
×
645

646
/**
647
 * @brief MainWindow::selectFirstFile select the first possible file in the
648
 * tree
649
 */
650
void MainWindow::selectFirstFile() {
×
651
  QModelIndex index = proxyModel.mapFromSource(
×
652
      model.setRootPath(QtPassSettings::getPassStore()));
×
653
  index = firstFile(index);
×
654
  ui->treeView->setCurrentIndex(index);
×
655
}
×
656

657
/**
658
 * @brief MainWindow::firstFile return location of first possible file
659
 * @param parentIndex
660
 * @return QModelIndex
661
 */
662
auto MainWindow::firstFile(QModelIndex parentIndex) -> QModelIndex {
×
663
  QModelIndex index = parentIndex;
×
664
  int numRows = proxyModel.rowCount(parentIndex);
×
665
  for (int row = 0; row < numRows; ++row) {
×
666
    index = proxyModel.index(row, 0, parentIndex);
×
667
    if (model.fileInfo(proxyModel.mapToSource(index)).isFile()) {
×
668
      return index;
×
669
    }
670
    if (proxyModel.hasChildren(index)) {
×
671
      return firstFile(index);
×
672
    }
673
  }
674
  return index;
×
675
}
676

677
/**
678
 * @brief MainWindow::setPassword open passworddialog
679
 * @param file which pgp file
680
 * @param isNew insert (not update)
681
 */
682
void MainWindow::setPassword(const QString &file, bool isNew) {
×
683
  PasswordDialog d(file, isNew, this);
×
684

685
  if (!d.exec()) {
×
686
    ui->treeView->setFocus();
×
687
  }
688
}
×
689

690
/**
691
 * @brief MainWindow::addPassword add a new password by showing a
692
 * number of dialogs.
693
 */
694
void MainWindow::addPassword() {
×
695
  bool ok;
696
  QString dir =
697
      Util::getDir(ui->treeView->currentIndex(), true, model, proxyModel);
×
698
  QString file =
699
      QInputDialog::getText(this, tr("New file"),
×
700
                            tr("New password file: \n(Will be placed in %1 )")
×
701
                                .arg(QtPassSettings::getPassStore() +
×
702
                                     Util::getDir(ui->treeView->currentIndex(),
×
703
                                                  true, model, proxyModel)),
704
                            QLineEdit::Normal, "", &ok);
×
705
  if (!ok || file.isEmpty()) {
×
706
    return;
707
  }
708
  file = dir + file;
×
709
  setPassword(file);
×
710
}
711

712
/**
713
 * @brief MainWindow::onDelete remove password, if you are
714
 * sure.
715
 */
716
void MainWindow::onDelete() {
×
717
  QModelIndex currentIndex = ui->treeView->currentIndex();
×
718
  if (!currentIndex.isValid()) {
719
    // This fixes https://github.com/IJHack/QtPass/issues/556
720
    // Otherwise the entire password directory would be deleted if
721
    // nothing is selected in the tree view.
722
    return;
×
723
  }
724

725
  QFileInfo fileOrFolder =
726
      model.fileInfo(proxyModel.mapToSource(ui->treeView->currentIndex()));
×
727
  QString file = "";
×
728
  bool isDir = false;
729

730
  if (fileOrFolder.isFile()) {
×
731
    file = getFile(ui->treeView->currentIndex(), true);
×
732
  } else {
733
    file = Util::getDir(ui->treeView->currentIndex(), true, model, proxyModel);
×
734
    isDir = true;
735
  }
736

737
  QString dirMessage = tr(" and the whole content?");
738
  if (isDir) {
×
739
    QDirIterator it(model.rootPath() + QDir::separator() + file,
×
740
                    QDirIterator::Subdirectories);
×
741
    bool okDir = true;
742
    while (it.hasNext() && okDir) {
×
743
      it.next();
×
744
      if (QFileInfo(it.filePath()).isFile()) {
×
745
        if (QFileInfo(it.filePath()).suffix() != "gpg") {
×
746
          okDir = false;
747
          dirMessage = tr(" and the whole content? <br><strong>Attention: "
×
748
                          "there are unexpected files in the given folder, "
749
                          "check them before continue.</strong>");
750
        }
751
      }
752
    }
753
  }
×
754

755
  if (QMessageBox::question(
×
756
          this, isDir ? tr("Delete folder?") : tr("Delete password?"),
×
757
          tr("Are you sure you want to delete %1%2?")
×
758
              .arg(QDir::separator() + file, isDir ? dirMessage : "?"),
×
759
          QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes) {
760
    return;
761
  }
762

763
  QtPassSettings::getPass()->Remove(file, isDir);
×
764
}
×
765

766
/**
767
 * @brief MainWindow::onOTP try and generate (selected) OTP code.
768
 */
769
void MainWindow::onOtp() {
×
770
  QString file = getFile(ui->treeView->currentIndex(), true);
×
771
  if (!file.isEmpty()) {
×
772
    if (QtPassSettings::isUseOtp()) {
×
773
      setUiElementsEnabled(false);
×
774
      QtPassSettings::getPass()->OtpGenerate(file);
×
775
    }
776
  } else {
777
    flashText(tr("No password selected for OTP generation"), true);
×
778
  }
779
}
×
780

781
/**
782
 * @brief MainWindow::onEdit try and edit (selected) password.
783
 */
784
void MainWindow::onEdit() {
×
785
  QString file = getFile(ui->treeView->currentIndex(), true);
×
786
  editPassword(file);
×
787
}
×
788

789
/**
790
 * @brief MainWindow::userDialog see MainWindow::onUsers()
791
 * @param dir folder to edit users for.
792
 */
793
void MainWindow::userDialog(const QString &dir) {
×
794
  if (!dir.isEmpty()) {
×
795
    currentDir = dir;
×
796
  }
797
  onUsers();
×
798
}
×
799

800
/**
801
 * @brief MainWindow::onUsers edit users for the current
802
 * folder,
803
 * gets lists and opens UserDialog.
804
 */
805
void MainWindow::onUsers() {
×
806
  QString dir =
807
      currentDir.isEmpty()
808
          ? Util::getDir(ui->treeView->currentIndex(), false, model, proxyModel)
×
809
          : currentDir;
×
810

811
  UsersDialog d(dir, this);
×
812
  if (!d.exec()) {
×
813
    ui->treeView->setFocus();
×
814
  }
815
}
×
816

817
/**
818
 * @brief MainWindow::messageAvailable we have some text/message/search to do.
819
 * @param message
820
 */
821
void MainWindow::messageAvailable(const QString &message) {
×
822
  if (message.isEmpty()) {
×
823
    focusInput();
×
824
  } else {
825
    ui->treeView->expandAll();
×
826
    ui->lineEdit->setText(message);
×
827
    on_lineEdit_returnPressed();
×
828
  }
829
  show();
×
830
  raise();
×
831
}
×
832

833
/**
834
 * @brief MainWindow::generateKeyPair internal gpg keypair generator . .
835
 * @param batch
836
 * @param keygenWindow
837
 */
838
void MainWindow::generateKeyPair(const QString &batch, QDialog *keygenWindow) {
×
839
  keygen = keygenWindow;
×
840
  emit generateGPGKeyPair(batch);
×
841
}
×
842

843
/**
844
 * @brief MainWindow::updateProfileBox update the list of profiles, optionally
845
 * select a more appropriate one to view too
846
 */
847
void MainWindow::updateProfileBox() {
×
848
  QHash<QString, QHash<QString, QString>> profiles =
849
      QtPassSettings::getProfiles();
×
850

851
  if (profiles.isEmpty()) {
852
    ui->profileWidget->hide();
×
853
  } else {
854
    ui->profileWidget->show();
×
855
    ui->profileBox->setEnabled(profiles.size() > 1);
×
856
    ui->profileBox->clear();
×
857
    QHashIterator<QString, QHash<QString, QString>> i(profiles);
×
858
    while (i.hasNext()) {
×
859
      i.next();
860
      if (!i.key().isEmpty()) {
×
861
        ui->profileBox->addItem(i.key());
×
862
      }
863
    }
864
    ui->profileBox->model()->sort(0);
×
865
  }
866
  int index = ui->profileBox->findText(QtPassSettings::getProfile());
×
867
  if (index != -1) { //  -1 for not found
×
868
    ui->profileBox->setCurrentIndex(index);
×
869
  }
870
}
×
871

872
/**
873
 * @brief MainWindow::on_profileBox_currentIndexChanged make sure we show the
874
 * correct "profile"
875
 * @param name
876
 */
877
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
878
void MainWindow::on_profileBox_currentIndexChanged(QString name) {
879
#else
880
/**
881
 * @brief Handles changes to the selected profile in the profile combo box.
882
 * @details Ignores the event during a fresh start or when the selected profile
883
 * matches the current profile. Otherwise, it clears the password field, updates
884
 * the active profile and related settings, refreshes the environment, and
885
 * resets the tree view and action states to reflect the newly selected profile.
886
 *
887
 * @param name - The newly selected profile name.
888
 * @return void - This function does not return a value.
889
 *
890
 */
891
void MainWindow::on_profileBox_currentTextChanged(const QString &name) {
×
892
#endif
893
  if (m_qtPass->isFreshStart() || name == QtPassSettings::getProfile()) {
×
894
    return;
895
  }
896

897
  ui->lineEdit->clear();
×
898

899
  QtPassSettings::setProfile(name);
×
900

901
  QtPassSettings::setPassStore(
×
902
      QtPassSettings::getProfiles().value(name).value("path"));
×
903
  QtPassSettings::setPassSigningKey(
×
904
      QtPassSettings::getProfiles().value(name).value("signingKey"));
×
905
  ui->statusBar->showMessage(tr("Profile changed to %1").arg(name), 2000);
×
906

907
  QtPassSettings::getPass()->updateEnv();
×
908

909
  ui->treeView->selectionModel()->clear();
×
910
  ui->treeView->setRootIndex(proxyModel.mapFromSource(
×
911
      model.setRootPath(QtPassSettings::getPassStore())));
×
912

913
  ui->actionEdit->setEnabled(false);
×
914
  ui->actionDelete->setEnabled(false);
×
915
}
916

917
/**
918
 * @brief MainWindow::initTrayIcon show a nice tray icon on systems that
919
 * support
920
 * it
921
 */
922
void MainWindow::initTrayIcon() {
×
923
  this->tray = new TrayIcon(this);
×
924
  // Setup tray icon
925

926
  if (tray == nullptr) {
927
#ifdef QT_DEBUG
928
    dbg() << "Allocating tray icon failed.";
929
#endif
930
  }
931

932
  if (!tray->getIsAllocated()) {
×
933
    destroyTrayIcon();
×
934
  }
935
}
×
936

937
/**
938
 * @brief MainWindow::destroyTrayIcon remove that pesky tray icon
939
 */
940
void MainWindow::destroyTrayIcon() {
×
941
  delete this->tray;
×
942
  tray = nullptr;
×
943
}
×
944

945
/**
946
 * @brief MainWindow::closeEvent hide or quit
947
 * @param event
948
 */
949
void MainWindow::closeEvent(QCloseEvent *event) {
×
950
  if (QtPassSettings::isHideOnClose()) {
×
951
    this->hide();
×
952
    event->ignore();
953
  } else {
954
    m_qtPass->clearClipboard();
×
955

956
    QtPassSettings::setGeometry(saveGeometry());
×
957
    QtPassSettings::setSavestate(saveState());
×
958
    QtPassSettings::setMaximized(isMaximized());
×
959
    if (!isMaximized()) {
×
960
      QtPassSettings::setPos(pos());
×
961
      QtPassSettings::setSize(size());
×
962
    }
963
    event->accept();
964
  }
965
}
×
966

967
/**
968
 * @brief MainWindow::eventFilter filter out some events and focus the
969
 * treeview
970
 * @param obj
971
 * @param event
972
 * @return
973
 */
974
auto MainWindow::eventFilter(QObject *obj, QEvent *event) -> bool {
×
975
  if (obj == ui->lineEdit && event->type() == QEvent::KeyPress) {
×
976
    auto *key = dynamic_cast<QKeyEvent *>(event);
×
977
    if (key != nullptr && key->key() == Qt::Key_Down) {
×
978
      ui->treeView->setFocus();
×
979
    }
980
  }
981
  return QObject::eventFilter(obj, event);
×
982
}
983

984
/**
985
 * @brief MainWindow::keyPressEvent did anyone press return, enter or escape?
986
 * @param event
987
 */
988
void MainWindow::keyPressEvent(QKeyEvent *event) {
×
989
  switch (event->key()) {
×
990
  case Qt::Key_Delete:
×
991
    onDelete();
×
992
    break;
×
993
  case Qt::Key_Return:
×
994
  case Qt::Key_Enter:
995
    if (proxyModel.rowCount() > 0) {
×
996
      on_treeView_clicked(ui->treeView->currentIndex());
×
997
    }
998
    break;
999
  case Qt::Key_Escape:
×
1000
    ui->lineEdit->clear();
×
1001
    break;
×
1002
  default:
1003
    break;
1004
  }
1005
}
×
1006

1007
/**
1008
 * @brief MainWindow::showContextMenu show us the (file or folder) context
1009
 * menu
1010
 * @param pos
1011
 */
1012
void MainWindow::showContextMenu(const QPoint &pos) {
×
1013
  QModelIndex index = ui->treeView->indexAt(pos);
×
1014
  bool selected = true;
1015
  if (!index.isValid()) {
1016
    ui->treeView->clearSelection();
×
1017
    ui->actionDelete->setEnabled(false);
×
1018
    ui->actionEdit->setEnabled(false);
×
1019
    currentDir = "";
×
1020
    selected = false;
1021
  }
1022

1023
  ui->treeView->setCurrentIndex(index);
×
1024

1025
  QPoint globalPos = ui->treeView->viewport()->mapToGlobal(pos);
×
1026

1027
  QFileInfo fileOrFolder =
1028
      model.fileInfo(proxyModel.mapToSource(ui->treeView->currentIndex()));
×
1029

1030
  QMenu contextMenu;
×
1031
  if (!selected || fileOrFolder.isDir()) {
×
1032
    QAction *openFolder =
1033
        contextMenu.addAction(tr("Open folder with file manager"));
×
1034
    QAction *addFolder = contextMenu.addAction(tr("Add folder"));
×
1035
    QAction *addPassword = contextMenu.addAction(tr("Add password"));
×
1036
    QAction *users = contextMenu.addAction(tr("Users"));
×
1037
    connect(openFolder, &QAction::triggered, this, &MainWindow::openFolder);
×
1038
    connect(addFolder, &QAction::triggered, this, &MainWindow::addFolder);
×
1039
    connect(addPassword, &QAction::triggered, this, &MainWindow::addPassword);
×
1040
    connect(users, &QAction::triggered, this, &MainWindow::onUsers);
×
1041
  } else if (fileOrFolder.isFile()) {
×
1042
    QAction *edit = contextMenu.addAction(tr("Edit"));
×
1043
    connect(edit, &QAction::triggered, this, &MainWindow::onEdit);
×
1044
  }
1045
  if (selected) {
×
1046
    // if (useClipboard != CLIPBOARD_NEVER) {
1047
    // contextMenu.addSeparator();
1048
    // QAction* copyItem = contextMenu.addAction(tr("Copy Password"));
1049
    // if (getClippedPassword().length() == 0) copyItem->setEnabled(false);
1050
    // connect(copyItem, SIGNAL(triggered()), this,
1051
    // SLOT(copyPasswordToClipboard()));
1052
    // }
1053
    contextMenu.addSeparator();
×
1054
    if (fileOrFolder.isDir()) {
×
1055
      QAction *renameFolder = contextMenu.addAction(tr("Rename folder"));
×
1056
      connect(renameFolder, &QAction::triggered, this,
×
1057
              &MainWindow::renameFolder);
×
1058
    } else if (fileOrFolder.isFile()) {
×
1059
      QAction *renamePassword = contextMenu.addAction(tr("Rename password"));
×
1060
      connect(renamePassword, &QAction::triggered, this,
×
1061
              &MainWindow::renamePassword);
×
1062
    }
1063
    QAction *deleteItem = contextMenu.addAction(tr("Delete"));
×
1064
    connect(deleteItem, &QAction::triggered, this, &MainWindow::onDelete);
×
1065
  }
1066
  contextMenu.exec(globalPos);
×
1067
}
×
1068

1069
/**
1070
 * @brief MainWindow::showBrowserContextMenu show us the context menu in
1071
 * password window
1072
 * @param pos
1073
 */
1074
void MainWindow::showBrowserContextMenu(const QPoint &pos) {
×
1075
  QMenu *contextMenu = ui->textBrowser->createStandardContextMenu(pos);
×
1076
  QPoint globalPos = ui->textBrowser->viewport()->mapToGlobal(pos);
×
1077

1078
  contextMenu->exec(globalPos);
×
1079
  delete contextMenu;
×
1080
}
×
1081

1082
/**
1083
 * @brief MainWindow::openFolder open the folder in the default file manager
1084
 */
1085
void MainWindow::openFolder() {
×
1086
  QString dir =
1087
      Util::getDir(ui->treeView->currentIndex(), false, model, proxyModel);
×
1088

1089
  QString path = QDir::toNativeSeparators(dir);
×
1090
  QDesktopServices::openUrl(QUrl::fromLocalFile(path));
×
1091
}
×
1092

1093
/**
1094
 * @brief MainWindow::addFolder add a new folder to store passwords in
1095
 */
1096
void MainWindow::addFolder() {
×
1097
  bool ok;
1098
  QString dir =
1099
      Util::getDir(ui->treeView->currentIndex(), false, model, proxyModel);
×
1100
  QString newdir =
1101
      QInputDialog::getText(this, tr("New file"),
×
1102
                            tr("New Folder: \n(Will be placed in %1 )")
×
1103
                                .arg(QtPassSettings::getPassStore() +
×
1104
                                     Util::getDir(ui->treeView->currentIndex(),
×
1105
                                                  true, model, proxyModel)),
1106
                            QLineEdit::Normal, "", &ok);
×
1107
  if (!ok || newdir.isEmpty()) {
×
1108
    return;
1109
  }
1110
  newdir.prepend(dir);
1111
  // dbg()<< newdir;
1112
  if (!QDir().mkdir(newdir)) {
×
1113
    QMessageBox::warning(this, tr("Error"),
×
1114
                         tr("Failed to create folder: %1").arg(newdir));
×
1115
    return;
×
1116
  }
1117
  if (QtPassSettings::isAddGPGId(true)) {
×
1118
    QString gpgIdFile = newdir + "/.gpg-id";
×
1119
    QFile gpgId(gpgIdFile);
×
1120
    if (!gpgId.open(QIODevice::WriteOnly)) {
×
1121
      QMessageBox::warning(
×
1122
          this, tr("Error"),
×
1123
          tr("Failed to create .gpg-id file in: %1").arg(newdir));
×
1124
      return;
1125
    }
1126
    QList<UserInfo> users = QtPassSettings::getPass()->listKeys("", true);
×
1127
    for (const UserInfo &user : users) {
×
1128
      if (user.enabled) {
×
1129
        gpgId.write((user.key_id + "\n").toUtf8());
×
1130
      }
1131
    }
1132
    gpgId.close();
×
1133
  }
×
1134
}
1135

1136
/**
1137
 * @brief MainWindow::renameFolder rename an existing folder
1138
 */
1139
void MainWindow::renameFolder() {
×
1140
  bool ok;
1141
  QString srcDir = QDir::cleanPath(
1142
      Util::getDir(ui->treeView->currentIndex(), false, model, proxyModel));
×
1143
  QString srcDirName = QDir(srcDir).dirName();
×
1144
  QString newName =
1145
      QInputDialog::getText(this, tr("Rename file"), tr("Rename Folder To: "),
×
1146
                            QLineEdit::Normal, srcDirName, &ok);
×
1147
  if (!ok || newName.isEmpty()) {
×
1148
    return;
1149
  }
1150
  QString destDir = srcDir;
1151
  destDir.replace(srcDir.lastIndexOf(srcDirName), srcDirName.length(), newName);
×
1152
  QtPassSettings::getPass()->Move(srcDir, destDir);
×
1153
}
1154

1155
/**
1156
 * @brief MainWindow::editPassword read password and open edit window via
1157
 * MainWindow::onEdit()
1158
 */
1159
void MainWindow::editPassword(const QString &file) {
×
1160
  if (!file.isEmpty()) {
×
1161
    if (QtPassSettings::isUseGit() && QtPassSettings::isAutoPull()) {
×
1162
      onUpdate(true);
×
1163
    }
1164
    setPassword(file, false);
×
1165
  }
1166
}
×
1167

1168
/**
1169
 * @brief MainWindow::renamePassword rename an existing password
1170
 */
1171
void MainWindow::renamePassword() {
×
1172
  bool ok;
1173
  QString file = getFile(ui->treeView->currentIndex(), false);
×
1174
  QString filePath = QFileInfo(file).path();
×
1175
  QString fileName = QFileInfo(file).fileName();
×
1176
  if (fileName.endsWith(".gpg", Qt::CaseInsensitive)) {
×
1177
    fileName.chop(4);
×
1178
  }
1179

1180
  QString newName =
1181
      QInputDialog::getText(this, tr("Rename file"), tr("Rename File To: "),
×
1182
                            QLineEdit::Normal, fileName, &ok);
×
1183
  if (!ok || newName.isEmpty()) {
×
1184
    return;
1185
  }
1186
  QString newFile = QDir(filePath).filePath(newName);
×
1187
  QtPassSettings::getPass()->Move(file, newFile);
×
1188
}
1189

1190
/**
1191
 * @brief MainWindow::clearTemplateWidgets empty the template widget fields in
1192
 * the UI
1193
 */
1194
void MainWindow::clearTemplateWidgets() {
×
1195
  while (ui->gridLayout->count() > 0) {
×
1196
    QLayoutItem *item = ui->gridLayout->takeAt(0);
×
1197
    delete item->widget();
×
1198
    delete item;
×
1199
  }
1200
  ui->verticalLayoutPassword->setSpacing(0);
×
1201
}
×
1202

1203
/**
1204
 * @brief Copies the password of the selected file from the tree view to the
1205
 * clipboard.
1206
 * @example
1207
 * MainWindow::copyPasswordFromTreeview();
1208
 *
1209
 * @return void - This function does not return a value.
1210
 */
1211
void MainWindow::copyPasswordFromTreeview() {
×
1212
  QFileInfo fileOrFolder =
1213
      model.fileInfo(proxyModel.mapToSource(ui->treeView->currentIndex()));
×
1214

1215
  if (fileOrFolder.isFile()) {
×
1216
    QString file = getFile(ui->treeView->currentIndex(), true);
×
1217
    // Disconnect any previous connection to avoid accumulation
1218
    disconnect(QtPassSettings::getPass(), &Pass::finishedShow, this,
×
1219
               &MainWindow::passwordFromFileToClipboard);
1220
    connect(QtPassSettings::getPass(), &Pass::finishedShow, this,
×
1221
            &MainWindow::passwordFromFileToClipboard);
×
1222
    QtPassSettings::getPass()->Show(file);
×
1223
  }
1224
}
×
1225

1226
void MainWindow::passwordFromFileToClipboard(const QString &text) {
×
1227
  QStringList tokens = text.split('\n');
×
1228
  m_qtPass->copyTextToClipboard(tokens[0]);
×
1229
}
×
1230

1231
/**
1232
 * @brief MainWindow::addToGridLayout add a field to the template grid
1233
 * @param position
1234
 * @param field
1235
 * @param value
1236
 */
1237
void MainWindow::addToGridLayout(int position, const QString &field,
×
1238
                                 const QString &value) {
1239
  QString trimmedField = field.trimmed();
1240
  QString trimmedValue = value.trimmed();
1241

1242
  // Combine the Copy button and the line edit in one widget
1243
  auto *frame = new QFrame();
×
1244
  QLayout *ly = new QHBoxLayout();
×
1245
  ly->setContentsMargins(5, 2, 2, 2);
×
1246
  ly->setSpacing(0);
×
1247
  frame->setLayout(ly);
×
1248
  if (QtPassSettings::getClipBoardType() != Enums::CLIPBOARD_NEVER) {
×
1249
    auto *fieldLabel = new QPushButtonWithClipboard(trimmedValue, this);
×
1250
    connect(fieldLabel, &QPushButtonWithClipboard::clicked, m_qtPass,
×
1251
            &QtPass::copyTextToClipboard);
×
1252

1253
    fieldLabel->setStyleSheet(
×
1254
        "border-style: none ; background: transparent; padding: 0; margin: 0;");
1255
    frame->layout()->addWidget(fieldLabel);
×
1256
  }
1257

1258
  if (QtPassSettings::isUseQrencode()) {
×
1259
    auto *qrbutton = new QPushButtonAsQRCode(trimmedValue, this);
×
1260
    connect(qrbutton, &QPushButtonAsQRCode::clicked, m_qtPass,
×
1261
            &QtPass::showTextAsQRCode);
×
1262
    qrbutton->setStyleSheet(
×
1263
        "border-style: none ; background: transparent; padding: 0; margin: 0;");
1264
    frame->layout()->addWidget(qrbutton);
×
1265
  }
1266

1267
  // set the echo mode to password, if the field is "password"
1268
  const QString lineStyle =
1269
      QtPassSettings::isUseMonospace()
×
1270
          ? "border-style: none; background: transparent; font-family: "
1271
            "monospace;"
1272
          : "border-style: none; background: transparent;";
×
1273

1274
  if (QtPassSettings::isHidePassword() && trimmedField == tr("Password")) {
×
1275
    auto *line = new QLineEdit();
×
1276
    line->setObjectName(trimmedField);
×
1277
    line->setText(trimmedValue);
×
1278
    line->setReadOnly(true);
×
1279
    line->setStyleSheet(lineStyle);
×
1280
    line->setContentsMargins(0, 0, 0, 0);
×
1281
    line->setEchoMode(QLineEdit::Password);
×
1282
    auto *showButton = new QPushButtonShowPassword(line, this);
×
1283
    showButton->setStyleSheet(
×
1284
        "border-style: none ; background: transparent; padding: 0; margin: 0;");
1285
    showButton->setContentsMargins(0, 0, 0, 0);
×
1286
    frame->layout()->addWidget(showButton);
×
1287
    frame->layout()->addWidget(line);
×
1288
  } else {
1289
    auto *line = new QTextBrowser();
×
1290
    line->setOpenExternalLinks(true);
×
1291
    line->setOpenLinks(true);
×
1292
    line->setMaximumHeight(26);
×
1293
    line->setMinimumHeight(26);
×
1294
    line->setSizePolicy(
×
1295
        QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum));
1296
    line->setObjectName(trimmedField);
×
1297
    trimmedValue.replace(Util::protocolRegex(), R"(<a href="\1">\1</a>)");
×
1298
    line->setText(trimmedValue);
×
1299
    line->setReadOnly(true);
×
1300
    line->setStyleSheet(lineStyle);
×
1301
    line->setContentsMargins(0, 0, 0, 0);
×
1302
    frame->layout()->addWidget(line);
×
1303
  }
1304

1305
  frame->setStyleSheet(
×
1306
      ".QFrame{border: 1px solid lightgrey; border-radius: 5px;}");
1307

1308
  // set into the layout
1309
  ui->gridLayout->addWidget(new QLabel(trimmedField), position, 0);
×
1310
  ui->gridLayout->addWidget(frame, position, 1);
×
1311
}
×
1312

1313
/**
1314
 * @brief Displays message in status bar
1315
 *
1316
 * @param msg     text to be displayed
1317
 * @param timeout time for which msg shall be visible
1318
 */
1319
void MainWindow::showStatusMessage(const QString &msg, int timeout) {
×
1320
  ui->statusBar->showMessage(msg, timeout);
×
1321
}
×
1322

1323
/**
1324
 * @brief MainWindow::startReencryptPath disable ui elements and treeview
1325
 */
1326
void MainWindow::startReencryptPath() {
×
1327
  setUiElementsEnabled(false);
×
1328
  ui->treeView->setDisabled(true);
×
1329
}
×
1330

1331
/**
1332
 * @brief MainWindow::endReencryptPath re-enable ui elements
1333
 */
1334
void MainWindow::endReencryptPath() { setUiElementsEnabled(true); }
×
1335

1336
void MainWindow::updateGitButtonVisibility() {
×
1337
  if (!QtPassSettings::isUseGit() ||
×
1338
      (QtPassSettings::getGitExecutable().isEmpty() &&
×
1339
       QtPassSettings::getPassExecutable().isEmpty())) {
×
1340
    enableGitButtons(false);
×
1341
  } else {
1342
    enableGitButtons(true);
×
1343
  }
1344
}
×
1345

1346
void MainWindow::updateOtpButtonVisibility() {
×
1347
#if defined(Q_OS_WIN) || defined(__APPLE__)
1348
  ui->actionOtp->setVisible(false);
1349
#endif
1350
  if (!QtPassSettings::isUseOtp()) {
×
1351
    ui->actionOtp->setEnabled(false);
×
1352
  } else {
1353
    ui->actionOtp->setEnabled(true);
×
1354
  }
1355
}
×
1356

1357
void MainWindow::enableGitButtons(const bool &state) {
×
1358
  // Following GNOME guidelines is preferable disable buttons instead of hide
1359
  ui->actionPush->setEnabled(state);
×
1360
  ui->actionUpdate->setEnabled(state);
×
1361
}
×
1362

1363
/**
1364
 * @brief MainWindow::critical critical message popup wrapper.
1365
 * @param title
1366
 * @param msg
1367
 */
1368
void MainWindow::critical(const QString &title, const QString &msg) {
×
1369
  QMessageBox::critical(this, title, msg);
×
1370
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc