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

kontact

  • sources
  • kde-4.12
  • kdepim
  • kontact
  • plugins
  • kmail
kmail/summarywidget.cpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-file-style: "gnu" -*-
2 
3  This file is part of Kontact.
4 
5  Copyright (c) 2003 Tobias Koenig <tokoe@kde.org>
6  Copyright (c) 2013 Laurent Montel <montel@kde.org>
7 
8  This program is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License along
19  with this program; if not, write to the Free Software Foundation, Inc.,
20  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 
22  As a special exception, permission is given to link this program
23  with any edition of Qt, and distribute the resulting executable,
24  without including the source code for Qt in the source distribution.
25 */
26 
27 #include "summarywidget.h"
28 #include "kmailinterface.h"
29 
30 #include <KontactInterface/Core>
31 #include <KontactInterface/Plugin>
32 
33 #include <Akonadi/ChangeRecorder>
34 #include <Akonadi/CollectionFetchScope>
35 #include <Akonadi/CollectionStatistics>
36 #include <Akonadi/EntityTreeModel>
37 #include <Akonadi/ETMViewStateSaver>
38 
39 #include <KMime/KMimeMessage>
40 
41 #include <KCheckableProxyModel>
42 #include <KConfigGroup>
43 #include <KDebug>
44 #include <KLocale>
45 #include <KUrlLabel>
46 
47 #include <QEvent>
48 #include <QIcon>
49 #include <QGridLayout>
50 #include <QVBoxLayout>
51 #include <QItemSelectionModel>
52 
53 #include <ctime>
54 
55 SummaryWidget::SummaryWidget( KontactInterface::Plugin *plugin, QWidget *parent )
56  : KontactInterface::Summary( parent ), mPlugin( plugin )
57 {
58  QVBoxLayout *mainLayout = new QVBoxLayout( this );
59  mainLayout->setSpacing( 3 );
60  mainLayout->setMargin( 3 );
61 
62  QWidget *header = createHeader( this, QLatin1String("view-pim-mail"), i18n( "New Messages" ) );
63  mainLayout->addWidget( header );
64 
65  mLayout = new QGridLayout();
66  mainLayout->addItem( mLayout );
67  mLayout->setSpacing( 3 );
68  mLayout->setRowStretch( 6, 1 );
69 
70  // Create a new change recorder.
71  mChangeRecorder = new Akonadi::ChangeRecorder( this );
72  mChangeRecorder->setMimeTypeMonitored( KMime::Message::mimeType() );
73  mChangeRecorder->fetchCollectionStatistics( true );
74  mChangeRecorder->setAllMonitored( true );
75  mChangeRecorder->collectionFetchScope().setIncludeStatistics( true );
76 
77  mModel = new Akonadi::EntityTreeModel( mChangeRecorder, this );
78  mModel->setItemPopulationStrategy( Akonadi::EntityTreeModel::NoItemPopulation );
79 
80  mSelectionModel = new QItemSelectionModel( mModel );
81  mModelProxy = new KCheckableProxyModel( this );
82  mModelProxy->setSelectionModel( mSelectionModel );
83  mModelProxy->setSourceModel( mModel );
84 
85  KSharedConfigPtr _config = KSharedConfig::openConfig( QLatin1String("kcmkmailsummaryrc") );
86 
87  mModelState =
88  new KViewStateMaintainer<Akonadi::ETMViewStateSaver>( _config->group( "CheckState" ), this );
89  mModelState->setSelectionModel( mSelectionModel );
90 
91  connect( mChangeRecorder, SIGNAL(collectionChanged(Akonadi::Collection)),
92  SLOT(slotCollectionChanged()) );
93  connect( mChangeRecorder, SIGNAL(collectionRemoved(Akonadi::Collection)),
94  SLOT(slotCollectionChanged()) );
95  connect( mChangeRecorder, SIGNAL(collectionStatisticsChanged(Akonadi::Collection::Id,Akonadi::CollectionStatistics)),
96  SLOT(slotCollectionChanged()));
97  QTimer::singleShot(0, this, SLOT(slotUpdateFolderList()));
98 }
99 
100 void SummaryWidget::slotCollectionChanged()
101 {
102  QTimer::singleShot(0, this, SLOT(slotUpdateFolderList()));
103 }
104 
105 void SummaryWidget::slotRowInserted( const QModelIndex & parent, int start, int end )
106 {
107  Q_UNUSED( parent );
108  Q_UNUSED( start );
109  Q_UNUSED( end );
110  QTimer::singleShot(0, this, SLOT(slotUpdateFolderList()));
111 }
112 
113 void SummaryWidget::updateSummary( bool force )
114 {
115  Q_UNUSED( force );
116  QTimer::singleShot(0, this, SLOT(slotUpdateFolderList()));
117 }
118 
119 void SummaryWidget::selectFolder( const QString &folder )
120 {
121  if ( mPlugin->isRunningStandalone() ) {
122  mPlugin->bringToForeground();
123  } else {
124  mPlugin->core()->selectPlugin( mPlugin );
125  }
126 
127  org::kde::kmail::kmail kmail( QLatin1String("org.kde.kmail"), QLatin1String("/KMail"), QDBusConnection::sessionBus() );
128  kmail.selectFolder( folder );
129 }
130 
131 void SummaryWidget::displayModel( const QModelIndex &parent,
132  int &counter,
133  const bool showFolderPaths,
134  QStringList parentTreeNames )
135 {
136  const int nbCol = mModelProxy->rowCount( parent );
137  for ( int i = 0; i < nbCol; ++i ) {
138  const QModelIndex child = mModelProxy->index( i, 0, parent );
139  Akonadi::Collection col =
140  mModelProxy->data( child,
141  Akonadi::EntityTreeModel::CollectionRole ).value<Akonadi::Collection>();
142  int showCollection =
143  mModelProxy->data( child, Qt::CheckStateRole ).value<int>();
144 
145  if ( col.isValid() ) {
146  const Akonadi::CollectionStatistics stats = col.statistics();
147  if ( ( ( stats.unreadCount() ) != Q_INT64_C(0) ) && showCollection ) {
148  // Collection Name.
149  KUrlLabel *urlLabel;
150 
151  if ( showFolderPaths ) {
152  // Construct the full path string.
153  parentTreeNames.insert( parentTreeNames.size(), col.name() );
154  urlLabel = new KUrlLabel( QString::number( col.id() ),
155  parentTreeNames.join( QLatin1String("/") ), this );
156  parentTreeNames.removeLast();
157  } else {
158  urlLabel = new KUrlLabel( QString::number( col.id() ), col.name(), this );
159  }
160 
161  urlLabel->installEventFilter( this );
162  urlLabel->setAlignment( Qt::AlignLeft );
163  urlLabel->setWordWrap( true );
164  mLayout->addWidget( urlLabel, counter, 1 );
165  mLabels.append( urlLabel );
166 
167  // tooltip
168  urlLabel->setToolTip( i18n( "<qt><b>%1</b>"
169  "<br/>Total: %2<br/>"
170  "Unread: %3</qt>",
171  col.name(),
172  stats.count(),
173  stats.unreadCount() ) );
174 
175  connect( urlLabel, SIGNAL(leftClickedUrl(QString)),
176  SLOT(selectFolder(QString)) );
177 
178  // Read and unread count.
179  QLabel *label = new QLabel( i18nc( "%1: number of unread messages "
180  "%2: total number of messages",
181  "%1 / %2", stats.unreadCount(), stats.count() ), this );
182 
183  label->setAlignment( Qt::AlignLeft );
184  mLayout->addWidget( label, counter, 2 );
185  mLabels.append( label );
186 
187  // Folder icon.
188  QIcon icon = mModelProxy->data( child, Qt::DecorationRole ).value<QIcon>();
189  label = new QLabel( this );
190  label->setPixmap( icon.pixmap( label->height() / 1.5 ) );
191  label->setMaximumWidth( label->minimumSizeHint().width() );
192  label->setAlignment( Qt::AlignVCenter );
193  mLayout->addWidget( label, counter, 0 );
194  mLabels.append( label );
195 
196  counter ++;
197  }
198  parentTreeNames.insert( parentTreeNames.size(), col.name() );
199  displayModel( child, counter, showFolderPaths, parentTreeNames );
200  // Remove the last parent collection name for the next iteration.
201  parentTreeNames.removeLast();
202  }
203  }
204 }
205 
206 void SummaryWidget::slotUpdateFolderList()
207 {
208  qDeleteAll( mLabels );
209  mLabels.clear();
210  mModelState->restoreState();
211  int counter = 0;
212  kDebug() << QLatin1String("Iterating over") << mModel->rowCount() << QLatin1String("collections.");
213  KConfig _config( QLatin1String("kcmkmailsummaryrc") );
214  KConfigGroup config( &_config, "General" );
215  const bool showFolderPaths = config.readEntry( "showFolderPaths", false );
216  displayModel( QModelIndex(), counter, showFolderPaths, QStringList() );
217 
218  if ( counter == 0 ) {
219  QLabel *label = new QLabel( i18n( "No unread messages in your monitored folders" ), this );
220  label->setAlignment( Qt::AlignHCenter | Qt::AlignVCenter );
221  mLayout->addWidget( label, 0, 0 );
222  mLabels.append( label );
223  }
224 
225  QList<QLabel*>::const_iterator lit;
226  QList<QLabel*>::const_iterator lend( mLabels.constEnd() );
227  for ( lit = mLabels.constBegin(); lit != lend; ++lit ) {
228  (*lit)->show();
229  }
230 }
231 
232 bool SummaryWidget::eventFilter( QObject *obj, QEvent *e )
233 {
234  if ( obj->inherits( "KUrlLabel" ) ) {
235  KUrlLabel* label = static_cast<KUrlLabel*>( obj );
236  if ( e->type() == QEvent::Enter ) {
237  emit message( i18n( "Open Folder: \"%1\"", label->text() ) );
238  } else if ( e->type() == QEvent::Leave ) {
239  emit message( QString::null ); //krazy:exclude=nullstrassign for old broken gcc
240  }
241  }
242 
243  return KontactInterface::Summary::eventFilter( obj, e );
244 }
245 
246 QStringList SummaryWidget::configModules() const
247 {
248  return QStringList()<<QLatin1String( "kcmkmailsummary.desktop" );
249 }
250 
251 #include "summarywidget.moc"
QWidget
SummaryWidget::updateSummary
virtual void updateSummary(bool force)
Definition: kmail/summarywidget.cpp:113
QObject
summarywidget.h
SummaryWidget::configModules
QStringList configModules() const
Definition: kmail/summarywidget.cpp:246
QLabel
SummaryWidget::SummaryWidget
SummaryWidget(KontactInterface::Plugin *plugin, QWidget *parent)
Definition: kmail/summarywidget.cpp:55
SummaryWidget::eventFilter
virtual bool eventFilter(QObject *obj, QEvent *e)
Definition: kmail/summarywidget.cpp:232
QList
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:58:30 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kontact

Skip menu "kontact"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

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

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