27 #include <Akonadi/Collection>
28 #include <Akonadi/CollectionCreateJob>
29 #include <Akonadi/EntityMimeTypeFilterModel>
30 #include <Akonadi/EntityTreeModel>
32 #include <KInputDialog>
34 #include <KMessageBox>
38 #include <QVBoxLayout>
40 namespace MailCommon {
42 class FolderSelectionDialog::FolderSelectionDialogPrivate
45 FolderSelectionDialogPrivate()
46 : folderTreeWidget( 0 ),
47 mNotAllowToCreateNewFolder( false ),
48 mUseGlobalSettings( true )
51 FolderTreeWidget *folderTreeWidget;
52 bool mNotAllowToCreateNewFolder;
53 bool mUseGlobalSettings;
57 :
KDialog( parent ), d( new FolderSelectionDialogPrivate() )
59 setObjectName( QLatin1String(
"folder dialog") );
63 if ( d->mNotAllowToCreateNewFolder ) {
64 setButtons( Ok | Cancel );
66 setButtons( Ok | Cancel | User1 );
69 KGuiItem( i18n(
"&New Subfolder..." ), QLatin1String(
"folder-new"),
70 i18n(
"Create a new subfolder under the currently selected folder" ) ) );
74 QVBoxLayout *layout =
new QVBoxLayout( widget );
75 layout->setMargin( 0 );
83 FolderTreeWidgetProxyModel::FolderTreeWidgetProxyModelOptions optReadableProxy =
96 d->folderTreeWidget =
new FolderTreeWidget(
this, 0, opt, optReadableProxy );
97 d->folderTreeWidget->readConfig();
98 d->folderTreeWidget->disableContextMenuAndExtraColumn();
99 d->folderTreeWidget->folderTreeWidgetProxyModel()->setEnabledCheck( ( options &
EnableCheck ) );
101 d->folderTreeWidget->folderTreeView()->disableSaveConfig();
103 #ifndef QT_NO_DRAGANDDROP
104 d->folderTreeWidget->folderTreeView()->setDragDropMode( QAbstractItemView::NoDragDrop );
106 layout->addWidget( d->folderTreeWidget );
108 enableButton( KDialog::Ok,
false );
109 if ( !d->mNotAllowToCreateNewFolder ) {
110 enableButton( KDialog::User1,
false );
111 connect(
this, SIGNAL(user1Clicked()),
this, SLOT(slotAddChildFolder()) );
112 d->folderTreeWidget->folderTreeView()->setContextMenuPolicy(Qt::CustomContextMenu);
113 connect( d->folderTreeWidget->folderTreeView(), SIGNAL(customContextMenuRequested(QPoint)),
114 SLOT(slotFolderTreeWidgetContextMenuRequested(QPoint)) );
118 connect( d->folderTreeWidget->selectionModel(),
119 SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
120 this, SLOT(slotSelectionChanged()) );
121 connect( d->folderTreeWidget->folderTreeWidgetProxyModel(),
122 SIGNAL(rowsInserted(QModelIndex,
int,
int)),
123 this, SLOT(rowsInserted(QModelIndex,
int,
int)) );
125 connect( d->folderTreeWidget->folderTreeView(),
126 SIGNAL(doubleClicked(QModelIndex)),
127 this, SLOT(slotDoubleClick(QModelIndex)) );
140 void FolderSelectionDialog::slotFolderTreeWidgetContextMenuRequested(
const QPoint& pos)
142 if (isButtonEnabled( KDialog::User1 ) && d->folderTreeWidget->folderTreeView()->indexAt(pos).isValid()) {
144 menu.addAction(i18n(
"&New Subfolder..." ),
this, SLOT(slotAddChildFolder()));
145 menu.exec(QCursor::pos());
150 void FolderSelectionDialog::slotDoubleClick(
const QModelIndex& index)
153 const bool hasSelectedCollection =
154 ( d->folderTreeWidget->selectionModel()->selectedIndexes().count() > 0 );
155 if (hasSelectedCollection) {
162 d->folderTreeWidget->folderTreeView()->expandAll();
163 d->folderTreeWidget->folderTreeView()->setFocus();
168 if ( !event->spontaneous () ) {
171 view->scrollTo( view->currentIndex() );
173 KDialog::showEvent( event );
176 void FolderSelectionDialog::rowsInserted(
const QModelIndex &,
int,
int )
178 d->folderTreeWidget->folderTreeView()->expandAll();
184 if ( !parentCol.isValid() ) {
188 if ( ( parentCol.rights() & Akonadi::Collection::CanCreateCollection ) &&
189 parentCol.contentMimeTypes().contains( Akonadi::Collection::mimeType() ) ) {
195 void FolderSelectionDialog::slotAddChildFolder()
197 Akonadi::Collection parentCol;
199 const QString name = KInputDialog::getText(
200 i18nc(
"@title:window",
"New Folder" ),
201 i18nc(
"@label:textbox, name of a thing",
"Name" ),
202 QString(), 0,
this );
204 if ( name.isEmpty() ) {
208 Akonadi::Collection col;
210 col.parentCollection().setId( parentCol.id() );
211 Akonadi::CollectionCreateJob *job =
new Akonadi::CollectionCreateJob( col );
212 connect( job, SIGNAL(result(KJob*)),
this, SLOT(collectionCreationResult(KJob*)) );
216 void FolderSelectionDialog::collectionCreationResult( KJob *job )
218 if ( job->error() ) {
221 i18n(
"Could not create folder: %1", job->errorString() ),
222 i18n(
"Folder creation failed" ) );
226 void FolderSelectionDialog::slotSelectionChanged()
228 const bool enablebuttons =
229 ( d->folderTreeWidget->selectionModel()->selectedIndexes().count() > 0 );
230 enableButton( KDialog::Ok, enablebuttons );
232 if ( !d->mNotAllowToCreateNewFolder ) {
233 Akonadi::Collection parent;
235 if ( parent.isValid() ) {
237 enableButton( KDialog::Ok, fd->canCreateMessages() );
244 d->folderTreeWidget->setSelectionMode( mode );
249 return d->folderTreeWidget->selectionMode();
254 return d->folderTreeWidget->selectedCollection();
259 d->folderTreeWidget->selectCollectionFolder( collection );
264 return d->folderTreeWidget->selectedCollections();
273 const QSize size = group.readEntry(
"Size", QSize(500, 300) );
274 if ( size.isValid() ) {
277 if ( d->mUseGlobalSettings ) {
278 const Akonadi::Collection::Id
id =
SettingsIf->lastSelectedFolder();
281 d->folderTreeWidget->selectCollectionFolder( col );
289 group.writeEntry(
"Size", size() );
291 if ( d->mUseGlobalSettings ) {
293 if ( col.isValid() ) {
294 SettingsIf->setLastSelectedFolder( col.id() );
301 d->folderTreeWidget->clearFilter();
306 #include "folderselectiondialog.moc"
void setSelectedCollection(const Akonadi::Collection &collection)
void setSelectionMode(QAbstractItemView::SelectionMode mode)
FolderSelectionDialog(QWidget *parent, FolderSelectionDialog::SelectionFolderOptions options)
Akonadi::Collection::List selectedCollections() const
This is an enhanced EntityTreeView specially suited for the folders in KMail's main folder widget...
bool canCreateCollection(Akonadi::Collection &parentCol)
Akonadi::Collection collectionFromId(const Akonadi::Collection::Id &id) const
Returns the collection associated with the given id, or an invalid collection if not found...
static QSharedPointer< FolderCollection > forCollection(const Akonadi::Collection &coll, bool writeConfig=true)
void hideEvent(QHideEvent *)
Akonadi::Collection selectedCollection() const
QAbstractItemView::SelectionMode selectionMode() const
static const char * myConfigGroupName
void showEvent(QShowEvent *)