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

IJHack / QtPass / 24125149296

08 Apr 2026 08:14AM UTC coverage: 21.044%. Remained the same
24125149296

Pull #930

github

web-flow
Merge 18ce7fd68 into 4828f4ef1
Pull Request #930: chore: remove commented-out debug code

1 of 3 new or added lines in 3 files covered. (33.33%)

52 existing lines in 4 files now uncovered.

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

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

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

88
    if (QtPassSettings::getAutoclearSeconds() < 5) {
×
89
      QtPassSettings::setAutoclearSeconds(10);
×
90
    }
91
    if (QtPassSettings::getAutoclearPanelSeconds() < 5) {
×
92
      QtPassSettings::setAutoclearPanelSeconds(10);
×
93
    }
94
    if (!QtPassSettings::getPwgenExecutable().isEmpty()) {
×
95
      QtPassSettings::setUsePwgen(true);
×
96
    } else {
97
      QtPassSettings::setUsePwgen(false);
×
98
    }
99
    QtPassSettings::setPassTemplate("login\nurl");
×
100
  } else {
UNCOV
101
    if (QtPassSettings::getPassTemplate().isEmpty()) {
×
UNCOV
102
      QtPassSettings::setPassTemplate("login\nurl");
×
103
    }
104
  }
105

106
  QtPassSettings::setVersion(VERSION);
×
107

108
  if (!Util::configIsValid()) {
×
109
    m_mainWindow->config();
×
110
    if (freshStart && !Util::configIsValid()) {
×
111
      return false;
112
    }
113
  }
114

115
  // Note: WebDAV mount needs to happen before accessing the store,
116
  // but ideally should be done after Window is shown to avoid long delay.
117
  if (QtPassSettings::isUseWebDav()) {
×
118
    mountWebDav();
×
119
  }
120

121
  freshStart = false;
×
UNCOV
122
  return true;
×
123
}
124

125
/**
126
 * @brief Sets up the main window and connects signal handlers.
127
 */
128
void QtPass::setMainWindow() {
×
129
  m_mainWindow->restoreWindow();
×
130

131
  fusedav.setParent(m_mainWindow);
×
132

133
  // Signal handlers are connected for both pass implementations
134
  // Note: When pass binary changes, QtPass restart is required to reconnect
135
  // This is acceptable as pass binary change is infrequent
136
  connectPassSignalHandlers(QtPassSettings::getRealPass());
×
137
  connectPassSignalHandlers(QtPassSettings::getImitatePass());
×
138

139
  connect(m_mainWindow, &MainWindow::passShowHandlerFinished, this,
×
140
          &QtPass::passShowHandlerFinished);
×
141

142
  // only for ipass
143
  connect(QtPassSettings::getImitatePass(), &ImitatePass::startReencryptPath,
×
144
          m_mainWindow, &MainWindow::startReencryptPath);
×
145
  connect(QtPassSettings::getImitatePass(), &ImitatePass::endReencryptPath,
×
146
          m_mainWindow, &MainWindow::endReencryptPath);
×
147

148
  connect(m_mainWindow, &MainWindow::passGitInitNeeded, [this]() {
×
149
#ifdef QT_DEBUG
150
    dbg() << "Pass git init called";
151
#endif
152
    QtPassSettings::getPass()->GitInit();
×
153
  });
×
154

155
  connect(m_mainWindow, &MainWindow::generateGPGKeyPair, m_mainWindow,
×
156
          [this](const QString &batch) {
×
157
            QtPassSettings::getPass()->GenerateGPGKeys(batch);
×
158
            m_mainWindow->showStatusMessage(tr("Generating GPG key pair"),
×
159
                                            60000);
160
          });
×
161
}
×
162

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

179
  connect(pass, &Pass::finishedGitInit, this, &QtPass::passStoreChanged);
×
180
  connect(pass, &Pass::finishedGitPull, this, &QtPass::processFinished);
×
181
  connect(pass, &Pass::finishedGitPush, this, &QtPass::processFinished);
×
182
  connect(pass, &Pass::finishedInsert, this, &QtPass::finishedInsert);
×
183
  connect(pass, &Pass::finishedRemove, this, &QtPass::passStoreChanged);
×
184
  connect(pass, &Pass::finishedInit, this, &QtPass::passStoreChanged);
×
185
  connect(pass, &Pass::finishedMove, this, &QtPass::passStoreChanged);
×
186
  connect(pass, &Pass::finishedCopy, this, &QtPass::passStoreChanged);
×
187
  connect(pass, &Pass::finishedGenerateGPGKeys, this,
×
188
          &QtPass::onKeyGenerationComplete);
×
189
}
×
190

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

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

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

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

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

321
    m_mainWindow->flashText(output, false, true);
×
322
  }
323

324
  m_mainWindow->setUiElementsEnabled(true);
×
325
}
×
326

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

340
  m_mainWindow->setUiElementsEnabled(true);
×
341
}
×
342

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

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

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

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

381
  processFinished(p_output, p_errout);
×
382
}
×
383

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

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

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

406
  m_mainWindow->flashText(output, false, true);
×
407
}
×
408

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

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

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

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

467
  clippedText.clear();
×
468
}
×
469

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

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

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

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

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