1 | /* |
2 | Copyright (C) 2010 Klarälvdalens Datakonsult AB, |
3 | a KDAB Group company, info@kdab.net, |
4 | author Stephen Kelly <stephen@kdab.com> |
5 | |
6 | This library is free software; you can redistribute it and/or modify it |
7 | under the terms of the GNU Library General Public License as published by |
8 | the Free Software Foundation; either version 2 of the License, or (at your |
9 | option) any later version. |
10 | |
11 | This library is distributed in the hope that it will be useful, but WITHOUT |
12 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
13 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public |
14 | License for more details. |
15 | |
16 | You should have received a copy of the GNU Library General Public License |
17 | along with this library; see the file COPYING.LIB. If not, write to the |
18 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
19 | 02110-1301, USA. |
20 | */ |
21 | |
22 | #include "etmviewstatesaver.h" |
23 | |
24 | #include <QtCore/QModelIndex> |
25 | |
26 | #include "entitytreemodel.h" |
27 | |
28 | using namespace Akonadi; |
29 | |
30 | ETMViewStateSaver::ETMViewStateSaver(QObject *parent) |
31 | : KViewStateSaver(parent) |
32 | { |
33 | } |
34 | |
35 | QModelIndex ETMViewStateSaver::indexFromConfigString(const QAbstractItemModel *model, const QString &key) const |
36 | { |
37 | if (key.startsWith(QLatin1Char('x'))) { |
38 | return QModelIndex(); |
39 | } |
40 | |
41 | Entity::Id id = key.mid(1).toLongLong(); |
42 | if (id < 0) { |
43 | return QModelIndex(); |
44 | } |
45 | |
46 | if (key.startsWith(QLatin1Char('c'))) { |
47 | QModelIndex idx = EntityTreeModel::modelIndexForCollection(model, Collection(id)); |
48 | if (!idx.isValid()) { |
49 | return QModelIndex(); |
50 | } |
51 | return idx; |
52 | } else if (key.startsWith(QLatin1Char('i'))) { |
53 | QModelIndexList list = EntityTreeModel::modelIndexesForItem(model, Item(id)); |
54 | if (list.isEmpty()) { |
55 | return QModelIndex(); |
56 | } |
57 | return list.first(); |
58 | } |
59 | return QModelIndex(); |
60 | } |
61 | |
62 | QString ETMViewStateSaver::indexToConfigString(const QModelIndex &index) const |
63 | { |
64 | if (!index.isValid()) { |
65 | return QLatin1String("x-1" ); |
66 | } |
67 | const Collection c = index.data(EntityTreeModel::CollectionRole).value<Collection>(); |
68 | if (c.isValid()) { |
69 | return QString::fromLatin1("c%1" ).arg(c.id()); |
70 | } |
71 | Entity::Id id = index.data(EntityTreeModel::ItemIdRole).value<Entity::Id>(); |
72 | if (id >= 0) { |
73 | return QString::fromLatin1("i%1" ).arg(id); |
74 | } |
75 | return QString(); |
76 | } |
77 | |
78 | void ETMViewStateSaver::selectCollections(const Akonadi::Collection::List &list) |
79 | { |
80 | QStringList colStrings; |
81 | foreach (const Collection &col, list) { |
82 | colStrings << QString::fromLatin1("c%1" ).arg(col.id()); |
83 | } |
84 | restoreSelection(colStrings); |
85 | } |
86 | |
87 | void ETMViewStateSaver::selectCollections(const QList< Collection::Id > &list) |
88 | { |
89 | QStringList colStrings; |
90 | foreach (const Collection::Id &colId, list) { |
91 | colStrings << QString::fromLatin1("c%1" ).arg(colId); |
92 | } |
93 | restoreSelection(colStrings); |
94 | } |
95 | |
96 | void ETMViewStateSaver::selectItems(const Akonadi::Item::List &list) |
97 | { |
98 | QStringList itemStrings; |
99 | foreach (const Item &item, list) { |
100 | itemStrings << QString::fromLatin1("i%1" ).arg(item.id()); |
101 | } |
102 | restoreSelection(itemStrings); |
103 | } |
104 | |
105 | void ETMViewStateSaver::selectItems(const QList< Item::Id > &list) |
106 | { |
107 | QStringList itemStrings; |
108 | foreach (const Item::Id &itemId, list) { |
109 | itemStrings << QString::fromLatin1("i%1" ).arg(itemId); |
110 | } |
111 | restoreSelection(itemStrings); |
112 | } |
113 | |