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