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

akonadi

  • sources
  • kde-4.12
  • 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(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();
46 }
47 
48 RecentCollectionAction::~RecentCollectionAction()
49 {
50 }
51 
52 void RecentCollectionAction::fillRecentCollection()
53 {
54  delete mRecentAction->menu();
55  if ( mListRecentCollection.isEmpty() ) {
56  mRecentAction->setEnabled( false );
57  return;
58  }
59 
60  QMenu* popup = new QMenu;
61  mRecentAction->setMenu( popup );
62 
63  const int numberOfRecentCollection(mListRecentCollection.count());
64  for ( int i=0; i < numberOfRecentCollection; ++i )
65  {
66  const QModelIndex index = Akonadi::EntityTreeModel::modelIndexForCollection( mModel, Akonadi::Collection( mListRecentCollection.at( i ).toLongLong() ) );
67  const Akonadi::Collection collection = mModel->data( index, Akonadi::CollectionModel::CollectionRole ).value<Akonadi::Collection>();
68  if ( index.isValid() ) {
69  const bool canCreateNewItems = (collection.rights() & Collection::CanCreateItem);
70  QAction *action = popup->addAction( actionName( index ) );
71  const QIcon icon = mModel->data( index, Qt::DecorationRole ).value<QIcon>();
72  action->setIcon( icon );
73  action->setData( QVariant::fromValue<QModelIndex>( index ) );
74  action->setEnabled(canCreateNewItems);
75  }
76  }
77 }
78 
79 QString RecentCollectionAction::actionName(QModelIndex index)
80 {
81  QString name = index.data().toString();
82  name.replace( QLatin1String( "&" ), QLatin1String( "&&" ) );
83 
84  index = index.parent();
85  QString topLevelName;
86  while ( index != QModelIndex() ) {
87  topLevelName = index.data().toString();
88  index = index.parent();
89  }
90  if ( topLevelName.isEmpty() )
91  return QString::fromLatin1( "%1" ).arg( name );
92  else {
93  topLevelName.replace( QLatin1String( "&" ), QLatin1String( "&&" ) );
94  return QString::fromLatin1( "%1 - %2" ).arg( name ).arg( topLevelName );
95  }
96 }
97 
98 void RecentCollectionAction::addRecentCollection( Akonadi::Collection::Id id )
99 {
100  const QString newCollectionID = QString::number( id );
101  if ( mListRecentCollection.isEmpty() ||
102  !mListRecentCollection.contains( newCollectionID ) ) {
103  if ( mListRecentCollection.count() == s_maximumRecentCollection )
104  mListRecentCollection.removeFirst();
105  mListRecentCollection.append( newCollectionID );
106  writeConfig();
107  fillRecentCollection();
108  }
109 }
110 
111 void RecentCollectionAction::writeConfig()
112 {
113  KConfigGroup group( mAkonadiConfig, QLatin1String( "Recent Collections" ) );
114  group.writeEntry( "Collections", mListRecentCollection );
115  group.sync();
116 }
117 
118 void RecentCollectionAction::cleanRecentCollection()
119 {
120  mListRecentCollection.clear();
121  writeConfig();
122  fillRecentCollection();
123 }
124 
125 #include "moc_recentcollectionaction_p.cpp"
Akonadi::Collection
Represents a collection of PIM items.
Definition: collection.h:75
Akonadi::Entity::Id
qint64 Id
Describes the unique id type.
Definition: entity.h:65
Akonadi::Collection::CanCreateItem
Can create new items in this collection.
Definition: collection.h:89
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:1192
Akonadi::CollectionModel::CollectionRole
The actual collection object.
Definition: collectionmodel.h:66
Akonadi::Collection::parent
AKONADI_DEPRECATED Id parent() const
Returns the identifier of the parent collection.
Definition: collection.cpp:129
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:00:27 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
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

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