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

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