• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.10.5 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 <KLocale>
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  changeCollectionDialogOptions( options );
59 
60  mTextLabel = new QLabel;
61  layout->addWidget( mTextLabel );
62  mTextLabel->hide();
63 
64  KLineEdit* filterCollectionLineEdit = new KLineEdit( widget );
65  filterCollectionLineEdit->setClearButtonShown( true );
66  filterCollectionLineEdit->setClickMessage( i18nc( "@info/plain Displayed grayed-out inside the "
67  "textbox, verb to search", "Search" ) );
68  layout->addWidget( filterCollectionLineEdit );
69 
70  mView = new EntityTreeView;
71  mView->setDragDropMode( QAbstractItemView::NoDragDrop );
72  mView->header()->hide();
73  layout->addWidget( mView );
74 
75 
76  mParent->enableButton( KDialog::Ok, false );
77 
78  // setup models
79  QAbstractItemModel *baseModel;
80 
81  if ( customModel ) {
82  baseModel = customModel;
83  } else {
84  mMonitor = new Akonadi::ChangeRecorder( mParent );
85  mMonitor->fetchCollection( true );
86  mMonitor->setCollectionMonitored( Akonadi::Collection::root() );
87 
88  EntityTreeModel *model = new EntityTreeModel( mMonitor, mParent );
89  model->setItemPopulationStrategy( EntityTreeModel::NoItemPopulation );
90  baseModel = model;
91  }
92 
93  mMimeTypeFilterModel = new CollectionFilterProxyModel( mParent );
94  mMimeTypeFilterModel->setSourceModel( baseModel );
95  mMimeTypeFilterModel->setExcludeVirtualCollections( true );
96 
97  mRightsFilterModel = new EntityRightsFilterModel( mParent );
98  mRightsFilterModel->setSourceModel( mMimeTypeFilterModel );
99 
100  KRecursiveFilterProxyModel* filterCollection = new KRecursiveFilterProxyModel( mParent );
101  filterCollection->setDynamicSortFilter( true );
102  filterCollection->setSourceModel( mRightsFilterModel );
103  filterCollection->setFilterCaseSensitivity( Qt::CaseInsensitive );
104  mView->setModel( filterCollection );
105 
106  mParent->connect( filterCollectionLineEdit, SIGNAL(textChanged(QString)),
107  filterCollection, SLOT(setFilterFixedString(QString)) );
108 
109  mParent->connect( mView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
110  mParent, SLOT(slotSelectionChanged()) );
111 
112  mParent->connect( mView, SIGNAL(doubleClicked(QModelIndex)),
113  mParent, SLOT(accept()) );
114 
115  mSelectionHandler = new AsyncSelectionHandler( filterCollection, mParent );
116  mParent->connect( mSelectionHandler, SIGNAL(collectionAvailable(QModelIndex)),
117  mParent, SLOT(slotCollectionAvailable(QModelIndex)) );
118  }
119 
120  ~Private()
121  {
122  }
123 
124  void slotCollectionAvailable( const QModelIndex &index )
125  {
126  mView->expandAll();
127  mView->setCurrentIndex( index );
128  }
129 
130  CollectionDialog *mParent;
131 
132  ChangeRecorder *mMonitor;
133  CollectionFilterProxyModel *mMimeTypeFilterModel;
134  EntityRightsFilterModel *mRightsFilterModel;
135  EntityTreeView *mView;
136  AsyncSelectionHandler *mSelectionHandler;
137  QLabel *mTextLabel;
138  bool mAllowToCreateNewChildCollection;
139  bool mKeepTreeExpanded;
140 
141  void slotSelectionChanged();
142  void slotAddChildCollection();
143  void slotCollectionCreationResult( KJob* job );
144  bool canCreateCollection( const Akonadi::Collection &parentCollection ) const;
145  void changeCollectionDialogOptions( CollectionDialogOptions options );
146 
147 };
148 
149 void CollectionDialog::Private::slotSelectionChanged()
150 {
151  mParent->enableButton( KDialog::Ok, mView->selectionModel()->selectedIndexes().count() > 0 );
152  if ( mAllowToCreateNewChildCollection ) {
153  const Akonadi::Collection parentCollection = mParent->selectedCollection();
154  const bool canCreateChildCollections = canCreateCollection( parentCollection );
155 
156  mParent->enableButton( KDialog::User1, ( canCreateChildCollections && !parentCollection.isVirtual() ) );
157  if ( parentCollection.isValid() ) {
158  const bool canCreateItems = ( parentCollection.rights() & Akonadi::Collection::CanCreateItem );
159  mParent->enableButton( KDialog::Ok, canCreateItems );
160  }
161  }
162 }
163 
164 void CollectionDialog::Private::changeCollectionDialogOptions( CollectionDialogOptions options )
165 {
166  mAllowToCreateNewChildCollection = ( options & AllowToCreateNewChildCollection );
167  if ( mAllowToCreateNewChildCollection ) {
168  mParent->setButtons( Ok | Cancel | User1 );
169  mParent->setButtonGuiItem( User1, KGuiItem( i18n( "&New Subfolder..." ), QLatin1String( "folder-new" ),
170  i18n( "Create a new subfolder under the currently selected folder" ) ) );
171  mParent->enableButton( KDialog::User1, false );
172  connect( mParent, SIGNAL(user1Clicked()), mParent, SLOT(slotAddChildCollection()) );
173  }
174  mKeepTreeExpanded = ( options & KeepTreeExpanded );
175  if ( mKeepTreeExpanded ) {
176 
177  mParent->connect( mRightsFilterModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
178  mView, SLOT(expandAll()), Qt::UniqueConnection );
179  mView->expandAll();
180  }
181 }
182 
183 
184 
185 bool CollectionDialog::Private::canCreateCollection( const Akonadi::Collection &parentCollection ) const
186 {
187  if ( !parentCollection.isValid() ) {
188  return false;
189  }
190 
191  if ( ( parentCollection.rights() & Akonadi::Collection::CanCreateCollection ) ) {
192  const QStringList dialogMimeTypeFilter = mParent->mimeTypeFilter();
193  const QStringList parentCollectionMimeTypes = parentCollection.contentMimeTypes();
194  Q_FOREACH ( const QString& mimetype, dialogMimeTypeFilter ) {
195  if ( parentCollectionMimeTypes.contains( mimetype ) ) {
196  return true;
197  }
198  }
199  return true;
200  }
201  return false;
202 }
203 
204 
205 void CollectionDialog::Private::slotAddChildCollection()
206 {
207  const Akonadi::Collection parentCollection = mParent->selectedCollection();
208  if ( canCreateCollection( parentCollection ) ) {
209  const QString name = KInputDialog::getText( i18nc( "@title:window", "New Folder" ),
210  i18nc( "@label:textbox, name of a thing", "Name" ),
211  QString(), 0, mParent );
212  if ( name.isEmpty() ) {
213  return;
214  }
215 
216  Akonadi::Collection collection;
217  collection.setName( name );
218  collection.setParentCollection( parentCollection );
219  Akonadi::CollectionCreateJob *job = new Akonadi::CollectionCreateJob( collection );
220  connect( job, SIGNAL(result(KJob*)), mParent, SLOT(slotCollectionCreationResult(KJob*)) );
221  }
222 }
223 
224 void CollectionDialog::Private::slotCollectionCreationResult( KJob* job )
225 {
226  if ( job->error() ) {
227  KMessageBox::error( mParent, i18n( "Could not create folder: %1", job->errorString() ),
228  i18n( "Folder creation failed" ) );
229  }
230 }
231 
232 
233 
234 CollectionDialog::CollectionDialog( QWidget *parent )
235  : KDialog( parent ),
236  d( new Private( 0, this, CollectionDialog::None ) )
237 {
238 }
239 
240 CollectionDialog::CollectionDialog( QAbstractItemModel *model, QWidget *parent )
241  : KDialog( parent ),
242  d( new Private( model, this, CollectionDialog::None ) )
243 {
244 }
245 
246 CollectionDialog::CollectionDialog( CollectionDialogOptions options, QAbstractItemModel *model, QWidget *parent )
247  : KDialog( parent ),
248  d( new Private( model, this, options ) )
249 {
250 }
251 
252 
253 CollectionDialog::~CollectionDialog()
254 {
255  delete d;
256 }
257 
258 Akonadi::Collection CollectionDialog::selectedCollection() const
259 {
260  if ( selectionMode() == QAbstractItemView::SingleSelection ) {
261  const QModelIndex index = d->mView->currentIndex();
262  if ( index.isValid() ) {
263  return index.model()->data( index, EntityTreeModel::CollectionRole ).value<Collection>();
264  }
265  }
266 
267  return Collection();
268 }
269 
270 Akonadi::Collection::List CollectionDialog::selectedCollections() const
271 {
272  Collection::List collections;
273  const QItemSelectionModel *selectionModel = d->mView->selectionModel();
274  const QModelIndexList selectedIndexes = selectionModel->selectedIndexes();
275  foreach ( const QModelIndex &index, selectedIndexes ) {
276  if ( index.isValid() ) {
277  const Collection collection = index.model()->data( index, EntityTreeModel::CollectionRole ).value<Collection>();
278  if ( collection.isValid() ) {
279  collections.append( collection );
280  }
281  }
282  }
283 
284  return collections;
285 }
286 
287 void CollectionDialog::setMimeTypeFilter( const QStringList &mimeTypes )
288 {
289  if ( mimeTypeFilter() == mimeTypes )
290  return;
291 
292  d->mMimeTypeFilterModel->clearFilters();
293  d->mMimeTypeFilterModel->addMimeTypeFilters( mimeTypes );
294 
295  if ( d->mMonitor ) {
296  foreach ( const QString &mimetype, mimeTypes ) {
297  d->mMonitor->setMimeTypeMonitored( mimetype );
298  }
299  }
300 }
301 
302 QStringList CollectionDialog::mimeTypeFilter() const
303 {
304  return d->mMimeTypeFilterModel->mimeTypeFilters();
305 }
306 
307 void CollectionDialog::setAccessRightsFilter( Collection::Rights rights )
308 {
309  if ( accessRightsFilter() == rights )
310  return;
311  d->mRightsFilterModel->setAccessRights( rights );
312 }
313 
314 Akonadi::Collection::Rights CollectionDialog::accessRightsFilter() const
315 {
316  return d->mRightsFilterModel->accessRights();
317 }
318 
319 void CollectionDialog::setDescription( const QString &text )
320 {
321  d->mTextLabel->setText( text );
322  d->mTextLabel->show();
323 }
324 
325 void CollectionDialog::setDefaultCollection( const Collection &collection )
326 {
327  d->mSelectionHandler->waitForCollection( collection );
328 }
329 
330 void CollectionDialog::setSelectionMode( QAbstractItemView::SelectionMode mode )
331 {
332  d->mView->setSelectionMode( mode );
333 }
334 
335 QAbstractItemView::SelectionMode CollectionDialog::selectionMode() const
336 {
337  return d->mView->selectionMode();
338 }
339 
340 void CollectionDialog::changeCollectionDialogOptions( CollectionDialogOptions options )
341 {
342  d->changeCollectionDialogOptions( options );
343 }
344 
345 #include "collectiondialog.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:27:33 by doxygen 1.8.3.1 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.10.5 API Reference

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