akonadi
noteutils.cpp
00001 /* This file is part of the KDE project 00002 Copyright (C) 2011 Christian Mollekopf <chrigi_1@fastmail.fm> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include "noteutils.h" 00021 00022 #include <klocalizedstring.h> 00023 #include <kdatetime.h> 00024 #include <kmime/kmime_message.h> 00025 #include <kdebug.h> 00026 00027 #include <qstring.h> 00028 #include <qtextdocument.h> 00029 00030 namespace Akonadi { 00031 namespace NoteUtils { 00032 00033 class NoteMessageWrapper::NoteMessageWrapperPrivate 00034 { 00035 public: 00036 NoteMessageWrapperPrivate() 00037 { 00038 } 00039 00040 NoteMessageWrapperPrivate( const KMime::Message::Ptr &msg ) 00041 : textFormat( Qt::PlainText ) 00042 { 00043 if (msg.get()) { 00044 title = msg->subject( true )->asUnicodeString(); 00045 text = msg->mainBodyPart()->decodedText( true ); //remove trailing whitespace, so we get rid of " " in empty notes 00046 if ( msg->from( false ) ) 00047 from = msg->from( false )->asUnicodeString(); 00048 creationDate = msg->date( true )->dateTime(); 00049 if ( msg->contentType( false ) && msg->contentType( false )->asUnicodeString() == QLatin1String("text/html") ) { 00050 textFormat = Qt::RichText; 00051 } 00052 } else { 00053 kWarning() << "Empty message"; 00054 } 00055 } 00056 00057 QString title; 00058 QString text; 00059 QString from; 00060 KDateTime creationDate; 00061 Qt::TextFormat textFormat; 00062 }; 00063 00064 NoteMessageWrapper::NoteMessageWrapper() 00065 : d_ptr( new NoteMessageWrapperPrivate() ) 00066 { 00067 } 00068 00069 NoteMessageWrapper::NoteMessageWrapper( const KMime::Message::Ptr &msg ) 00070 : d_ptr( new NoteMessageWrapperPrivate(msg) ) 00071 { 00072 } 00073 00074 NoteMessageWrapper::~NoteMessageWrapper() 00075 { 00076 delete d_ptr; 00077 } 00078 00079 KMime::Message::Ptr NoteMessageWrapper::message() const 00080 { 00081 const Q_D( NoteMessageWrapper ); 00082 KMime::Message::Ptr msg = KMime::Message::Ptr( new KMime::Message() ); 00083 00084 QByteArray encoding( "utf-8" ); 00085 00086 QString title = i18nc( "The default name for new notes.", "New Note" ); 00087 if ( !d->title.isEmpty() ) { 00088 title = d->title; 00089 } 00090 // Need a non-empty body part so that the serializer regards this as a valid message. 00091 QString text = QLatin1String(" "); 00092 if ( !d->text.isEmpty() ) { 00093 text = d->text; 00094 } 00095 00096 KDateTime creationDate = KDateTime::currentLocalDateTime(); 00097 if ( d->creationDate.isValid() ) { 00098 creationDate = d->creationDate; 00099 } 00100 00101 msg->subject( true )->fromUnicodeString( title, encoding ); 00102 msg->contentType( true )->setMimeType( d->textFormat == Qt::RichText ? "text/html" : "text/plain" ); 00103 msg->date( true )->setDateTime( creationDate ); 00104 msg->from( true )->fromUnicodeString( d->from, encoding ); 00105 msg->mainBodyPart()->fromUnicodeString( text ); 00106 00107 msg->assemble(); 00108 return msg; 00109 } 00110 00111 void NoteMessageWrapper::setCreationDate( const KDateTime &creationDate ) 00112 { 00113 Q_D(NoteMessageWrapper); 00114 d->creationDate = creationDate; 00115 } 00116 00117 KDateTime NoteMessageWrapper::creationDate() const 00118 { 00119 const Q_D(NoteMessageWrapper); 00120 return d->creationDate; 00121 } 00122 00123 void NoteMessageWrapper::setFrom( const QString &from ) 00124 { 00125 Q_D(NoteMessageWrapper); 00126 d->from = from; 00127 } 00128 00129 QString NoteMessageWrapper::from() const 00130 { 00131 const Q_D(NoteMessageWrapper); 00132 return d->from; 00133 } 00134 00135 void NoteMessageWrapper::setTitle( const QString &title ) 00136 { 00137 Q_D( NoteMessageWrapper ); 00138 d->title = title; 00139 } 00140 00141 QString NoteMessageWrapper::title() const 00142 { 00143 const Q_D( NoteMessageWrapper ); 00144 return d->title; 00145 } 00146 00147 void NoteMessageWrapper::setText( const QString &text, Qt::TextFormat format ) 00148 { 00149 Q_D( NoteMessageWrapper ); 00150 d->text = text; 00151 d->textFormat = format; 00152 } 00153 00154 QString NoteMessageWrapper::text() const 00155 { 00156 const Q_D( NoteMessageWrapper ); 00157 return d->text; 00158 } 00159 00160 Qt::TextFormat NoteMessageWrapper::textFormat() const 00161 { 00162 const Q_D( NoteMessageWrapper ); 00163 return d->textFormat; 00164 } 00165 00166 QString NoteMessageWrapper::toPlainText() const 00167 { 00168 const Q_D( NoteMessageWrapper ); 00169 if ( d->textFormat == Qt::PlainText ) { 00170 return d->text; 00171 } 00172 00173 //From cleanHtml in kdepimlibs/kcalutils/incidenceformatter.cpp 00174 QRegExp rx( QLatin1String("<body[^>]*>(.*)</body>"), Qt::CaseInsensitive ); 00175 rx.indexIn( d->text ); 00176 QString body = rx.cap( 1 ); 00177 00178 return Qt::escape( body.remove( QRegExp( QLatin1String("<[^>]*>") ) ).trimmed() ); 00179 } 00180 00181 QString noteIconName() 00182 { 00183 return QString::fromLatin1( "text-plain" ); 00184 } 00185 00186 QString noteMimeType() 00187 { 00188 return QString::fromLatin1( "text/x-vnd.akonadi.note" ); 00189 } 00190 00191 } //End Namepsace 00192 } //End Namepsace
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Apr 30 2012 21:49:15 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Apr 30 2012 21:49:15 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.