1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
|
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the Qt Messaging Framework.
**
** $QT_BEGIN_LICENSE:LGPL21$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "messagelistview.h"
#include <qmaillog.h>
#include <qmailaccount.h>
#include <qmailfolder.h>
#include <qmailfolderkey.h>
#include <qmailmessagethreadedmodel.h>
#include <qmailmessagelistmodel.h>
#include <QTreeView>
#include <QKeyEvent>
#include <QLayout>
#include <QLabel>
#include <QMap>
#include <QModelIndex>
#include <QPushButton>
#include <QTabBar>
#include <QToolButton>
#include <QVBoxLayout>
#include <QHeaderView>
#include <QItemDelegate>
#include <QPainter>
#include <QComboBox>
#include <QLineEdit>
#include <QApplication>
#include <qmaildatacomparator.h>
#include <qtmailnamespace.h>
static QStringList headers(QStringList() << QObject::tr("Subject") << QObject::tr("Addressee") << QObject::tr("Date") << QObject::tr("Size"));
static const QColor newMessageColor(Qt::blue);
class QuickSearchWidget : public QWidget
{
Q_OBJECT
public:
QuickSearchWidget(QWidget* parent = Q_NULLPTR);
QMailMessageKey searchKey() const;
public slots:
void reset(bool emitReset = true);
signals:
void quickSearchRequested(const QMailMessageKey& key);
void quickSearchReset();
void fullSearchRequested();
private slots:
void searchTermsChanged();
private:
QMailMessageKey buildSearchKey() const;
private:
QLineEdit* m_searchTerms;
QComboBox* m_statusCombo;
QToolButton* m_clearButton;
QMailMessageKey m_key;
};
QuickSearchWidget::QuickSearchWidget(QWidget* parent)
:
QWidget(parent)
{
QHBoxLayout* layout = new QHBoxLayout(this);
m_clearButton = new QToolButton(this);
m_clearButton->setIcon(Qtmail::icon("clear"));
connect(m_clearButton,SIGNAL(clicked()),this,SLOT(reset()));
layout->addWidget(m_clearButton);
layout->addWidget(new QLabel("Search:"));
m_searchTerms = new QLineEdit(this);
connect(m_searchTerms,SIGNAL(textChanged(QString)),this,SLOT(searchTermsChanged()));
layout->addWidget(m_searchTerms);
layout->addWidget(new QLabel("Status:"));
m_statusCombo = new QComboBox(this);
QMailMessageKey removed(QMailMessageKey::status(QMailMessage::Removed));
m_statusCombo->addItem(QIcon(":icon/exec"),tr("Any Status"),~removed);
m_statusCombo->addItem(QIcon(":icon/mail_generic"),tr("Unread"),QMailMessageKey::status(QMailMessage::Read,QMailDataComparator::Excludes)&~removed);
m_statusCombo->addItem(QIcon(":icon/mail_generic"),tr("Important"),QMailMessageKey::status(QMailMessage::Important)&~removed);
m_statusCombo->addItem(QIcon(":icon/new"),tr("New"),QMailMessageKey::status(QMailMessage::New)&~removed);
m_statusCombo->addItem(QIcon(":icon/mail_reply"),tr("Replied"),QMailMessageKey::status(QMailMessage::Replied)&~removed);
m_statusCombo->addItem(QIcon(":icon/mail_forward"),tr("Forwarded"),QMailMessageKey::status(QMailMessage::Forwarded)&~removed);
m_statusCombo->addItem(QIcon(":/icon/attach"),tr("Has Attachment"),QMailMessageKey::status(QMailMessage::HasAttachments)&~removed);
m_statusCombo->addItem(QIcon(":icon/exec"),tr("Removed"), removed);
connect(m_statusCombo,SIGNAL(currentIndexChanged(int)),this,SLOT(searchTermsChanged()));
layout->addWidget(m_statusCombo);
m_key = buildSearchKey();
}
QMailMessageKey QuickSearchWidget::searchKey() const
{
return m_key;
}
void QuickSearchWidget::reset(bool emitReset)
{
blockSignals(!emitReset);
m_statusCombo->setCurrentIndex(0);
m_searchTerms->clear();
blockSignals(false);
}
void QuickSearchWidget::searchTermsChanged()
{
m_key = buildSearchKey();
emit quickSearchRequested(m_key);
if (m_searchTerms->text().isEmpty() && m_statusCombo->currentIndex() == 0)
emit quickSearchReset();
}
QMailMessageKey QuickSearchWidget::buildSearchKey() const
{
QMailMessageKey statusKey = qvariant_cast<QMailMessageKey>(m_statusCombo->itemData(m_statusCombo->currentIndex()));
if (m_searchTerms->text().isEmpty() && m_statusCombo->currentIndex() == 0)
return statusKey;
QMailMessageKey subjectKey = QMailMessageKey::subject(m_searchTerms->text(),QMailDataComparator::Includes);
QMailMessageKey senderKey = QMailMessageKey::sender(m_searchTerms->text(),QMailDataComparator::Includes);
QMailMessageKey recipientsKey = QMailMessageKey::recipients(m_searchTerms->text(),QMailDataComparator::Includes);
return ((subjectKey | senderKey | recipientsKey) & statusKey);
}
template <typename BaseModel>
class MessageListModel : public BaseModel
{
// Doesn't work in template classes...
//Q_OBJECT
typedef BaseModel SuperType;
public:
MessageListModel(MessageListView* parent);
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
int columnCount(const QModelIndex & parent = QModelIndex()) const;
int rowCount(const QModelIndex& parent = QModelIndex()) const;
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
private:
MessageListView *m_parent;
};
template <typename BaseModel>
MessageListModel<BaseModel>::MessageListModel(MessageListView *parent)
: BaseModel(parent),
m_parent(parent)
{
}
template <typename BaseModel>
QVariant MessageListModel<BaseModel>::headerData(int section, Qt::Orientation o, int role) const
{
if (role == Qt::DisplayRole) {
if (section < headers.count())
return headers.at(section);
}
return SuperType::headerData(section, o, role);
}
template <typename BaseModel>
int MessageListModel<BaseModel>::columnCount(const QModelIndex & parent) const
{
Q_UNUSED(parent);
return headers.count();
}
template <typename BaseModel>
int MessageListModel<BaseModel>::rowCount(const QModelIndex &parentIndex) const
{
int actualRows = SuperType::rowCount(parentIndex);
if (!parentIndex.isValid() && m_parent->moreButtonVisible())
actualRows++;
return actualRows;
}
static QString dateString(const QDateTime& dt)
{
QDateTime current = QDateTime::currentDateTime();
//today
if (dt.date() == current.date())
return QString("Today %1").arg(dt.toString("h:mm:ss ap"));
//yesterday
else if (dt.daysTo(current) <= 1)
return QString("Yesterday %1").arg(dt.toString("h:mm:ss ap"));
//within 7 days
else if (dt.daysTo(current) < 7)
return dt.toString("dddd h:mm:ss ap");
else return dt.toString("dd/MM/yy h:mm:ss ap");
}
template <typename BaseModel>
QVariant MessageListModel<BaseModel>::data(const QModelIndex & index, int role) const
{
if (index.isValid()) {
if (role == Qt::DisplayRole && index.isValid()) {
switch (index.column())
{
case 0:
return SuperType::data(index, QMailMessageModelBase::MessageSubjectTextRole);
case 1:
return SuperType::data(index, QMailMessageModelBase::MessageAddressTextRole);
//case 2:
// return SuperType::data(index, QMailMessageModelBase::MessageTimeStampTextRole);
case 3:
return SuperType::data(index, QMailMessageModelBase::MessageSizeTextRole);
default:
break;
}
if (index.column() == 2) {
// Customize the date display
QMailMessageMetaData message(SuperType::idFromIndex(index));
if (message.id().isValid()) {
return dateString(message.date().toLocalTime());
}
}
} else if ((role == Qt::DecorationRole || role == Qt::CheckStateRole) && (index.column() > 0)) {
return QVariant();
} else if (role == Qt::CheckStateRole && !m_parent->markingMode()) {
Qt::CheckState state = static_cast<Qt::CheckState>(SuperType::data(index,role).toInt());
if (state == Qt::Unchecked)
return QVariant();
} else if (role == Qt::DecorationRole) {
return QIcon(SuperType::data(index, QMailMessageModelBase::MessageStatusIconRole).toString());
} else if (role == Qt::ForegroundRole) {
QMailMessageMetaData message(SuperType::idFromIndex(index));
if (message.id().isValid()) {
quint64 status = message.status();
bool unread = !(status & QMailMessage::Read);
if (status & QMailMessage::PartialContentAvailable && unread)
return newMessageColor;
}
}
}
return SuperType::data(index, role);
}
class MessageList : public QTreeView
{
Q_OBJECT
public:
MessageList(MessageListView *parent);
virtual ~MessageList();
QPoint clickPos() const { return pos; }
signals:
void backPressed();
void scrolled();
void moreButtonClicked();
protected:
void keyPressEvent(QKeyEvent* e);
void mouseReleaseEvent(QMouseEvent* e);
void scrollContentsBy(int dx, int dy);
void mouseMoveEvent(QMouseEvent* e);
void mousePressEvent(QMouseEvent* e);
void mouseDoubleClickEvent(QMouseEvent* e);
bool mouseOverMoreLink(QMouseEvent* e);
void drawRow(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
protected slots:
void rowsInserted(const QModelIndex &parent, int start, int end);
private:
MessageListView *m_parent;
QPoint pos;
};
MessageList::MessageList(MessageListView *parent)
: QTreeView(parent),
m_parent(parent)
{
setMouseTracking(true);
}
MessageList::~MessageList()
{
}
void MessageList::keyPressEvent(QKeyEvent* e)
{
switch ( e->key() ) {
case Qt::Key_Space:
case Qt::Key_Return:
case Qt::Key_Select:
case Qt::Key_Enter:
{
if (currentIndex().isValid())
emit clicked(currentIndex());
}
break;
case Qt::Key_No:
case Qt::Key_Back:
case Qt::Key_Backspace:
emit backPressed();
break;
case Qt::Key_Plus:
case Qt::Key_Minus:
{
QModelIndex index(currentIndex());
if (!index.isValid())
break;
if (e->key() == Qt::Key_Plus) {
index = indexBelow(index);
} else if (e->key() == Qt::Key_Minus) {
index = indexAbove(index);
}
while (index.isValid()) {
QMailMessageId id(index.data(QMailMessageModelBase::MessageIdRole).value<QMailMessageId>());
if (!id.isValid())
break;
QMailMessageMetaData message(id);
quint64 status = message.status();
bool unread = !(status & QMailMessage::Read);
if (unread) {
setCurrentIndex(index);
scrollTo(index);
QApplication::postEvent(this, new QKeyEvent(QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier));
QApplication::postEvent(this, new QKeyEvent(QEvent::KeyRelease, Qt::Key_Enter, Qt::NoModifier));
break;
}
if (e->key() == Qt::Key_Plus) {
index = indexBelow(index);
} else if (e->key() == Qt::Key_Minus) {
index = indexAbove(index);
}
}
}
break;
default: QTreeView::keyPressEvent(e);
}
}
void MessageList::mouseReleaseEvent(QMouseEvent* e)
{
pos = e->pos();
QTreeView::mouseReleaseEvent(e);
}
void MessageList::scrollContentsBy(int dx, int dy)
{
QTreeView::scrollContentsBy(dx, dy);
emit scrolled();
}
void MessageList::mouseMoveEvent(QMouseEvent* e)
{
if (mouseOverMoreLink(e)) {
QCursor handCursor(Qt::PointingHandCursor);
setCursor(handCursor);
} else if (cursor().shape() == Qt::PointingHandCursor) {
setCursor(QCursor());
}
QTreeView::mouseMoveEvent(e);
}
void MessageList::mousePressEvent(QMouseEvent* e)
{
// TODO: Should be in release event?
if (mouseOverMoreLink(e)) {
emit moreButtonClicked();
}
QTreeView::mousePressEvent(e);
}
void MessageList::mouseDoubleClickEvent(QMouseEvent* e)
{
QTreeView::mouseDoubleClickEvent(e);
}
bool MessageList::mouseOverMoreLink(QMouseEvent* e)
{
if (!m_parent->moreButtonVisible())
return false;
QModelIndex index = indexAt(e->pos());
if (index.isValid() && ((!index.parent().isValid()) && (index.row() == model()->rowCount(QModelIndex()) - 1))) {
QFont font;
font.setUnderline(true);
QFontMetrics fm(font);
QItemSelection fakeSelection(index.sibling(index.row(),0), index.sibling(index.row(),3));
QRegion r = visualRegionForSelection(fakeSelection);
QRect brect = r.boundingRect();
QRect textRect = fm.boundingRect(brect, Qt::AlignHCenter | Qt::AlignVCenter, tr("Get more messages"));
return textRect.contains(e->pos());
}
return false;
}
void MessageList::drawRow(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
bool isMoreEntry = (!index.parent().isValid() &&
m_parent->moreButtonVisible() &&
(index.row() == (model()->rowCount(QModelIndex()) - 1)));
if (isMoreEntry) {
QLinearGradient lg(option.rect.topLeft(), option.rect.topRight());
lg.setColorAt(0,option.palette.color(QPalette::Base));
lg.setColorAt(0.5,option.palette.color(QPalette::Button));
lg.setColorAt(1,option.palette.color(QPalette::Base));
QFont font = painter->font();
font.setUnderline(true);
painter->save();
painter->setFont(font);
painter->fillRect(option.rect, QBrush(lg));
painter->drawText(option.rect, Qt::AlignHCenter | Qt::AlignVCenter, tr("Get more messages"));
painter->restore();
} else {
QTreeView::drawRow(painter, option, index);
}
}
void MessageList::rowsInserted(const QModelIndex &parent, int start, int end)
{
QTreeView::rowsInserted(parent, start, end);
setExpanded(parent, true);
}
MessageListView::MessageListView(QWidget* parent)
:
QWidget(parent),
mMessageList(new MessageList(this)),
mModel(0),
mMarkingMode(false),
mIgnoreWhenHidden(true),
mSelectedRowsRemoved(false),
mQuickSearchWidget(0),
mShowMoreButton(false),
mThreaded(true),
mQuickSearchIsEmpty(true)
{
init();
showQuickSearch(true);
}
MessageListView::~MessageListView()
{
}
QMailMessageKey MessageListView::key() const
{
return mModel->key();
}
void MessageListView::setKey(const QMailMessageKey& newKey)
{
// Save the current index for the old key
QByteArray keyArray;
QDataStream keyStream(&keyArray, QIODevice::WriteOnly);
mKey.serialize<QDataStream>(keyStream);
mPreviousCurrentCache.insert(keyArray, new QMailMessageId(current()));
// Find the previous current index for the new key
QByteArray newKeyArray;
QDataStream newKeyStream(&newKeyArray, QIODevice::WriteOnly);
newKey.serialize<QDataStream>(newKeyStream);
if (mPreviousCurrentCache[newKeyArray]) {
mPreviousCurrent = *mPreviousCurrentCache[newKeyArray];
}
mModel->setKey(newKey & mQuickSearchWidget->searchKey());
mExpandAllTimer.start(0);
mScrollTimer.stop();
mKey = newKey;
}
QMailMessageSortKey MessageListView::sortKey() const
{
return mModel->sortKey();
}
void MessageListView::setSortKey(const QMailMessageSortKey& sortKey)
{
mModel->setSortKey(sortKey);
}
QMailFolderId MessageListView::folderId() const
{
return mFolderId;
}
void MessageListView::setFolderId(const QMailFolderId& folderId)
{
mFolderId = folderId;
}
void MessageListView::init()
{
mPreviousCurrentCache.setMaxCost(10000); // Cache current item for 1,000 folders
connect(&mExpandAllTimer, SIGNAL(timeout()),
this, SLOT(expandAll()));
mQuickSearchWidget = new QuickSearchWidget(this);
connect(mQuickSearchWidget,SIGNAL(quickSearchRequested(QMailMessageKey)),this,SLOT(quickSearch(QMailMessageKey)));
connect(mQuickSearchWidget,SIGNAL(quickSearchReset()),this,SLOT(quickSearchReset()));
connect(mQuickSearchWidget,SIGNAL(fullSearchRequested()),this,SIGNAL(fullSearchRequested()));
reset();
mMessageList->setObjectName("messagelistview");
mMessageList->setUniformRowHeights(true);
mMessageList->setAlternatingRowColors(true);
mMessageList->header()->setDefaultSectionSize(180);
connect(mMessageList, SIGNAL(clicked(QModelIndex)),
this, SLOT(indexClicked(QModelIndex)));
connect(mMessageList, SIGNAL(activated(QModelIndex)),
this, SLOT(indexActivated(QModelIndex)));
connect(mMessageList, SIGNAL(backPressed()),
this, SIGNAL(backPressed()));
connect(mMessageList, SIGNAL(doubleClicked(QModelIndex)),
this, SLOT(indexDoubleClicked(QModelIndex)));
connect(mMessageList, SIGNAL(moreButtonClicked()),
this, SIGNAL(moreClicked()));
connect(mMessageList, SIGNAL(scrolled()),
this, SLOT(reviewVisibleMessages()));
connect(mMessageList, SIGNAL(collapsed(QModelIndex)),
this, SIGNAL(rowCountChanged()));
connect(mMessageList, SIGNAL(expanded(QModelIndex)),
this, SIGNAL(rowCountChanged()));
mScrollTimer.setSingleShot(true);
connect(&mScrollTimer, SIGNAL(timeout()),
this, SLOT(scrollTimeout()));
QVBoxLayout* vLayout = new QVBoxLayout(this);
vLayout->setContentsMargins(0,0,0,0);
vLayout->setSpacing(0);
vLayout->addWidget(mQuickSearchWidget);
vLayout->addWidget(mMessageList);
setFocusProxy(mMessageList);
}
void MessageListView::updateActions()
{
// Don't show get more message button when quick search bar is being used
if (!mQuickSearchIsEmpty) {
setMoreButtonVisible(false);
return;
}
// We need to see if the folder status has changed
bool partialContent(false);
if (mFolderId.isValid()) {
QMailFolder folder;
folder = QMailFolder(mFolderId);
// Are there more messages to be retrieved for this folder?
if (folder.status() & QMailFolder::PartialContent) {
// More messages to retrieve
partialContent = true;
}
}
setMoreButtonVisible(partialContent);
}
QMailMessageId MessageListView::current() const
{
return mMessageList->currentIndex().data(QMailMessageModelBase::MessageIdRole).value<QMailMessageId>();
}
void MessageListView::setCurrent(const QMailMessageId& id)
{
QModelIndex index = mModel->indexFromId(id);
if (index.isValid()) {
mMessageList->setCurrentIndex(index);
}
}
void MessageListView::setNextCurrent()
{
QModelIndex index = mMessageList->indexBelow(mMessageList->currentIndex());
if (index.isValid()) {
mMessageList->setCurrentIndex(index);
}
}
void MessageListView::setPreviousCurrent()
{
QModelIndex index = mMessageList->indexAbove(mMessageList->currentIndex());
if (index.isValid()) {
mMessageList->setCurrentIndex(index);
}
}
bool MessageListView::hasNext() const
{
return (mMessageList->indexBelow(mMessageList->currentIndex()).isValid());
}
bool MessageListView::hasPrevious() const
{
return (mMessageList->indexAbove(mMessageList->currentIndex()).isValid());
}
bool MessageListView::hasParent() const
{
return (mMessageList->currentIndex().parent().isValid());
}
bool MessageListView::hasChildren() const
{
return (mModel->rowCount(mMessageList->currentIndex()) > 0);
}
int MessageListView::rowCount(const QModelIndex &parentIndex) const
{
int total = 0;
int count = mModel->rowCount(parentIndex);
total += count;
for (int i = 0; i < count; ++i) {
// Count the children of any expanded node
QModelIndex idx(mModel->index(i, 0, parentIndex));
if (mMessageList->isExpanded(idx)) {
total += rowCount(idx);
}
}
return total;
}
void MessageListView::selectedChildren(const QModelIndex &parentIndex, QMailMessageIdList *selectedIds) const
{
for (int i = 0, count = mModel->rowCount(parentIndex); i < count; ++i) {
QModelIndex idx(mModel->index(i, 0, parentIndex));
if (static_cast<Qt::CheckState>(idx.data(Qt::CheckStateRole).toInt()) == Qt::Checked) {
selectedIds->append(idx.data(QMailMessageModelBase::MessageIdRole).value<QMailMessageId>());
}
selectedChildren(idx, selectedIds);
}
}
QMailMessageIdList MessageListView::selected() const
{
QMailMessageIdList selectedIds;
if (mMarkingMode) {
selectedChildren(mMessageList->rootIndex(), &selectedIds);
} else {
if (current().isValid())
selectedIds.append(current());
}
return selectedIds;
}
void MessageListView::setSelected(const QMailMessageIdList& ids)
{
if (mMarkingMode) {
foreach (const QMailMessageId& id, ids)
setSelected(id);
}
}
void MessageListView::setSelected(const QMailMessageId& id)
{
QModelIndex index = mModel->indexFromId(id);
if (index.isValid()) {
if (mMarkingMode) {
mModel->setData(index, static_cast<int>(Qt::Checked), Qt::CheckStateRole);
} else {
mMessageList->setCurrentIndex(index);
}
}
}
void MessageListView::selectChildren(const QModelIndex &parentIndex, bool selected, bool *modified)
{
for (int i = 0, count = mModel->rowCount(parentIndex); i < count; ++i) {
QModelIndex idx(mModel->index(i, 0, parentIndex));
Qt::CheckState state(static_cast<Qt::CheckState>(idx.data(Qt::CheckStateRole).toInt()));
if (selected && (state == Qt::Unchecked)) {
mModel->setData(idx, static_cast<int>(Qt::Checked), Qt::CheckStateRole);
*modified = true;
} else if (!selected && (state == Qt::Checked)) {
mModel->setData(idx, static_cast<int>(Qt::Unchecked), Qt::CheckStateRole);
*modified = true;
}
selectChildren(idx, selected, modified);
}
}
void MessageListView::selectAll()
{
bool modified(false);
selectChildren(mMessageList->rootIndex(), true, &modified);
if (modified)
emit selectionChanged();
}
void MessageListView::clearSelection()
{
bool modified(false);
selectChildren(mMessageList->rootIndex(), false, &modified);
if (modified)
emit selectionChanged();
}
bool MessageListView::markingMode() const
{
return mMarkingMode;
}
void MessageListView::setMarkingMode(bool set)
{
if (mMarkingMode != set) {
mMarkingMode = set;
emit selectionChanged();
}
}
bool MessageListView::moreButtonVisible() const
{
return mShowMoreButton;
}
void MessageListView::setMoreButtonVisible(bool set)
{
if (mShowMoreButton != set) {
mShowMoreButton = set;
mMessageList->reset();
}
}
bool MessageListView::threaded() const
{
return mThreaded;
}
void MessageListView::setThreaded(bool set)
{
if (mThreaded != set) {
mThreaded = set;
reset();
}
}
bool MessageListView::ignoreUpdatesWhenHidden() const
{
return mIgnoreWhenHidden;
}
void MessageListView::setIgnoreUpdatesWhenHidden(bool ignore)
{
if (ignore != mIgnoreWhenHidden) {
mIgnoreWhenHidden = ignore;
if (!ignore && mModel->ignoreMailStoreUpdates())
mModel->setIgnoreMailStoreUpdates(false);
}
}
void MessageListView::showEvent(QShowEvent* e)
{
mModel->setIgnoreMailStoreUpdates(false);
QWidget::showEvent(e);
}
void MessageListView::hideEvent(QHideEvent* e)
{
if (mIgnoreWhenHidden)
mModel->setIgnoreMailStoreUpdates(true);
QWidget::hideEvent(e);
}
void MessageListView::modelChanged()
{
mMarkingMode = false;
}
void MessageListView::indexClicked(const QModelIndex& index)
{
if (mMarkingMode) {
bool checked(static_cast<Qt::CheckState>(index.data(Qt::CheckStateRole).toInt()) == Qt::Checked);
mModel->setData(index, static_cast<int>(checked ? Qt::Unchecked : Qt::Checked), Qt::CheckStateRole);
emit selectionChanged();
} else {
QMailMessageId id(index.data(QMailMessageModelBase::MessageIdRole).value<QMailMessageId>());
if (id.isValid()) {
emit clicked(id);
}
}
}
void MessageListView::indexDoubleClicked(const QModelIndex& index)
{
if (!mMarkingMode) {
QMailMessageId id(index.data(QMailMessageModelBase::MessageIdRole).value<QMailMessageId>());
if (id.isValid()) {
emit doubleClicked(id);
}
}
}
void MessageListView::indexActivated(const QModelIndex& index)
{
if (mMarkingMode) {
return;
} else {
QMailMessageId id(index.data(QMailMessageListModel::MessageIdRole).value<QMailMessageId>());
if (id.isValid()) {
emit activated(id);
}
}
}
void MessageListView::currentIndexChanged(const QModelIndex& currentIndex,
const QModelIndex& previousIndex)
{
QMailMessageId currentId(currentIndex.data(QMailMessageModelBase::MessageIdRole).value<QMailMessageId>());
QMailMessageId previousId(previousIndex.data(QMailMessageModelBase::MessageIdRole).value<QMailMessageId>());
emit currentChanged(currentId, previousId);
if (!mMarkingMode)
emit selectionChanged();
}
void MessageListView::rowsAboutToBeRemoved(const QModelIndex &parentIndex, int start, int end)
{
if (mMarkingMode && !mSelectedRowsRemoved) {
// See if any of the removed rows are currently selected
for (int row = start; row <= end; ++row) {
QModelIndex idx(mModel->index(row, 0, parentIndex));
if (static_cast<Qt::CheckState>(idx.data(Qt::CheckStateRole).toInt()) == Qt::Checked) {
mSelectedRowsRemoved = true;
break;
}
}
}
}
void MessageListView::layoutChanged()
{
if (mSelectedRowsRemoved) {
mSelectedRowsRemoved = false;
emit selectionChanged();
}
}
void MessageListView::reviewVisibleMessages()
{
const int maximumRefreshRate = 7; //seconds
if (mScrollTimer.isActive())
return;
mPreviousVisibleItems = visibleMessagesIds();
emit visibleMessagesChanged();
mScrollTimer.start(maximumRefreshRate*1000);
}
void MessageListView::scrollTimeout()
{
QMailMessageIdList strictlyVisibleIds(visibleMessagesIds(false));
bool contained = true;
foreach(QMailMessageId id, strictlyVisibleIds) {
if (!mPreviousVisibleItems.contains(id)) {
contained = false;
break;
}
}
if (!contained) {
reviewVisibleMessages();
} else {
mPreviousVisibleItems.clear();
}
}
void MessageListView::quickSearch(const QMailMessageKey& key)
{
mQuickSearchIsEmpty = false;
if (current().isValid())
mPreviousCurrent = current();
if (key.isEmpty()) {
mModel->setKey(mKey);
mKey = QMailMessageKey();
} else {
if (mKey.isEmpty()) {
mKey = mModel->key();
}
mModel->setKey(key & mKey);
setMoreButtonVisible(false);
}
mExpandAllTimer.start(0);
}
void MessageListView::quickSearchReset()
{
mQuickSearchIsEmpty = true;
updateActions();
}
QMailMessageIdList MessageListView::visibleMessagesIds(bool buffer) const
{
QMailMessageIdList visibleIds;
const int bufferWidth = 50;
QRect viewRect(mMessageList->viewport()->rect());
QModelIndex start(mMessageList->indexAt(QPoint(0,0)));
QModelIndex index = start;
while (index.isValid()) { // Add visible items
QRect indexRect(mMessageList->visualRect(index));
if (!indexRect.isValid() || (!viewRect.intersects(indexRect)))
break;
visibleIds.append(index.data(QMailMessageModelBase::MessageIdRole).value<QMailMessageId>());
index = mMessageList->indexBelow(index);
}
if (!buffer)
return visibleIds;
int i = bufferWidth;
while (index.isValid() && (i > 0)) { // Add succeeding items
visibleIds.append(index.data(QMailMessageModelBase::MessageIdRole).value<QMailMessageId>());
index = mMessageList->indexBelow(index);
--i;
}
i = bufferWidth;
index = start;
if (index.isValid())
index = mMessageList->indexAbove(index);
while (index.isValid() && (i > 0)) { // Add preceding items
visibleIds.append(index.data(QMailMessageModelBase::MessageIdRole).value<QMailMessageId>());
index = mMessageList->indexAbove(index);
--i;
}
return visibleIds;
}
bool MessageListView::showingQuickSearch() const
{
return mQuickSearchWidget->isVisible();
}
void MessageListView::showQuickSearch(bool val)
{
mQuickSearchWidget->setVisible(val);
}
bool MessageListView::eventFilter(QObject *, QEvent *event)
{
if (event->type() == QEvent::KeyPress) {
if (QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event)) {
if (keyEvent->key() == Qt::Key_Back) {
emit backPressed();
return true;
}
}
}
return false;
}
void MessageListView::reset()
{
if (mQuickSearchWidget && mQuickSearchWidget->isVisible()) {
mQuickSearchWidget->reset();
}
QMailMessageKey key;
QMailMessageSortKey sortKey;
QMailMessageId currentId;
if (mModel) {
key = this->key();
sortKey = this->sortKey();
currentId = this->current();
delete mModel;
}
if (mThreaded) {
mModel = new MessageListModel<QMailMessageThreadedModel>(this);
} else {
mModel = new MessageListModel<QMailMessageListModel>(this);
}
connect(mModel, SIGNAL(modelChanged()),
this, SLOT(modelChanged()));
connect(mModel, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
this, SLOT(rowsAboutToBeRemoved(QModelIndex,int,int)));
connect(mModel, SIGNAL(layoutChanged()),
this, SLOT(layoutChanged()));
connect(mModel, SIGNAL(rowsRemoved(QModelIndex,int,int)),
this, SLOT(reviewVisibleMessages()));
connect(mModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
this, SLOT(reviewVisibleMessages()));
mMessageList->setModel(mModel);
mMessageList->setRootIsDecorated(false);
mExpandAllTimer.start(0);
connect(mMessageList->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
this, SLOT(currentIndexChanged(QModelIndex,QModelIndex)));
if (!key.isEmpty() || !mQuickSearchWidget->searchKey().isEmpty()) {
setKey(key);
}
if (!sortKey.isEmpty()) {
setSortKey(sortKey);
}
if (currentId.isValid()) {
setCurrent(currentId);
}
}
void MessageListView::expandAll()
{
mExpandAllTimer.stop();
mMessageList->expandAll();
scrollTo(mPreviousCurrent);
QTimer::singleShot(0, this, SLOT(reviewVisibleMessages()));
}
void MessageListView::scrollTo(const QMailMessageId &id)
{
if (!id.isValid())
return;
QModelIndex index(mModel->indexFromId(id));
if (index.isValid()) {
mMessageList->scrollTo(index);
mMessageList->setCurrentIndex(index);
}
}
#include "messagelistview.moc"
|