• 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
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::slotFilterFixedString( const QString &filter)
136 {
137 }
138 
139 void CollectionDialog::Private::slotSelectionChanged()
140 {
141  mOkButtonEnabled = mSelectionModel->hasSelection();
142  if ( mAllowToCreateNewChildCollection ) {
143  const Akonadi::Collection parentCollection = mParent->selectedCollection();
144  const bool canCreateChildCollections = canCreateCollection( parentCollection );
145  const bool isVirtual = parentCollection.isVirtual();
146 
147  mCreateButtonEnabled = ( canCreateChildCollections && !isVirtual );
148  if ( parentCollection.isValid() ) {
149  const bool canCreateItems = ( parentCollection.rights() & Akonadi::Collection::CanCreateItem );
150  mOkButtonEnabled = canCreateItems;
151  }
152  }
153 
154  emit buttonStatusChanged();
155 }
156 
157 void CollectionDialog::Private::changeCollectionDialogOptions( CollectionDialogOptions options )
158 {
159  mAllowToCreateNewChildCollection = ( options & AllowToCreateNewChildCollection );
160  emit buttonStatusChanged();
161 }
162 
163 bool CollectionDialog::Private::canCreateCollection( const Akonadi::Collection &parentCollection ) const
164 {
165  if ( !parentCollection.isValid() ) {
166  return false;
167  }
168 
169  if ( ( parentCollection.rights() & Akonadi::Collection::CanCreateCollection ) ) {
170  const QStringList dialogMimeTypeFilter = mParent->mimeTypeFilter();
171  const QStringList parentCollectionMimeTypes = parentCollection.contentMimeTypes();
172  Q_FOREACH ( const QString& mimetype, dialogMimeTypeFilter ) {
173  if ( parentCollectionMimeTypes.contains( mimetype ) ) {
174  return true;
175  }
176  }
177  return true;
178  }
179  return false;
180 }
181 
182 void CollectionDialog::Private::slotAddChildCollection()
183 {
184  const Akonadi::Collection parentCollection = mParent->selectedCollection();
185  if ( canCreateCollection( parentCollection ) ) {
186  const QString name = KInputDialog::getText( i18nc( "@title:window", "New Folder" ),
187  i18nc( "@label:textbox, name of a thing", "Name" ),
188  QString(), 0, mParent );
189  if ( name.isEmpty() ) {
190  return;
191  }
192 
193  Akonadi::Collection collection;
194  collection.setName( name );
195  collection.setParentCollection( parentCollection );
196  Akonadi::CollectionCreateJob *job = new Akonadi::CollectionCreateJob( collection );
197  connect( job, SIGNAL(result(KJob*)), mParent, SLOT(slotCollectionCreationResult(KJob*)) );
198  }
199 }
200 
201 void CollectionDialog::Private::slotCollectionCreationResult( KJob* job )
202 {
203  if ( job->error() ) {
204  KMessageBox::error( mParent, i18n( "Could not create folder: %1", job->errorString() ),
205  i18n( "Folder creation failed" ) );
206  }
207 }
208 
209 void CollectionDialog::Private::setDescriptionText( const QString &text )
210 {
211  mDescriptionText = text;
212  emit descriptionTextChanged();
213 }
214 
215 QString CollectionDialog::Private::descriptionText() const
216 {
217  return mDescriptionText;
218 }
219 
220 bool CollectionDialog::Private::okButtonEnabled() const
221 {
222  return mOkButtonEnabled;
223 }
224 
225 bool CollectionDialog::Private::cancelButtonEnabled() const
226 {
227  return mCancelButtonEnabled;
228 }
229 
230 bool CollectionDialog::Private::createButtonEnabled() const
231 {
232  return mCreateButtonEnabled;
233 }
234 
235 bool CollectionDialog::Private::createButtonVisible() const
236 {
237  return mAllowToCreateNewChildCollection;
238 }
239 
240 void CollectionDialog::Private::okClicked()
241 {
242  mParent->accept();
243 }
244 
245 void CollectionDialog::Private::cancelClicked()
246 {
247  mParent->reject();
248 }
249 
250 void CollectionDialog::Private::createClicked()
251 {
252  slotAddChildCollection();
253 }
254 
255 void CollectionDialog::Private::setCurrentIndex( int row )
256 {
257  const QModelIndex index = mSelectionModel->model()->index( row, 0 );
258  mSelectionModel->select( index, QItemSelectionModel::ClearAndSelect );
259 }
260 
261 void CollectionDialog::Private::setFilterText( const QString &text )
262 {
263  mFilterModel->setFilterFixedString( text );
264 }
265 
266 void CollectionDialog::Private::selectionChanged( const QItemSelection &selection, const QItemSelection& )
267 {
268  if ( selection.isEmpty() ) {
269  return;
270  }
271 
272  emit selectionChanged( selection.indexes().first().row() );
273 }
274 
275 CollectionDialog::CollectionDialog( QWidget *parent )
276  : KDialog( parent, Qt::Window ),
277  d( new Private( 0, this, CollectionDialog::None ) )
278 {
279 }
280 
281 CollectionDialog::CollectionDialog( QAbstractItemModel *model, QWidget *parent )
282  : KDialog( parent, Qt::Window ),
283  d( new Private( model, this, CollectionDialog::None ) )
284 {
285 }
286 
287 CollectionDialog::CollectionDialog( CollectionDialogOptions options, QAbstractItemModel *model, QWidget *parent )
288  : KDialog( parent, Qt::Window ),
289  d( new Private( model, this, options ) )
290 {
291 }
292 
293 CollectionDialog::~CollectionDialog()
294 {
295 }
296 
297 Akonadi::Collection CollectionDialog::selectedCollection() const
298 {
299  if ( !d->mSelectionModel->hasSelection() ) {
300  return Akonadi::Collection();
301  }
302 
303  return d->mSelectionModel->selectedRows().first().data( Akonadi::EntityTreeModel::CollectionRole ).value<Akonadi::Collection>();
304 }
305 
306 Akonadi::Collection::List CollectionDialog::selectedCollections() const
307 {
308  if ( !d->mSelectionModel->hasSelection() ) {
309  return Akonadi::Collection::List();
310  }
311 
312  return ( Akonadi::Collection::List() << d->mSelectionModel->selectedRows().first().data( Akonadi::EntityTreeModel::CollectionRole ).value<Akonadi::Collection>() );
313 }
314 
315 void CollectionDialog::setMimeTypeFilter( const QStringList &mimeTypes )
316 {
317  d->mMimeTypeFilterModel->clearFilters();
318  d->mMimeTypeFilterModel->addMimeTypeFilters( mimeTypes );
319 }
320 
321 QStringList CollectionDialog::mimeTypeFilter() const
322 {
323  return d->mMimeTypeFilterModel->mimeTypes();
324 }
325 
326 void CollectionDialog::setAccessRightsFilter( Collection::Rights rights )
327 {
328  d->mRightsFilterModel->setAccessRights( rights );
329 }
330 
331 Akonadi::Collection::Rights CollectionDialog::accessRightsFilter() const
332 {
333  return d->mRightsFilterModel->accessRights();
334 }
335 
336 void CollectionDialog::setDescription( const QString &text )
337 {
338  d->setDescriptionText( text );
339 }
340 
341 void CollectionDialog::setDefaultCollection( const Collection &collection )
342 {
343  d->mSelectionHandler->waitForCollection( collection );
344 }
345 
346 void CollectionDialog::setSelectionMode( QAbstractItemView::SelectionMode mode )
347 {
348  d->mSelectionMode = mode;
349 }
350 
351 QAbstractItemView::SelectionMode CollectionDialog::selectionMode() const
352 {
353  return d->mSelectionMode;
354 }
355 
356 void CollectionDialog::changeCollectionDialogOptions( CollectionDialogOptions options )
357 {
358  d->changeCollectionDialogOptions( options );
359 }
360 
361 #include "moc_collectiondialog.cpp"
362 #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:233
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'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-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:00:26 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