2
* Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation, either version 2 or (at your option)
7
* version 3 of the License.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18
#include "EntryModel.h"
24
#include "core/Entry.h"
25
#include "core/Group.h"
26
#include "core/Metadata.h"
27
#include "core/PasswordHealth.h"
28
#include "gui/DatabaseIcons.h"
30
#include "gui/styles/StateColorPalette.h"
32
#include "gui/osutils/macutils/MacUtils.h"
35
EntryModel::EntryModel(QObject* parent)
36
: QAbstractTableModel(parent)
38
, HiddenContentDisplay(QString("\u25cf").repeated(6))
39
, DateFormat(Qt::DefaultLocaleShortDate)
41
connect(config(), &Config::changed, this, &EntryModel::onConfigChanged);
44
Entry* EntryModel::entryFromIndex(const QModelIndex& index) const
46
Q_ASSERT(index.isValid() && index.row() < m_entries.size());
47
return m_entries.at(index.row());
50
QModelIndex EntryModel::indexFromEntry(Entry* entry) const
52
int row = m_entries.indexOf(entry);
59
void EntryModel::setGroup(Group* group)
61
if (!group || group == m_group) {
71
m_entries = group->entries();
74
makeConnections(group);
79
void EntryModel::setEntries(const QList<Entry*>& entries)
88
m_orgEntries = entries;
90
for (const auto entry : asConst(m_entries)) {
92
m_allGroups.insert(entry->group());
96
for (const auto group : m_allGroups) {
97
makeConnections(group);
103
int EntryModel::rowCount(const QModelIndex& parent) const
105
if (parent.isValid()) {
108
return m_entries.size();
112
int EntryModel::columnCount(const QModelIndex& parent) const
114
// Advised by Qt documentation
115
if (parent.isValid()) {
122
QVariant EntryModel::data(const QModelIndex& index, int role) const
124
if (!index.isValid()) {
128
Entry* entry = entryFromIndex(index);
129
EntryAttributes* attr = entry->attributes();
131
if (role == Qt::DisplayRole) {
133
switch (index.column()) {
135
if (entry->group()) {
136
return entry->group()->name();
140
result = entry->resolveMultiplePlaceholders(entry->title());
141
if (attr->isReference(EntryAttributes::TitleKey)) {
142
result.prepend(tr("Ref: ", "Reference abbreviation"));
146
if (config()->get(Config::GUI_HideUsernames).toBool()) {
147
result = EntryModel::HiddenContentDisplay;
149
result = entry->resolveMultiplePlaceholders(entry->username());
151
if (attr->isReference(EntryAttributes::UserNameKey)) {
152
result.prepend(tr("Ref: ", "Reference abbreviation"));
154
if (entry->username().isEmpty() && !config()->get(Config::Security_PasswordEmptyPlaceholder).toBool()) {
159
if (config()->get(Config::GUI_HidePasswords).toBool()) {
160
result = EntryModel::HiddenContentDisplay;
162
result = entry->resolveMultiplePlaceholders(entry->password());
164
if (attr->isReference(EntryAttributes::PasswordKey)) {
165
result.prepend(tr("Ref: ", "Reference abbreviation"));
167
if (entry->password().isEmpty() && !config()->get(Config::Security_PasswordEmptyPlaceholder).toBool()) {
172
result = entry->resolveMultiplePlaceholders(entry->displayUrl());
173
if (attr->isReference(EntryAttributes::URLKey)) {
174
result.prepend(tr("Ref: ", "Reference abbreviation"));
178
if (!entry->notes().isEmpty()) {
179
if (config()->get(Config::Security_HideNotes).toBool()) {
180
result = EntryModel::HiddenContentDisplay;
182
// Display only first line of notes in simplified format if not hidden
183
result = entry->notes().section("\n", 0, 0).simplified();
185
if (attr->isReference(EntryAttributes::NotesKey)) {
186
result.prepend(tr("Ref: ", "Reference abbreviation"));
191
// Display either date of expiry or 'Never'
192
result = entry->timeInfo().expires()
193
? entry->timeInfo().expiryTime().toLocalTime().toString(EntryModel::DateFormat)
197
result = entry->timeInfo().creationTime().toLocalTime().toString(EntryModel::DateFormat);
200
result = entry->timeInfo().lastModificationTime().toLocalTime().toString(EntryModel::DateFormat);
203
result = entry->timeInfo().lastAccessTime().toLocalTime().toString(EntryModel::DateFormat);
206
// Display comma-separated list of attachments
207
QList<QString> attachments = entry->attachments()->keys();
208
for (const auto& attachment : attachments) {
209
if (result.isEmpty()) {
210
result.append(attachment);
213
result.append(QString(", ") + attachment);
218
const int unitsSize = 4;
219
QString units[unitsSize] = {"B", "KiB", "MiB", "GiB"};
220
float resultInt = entry->size();
222
for (int i = 0; i < unitsSize; i++) {
223
if (resultInt < 1024 || i == unitsSize - 1) {
224
resultInt = qRound(resultInt * 100) / 100.0;
225
result = QStringLiteral("%1 %2").arg(QString::number(resultInt), units[i]);
234
QColor backgroundColor;
235
backgroundColor.setNamedColor(entry->backgroundColor());
236
if (backgroundColor.isValid()) {
241
} else if (role == Qt::UserRole) { // Qt::UserRole is used as sort role, see EntryView::EntryView()
242
switch (index.column()) {
244
return entry->resolveMultiplePlaceholders(entry->username());
246
return entry->resolveMultiplePlaceholders(entry->password());
247
case PasswordStrength: {
248
if (!entry->password().isEmpty() && !entry->excludeFromReports()) {
249
return entry->passwordHealth()->score();
254
// There seems to be no better way of expressing 'infinity'
255
return entry->timeInfo().expires() ? entry->timeInfo().expiryTime() : QDateTime(QDate(9999, 1, 1));
257
return entry->timeInfo().creationTime();
259
return entry->timeInfo().lastModificationTime();
261
return entry->timeInfo().lastAccessTime();
263
// Display entries with attachments above those without when
264
// sorting ascendingly (and vice versa when sorting descendingly)
265
return !entry->attachments()->isEmpty();
267
return entry->hasTotp();
269
return entry->size();
271
// For all other columns, simply use data provided by Qt::Display-
273
return data(index, Qt::DisplayRole);
275
} else if (role == Qt::DecorationRole) {
276
switch (index.column()) {
278
if (entry->group()) {
279
return Icons::groupIconPixmap(entry->group());
283
return Icons::entryIconPixmap(entry);
285
if (!entry->attachments()->isEmpty()) {
286
return icons()->icon("paperclip");
290
if (entry->hasTotp()) {
291
return icons()->icon("totp");
294
case PasswordStrength:
295
if (!entry->password().isEmpty() && !entry->excludeFromReports()) {
296
StateColorPalette statePalette;
297
QColor color = statePalette.color(StateColorPalette::Error);
299
switch (entry->passwordHealth()->quality()) {
300
case PasswordHealth::Quality::Bad:
301
case PasswordHealth::Quality::Poor:
302
color = statePalette.color(StateColorPalette::HealthCritical);
304
case PasswordHealth::Quality::Weak:
305
color = statePalette.color(StateColorPalette::HealthBad);
307
case PasswordHealth::Quality::Good:
308
case PasswordHealth::Quality::Excellent:
309
color = statePalette.color(StateColorPalette::HealthExcellent);
317
} else if (role == Qt::FontRole) {
319
if (entry->isExpired()) {
320
font.setStrikeOut(true);
323
} else if (role == Qt::ForegroundRole) {
325
if (index.column() == Color) {
326
QColor backgroundColor;
327
backgroundColor.setNamedColor(entry->backgroundColor());
328
if (backgroundColor.isValid()) {
329
return backgroundColor;
333
QColor foregroundColor;
334
foregroundColor.setNamedColor(entry->foregroundColor());
335
if (entry->hasReferences()) {
337
foregroundColor = p.color(QPalette::Current, QPalette::Text);
339
qMin(255, qMax(0, foregroundColor.lightness() + (foregroundColor.lightness() < 110 ? 85 : -51)));
340
foregroundColor.setHsl(foregroundColor.hue(), foregroundColor.saturation(), lightness);
341
return QVariant(foregroundColor);
342
} else if (foregroundColor.isValid()) {
343
return QVariant(foregroundColor);
345
} else if (role == Qt::BackgroundRole) {
346
if (m_backgroundColorVisible) {
347
QColor backgroundColor;
348
backgroundColor.setNamedColor(entry->backgroundColor());
349
if (backgroundColor.isValid()) {
350
return QVariant(backgroundColor);
353
} else if (role == Qt::ToolTipRole) {
354
if (index.column() == PasswordStrength && !entry->password().isEmpty() && !entry->excludeFromReports()) {
355
return entry->passwordHealth()->scoreReason();
362
QVariant EntryModel::headerData(int section, Qt::Orientation orientation, int role) const
364
Q_UNUSED(orientation);
366
if (role == Qt::DisplayRole) {
373
return tr("Username");
375
return tr("Password");
381
return tr("Expires");
383
return tr("Created");
385
return tr("Modified");
387
return tr("Accessed");
389
return tr("Attachments");
394
} else if (role == Qt::DecorationRole) {
397
return icons()->icon("paperclip");
399
return icons()->icon("totp");
400
case PasswordStrength:
401
return icons()->icon("lock-question");
403
} else if (role == Qt::ToolTipRole) {
406
return tr("Group name");
408
return tr("Entry title");
410
return tr("Username");
412
return tr("Password");
413
case PasswordStrength:
414
return tr("Password Strength");
418
return tr("Entry notes");
420
return tr("Entry expires at");
422
return tr("Creation date");
424
return tr("Last modification date");
426
return tr("Last access date");
428
return tr("Attached files");
430
return tr("Entry size");
432
return tr("Has attachments");
434
return tr("Has TOTP");
436
return tr("Background Color");
443
Qt::DropActions EntryModel::supportedDropActions() const
445
return Qt::IgnoreAction;
448
Qt::DropActions EntryModel::supportedDragActions() const
450
return (Qt::MoveAction | Qt::CopyAction);
453
Qt::ItemFlags EntryModel::flags(const QModelIndex& modelIndex) const
455
if (!modelIndex.isValid()) {
456
return Qt::NoItemFlags;
458
return QAbstractItemModel::flags(modelIndex) | Qt::ItemIsDragEnabled;
462
QStringList EntryModel::mimeTypes() const
465
types << QLatin1String("application/x-keepassx-entry");
469
QMimeData* EntryModel::mimeData(const QModelIndexList& indexes) const
471
if (indexes.isEmpty()) {
475
auto data = new QMimeData();
477
QDataStream stream(&encoded, QIODevice::WriteOnly);
479
QSet<Entry*> seenEntries;
481
for (const QModelIndex& index : indexes) {
482
if (!index.isValid()) {
486
Entry* entry = entryFromIndex(index);
487
if (!seenEntries.contains(entry)) {
488
// make sure we don't add entries multiple times when we get indexes
489
// with the same row but different columns
490
stream << entry->group()->database()->uuid() << entry->uuid();
491
seenEntries.insert(entry);
495
if (seenEntries.isEmpty()) {
499
data->setData(mimeTypes().at(0), encoded);
504
void EntryModel::entryAboutToAdd(Entry* entry)
506
if (!m_group && !m_orgEntries.contains(entry)) {
510
beginInsertRows(QModelIndex(), m_entries.size(), m_entries.size());
512
m_entries.append(entry);
516
void EntryModel::entryAdded(Entry* entry)
518
if (!m_group && !m_orgEntries.contains(entry)) {
523
m_entries = m_group->entries();
528
void EntryModel::entryAboutToRemove(Entry* entry)
530
beginRemoveRows(QModelIndex(), m_entries.indexOf(entry), m_entries.indexOf(entry));
532
m_entries.removeAll(entry);
536
void EntryModel::entryRemoved()
539
m_entries = m_group->entries();
544
void EntryModel::entryAboutToMoveUp(int row)
546
beginMoveRows(QModelIndex(), row, row, QModelIndex(), row - 1);
548
m_entries.move(row, row - 1);
552
void EntryModel::entryMovedUp()
555
m_entries = m_group->entries();
560
void EntryModel::entryAboutToMoveDown(int row)
562
beginMoveRows(QModelIndex(), row, row, QModelIndex(), row + 2);
564
m_entries.move(row, row + 1);
568
void EntryModel::entryMovedDown()
571
m_entries = m_group->entries();
576
void EntryModel::entryDataChanged(Entry* entry)
578
int row = m_entries.indexOf(entry);
579
emit dataChanged(index(row, 0), index(row, columnCount() - 1));
582
void EntryModel::onConfigChanged(Config::ConfigKey key)
585
case Config::GUI_HideUsernames:
586
emit dataChanged(index(0, Username), index(rowCount() - 1, Username), {Qt::DisplayRole});
588
case Config::GUI_HidePasswords:
589
emit dataChanged(index(0, Password), index(rowCount() - 1, Password), {Qt::DisplayRole});
596
void EntryModel::severConnections()
599
disconnect(m_group, nullptr, this, nullptr);
602
for (const Group* group : asConst(m_allGroups)) {
603
disconnect(group, nullptr, this, nullptr);
607
void EntryModel::makeConnections(const Group* group)
609
connect(group, SIGNAL(entryAboutToAdd(Entry*)), SLOT(entryAboutToAdd(Entry*)));
610
connect(group, SIGNAL(entryAdded(Entry*)), SLOT(entryAdded(Entry*)));
611
connect(group, SIGNAL(entryAboutToRemove(Entry*)), SLOT(entryAboutToRemove(Entry*)));
612
connect(group, SIGNAL(entryRemoved(Entry*)), SLOT(entryRemoved()));
613
connect(group, SIGNAL(entryAboutToMoveUp(int)), SLOT(entryAboutToMoveUp(int)));
614
connect(group, SIGNAL(entryMovedUp()), SLOT(entryMovedUp()));
615
connect(group, SIGNAL(entryAboutToMoveDown(int)), SLOT(entryAboutToMoveDown(int)));
616
connect(group, SIGNAL(entryMovedDown()), SLOT(entryMovedDown()));
617
connect(group, SIGNAL(entryDataChanged(Entry*)), SLOT(entryDataChanged(Entry*)));
619
void EntryModel::setBackgroundColorVisible(bool visible)
621
m_backgroundColorVisible = visible;