libtdepim

maillistdrag.cpp
1/*
2 This file is part of libtdepim.
3
4 Copyright (c) 2003 Don Sanders <sanders@kde.org>
5 Copyright (c) 2005 George Staikos <staikos@kde.org>
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
16
17 You should have received a copy of the GNU Library General Public License
18 along with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
21*/
22
23#include "maillistdrag.h"
24#include <tqbuffer.h>
25#include <tqdatastream.h>
26#include <tqeventloop.h>
27#include <tdeapplication.h>
28#include <tdelocale.h>
29#include <kprogress.h>
30
31using namespace KPIM;
32
33MailSummary::MailSummary( TQ_UINT32 serialNumber, TQString messageId,
34 TQString subject, TQString from, TQString to,
35 time_t date )
36 : mSerialNumber( serialNumber ), mMessageId( messageId ),
37 mSubject( subject ), mFrom( from ), mTo( to ), mDate( date )
38{}
39
40TQ_UINT32 MailSummary::serialNumber() const
41{
42 return mSerialNumber;
43}
44
45TQString MailSummary::messageId()
46{
47 return mMessageId;
48}
49
50TQString MailSummary::subject()
51{
52 return mSubject;
53}
54
55TQString MailSummary::from()
56{
57 return mFrom;
58}
59
60TQString MailSummary::to()
61{
62 return mTo;
63}
64
65time_t MailSummary::date()
66{
67 return mDate;
68}
69
70void MailSummary::set( TQ_UINT32 serialNumber, TQString messageId,
71 TQString subject, TQString from, TQString to, time_t date )
72{
73 mSerialNumber = serialNumber;
74 mMessageId = messageId;
75 mSubject = subject;
76 mFrom = from;
77 mTo = to;
78 mDate = date;
79}
80
81MailListDrag::MailListDrag( MailList mailList, TQWidget * parent, MailTextSource *src )
82 : TQStoredDrag( MailListDrag::format(), parent ), _src(src)
83{
84 setMailList( mailList );
85}
86
87MailListDrag::~MailListDrag()
88{
89 delete _src;
90 _src = 0;
91}
92
93const char* MailListDrag::format()
94{
95 return "x-kmail-drag/message-list";
96}
97
98bool MailListDrag::canDecode( TQMimeSource *e )
99{
100 return e->provides( MailListDrag::format() );
101}
102
103// Have to define before use
104TQDataStream& operator<< ( TQDataStream &s, MailSummary &d )
105{
106 s << d.serialNumber();
107 s << d.messageId();
108 s << d.subject();
109 s << d.from();
110 s << d.to();
111 s << d.date();
112 return s;
113}
114
115TQDataStream& operator>> ( TQDataStream &s, MailSummary &d )
116{
117 TQ_UINT32 serialNumber;
118 TQString messageId, subject, from, to;
119 time_t date;
120 s >> serialNumber;
121 s >> messageId;
122 s >> subject;
123 s >> from;
124 s >> to;
125 s >> date;
126 d.set( serialNumber, messageId, subject, from, to, date );
127 return s;
128}
129
130TQDataStream& operator<< ( TQDataStream &s, MailList &mailList )
131{
132 MailList::iterator it;
133 for (it = mailList.begin(); it != mailList.end(); ++it) {
134 MailSummary mailDrag = *it;
135 s << mailDrag;
136 }
137 return s;
138}
139
140TQDataStream& operator>> ( TQDataStream &s, MailList &mailList )
141{
142 mailList.clear();
143 MailSummary mailDrag;
144 while (!s.atEnd()) {
145 s >> mailDrag;
146 mailList.append( mailDrag );
147 }
148 return s;
149}
150
151bool MailListDrag::decode( TQDropEvent* e, MailList& mailList )
152{
153 TQByteArray payload = e->encodedData( MailListDrag::format() );
154 TQDataStream buffer( payload, IO_ReadOnly );
155 if ( payload.size() ) {
156 e->accept();
157 buffer >> mailList;
158 return TRUE;
159 }
160 return FALSE;
161}
162
163bool MailListDrag::decode( TQByteArray& payload, MailList& mailList )
164{
165 TQDataStream stream( payload, IO_ReadOnly );
166 if ( payload.size() ) {
167 stream >> mailList;
168 return TRUE;
169 }
170 return FALSE;
171}
172
173bool MailListDrag::decode( TQDropEvent* e, TQByteArray &a )
174{
175 MailList mailList;
176 if (decode( e, mailList )) {
177 MailList::iterator it;
178 TQBuffer buffer( a );
179 buffer.open( IO_WriteOnly );
180 TQDataStream stream( &buffer );
181 for (it = mailList.begin(); it != mailList.end(); ++it) {
182 MailSummary mailDrag = *it;
183 stream << mailDrag.serialNumber();
184 }
185 buffer.close();
186 return TRUE;
187 }
188 return FALSE;
189}
190
191void MailListDrag::setMailList( MailList mailList )
192{
193 TQByteArray array;
194 TQBuffer buffer( array );
195 buffer.open( IO_WriteOnly);
196 TQDataStream stream( array, IO_WriteOnly );
197 stream << mailList;
198 buffer.close();
199 setEncodedData( array );
200}
201
202const char *MailListDrag::format(int i) const
203{
204 if (_src) {
205 if (i == 0) {
206 return "message/rfc822";
207 } else {
208 return TQStoredDrag::format(i - 1);
209 }
210 }
211
212 return TQStoredDrag::format(i);
213}
214
215bool MailListDrag::provides(const char *mimeType) const
216{
217 if (_src && TQCString(mimeType) == "message/rfc822") {
218 return true;
219 }
220
221 return TQStoredDrag::provides(mimeType);
222}
223
224TQByteArray MailListDrag::encodedData(const char *mimeType) const
225{
226 if (TQCString(mimeType) != "message/rfc822") {
227 return TQStoredDrag::encodedData(mimeType);
228 }
229
230 TQByteArray rc;
231 if (_src) {
232 MailList ml;
233 TQByteArray enc = TQStoredDrag::encodedData(format());
234 decode(enc, ml);
235
236 KProgressDialog *dlg = new KProgressDialog(0, 0, TQString(), i18n("Retrieving and storing messages..."), true);
237 dlg->setAllowCancel(true);
238 dlg->progressBar()->setTotalSteps(ml.count());
239 int i = 0;
240 dlg->progressBar()->setValue(i);
241 dlg->show();
242
243 TQTextStream *ts = new TQTextStream(rc, IO_WriteOnly);
244 for (MailList::ConstIterator it = ml.begin(); it != ml.end(); ++it) {
245 MailSummary mailDrag = *it;
246 *ts << _src->text(mailDrag.serialNumber());
247 if (dlg->wasCancelled()) {
248 break;
249 }
250 dlg->progressBar()->setValue(++i);
251 tdeApp->eventLoop()->processEvents(TQEventLoop::ExcludeSocketNotifiers);
252 }
253
254 delete dlg;
255 delete ts;
256 }
257 return rc;
258}
259
TDEPIM classes for drag and drop of mails.