• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdepim
  • Sitemap
  • Contact Us
 

libkdepim

maillistdrag.cpp

Go to the documentation of this file.
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     Copyright (c) 2005 Rafal Rzepecki <divide@users.sourceforge.net>
00007     Copyright (c) 2008 Thomas McGuire <mcguire@kde.org>
00008 
00009     This library is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU Library General Public
00011     License as published by the Free Software Foundation; either
00012     version 2 of the License, or (at your option) any later version.
00013 
00014     This library is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017     Library General Public License for more details.
00018 
00019     You should have received a copy of the GNU Library General Public License
00020     along with this library; see the file COPYING.LIB.  If not, write to
00021     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00022     Boston, MA 02110-1301, USA.
00023 */
00024 
00025 #include "maillistdrag.h"
00026 #include "kdepimprotocols.h"
00027 
00028 #include <KDateTime>
00029 #include <KLocale>
00030 #include <KProgressDialog>
00031 #include <KUrl>
00032 
00033 #include <QBuffer>
00034 #include <QDataStream>
00035 #include <QEventLoop>
00036 #include <QProgressBar>
00037 #include <QTextStream>
00038 #include <QDropEvent>
00039 
00040 using namespace KPIM;
00041 
00042 
00043 // Have to define before use
00044 QDataStream& operator<< ( QDataStream &s, const MailSummary &d )
00045 {
00046   s << d.serialNumber();
00047   s << d.messageId();
00048   s << d.subject();
00049   s << d.from();
00050   s << d.to();
00051   KDateTime tempTime;
00052   tempTime.setTime_t( d.date() );
00053   s << tempTime.dateTime();
00054   return s;
00055 }
00056 
00057 QDataStream& operator>> ( QDataStream &s, MailSummary &d )
00058 {
00059   quint32 serialNumber;
00060   QString messageId, subject, from, to;
00061   time_t date;
00062   s >> serialNumber;
00063   s >> messageId;
00064   s >> subject;
00065   s >> from;
00066   s >> to;
00067   QDateTime tempTime;
00068   s >> tempTime;
00069   date = KDateTime( tempTime ).toTime_t();
00070   d.set( serialNumber, messageId, subject, from, to, date );
00071   return s;
00072 }
00073 
00074 QDataStream& operator<< ( QDataStream &s, const MailList &mailList )
00075 {
00076   MailList::const_iterator it;
00077   for (it = mailList.begin(); it != mailList.end(); ++it) {
00078     MailSummary mailDrag = *it;
00079     s << mailDrag;
00080   }
00081   return s;
00082 }
00083 
00084 QDataStream& operator>> ( QDataStream &s, MailList &mailList )
00085 {
00086   mailList.clear();
00087   MailSummary mailDrag;
00088   while (!s.atEnd()) {
00089     s >> mailDrag;
00090     mailList.append( mailDrag );
00091   }
00092   return s;
00093 }
00094 
00095 MailSummary::MailSummary( quint32 serialNumber, const QString &messageId,
00096               const QString &subject, const QString &from, const QString &to,
00097               time_t date )
00098     : mSerialNumber( serialNumber ), mMessageId( messageId ),
00099       mSubject( subject ), mFrom( from ), mTo( to ), mDate( date )
00100 {}
00101 
00102 quint32 MailSummary::serialNumber() const
00103 {
00104     return mSerialNumber;
00105 }
00106 
00107 QString MailSummary::messageId()  const
00108 {
00109     return mMessageId;
00110 }
00111 
00112 QString MailSummary::subject() const
00113 {
00114     return mSubject;
00115 }
00116 
00117 QString MailSummary::from() const
00118 {
00119     return mFrom;
00120 }
00121 
00122 QString MailSummary::to() const
00123 {
00124     return mTo;
00125 }
00126 
00127 time_t MailSummary::date() const
00128 {
00129     return mDate;
00130 }
00131 
00132 void MailSummary::set( quint32 serialNumber, const QString &messageId,
00133                const QString &subject, const QString &from, const QString &to, time_t date )
00134 {
00135     mSerialNumber = serialNumber;
00136     mMessageId = messageId;
00137     mSubject = subject;
00138     mFrom = from;
00139     mTo = to;
00140     mDate = date;
00141 }
00142 
00143 #ifdef Q_CC_MSVC
00144 MailSummary::operator KUrl() const { return KUrl(); }
00145 #endif
00146 
00147 QString MailList::mimeDataType()
00148 {
00149   return "x-kmail-drag/message-list";
00150 }
00151 
00152 bool MailList::canDecode( const QMimeData *md )
00153 {
00154   return md->hasFormat( mimeDataType() );
00155 }
00156 
00157 void MailList::populateMimeData( QMimeData *md )
00158 {
00159   /* We have three different possible mime types: x-kmail-drag/message-list, message/rfc822, and URL
00160      Add them in this order */
00161 
00162   /* Popuplate the MimeData with the custom streaming x-kmail-drag/message-list mime type */
00163   if ( count() ) {
00164     QByteArray array;
00165     QBuffer buffer( &array, 0 );
00166     buffer.open( QIODevice::WriteOnly);
00167     QDataStream stream( &array, QIODevice::WriteOnly );
00168     stream << (*this);
00169     buffer.close();
00170     md->setData( MailList::mimeDataType(), array );
00171   }
00172 }
00173 
00174 MailList MailList::fromMimeData( const QMimeData *md )
00175 {
00176   if ( canDecode(md) ) {
00177     return decode( md->data( mimeDataType() ) );
00178   } else {
00179     return MailList();
00180   }
00181 }
00182 
00183 MailList MailList::decode( const QByteArray& payload )
00184 {
00185   MailList mailList;
00186   // A read-only data stream
00187   QDataStream stream( payload );
00188   if ( payload.size() ) {
00189     stream >> mailList;
00190   }
00191   return mailList;
00192 }
00193 
00194 QByteArray MailList::serialsFromMimeData( const QMimeData *md )
00195 {
00196   MailList mailList = fromMimeData( md );
00197   if ( mailList.count() ) {
00198     MailList::iterator it;
00199     QByteArray a;
00200     QBuffer buffer( &a );
00201     buffer.open( QIODevice::WriteOnly );
00202     QDataStream stream( &buffer );
00203     for (it = mailList.begin(); it != mailList.end(); ++it) {
00204       MailSummary mailDrag = *it;
00205       stream << mailDrag.serialNumber();
00206     }
00207     buffer.close();
00208     return a;
00209   } else {
00210     return QByteArray();
00211   }
00212 }
00213 
00214 MailListMimeData::MailListMimeData( MailTextSource *src )
00215   : mMailTextSource( src )
00216 {
00217 }
00218 
00219 MailListMimeData::~MailListMimeData()
00220 {
00221   delete mMailTextSource;
00222   mMailTextSource = 0;
00223 }
00224 
00225 bool MailListMimeData::hasFormat ( const QString & mimeType ) const
00226 {
00227   if ( mimeType == "message/rfc822" && mMailTextSource )
00228     return true;
00229   else
00230     return QMimeData::hasFormat( mimeType );
00231 }
00232 
00233 QStringList MailListMimeData::formats () const
00234 {
00235   QStringList theFormats = QMimeData::formats();
00236   if ( mMailTextSource )
00237     theFormats.prepend( QByteArray( "message/rfc822" ) );
00238   return theFormats;
00239 }
00240 
00241 QVariant MailListMimeData::retrieveData( const QString & mimeType,
00242                                          QVariant::Type type ) const
00243 {
00244   if ( ( mimeType == "message/rfc822" ) && mMailTextSource ) {
00245 
00246     if ( mMails.isEmpty() ) {
00247       MailList list = MailList::fromMimeData( this );
00248       KProgressDialog *dlg = new KProgressDialog( 0, QString(),
00249                                    i18n("Retrieving and storing messages...") );
00250       dlg->setWindowModality( Qt::WindowModal );
00251       dlg->setAllowCancel( true );
00252       dlg->progressBar()->setMaximum( list.size() );
00253       int i = 0;
00254       dlg->progressBar()->setValue( i );
00255       dlg->show();
00256 
00257       for ( MailList::ConstIterator it = list.begin(); it != list.end(); ++it ) {
00258 
00259         // Get the serial number from the mail summary and use the mail text source
00260         // to get the actual text of the mail.
00261         MailSummary mailSummary = *it;
00262         mMails.append( mMailTextSource->text( mailSummary.serialNumber() ) );
00263         if ( dlg->wasCancelled() ) {
00264           break;
00265         }
00266         dlg->progressBar()->setValue(++i);
00267 #ifdef __GNUC__
00268 #warning Port me!
00269 #endif
00270         //kapp->eventLoop()->processEvents(QEventLoop::ExcludeSocketNotifiers);
00271       }
00272       delete dlg;
00273     }
00274     return mMails;
00275   }
00276   else
00277     return QMimeData::retrieveData( mimeType, type );
00278 }

libkdepim

Skip menu "libkdepim"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdepim

Skip menu "kdepim"
  • akonadi
  •   clients
  •   kabc
  •   kcal
  •   kcm
  • akregator
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt
  • kdgantt1
  • kjots
  • kleopatra
  • kmail
  • kmobiletools
  • knode
  • knotes
  • kontact
  • kontactinterfaces
  • korganizer
  •   korgac
  • kpilot
  • ktimetracker
  •   doc
  • libkdepim
  • libkholidays
  • libkleo
  • libkpgp
  • maildir
Generated for kdepim by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal