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

akonadi

  • akonadi
collectiondialog_desktop.cpp
1 /*
2  Copyright 2008 Ingo Klöcker <kloecker@kde.org>
3  Copyright 2010 Laurent Montel <montel@kde.org>
4 
5  This library is free software; you can redistribute it and/or modify it
6  under the terms of the GNU Library General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or (at your
8  option) any later version.
9 
10  This library is distributed in the hope that it will be useful, but WITHOUT
11  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13  License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to the
17  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  02110-1301, USA.
19 */
20 
21 #include "collectiondialog.h"
22 
23 #include "asyncselectionhandler_p.h"
24 
25 #include <akonadi/changerecorder.h>
26 #include <akonadi/collectionfetchscope.h>
27 #include <akonadi/collectionfilterproxymodel.h>
28 #include <akonadi/entityrightsfiltermodel.h>
29 #include <akonadi/entitytreemodel.h>
30 #include <akonadi/entitytreeview.h>
31 #include <akonadi/session.h>
32 #include <akonadi/collectioncreatejob.h>
33 #include <akonadi/collectionutils_p.h>
34 
35 #include <QHeaderView>
36 #include <QLabel>
37 #include <QVBoxLayout>
38 
39 #include <KLineEdit>
40 #include <KLocalizedString>
41 #include <KInputDialog>
42 #include <KMessageBox>
43 
44 using namespace Akonadi;
45 
46 class CollectionDialog::Private
47 {
48  public:
49  Private( QAbstractItemModel *customModel, CollectionDialog *parent, CollectionDialogOptions options )
50  : mParent( parent ),
51  mMonitor( 0 )
52  {
53  // setup GUI
54  QWidget *widget = mParent->mainWidget();
55  QVBoxLayout *layout = new QVBoxLayout( widget );
56  layout->setContentsMargins( 0, 0, 0, 0 );
57 
58 
59  mTextLabel = new QLabel;
60  layout->addWidget( mTextLabel );
61  mTextLabel->hide();
62 
63  KLineEdit* filterCollectionLineEdit = new KLineEdit( widget );
64  filterCollectionLineEdit->setClearButtonShown( true );
65  filterCollectionLineEdit->setClickMessage( i18nc( "@info/plain Displayed grayed-out inside the "
66  "textbox, verb to search", "Search" ) );
67  layout->addWidget( filterCollectionLineEdit );
68 
69  mView = new EntityTreeView;
70  mView->setDragDropMode( QAbstractItemView::NoDragDrop );
71  mView->header()->hide();
72  layout->addWidget( mView );
73 
74  mParent->enableButton( KDialog::Ok, false );
75 
76  // setup models
77  QAbstractItemModel *baseModel;
78 
79  if ( customModel ) {
80  baseModel = customModel;
81  } else {
82  mMonitor = new Akonadi::ChangeRecorder( mParent );
83  mMonitor->fetchCollection( true );
84  mMonitor->setCollectionMonitored( Akonadi::Collection::root() );
85 
86  EntityTreeModel *model = new EntityTreeModel( mMonitor, mParent );
87  model->setItemPopulationStrategy( EntityTreeModel::NoItemPopulation );
88  baseModel = model;
89  }
90 
91  mMimeTypeFilterModel = new CollectionFilterProxyModel( mParent );
92  mMimeTypeFilterModel->setSourceModel( baseModel );
93  mMimeTypeFilterModel->setExcludeVirtualCollections( true );
94 
95  mRightsFilterModel = new EntityRightsFilterModel( mParent );
96  mRightsFilterModel->setSourceModel( mMimeTypeFilterModel );
97 
98  KRecursiveFilterProxyModel* filterCollection = new KRecursiveFilterProxyModel( mParent );
99  filterCollection->setDynamicSortFilter( true );
100  filterCollection->setSourceModel( mRightsFilterModel );
101  filterCollection->setFilterCaseSensitivity( Qt::CaseInsensitive );
102  mView->setModel( filterCollection );
103 
104  changeCollectionDialogOptions( options );
105  mParent->connect( filterCollectionLineEdit, SIGNAL(textChanged(QString)),
106  filterCollection, SLOT(setFilterFixedString(QString)) );
107 
108  mParent->connect( mView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
109  mParent, SLOT(slotSelectionChanged()) );
110 
111  mParent->connect( mView, SIGNAL(doubleClicked(QModelIndex)),
112  mParent, SLOT(accept()) );
113 
114  mSelectionHandler = new AsyncSelectionHandler( filterCollection, mParent );
115  mParent->connect( mSelectionHandler, SIGNAL(collectionAvailable(QModelIndex)),
116  mParent, SLOT(slotCollectionAvailable(QModelIndex)) );
117  }
118 
119  ~Private()
120  {
121  }
122 
123  void slotCollectionAvailable( const QModelIndex &index )
124  {
125  mView->expandAll();
126  mView->setCurrentIndex( index );
127  }
128 
129  CollectionDialog *mParent;
130 
131  ChangeRecorder *mMonitor;
132  CollectionFilterProxyModel *mMimeTypeFilterModel;
133  EntityRightsFilterModel *mRightsFilterModel;
134  EntityTreeView *mView;
135  AsyncSelectionHandler *mSelectionHandler;
136  QLabel *mTextLabel;
137  bool mAllowToCreateNewChildCollection;
138  bool mKeepTreeExpanded;
139 
140  void slotSelectionChanged();
141  void slotAddChildCollection();
142  void slotCollectionCreationResult( KJob* job );
143  bool canCreateCollection( const Akonadi::Collection &parentCollection ) const;
144  void changeCollectionDialogOptions( CollectionDialogOptions options );
145 
146 };
147 
148 void CollectionDialog::Private::slotSelectionChanged()
149 {
150  mParent->enableButton( KDialog::Ok, mView->selectionModel()->selectedIndexes().count() > 0 );
151  if ( mAllowToCreateNewChildCollection ) {
152  const Akonadi::Collection parentCollection = mParent->selectedCollection();
153  const bool canCreateChildCollections = canCreateCollection( parentCollection );
154 
155  mParent->enableButton( KDialog::User1, ( canCreateChildCollections && !parentCollection.isVirtual() ) );
156  if ( parentCollection.isValid() ) {
157  const bool canCreateItems = ( parentCollection.rights() & Akonadi::Collection::CanCreateItem );
158  mParent->enableButton( KDialog::Ok, canCreateItems );
159  }
160  }
161 }
162 
163 void CollectionDialog::Private::changeCollectionDialogOptions( CollectionDialogOptions options )
164 {
165  mAllowToCreateNewChildCollection = ( options & AllowToCreateNewChildCollection );
166  if ( mAllowToCreateNewChildCollection ) {
167  mParent->setButtons( Ok | Cancel | User1 );
168  mParent->setButtonGuiItem( User1, KGuiItem( i18n( "&New Subfolder..." ), QLatin1String( "folder-new" ),
169  i18n( "Create a new subfolder under the currently selected folder" ) ) );
170  mParent->enableButton( KDialog::User1, false );
171  connect( mParent, SIGNAL(user1Clicked()), mParent, SLOT(slotAddChildCollection()) );
172  }
173  mKeepTreeExpanded = ( options & KeepTreeExpanded );
174  if ( mKeepTreeExpanded ) {
175  mParent->connect( mRightsFilterModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
176  mView, SLOT(expandAll()), Qt::UniqueConnection );
177  mView->expandAll();
178  }
179 }
180 
181 
182 
183 bool CollectionDialog::Private::canCreateCollection( const Akonadi::Collection &parentCollection ) const
184 {
185  if ( !parentCollection.isValid() ) {
186  return false;
187  }
188 
189  if ( ( parentCollection.rights() & Akonadi::Collection::CanCreateCollection ) ) {
190  const QStringList dialogMimeTypeFilter = mParent->mimeTypeFilter();
191  const QStringList parentCollectionMimeTypes = parentCollection.contentMimeTypes();
192  Q_FOREACH ( const QString& mimetype, dialogMimeTypeFilter ) {
193  if ( parentCollectionMimeTypes.contains( mimetype ) ) {
194  return true;
195  }
196  }
197  return true;
198  }
199  return false;
200 }
201 
202 
203 void CollectionDialog::Private::slotAddChildCollection()
204 {
205  const Akonadi::Collection parentCollection = mParent->selectedCollection();
206  if ( canCreateCollection( parentCollection ) ) {
207  const QString name = KInputDialog::getText( i18nc( "@title:window", "New Folder" ),
208  i18nc( "@label:textbox, name of a thing", "Name" ),
209  QString(), 0, mParent );
210  if ( name.isEmpty() ) {
211  return;
212  }
213 
214  Akonadi::Collection collection;
215  collection.setName( name );
216  collection.setParentCollection( parentCollection );
217  Akonadi::CollectionCreateJob *job = new Akonadi::CollectionCreateJob( collection );
218  connect( job, SIGNAL(result(KJob*)), mParent, SLOT(slotCollectionCreationResult(KJob*)) );
219  }
220 }
221 
222 void CollectionDialog::Private::slotCollectionCreationResult( KJob* job )
223 {
224  if ( job->error() ) {
225  KMessageBox::error( mParent, i18n( "Could not create folder: %1", job->errorString() ),
226  i18n( "Folder creation failed" ) );
227  }
228 }
229 
230 
231 
232 CollectionDialog::CollectionDialog( QWidget *parent )
233  : KDialog( parent ),
234  d( new Private( 0, this, CollectionDialog::None ) )
235 {
236 }
237 
238 CollectionDialog::CollectionDialog( QAbstractItemModel *model, QWidget *parent )
239  : KDialog( parent ),
240  d( new Private( model, this, CollectionDialog::None ) )
241 {
242 }
243 
244 CollectionDialog::CollectionDialog( CollectionDialogOptions options, QAbstractItemModel *model, QWidget *parent )
245  : KDialog( parent ),
246  d( new Private( model, this, options ) )
247 {
248 }
249 
250 
251 CollectionDialog::~CollectionDialog()
252 {
253  delete d;
254 }
255 
256 Akonadi::Collection CollectionDialog::selectedCollection() const
257 {
258  if ( selectionMode() == QAbstractItemView::SingleSelection ) {
259  const QModelIndex index = d->mView->currentIndex();
260  if ( index.isValid() ) {
261  return index.model()->data( index, EntityTreeModel::CollectionRole ).value<Collection>();
262  }
263  }
264 
265  return Collection();
266 }
267 
268 Akonadi::Collection::List CollectionDialog::selectedCollections() const
269 {
270  Collection::List collections;
271  const QItemSelectionModel *selectionModel = d->mView->selectionModel();
272  const QModelIndexList selectedIndexes = selectionModel->selectedIndexes();
273  foreach ( const QModelIndex &index, selectedIndexes ) {
274  if ( index.isValid() ) {
275  const Collection collection = index.model()->data( index, EntityTreeModel::CollectionRole ).value<Collection>();
276  if ( collection.isValid() ) {
277  collections.append( collection );
278  }
279  }
280  }
281 
282  return collections;
283 }
284 
285 void CollectionDialog::setMimeTypeFilter( const QStringList &mimeTypes )
286 {
287  if ( mimeTypeFilter() == mimeTypes )
288  return;
289 
290  d->mMimeTypeFilterModel->clearFilters();
291  d->mMimeTypeFilterModel->addMimeTypeFilters( mimeTypes );
292 
293  if ( d->mMonitor ) {
294  foreach ( const QString &mimetype, mimeTypes ) {
295  d->mMonitor->setMimeTypeMonitored( mimetype );
296  }
297  }
298 }
299 
300 QStringList CollectionDialog::mimeTypeFilter() const
301 {
302  return d->mMimeTypeFilterModel->mimeTypeFilters();
303 }
304 
305 void CollectionDialog::setAccessRightsFilter( Collection::Rights rights )
306 {
307  if ( accessRightsFilter() == rights )
308  return;
309  d->mRightsFilterModel->setAccessRights( rights );
310 }
311 
312 Akonadi::Collection::Rights CollectionDialog::accessRightsFilter() const
313 {
314  return d->mRightsFilterModel->accessRights();
315 }
316 
317 void CollectionDialog::setDescription( const QString &text )
318 {
319  d->mTextLabel->setText( text );
320  d->mTextLabel->show();
321 }
322 
323 void CollectionDialog::setDefaultCollection( const Collection &collection )
324 {
325  d->mSelectionHandler->waitForCollection( collection );
326 }
327 
328 void CollectionDialog::setSelectionMode( QAbstractItemView::SelectionMode mode )
329 {
330  d->mView->setSelectionMode( mode );
331 }
332 
333 QAbstractItemView::SelectionMode CollectionDialog::selectionMode() const
334 {
335  return d->mView->selectionMode();
336 }
337 
338 void CollectionDialog::changeCollectionDialogOptions( CollectionDialogOptions options )
339 {
340  d->changeCollectionDialogOptions( options );
341 }
342 
343 #include "collectiondialog.moc"
Akonadi::CollectionDialog::setAccessRightsFilter
void setAccessRightsFilter(Collection::Rights rights)
Sets the access rights that the listed collections shall match with.
Definition: collectiondialog_desktop.cpp:305
Akonadi::CollectionDialog::CollectionDialog
CollectionDialog(QWidget *parent=0)
Creates a new collection dialog.
Definition: collectiondialog_desktop.cpp:232
Akonadi::CollectionDialog::selectedCollection
Akonadi::Collection selectedCollection() const
Returns the selected collection if the selection mode is QAbstractItemView::SingleSelection.
Definition: collectiondialog_desktop.cpp:256
Akonadi::CollectionDialog::setSelectionMode
void setSelectionMode(QAbstractItemView::SelectionMode mode)
Sets the selection mode.
Definition: collectiondialog_desktop.cpp:328
Akonadi::CollectionDialog::setMimeTypeFilter
void setMimeTypeFilter(const QStringList &mimeTypes)
Sets the mime types any of which the selected collection(s) shall support.
Definition: collectiondialog_desktop.cpp:285
Akonadi::CollectionFilterProxyModel
A proxy model that filters collections by mime type.
Definition: collectionfilterproxymodel.h:54
Akonadi::AsyncSelectionHandler
Definition: asyncselectionhandler_p.h:42
Akonadi::CollectionDialog
A collection selection dialog.
Definition: collectiondialog.h:67
Akonadi::CollectionDialog::selectedCollections
Akonadi::Collection::List selectedCollections() const
Returns the list of selected collections.
Definition: collectiondialog_desktop.cpp:268
Akonadi::Collection
Represents a collection of PIM items.
Definition: collection.h:75
Akonadi::EntityTreeModel::NoItemPopulation
Do not include items in the model.
Definition: entitytreemodel.h:407
Akonadi::Collection::CanCreateCollection
Can create new subcollections in this collection.
Definition: collection.h:92
Akonadi::EntityRightsFilterModel
A proxy model that filters entities by access rights.
Definition: entityrightsfiltermodel.h:60
Akonadi::Entity::setParentCollection
void setParentCollection(const Collection &parent)
Set the parent collection of this object.
Definition: entity.cpp:195
Akonadi::Collection::setName
void setName(const QString &name)
Sets the i18n&#39;ed name of the collection.
Definition: collection.cpp:93
Akonadi::CollectionDialog::selectionMode
QAbstractItemView::SelectionMode selectionMode() const
Returns the selection mode.
Definition: collectiondialog_desktop.cpp:333
Akonadi::EntityTreeView
A view to show an item/collection tree provided by an EntityTreeModel.
Definition: entitytreeview.h:71
Akonadi::Collection::CanCreateItem
Can create new items in this collection.
Definition: collection.h:89
Akonadi::Collection::root
static Collection root()
Returns the root collection.
Definition: collection.cpp:192
Akonadi::EntityTreeModel::CollectionRole
The collection.
Definition: entitytreemodel.h:335
Akonadi::CollectionDialog::setDefaultCollection
void setDefaultCollection(const Collection &collection)
Sets the collection that shall be selected by default.
Definition: collectiondialog_desktop.cpp:323
Akonadi::CollectionDialog::changeCollectionDialogOptions
void changeCollectionDialogOptions(CollectionDialogOptions options)
Change collection dialog options.
Definition: collectiondialog_desktop.cpp:338
Akonadi::Collection::rights
Rights rights() const
Returns the rights the user has on the collection.
Definition: collection.cpp:99
Akonadi::CollectionDialog::mimeTypeFilter
QStringList mimeTypeFilter() const
Returns the mime types any of which the selected collection(s) shall support.
Definition: collectiondialog_desktop.cpp:300
Akonadi::CollectionDialog::accessRightsFilter
Collection::Rights accessRightsFilter() const
Sets the access rights that the listed collections shall match with.
Definition: collectiondialog_desktop.cpp:312
Akonadi::EntityTreeModel::setItemPopulationStrategy
void setItemPopulationStrategy(ItemPopulationStrategy strategy)
Sets the item population strategy of the model.
Definition: entitytreemodel.cpp:1075
Akonadi::CollectionDialog::setDescription
void setDescription(const QString &text)
Sets the text that will be shown in the dialog.
Definition: collectiondialog_desktop.cpp:317
Akonadi::EntityTreeModel
A model for collections and items together.
Definition: entitytreemodel.h:317
Akonadi::Collection::contentMimeTypes
QStringList contentMimeTypes() const
Returns a list of possible content mimetypes, e.g.
Definition: collection.cpp:115
Akonadi::CollectionDialog::~CollectionDialog
~CollectionDialog()
Destroys the collection dialog.
Definition: collectiondialog_desktop.cpp:251
Akonadi::CollectionCreateJob
Job that creates a new collection in the Akonadi storage.
Definition: collectioncreatejob.h:52
Akonadi::Entity::isValid
bool isValid() const
Returns whether the entity is valid.
Definition: entity.cpp:97
Akonadi::Collection::List
QList< Collection > List
Describes a list of collections.
Definition: collection.h:81
Akonadi::Collection::isVirtual
bool isVirtual() const
Returns whether the collection is virtual, for example a search collection.
Definition: collection.cpp:261
Akonadi::ChangeRecorder
Records and replays change notification.
Definition: changerecorder.h:47
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Tue Nov 26 2013 09:03:16 by doxygen 1.8.5 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-4.11.3 API Reference

Skip menu "kdepimlibs-4.11.3 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
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