Akonadi

etmviewstatesaver.cpp
1 /*
2  Copyright (C) 2010 Klarälvdalens Datakonsult AB,
3  a KDAB Group company, [email protected],
4  author Stephen Kelly <[email protected]>
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 <QModelIndex>
25 
26 #include "entitytreemodel.h"
27 
28 using namespace Akonadi;
29 
30 ETMViewStateSaver::ETMViewStateSaver(QObject *parent)
31  : KConfigViewStateSaver(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  Item::Id id = key.mid(1).toLongLong();
42  if (id < 0) {
43  return QModelIndex();
44  }
45 
46  if (key.startsWith(QLatin1Char('c'))) {
48  if (!idx.isValid()) {
49  return QModelIndex();
50  }
51  return idx;
52  } else if (key.startsWith(QLatin1Char('i'))) {
53  const 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 QStringLiteral("x-1");
66  }
68  if (c.isValid()) {
69  return QStringLiteral("c%1").arg(c.id());
70  }
71  Item::Id id = index.data(EntityTreeModel::ItemIdRole).value<Item::Id>();
72  if (id >= 0) {
73  return QStringLiteral("i%1").arg(id);
74  }
75  return QString();
76 }
77 
78 void ETMViewStateSaver::selectCollections(const Akonadi::Collection::List &list)
79 {
80  QStringList colStrings;
81  colStrings.reserve(list.count());
82  for (const Collection &col : list) {
83  colStrings << QStringLiteral("c%1").arg(col.id());
84  }
85  restoreSelection(colStrings);
86 }
87 
88 void ETMViewStateSaver::selectCollections(const QList< Collection::Id > &list)
89 {
90  QStringList colStrings;
91  colStrings.reserve(list.count());
92  for (const Collection::Id &colId : list) {
93  colStrings << QStringLiteral("c%1").arg(colId);
94  }
95  restoreSelection(colStrings);
96 }
97 
98 void ETMViewStateSaver::selectItems(const Akonadi::Item::List &list)
99 {
100  QStringList itemStrings;
101  itemStrings.reserve(list.count());
102  for (const Item &item : list) {
103  itemStrings << QStringLiteral("i%1").arg(item.id());
104  }
105  restoreSelection(itemStrings);
106 }
107 
108 void ETMViewStateSaver::selectItems(const QList< Item::Id > &list)
109 {
110  QStringList itemStrings;
111  itemStrings.reserve(list.count());
112  for (const Item::Id &itemId : list) {
113  itemStrings << QStringLiteral("i%1").arg(itemId);
114  }
115  restoreSelection(itemStrings);
116 }
117 
118 void ETMViewStateSaver::setCurrentItem(const Akonadi::Item &item)
119 {
120  restoreCurrentItem(QStringLiteral("i%1").arg(item.id()));
121 }
122 
123 void ETMViewStateSaver::setCurrentCollection(const Akonadi::Collection &col)
124 {
125  restoreCurrentItem(QStringLiteral("c%1").arg(col.id()));
126 }
bool isValid() const
Returns whether the collection is valid.
Definition: collection.cpp:137
void reserve(int alloc)
Represents a collection of PIM items.
Definition: collection.h:76
qint64 Id
Describes the unique id type.
Definition: collection.h:82
T value() const const
bool isValid() const const
int count(const T &value) const const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
static QModelIndex modelIndexForCollection(const QAbstractItemModel *model, const Collection &collection)
Returns a QModelIndex in model which points to collection.
Id id() const
Returns the unique identifier of the collection.
Definition: collection.cpp:112
QString mid(int position, int n) const const
QVariant data(int role) const const
Helper integration between Akonadi and Qt.
int count(const T &value) const const
static QModelIndexList modelIndexesForItem(const QAbstractItemModel *model, const Item &item)
Returns a QModelIndex in model which points to item.
QAbstractItemModel * model() const const
qlonglong toLongLong(bool *ok, int base) const const
KIOFILEWIDGETS_EXPORT QStringList list(const QString &fileClass)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Jun 5 2020 23:08:54 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.