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

akonadi

  • akonadi
collectiondialog_mobile.cpp
1 /*
2  Copyright 2010 Tobias Koenig <tokoe@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 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 */
19 
20 #include "collectiondialog_mobile_p.h"
21 #include "asyncselectionhandler_p.h"
22 #include "collectiondialog.h"
23 
24 #include <qplatformdefs.h>
25 
26 #include <kdescendantsproxymodel.h>
27 
28 #include <akonadi/changerecorder.h>
29 #include <akonadi/collectioncreatejob.h>
30 #include <akonadi/collectionfilterproxymodel.h>
31 #include <akonadi/collectionutils_p.h>
32 #include <akonadi/entityrightsfiltermodel.h>
33 #include <akonadi/entitytreemodel.h>
34 
35 #include <KLocalizedString>
36 #include <KInputDialog>
37 #include <KUrl>
38 #include <KMessageBox>
39 #include <KStandardDirs>
40 
41 #include <QApplication>
42 #include <QDeclarativeContext>
43 #include <QDeclarativeEngine>
44 #include <QDeclarativeView>
45 
46 using namespace Akonadi;
47 
48 CollectionDialog::Private::Private( QAbstractItemModel *customModel, CollectionDialog *parent, CollectionDialogOptions options )
49  : QObject( parent ),
50  mParent( parent ),
51  mSelectionMode( QAbstractItemView::SingleSelection ),
52  mOkButtonEnabled( false ),
53  mCancelButtonEnabled( true ),
54  mCreateButtonEnabled( false )
55 {
56  // setup GUI
57  mView = new QDeclarativeView( mParent );
58  mView->setResizeMode( QDeclarativeView::SizeRootObjectToView );
59 
60  mParent->setMainWidget( mView );
61  mParent->setButtons( KDialog::None );
62 
63  changeCollectionDialogOptions( options );
64 
65  QAbstractItemModel *baseModel;
66 
67  if ( customModel ) {
68  baseModel = customModel;
69  } else {
70  mMonitor = new Akonadi::ChangeRecorder( mParent );
71  mMonitor->fetchCollection( true );
72  mMonitor->setCollectionMonitored( Akonadi::Collection::root() );
73 
74  mModel = new EntityTreeModel( mMonitor, mParent );
75  mModel->setItemPopulationStrategy( EntityTreeModel::NoItemPopulation );
76 
77  baseModel = mModel;
78  }
79 
80  KDescendantsProxyModel *proxyModel = new KDescendantsProxyModel( parent );
81  proxyModel->setDisplayAncestorData( true );
82  proxyModel->setSourceModel( baseModel );
83 
84  mMimeTypeFilterModel = new CollectionFilterProxyModel( parent );
85  mMimeTypeFilterModel->setSourceModel( proxyModel );
86 
87  mRightsFilterModel = new EntityRightsFilterModel( parent );
88  mRightsFilterModel->setSourceModel( mMimeTypeFilterModel );
89 
90  mFilterModel = new QSortFilterProxyModel( parent );
91  mFilterModel->setFilterCaseSensitivity( Qt::CaseInsensitive );
92  mFilterModel->setSourceModel( mRightsFilterModel );
93 
94  mSelectionModel = new QItemSelectionModel( mFilterModel );
95  mParent->connect( mSelectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
96  SLOT(slotSelectionChanged()) );
97  mParent->connect( mSelectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
98  this, SLOT(selectionChanged(QItemSelection,QItemSelection)) );
99 
100  mSelectionHandler = new AsyncSelectionHandler( mFilterModel, mParent );
101  mParent->connect( mSelectionHandler, SIGNAL(collectionAvailable(QModelIndex)),
102  SLOT(slotCollectionAvailable(QModelIndex)) );
103 
104  foreach ( const QString &importPath, KGlobal::dirs()->findDirs( "module", QLatin1String( "imports" ) ) ) {
105  mView->engine()->addImportPath( importPath );
106  }
107 
108  mView->rootContext()->setContextProperty( QLatin1String( "dialogController" ), this );
109  mView->rootContext()->setContextProperty( QLatin1String( "collectionModel" ), mFilterModel );
110 
111  // QUICKHACK: since we have no KDE integration plugin available in kdelibs, we have to do the translation in C++ space
112  mView->rootContext()->setContextProperty( QLatin1String( "okButtonText" ), KStandardGuiItem::ok().text().remove( QLatin1Char( '&' ) ) );
113  mView->rootContext()->setContextProperty( QLatin1String( "cancelButtonText" ), KStandardGuiItem::cancel().text().remove( QLatin1Char( '&' ) ) );
114  mView->rootContext()->setContextProperty( QLatin1String( "createButtonText" ), i18n( "&New Subfolder..." ).remove( QLatin1Char( '&' ) ) );
115 
116  mView->setSource( KUrl::fromLocalFile( KStandardDirs::locate( "data", QLatin1String( "akonadi-kde/qml/CollectionDialogMobile.qml" ) ) ) );
117 
118 #if defined (Q_WS_MAEMO_5) || defined (Q_OS_WINCE) || defined (MEEGO_EDITION_HARMATTAN)
119  mParent->setWindowState( Qt::WindowFullScreen );
120 #else
121  // on the desktop start with a nice size
122  mParent->resize( 800, 480 );
123 #endif
124 }
125 
126 CollectionDialog::Private::~Private()
127 {
128 }
129 
130 void CollectionDialog::Private::slotCollectionAvailable( const QModelIndex &index )
131 {
132  mSelectionModel->setCurrentIndex( index, QItemSelectionModel::ClearAndSelect );
133 }
134 
135 void CollectionDialog::Private::slotSelectionChanged()
136 {
137  mOkButtonEnabled = mSelectionModel->hasSelection();
138  if ( mAllowToCreateNewChildCollection ) {
139  const Akonadi::Collection parentCollection = mParent->selectedCollection();
140  const bool canCreateChildCollections = canCreateCollection( parentCollection );
141  const bool isVirtual = parentCollection.isVirtual();
142 
143  mCreateButtonEnabled = ( canCreateChildCollections && !isVirtual );
144  if ( parentCollection.isValid() ) {
145  const bool canCreateItems = ( parentCollection.rights() & Akonadi::Collection::CanCreateItem );
146  mOkButtonEnabled = canCreateItems;
147  }
148  }
149 
150  emit buttonStatusChanged();
151 }
152 
153 void CollectionDialog::Private::changeCollectionDialogOptions( CollectionDialogOptions options )
154 {
155  mAllowToCreateNewChildCollection = ( options & AllowToCreateNewChildCollection );
156  emit buttonStatusChanged();
157 }
158 
159 bool CollectionDialog::Private::canCreateCollection( const Akonadi::Collection &parentCollection ) const
160 {
161  if ( !parentCollection.isValid() ) {
162  return false;
163  }
164 
165  if ( ( parentCollection.rights() & Akonadi::Collection::CanCreateCollection ) ) {
166  const QStringList dialogMimeTypeFilter = mParent->mimeTypeFilter();
167  const QStringList parentCollectionMimeTypes = parentCollection.contentMimeTypes();
168  Q_FOREACH ( const QString& mimetype, dialogMimeTypeFilter ) {
169  if ( parentCollectionMimeTypes.contains( mimetype ) ) {
170  return true;
171  }
172  }
173  return true;
174  }
175  return false;
176 }
177 
178 void CollectionDialog::Private::slotAddChildCollection()
179 {
180  const Akonadi::Collection parentCollection = mParent->selectedCollection();
181  if ( canCreateCollection( parentCollection ) ) {
182  const QString name = KInputDialog::getText( i18nc( "@title:window", "New Folder" ),
183  i18nc( "@label:textbox, name of a thing", "Name" ),
184  QString(), 0, mParent );
185  if ( name.isEmpty() ) {
186  return;
187  }
188 
189  Akonadi::Collection collection;
190  collection.setName( name );
191  collection.setParentCollection( parentCollection );
192  Akonadi::CollectionCreateJob *job = new Akonadi::CollectionCreateJob( collection );
193  connect( job, SIGNAL(result(KJob*)), mParent, SLOT(slotCollectionCreationResult(KJob*)) );
194  }
195 }
196 
197 void CollectionDialog::Private::slotCollectionCreationResult( KJob* job )
198 {
199  if ( job->error() ) {
200  KMessageBox::error( mParent, i18n( "Could not create folder: %1", job->errorString() ),
201  i18n( "Folder creation failed" ) );
202  }
203 }
204 
205 void CollectionDialog::Private::setDescriptionText( const QString &text )
206 {
207  mDescriptionText = text;
208  emit descriptionTextChanged();
209 }
210 
211 QString CollectionDialog::Private::descriptionText() const
212 {
213  return mDescriptionText;
214 }
215 
216 bool CollectionDialog::Private::okButtonEnabled() const
217 {
218  return mOkButtonEnabled;
219 }
220 
221 bool CollectionDialog::Private::cancelButtonEnabled() const
222 {
223  return mCancelButtonEnabled;
224 }
225 
226 bool CollectionDialog::Private::createButtonEnabled() const
227 {
228  return mCreateButtonEnabled;
229 }
230 
231 bool CollectionDialog::Private::createButtonVisible() const
232 {
233  return mAllowToCreateNewChildCollection;
234 }
235 
236 void CollectionDialog::Private::okClicked()
237 {
238  mParent->accept();
239 }
240 
241 void CollectionDialog::Private::cancelClicked()
242 {
243  mParent->reject();
244 }
245 
246 void CollectionDialog::Private::createClicked()
247 {
248  slotAddChildCollection();
249 }
250 
251 void CollectionDialog::Private::setCurrentIndex( int row )
252 {
253  const QModelIndex index = mSelectionModel->model()->index( row, 0 );
254  mSelectionModel->select( index, QItemSelectionModel::ClearAndSelect );
255 }
256 
257 void CollectionDialog::Private::setFilterText( const QString &text )
258 {
259  mFilterModel->setFilterFixedString( text );
260 }
261 
262 void CollectionDialog::Private::selectionChanged( const QItemSelection &selection, const QItemSelection& )
263 {
264  if ( selection.isEmpty() ) {
265  return;
266  }
267 
268  emit selectionChanged( selection.indexes().first().row() );
269 }
270 
271 CollectionDialog::CollectionDialog( QWidget *parent )
272  : KDialog( parent, Qt::Window ),
273  d( new Private( 0, this, CollectionDialog::None ) )
274 {
275 }
276 
277 CollectionDialog::CollectionDialog( QAbstractItemModel *model, QWidget *parent )
278  : KDialog( parent, Qt::Window ),
279  d( new Private( model, this, CollectionDialog::None ) )
280 {
281 }
282 
283 CollectionDialog::CollectionDialog( CollectionDialogOptions options, QAbstractItemModel *model, QWidget *parent )
284  : KDialog( parent, Qt::Window ),
285  d( new Private( model, this, options ) )
286 {
287 }
288 
289 
290 CollectionDialog::~CollectionDialog()
291 {
292 }
293 
294 Akonadi::Collection CollectionDialog::selectedCollection() const
295 {
296  if ( !d->mSelectionModel->hasSelection() ) {
297  return Akonadi::Collection();
298  }
299 
300  return d->mSelectionModel->selectedRows().first().data( Akonadi::EntityTreeModel::CollectionRole ).value<Akonadi::Collection>();
301 }
302 
303 Akonadi::Collection::List CollectionDialog::selectedCollections() const
304 {
305  if ( !d->mSelectionModel->hasSelection() ) {
306  return Akonadi::Collection::List();
307  }
308 
309  return ( Akonadi::Collection::List() << d->mSelectionModel->selectedRows().first().data( Akonadi::EntityTreeModel::CollectionRole ).value<Akonadi::Collection>() );
310 }
311 
312 void CollectionDialog::setMimeTypeFilter( const QStringList &mimeTypes )
313 {
314  d->mMimeTypeFilterModel->clearFilters();
315  d->mMimeTypeFilterModel->addMimeTypeFilters( mimeTypes );
316 }
317 
318 QStringList CollectionDialog::mimeTypeFilter() const
319 {
320  return d->mMimeTypeFilterModel->mimeTypes();
321 }
322 
323 void CollectionDialog::setAccessRightsFilter( Collection::Rights rights )
324 {
325  d->mRightsFilterModel->setAccessRights( rights );
326 }
327 
328 Akonadi::Collection::Rights CollectionDialog::accessRightsFilter() const
329 {
330  return d->mRightsFilterModel->accessRights();
331 }
332 
333 void CollectionDialog::setDescription( const QString &text )
334 {
335  d->setDescriptionText( text );
336 }
337 
338 void CollectionDialog::setDefaultCollection( const Collection &collection )
339 {
340  d->mSelectionHandler->waitForCollection( collection );
341 }
342 
343 void CollectionDialog::setSelectionMode( QAbstractItemView::SelectionMode mode )
344 {
345  d->mSelectionMode = mode;
346 }
347 
348 QAbstractItemView::SelectionMode CollectionDialog::selectionMode() const
349 {
350  return d->mSelectionMode;
351 }
352 
353 void CollectionDialog::changeCollectionDialogOptions( CollectionDialogOptions options )
354 {
355  d->changeCollectionDialogOptions( options );
356 }
357 
358 #include "collectiondialog.moc"
359 #include "moc_collectiondialog_mobile_p.cpp"
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::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::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