• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.9.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 <QtGui/QHeaderView>
36 #include <QtGui/QLabel>
37 #include <QtGui/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 
57  changeCollectionDialogOptions( options );
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 
75  mParent->enableButton( KDialog::Ok, false );
76 
77  // setup models
78  QAbstractItemModel *baseModel;
79 
80  if ( customModel ) {
81  baseModel = customModel;
82  } else {
83  mMonitor = new Akonadi::ChangeRecorder( mParent );
84  mMonitor->fetchCollection( true );
85  mMonitor->setCollectionMonitored( Akonadi::Collection::root() );
86 
87  EntityTreeModel *model = new EntityTreeModel( mMonitor, mParent );
88  model->setItemPopulationStrategy( EntityTreeModel::NoItemPopulation );
89  baseModel = model;
90  }
91 
92  mMimeTypeFilterModel = new CollectionFilterProxyModel( mParent );
93  mMimeTypeFilterModel->setSourceModel( baseModel );
94  mMimeTypeFilterModel->setExcludeVirtualCollections( true );
95 
96  mRightsFilterModel = new EntityRightsFilterModel( mParent );
97  mRightsFilterModel->setSourceModel( mMimeTypeFilterModel );
98 
99  mSelectionHandler = new AsyncSelectionHandler( mRightsFilterModel, mParent );
100  mParent->connect( mSelectionHandler, SIGNAL(collectionAvailable(QModelIndex)),
101  mParent, SLOT(slotCollectionAvailable(QModelIndex)) );
102 
103  KRecursiveFilterProxyModel* filterCollection = new KRecursiveFilterProxyModel( mParent );
104  filterCollection->setDynamicSortFilter( true );
105  filterCollection->setSourceModel( mRightsFilterModel );
106  filterCollection->setFilterCaseSensitivity( Qt::CaseInsensitive );
107  mView->setModel( filterCollection );
108 
109  mParent->connect( filterCollectionLineEdit, SIGNAL(textChanged(QString)),
110  filterCollection, SLOT(setFilterFixedString(QString)) );
111 
112  mParent->connect( mView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
113  mParent, SLOT(slotSelectionChanged()) );
114 
115  mParent->connect( mView, SIGNAL(doubleClicked(QModelIndex)),
116  mParent, SLOT(accept()) );
117 
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 
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  const bool isVirtual = Akonadi::CollectionUtils::isVirtual( parentCollection );
155 
156  mParent->enableButton( KDialog::User1, (canCreateChildCollections && !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 }
175 
176 
177 
178 bool CollectionDialog::Private::canCreateCollection( const Akonadi::Collection &parentCollection ) const
179 {
180  if ( !parentCollection.isValid() )
181  return false;
182 
183  if ( ( parentCollection.rights() & Akonadi::Collection::CanCreateCollection ) ) {
184  const QStringList dialogMimeTypeFilter = mParent->mimeTypeFilter();
185  const QStringList parentCollectionMimeTypes = parentCollection.contentMimeTypes();
186  Q_FOREACH ( const QString& mimetype, dialogMimeTypeFilter ) {
187  if ( parentCollectionMimeTypes.contains( mimetype ) )
188  return true;
189  }
190  return true;
191  }
192  return false;
193 }
194 
195 
196 void CollectionDialog::Private::slotAddChildCollection()
197 {
198  const Akonadi::Collection parentCollection = mParent->selectedCollection();
199  if ( canCreateCollection( parentCollection ) ) {
200  const QString name = KInputDialog::getText( i18nc( "@title:window", "New Folder" ),
201  i18nc( "@label:textbox, name of a thing", "Name" ),
202  QString(), 0, mParent );
203  if ( name.isEmpty() )
204  return;
205 
206  Akonadi::Collection collection;
207  collection.setName( name );
208  collection.setParentCollection( parentCollection );
209  Akonadi::CollectionCreateJob *job = new Akonadi::CollectionCreateJob( collection );
210  connect( job, SIGNAL(result(KJob*)), mParent, SLOT(slotCollectionCreationResult(KJob*)) );
211  }
212 }
213 
214 void CollectionDialog::Private::slotCollectionCreationResult( KJob* job )
215 {
216  if ( job->error() ) {
217  KMessageBox::error( mParent, i18n( "Could not create folder: %1", job->errorString() ),
218  i18n( "Folder creation failed" ) );
219  }
220 }
221 
222 
223 
224 CollectionDialog::CollectionDialog( QWidget *parent )
225  : KDialog( parent ),
226  d( new Private( 0, this, CollectionDialog::None ) )
227 {
228 }
229 
230 CollectionDialog::CollectionDialog( QAbstractItemModel *model, QWidget *parent )
231  : KDialog( parent ),
232  d( new Private( model, this, CollectionDialog::None ) )
233 {
234 }
235 
236 CollectionDialog::CollectionDialog( CollectionDialogOptions options, QAbstractItemModel *model, QWidget *parent )
237  : KDialog( parent ),
238  d( new Private( model, this, options ) )
239 {
240 }
241 
242 
243 CollectionDialog::~CollectionDialog()
244 {
245  delete d;
246 }
247 
248 Akonadi::Collection CollectionDialog::selectedCollection() const
249 {
250  if ( selectionMode() == QAbstractItemView::SingleSelection ) {
251  const QModelIndex index = d->mView->currentIndex();
252  if ( index.isValid() )
253  return index.model()->data( index, EntityTreeModel::CollectionRole ).value<Collection>();
254  }
255 
256  return Collection();
257 }
258 
259 Akonadi::Collection::List CollectionDialog::selectedCollections() const
260 {
261  Collection::List collections;
262  const QItemSelectionModel *selectionModel = d->mView->selectionModel();
263  const QModelIndexList selectedIndexes = selectionModel->selectedIndexes();
264  foreach ( const QModelIndex &index, selectedIndexes ) {
265  if ( index.isValid() ) {
266  const Collection collection = index.model()->data( index, EntityTreeModel::CollectionRole ).value<Collection>();
267  if ( collection.isValid() )
268  collections.append( collection );
269  }
270  }
271 
272  return collections;
273 }
274 
275 void CollectionDialog::setMimeTypeFilter( const QStringList &mimeTypes )
276 {
277  d->mMimeTypeFilterModel->clearFilters();
278  d->mMimeTypeFilterModel->addMimeTypeFilters( mimeTypes );
279 
280  if ( d->mMonitor )
281  foreach ( const QString &mimetype, mimeTypes )
282  d->mMonitor->setMimeTypeMonitored( mimetype );
283 }
284 
285 QStringList CollectionDialog::mimeTypeFilter() const
286 {
287  return d->mMimeTypeFilterModel->mimeTypeFilters();
288 }
289 
290 void CollectionDialog::setAccessRightsFilter( Collection::Rights rights )
291 {
292  d->mRightsFilterModel->setAccessRights( rights );
293 }
294 
295 Akonadi::Collection::Rights CollectionDialog::accessRightsFilter() const
296 {
297  return d->mRightsFilterModel->accessRights();
298 }
299 
300 void CollectionDialog::setDescription( const QString &text )
301 {
302  d->mTextLabel->setText( text );
303  d->mTextLabel->show();
304 }
305 
306 void CollectionDialog::setDefaultCollection( const Collection &collection )
307 {
308  d->mSelectionHandler->waitForCollection( collection );
309 }
310 
311 void CollectionDialog::setSelectionMode( QAbstractItemView::SelectionMode mode )
312 {
313  d->mView->setSelectionMode( mode );
314 }
315 
316 QAbstractItemView::SelectionMode CollectionDialog::selectionMode() const
317 {
318  return d->mView->selectionMode();
319 }
320 
321 void CollectionDialog::changeCollectionDialogOptions( CollectionDialogOptions options )
322 {
323  d->changeCollectionDialogOptions( options );
324 }
325 
326 #include "collectiondialog.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Nov 26 2012 16:48:18 by doxygen 1.8.1.2 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.9.3 API Reference

Skip menu "kdepimlibs-4.9.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • 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