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"
29 #include <KProgressDialog>
33 #include <QDataStream>
35 #include <QProgressBar>
36 #include <QTextStream>
42 // Have to define before use
43 QDataStream
& operator<< ( QDataStream
&s
, const MailSummary
&d
)
45 s
<< d
.serialNumber();
51 tempTime
.setTime_t( d
.date() );
52 s
<< tempTime
.dateTime();
56 QDataStream
& operator>> ( QDataStream
&s
, MailSummary
&d
)
59 QString messageId
, subject
, from
, to
;
68 date
= KDateTime( tempTime
).toTime_t();
69 d
.set( serialNumber
, messageId
, subject
, from
, to
, date
);
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
;
83 QDataStream
& operator>> ( QDataStream
&s
, MailList
&mailList
)
89 mailList
.append( mailDrag
);
94 MailSummary::MailSummary( quint32 serialNumber
, const QString
&messageId
,
95 const QString
&subject
, const QString
&from
, const QString
&to
,
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
111 QString
MailSummary::subject() const
116 QString
MailSummary::from() const
121 QString
MailSummary::to() const
126 time_t MailSummary::date() const
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
;
143 MailSummary::operator KUrl() const { return KUrl(); }
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 */
164 QBuffer
buffer( &array
, 0 );
165 buffer
.open( QIODevice::WriteOnly
);
166 QDataStream
stream( &buffer
);
169 md
->setData( MailList::mimeDataType(), array
);
173 MailList
MailList::fromMimeData( const QMimeData
*md
)
175 if ( canDecode(md
) ) {
176 return decode( md
->data( mimeDataType() ) );
182 MailList
MailList::decode( const QByteArray
& payload
)
185 // A read-only data stream
186 QDataStream
stream( payload
);
187 if ( payload
.size() ) {
193 QByteArray
MailList::serialsFromMimeData( const QMimeData
*md
)
195 MailList mailList
= fromMimeData( md
);
196 if ( mailList
.count() ) {
197 MailList::iterator it
;
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();
213 MailListMimeData::MailListMimeData( MailTextSource
*src
)
214 : mMailTextSource( src
)
218 MailListMimeData::~MailListMimeData()
220 delete mMailTextSource
;
224 bool MailListMimeData::hasFormat ( const QString
& mimeType
) const
226 if ( mimeType
== QLatin1String( "message/rfc822" ) && mMailTextSource
)
229 return QMimeData::hasFormat( mimeType
);
232 QStringList
MailListMimeData::formats () const
234 QStringList theFormats
= QMimeData::formats();
235 if ( mMailTextSource
)
236 theFormats
.prepend( QLatin1String( "message/rfc822" ) );
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() );
253 dlg
->progressBar()->setValue( i
);
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() ) {
265 dlg
->progressBar()->setValue(++i
);
269 //kapp->eventLoop()->processEvents(QEventLoop::ExcludeSocketNotifiers);
276 return QMimeData::retrieveData( mimeType
, type
);