libkdepim

maillistdrag.cpp

00001 /*
00002     This file is part of libkdepim.
00003 
00004     Copyright (c) 2003 Don Sanders <sanders@kde.org>
00005     Copyright (c) 2005 George Staikos <staikos@kde.org>
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Library General Public
00009     License as published by the Free Software Foundation; either
00010     version 2 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Library General Public License for more details.
00016 
00017     You should have received a copy of the GNU Library General Public License
00018     along with this library; see the file COPYING.LIB.  If not, write to
00019     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00020     Boston, MA 02110-1301, USA.
00021 */
00022 
00023 #include "maillistdrag.h"
00024 #include <qbuffer.h>
00025 #include <qdatastream.h>
00026 #include <qeventloop.h>
00027 #include <kapplication.h>
00028 #include <klocale.h>
00029 #include <kprogress.h>
00030 
00031 using namespace KPIM;
00032 
00033 MailSummary::MailSummary( Q_UINT32 serialNumber, QString messageId,
00034               QString subject, QString from, QString to,
00035               time_t date )
00036     : mSerialNumber( serialNumber ), mMessageId( messageId ),
00037       mSubject( subject ), mFrom( from ), mTo( to ), mDate( date )
00038 {}
00039 
00040 Q_UINT32 MailSummary::serialNumber() const
00041 {
00042     return mSerialNumber;
00043 }
00044 
00045 QString MailSummary::messageId()
00046 {
00047     return mMessageId;
00048 }
00049 
00050 QString MailSummary::subject()
00051 {
00052     return mSubject;
00053 }
00054 
00055 QString MailSummary::from()
00056 {
00057     return mFrom;
00058 }
00059 
00060 QString MailSummary::to()
00061 {
00062     return mTo;
00063 }
00064 
00065 time_t MailSummary::date()
00066 {
00067     return mDate;
00068 }
00069 
00070 void MailSummary::set( Q_UINT32 serialNumber, QString messageId,
00071                QString subject, QString from, QString to, time_t date )
00072 {
00073     mSerialNumber = serialNumber;
00074     mMessageId = messageId;
00075     mSubject = subject;
00076     mFrom = from;
00077     mTo = to;
00078     mDate = date;
00079 }
00080 
00081 MailListDrag::MailListDrag( const MailList& mailList, QWidget * parent, MailTextSource *src )
00082     : QDragObject( parent ), _src(src)
00083 {
00084     setMailList( mailList );
00085 }
00086 
00087 MailListDrag::~MailListDrag()
00088 {
00089     delete _src;
00090     _src = 0;
00091 }
00092 
00093 const char* MailListDrag::format()
00094 {
00095     return "x-kmail-drag/message-list";
00096 }
00097 
00098 bool MailListDrag::canDecode( QMimeSource *e )
00099 {
00100     return e->provides( MailListDrag::format() );
00101 }
00102 
00103 // Have to define before use
00104 QDataStream& operator<< ( QDataStream &s, MailSummary &d )
00105 {
00106     s << d.serialNumber();
00107     s << d.messageId();
00108     s << d.subject();
00109     s << d.from();
00110     s << d.to();
00111     s << d.date();
00112     return s;
00113 }
00114 
00115 QDataStream& operator>> ( QDataStream &s, MailSummary &d )
00116 {
00117     Q_UINT32 serialNumber;
00118     QString messageId, subject, from, to;
00119     time_t date;
00120     s >> serialNumber;
00121     s >> messageId;
00122     s >> subject;
00123     s >> from;
00124     s >> to;
00125     s >> date;
00126     d.set( serialNumber, messageId, subject, from, to, date );
00127     return s;
00128 }
00129 
00130 QDataStream& operator<< ( QDataStream &s, const MailList &mailList )
00131 {
00132     MailList::const_iterator it;
00133     for (it = mailList.begin(); it != mailList.end(); ++it) {
00134     MailSummary mailDrag = *it;
00135     s << mailDrag;
00136     }
00137     return s;
00138 }
00139 
00140 QDataStream& operator>> ( QDataStream &s, MailList &mailList )
00141 {
00142     mailList.clear();
00143     MailSummary mailDrag;
00144     while (!s.atEnd()) {
00145     s >> mailDrag;
00146     mailList.append( mailDrag );
00147     }
00148     return s;
00149 }
00150 
00151 bool MailListDrag::decode( QDropEvent* e, MailList& mailList )
00152 {
00153     QByteArray payload = e->encodedData( MailListDrag::format() );
00154     QDataStream buffer( payload, IO_ReadOnly );
00155     if ( payload.size() ) {
00156     e->accept();
00157     buffer >> mailList;
00158     return TRUE;
00159     }
00160     return FALSE;
00161 }
00162 
00163 bool MailListDrag::decode( QByteArray& payload, MailList& mailList )
00164 {
00165     QDataStream stream( payload, IO_ReadOnly );
00166     if ( payload.size() ) {
00167     stream >> mailList;
00168     return TRUE;
00169     }
00170     return FALSE;
00171 }
00172 
00173 bool MailListDrag::decode( QDropEvent* e, QByteArray &a )
00174 {
00175     MailList mailList;
00176     if (decode( e, mailList )) {
00177     MailList::iterator it;
00178     QBuffer buffer( a );
00179     buffer.open( IO_WriteOnly );
00180     QDataStream stream( &buffer );
00181     for (it = mailList.begin(); it != mailList.end(); ++it) {
00182         MailSummary mailDrag = *it;
00183         stream << mailDrag.serialNumber();
00184     }
00185     buffer.close();
00186     return TRUE;
00187     }
00188     return FALSE;
00189 }
00190 
00191 void MailListDrag::setMailList( const MailList& mailList )
00192 {
00193     _mailList = mailList;
00194 }
00195 
00196 const char *MailListDrag::format(int i) const
00197 {
00198   if (i == 0) {
00199     return "message/rfc822";
00200   } else if (i == 1) {
00201     return format();
00202   } else if (i == 2) {
00203     return "application/x-kde-suggestedfilename"; // issue3689
00204   } else {
00205     return 0;
00206   }
00207 }
00208 
00209 QByteArray MailListDrag::encodedData(const char *mimeType) const
00210 {
00211   QByteArray array;
00212 
00213   if (!qstricmp(mimeType, format())) {
00214     QBuffer buffer( array );
00215     buffer.open( IO_WriteOnly);
00216     QDataStream stream( array, IO_WriteOnly );
00217     stream << _mailList;
00218     buffer.close();
00219   } else if (!qstricmp(mimeType, "application/x-kde-suggestedfilename")) {
00220     if (!_mailList.isEmpty()) {
00221       MailSummary firstMail = _mailList.first();
00222       array = firstMail.subject().utf8();
00223     }
00224   } else if (!qstricmp(mimeType, "message/rfc822")) {
00225     KProgressDialog *dlg = new KProgressDialog(0, 0, QString::null, i18n("Retrieving and storing messages..."), true);
00226     dlg->setAllowCancel(true);
00227     dlg->progressBar()->setTotalSteps(_mailList.count());
00228     int i = 0;
00229     dlg->progressBar()->setValue(i);
00230     dlg->show();
00231 
00232     QTextStream ts(array, IO_WriteOnly);
00233     for (MailList::ConstIterator it = _mailList.begin(); it != _mailList.end(); ++it) {
00234       MailSummary mailDrag = *it;
00235       ts << _src->text(mailDrag.serialNumber());
00236       if (dlg->wasCancelled()) {
00237         break;
00238       }
00239       dlg->progressBar()->setValue(++i);
00240       qApp->eventLoop()->processEvents(QEventLoop::ExcludeSocketNotifiers);
00241     }
00242 
00243     delete dlg;
00244   }
00245   return array;
00246 }
KDE Home | KDE Accessibility Home | Description of Access Keys