2010-08-18 10:22:48 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 or (at your option)
|
|
|
|
* version 3 of the License.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "EntryModel.h"
|
|
|
|
|
2013-10-03 09:18:16 -04:00
|
|
|
#include <QFont>
|
|
|
|
#include <QMimeData>
|
2017-03-05 17:47:08 -05:00
|
|
|
#include <QPalette>
|
2017-12-19 04:47:51 -05:00
|
|
|
/**
|
|
|
|
* @author Fonic <https://github.com/fonic>
|
|
|
|
* Add include required for additional columns 'Expires', 'Created', 'Modified'
|
|
|
|
* and 'Accessed'
|
|
|
|
*/
|
|
|
|
#include <QDateTime>
|
2012-04-26 10:35:13 -04:00
|
|
|
|
2012-05-15 13:58:10 -04:00
|
|
|
#include "core/DatabaseIcons.h"
|
2010-08-18 10:22:48 -04:00
|
|
|
#include "core/Entry.h"
|
2016-09-02 13:51:51 -04:00
|
|
|
#include "core/Global.h"
|
2010-08-18 10:22:48 -04:00
|
|
|
#include "core/Group.h"
|
2013-10-08 15:13:20 -04:00
|
|
|
#include "core/Metadata.h"
|
2010-08-18 10:22:48 -04:00
|
|
|
|
2017-12-19 04:47:51 -05:00
|
|
|
/**
|
|
|
|
* @author Fonic <https://github.com/fonic>
|
|
|
|
* Define constant string used to display hidden content in columns 'Username'
|
|
|
|
* and 'Password'
|
|
|
|
*
|
|
|
|
* TODO:
|
|
|
|
* Decide which of the proposed options should be used (stars, bullet, black
|
|
|
|
* circle)
|
|
|
|
*/
|
|
|
|
//const QString EntryModel::HiddenContent("******");
|
|
|
|
//const QString EntryModel::HiddenContent(QString(QChar(0x2022)).repeated(6));
|
|
|
|
const QString EntryModel::HiddenContent(QString(QChar(0x25CF)).repeated(6));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Fonic <https://github.com/fonic>
|
|
|
|
* Define date format used to display dates in columns 'Expires', 'Created',
|
|
|
|
* 'Modified' and 'Accessed'
|
|
|
|
*/
|
|
|
|
const Qt::DateFormat EntryModel::DateFormat = Qt::DefaultLocaleShortDate;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Fonic <https://github.com/fonic>
|
|
|
|
* Initialize 'Hide Usernames' and 'Hide Passwords' settings using sane
|
|
|
|
* defaults (usernames visible, passwords hidden)
|
|
|
|
*/
|
2010-08-18 10:22:48 -04:00
|
|
|
EntryModel::EntryModel(QObject* parent)
|
|
|
|
: QAbstractTableModel(parent)
|
2015-07-24 12:28:12 -04:00
|
|
|
, m_group(nullptr)
|
2017-12-19 04:47:51 -05:00
|
|
|
, m_hideUsernames(false)
|
|
|
|
, m_hidePasswords(true)
|
2010-08-18 10:22:48 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-09-19 13:45:14 -04:00
|
|
|
Entry* EntryModel::entryFromIndex(const QModelIndex& index) const
|
|
|
|
{
|
2012-05-12 07:22:41 -04:00
|
|
|
Q_ASSERT(index.isValid() && index.row() < m_entries.size());
|
|
|
|
return m_entries.at(index.row());
|
2010-09-19 13:45:14 -04:00
|
|
|
}
|
|
|
|
|
2012-05-02 13:33:37 -04:00
|
|
|
QModelIndex EntryModel::indexFromEntry(Entry* entry) const
|
|
|
|
{
|
2012-05-12 07:22:41 -04:00
|
|
|
int row = m_entries.indexOf(entry);
|
2012-05-02 13:33:37 -04:00
|
|
|
Q_ASSERT(row != -1);
|
2012-07-22 15:56:31 -04:00
|
|
|
return index(row, 1);
|
2012-05-02 13:33:37 -04:00
|
|
|
}
|
|
|
|
|
2010-08-23 15:30:20 -04:00
|
|
|
void EntryModel::setGroup(Group* group)
|
2010-08-18 10:22:48 -04:00
|
|
|
{
|
2012-05-12 07:22:41 -04:00
|
|
|
if (!group || group == m_group) {
|
2012-05-13 08:53:08 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-08-18 10:22:48 -04:00
|
|
|
beginResetModel();
|
|
|
|
|
2012-05-12 07:22:41 -04:00
|
|
|
severConnections();
|
|
|
|
|
2010-08-18 10:22:48 -04:00
|
|
|
m_group = group;
|
2012-07-21 12:19:40 -04:00
|
|
|
m_allGroups.clear();
|
2012-05-12 07:22:41 -04:00
|
|
|
m_entries = group->entries();
|
2012-07-21 12:19:40 -04:00
|
|
|
m_orgEntries.clear();
|
2012-05-12 07:22:41 -04:00
|
|
|
|
|
|
|
makeConnections(group);
|
2010-08-18 10:22:48 -04:00
|
|
|
|
|
|
|
endResetModel();
|
2017-12-23 02:40:00 -05:00
|
|
|
emit switchedToListMode();
|
2012-05-12 07:22:41 -04:00
|
|
|
}
|
|
|
|
|
2012-07-21 12:35:24 -04:00
|
|
|
void EntryModel::setEntryList(const QList<Entry*>& entries)
|
2012-05-12 07:22:41 -04:00
|
|
|
{
|
|
|
|
beginResetModel();
|
|
|
|
|
|
|
|
severConnections();
|
|
|
|
|
2015-07-24 12:28:12 -04:00
|
|
|
m_group = nullptr;
|
2012-05-12 07:22:41 -04:00
|
|
|
m_allGroups.clear();
|
|
|
|
m_entries = entries;
|
2012-07-21 12:19:40 -04:00
|
|
|
m_orgEntries = entries;
|
2012-05-12 07:22:41 -04:00
|
|
|
|
2012-07-21 12:39:18 -04:00
|
|
|
QSet<Database*> databases;
|
|
|
|
|
2016-09-02 13:51:51 -04:00
|
|
|
for (Entry* entry : asConst(m_entries)) {
|
2012-07-21 12:39:18 -04:00
|
|
|
databases.insert(entry->group()->database());
|
|
|
|
}
|
|
|
|
|
2016-09-02 13:51:51 -04:00
|
|
|
for (Database* db : asConst(databases)) {
|
2012-07-22 15:56:31 -04:00
|
|
|
Q_ASSERT(db);
|
2016-09-02 13:51:51 -04:00
|
|
|
const QList<Group*> groupList = db->rootGroup()->groupsRecursive(true);
|
|
|
|
for (const Group* group : groupList) {
|
2015-01-11 10:20:24 -05:00
|
|
|
m_allGroups.append(group);
|
|
|
|
}
|
|
|
|
|
2013-10-08 15:13:20 -04:00
|
|
|
if (db->metadata()->recycleBin()) {
|
|
|
|
m_allGroups.removeOne(db->metadata()->recycleBin());
|
|
|
|
}
|
2012-05-12 07:22:41 -04:00
|
|
|
}
|
|
|
|
|
2016-09-02 13:51:51 -04:00
|
|
|
for (const Group* group : asConst(m_allGroups)) {
|
2012-05-12 07:22:41 -04:00
|
|
|
makeConnections(group);
|
|
|
|
}
|
|
|
|
|
|
|
|
endResetModel();
|
2017-12-23 02:40:00 -05:00
|
|
|
emit switchedToSearchMode();
|
2010-08-18 10:22:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int EntryModel::rowCount(const QModelIndex& parent) const
|
|
|
|
{
|
2012-05-12 07:22:41 -04:00
|
|
|
if (parent.isValid()) {
|
2010-08-18 10:22:48 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else {
|
2012-05-12 07:22:41 -04:00
|
|
|
return m_entries.size();
|
2010-08-18 10:22:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int EntryModel::columnCount(const QModelIndex& parent) const
|
|
|
|
{
|
2017-12-19 04:47:51 -05:00
|
|
|
/**
|
|
|
|
* @author Fonic <https://github.com/fonic>
|
|
|
|
* Change column count to include additional columns 'Password', 'Notes',
|
|
|
|
* 'Expires', 'Created', 'Modified', 'Accessed' and 'Attachments'. Also,
|
|
|
|
* return 0 when parent is valid as advised by Qt documentation
|
|
|
|
*/
|
|
|
|
if (parent.isValid()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 11;
|
|
|
|
}
|
2010-08-18 10:22:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant EntryModel::data(const QModelIndex& index, int role) const
|
|
|
|
{
|
|
|
|
if (!index.isValid()) {
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2010-09-19 13:45:14 -04:00
|
|
|
Entry* entry = entryFromIndex(index);
|
2017-03-05 17:47:08 -05:00
|
|
|
EntryAttributes* attr = entry->attributes();
|
2010-08-18 10:22:48 -04:00
|
|
|
|
2017-12-19 04:47:51 -05:00
|
|
|
/**
|
|
|
|
* @author Fonic <https://github.com/fonic>
|
|
|
|
*
|
|
|
|
* Add display data providers for additional columns 'Password', 'Notes',
|
|
|
|
* 'Expires', 'Created', 'Modified', 'Accessed' and 'Attachments'
|
|
|
|
*
|
|
|
|
* Add ability to display usernames and passwords hidden or visible
|
|
|
|
* depending on current state of 'Hide Usernames' and 'Hide Passwords'
|
|
|
|
* settings
|
|
|
|
*
|
|
|
|
* TODO:
|
|
|
|
* Decide which of the additional columns should expand placeholders
|
|
|
|
* -> code added where applicable, but currently commented out
|
|
|
|
*
|
|
|
|
* Check what attr->isReference() does and if it applies to any of the
|
|
|
|
* additional columns
|
|
|
|
* -> code added for columns 'Password' and 'Notes', as EntryAttributes::
|
|
|
|
* PasswordKey and EntryAttributes::NotesKey already existed
|
|
|
|
*/
|
2010-08-18 10:22:48 -04:00
|
|
|
if (role == Qt::DisplayRole) {
|
2017-03-05 17:47:08 -05:00
|
|
|
QString result;
|
2010-09-21 17:01:56 -04:00
|
|
|
switch (index.column()) {
|
2013-04-07 15:17:08 -04:00
|
|
|
case ParentGroup:
|
2012-05-12 07:22:41 -04:00
|
|
|
if (entry->group()) {
|
|
|
|
return entry->group()->name();
|
|
|
|
}
|
2012-05-14 10:45:32 -04:00
|
|
|
break;
|
2013-04-07 15:17:08 -04:00
|
|
|
case Title:
|
2017-03-16 15:31:14 -04:00
|
|
|
result = entry->resolveMultiplePlaceholders(entry->title());
|
2017-03-05 17:47:08 -05:00
|
|
|
if (attr->isReference(EntryAttributes::TitleKey)) {
|
|
|
|
result.prepend(tr("Ref: ","Reference abbreviation"));
|
|
|
|
}
|
|
|
|
return result;
|
2013-04-07 15:17:08 -04:00
|
|
|
case Username:
|
2017-12-19 04:47:51 -05:00
|
|
|
/*
|
|
|
|
* Display usernames hidden or visible according to current state
|
|
|
|
* of 'Hide Usernames' setting
|
|
|
|
*/
|
|
|
|
if (m_hideUsernames) {
|
|
|
|
result = EntryModel::HiddenContent;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
//result = entry->username();
|
|
|
|
result = entry->resolveMultiplePlaceholders(entry->username());
|
|
|
|
}
|
2017-03-05 17:47:08 -05:00
|
|
|
if (attr->isReference(EntryAttributes::UserNameKey)) {
|
|
|
|
result.prepend(tr("Ref: ","Reference abbreviation"));
|
|
|
|
}
|
|
|
|
return result;
|
2017-12-19 04:47:51 -05:00
|
|
|
case Password:
|
|
|
|
/*
|
|
|
|
* Display passwords hidden or visible according to current state
|
|
|
|
* of 'Hide Passwords' setting
|
|
|
|
*/
|
|
|
|
if (m_hidePasswords) {
|
|
|
|
result = EntryModel::HiddenContent;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
//result = entry->resolveMultiplePlaceholders(entry->password());
|
|
|
|
result = entry->password();
|
|
|
|
}
|
|
|
|
if (attr->isReference(EntryAttributes::PasswordKey)) {
|
|
|
|
result.prepend(tr("Ref: ","Reference abbreviation"));
|
|
|
|
}
|
|
|
|
return result;
|
2013-04-07 15:17:08 -04:00
|
|
|
case Url:
|
2017-12-19 04:47:51 -05:00
|
|
|
//result = entry->resolveMultiplePlaceholders(entry->displayUrl());
|
2017-10-25 23:29:01 -04:00
|
|
|
result = entry->displayUrl();
|
2017-03-05 17:47:08 -05:00
|
|
|
if (attr->isReference(EntryAttributes::URLKey)) {
|
|
|
|
result.prepend(tr("Ref: ","Reference abbreviation"));
|
|
|
|
}
|
|
|
|
return result;
|
2017-12-19 04:47:51 -05:00
|
|
|
case Notes:
|
|
|
|
/*
|
|
|
|
* Display only first line of notes in simplified format like
|
|
|
|
* KeePassX does
|
|
|
|
*/
|
|
|
|
//result = entry->resolveMultiplePlaceholders(entry->notes().section("\n", 0, 0).simplified());
|
|
|
|
result = entry->notes().section("\n", 0, 0).simplified();
|
|
|
|
if (attr->isReference(EntryAttributes::NotesKey)) {
|
|
|
|
result.prepend(tr("Ref: ","Reference abbreviation"));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
case Expires:
|
|
|
|
/*
|
|
|
|
* Display either date of expiry or 'Never' like KeePassX does
|
|
|
|
*/
|
|
|
|
result = entry->timeInfo().expires() ? entry->timeInfo().expiryTime().toLocalTime().toString(EntryModel::DateFormat) : tr("Never");
|
|
|
|
return result;
|
|
|
|
case Created:
|
|
|
|
result = entry->timeInfo().creationTime().toLocalTime().toString(EntryModel::DateFormat);
|
|
|
|
return result;
|
|
|
|
case Modified:
|
|
|
|
result = entry->timeInfo().lastModificationTime().toLocalTime().toString(EntryModel::DateFormat);
|
|
|
|
return result;
|
|
|
|
case Accessed:
|
|
|
|
result = entry->timeInfo().lastAccessTime().toLocalTime().toString(EntryModel::DateFormat);
|
|
|
|
return result;
|
|
|
|
case Attachments:
|
|
|
|
/*
|
|
|
|
* Display comma-separated list of attachments
|
|
|
|
*
|
|
|
|
* TODO:
|
|
|
|
* 'entry->attachments()->keys().join()' works locally, yet it fails
|
|
|
|
* on GitHub/Travis CI, most likely due to an older Qt version, thus
|
|
|
|
* using loop for now (http://doc.qt.io/qt-5/qlist.html#more-members)
|
|
|
|
*/
|
|
|
|
//result = entry->resolveMultiplePlaceholders(entry->attachments()->keys().join(", "));
|
|
|
|
//result = entry->attachments()->keys().join(", ");
|
|
|
|
|
|
|
|
QList<QString> attachments = entry->attachments()->keys();
|
|
|
|
for (int i=0; i < attachments.size(); i++) {
|
|
|
|
if (result.isEmpty()) {
|
|
|
|
result.append(attachments.at(i));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result.append(QString(", ") + attachments.at(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//result = entry->resolveMultiplePlaceholders(result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @author Fonic <https://github.com/fonic>
|
|
|
|
*
|
|
|
|
* Add sort data providers for columns 'Username' and 'Password', required
|
|
|
|
* for correct sorting even if displayed hidden (i.e. settings 'Hide User-
|
|
|
|
* names' and/or 'Hide Passwords' are enabled)
|
|
|
|
*
|
|
|
|
* Add sort data providers for columns 'Expires', 'Created', 'Modified'
|
|
|
|
* and 'Accessed', required for correct sorting of dates (without this,
|
|
|
|
* sorting would be based on string representation of dates, yielding un-
|
|
|
|
* desired results)
|
|
|
|
*
|
|
|
|
* NOTE:
|
|
|
|
* Qt::UserRole is used as sort role, using 'm_sortModel->setSortRole(Qt::
|
|
|
|
* UserRole)' in EntryView.cpp, EntryView::EntryView()
|
|
|
|
*/
|
|
|
|
else if (role == Qt::UserRole) {
|
|
|
|
switch (index.column()) {
|
|
|
|
case Username:
|
|
|
|
//return entry->username();
|
|
|
|
return entry->resolveMultiplePlaceholders(entry->username());
|
|
|
|
case Password:
|
|
|
|
//return entry->resolveMultiplePlaceholders(entry->password());
|
|
|
|
return entry->password();
|
|
|
|
case Expires:
|
|
|
|
/*
|
|
|
|
* TODO:
|
|
|
|
* Is there any better way to return a QDateTime representing
|
|
|
|
* 'Never' / infinity / end of all time?
|
|
|
|
*/
|
|
|
|
return entry->timeInfo().expires() ? entry->timeInfo().expiryTime() : QDateTime(QDate(9999, 1, 1));
|
|
|
|
case Created:
|
|
|
|
return entry->timeInfo().creationTime();
|
|
|
|
case Modified:
|
|
|
|
return entry->timeInfo().lastModificationTime();
|
|
|
|
case Accessed:
|
|
|
|
return entry->timeInfo().lastAccessTime();
|
|
|
|
default:
|
|
|
|
/*
|
|
|
|
* For all other columns, simply use data provided by Qt::Display-
|
|
|
|
* Role for sorting
|
|
|
|
*/
|
|
|
|
return data(index, Qt::DisplayRole);
|
2010-09-21 17:01:56 -04:00
|
|
|
}
|
2010-08-18 10:22:48 -04:00
|
|
|
}
|
2012-05-12 07:22:41 -04:00
|
|
|
else if (role == Qt::DecorationRole) {
|
|
|
|
switch (index.column()) {
|
2013-04-07 15:17:08 -04:00
|
|
|
case ParentGroup:
|
2012-05-12 07:22:41 -04:00
|
|
|
if (entry->group()) {
|
2016-01-24 13:03:50 -05:00
|
|
|
return entry->group()->iconScaledPixmap();
|
2012-05-12 07:22:41 -04:00
|
|
|
}
|
2012-05-14 10:45:32 -04:00
|
|
|
break;
|
2013-04-07 15:17:08 -04:00
|
|
|
case Title:
|
2012-05-15 13:58:10 -04:00
|
|
|
if (entry->isExpired()) {
|
2012-06-29 09:22:43 -04:00
|
|
|
return databaseIcons()->iconPixmap(DatabaseIcons::ExpiredIconIndex);
|
2012-05-15 13:58:10 -04:00
|
|
|
}
|
|
|
|
else {
|
2016-01-24 13:03:50 -05:00
|
|
|
return entry->iconScaledPixmap();
|
2012-05-15 13:58:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (role == Qt::FontRole) {
|
|
|
|
QFont font;
|
|
|
|
if (entry->isExpired()) {
|
|
|
|
font.setStrikeOut(true);
|
2012-05-12 07:22:41 -04:00
|
|
|
}
|
2012-05-15 13:58:10 -04:00
|
|
|
return font;
|
2010-09-19 15:22:24 -04:00
|
|
|
}
|
2017-03-05 17:47:08 -05:00
|
|
|
else if (role == Qt::TextColorRole) {
|
|
|
|
if (entry->hasReferences()) {
|
|
|
|
QPalette p;
|
|
|
|
return QVariant(p.color(QPalette::Active, QPalette::Mid));
|
|
|
|
}
|
|
|
|
}
|
2010-09-21 17:01:56 -04:00
|
|
|
|
|
|
|
return QVariant();
|
2010-08-18 10:22:48 -04:00
|
|
|
}
|
2017-12-19 04:47:51 -05:00
|
|
|
|
2010-08-18 10:22:48 -04:00
|
|
|
QVariant EntryModel::headerData(int section, Qt::Orientation orientation, int role) const
|
|
|
|
{
|
2017-12-19 04:47:51 -05:00
|
|
|
/**
|
|
|
|
* @author Fonic <https://github.com/fonic>
|
|
|
|
* Add captions for additional columns 'Password', 'Notes', 'Expires',
|
|
|
|
* 'Created', 'Modified', 'Accessed' and 'Attachments'
|
|
|
|
*/
|
2010-08-18 10:22:48 -04:00
|
|
|
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
|
2010-09-21 17:01:56 -04:00
|
|
|
switch (section) {
|
2013-04-07 15:17:08 -04:00
|
|
|
case ParentGroup:
|
2012-05-12 07:22:41 -04:00
|
|
|
return tr("Group");
|
2013-04-07 15:17:08 -04:00
|
|
|
case Title:
|
2012-05-12 07:22:41 -04:00
|
|
|
return tr("Title");
|
2013-04-07 15:17:08 -04:00
|
|
|
case Username:
|
2012-05-12 07:22:41 -04:00
|
|
|
return tr("Username");
|
2017-12-19 04:47:51 -05:00
|
|
|
case Password:
|
|
|
|
return tr("Password");
|
2013-04-07 15:17:08 -04:00
|
|
|
case Url:
|
2010-09-21 17:01:56 -04:00
|
|
|
return tr("URL");
|
2017-12-19 04:47:51 -05:00
|
|
|
case Notes:
|
|
|
|
return tr("Notes");
|
|
|
|
case Expires:
|
|
|
|
return tr("Expires");
|
|
|
|
case Created:
|
|
|
|
return tr("Created");
|
|
|
|
case Modified:
|
|
|
|
return tr("Modified");
|
|
|
|
case Accessed:
|
|
|
|
return tr("Accessed");
|
|
|
|
case Attachments:
|
|
|
|
return tr("Attachments");
|
2010-09-21 17:01:56 -04:00
|
|
|
}
|
2010-08-18 10:22:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2012-04-26 10:35:13 -04:00
|
|
|
Qt::DropActions EntryModel::supportedDropActions() const
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-07-22 11:03:36 -04:00
|
|
|
Qt::DropActions EntryModel::supportedDragActions() const
|
|
|
|
{
|
|
|
|
return (Qt::MoveAction | Qt::CopyAction);
|
|
|
|
}
|
|
|
|
|
2012-04-26 10:35:13 -04:00
|
|
|
Qt::ItemFlags EntryModel::flags(const QModelIndex& modelIndex) const
|
|
|
|
{
|
|
|
|
if (!modelIndex.isValid()) {
|
|
|
|
return Qt::NoItemFlags;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return QAbstractItemModel::flags(modelIndex) | Qt::ItemIsDragEnabled;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList EntryModel::mimeTypes() const
|
|
|
|
{
|
|
|
|
QStringList types;
|
|
|
|
types << QLatin1String("application/x-keepassx-entry");
|
|
|
|
return types;
|
|
|
|
}
|
|
|
|
|
|
|
|
QMimeData* EntryModel::mimeData(const QModelIndexList& indexes) const
|
|
|
|
{
|
|
|
|
if (indexes.isEmpty()) {
|
2015-07-24 12:28:12 -04:00
|
|
|
return nullptr;
|
2012-04-26 10:35:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
QMimeData* data = new QMimeData();
|
|
|
|
QByteArray encoded;
|
|
|
|
QDataStream stream(&encoded, QIODevice::WriteOnly);
|
|
|
|
|
2013-04-07 05:47:50 -04:00
|
|
|
QSet<Entry*> seenEntries;
|
|
|
|
|
2016-09-02 13:51:51 -04:00
|
|
|
for (const QModelIndex& index : indexes) {
|
2013-04-07 05:52:55 -04:00
|
|
|
if (!index.isValid()) {
|
2012-04-26 10:35:13 -04:00
|
|
|
continue;
|
|
|
|
}
|
2013-04-07 05:47:50 -04:00
|
|
|
|
2013-04-07 05:52:55 -04:00
|
|
|
Entry* entry = entryFromIndex(index);
|
2013-04-07 05:47:50 -04:00
|
|
|
if (!seenEntries.contains(entry)) {
|
|
|
|
// make sure we don't add entries multiple times when we get indexes
|
|
|
|
// with the same row but different columns
|
|
|
|
stream << entry->group()->database()->uuid() << entry->uuid();
|
|
|
|
seenEntries.insert(entry);
|
|
|
|
}
|
2012-04-26 10:35:13 -04:00
|
|
|
}
|
|
|
|
|
2013-04-07 05:50:42 -04:00
|
|
|
if (seenEntries.isEmpty()) {
|
|
|
|
delete data;
|
2015-07-24 12:28:12 -04:00
|
|
|
return nullptr;
|
2013-04-07 05:50:42 -04:00
|
|
|
}
|
|
|
|
else {
|
2015-09-25 15:33:55 -04:00
|
|
|
data->setData(mimeTypes().at(0), encoded);
|
2013-04-07 05:50:42 -04:00
|
|
|
return data;
|
|
|
|
}
|
2012-04-26 10:35:13 -04:00
|
|
|
}
|
|
|
|
|
2010-08-23 15:30:20 -04:00
|
|
|
void EntryModel::entryAboutToAdd(Entry* entry)
|
2010-08-18 10:22:48 -04:00
|
|
|
{
|
2012-07-21 12:19:40 -04:00
|
|
|
if (!m_group && !m_orgEntries.contains(entry)) {
|
|
|
|
return;
|
|
|
|
}
|
2010-08-18 10:22:48 -04:00
|
|
|
|
2012-05-12 07:22:41 -04:00
|
|
|
beginInsertRows(QModelIndex(), m_entries.size(), m_entries.size());
|
|
|
|
if (!m_group) {
|
|
|
|
m_entries.append(entry);
|
|
|
|
}
|
2010-08-18 10:22:48 -04:00
|
|
|
}
|
|
|
|
|
2012-07-21 12:19:40 -04:00
|
|
|
void EntryModel::entryAdded(Entry* entry)
|
2010-08-18 10:22:48 -04:00
|
|
|
{
|
2012-07-21 12:19:40 -04:00
|
|
|
if (!m_group && !m_orgEntries.contains(entry)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-05-12 07:22:41 -04:00
|
|
|
if (m_group) {
|
|
|
|
m_entries = m_group->entries();
|
|
|
|
}
|
2010-08-18 10:22:48 -04:00
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
2010-08-23 15:30:20 -04:00
|
|
|
void EntryModel::entryAboutToRemove(Entry* entry)
|
2010-08-18 10:22:48 -04:00
|
|
|
{
|
2012-05-12 07:22:41 -04:00
|
|
|
beginRemoveRows(QModelIndex(), m_entries.indexOf(entry), m_entries.indexOf(entry));
|
|
|
|
if (!m_group) {
|
|
|
|
m_entries.removeAll(entry);
|
|
|
|
}
|
2010-08-18 10:22:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void EntryModel::entryRemoved()
|
|
|
|
{
|
2012-05-12 07:22:41 -04:00
|
|
|
if (m_group) {
|
|
|
|
m_entries = m_group->entries();
|
|
|
|
}
|
|
|
|
|
2010-08-18 10:22:48 -04:00
|
|
|
endRemoveRows();
|
|
|
|
}
|
|
|
|
|
2010-08-23 15:30:20 -04:00
|
|
|
void EntryModel::entryDataChanged(Entry* entry)
|
2010-08-18 10:22:48 -04:00
|
|
|
{
|
2012-05-12 07:22:41 -04:00
|
|
|
int row = m_entries.indexOf(entry);
|
2017-03-10 09:58:42 -05:00
|
|
|
emit dataChanged(index(row, 0), index(row, columnCount()-1));
|
2010-08-18 10:22:48 -04:00
|
|
|
}
|
2012-05-12 07:22:41 -04:00
|
|
|
|
|
|
|
void EntryModel::severConnections()
|
|
|
|
{
|
|
|
|
if (m_group) {
|
2015-07-24 12:28:12 -04:00
|
|
|
disconnect(m_group, nullptr, this, nullptr);
|
2012-05-12 07:22:41 -04:00
|
|
|
}
|
|
|
|
|
2016-09-02 13:51:51 -04:00
|
|
|
for (const Group* group : asConst(m_allGroups)) {
|
2015-07-24 12:28:12 -04:00
|
|
|
disconnect(group, nullptr, this, nullptr);
|
2012-05-12 07:22:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-15 11:48:48 -04:00
|
|
|
void EntryModel::makeConnections(const Group* group)
|
2012-05-12 07:22:41 -04:00
|
|
|
{
|
|
|
|
connect(group, SIGNAL(entryAboutToAdd(Entry*)), SLOT(entryAboutToAdd(Entry*)));
|
2012-07-21 12:19:40 -04:00
|
|
|
connect(group, SIGNAL(entryAdded(Entry*)), SLOT(entryAdded(Entry*)));
|
2012-05-12 07:22:41 -04:00
|
|
|
connect(group, SIGNAL(entryAboutToRemove(Entry*)), SLOT(entryAboutToRemove(Entry*)));
|
2012-07-21 12:19:40 -04:00
|
|
|
connect(group, SIGNAL(entryRemoved(Entry*)), SLOT(entryRemoved()));
|
2012-05-12 07:22:41 -04:00
|
|
|
connect(group, SIGNAL(entryDataChanged(Entry*)), SLOT(entryDataChanged(Entry*)));
|
|
|
|
}
|
2017-12-19 04:47:51 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Fonic <https://github.com/fonic>
|
|
|
|
* Get current state of 'Hide Usernames' setting
|
|
|
|
*/
|
|
|
|
bool EntryModel::hideUsernames() const
|
|
|
|
{
|
|
|
|
return m_hideUsernames;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Fonic <https://github.com/fonic>
|
|
|
|
* Set state of 'Hide Usernames' setting and signal change
|
|
|
|
*/
|
|
|
|
void EntryModel::setHideUsernames(const bool hide)
|
|
|
|
{
|
|
|
|
m_hideUsernames = hide;
|
|
|
|
emit hideUsernamesChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Fonic <https://github.com/fonic>
|
|
|
|
* Get current state of 'Hide Passwords' setting
|
|
|
|
*/
|
|
|
|
bool EntryModel::hidePasswords() const
|
|
|
|
{
|
|
|
|
return m_hidePasswords;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Fonic <https://github.com/fonic>
|
|
|
|
* Set state of 'Hide Passwords' setting and signal change
|
|
|
|
*/
|
|
|
|
void EntryModel::setHidePasswords(const bool hide)
|
|
|
|
{
|
|
|
|
m_hidePasswords = hide;
|
|
|
|
emit hidePasswordsChanged();
|
|
|
|
}
|
2017-12-21 04:01:47 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Fonic <https://github.com/fonic>
|
|
|
|
* Toggle state of 'Hide Usernames' setting
|
|
|
|
*/
|
|
|
|
void EntryModel::toggleHideUsernames(const bool hide)
|
|
|
|
{
|
|
|
|
setHideUsernames(hide);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Fonic <https://github.com/fonic>
|
|
|
|
* Toggle state of 'Hide Passwords' setting
|
|
|
|
*/
|
|
|
|
void EntryModel::toggleHidePasswords(const bool hide)
|
|
|
|
{
|
|
|
|
setHidePasswords(hide);
|
|
|
|
}
|