Akonadi

etmviewstatesaver.cpp
1 /*
2  SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB,
3  a KDAB Group company, [email protected]
4  SPDX-FileContributor: Stephen Kelly <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8 
9 #include "etmviewstatesaver.h"
10 
11 #include <QModelIndex>
12 
13 #include "entitytreemodel.h"
14 
15 using namespace Akonadi;
16 
17 ETMViewStateSaver::ETMViewStateSaver(QObject *parent)
18  : KConfigViewStateSaver(parent)
19 {
20 }
21 
22 QModelIndex ETMViewStateSaver::indexFromConfigString(const QAbstractItemModel *model, const QString &key) const
23 {
24  if (key.startsWith(QLatin1Char('x'))) {
25  return QModelIndex();
26  }
27 
28  Item::Id id = key.mid(1).toLongLong();
29  if (id < 0) {
30  return QModelIndex();
31  }
32 
33  if (key.startsWith(QLatin1Char('c'))) {
35  if (!idx.isValid()) {
36  return QModelIndex();
37  }
38  return idx;
39  } else if (key.startsWith(QLatin1Char('i'))) {
40  const QModelIndexList list = EntityTreeModel::modelIndexesForItem(model, Item(id));
41  if (list.isEmpty()) {
42  return QModelIndex();
43  }
44  return list.first();
45  }
46  return QModelIndex();
47 }
48 
49 QString ETMViewStateSaver::indexToConfigString(const QModelIndex &index) const
50 {
51  if (!index.isValid()) {
52  return QStringLiteral("x-1");
53  }
54  const auto c = index.data(EntityTreeModel::CollectionRole).value<Collection>();
55  if (c.isValid()) {
56  return QStringLiteral("c%1").arg(c.id());
57  }
58  auto id = index.data(EntityTreeModel::ItemIdRole).value<Item::Id>();
59  if (id >= 0) {
60  return QStringLiteral("i%1").arg(id);
61  }
62  return QString();
63 }
64 
65 void ETMViewStateSaver::selectCollections(const Akonadi::Collection::List &list)
66 {
67  QStringList colStrings;
68  colStrings.reserve(list.count());
69  for (const Collection &col : list) {
70  colStrings << QStringLiteral("c%1").arg(col.id());
71  }
72  restoreSelection(colStrings);
73 }
74 
75 void ETMViewStateSaver::selectCollections(const QList<Collection::Id> &list)
76 {
77  QStringList colStrings;
78  colStrings.reserve(list.count());
79  for (const Collection::Id &colId : list) {
80  colStrings << QStringLiteral("c%1").arg(colId);
81  }
82  restoreSelection(colStrings);
83 }
84 
85 void ETMViewStateSaver::selectItems(const Akonadi::Item::List &list)
86 {
87  QStringList itemStrings;
88  itemStrings.reserve(list.count());
89  for (const Item &item : list) {
90  itemStrings << QStringLiteral("i%1").arg(item.id());
91  }
92  restoreSelection(itemStrings);
93 }
94 
95 void ETMViewStateSaver::selectItems(const QList<Item::Id> &list)
96 {
97  QStringList itemStrings;
98  itemStrings.reserve(list.count());
99  for (const Item::Id &itemId : list) {
100  itemStrings << QStringLiteral("i%1").arg(itemId);
101  }
102  restoreSelection(itemStrings);
103 }
104 
105 void ETMViewStateSaver::setCurrentItem(const Akonadi::Item &item)
106 {
107  restoreCurrentItem(QStringLiteral("i%1").arg(item.id()));
108 }
109 
110 void ETMViewStateSaver::setCurrentCollection(const Akonadi::Collection &col)
111 {
112  restoreCurrentItem(QStringLiteral("c%1").arg(col.id()));
113 }
T & first()
int count(const T &value) const const
T value() const const
qlonglong toLongLong(bool *ok, int base) const const
Represents a collection of PIM items.
Definition: collection.h:61
KIOFILEWIDGETS_EXPORT QStringList list(const QString &fileClass)
void reserve(int alloc)
qint64 Id
Describes the unique id type.
Definition: item.h:110
QVariant data(int role) const const
@ CollectionRole
The collection.
static QModelIndexList modelIndexesForItem(const QAbstractItemModel *model, const Item &item)
Returns a QModelIndex in model which points to item.
bool isEmpty() const const
bool isValid() const const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
Id id() const
Returns the unique identifier of the item.
Definition: item.cpp:63
static QModelIndex modelIndexForCollection(const QAbstractItemModel *model, const Collection &collection)
Returns a QModelIndex in model which points to collection.
QString mid(int position, int n) const const
qint64 Id
Describes the unique id type.
Definition: collection.h:79
Represents a PIM item stored in Akonadi storage.
Definition: item.h:104
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Mon Jun 27 2022 04:01:06 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.