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

kmail

  • sources
  • kde-4.14
  • kdepim
  • kmail
  • searchdialog
kmsearchmessagemodel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2015 Montel Laurent <montel@kde.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16  *
17  * In addition, as a special exception, the copyright holders give
18  * permission to link the code of this program with any edition of
19  * the Qt library by Trolltech AS, Norway (or with modified versions
20  * of Qt that use the same license as Qt), and distribute linked
21  * combinations including the two. You must obey the GNU General
22  * Public License in all respects for all of the code used other than
23  * Qt. If you modify this file, you may extend this exception to
24  * your version of the file, but you are not obligated to do so. If
25  * you do not wish to do so, delete this exception statement from
26  * your version.
27  */
28 #include "kmsearchmessagemodel.h"
29 #include "mailcommon/util/mailutil.h"
30 #include "messagelist/messagelistutil.h"
31 
32 #include "messagecore/utils/stringutil.h"
33 
34 #include <akonadi/itemfetchscope.h>
35 #include <akonadi/monitor.h>
36 #include <akonadi/session.h>
37 
38 #include <akonadi/kmime/messageparts.h>
39 #include <kmime/kmime_message.h>
40 #ifndef Q_MOC_RUN
41 #include <boost/shared_ptr.hpp>
42 #endif
43 typedef boost::shared_ptr<KMime::Message> MessagePtr;
44 
45 #include <QColor>
46 #include <QApplication>
47 #include <QPalette>
48 #include <QTextDocument>
49 #include <kdebug.h>
50 #include <kglobal.h>
51 #include <klocale.h>
52 
53 KMSearchMessageModel::KMSearchMessageModel( QObject *parent )
54  : Akonadi::MessageModel( parent )
55 {
56  fetchScope().fetchFullPayload();
57  fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::All );
58 }
59 
60 KMSearchMessageModel::~KMSearchMessageModel( )
61 {
62 }
63 
64 QString toolTip( const Akonadi::Item& item )
65 {
66  MessagePtr msg = item.payload<MessagePtr>();
67 
68  QColor bckColor = QApplication::palette().color( QPalette::ToolTipBase );
69  QColor txtColor = QApplication::palette().color( QPalette::ToolTipText );
70 
71  const QString bckColorName = bckColor.name();
72  const QString txtColorName = txtColor.name();
73  const bool textIsLeftToRight = ( QApplication::layoutDirection() == Qt::LeftToRight );
74  const QString textDirection = textIsLeftToRight ? QLatin1String( "left" ) : QLatin1String( "right" );
75 
76  QString tip = QString::fromLatin1(
77  "<table width=\"100%\" border=\"0\" cellpadding=\"2\" cellspacing=\"0\">"
78  );
79  tip += QString::fromLatin1(
80  "<tr>" \
81  "<td bgcolor=\"%1\" align=\"%4\" valign=\"middle\">" \
82  "<div style=\"color: %2; font-weight: bold;\">" \
83  "%3" \
84  "</div>" \
85  "</td>" \
86  "</tr>"
87  ).arg( txtColorName ).arg( bckColorName ).arg( Qt::escape( msg->subject()->asUnicodeString() ) ).arg( textDirection );
88 
89  tip += QString::fromLatin1(
90  "<tr>" \
91  "<td align=\"center\" valign=\"middle\">" \
92  "<table width=\"100%\" border=\"0\" cellpadding=\"2\" cellspacing=\"0\">"
93  );
94 
95  const QString htmlCodeForStandardRow = QString::fromLatin1(
96  "<tr>" \
97  "<td align=\"right\" valign=\"top\" width=\"45\">" \
98  "<div style=\"font-weight: bold;\"><nobr>" \
99  "%1:" \
100  "</nobr></div>" \
101  "</td>" \
102  "<td align=\"left\" valign=\"top\">" \
103  "%2" \
104  "</td>" \
105  "</tr>" );
106 
107  QString content = MessageList::Util::contentSummary( item );
108 
109  if ( textIsLeftToRight ) {
110  tip += htmlCodeForStandardRow.arg( i18n( "From" ) ).arg( MessageCore::StringUtil::stripEmailAddr( msg->from()->asUnicodeString() ) );
111  tip += htmlCodeForStandardRow.arg( i18nc( "Receiver of the email", "To" ) ).arg( MessageCore::StringUtil::stripEmailAddr( msg->to()->asUnicodeString() ) );
112  tip += htmlCodeForStandardRow.arg( i18n( "Date" ) ).arg( KGlobal::locale()->formatDateTime( msg->date()->dateTime().toLocalZone(), KLocale::FancyLongDate ) );
113  if ( !content.isEmpty() ) {
114  tip += htmlCodeForStandardRow.arg( i18n( "Preview" ) ).arg( content.replace( QLatin1Char( '\n' ), QLatin1String( "<br>" ) ) );
115  }
116  } else {
117  tip += htmlCodeForStandardRow.arg( MessageCore::StringUtil::stripEmailAddr( msg->from()->asUnicodeString() ) ).arg( i18n( "From" ) );
118  tip += htmlCodeForStandardRow.arg( MessageCore::StringUtil::stripEmailAddr( msg->to()->asUnicodeString() ) ).arg( i18nc( "Receiver of the email", "To" ) );
119  tip += htmlCodeForStandardRow.arg( KGlobal::locale()->formatDateTime( msg->date()->dateTime().toLocalZone(), KLocale::FancyLongDate ) ).arg( i18n( "Date" ) );
120  if ( !content.isEmpty() ) {
121  tip += htmlCodeForStandardRow.arg( content.replace( QLatin1Char( '\n' ), QLatin1String( "<br>" ) ) ).arg( i18n( "Preview" ) );
122  }
123  }
124  tip += QString::fromLatin1(
125  "</table" \
126  "</td>" \
127  "</tr>"
128  );
129  return tip;
130 }
131 
132 int KMSearchMessageModel::columnCount( const QModelIndex & parent ) const
133 {
134  if ( collection().isValid()
135  && !collection().contentMimeTypes().contains( QLatin1String("message/rfc822") )
136  && collection().contentMimeTypes() != QStringList( QLatin1String("inode/directory") ) )
137  return 1;
138 
139  if ( !parent.isValid() )
140  return 8; // keep in sync with the column type enum
141 
142  return 0;
143 }
144 
145 QVariant KMSearchMessageModel::data( const QModelIndex & index, int role ) const
146 {
147  if ( !index.isValid() )
148  return QVariant();
149  if ( index.row() >= rowCount() )
150  return QVariant();
151 
152  if ( !collection().contentMimeTypes().contains( QLatin1String("message/rfc822") ) ) {
153  if ( role == Qt::DisplayRole )
154  return i18nc( "@label", "This model can only handle email folders. The current collection holds mimetypes: %1",
155  collection().contentMimeTypes().join( QLatin1String(",") ) );
156  else
157  return QVariant();
158  }
159 
160  Akonadi::Item item = itemForIndex( index );
161  if ( !item.hasPayload<MessagePtr>() )
162  return QVariant();
163  MessagePtr msg = item.payload<MessagePtr>();
164  if ( role == Qt::DisplayRole ) {
165  switch ( index.column() ) {
166  case Collection:
167  if ( item.storageCollectionId() >= 0 ) {
168  return MailCommon::Util::fullCollectionPath( Akonadi::Collection( item.storageCollectionId() ) );
169  }
170  return MailCommon::Util::fullCollectionPath(item.parentCollection());
171  case Subject:
172  return msg->subject()->asUnicodeString();
173  case Sender:
174  return msg->from()->asUnicodeString();
175  case Receiver:
176  return msg->to()->asUnicodeString();
177  case Date:
178  return KGlobal::locale()->formatDateTime( msg->date()->dateTime().toLocalZone(), KLocale::FancyLongDate );
179  case Size:
180  if ( item.size() == 0 )
181  return i18nc( "@label No size available", "-" );
182  else
183  return KGlobal::locale()->formatByteSize( item.size() );
184  case SizeNotLocalized:
185  return item.size();
186  case DateNotTranslated:
187  return msg->date()->dateTime().dateTime();
188  default:
189  return QVariant();
190  }
191  } else if ( role == Qt::EditRole ) {
192  switch ( index.column() ) {
193  case Collection:
194  if ( item.storageCollectionId() >= 0 ) {
195  return MailCommon::Util::fullCollectionPath( Akonadi::Collection( item.storageCollectionId() ) );
196  }
197  return MailCommon::Util::fullCollectionPath(item.parentCollection());
198  case Subject:
199  return msg->subject()->asUnicodeString();
200  case Sender:
201  return msg->from()->asUnicodeString();
202  case Receiver:
203  return msg->to()->asUnicodeString();
204  case Date:
205  return msg->date()->dateTime().dateTime();
206  case SizeNotLocalized:
207  case Size:
208  return item.size();
209  case DateNotTranslated:
210  return msg->date()->dateTime().dateTime();
211  default:
212  return QVariant();
213  }
214  } else if( role == Qt::ToolTipRole ) {
215  return toolTip( item );
216  }
217  return ItemModel::data( index, role );
218 }
219 
220 QVariant KMSearchMessageModel::headerData( int section, Qt::Orientation orientation, int role ) const
221 {
222 
223  if ( collection().isValid()
224  && !collection().contentMimeTypes().contains( QLatin1String("message/rfc822") )
225  && collection().contentMimeTypes() != QStringList( QLatin1String("inode/directory") ) )
226  return QVariant();
227 
228  if ( orientation == Qt::Horizontal && role == Qt::DisplayRole ) {
229  switch ( section ) {
230  case Collection:
231  return i18nc( "@title:column, folder (e.g. email)", "Folder" );
232  default:
233  return Akonadi::MessageModel::headerData( ( section-1 ), orientation, role );
234  }
235  }
236  return Akonadi::MessageModel::headerData( ( section-1 ), orientation, role );
237 }
238 
QModelIndex
QApplication::layoutDirection
Qt::LayoutDirection layoutDirection()
QColor::name
QString name() const
KMSearchMessageModel::~KMSearchMessageModel
~KMSearchMessageModel()
Definition: kmsearchmessagemodel.cpp:60
KMSearchMessageModel::Subject
Definition: kmsearchmessagemodel.h:40
KMSearchMessageModel::KMSearchMessageModel
KMSearchMessageModel(QObject *parent=0)
Definition: kmsearchmessagemodel.cpp:53
QPalette::color
const QColor & color(ColorGroup group, ColorRole role) const
QModelIndex::isValid
bool isValid() const
boost::shared_ptr
Definition: kmcomposewin.h:76
KMSearchMessageModel::columnCount
int columnCount(const QModelIndex &parent=QModelIndex()) const
Definition: kmsearchmessagemodel.cpp:132
KMSearchMessageModel::DateNotTranslated
Definition: kmsearchmessagemodel.h:45
QObject
QString::isEmpty
bool isEmpty() const
KMSearchMessageModel::SizeNotLocalized
Definition: kmsearchmessagemodel.h:46
QModelIndex::row
int row() const
KMSearchMessageModel::Size
Definition: kmsearchmessagemodel.h:44
KMSearchMessageModel::Collection
Definition: kmsearchmessagemodel.h:39
kmsearchmessagemodel.h
QString
QColor
QApplication::palette
QPalette palette()
QStringList
QLatin1Char
QString::replace
QString & replace(int position, int n, QChar after)
toolTip
QString toolTip(const Akonadi::Item &item)
Definition: kmsearchmessagemodel.cpp:64
QLatin1String
Qt::escape
QString escape(const QString &plain)
KMSearchMessageModel::data
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Definition: kmsearchmessagemodel.cpp:145
KMSearchMessageModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Definition: kmsearchmessagemodel.cpp:220
QModelIndex::column
int column() const
QString::fromLatin1
QString fromLatin1(const char *str, int size)
KMSearchMessageModel::Date
Definition: kmsearchmessagemodel.h:43
KMSearchMessageModel::Sender
Definition: kmsearchmessagemodel.h:41
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
MessagePtr
boost::shared_ptr< KMime::Message > MessagePtr
Definition: kmsearchmessagemodel.cpp:43
KMSearchMessageModel::Receiver
Definition: kmsearchmessagemodel.h:42
QVariant
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:34:33 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kmail

Skip menu "kmail"
  • Main Page
  • Namespace List
  • Namespace Members
  • 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