QtPass  1.3.2
Multi-platform GUI for pass, the standard unix password manager.
usersdialog.cpp
Go to the documentation of this file.
1 #include "usersdialog.h"
2 #include "qtpasssettings.h"
3 #include "ui_usersdialog.h"
4 #include <QCloseEvent>
5 #include <QKeyEvent>
6 #include <QMessageBox>
7 #include <QRegExp>
8 #include <QWidget>
9 #include <utility>
10 
11 #ifdef QT_DEBUG
12 #include "debughelper.h"
13 #endif
14 
18 UsersDialog::UsersDialog(QString dir, QWidget *parent)
19  : QDialog(parent), ui(new Ui::UsersDialog), m_dir(std::move(dir)) {
20 
21  ui->setupUi(this);
22 
23  QList<UserInfo> users = QtPassSettings::getPass()->listKeys();
24  if (users.isEmpty()) {
25  QMessageBox::critical(parent, tr("Keylist missing"),
26  tr("Could not fetch list of available GPG keys"));
27  return;
28  }
29 
30  QList<UserInfo> secret_keys = QtPassSettings::getPass()->listKeys("", true);
31  foreach (const UserInfo &sec, secret_keys) {
32  for (auto &user : users)
33  if (sec.key_id == user.key_id)
34  user.have_secret = true;
35  }
36 
37  QList<UserInfo> selected_users;
38  int count = 0;
39 
40  QStringList recipients = QtPassSettings::getPass()->getRecipientString(
41  m_dir.isEmpty() ? "" : m_dir, " ", &count);
42  if (!recipients.isEmpty())
43  selected_users = QtPassSettings::getPass()->listKeys(recipients);
44  foreach (const UserInfo &sel, selected_users) {
45  for (auto &user : users)
46  if (sel.key_id == user.key_id)
47  user.enabled = true;
48  }
49 
50  if (count > selected_users.size()) {
51  // Some keys seem missing from keyring, add them separately
52  QStringList recipients = QtPassSettings::getPass()->getRecipientList(
53  m_dir.isEmpty() ? "" : m_dir);
54  foreach (const QString recipient, recipients) {
55  if (QtPassSettings::getPass()->listKeys(recipient).empty()) {
56  UserInfo i;
57  i.enabled = true;
58  i.key_id = recipient;
59  i.name = " ?? " + tr("Key not found in keyring");
60  users.append(i);
61  }
62  }
63  }
64 
65  m_userList = users;
66  populateList();
67 
68  connect(ui->buttonBox, &QDialogButtonBox::accepted, this,
70  connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
71  connect(ui->listWidget, &QListWidget::itemChanged, this,
72  &UsersDialog::itemChange);
73 
74 #if QT_VERSION >= 0x050200
75  ui->lineEdit->setClearButtonEnabled(true);
76 #endif
77 }
78 
82 UsersDialog::~UsersDialog() { delete ui; }
83 
84 Q_DECLARE_METATYPE(UserInfo *)
85 
86 
89 void UsersDialog::accept() {
90  QtPassSettings::getPass()->Init(m_dir, m_userList);
91 
92  QDialog::accept();
93 }
94 
100 void UsersDialog::closeEvent(QCloseEvent *event) {
101  // TODO(annejan) save window size or something
102  event->accept();
103 }
104 
110 void UsersDialog::keyPressEvent(QKeyEvent *event) {
111  switch (event->key()) {
112  case Qt::Key_Escape:
113  ui->lineEdit->clear();
114  break;
115  default:
116  break;
117  }
118 }
119 
124 void UsersDialog::itemChange(QListWidgetItem *item) {
125  if (!item)
126  return;
127  auto *info = item->data(Qt::UserRole).value<UserInfo *>();
128  if (!info)
129  return;
130  info->enabled = item->checkState() == Qt::Checked;
131 }
132 
138 void UsersDialog::populateList(const QString &filter) {
139  QRegExp nameFilter("*" + filter + "*");
140  nameFilter.setPatternSyntax(QRegExp::Wildcard);
141  nameFilter.setCaseSensitivity(Qt::CaseInsensitive);
142  ui->listWidget->clear();
143  if (!m_userList.isEmpty()) {
144  for (auto &user : m_userList) {
145  if (filter.isEmpty() || nameFilter.exactMatch(user.name)) {
146  if (!user.isValid() && !ui->checkBox->isChecked())
147  continue;
148  if (user.expiry.toTime_t() > 0 &&
149  user.expiry.daysTo(QDateTime::currentDateTime()) > 0 &&
150  !ui->checkBox->isChecked())
151  continue;
152  QString userText = user.name + "\n" + user.key_id;
153  if (user.created.toTime_t() > 0) {
154  userText += " " + tr("created") + " " +
155  user.created.toString(Qt::SystemLocaleShortDate);
156  }
157  if (user.expiry.toTime_t() > 0)
158  userText += " " + tr("expires") + " " +
159  user.expiry.toString(Qt::SystemLocaleShortDate);
160  auto *item = new QListWidgetItem(userText, ui->listWidget);
161  item->setCheckState(user.enabled ? Qt::Checked : Qt::Unchecked);
162  item->setData(Qt::UserRole, QVariant::fromValue(&user));
163  if (user.have_secret) {
164  // item->setForeground(QColor(32, 74, 135));
165  item->setForeground(Qt::blue);
166  QFont font;
167  font.setFamily(font.defaultFamily());
168  font.setBold(true);
169  item->setFont(font);
170  } else if (!user.isValid()) {
171  item->setBackground(QColor(164, 0, 0));
172  item->setForeground(Qt::white);
173  } else if (user.expiry.toTime_t() > 0 &&
174  user.expiry.daysTo(QDateTime::currentDateTime()) > 0) {
175  item->setForeground(QColor(164, 0, 0));
176  } else if (!user.fullyValid()) {
177  item->setBackground(QColor(164, 80, 0));
178  item->setForeground(Qt::white);
179  }
180 
181  ui->listWidget->addItem(item);
182  }
183  }
184  }
185 }
186 
191 void UsersDialog::on_lineEdit_textChanged(const QString &filter) {
192  populateList(filter);
193 }
194 
198 void UsersDialog::on_checkBox_clicked() { populateList(ui->lineEdit->text()); }
UsersDialog::closeEvent
void closeEvent(QCloseEvent *event)
UsersDialog::closeEvent might have to store size and location if that is wanted.
Definition: usersdialog.cpp:100
UsersDialog
Handles listing and editing of GPG users.
Definition: usersdialog.h:23
Pass::listKeys
QList< UserInfo > listKeys(QStringList keystrings, bool secret=false)
Pass::listKeys list users.
Definition: pass.cpp:128
UserInfo::key_id
QString key_id
UserInfo::key_id hexadecimal representation.
Definition: userinfo.h:36
UsersDialog::accept
void accept()
UsersDialog::accept.
Definition: usersdialog.cpp:89
UserInfo::enabled
bool enabled
UserInfo::enabled.
Definition: userinfo.h:50
debughelper.h
UsersDialog::UsersDialog
UsersDialog(QString dir, QWidget *parent=nullptr)
UsersDialog::UsersDialog basic constructor.
Definition: usersdialog.cpp:18
QtPassSettings::getPass
static Pass * getPass()
Definition: qtpasssettings.cpp:87
Pass::getRecipientList
static QStringList getRecipientList(QString for_file)
Pass::getRecipientList return list of gpg-id's to encrypt for.
Definition: pass.cpp:259
usersdialog.h
Pass::getRecipientString
static QStringList getRecipientString(QString for_file, QString separator=" ", int *count=NULL)
Pass::getRecipientString formated string for use with GPG.
Definition: pass.cpp:295
UsersDialog::keyPressEvent
void keyPressEvent(QKeyEvent *event)
UsersDialog::keyPressEvent clear the lineEdit when escape is pressed. No action for Enter currently.
Definition: usersdialog.cpp:110
Ui
Definition: configdialog.h:9
UsersDialog::~UsersDialog
~UsersDialog()
UsersDialog::~UsersDialog basic destructor.
Definition: usersdialog.cpp:82
Pass::Init
virtual void Init(QString path, const QList< UserInfo > &users)=0
UserInfo::name
QString name
UserInfo::name full name.
Definition: userinfo.h:32
UserInfo
Stores key info lines including validity, creation date and more.
Definition: userinfo.h:11
qtpasssettings.h