1 /* -*- mode: C++; c-file-style: "gnu" -*-
2 This file is part of KMail, the KDE mail client.
3 Copyright (c) 2011 Montel Laurent <montel@kde.org>
5 KMail is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License, version 2, as
7 published by the Free Software Foundation.
9 KMail is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 #include "messagelistutil.h"
19 #include "core/settings.h"
20 #include <QTextDocument>
22 #include <KConfigGroup>
25 #include <KLocalizedString>
26 #include <KMime/Message>
27 #include <Akonadi/Item>
29 using namespace MessageList::Core
;
31 QString
MessageList::Util::messageSortingConfigName()
33 return QLatin1String( "MessageSorting" );
36 QString
MessageList::Util::messageSortDirectionConfigName()
38 return QLatin1String( "MessageSortDirection" );
41 QString
MessageList::Util::groupSortingConfigName()
43 return QLatin1String( "GroupSorting" );
46 QString
MessageList::Util::groupSortDirectionConfigName()
48 return QLatin1String( "GroupSortDirection" );
51 QString
MessageList::Util::messageUniqueIdConfigName()
53 return QString::fromLatin1( "MessageUniqueIdForStorageModel%1" );
56 QString
MessageList::Util::storageModelSortOrderGroup()
58 return QLatin1String( "MessageListView::StorageModelSortOrder" );
61 QString
MessageList::Util::storageModelThemesGroup()
63 return QLatin1String( "MessageListView::StorageModelThemes" );
66 QString
MessageList::Util::storageModelAggregationsGroup()
68 return QLatin1String( "MessageListView::StorageModelAggregations" );
72 QString
MessageList::Util::setForStorageModelConfigName()
74 return QString::fromLatin1( "SetForStorageModel%1" );
77 QString
MessageList::Util::storageModelSelectedMessageGroup()
79 return QLatin1String( "MessageListView::StorageModelSelectedMessages" );
83 void MessageList::Util::deleteConfig( const QString
& collectionId
)
85 KConfigGroup
confselectedMessage( Settings::self()->config(),
86 MessageList::Util::storageModelSelectedMessageGroup() );
87 confselectedMessage
.deleteEntry( MessageList::Util::messageUniqueIdConfigName().arg( collectionId
) );
89 KConfigGroup
storageModelOrder( Settings::self()->config(),
90 MessageList::Util::storageModelSortOrderGroup() );
91 storageModelOrder
.deleteEntry( collectionId
+ groupSortDirectionConfigName() );
92 storageModelOrder
.deleteEntry( collectionId
+ groupSortingConfigName() );
93 storageModelOrder
.deleteEntry( collectionId
+ messageSortDirectionConfigName() );
94 storageModelOrder
.deleteEntry( collectionId
+ messageSortingConfigName() );
96 KConfigGroup
storageModelTheme( Settings::self()->config(),
97 MessageList::Util::storageModelThemesGroup() );
98 storageModelTheme
.deleteEntry( collectionId
+ setForStorageModelConfigName() );
101 KConfigGroup
storageModelAggregation( Settings::self()->config(),
102 MessageList::Util::storageModelAggregationsGroup() );
103 storageModelAggregation
.deleteEntry( collectionId
+ setForStorageModelConfigName() );
107 QColor
MessageList::Util::unreadDefaultMessageColor()
109 return QColor( "blue" );
112 QColor
MessageList::Util::importantDefaultMessageColor()
114 return QColor( 0x98, 0x0, 0x0 );
117 QColor
MessageList::Util::todoDefaultMessageColor()
119 return QColor( 0x0, 0x98, 0x0 );
122 void MessageList::Util::fillViewMenu( KMenu
* menu
, QObject
*receiver
)
124 KMenu
* sortingMenu
= new KMenu( i18n( "Sorting" ), menu
);
125 sortingMenu
->setIcon( KIcon( QLatin1String( "view-sort-ascending" ) ) );
126 menu
->addMenu( sortingMenu
);
127 QObject::connect( sortingMenu
, SIGNAL(aboutToShow()),
128 receiver
, SLOT(sortOrderMenuAboutToShow()) );
130 KMenu
* aggregationMenu
= new KMenu( i18n( "Aggregation" ), menu
);
131 aggregationMenu
->setIcon( KIcon( QLatin1String( "view-process-tree" ) ) );
132 menu
->addMenu( aggregationMenu
);
133 QObject::connect( aggregationMenu
, SIGNAL(aboutToShow()),
134 receiver
, SLOT(aggregationMenuAboutToShow()) );
136 KMenu
* themeMenu
= new KMenu( i18n( "Theme" ), menu
);
137 themeMenu
->setIcon( KIcon( QLatin1String( "preferences-desktop-theme" ) ) );
138 menu
->addMenu( themeMenu
);
139 QObject::connect( themeMenu
, SIGNAL(aboutToShow()),
140 receiver
, SLOT(themeMenuAboutToShow()) );
143 QString
MessageList::Util::contentSummary( const Akonadi::Item
&item
)
145 if ( !item
.hasPayload
<KMime::Message::Ptr
>() ) {
149 KMime::Message::Ptr message
= item
.payload
<KMime::Message::Ptr
>();
150 KMime::Content
*textContent
= message
->textContent();
151 if ( !textContent
) {
154 const QString content
= textContent
->decodedText( true, true );
155 if ( content
.isEmpty() ) {
159 // Extract the first 5 non-empty, non-quoted lines from the content and return it
161 const int maxLines
= 5;
162 const QStringList lines
= content
.split( QLatin1Char( '\n' ) );
164 foreach( const QString
&line
, lines
) {
165 const QString lineTrimmed
= line
.trimmed();
166 const bool isQuoted
= lineTrimmed
.startsWith( QLatin1Char( '>' ) ) || lineTrimmed
.startsWith( QLatin1Char( '|' ) );
167 if ( !isQuoted
&& !lineTrimmed
.isEmpty() ) {
168 ret
+= line
+ QLatin1Char( '\n' );
170 if ( numLines
>= maxLines
)
174 return Qt::escape(ret
);