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

kmail

  • sources
  • kde-4.12
  • kdepim
  • kmail
  • searchdialog
kmsearchmessagemodel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011, 2012 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 <nepomuk2/nmo.h>
35 #include <Nepomuk2/Resource>
36 #include <Nepomuk2/Variant>
37 
38 
39 #include <akonadi/itemfetchscope.h>
40 #include <akonadi/monitor.h>
41 #include <akonadi/session.h>
42 
43 #include <akonadi/kmime/messageparts.h>
44 #include <kmime/kmime_message.h>
45 #include <boost/shared_ptr.hpp>
46 typedef boost::shared_ptr<KMime::Message> MessagePtr;
47 
48 #include <QColor>
49 #include <QApplication>
50 #include <QPalette>
51 #include <QTextDocument>
52 #include <kdebug.h>
53 #include <kglobal.h>
54 #include <klocale.h>
55 
56 KMSearchMessageModel::KMSearchMessageModel( QObject *parent )
57  : Akonadi::MessageModel( parent )
58 {
59  fetchScope().fetchPayloadPart( Akonadi::MessagePart::Envelope );
60  fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::All );
61 }
62 
63 KMSearchMessageModel::~KMSearchMessageModel( )
64 {
65 }
66 
67 QString toolTip( const Akonadi::Item& item )
68 {
69  MessagePtr msg = item.payload<MessagePtr>();
70 
71  QColor bckColor = QApplication::palette().color( QPalette::ToolTipBase );
72  QColor txtColor = QApplication::palette().color( QPalette::ToolTipText );
73 
74  const QString bckColorName = bckColor.name();
75  const QString txtColorName = txtColor.name();
76  const bool textIsLeftToRight = ( QApplication::layoutDirection() == Qt::LeftToRight );
77  const QString textDirection = textIsLeftToRight ? QLatin1String( "left" ) : QLatin1String( "right" );
78 
79  QString tip = QString::fromLatin1(
80  "<table width=\"100%\" border=\"0\" cellpadding=\"2\" cellspacing=\"0\">"
81  );
82  tip += QString::fromLatin1(
83  "<tr>" \
84  "<td bgcolor=\"%1\" align=\"%4\" valign=\"middle\">" \
85  "<div style=\"color: %2; font-weight: bold;\">" \
86  "%3" \
87  "</div>" \
88  "</td>" \
89  "</tr>"
90  ).arg( txtColorName ).arg( bckColorName ).arg( Qt::escape( msg->subject()->asUnicodeString() ) ).arg( textDirection );
91 
92  tip += QString::fromLatin1(
93  "<tr>" \
94  "<td align=\"center\" valign=\"middle\">" \
95  "<table width=\"100%\" border=\"0\" cellpadding=\"2\" cellspacing=\"0\">"
96  );
97 
98  const QString htmlCodeForStandardRow = QString::fromLatin1(
99  "<tr>" \
100  "<td align=\"right\" valign=\"top\" width=\"45\">" \
101  "<div style=\"font-weight: bold;\"><nobr>" \
102  "%1:" \
103  "</nobr></div>" \
104  "</td>" \
105  "<td align=\"left\" valign=\"top\">" \
106  "%2" \
107  "</td>" \
108  "</tr>" );
109 
110  QString content = MessageList::Util::contentSummary( item.url() );
111 
112  if ( textIsLeftToRight ) {
113  tip += htmlCodeForStandardRow.arg( i18n( "From" ) ).arg( MessageCore::StringUtil::stripEmailAddr( msg->from()->asUnicodeString() ) );
114  tip += htmlCodeForStandardRow.arg( i18nc( "Receiver of the email", "To" ) ).arg( MessageCore::StringUtil::stripEmailAddr( msg->to()->asUnicodeString() ) );
115  tip += htmlCodeForStandardRow.arg( i18n( "Date" ) ).arg( KGlobal::locale()->formatDateTime( msg->date()->dateTime().toLocalZone(), KLocale::FancyLongDate ) );
116  if ( !content.isEmpty() ) {
117  tip += htmlCodeForStandardRow.arg( i18n( "Preview" ) ).arg( content.replace( QLatin1Char( '\n' ), QLatin1String( "<br>" ) ) );
118  }
119  } else {
120  tip += htmlCodeForStandardRow.arg( MessageCore::StringUtil::stripEmailAddr( msg->from()->asUnicodeString() ) ).arg( i18n( "From" ) );
121  tip += htmlCodeForStandardRow.arg( MessageCore::StringUtil::stripEmailAddr( msg->to()->asUnicodeString() ) ).arg( i18nc( "Receiver of the email", "To" ) );
122  tip += htmlCodeForStandardRow.arg( KGlobal::locale()->formatDateTime( msg->date()->dateTime().toLocalZone(), KLocale::FancyLongDate ) ).arg( i18n( "Date" ) );
123  if ( !content.isEmpty() ) {
124  tip += htmlCodeForStandardRow.arg( content.replace( QLatin1Char( '\n' ), QLatin1String( "<br>" ) ) ).arg( i18n( "Preview" ) );
125  }
126  }
127  tip += QString::fromLatin1(
128  "</table" \
129  "</td>" \
130  "</tr>"
131  );
132  return tip;
133 }
134 
135 int KMSearchMessageModel::columnCount( const QModelIndex & parent ) const
136 {
137  if ( collection().isValid()
138  && !collection().contentMimeTypes().contains( QLatin1String("message/rfc822") )
139  && collection().contentMimeTypes() != QStringList( QLatin1String("inode/directory") ) )
140  return 1;
141 
142  if ( !parent.isValid() )
143  return 8; // keep in sync with the column type enum
144 
145  return 0;
146 }
147 
148 QVariant KMSearchMessageModel::data( const QModelIndex & index, int role ) const
149 {
150  if ( !index.isValid() )
151  return QVariant();
152  if ( index.row() >= rowCount() )
153  return QVariant();
154 
155  if ( !collection().contentMimeTypes().contains( QLatin1String("message/rfc822") ) ) {
156  if ( role == Qt::DisplayRole )
157  return i18nc( "@label", "This model can only handle email folders. The current collection holds mimetypes: %1",
158  collection().contentMimeTypes().join( QLatin1String(",") ) );
159  else
160  return QVariant();
161  }
162 
163  Akonadi::Item item = itemForIndex( index );
164  if ( !item.hasPayload<MessagePtr>() )
165  return QVariant();
166  MessagePtr msg = item.payload<MessagePtr>();
167  if ( role == Qt::DisplayRole ) {
168  switch ( index.column() ) {
169  case Collection:
170  if ( item.storageCollectionId() >= 0 ) {
171  return MailCommon::Util::fullCollectionPath( Akonadi::Collection( item.storageCollectionId() ) );
172  }
173  return MailCommon::Util::fullCollectionPath(item.parentCollection());
174  case Subject:
175  return msg->subject()->asUnicodeString();
176  case Sender:
177  return msg->from()->asUnicodeString();
178  case Receiver:
179  return msg->to()->asUnicodeString();
180  case Date:
181  return KGlobal::locale()->formatDateTime( msg->date()->dateTime().toLocalZone(), KLocale::FancyLongDate );
182  case Size:
183  if ( item.size() == 0 )
184  return i18nc( "@label No size available", "-" );
185  else
186  return KGlobal::locale()->formatByteSize( item.size() );
187  case SizeNotLocalized:
188  return item.size();
189  case DateNotTranslated:
190  return msg->date()->dateTime().dateTime();
191  default:
192  return QVariant();
193  }
194  } else if ( role == Qt::EditRole ) {
195  switch ( index.column() ) {
196  case Collection:
197  if ( item.storageCollectionId() >= 0 ) {
198  return MailCommon::Util::fullCollectionPath( Akonadi::Collection( item.storageCollectionId() ) );
199  }
200  return MailCommon::Util::fullCollectionPath(item.parentCollection());
201  case Subject:
202  return msg->subject()->asUnicodeString();
203  case Sender:
204  return msg->from()->asUnicodeString();
205  case Receiver:
206  return msg->to()->asUnicodeString();
207  case Date:
208  return msg->date()->dateTime().dateTime();
209  case SizeNotLocalized:
210  case Size:
211  return item.size();
212  case DateNotTranslated:
213  return msg->date()->dateTime().dateTime();
214  default:
215  return QVariant();
216  }
217  } else if( role == Qt::ToolTipRole ) {
218  return toolTip( item );
219  }
220  return ItemModel::data( index, role );
221 }
222 
223 
224 
225 QVariant KMSearchMessageModel::headerData( int section, Qt::Orientation orientation, int role ) const
226 {
227 
228  if ( collection().isValid()
229  && !collection().contentMimeTypes().contains( QLatin1String("message/rfc822") )
230  && collection().contentMimeTypes() != QStringList( QLatin1String("inode/directory") ) )
231  return QVariant();
232 
233  if ( orientation == Qt::Horizontal && role == Qt::DisplayRole ) {
234  switch ( section ) {
235  case Collection:
236  return i18nc( "@title:column, folder (e.g. email)", "Folder" );
237  default:
238  return Akonadi::MessageModel::headerData( ( section-1 ), orientation, role );
239  }
240  }
241  return Akonadi::MessageModel::headerData( ( section-1 ), orientation, role );
242 }
243 
244 #include "kmsearchmessagemodel.moc"
KMSearchMessageModel::~KMSearchMessageModel
~KMSearchMessageModel()
Definition: kmsearchmessagemodel.cpp:63
KMSearchMessageModel::Subject
Definition: kmsearchmessagemodel.h:40
KMSearchMessageModel::KMSearchMessageModel
KMSearchMessageModel(QObject *parent=0)
Definition: kmsearchmessagemodel.cpp:56
QObject
boost::shared_ptr
Definition: kmcomposewin.h:72
KMSearchMessageModel::columnCount
int columnCount(const QModelIndex &parent=QModelIndex()) const
Definition: kmsearchmessagemodel.cpp:135
KMSearchMessageModel::DateNotTranslated
Definition: kmsearchmessagemodel.h:45
KMSearchMessageModel::SizeNotLocalized
Definition: kmsearchmessagemodel.h:46
KMSearchMessageModel::Size
Definition: kmsearchmessagemodel.h:44
KMSearchMessageModel::Collection
Definition: kmsearchmessagemodel.h:39
kmsearchmessagemodel.h
toolTip
QString toolTip(const Akonadi::Item &item)
Definition: kmsearchmessagemodel.cpp:67
KMSearchMessageModel::data
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Definition: kmsearchmessagemodel.cpp:148
KMSearchMessageModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Definition: kmsearchmessagemodel.cpp:225
KMSearchMessageModel::Date
Definition: kmsearchmessagemodel.h:43
KMSearchMessageModel::Sender
Definition: kmsearchmessagemodel.h:41
MessagePtr
boost::shared_ptr< KMime::Message > MessagePtr
Definition: kmsearchmessagemodel.cpp:46
KMSearchMessageModel::Receiver
Definition: kmsearchmessagemodel.h:42
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:58:52 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

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