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

IJHack / QtPass / 23797982704

31 Mar 2026 12:45PM UTC coverage: 18.508%. Remained the same
23797982704

Pull #866

github

web-flow
Merge 18663b50c into ced44afd6
Pull Request #866: docs: add comprehensive docstrings to C++ source files

933 of 5041 relevant lines covered (18.51%)

7.68 hits per line

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

4.84
/src/qtpass.cpp
1
// SPDX-FileCopyrightText: 2016 Anne Jan Brouwer
2
// SPDX-License-Identifier: GPL-3.0-or-later
3
#include "qtpass.h"
4
#include "mainwindow.h"
5
#include "qtpasssettings.h"
6
#include "util.h"
7
#include <QApplication>
8
#include <QClipboard>
9
#include <QDialog>
10
#include <QLabel>
11
#include <QPixmap>
12
#include <QVBoxLayout>
13

14
#ifndef Q_OS_WIN
15
#include <QInputDialog>
16
#include <QLineEdit>
17
#include <utility>
18
#else
19
#define WIN32_LEAN_AND_MEAN /*_KILLING_MACHINE*/
20
#define WIN32_EXTRA_LEAN
21
#include <windows.h>
22
#include <winnetwk.h>
23
#undef DELETE
24
#endif
25

26
#ifdef QT_DEBUG
27
#include "debughelper.h"
28
#endif
29

30
/**
31
 * @brief Constructs a QtPass instance.
32
 * @param mainWindow The main window reference
33
 */
34
QtPass::QtPass(MainWindow *mainWindow)
×
35
    : m_mainWindow(mainWindow), freshStart(true) {
×
36
  setClipboardTimer();
×
37
  clearClipboardTimer.setSingleShot(true);
×
38
  connect(&clearClipboardTimer, &QTimer::timeout, this,
×
39
          &QtPass::clearClipboard);
×
40

41
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
42
#pragma GCC diagnostic push
43
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
44
#endif
45
  QObject::connect(qApp, &QApplication::aboutToQuit, this,
×
46
                   &QtPass::clearClipboard);
×
47
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
48
#pragma GCC diagnostic pop
49
#endif
50

51
  setMainWindow();
×
52
}
×
53

54
/**
55
 * @brief QtPass::~QtPass destroy!
56
 */
57
QtPass::~QtPass() {
×
58
#ifdef Q_OS_WIN
59
  if (QtPassSettings::isUseWebDav())
60
    WNetCancelConnection2A(QtPassSettings::getPassStore().toUtf8().constData(),
61
                           0, 1);
62
#else
63
  if (fusedav.state() == QProcess::Running) {
×
64
    fusedav.terminate();
×
65
    fusedav.waitForFinished(2000);
×
66
  }
67
#endif
68
}
×
69

70
/**
71
 * @brief QtPass::init make sure we are ready to go as soon as
72
 * possible
73
 */
74
auto QtPass::init() -> bool {
×
75
  QString passStore = QtPassSettings::getPassStore(Util::findPasswordStore());
×
76
  QtPassSettings::setPassStore(passStore);
×
77

78
  QtPassSettings::initExecutables();
×
79

80
  QString version = QtPassSettings::getVersion();
×
81
  // dbg()<< version;
82

83
  // Config updates
84
  if (version.isEmpty()) {
×
85
#ifdef QT_DEBUG
86
    dbg() << "assuming fresh install";
87
#endif
88

89
    if (QtPassSettings::getAutoclearSeconds() < 5) {
×
90
      QtPassSettings::setAutoclearSeconds(10);
×
91
    }
92
    if (QtPassSettings::getAutoclearPanelSeconds() < 5) {
×
93
      QtPassSettings::setAutoclearPanelSeconds(10);
×
94
    }
95
    if (!QtPassSettings::getPwgenExecutable().isEmpty()) {
×
96
      QtPassSettings::setUsePwgen(true);
×
97
    } else {
98
      QtPassSettings::setUsePwgen(false);
×
99
    }
100
    QtPassSettings::setPassTemplate("login\nurl");
×
101
  } else {
102
    // QStringList ver = version.split(".");
103
    // dbg()<< ver;
104
    // if (ver[0] == "0" && ver[1] == "8") {
105
    //// upgrade to 0.9
106
    // }
107
    if (QtPassSettings::getPassTemplate().isEmpty()) {
×
108
      QtPassSettings::setPassTemplate("login\nurl");
×
109
    }
110
  }
111

112
  QtPassSettings::setVersion(VERSION);
×
113

114
  if (Util::checkConfig()) {
×
115
    m_mainWindow->config();
×
116
    if (freshStart && Util::checkConfig()) {
×
117
      return false;
118
    }
119
  }
120

121
  // TODO(annejan): this needs to be before we try to access the store,
122
  // but it would be better to do it after the Window is shown,
123
  // as the long delay it can cause is irritating otherwise.
124
  if (QtPassSettings::isUseWebDav()) {
×
125
    mountWebDav();
×
126
  }
127

128
  freshStart = false;
×
129
  // startupPhase = false;
130
  return true;
×
131
}
132

133
/**
134
 * @brief Sets up the main window and connects signal handlers.
135
 */
136
void QtPass::setMainWindow() {
×
137
  m_mainWindow->restoreWindow();
×
138

139
  fusedav.setParent(m_mainWindow);
×
140

141
  // TODO(bezet): this should be reconnected dynamically when pass changes
142
  connectPassSignalHandlers(QtPassSettings::getRealPass());
×
143
  connectPassSignalHandlers(QtPassSettings::getImitatePass());
×
144

145
  connect(m_mainWindow, &MainWindow::passShowHandlerFinished, this,
×
146
          &QtPass::passShowHandlerFinished);
×
147

148
  // only for ipass
149
  connect(QtPassSettings::getImitatePass(), &ImitatePass::startReencryptPath,
×
150
          m_mainWindow, &MainWindow::startReencryptPath);
×
151
  connect(QtPassSettings::getImitatePass(), &ImitatePass::endReencryptPath,
×
152
          m_mainWindow, &MainWindow::endReencryptPath);
×
153

154
  connect(m_mainWindow, &MainWindow::passGitInitNeeded, [=]() -> void {
×
155
#ifdef QT_DEBUG
156
    dbg() << "Pass git init called";
157
#endif
158
    QtPassSettings::getPass()->GitInit();
×
159
  });
×
160

161
  connect(m_mainWindow, &MainWindow::generateGPGKeyPair, m_mainWindow,
×
162
          [=](const QString &batch) -> void {
×
163
            QtPassSettings::getPass()->GenerateGPGKeys(batch);
×
164
            m_mainWindow->showStatusMessage(tr("Generating GPG key pair"),
×
165
                                            60000);
166
          });
×
167
}
×
168

169
/**
170
 * @brief Connects pass signal handlers to QtPass slots.
171
 * @param pass The pass instance to connect
172
 */
173
void QtPass::connectPassSignalHandlers(Pass *pass) {
×
174
  connect(pass, &Pass::error, this, &QtPass::processError);
×
175
  connect(pass, &Pass::processErrorExit, this, &QtPass::processErrorExit);
×
176
  connect(pass, &Pass::critical, m_mainWindow, &MainWindow::critical);
×
177
  connect(pass, &Pass::startingExecuteWrapper, m_mainWindow,
×
178
          &MainWindow::executeWrapperStarted);
×
179
  connect(pass, &Pass::statusMsg, m_mainWindow, &MainWindow::showStatusMessage);
×
180
  connect(pass, &Pass::finishedShow, m_mainWindow,
×
181
          &MainWindow::passShowHandler);
×
182
  connect(pass, &Pass::finishedOtpGenerate, m_mainWindow,
×
183
          &MainWindow::passOtpHandler);
×
184

185
  connect(pass, &Pass::finishedGitInit, this, &QtPass::passStoreChanged);
×
186
  connect(pass, &Pass::finishedGitPull, this, &QtPass::processFinished);
×
187
  connect(pass, &Pass::finishedGitPush, this, &QtPass::processFinished);
×
188
  connect(pass, &Pass::finishedInsert, this, &QtPass::finishedInsert);
×
189
  connect(pass, &Pass::finishedRemove, this, &QtPass::passStoreChanged);
×
190
  connect(pass, &Pass::finishedInit, this, &QtPass::passStoreChanged);
×
191
  connect(pass, &Pass::finishedMove, this, &QtPass::passStoreChanged);
×
192
  connect(pass, &Pass::finishedCopy, this, &QtPass::passStoreChanged);
×
193
  connect(pass, &Pass::finishedGenerateGPGKeys, this,
×
194
          &QtPass::onKeyGenerationComplete);
×
195
}
×
196

197
/**
198
 * @brief QtPass::mountWebDav is some scary voodoo magic
199
 */
200
void QtPass::mountWebDav() {
×
201
#ifdef Q_OS_WIN
202
  char dst[20] = {0};
203
  NETRESOURCEA netres;
204
  memset(&netres, 0, sizeof(netres));
205
  netres.dwType = RESOURCETYPE_DISK;
206
  netres.lpLocalName = 0;
207
  netres.lpRemoteName = QtPassSettings::getWebDavUrl().toUtf8().data();
208
  DWORD size = sizeof(dst);
209
  DWORD r = WNetUseConnectionA(
210
      reinterpret_cast<HWND>(m_mainWindow->effectiveWinId()), &netres,
211
      QtPassSettings::getWebDavPassword().toUtf8().constData(),
212
      QtPassSettings::getWebDavUser().toUtf8().constData(),
213
      CONNECT_TEMPORARY | CONNECT_INTERACTIVE | CONNECT_REDIRECT, dst, &size,
214
      0);
215
  if (r == NO_ERROR) {
216
    QtPassSettings::setPassStore(dst);
217
  } else {
218
    char message[256] = {0};
219
    FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, 0, r, 0, message,
220
                   sizeof(message), 0);
221
    m_mainWindow->flashText(tr("Failed to connect WebDAV:\n") + message +
222
                                " (0x" + QString::number(r, 16) + ")",
223
                            true);
224
  }
225
#else
226
  fusedav.start("fusedav", QStringList()
×
227
                               << "-o"
×
228
                               << "nonempty"
×
229
                               << "-u"
×
230
                               << "\"" + QtPassSettings::getWebDavUser() + "\""
×
231
                               << QtPassSettings::getWebDavUrl()
×
232
                               << "\"" + QtPassSettings::getPassStore() + "\"");
×
233
  fusedav.waitForStarted();
×
234
  if (fusedav.state() == QProcess::Running) {
×
235
    QString pwd = QtPassSettings::getWebDavPassword();
×
236
    bool ok = true;
×
237
    if (pwd.isEmpty()) {
×
238
      pwd = QInputDialog::getText(m_mainWindow, tr("QtPass WebDAV password"),
×
239
                                  tr("Enter password to connect to WebDAV:"),
×
240
                                  QLineEdit::Password, "", &ok);
241
    }
242
    if (ok && !pwd.isEmpty()) {
×
243
      fusedav.write(pwd.toUtf8() + '\n');
×
244
      fusedav.closeWriteChannel();
×
245
      fusedav.waitForFinished(2000);
×
246
    } else {
247
      fusedav.terminate();
×
248
    }
249
  }
250
  QString error = fusedav.readAllStandardError();
×
251
  int prompt = error.indexOf("Password:");
×
252
  if (prompt >= 0) {
×
253
    error.remove(0, prompt + 10);
×
254
  }
255
  if (fusedav.state() != QProcess::Running) {
×
256
    error = tr("fusedav exited unexpectedly\n") + error;
×
257
  }
258
  if (error.size() > 0) {
×
259
    m_mainWindow->flashText(
×
260
        tr("Failed to start fusedav to connect WebDAV:\n") + error, true);
×
261
  }
262
#endif
263
}
×
264

265
/**
266
 * @brief QtPass::processError something went wrong
267
 * @param error
268
 */
269
void QtPass::processError(QProcess::ProcessError error) {
×
270
  QString errorString;
×
271
  switch (error) {
×
272
  case QProcess::FailedToStart:
×
273
    errorString = tr("QProcess::FailedToStart");
×
274
    break;
×
275
  case QProcess::Crashed:
×
276
    errorString = tr("QProcess::Crashed");
×
277
    break;
×
278
  case QProcess::Timedout:
×
279
    errorString = tr("QProcess::Timedout");
×
280
    break;
×
281
  case QProcess::ReadError:
×
282
    errorString = tr("QProcess::ReadError");
×
283
    break;
×
284
  case QProcess::WriteError:
×
285
    errorString = tr("QProcess::WriteError");
×
286
    break;
×
287
  case QProcess::UnknownError:
×
288
    errorString = tr("QProcess::UnknownError");
×
289
    break;
×
290
  }
291

292
  m_mainWindow->flashText(errorString, true);
×
293
  m_mainWindow->setUiElementsEnabled(true);
×
294
}
×
295

296
/**
297
 * @brief Handles process error exit.
298
 * @param exitCode The exit code
299
 * @param p_error The error message
300
 */
301
void QtPass::processErrorExit(int exitCode, const QString &p_error) {
×
302
  if (nullptr != m_mainWindow->getKeygenDialog()) {
×
303
    m_mainWindow->cleanKeygenDialog();
×
304
    if (exitCode != 0) {
×
305
      m_mainWindow->showStatusMessage(tr("GPG key pair generation failed"),
×
306
                                      10000);
307
    }
308
  }
309

310
  if (!p_error.isEmpty()) {
×
311
    QString output;
×
312
    QString error = p_error.toHtmlEscaped();
×
313
    if (exitCode == 0) {
×
314
      //  https://github.com/IJHack/qtpass/issues/111
315
      output = "<span style=\"color: darkgray;\">" + error + "</span><br />";
×
316
    } else {
317
      output = "<span style=\"color: red;\">" + error + "</span><br />";
×
318
    }
319

320
    output.replace(Util::protocolRegex(), R"(<a href="\1">\1</a>)");
×
321
    output.replace(QStringLiteral("\n"), "<br />");
×
322

323
    m_mainWindow->flashText(output, false, true);
×
324
  }
325

326
  m_mainWindow->setUiElementsEnabled(true);
×
327
}
×
328

329
/**
330
 * @brief QtPass::processFinished background process has finished
331
 * @param exitCode
332
 * @param exitStatus
333
 * @param output    stdout from a process
334
 * @param errout    stderr from a process
335
 */
336
void QtPass::processFinished(const QString &p_output, const QString &p_errout) {
×
337
  showInTextBrowser(p_output);
×
338
  //    Sometimes there is error output even with 0 exit code, which is
339
  //    assumed in this function
340
  processErrorExit(0, p_errout);
×
341

342
  m_mainWindow->setUiElementsEnabled(true);
×
343
}
×
344

345
/**
346
 * @brief Called when pass store has changed.
347
 * @param p_out Output from the process
348
 * @param p_err Error output
349
 */
350
void QtPass::passStoreChanged(const QString &p_out, const QString &p_err) {
×
351
  processFinished(p_out, p_err);
×
352
  doGitPush();
×
353
}
×
354

355
/**
356
 * @brief Called when an insert operation has finished.
357
 * @param p_output Output from the process
358
 * @param p_errout Error output
359
 */
360
void QtPass::finishedInsert(const QString &p_output, const QString &p_errout) {
×
361
  processFinished(p_output, p_errout);
×
362
  doGitPush();
×
363
  m_mainWindow->on_treeView_clicked(m_mainWindow->getCurrentTreeViewIndex());
×
364
}
×
365

366
/**
367
 * @brief Called when GPG key generation is complete.
368
 * @param p_output Standard output from the key generation process
369
 * @param p_errout Standard error output from the key generation process
370
 */
371
void QtPass::onKeyGenerationComplete(const QString &p_output,
×
372
                                     const QString &p_errout) {
373
  if (nullptr != m_mainWindow->getKeygenDialog()) {
×
374
#ifdef QT_DEBUG
375
    qDebug() << "Keygen Done";
376
#endif
377

378
    m_mainWindow->cleanKeygenDialog();
×
379
    m_mainWindow->showStatusMessage(tr("GPG key pair generated successfully"),
×
380
                                    10000);
381
  }
382

383
  processFinished(p_output, p_errout);
×
384
}
×
385

386
/**
387
 * @brief Called when the password show handler has finished.
388
 * @param output The password content to display
389
 */
390
void QtPass::passShowHandlerFinished(QString output) {
×
391
  showInTextBrowser(std::move(output));
×
392
}
×
393

394
/**
395
 * @brief Displays output text in the main window's text browser.
396
 * @param output The text to display
397
 * @param prefix Optional prefix to prepend to the output
398
 * @param postfix Optional postfix to append to the output
399
 */
400
void QtPass::showInTextBrowser(QString output, const QString &prefix,
×
401
                               const QString &postfix) {
402
  output = output.toHtmlEscaped();
×
403

404
  output.replace(Util::protocolRegex(), R"(<a href="\1">\1</a>)");
×
405
  output.replace(QStringLiteral("\n"), "<br />");
×
406
  output = prefix + output + postfix;
×
407

408
  m_mainWindow->flashText(output, false, true);
×
409
}
×
410

411
/**
412
 * @brief Performs automatic git push if enabled in settings.
413
 */
414
void QtPass::doGitPush() {
×
415
  if (QtPassSettings::isAutoPush()) {
×
416
    m_mainWindow->onPush();
×
417
  }
418
}
×
419

420
/**
421
 * @brief Sets the text to be stored in clipboard and handles clipboard
422
 * operations.
423
 * @param password The password or text to store
424
 * @param p_output Additional output text
425
 */
426
void QtPass::setClippedText(const QString &password, const QString &p_output) {
×
427
  if (QtPassSettings::getClipBoardType() != Enums::CLIPBOARD_NEVER &&
×
428
      !p_output.isEmpty()) {
429
    clippedText = password;
×
430
    if (QtPassSettings::getClipBoardType() == Enums::CLIPBOARD_ALWAYS) {
×
431
      copyTextToClipboard(password);
×
432
    }
433
  }
434
}
×
435
/**
436
 * @brief Clears the stored clipped text.
437
 */
438
void QtPass::clearClippedText() { clippedText = ""; }
×
439

440
/**
441
 * @brief Sets the clipboard clear timer based on autoclear settings.
442
 */
443
void QtPass::setClipboardTimer() {
×
444
  clearClipboardTimer.setInterval(MS_PER_SECOND *
×
445
                                  QtPassSettings::getAutoclearSeconds());
×
446
}
×
447

448
/**
449
 * @brief MainWindow::clearClipboard remove clipboard contents.
450
 */
451
void QtPass::clearClipboard() {
×
452
  QClipboard *clipboard = QApplication::clipboard();
×
453
  bool cleared = false;
454
  if (this->clippedText == clipboard->text(QClipboard::Selection)) {
×
455
    clipboard->clear(QClipboard::Selection);
×
456
    clipboard->setText(QString(""), QClipboard::Selection);
×
457
    cleared = true;
458
  }
459
  if (this->clippedText == clipboard->text(QClipboard::Clipboard)) {
×
460
    clipboard->clear(QClipboard::Clipboard);
×
461
    cleared = true;
462
  }
463
  if (cleared) {
×
464
    m_mainWindow->showStatusMessage(tr("Clipboard cleared"));
×
465
  } else {
466
    m_mainWindow->showStatusMessage(tr("Clipboard not cleared"));
×
467
  }
468

469
  clippedText.clear();
×
470
}
×
471

472
/**
473
 * @brief MainWindow::copyTextToClipboard copies text to your clipboard
474
 * @param text
475
 */
476
void QtPass::copyTextToClipboard(const QString &text) {
×
477
  QClipboard *clip = QApplication::clipboard();
×
478
  if (!QtPassSettings::isUseSelection()) {
×
479
    clip->setText(text, QClipboard::Clipboard);
×
480
  } else {
481
    clip->setText(text, QClipboard::Selection);
×
482
  }
483

484
  clippedText = text;
×
485
  m_mainWindow->showStatusMessage(tr("Copied to clipboard"));
×
486
  if (QtPassSettings::isUseAutoclear()) {
×
487
    clearClipboardTimer.start();
×
488
  }
489
}
×
490

491
/**
492
 * @brief displays the text as qrcode
493
 * @param text
494
 */
495
void QtPass::showTextAsQRCode(const QString &text) {
×
496
  QProcess qrencode;
×
497
  qrencode.start(QtPassSettings::getQrencodeExecutable("/usr/bin/qrencode"),
×
498
                 QStringList() << "-o-"
×
499
                               << "-tPNG");
×
500
  qrencode.write(text.toUtf8());
×
501
  qrencode.closeWriteChannel();
×
502
  qrencode.waitForFinished();
×
503
  QByteArray output(qrencode.readAllStandardOutput());
×
504

505
  if (qrencode.exitStatus() || qrencode.exitCode()) {
×
506
    QString error(qrencode.readAllStandardError());
×
507
    m_mainWindow->showStatusMessage(error);
×
508
  } else {
509
    QPixmap image;
×
510
    image.loadFromData(output, "PNG");
×
511
    QDialog *popup = createQRCodePopup(image);
×
512
    popup->exec();
×
513
  }
×
514
}
×
515

516
/**
517
 * @brief QtPass::createQRCodePopup creates a popup dialog with the given QR
518
 * code image. This is extracted for testability. The caller is responsible
519
 * for showing and managing the popup lifecycle.
520
 * @param image The QR code pixmap to display
521
 * @return The created popup dialog
522
 */
523
QDialog *QtPass::createQRCodePopup(const QPixmap &image) {
1✔
524
  auto *popup = new QDialog(nullptr, Qt::Popup | Qt::FramelessWindowHint);
1✔
525
  popup->setAttribute(Qt::WA_DeleteOnClose);
1✔
526
  auto *layout = new QVBoxLayout;
1✔
527
  auto *popupLabel = new QLabel();
1✔
528
  layout->addWidget(popupLabel);
1✔
529
  popupLabel->setPixmap(image);
1✔
530
  popupLabel->setScaledContents(true);
1✔
531
  popupLabel->show();
1✔
532
  popup->setLayout(layout);
1✔
533
  popup->move(QCursor::pos());
1✔
534
  return popup;
1✔
535
}
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