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

kontact

  • sources
  • kde-4.14
  • 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-2015 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 <KLocalizedString>
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::updateSummary( bool force )
106 {
107  Q_UNUSED( force );
108  QTimer::singleShot(0, this, SLOT(slotUpdateFolderList()));
109 }
110 
111 void SummaryWidget::selectFolder( const QString &folder )
112 {
113  if ( mPlugin->isRunningStandalone() ) {
114  mPlugin->bringToForeground();
115  } else {
116  mPlugin->core()->selectPlugin( mPlugin );
117  }
118 
119  org::kde::kmail::kmail kmail( QLatin1String("org.kde.kmail"), QLatin1String("/KMail"), QDBusConnection::sessionBus() );
120  kmail.selectFolder( folder );
121 }
122 
123 void SummaryWidget::displayModel( const QModelIndex &parent,
124  int &counter,
125  const bool showFolderPaths,
126  QStringList parentTreeNames )
127 {
128  const int nbCol = mModelProxy->rowCount( parent );
129  for ( int i = 0; i < nbCol; ++i ) {
130  const QModelIndex child = mModelProxy->index( i, 0, parent );
131  const Akonadi::Collection col =
132  mModelProxy->data( child,
133  Akonadi::EntityTreeModel::CollectionRole ).value<Akonadi::Collection>();
134  const int showCollection =
135  mModelProxy->data( child, Qt::CheckStateRole ).value<int>();
136 
137  if ( col.isValid() ) {
138  const Akonadi::CollectionStatistics stats = col.statistics();
139  if ( ( ( stats.unreadCount() ) != Q_INT64_C(0) ) && showCollection ) {
140  // Collection Name.
141  KUrlLabel *urlLabel;
142 
143  if ( showFolderPaths ) {
144  // Construct the full path string.
145  parentTreeNames.insert( parentTreeNames.size(), col.name() );
146  urlLabel = new KUrlLabel( QString::number( col.id() ),
147  parentTreeNames.join( QLatin1String("/") ), this );
148  parentTreeNames.removeLast();
149  } else {
150  urlLabel = new KUrlLabel( QString::number( col.id() ), col.name(), this );
151  }
152 
153  urlLabel->installEventFilter( this );
154  urlLabel->setAlignment( Qt::AlignLeft );
155  urlLabel->setWordWrap( true );
156  mLayout->addWidget( urlLabel, counter, 1 );
157  mLabels.append( urlLabel );
158 
159  // tooltip
160  urlLabel->setToolTip( i18n( "<qt><b>%1</b>"
161  "<br/>Total: %2<br/>"
162  "Unread: %3</qt>",
163  col.name(),
164  stats.count(),
165  stats.unreadCount() ) );
166 
167  connect( urlLabel, SIGNAL(leftClickedUrl(QString)),
168  SLOT(selectFolder(QString)) );
169 
170  // Read and unread count.
171  QLabel *label = new QLabel( i18nc( "%1: number of unread messages "
172  "%2: total number of messages",
173  "%1 / %2", stats.unreadCount(), stats.count() ), this );
174 
175  label->setAlignment( Qt::AlignLeft );
176  mLayout->addWidget( label, counter, 2 );
177  mLabels.append( label );
178 
179  // Folder icon.
180  QIcon icon = mModelProxy->data( child, Qt::DecorationRole ).value<QIcon>();
181  label = new QLabel( this );
182  label->setPixmap( icon.pixmap( label->height() / 1.5 ) );
183  label->setMaximumWidth( label->minimumSizeHint().width() );
184  label->setAlignment( Qt::AlignVCenter );
185  mLayout->addWidget( label, counter, 0 );
186  mLabels.append( label );
187 
188  ++counter;
189  }
190  parentTreeNames.insert( parentTreeNames.size(), col.name() );
191  displayModel( child, counter, showFolderPaths, parentTreeNames );
192  // Remove the last parent collection name for the next iteration.
193  parentTreeNames.removeLast();
194  }
195  }
196 }
197 
198 void SummaryWidget::slotUpdateFolderList()
199 {
200  qDeleteAll( mLabels );
201  mLabels.clear();
202  mModelState->restoreState();
203  int counter = 0;
204  kDebug() << QLatin1String("Iterating over") << mModel->rowCount() << QLatin1String("collections.");
205  KConfig _config( QLatin1String("kcmkmailsummaryrc") );
206  KConfigGroup config( &_config, "General" );
207  const bool showFolderPaths = config.readEntry( "showFolderPaths", false );
208  displayModel( QModelIndex(), counter, showFolderPaths, QStringList() );
209 
210  if ( counter == 0 ) {
211  QLabel *label = new QLabel( i18n( "No unread messages in your monitored folders" ), this );
212  label->setAlignment( Qt::AlignHCenter | Qt::AlignVCenter );
213  mLayout->addWidget( label, 0, 0 );
214  mLabels.append( label );
215  }
216 
217  QList<QLabel*>::const_iterator lit;
218  QList<QLabel*>::const_iterator lend( mLabels.constEnd() );
219  for ( lit = mLabels.constBegin(); lit != lend; ++lit ) {
220  (*lit)->show();
221  }
222 }
223 
224 bool SummaryWidget::eventFilter( QObject *obj, QEvent *e )
225 {
226  if ( obj->inherits( "KUrlLabel" ) ) {
227  KUrlLabel* label = static_cast<KUrlLabel*>( obj );
228  if ( e->type() == QEvent::Enter ) {
229  emit message( i18n( "Open Folder: \"%1\"", label->text() ) );
230  } else if ( e->type() == QEvent::Leave ) {
231  emit message( QString::null ); //krazy:exclude=nullstrassign for old broken gcc
232  }
233  }
234 
235  return KontactInterface::Summary::eventFilter( obj, e );
236 }
237 
238 QStringList SummaryWidget::configModules() const
239 {
240  return QStringList()<<QLatin1String( "kcmkmailsummary.desktop" );
241 }
242 
QList::clear
void clear()
QModelIndex
QEvent
QWidget
QEvent::type
Type type() const
QGridLayout::addWidget
void addWidget(QWidget *widget, int row, int column, QFlags< Qt::AlignmentFlag > alignment)
QLabel::setAlignment
void setAlignment(QFlags< Qt::AlignmentFlag >)
QDBusConnection::sessionBus
QDBusConnection sessionBus()
QGridLayout
QStringList::join
QString join(const QString &separator) const
SummaryWidget::updateSummary
virtual void updateSummary(bool force)
Definition: kmail/summarywidget.cpp:105
summarywidget.h
QGridLayout::setSpacing
void setSpacing(int spacing)
SummaryWidget::configModules
QStringList configModules() const
Definition: kmail/summarywidget.cpp:238
QIcon::pixmap
QPixmap pixmap(const QSize &size, Mode mode, State state) const
QList::size
int size() const
QBoxLayout::addWidget
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
QString::number
QString number(int n, int base)
QList::append
void append(const T &value)
QGridLayout::setRowStretch
void setRowStretch(int row, int stretch)
QObject::inherits
bool inherits(const char *className) const
QObject
QBoxLayout::addItem
virtual void addItem(QLayoutItem *item)
QVBoxLayout
QString
QList
QLayout::setMargin
void setMargin(int margin)
QStringList
QIcon::setPixmap
void setPixmap(const QPixmap &pixmap, Size size, Mode mode, State state)
QLatin1String
QList::insert
void insert(int i, const T &value)
QList::removeLast
void removeLast()
QList::constEnd
const_iterator constEnd() const
QList::constBegin
const_iterator constBegin() const
QItemSelectionModel
SummaryWidget::SummaryWidget
SummaryWidget(KontactInterface::Plugin *plugin, QWidget *parent)
Definition: kmail/summarywidget.cpp:55
QLabel
SummaryWidget::eventFilter
virtual bool eventFilter(QObject *obj, QEvent *e)
Definition: kmail/summarywidget.cpp:224
QBoxLayout::setSpacing
void setSpacing(int spacing)
QIcon
QTimer::singleShot
singleShot
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:34:11 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
  • 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