28 #include "kmailinterface.h"
30 #include <KontactInterface/Core>
31 #include <KontactInterface/Plugin>
33 #include <Akonadi/ChangeRecorder>
34 #include <Akonadi/CollectionFetchScope>
35 #include <Akonadi/CollectionStatistics>
36 #include <Akonadi/EntityTreeModel>
37 #include <Akonadi/ETMViewStateSaver>
39 #include <KMime/KMimeMessage>
41 #include <KCheckableProxyModel>
42 #include <KConfigGroup>
49 #include <QGridLayout>
50 #include <QVBoxLayout>
51 #include <QItemSelectionModel>
56 : KontactInterface::Summary( parent ), mPlugin( plugin )
58 QVBoxLayout *mainLayout =
new QVBoxLayout(
this );
59 mainLayout->setSpacing( 3 );
60 mainLayout->setMargin( 3 );
62 QWidget *header = createHeader(
this, QLatin1String(
"view-pim-mail"), i18n(
"New Messages" ) );
63 mainLayout->addWidget( header );
65 mLayout =
new QGridLayout();
66 mainLayout->addItem( mLayout );
67 mLayout->setSpacing( 3 );
68 mLayout->setRowStretch( 6, 1 );
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 );
77 mModel =
new Akonadi::EntityTreeModel( mChangeRecorder,
this );
78 mModel->setItemPopulationStrategy( Akonadi::EntityTreeModel::NoItemPopulation );
80 mSelectionModel =
new QItemSelectionModel( mModel );
81 mModelProxy =
new KCheckableProxyModel(
this );
82 mModelProxy->setSelectionModel( mSelectionModel );
83 mModelProxy->setSourceModel( mModel );
85 KSharedConfigPtr _config = KSharedConfig::openConfig( QLatin1String(
"kcmkmailsummaryrc") );
88 new KViewStateMaintainer<Akonadi::ETMViewStateSaver>( _config->group(
"CheckState" ), this );
89 mModelState->setSelectionModel( mSelectionModel );
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()));
100 void SummaryWidget::slotCollectionChanged()
102 QTimer::singleShot(0,
this, SLOT(slotUpdateFolderList()));
105 void SummaryWidget::slotRowInserted(
const QModelIndex & parent,
int start,
int end )
110 QTimer::singleShot(0,
this, SLOT(slotUpdateFolderList()));
116 QTimer::singleShot(0,
this, SLOT(slotUpdateFolderList()));
119 void SummaryWidget::selectFolder(
const QString &folder )
121 if ( mPlugin->isRunningStandalone() ) {
122 mPlugin->bringToForeground();
124 mPlugin->core()->selectPlugin( mPlugin );
127 org::kde::kmail::kmail kmail( QLatin1String(
"org.kde.kmail"), QLatin1String(
"/KMail"), QDBusConnection::sessionBus() );
128 kmail.selectFolder( folder );
131 void SummaryWidget::displayModel(
const QModelIndex &parent,
133 const bool showFolderPaths,
134 QStringList parentTreeNames )
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>();
143 mModelProxy->data( child, Qt::CheckStateRole ).value<
int>();
145 if ( col.isValid() ) {
146 const Akonadi::CollectionStatistics stats = col.statistics();
147 if ( ( ( stats.unreadCount() ) != Q_INT64_C(0) ) && showCollection ) {
151 if ( showFolderPaths ) {
153 parentTreeNames.insert( parentTreeNames.size(), col.name() );
154 urlLabel =
new KUrlLabel( QString::number( col.id() ),
155 parentTreeNames.join( QLatin1String(
"/") ), this );
156 parentTreeNames.removeLast();
158 urlLabel =
new KUrlLabel( QString::number( col.id() ), col.name(), this );
161 urlLabel->installEventFilter(
this );
162 urlLabel->setAlignment( Qt::AlignLeft );
163 urlLabel->setWordWrap(
true );
164 mLayout->addWidget( urlLabel, counter, 1 );
165 mLabels.append( urlLabel );
168 urlLabel->setToolTip( i18n(
"<qt><b>%1</b>"
169 "<br/>Total: %2<br/>"
173 stats.unreadCount() ) );
175 connect( urlLabel, SIGNAL(leftClickedUrl(QString)),
176 SLOT(selectFolder(QString)) );
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 );
183 label->setAlignment( Qt::AlignLeft );
184 mLayout->addWidget( label, counter, 2 );
185 mLabels.append( label );
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 );
198 parentTreeNames.insert( parentTreeNames.size(), col.name() );
199 displayModel( child, counter, showFolderPaths, parentTreeNames );
201 parentTreeNames.removeLast();
206 void SummaryWidget::slotUpdateFolderList()
208 qDeleteAll( mLabels );
210 mModelState->restoreState();
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() );
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 );
227 for ( lit = mLabels.constBegin(); lit != lend; ++lit ) {
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 );
243 return KontactInterface::Summary::eventFilter( obj, e );
248 return QStringList()<<QLatin1String(
"kcmkmailsummary.desktop" );
251 #include "summarywidget.moc"