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"
|
|
|
|
|
2012-04-26 10:35:13 -04:00
|
|
|
#include <QtCore/QMimeData>
|
2012-05-15 13:58:10 -04:00
|
|
|
#include <QtGui/QFont>
|
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"
|
|
|
|
#include "core/Group.h"
|
|
|
|
|
|
|
|
EntryModel::EntryModel(QObject* parent)
|
|
|
|
: QAbstractTableModel(parent)
|
2012-06-29 08:15:16 -04:00
|
|
|
, m_group(Q_NULLPTR)
|
2010-08-18 10:22:48 -04:00
|
|
|
{
|
2012-04-26 10:35:13 -04:00
|
|
|
setSupportedDragActions(Qt::MoveAction);
|
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);
|
|
|
|
return index(row, 0);
|
|
|
|
}
|
|
|
|
|
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();
|
2012-07-21 12:35:24 -04:00
|
|
|
Q_EMIT switchedToGroupMode();
|
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();
|
|
|
|
|
2012-06-29 08:15:16 -04:00
|
|
|
m_group = Q_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;
|
|
|
|
|
|
|
|
Q_FOREACH (Entry* entry, m_entries) {
|
|
|
|
databases.insert(entry->group()->database());
|
|
|
|
}
|
|
|
|
|
|
|
|
Q_FOREACH (Database* db, databases) {
|
|
|
|
m_allGroups.append(db->rootGroup()->groupsRecursive(true));
|
2012-05-12 07:22:41 -04:00
|
|
|
}
|
|
|
|
|
2012-05-14 11:56:43 -04:00
|
|
|
Q_FOREACH (const Group* group, m_allGroups) {
|
2012-05-12 07:22:41 -04:00
|
|
|
makeConnections(group);
|
|
|
|
}
|
|
|
|
|
|
|
|
endResetModel();
|
2012-07-21 12:35:24 -04:00
|
|
|
Q_EMIT switchedToEntryListMode();
|
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
|
|
|
|
{
|
|
|
|
Q_UNUSED(parent);
|
|
|
|
|
2012-05-12 07:22:41 -04:00
|
|
|
return 4;
|
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);
|
2010-08-18 10:22:48 -04:00
|
|
|
|
|
|
|
if (role == Qt::DisplayRole) {
|
2010-09-21 17:01:56 -04:00
|
|
|
switch (index.column()) {
|
|
|
|
case 0:
|
2012-05-12 07:22:41 -04:00
|
|
|
if (entry->group()) {
|
|
|
|
return entry->group()->name();
|
|
|
|
}
|
2012-05-14 10:45:32 -04:00
|
|
|
break;
|
2010-09-21 17:01:56 -04:00
|
|
|
case 1:
|
2012-05-12 07:22:41 -04:00
|
|
|
return entry->title();
|
2010-09-21 17:01:56 -04:00
|
|
|
case 2:
|
2012-05-12 07:22:41 -04:00
|
|
|
return entry->username();
|
|
|
|
case 3:
|
2010-09-21 17:01:56 -04:00
|
|
|
return entry->url();
|
|
|
|
}
|
2010-08-18 10:22:48 -04:00
|
|
|
}
|
2012-05-12 07:22:41 -04:00
|
|
|
else if (role == Qt::DecorationRole) {
|
|
|
|
switch (index.column()) {
|
|
|
|
case 0:
|
|
|
|
if (entry->group()) {
|
|
|
|
return entry->group()->iconPixmap();
|
|
|
|
}
|
2012-05-14 10:45:32 -04:00
|
|
|
break;
|
2012-05-12 07:22:41 -04:00
|
|
|
case 1:
|
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 {
|
|
|
|
return entry->iconPixmap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
2010-09-21 17:01:56 -04:00
|
|
|
|
|
|
|
return QVariant();
|
2010-08-18 10:22:48 -04:00
|
|
|
}
|
|
|
|
QVariant EntryModel::headerData(int section, Qt::Orientation orientation, int role) const
|
|
|
|
{
|
|
|
|
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
|
2010-09-21 17:01:56 -04:00
|
|
|
switch (section) {
|
|
|
|
case 0:
|
2012-05-12 07:22:41 -04:00
|
|
|
return tr("Group");
|
2010-09-21 17:01:56 -04:00
|
|
|
case 1:
|
2012-05-12 07:22:41 -04:00
|
|
|
return tr("Title");
|
2010-09-21 17:01:56 -04:00
|
|
|
case 2:
|
2012-05-12 07:22:41 -04:00
|
|
|
return tr("Username");
|
|
|
|
case 3:
|
2010-09-21 17:01:56 -04:00
|
|
|
return tr("URL");
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
QMimeData* data = new QMimeData();
|
|
|
|
QByteArray encoded;
|
|
|
|
QDataStream stream(&encoded, QIODevice::WriteOnly);
|
|
|
|
|
|
|
|
for (int i = 0; i < indexes.size(); i++) {
|
|
|
|
if (!indexes[i].isValid()) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-05-12 07:22:41 -04:00
|
|
|
Entry* entry = entryFromIndex(indexes[i]);
|
|
|
|
stream << entry->group()->database()->uuid() << entry->uuid();
|
2012-04-26 10:35:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
data->setData(mimeTypes().first(), encoded);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
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);
|
2010-08-18 10:22:48 -04:00
|
|
|
Q_EMIT dataChanged(index(row, 0), index(row, columnCount()-1));
|
|
|
|
}
|
2012-05-12 07:22:41 -04:00
|
|
|
|
|
|
|
void EntryModel::severConnections()
|
|
|
|
{
|
|
|
|
if (m_group) {
|
2012-06-29 08:15:16 -04:00
|
|
|
disconnect(m_group, Q_NULLPTR, this, Q_NULLPTR);
|
2012-05-12 07:22:41 -04:00
|
|
|
}
|
|
|
|
|
2012-05-14 11:56:43 -04:00
|
|
|
Q_FOREACH (const Group* group, m_allGroups) {
|
2012-06-29 08:15:16 -04:00
|
|
|
disconnect(group, Q_NULLPTR, this, Q_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*)));
|
|
|
|
}
|