Fix clipping/eliding of multi-line task descriptions.
[kdepim.git] / libkdepim / maillistdrag.cpp
blob102a2ea14c5a561472877c27f0af6c817aa0bc2a
1 /*
2 This file is part of libkdepim.
4 Copyright (c) 2003 Don Sanders <sanders@kde.org>
5 Copyright (c) 2005 George Staikos <staikos@kde.org>
6 Copyright (c) 2005 Rafal Rzepecki <divide@users.sourceforge.net>
7 Copyright (c) 2008 Thomas McGuire <mcguire@kde.org>
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public
11 License as published by the Free Software Foundation; either
12 version 2 of the License, or (at your option) any later version.
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Library General Public License for more details.
19 You should have received a copy of the GNU Library General Public License
20 along with this library; see the file COPYING.LIB. If not, write to
21 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA.
25 #include "maillistdrag.h"
27 #include <KDateTime>
28 #include <KLocale>
29 #include <KProgressDialog>
30 #include <KUrl>
32 #include <QBuffer>
33 #include <QDataStream>
34 #include <QEventLoop>
35 #include <QProgressBar>
36 #include <QTextStream>
37 #include <QDropEvent>
39 using namespace KPIM;
42 // Have to define before use
43 QDataStream& operator<< ( QDataStream &s, const MailSummary &d )
45 s << d.serialNumber();
46 s << d.messageId();
47 s << d.subject();
48 s << d.from();
49 s << d.to();
50 KDateTime tempTime;
51 tempTime.setTime_t( d.date() );
52 s << tempTime.dateTime();
53 return s;
56 QDataStream& operator>> ( QDataStream &s, MailSummary &d )
58 quint32 serialNumber;
59 QString messageId, subject, from, to;
60 time_t date;
61 s >> serialNumber;
62 s >> messageId;
63 s >> subject;
64 s >> from;
65 s >> to;
66 QDateTime tempTime;
67 s >> tempTime;
68 date = KDateTime( tempTime ).toTime_t();
69 d.set( serialNumber, messageId, subject, from, to, date );
70 return s;
73 QDataStream& operator<< ( QDataStream &s, const MailList &mailList )
75 MailList::const_iterator it;
76 for (it = mailList.begin(); it != mailList.end(); ++it) {
77 MailSummary mailDrag = *it;
78 s << mailDrag;
80 return s;
83 QDataStream& operator>> ( QDataStream &s, MailList &mailList )
85 mailList.clear();
86 MailSummary mailDrag;
87 while (!s.atEnd()) {
88 s >> mailDrag;
89 mailList.append( mailDrag );
91 return s;
94 MailSummary::MailSummary( quint32 serialNumber, const QString &messageId,
95 const QString &subject, const QString &from, const QString &to,
96 time_t date )
97 : mSerialNumber( serialNumber ), mMessageId( messageId ),
98 mSubject( subject ), mFrom( from ), mTo( to ), mDate( date )
101 quint32 MailSummary::serialNumber() const
103 return mSerialNumber;
106 QString MailSummary::messageId() const
108 return mMessageId;
111 QString MailSummary::subject() const
113 return mSubject;
116 QString MailSummary::from() const
118 return mFrom;
121 QString MailSummary::to() const
123 return mTo;
126 time_t MailSummary::date() const
128 return mDate;
131 void MailSummary::set( quint32 serialNumber, const QString &messageId,
132 const QString &subject, const QString &from, const QString &to, time_t date )
134 mSerialNumber = serialNumber;
135 mMessageId = messageId;
136 mSubject = subject;
137 mFrom = from;
138 mTo = to;
139 mDate = date;
142 #ifdef Q_CC_MSVC
143 MailSummary::operator KUrl() const { return KUrl(); }
144 #endif
146 QString MailList::mimeDataType()
148 return QLatin1String( "x-kmail-drag/message-list" );
151 bool MailList::canDecode( const QMimeData *md )
153 return md->hasFormat( mimeDataType() );
156 void MailList::populateMimeData( QMimeData *md )
158 /* We have three different possible mime types: x-kmail-drag/message-list, message/rfc822, and URL
159 Add them in this order */
161 /* Popuplate the MimeData with the custom streaming x-kmail-drag/message-list mime type */
162 if ( count() ) {
163 QByteArray array;
164 QBuffer buffer( &array, 0 );
165 buffer.open( QIODevice::WriteOnly);
166 QDataStream stream( &buffer );
167 stream << (*this);
168 buffer.close();
169 md->setData( MailList::mimeDataType(), array );
173 MailList MailList::fromMimeData( const QMimeData *md )
175 if ( canDecode(md) ) {
176 return decode( md->data( mimeDataType() ) );
177 } else {
178 return MailList();
182 MailList MailList::decode( const QByteArray& payload )
184 MailList mailList;
185 // A read-only data stream
186 QDataStream stream( payload );
187 if ( payload.size() ) {
188 stream >> mailList;
190 return mailList;
193 QByteArray MailList::serialsFromMimeData( const QMimeData *md )
195 MailList mailList = fromMimeData( md );
196 if ( mailList.count() ) {
197 MailList::iterator it;
198 QByteArray a;
199 QBuffer buffer( &a );
200 buffer.open( QIODevice::WriteOnly );
201 QDataStream stream( &buffer );
202 for (it = mailList.begin(); it != mailList.end(); ++it) {
203 MailSummary mailDrag = *it;
204 stream << mailDrag.serialNumber();
206 buffer.close();
207 return a;
208 } else {
209 return QByteArray();
213 MailListMimeData::MailListMimeData( MailTextSource *src )
214 : mMailTextSource( src )
218 MailListMimeData::~MailListMimeData()
220 delete mMailTextSource;
221 mMailTextSource = 0;
224 bool MailListMimeData::hasFormat ( const QString & mimeType ) const
226 if ( mimeType == QLatin1String( "message/rfc822" ) && mMailTextSource )
227 return true;
228 else
229 return QMimeData::hasFormat( mimeType );
232 QStringList MailListMimeData::formats () const
234 QStringList theFormats = QMimeData::formats();
235 if ( mMailTextSource )
236 theFormats.prepend( QLatin1String( "message/rfc822" ) );
237 return theFormats;
240 QVariant MailListMimeData::retrieveData( const QString & mimeType,
241 QVariant::Type type ) const
243 if ( ( mimeType == QLatin1String( "message/rfc822" ) ) && mMailTextSource ) {
245 if ( mMails.isEmpty() ) {
246 MailList list = MailList::fromMimeData( this );
247 KProgressDialog *dlg = new KProgressDialog( 0, QString(),
248 i18n("Retrieving and storing messages...") );
249 dlg->setWindowModality( Qt::WindowModal );
250 dlg->setAllowCancel( true );
251 dlg->progressBar()->setMaximum( list.size() );
252 int i = 0;
253 dlg->progressBar()->setValue( i );
254 dlg->show();
256 for ( MailList::ConstIterator it = list.constBegin(); it != list.constEnd(); ++it ) {
258 // Get the serial number from the mail summary and use the mail text source
259 // to get the actual text of the mail.
260 MailSummary mailSummary = *it;
261 mMails.append( mMailTextSource->text( mailSummary.serialNumber() ) );
262 if ( dlg->wasCancelled() ) {
263 break;
265 dlg->progressBar()->setValue(++i);
266 #ifdef __GNUC__
267 #warning Port me!
268 #endif
269 //kapp->eventLoop()->processEvents(QEventLoop::ExcludeSocketNotifiers);
271 delete dlg;
273 return mMails;
275 else
276 return QMimeData::retrieveData( mimeType, type );