• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepimlibs API Reference
  • KDE Home
  • Contact Us
 

akonadi

  • sources
  • kde-4.14
  • kdepimlibs
  • akonadi
recentcollectionaction.cpp
1 /*
2  * Copyright (c) 2011 Laurent Montel <montel@kde.org>
3  *
4  * This library is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU Library General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  * This library is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
11  * License for more details.
12  *
13  * You should have received a copy of the GNU Library General Public License
14  * along with this library; see the file COPYING.LIB. If not, write to the
15  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16  * 02110-1301, USA.
17  */
18 
19 #include "recentcollectionaction_p.h"
20 #include "metatypes.h"
21 #include <akonadi/entitytreemodel.h>
22 #include <akonadi/collectionmodel.h>
23 #include <KConfig>
24 #include <KConfigGroup>
25 #include <KLocalizedString>
26 
27 #include <QMenu>
28 #include <QAction>
29 using namespace Akonadi;
30 
31 static int s_maximumRecentCollection = 10;
32 
33 RecentCollectionAction::RecentCollectionAction(Akonadi::StandardActionManager::Type type, const Akonadi::Collection::List &selectedCollectionsList,const QAbstractItemModel *model, QMenu *menu)
34  : QObject(menu)
35  , mMenu(menu)
36  , mModel(model)
37  , mRecentAction(0)
38 {
39  mAkonadiConfig = KSharedConfig::openConfig(QLatin1String("akonadikderc"));
40  KConfigGroup group(mAkonadiConfig, QLatin1String("Recent Collections"));
41 
42  mListRecentCollection = group.readEntry("Collections", QStringList());
43  mRecentAction = mMenu->addAction(i18n("Recent Folder"));
44  mMenu->addSeparator();
45  fillRecentCollection(type, selectedCollectionsList);
46 }
47 
48 RecentCollectionAction::~RecentCollectionAction()
49 {
50 }
51 
52 bool RecentCollectionAction::clear()
53 {
54  delete mRecentAction->menu();
55  if (mListRecentCollection.isEmpty()) {
56  mRecentAction->setEnabled(false);
57  return true;
58  }
59  return false;
60 }
61 
62 void RecentCollectionAction::fillRecentCollection(Akonadi::StandardActionManager::Type type, const Akonadi::Collection::List &selectedCollectionsList)
63 {
64  if (clear())
65  return;
66 
67  QMenu *popup = new QMenu;
68  mRecentAction->setMenu(popup);
69 
70  const int numberOfRecentCollection(mListRecentCollection.count());
71  for (int i = 0; i < numberOfRecentCollection; ++i) {
72  const QModelIndex index = Akonadi::EntityTreeModel::modelIndexForCollection(mModel, Akonadi::Collection(mListRecentCollection.at(i).toLongLong()));
73  const Akonadi::Collection collection = mModel->data(index, Akonadi::CollectionModel::CollectionRole).value<Akonadi::Collection>();
74  if (index.isValid()) {
75  const bool collectionIsSelected = selectedCollectionsList.contains(collection);
76  if (type == Akonadi::StandardActionManager::MoveCollectionToMenu && collectionIsSelected) {
77  continue;
78  }
79 
80  const bool canCreateNewItems = (collection.rights() &Collection::CanCreateItem);
81  QAction *action = popup->addAction(actionName(index));
82  const QIcon icon = mModel->data(index, Qt::DecorationRole).value<QIcon>();
83  action->setIcon(icon);
84  action->setData(QVariant::fromValue<QModelIndex>(index));
85  action->setEnabled(canCreateNewItems);
86  }
87  }
88 }
89 
90 QString RecentCollectionAction::actionName(QModelIndex index)
91 {
92  QString name = index.data().toString();
93  name.replace(QLatin1String("&"), QLatin1String("&&"));
94 
95  index = index.parent();
96  QString topLevelName;
97  while (index != QModelIndex()) {
98  topLevelName = index.data().toString();
99  index = index.parent();
100  }
101  if (topLevelName.isEmpty()) {
102  return QString::fromLatin1("%1").arg(name);
103  } else {
104  topLevelName.replace(QLatin1String("&"), QLatin1String("&&"));
105  return QString::fromLatin1("%1 - %2").arg(name).arg(topLevelName);
106  }
107 }
108 
109 void RecentCollectionAction::addRecentCollection(Akonadi::StandardActionManager::Type type, Akonadi::Collection::Id id)
110 {
111  const QString newCollectionID = QString::number(id);
112  if (mListRecentCollection.isEmpty() ||
113  !mListRecentCollection.contains(newCollectionID)) {
114  if (mListRecentCollection.count() == s_maximumRecentCollection) {
115  mListRecentCollection.removeFirst();
116  }
117  mListRecentCollection.append(newCollectionID);
118  writeConfig();
119  fillRecentCollection(type, Akonadi::Collection::List());
120  }
121 }
122 
123 void RecentCollectionAction::writeConfig()
124 {
125  KConfigGroup group(mAkonadiConfig, QLatin1String("Recent Collections"));
126  group.writeEntry("Collections", mListRecentCollection);
127  group.sync();
128 }
129 
130 void RecentCollectionAction::cleanRecentCollection()
131 {
132  mListRecentCollection.clear();
133  writeConfig();
134  clear();
135 }
136 
137 #include "moc_recentcollectionaction_p.cpp"
Akonadi::StandardActionManager::Type
Type
Describes the supported actions.
Definition: standardactionmanager.h:133
QModelIndex
Akonadi::Collection
Represents a collection of PIM items.
Definition: collection.h:75
QAction::setIcon
void setIcon(const QIcon &icon)
QMenu::addAction
void addAction(QAction *action)
Akonadi::Entity::Id
qint64 Id
Describes the unique id type.
Definition: entity.h:65
QModelIndex::isValid
bool isValid() const
QString::number
QString number(int n, int base)
QObject
QString::isEmpty
bool isEmpty() const
Akonadi::Collection::CanCreateItem
Can create new items in this collection.
Definition: collection.h:89
QString
QList
QModelIndex::parent
QModelIndex parent() const
QStringList
Akonadi::Collection::rights
Rights rights() const
Returns the rights the user has on the collection.
Definition: collection.cpp:99
Akonadi::EntityTreeModel::modelIndexForCollection
static QModelIndex modelIndexForCollection(const QAbstractItemModel *model, const Collection &collection)
Returns a QModelIndex in model which points to collection.
Definition: entitytreemodel.cpp:1238
QAction::setData
void setData(const QVariant &userData)
QMenu
QList::contains
bool contains(const T &value) const
QString::replace
QString & replace(int position, int n, QChar after)
Akonadi::CollectionModel::CollectionRole
The actual collection object.
Definition: collectionmodel.h:66
QModelIndex::data
QVariant data(int role) const
QLatin1String
QAction
QAbstractItemModel
QString::fromLatin1
QString fromLatin1(const char *str, int size)
QString::data
QChar * data()
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QVariant::toString
QString toString() const
QAction::setEnabled
void setEnabled(bool)
QIcon
Akonadi::StandardActionManager::MoveCollectionToMenu
Menu allowing to move a collection into another collection.
Definition: standardactionmanager.h:149
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:38:03 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

Skip menu "akonadi"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Modules
  • Related Pages

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal