akonadi
collectiondialog_mobile.cpp
00001 /* 00002 Copyright 2010 Tobias Koenig <tokoe@kde.org> 00003 00004 This library is free software; you can redistribute it and/or modify it 00005 under the terms of the GNU Library General Public License as published by 00006 the Free Software Foundation; either version 2 of the License, or (at your 00007 option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, but WITHOUT 00010 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00011 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00012 License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to the 00016 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00017 02110-1301, USA. 00018 */ 00019 00020 #include "collectiondialog.h" 00021 00022 #include <akonadi/collectioncombobox.h> 00023 #include <akonadi/collectioncreatejob.h> 00024 #include <akonadi/collectionutils_p.h> 00025 00026 #include <QtGui/QLabel> 00027 #include <QtGui/QVBoxLayout> 00028 00029 #include <KLocale> 00030 #include <KInputDialog> 00031 #include <KMessageBox> 00032 00033 using namespace Akonadi; 00034 00035 class CollectionDialog::Private 00036 { 00037 public: 00038 Private( QAbstractItemModel *customModel, CollectionDialog *parent, CollectionDialogOptions options ) 00039 : mParent( parent ), 00040 mSelectionMode( QAbstractItemView::SingleSelection ) 00041 { 00042 // setup GUI 00043 QWidget *widget = mParent->mainWidget(); 00044 QVBoxLayout *layout = new QVBoxLayout( widget ); 00045 00046 changeCollectionDialogOptions( options ); 00047 00048 mTextLabel = new QLabel; 00049 layout->addWidget( mTextLabel ); 00050 mTextLabel->hide(); 00051 00052 mCollectionComboBox = new CollectionComboBox( customModel, widget ); 00053 layout->addWidget( mCollectionComboBox ); 00054 mParent->connect( mCollectionComboBox, SIGNAL( currentIndexChanged( int ) ), SLOT( slotSelectionChanged() ) ); 00055 00056 mParent->enableButton( KDialog::Ok, false ); 00057 } 00058 00059 ~Private() 00060 { 00061 } 00062 00063 void slotCollectionAvailable( const QModelIndex& ) 00064 { 00065 } 00066 00067 CollectionDialog *mParent; 00068 00069 QLabel *mTextLabel; 00070 CollectionComboBox *mCollectionComboBox; 00071 QAbstractItemView::SelectionMode mSelectionMode; 00072 bool mAllowToCreateNewChildCollection; 00073 00074 void slotSelectionChanged(); 00075 void slotAddChildCollection(); 00076 void slotCollectionCreationResult( KJob* job ); 00077 bool canCreateCollection( const Akonadi::Collection &parentCollection ) const; 00078 void changeCollectionDialogOptions( CollectionDialogOptions options ); 00079 00080 }; 00081 00082 void CollectionDialog::Private::slotSelectionChanged() 00083 { 00084 mParent->enableButton( KDialog::Ok, mCollectionComboBox->count() > 0 ); 00085 if ( mAllowToCreateNewChildCollection ) { 00086 const Akonadi::Collection parentCollection = mParent->selectedCollection(); 00087 const bool canCreateChildCollections = canCreateCollection( parentCollection ); 00088 const bool isVirtual = Akonadi::CollectionUtils::isVirtual( parentCollection ); 00089 00090 mParent->enableButton( KDialog::User1, (canCreateChildCollections && !isVirtual) ); 00091 if ( parentCollection.isValid() ) { 00092 const bool canCreateItems = (parentCollection.rights() & Akonadi::Collection::CanCreateItem); 00093 mParent->enableButton( KDialog::Ok, canCreateItems ); 00094 } 00095 } 00096 } 00097 00098 void CollectionDialog::Private::changeCollectionDialogOptions( CollectionDialogOptions options ) 00099 { 00100 mAllowToCreateNewChildCollection = ( options & AllowToCreateNewChildCollection ); 00101 if ( mAllowToCreateNewChildCollection ) { 00102 mParent->setButtons( Ok | Cancel | User1 ); 00103 mParent->setButtonGuiItem( User1, KGuiItem( i18n( "&New Subfolder..." ), QLatin1String( "folder-new" ), 00104 i18n( "Create a new subfolder under the currently selected folder" ) ) ); 00105 mParent->enableButton( KDialog::User1, false ); 00106 connect( mParent, SIGNAL( user1Clicked() ), mParent, SLOT( slotAddChildCollection() ) ); 00107 } 00108 } 00109 00110 00111 00112 bool CollectionDialog::Private::canCreateCollection( const Akonadi::Collection &parentCollection ) const 00113 { 00114 if ( !parentCollection.isValid() ) 00115 return false; 00116 00117 if ( ( parentCollection.rights() & Akonadi::Collection::CanCreateCollection ) ) { 00118 const QStringList dialogMimeTypeFilter = mParent->mimeTypeFilter(); 00119 const QStringList parentCollectionMimeTypes = parentCollection.contentMimeTypes(); 00120 Q_FOREACH ( const QString& mimetype, dialogMimeTypeFilter ) { 00121 if ( parentCollectionMimeTypes.contains( mimetype ) ) 00122 return true; 00123 } 00124 return true; 00125 } 00126 return false; 00127 } 00128 00129 00130 void CollectionDialog::Private::slotAddChildCollection() 00131 { 00132 const Akonadi::Collection parentCollection = mParent->selectedCollection(); 00133 if ( canCreateCollection( parentCollection ) ) { 00134 const QString name = KInputDialog::getText( i18nc( "@title:window", "New Folder" ), 00135 i18nc( "@label:textbox, name of a thing", "Name" ), 00136 QString(), 0, mParent ); 00137 if ( name.isEmpty() ) 00138 return; 00139 00140 Akonadi::Collection collection; 00141 collection.setName( name ); 00142 collection.setParentCollection( parentCollection ); 00143 Akonadi::CollectionCreateJob *job = new Akonadi::CollectionCreateJob( collection ); 00144 connect( job, SIGNAL( result( KJob* ) ), mParent, SLOT( slotCollectionCreationResult( KJob* ) ) ); 00145 } 00146 } 00147 00148 void CollectionDialog::Private::slotCollectionCreationResult( KJob* job ) 00149 { 00150 if ( job->error() ) { 00151 KMessageBox::error( mParent, i18n( "Could not create folder: %1", job->errorString() ), 00152 i18n( "Folder creation failed" ) ); 00153 } 00154 } 00155 00156 00157 00158 CollectionDialog::CollectionDialog( QWidget *parent ) 00159 : KDialog( parent ), 00160 d( new Private( 0, this, CollectionDialog::None ) ) 00161 { 00162 } 00163 00164 CollectionDialog::CollectionDialog( QAbstractItemModel *model, QWidget *parent ) 00165 : KDialog( parent ), 00166 d( new Private( model, this, CollectionDialog::None ) ) 00167 { 00168 } 00169 00170 CollectionDialog::CollectionDialog( CollectionDialogOptions options, QAbstractItemModel *model, QWidget *parent ) 00171 : KDialog( parent ), 00172 d( new Private( model, this, options ) ) 00173 { 00174 } 00175 00176 00177 CollectionDialog::~CollectionDialog() 00178 { 00179 delete d; 00180 } 00181 00182 Akonadi::Collection CollectionDialog::selectedCollection() const 00183 { 00184 return d->mCollectionComboBox->currentCollection(); 00185 } 00186 00187 Akonadi::Collection::List CollectionDialog::selectedCollections() const 00188 { 00189 return (Collection::List() << d->mCollectionComboBox->currentCollection()); 00190 } 00191 00192 void CollectionDialog::setMimeTypeFilter( const QStringList &mimeTypes ) 00193 { 00194 d->mCollectionComboBox->setMimeTypeFilter( mimeTypes ); 00195 } 00196 00197 QStringList CollectionDialog::mimeTypeFilter() const 00198 { 00199 return d->mCollectionComboBox->mimeTypeFilter(); 00200 } 00201 00202 void CollectionDialog::setAccessRightsFilter( Collection::Rights rights ) 00203 { 00204 d->mCollectionComboBox->setAccessRightsFilter( rights ); 00205 } 00206 00207 Akonadi::Collection::Rights CollectionDialog::accessRightsFilter() const 00208 { 00209 return d->mCollectionComboBox->accessRightsFilter(); 00210 } 00211 00212 void CollectionDialog::setDescription( const QString &text ) 00213 { 00214 d->mTextLabel->setText( text ); 00215 d->mTextLabel->show(); 00216 } 00217 00218 void CollectionDialog::setDefaultCollection( const Collection &collection ) 00219 { 00220 d->mCollectionComboBox->setDefaultCollection( collection ); 00221 } 00222 00223 void CollectionDialog::setSelectionMode( QAbstractItemView::SelectionMode mode ) 00224 { 00225 d->mSelectionMode = mode; 00226 } 00227 00228 QAbstractItemView::SelectionMode CollectionDialog::selectionMode() const 00229 { 00230 return d->mSelectionMode; 00231 } 00232 00233 void CollectionDialog::changeCollectionDialogOptions( CollectionDialogOptions options ) 00234 { 00235 d->changeCollectionDialogOptions( options ); 00236 } 00237 00238 #include "collectiondialog.moc"