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>
|
|
|
|
|
2010-08-18 10:22:48 -04:00
|
|
|
#include "core/Entry.h"
|
|
|
|
#include "core/Group.h"
|
|
|
|
|
|
|
|
EntryModel::EntryModel(QObject* parent)
|
|
|
|
: QAbstractTableModel(parent)
|
|
|
|
, m_group(0)
|
|
|
|
{
|
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
|
|
|
|
{
|
|
|
|
Q_ASSERT(index.isValid() && index.row() < m_group->entries().size());
|
|
|
|
return m_group->entries().at(index.row());
|
|
|
|
}
|
|
|
|
|
2012-05-02 13:33:37 -04:00
|
|
|
QModelIndex EntryModel::indexFromEntry(Entry* entry) const
|
|
|
|
{
|
|
|
|
int row = m_group->entries().indexOf(entry);
|
|
|
|
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
|
|
|
{
|
|
|
|
beginResetModel();
|
|
|
|
|
|
|
|
if (m_group) {
|
|
|
|
disconnect(m_group, 0, this, 0);
|
|
|
|
}
|
|
|
|
m_group = group;
|
2010-08-23 15:30:20 -04:00
|
|
|
connect(group, SIGNAL(entryAboutToAdd(Entry*)), SLOT(entryAboutToAdd(Entry*)));
|
2010-08-18 10:22:48 -04:00
|
|
|
connect(group, SIGNAL(entryAdded()), SLOT(entryAdded()));
|
2010-08-23 15:30:20 -04:00
|
|
|
connect(group, SIGNAL(entryAboutToRemove(Entry*)), SLOT(entryAboutToRemove(Entry*)));
|
2010-08-18 10:22:48 -04:00
|
|
|
connect(group, SIGNAL(entryRemoved()), SLOT(entryRemoved()));
|
2010-08-23 15:30:20 -04:00
|
|
|
connect(group, SIGNAL(entryDataChanged(Entry*)), SLOT(entryDataChanged(Entry*)));
|
2010-08-18 10:22:48 -04:00
|
|
|
|
|
|
|
endResetModel();
|
|
|
|
}
|
|
|
|
|
|
|
|
int EntryModel::rowCount(const QModelIndex& parent) const
|
|
|
|
{
|
|
|
|
if (!m_group || parent.isValid()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return m_group->entries().size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int EntryModel::columnCount(const QModelIndex& parent) const
|
|
|
|
{
|
|
|
|
Q_UNUSED(parent);
|
|
|
|
|
2010-09-21 17:01:56 -04:00
|
|
|
return 3;
|
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:
|
|
|
|
return entry->title();
|
|
|
|
case 1:
|
|
|
|
return entry->username();
|
|
|
|
case 2:
|
|
|
|
return entry->url();
|
|
|
|
}
|
2010-08-18 10:22:48 -04:00
|
|
|
}
|
2010-09-21 17:01:56 -04:00
|
|
|
else if ((role == Qt::DecorationRole) && (index.column() == 0)) {
|
2012-01-01 15:52:54 -05:00
|
|
|
return entry->iconPixmap();
|
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:
|
|
|
|
return tr("Title");
|
|
|
|
case 1:
|
|
|
|
return tr("Username");
|
|
|
|
case 2:
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
stream << m_group->database()->uuid() << entryFromIndex(indexes[i])->uuid();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
Q_UNUSED(entry);
|
|
|
|
|
|
|
|
beginInsertRows(QModelIndex(), m_group->entries().size(), m_group->entries().size());
|
|
|
|
}
|
|
|
|
|
|
|
|
void EntryModel::entryAdded()
|
|
|
|
{
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
2010-08-23 15:30:20 -04:00
|
|
|
void EntryModel::entryAboutToRemove(Entry* entry)
|
2010-08-18 10:22:48 -04:00
|
|
|
{
|
|
|
|
beginRemoveRows(QModelIndex(), m_group->entries().indexOf(entry), m_group->entries().indexOf(entry));
|
|
|
|
}
|
|
|
|
|
|
|
|
void EntryModel::entryRemoved()
|
|
|
|
{
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
|
|
|
|
2010-08-23 15:30:20 -04:00
|
|
|
void EntryModel::entryDataChanged(Entry* entry)
|
2010-08-18 10:22:48 -04:00
|
|
|
{
|
|
|
|
int row = m_group->entries().indexOf(entry);
|
|
|
|
Q_EMIT dataChanged(index(row, 0), index(row, columnCount()-1));
|
|
|
|
}
|