2
Copyright (c) 2007 Bruno Virlet <bruno.virlet@gmail.com>
4
This library is free software; you can redistribute it and/or modify it
5
under the terms of the GNU Library General Public License as published by
6
the Free Software Foundation; either version 2 of the License, or (at your
7
option) any later version.
9
This library is distributed in the hope that it will be useful, but WITHOUT
10
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12
License for more details.
14
You should have received a copy of the GNU Library General Public License
15
along with this library; see the file COPYING.LIB. If not, write to the
16
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20
#include "mainwidget.h"
21
#include "mainwindow.h"
23
#include <akonadi/collection.h>
24
#include <akonadi/collectionview.h>
25
#include <akonadi/collectionfilterproxymodel.h>
26
#include <akonadi/collectionstatisticsmodel.h>
27
#include <akonadi/collectionstatisticsdelegate.h>
28
#include <akonadi/itemfetchjob.h>
29
#include <akonadi/itemfetchscope.h>
30
#include <akonadi/collectionmodifyjob.h>
31
#include <akonadi/kmime/messagemodel.h>
32
#include <akonadi/kmime/messagethreaderproxymodel.h>
34
#include <QtCore/QDebug>
35
#include <QVBoxLayout>
38
#include <QtGui/QSortFilterProxyModel>
40
using namespace Akonadi;
42
MainWidget::MainWidget( MainWindow * parent) :
43
QWidget( parent ), mMainWindow( parent )
45
connect( mMainWindow, SIGNAL( threadCollection() ),
46
this, SLOT( threadCollection() ) );
48
QHBoxLayout *layout = new QHBoxLayout( this );
50
QSplitter *splitter = new QSplitter( Qt::Horizontal, this );
51
layout->addWidget( splitter );
53
// Left part, collection view
54
mCollectionList = new Akonadi::CollectionView();
55
connect( mCollectionList, SIGNAL(clicked(const Akonadi::Collection &)),
56
SLOT(collectionClicked(const Akonadi::Collection &)) );
57
CollectionStatisticsDelegate *collectionDelegate =
58
new CollectionStatisticsDelegate( mCollectionList );
59
collectionDelegate->setUnreadCountShown( true ); //For testing, should be toggleable columns eventually
60
mCollectionList->setItemDelegate( collectionDelegate );
61
splitter->addWidget( mCollectionList );
62
// Filter the collection to only show the emails
63
mCollectionModel = new Akonadi::CollectionStatisticsModel( this );
64
mCollectionProxyModel = new Akonadi::CollectionFilterProxyModel( this );
65
mCollectionProxyModel->setSourceModel( mCollectionModel );
66
mCollectionProxyModel->addMimeTypeFilter( QString::fromLatin1( "message/rfc822" ) );
68
// display collections sorted
69
QSortFilterProxyModel *sortModel = new QSortFilterProxyModel( this );
70
sortModel->setDynamicSortFilter( true );
71
sortModel->setSortCaseSensitivity( Qt::CaseInsensitive );
72
sortModel->setSourceModel( mCollectionProxyModel );
74
// Right part, message list + message viewer
75
QSplitter *rightSplitter = new QSplitter( Qt::Vertical, this );
76
splitter->addWidget( rightSplitter );
77
mMessageList = new QTreeView( this );
78
mMessageList->setDragEnabled( true );
79
mMessageList->setSelectionMode( QAbstractItemView::ExtendedSelection );
80
connect( mMessageList, SIGNAL(clicked(QModelIndex)), SLOT(itemActivated(QModelIndex)) );
81
rightSplitter->addWidget( mMessageList );
83
mCollectionList->setModel( sortModel );
84
mMessageModel = new Akonadi::MessageModel( this );
85
mMessageProxyModel = new Akonadi::MessageThreaderProxyModel( this );
86
mMessageProxyModel->setSourceModel( mMessageModel );
87
mMessageList->setModel( mMessageProxyModel );
89
mMessageView = new QTextEdit( this );
90
rightSplitter->addWidget( mMessageView );
93
splitter->setSizes( QList<int>() << 200 << 500 );
94
rightSplitter->setSizes( QList<int>() << 300 << 200 );
97
void MainWidget::collectionClicked(const Akonadi::Collection & collection)
99
mCurrentCollection = collection;
100
mMessageModel->setCollection( Collection( mCurrentCollection ) );
103
void MainWidget::itemActivated(const QModelIndex & index)
105
const Item item = mMessageModel->itemForIndex( mMessageProxyModel->mapToSource( index ) );
107
if ( !item.isValid() )
110
ItemFetchJob *job = new ItemFetchJob( item, this );
111
job->fetchScope().fetchFullPayload();
112
connect( job, SIGNAL( result(KJob*) ), SLOT( itemFetchDone(KJob*) ) );
116
void MainWidget::itemFetchDone(KJob * job)
118
ItemFetchJob *fetch = static_cast<ItemFetchJob*>( job );
119
if ( job->error() ) {
120
qWarning() << "Mail fetch failed: " << job->errorString();
121
} else if ( fetch->items().isEmpty() ) {
122
qWarning() << "No mail found!";
124
const Item item = fetch->items().first();
125
mMessageView->setPlainText( item.payloadData() );
129
void MainWidget::threadCollection()
131
/*MailThreaderAttribute *a = mCurrentCollection.attribute<MailThreaderAttribute>( Collection::AddIfMissing );
132
a->deserialize( QByteArray( "sort" ) );
133
CollectionModifyJob *job = new CollectionModifyJob( mCurrentCollection );
135
qDebug() << "Unable to modify collection";*/