• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

mailcommon

  • sources
  • kde-4.14
  • kdepim
  • mailcommon
  • folder
folderselectiondialog.cpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-file-style: "gnu" -*-
2 
3  Copyright (c) 2009-2015 Montel Laurent <montel@kde.org>
4 
5  This program is free software; you can redistribute it and/or modify it
6  under the terms of the GNU General Public License, version 2, as
7  published by the Free Software Foundation.
8 
9  This program is distributed in the hope that it will be useful, but
10  WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License along
15  with this program; if not, write to the Free Software Foundation, Inc.,
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #include "folderselectiondialog.h"
20 
21 #include "foldercollection.h"
22 #include "foldertreeview.h"
23 #include "foldertreewidget.h"
24 #include "foldertreewidgetproxymodel.h"
25 #include "kernel/mailkernel.h"
26 
27 #include <Akonadi/Collection>
28 #include <Akonadi/CollectionCreateJob>
29 #include <Akonadi/EntityMimeTypeFilterModel>
30 #include <Akonadi/EntityTreeModel>
31 
32 #include <KInputDialog>
33 #include <KLocale>
34 #include <KMessageBox>
35 #include <KMenu>
36 
37 #include <QKeyEvent>
38 #include <QVBoxLayout>
39 
40 namespace MailCommon {
41 
42 class FolderSelectionDialog::FolderSelectionDialogPrivate
43 {
44 public:
45  FolderSelectionDialogPrivate()
46  : folderTreeWidget( 0 ),
47  mNotAllowToCreateNewFolder( false ),
48  mUseGlobalSettings( true )
49  {
50  }
51  FolderTreeWidget *folderTreeWidget;
52  bool mNotAllowToCreateNewFolder;
53  bool mUseGlobalSettings;
54 };
55 
56 FolderSelectionDialog::FolderSelectionDialog( QWidget *parent, SelectionFolderOptions options )
57  :KDialog( parent ), d( new FolderSelectionDialogPrivate() )
58 {
59  setObjectName( QLatin1String("folder dialog") );
60 
61  d->mNotAllowToCreateNewFolder = ( options & FolderSelectionDialog::NotAllowToCreateNewFolder );
62 
63  if ( d->mNotAllowToCreateNewFolder ) {
64  setButtons( Ok | Cancel );
65  } else {
66  setButtons( Ok | Cancel | User1 );
67  setButtonGuiItem(
68  User1,
69  KGuiItem( i18n( "&New Subfolder..." ), QLatin1String("folder-new"),
70  i18n( "Create a new subfolder under the currently selected folder" ) ) );
71  }
72 
73  QWidget *widget = mainWidget();
74  QVBoxLayout *layout = new QVBoxLayout( widget );
75  layout->setMargin( 0 );
76 
77  FolderTreeWidget::TreeViewOptions opt = FolderTreeWidget::None;
78  if ( options & FolderSelectionDialog::ShowUnreadCount ) {
79  opt |= FolderTreeWidget::ShowUnreadCount;
80  }
81  opt |= FolderTreeWidget::UseDistinctSelectionModel;
82 
83  FolderTreeWidgetProxyModel::FolderTreeWidgetProxyModelOptions optReadableProxy =
84  FolderTreeWidgetProxyModel::None;
85 
86  if ( options & FolderSelectionDialog::HideVirtualFolder ) {
87  optReadableProxy |= FolderTreeWidgetProxyModel::HideVirtualFolder;
88  }
89 
90  optReadableProxy |= FolderTreeWidgetProxyModel::HideSpecificFolder;
91 
92  if ( options & FolderSelectionDialog::HideOutboxFolder ) {
93  optReadableProxy |= FolderTreeWidgetProxyModel::HideOutboxFolder;
94  }
95 
96  d->folderTreeWidget = new FolderTreeWidget( this, 0, opt, optReadableProxy );
97  d->folderTreeWidget->readConfig();
98  d->folderTreeWidget->disableContextMenuAndExtraColumn();
99  d->folderTreeWidget->folderTreeWidgetProxyModel()->setEnabledCheck( ( options & EnableCheck ) );
100  //Necessary otherwise we overwrite tooltip config for all application
101  d->folderTreeWidget->folderTreeView()->disableSaveConfig();
102  d->folderTreeWidget->folderTreeView()->setTooltipsPolicy( FolderTreeWidget::DisplayNever );
103 #ifndef QT_NO_DRAGANDDROP
104  d->folderTreeWidget->folderTreeView()->setDragDropMode( QAbstractItemView::NoDragDrop );
105 #endif
106  layout->addWidget( d->folderTreeWidget );
107 
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)) );
115 
116  }
117 
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)) );
124 
125  connect( d->folderTreeWidget->folderTreeView(),
126  SIGNAL(doubleClicked(QModelIndex)),
127  this, SLOT(slotDoubleClick(QModelIndex)) );
128 
129  d->mUseGlobalSettings = !( options & NotUseGlobalSettings );
130  readConfig();
131 
132 }
133 
134 FolderSelectionDialog::~FolderSelectionDialog()
135 {
136  writeConfig();
137  delete d;
138 }
139 
140 void FolderSelectionDialog::slotFolderTreeWidgetContextMenuRequested(const QPoint& pos)
141 {
142  if (isButtonEnabled( KDialog::User1 ) && d->folderTreeWidget->folderTreeView()->indexAt(pos).isValid()) {
143  KMenu menu;
144  menu.addAction(i18n( "&New Subfolder..." ),this, SLOT(slotAddChildFolder()));
145  menu.exec(QCursor::pos());
146  }
147 }
148 
149 
150 void FolderSelectionDialog::slotDoubleClick(const QModelIndex& index)
151 {
152  Q_UNUSED( index );
153  const bool hasSelectedCollection =
154  ( d->folderTreeWidget->selectionModel()->selectedIndexes().count() > 0 );
155  if (hasSelectedCollection) {
156  accept();
157  }
158 }
159 
160 void FolderSelectionDialog::focusTreeView()
161 {
162  d->folderTreeWidget->folderTreeView()->expandAll();
163  d->folderTreeWidget->folderTreeView()->setFocus();
164 }
165 
166 void FolderSelectionDialog::showEvent( QShowEvent *event )
167 {
168  if ( !event->spontaneous () ) {
169  focusTreeView();
170  FolderTreeView *view = d->folderTreeWidget->folderTreeView();
171  view->scrollTo( view->currentIndex() );
172  }
173  KDialog::showEvent( event );
174 }
175 
176 void FolderSelectionDialog::rowsInserted( const QModelIndex &, int, int )
177 {
178  d->folderTreeWidget->folderTreeView()->expandAll();
179 }
180 
181 bool FolderSelectionDialog::canCreateCollection( Akonadi::Collection &parentCol )
182 {
183  parentCol = selectedCollection();
184  if ( !parentCol.isValid() ) {
185  return false;
186  }
187 
188  if ( ( parentCol.rights() & Akonadi::Collection::CanCreateCollection ) &&
189  parentCol.contentMimeTypes().contains( Akonadi::Collection::mimeType() ) ) {
190  return true;
191  }
192  return false;
193 }
194 
195 void FolderSelectionDialog::slotAddChildFolder()
196 {
197  Akonadi::Collection parentCol;
198  if ( canCreateCollection( 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 );
203 
204  if ( name.isEmpty() ) {
205  return;
206  }
207 
208  Akonadi::Collection col;
209  col.setName( name );
210  col.parentCollection().setId( parentCol.id() );
211  Akonadi::CollectionCreateJob *job = new Akonadi::CollectionCreateJob( col );
212  connect( job, SIGNAL(result(KJob*)), this, SLOT(collectionCreationResult(KJob*)) );
213  }
214 }
215 
216 void FolderSelectionDialog::collectionCreationResult( KJob *job )
217 {
218  if ( job->error() ) {
219  KMessageBox::error(
220  this,
221  i18n( "Could not create folder: %1", job->errorString() ),
222  i18n( "Folder creation failed" ) );
223  }
224 }
225 
226 void FolderSelectionDialog::slotSelectionChanged()
227 {
228  const bool enablebuttons =
229  ( d->folderTreeWidget->selectionModel()->selectedIndexes().count() > 0 );
230  enableButton( KDialog::Ok, enablebuttons );
231 
232  if ( !d->mNotAllowToCreateNewFolder ) {
233  Akonadi::Collection parent;
234  enableButton( KDialog::User1, canCreateCollection( parent ) );
235  if ( parent.isValid() ) {
236  const QSharedPointer<FolderCollection> fd( FolderCollection::forCollection( parent, false ) );
237  enableButton( KDialog::Ok, fd->canCreateMessages() );
238  }
239  }
240 }
241 
242 void FolderSelectionDialog::setSelectionMode( QAbstractItemView::SelectionMode mode )
243 {
244  d->folderTreeWidget->setSelectionMode( mode );
245 }
246 
247 QAbstractItemView::SelectionMode FolderSelectionDialog::selectionMode() const
248 {
249  return d->folderTreeWidget->selectionMode();
250 }
251 
252 Akonadi::Collection FolderSelectionDialog::selectedCollection() const
253 {
254  return d->folderTreeWidget->selectedCollection();
255 }
256 
257 void FolderSelectionDialog::setSelectedCollection( const Akonadi::Collection &collection )
258 {
259  d->folderTreeWidget->selectCollectionFolder( collection );
260 }
261 
262 Akonadi::Collection::List FolderSelectionDialog::selectedCollections() const
263 {
264  return d->folderTreeWidget->selectedCollections();
265 }
266 
267 static const char *myConfigGroupName = "FolderSelectionDialog";
268 
269 void FolderSelectionDialog::readConfig()
270 {
271  KConfigGroup group( KernelIf->config(), myConfigGroupName );
272 
273  const QSize size = group.readEntry( "Size", QSize(500, 300) );
274  if ( size.isValid() ) {
275  resize( size );
276  }
277  if ( d->mUseGlobalSettings ) {
278  const Akonadi::Collection::Id id = SettingsIf->lastSelectedFolder();
279  if ( id > -1 ) {
280  const Akonadi::Collection col = Kernel::self()->collectionFromId( id );
281  d->folderTreeWidget->selectCollectionFolder( col );
282  }
283  }
284 }
285 
286 void FolderSelectionDialog::writeConfig()
287 {
288  KConfigGroup group( KernelIf->config(), myConfigGroupName );
289  group.writeEntry( "Size", size() );
290 
291  if ( d->mUseGlobalSettings ) {
292  Akonadi::Collection col = selectedCollection();
293  if ( col.isValid() ) {
294  SettingsIf->setLastSelectedFolder( col.id() );
295  }
296  }
297 }
298 
299 void FolderSelectionDialog::hideEvent( QHideEvent * )
300 {
301  d->folderTreeWidget->clearFilter();
302 }
303 
304 }
305 
QHideEvent
MailCommon::FolderSelectionDialog::setSelectedCollection
void setSelectedCollection(const Akonadi::Collection &collection)
Definition: folderselectiondialog.cpp:257
SettingsIf
#define SettingsIf
Definition: mailkernel.h:193
QModelIndex
QWidget
foldercollection.h
KernelIf
#define KernelIf
Definition: mailkernel.h:191
foldertreewidgetproxymodel.h
MailCommon::FolderSelectionDialog::setSelectionMode
void setSelectionMode(QAbstractItemView::SelectionMode mode)
Definition: folderselectiondialog.cpp:242
MailCommon::FolderSelectionDialog::FolderSelectionDialog
FolderSelectionDialog(QWidget *parent, FolderSelectionDialog::SelectionFolderOptions options)
Definition: folderselectiondialog.cpp:56
MailCommon::FolderSelectionDialog::selectedCollections
Akonadi::Collection::List selectedCollections() const
Definition: folderselectiondialog.cpp:262
MailCommon::FolderTreeWidget::DisplayNever
Nevery display tooltips.
Definition: foldertreewidget.h:79
QPoint
KDialog
MailCommon::FolderSelectionDialog::HideOutboxFolder
Definition: folderselectiondialog.h:49
MailCommon::FolderSelectionDialog::NotUseGlobalSettings
Definition: folderselectiondialog.h:50
MailCommon::FolderSelectionDialog::ShowUnreadCount
Definition: folderselectiondialog.h:46
QBoxLayout::addWidget
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
MailCommon::FolderTreeWidget::ShowUnreadCount
Definition: foldertreewidget.h:57
MailCommon::FolderTreeView
This is an enhanced EntityTreeView specially suited for the folders in KMail's main folder widget...
Definition: foldertreeview.h:41
MailCommon::FolderSelectionDialog::canCreateCollection
bool canCreateCollection(Akonadi::Collection &parentCol)
Definition: folderselectiondialog.cpp:181
QSharedPointer
Definition: collectiongeneralpage.h:30
foldertreeview.h
QShowEvent
MailCommon::FolderSelectionDialog::NotAllowToCreateNewFolder
Definition: folderselectiondialog.h:48
MailCommon::FolderSelectionDialog::~FolderSelectionDialog
~FolderSelectionDialog()
Definition: folderselectiondialog.cpp:134
QString::isEmpty
bool isEmpty() const
MailCommon::FolderTreeWidgetProxyModel::HideOutboxFolder
Definition: foldertreewidgetproxymodel.h:37
foldertreewidget.h
MailCommon::FolderSelectionDialog::EnableCheck
Definition: folderselectiondialog.h:45
QVBoxLayout
QEvent::spontaneous
bool spontaneous() const
QString
QLayout::setMargin
void setMargin(int margin)
MailCommon::Kernel::collectionFromId
Akonadi::Collection collectionFromId(const Akonadi::Collection::Id &id) const
Returns the collection associated with the given id, or an invalid collection if not found...
Definition: mailkernel.cpp:83
MailCommon::FolderTreeWidget::UseDistinctSelectionModel
Definition: foldertreewidget.h:59
MailCommon::FolderSelectionDialog::readConfig
void readConfig()
Definition: folderselectiondialog.cpp:269
MailCommon::FolderTreeWidget
This is the widget that shows the main folder tree.
Definition: foldertreewidget.h:50
QSize
MailCommon::FolderSelectionDialog::HideVirtualFolder
Definition: folderselectiondialog.h:47
folderselectiondialog.h
QItemSelection
MailCommon::FolderCollection::forCollection
static QSharedPointer< FolderCollection > forCollection(const Akonadi::Collection &coll, bool writeConfig=true)
Definition: foldercollection.cpp:46
MailCommon::FolderTreeWidgetProxyModel::None
Definition: foldertreewidgetproxymodel.h:34
MailCommon::FolderSelectionDialog::hideEvent
void hideEvent(QHideEvent *)
Definition: folderselectiondialog.cpp:299
QCursor::pos
QPoint pos()
MailCommon::FolderSelectionDialog::selectedCollection
Akonadi::Collection selectedCollection() const
Definition: folderselectiondialog.cpp:252
MailCommon::FolderTreeWidgetProxyModel::HideVirtualFolder
Definition: foldertreewidgetproxymodel.h:35
QLatin1String
mailkernel.h
MailCommon::FolderTreeWidgetProxyModel::HideSpecificFolder
Definition: foldertreewidgetproxymodel.h:36
MailCommon::FolderSelectionDialog::selectionMode
QAbstractItemView::SelectionMode selectionMode() const
Definition: folderselectiondialog.cpp:247
MailCommon::myConfigGroupName
static const char * myConfigGroupName
Definition: folderselectiondialog.cpp:267
MailCommon::Kernel::self
static Kernel * self()
Definition: mailkernel.cpp:73
MailCommon::FolderTreeWidget::None
Definition: foldertreewidget.h:56
MailCommon::FolderSelectionDialog::focusTreeView
void focusTreeView()
Definition: folderselectiondialog.cpp:160
MailCommon::FolderSelectionDialog::showEvent
void showEvent(QShowEvent *)
Definition: folderselectiondialog.cpp:166
MailCommon::FolderSelectionDialog::writeConfig
void writeConfig()
Definition: folderselectiondialog.cpp:286
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:31:40 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

mailcommon

Skip menu "mailcommon"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

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