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() 
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( MailList mailList, QWidget * parent, MailTextSource *src )
00082     : QStoredDrag( MailListDrag::format(), 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, MailList &mailList )
00131 {
00132     MailList::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( MailList mailList )
00192 {
00193     QByteArray array;
00194     QBuffer buffer( array );
00195     buffer.open( IO_WriteOnly);
00196     QDataStream stream( array, IO_WriteOnly );
00197     stream << mailList;
00198     buffer.close();
00199     setEncodedData( array );
00200 }
00201 
00202 const char *MailListDrag::format(int i) const
00203 {
00204     if (_src) {
00205         if (i == 0) {
00206             return "message/rfc822";
00207         } else {
00208             return QStoredDrag::format(i - 1);
00209         }
00210     }
00211 
00212     return QStoredDrag::format(i);
00213 }
00214 
00215 bool MailListDrag::provides(const char *mimeType) const
00216 {
00217     if (_src && QCString(mimeType) == "message/rfc822") {
00218         return true;
00219     }
00220 
00221     return QStoredDrag::provides(mimeType);
00222 }
00223 
00224 QByteArray MailListDrag::encodedData(const char *mimeType) const
00225 {
00226     if (QCString(mimeType) != "message/rfc822") {
00227         return QStoredDrag::encodedData(mimeType);
00228     }
00229 
00230     QByteArray rc; 
00231     if (_src) {
00232         MailList ml;
00233         QByteArray enc = QStoredDrag::encodedData(format());
00234         decode(enc, ml);
00235 
00236         KProgressDialog *dlg = new KProgressDialog(0, 0, QString::null, i18n("Retrieving and storing messages..."), true);
00237         dlg->setAllowCancel(true);
00238         dlg->progressBar()->setTotalSteps(ml.count());
00239         int i = 0;
00240         dlg->progressBar()->setValue(i);
00241         dlg->show();
00242 
00243         QTextStream *ts = new QTextStream(rc, IO_WriteOnly);
00244         for (MailList::ConstIterator it = ml.begin(); it != ml.end(); ++it) {
00245             MailSummary mailDrag = *it;
00246             *ts << _src->text(mailDrag.serialNumber());
00247             if (dlg->wasCancelled()) {
00248                 break;
00249             }
00250             dlg->progressBar()->setValue(++i);
00251             kapp->eventLoop()->processEvents(QEventLoop::ExcludeSocketNotifiers);
00252         }
00253 
00254         delete dlg;
00255         delete ts;
00256     }
00257     return rc;
00258 }
00259 
KDE Home | KDE Accessibility Home | Description of Access Keys