1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
|
/*
* jabberfiletransfer.cpp
*
* Copyright (c) 2004 by Till Gerken <till@tantalo.net>
*
* Kopete (c) by the Kopete developers <kopete-devel@kde.org>
*
* *************************************************************************
* * *
* * This program is free software; you can redistribute it and/or modify *
* * it under the terms of the GNU General Public License as published by *
* * the Free Software Foundation; either version 2 of the License, or *
* * (at your option) any later version. *
* * *
* *************************************************************************
*/
#include <kdebug.h>
#include <im.h>
#include <xmpp.h>
#include "jabberfiletransfer.h"
#include <klocale.h>
#include <kmessagebox.h>
#include <kconfig.h>
#include "kopeteuiglobal.h"
#include "kopetemetacontact.h"
#include "kopetecontactlist.h"
#include "kopetetransfermanager.h"
#include "jabberaccount.h"
#include "jabberprotocol.h"
#include "jabberclient.h"
#include "jabbercontactpool.h"
#include "jabberbasecontact.h"
#include "jabbercontact.h"
JabberFileTransfer::JabberFileTransfer ( JabberAccount *account, XMPP::FileTransfer *incomingTransfer )
{
kdDebug(JABBER_DEBUG_GLOBAL) << k_funcinfo << "New incoming transfer from " << incomingTransfer->peer().full () << ", filename " << incomingTransfer->fileName () << ", size " << TQString::number ( incomingTransfer->fileSize () ) << endl;
mAccount = account;
mXMPPTransfer = incomingTransfer;
// try to locate an exact match in our pool first
JabberBaseContact *contact = mAccount->contactPool()->findExactMatch ( mXMPPTransfer->peer () );
if ( !contact )
{
// we have no exact match, try a broader search
contact = mAccount->contactPool()->findRelevantRecipient ( mXMPPTransfer->peer () );
}
if ( !contact )
{
kdDebug(JABBER_DEBUG_GLOBAL) << k_funcinfo << "No matching local contact found, creating a new one." << endl;
Kopete::MetaContact *metaContact = new Kopete::MetaContact ();
metaContact->setTemporary (true);
contact = mAccount->contactPool()->addContact ( mXMPPTransfer->peer (), metaContact, false );
Kopete::ContactList::self ()->addMetaContact ( metaContact );
}
connect ( Kopete::TransferManager::transferManager (), TQT_SIGNAL ( accepted ( Kopete::Transfer *, const TQString & ) ),
this, TQT_SLOT ( slotIncomingTransferAccepted ( Kopete::Transfer *, const TQString & ) ) );
connect ( Kopete::TransferManager::transferManager (), TQT_SIGNAL ( refused ( const Kopete::FileTransferInfo & ) ),
this, TQT_SLOT ( slotTransferRefused ( const Kopete::FileTransferInfo & ) ) );
initializeVariables ();
mTransferId = Kopete::TransferManager::transferManager()->askIncomingTransfer ( contact,
mXMPPTransfer->fileName (),
mXMPPTransfer->fileSize (),
mXMPPTransfer->description () );
}
JabberFileTransfer::JabberFileTransfer ( JabberAccount *account, JabberBaseContact *contact, const TQString &file )
{
kdDebug(JABBER_DEBUG_GLOBAL) << k_funcinfo << "New outgoing transfer for " << contact->contactId() << ": " << file << endl;
mAccount = account;
mLocalFile.setName ( file );
mLocalFile.open ( IO_ReadOnly );
mKopeteTransfer = Kopete::TransferManager::transferManager()->addTransfer ( contact,
mLocalFile.name (),
mLocalFile.size (),
contact->contactId (),
Kopete::FileTransferInfo::Outgoing );
connect ( mKopeteTransfer, TQT_SIGNAL ( result ( KIO::Job * ) ), this, TQT_SLOT ( slotTransferResult () ) );
mXMPPTransfer = mAccount->client()->fileTransferManager()->createTransfer ();
initializeVariables ();
connect ( mXMPPTransfer, TQT_SIGNAL ( connected () ), this, TQT_SLOT ( slotOutgoingConnected () ) );
connect ( mXMPPTransfer, TQT_SIGNAL ( bytesWritten ( int ) ), this, TQT_SLOT ( slotOutgoingBytesWritten ( int ) ) );
connect ( mXMPPTransfer, TQT_SIGNAL ( error ( int ) ), this, TQT_SLOT ( slotTransferError ( int ) ) );
mXMPPTransfer->sendFile ( XMPP::Jid ( contact->fullAddress () ), KURL(file).fileName (), mLocalFile.size (), "" );
}
JabberFileTransfer::~JabberFileTransfer ()
{
kdDebug(JABBER_DEBUG_GLOBAL) << k_funcinfo << "Destroying Jabber file transfer object." << endl;
mLocalFile.close ();
mXMPPTransfer->close ();
delete mXMPPTransfer;
}
void JabberFileTransfer::initializeVariables ()
{
mTransferId = -1;
mBytesTransferred = 0;
mBytesToTransfer = 0;
mXMPPTransfer->setProxy ( XMPP::Jid ( mAccount->configGroup()->readEntry ( "ProxyJID" ) ) );
}
void JabberFileTransfer::slotIncomingTransferAccepted ( Kopete::Transfer *transfer, const TQString &fileName )
{
if ( (long)transfer->info().transferId () != mTransferId )
return;
kdDebug(JABBER_DEBUG_GLOBAL) << k_funcinfo << "Accepting transfer for " << mXMPPTransfer->peer().full () << endl;
mKopeteTransfer = transfer;
mLocalFile.setName ( fileName );
bool couldOpen = false;
TQ_LLONG offset = 0;
TQ_LLONG length = 0;
mBytesTransferred = 0;
mBytesToTransfer = mXMPPTransfer->fileSize ();
if ( mXMPPTransfer->rangeSupported () && mLocalFile.exists () )
{
KGuiItem resumeButton ( i18n ( "&Resume" ) );
KGuiItem overwriteButton ( i18n ( "Over&write" ) );
switch ( KMessageBox::questionYesNoCancel ( Kopete::UI::Global::mainWidget (),
i18n ( "The file %1 already exists, do you want to resume or overwrite it?" ).arg ( fileName ),
i18n ( "File Exists: %1" ).arg ( fileName ),
resumeButton, overwriteButton ) )
{
case KMessageBox::Yes: // resume
couldOpen = mLocalFile.open ( IO_ReadWrite );
if ( couldOpen )
{
offset = mLocalFile.size ();
length = mXMPPTransfer->fileSize () - offset;
mBytesTransferred = offset;
mBytesToTransfer = length;
mLocalFile.at ( mLocalFile.size () );
}
break;
case KMessageBox::No: // overwrite
couldOpen = mLocalFile.open ( IO_WriteOnly );
break;
default: // cancel
deleteLater ();
return;
}
}
else
{
// overwrite by default
couldOpen = mLocalFile.open ( IO_WriteOnly );
}
if ( !couldOpen )
{
transfer->slotError ( KIO::ERR_COULD_NOT_WRITE, fileName );
deleteLater ();
}
else
{
connect ( mKopeteTransfer, TQT_SIGNAL ( result ( KIO::Job * ) ), this, TQT_SLOT ( slotTransferResult () ) );
connect ( mXMPPTransfer, TQT_SIGNAL ( readyRead ( const TQByteArray& ) ), this, TQT_SLOT ( slotIncomingDataReady ( const TQByteArray & ) ) );
connect ( mXMPPTransfer, TQT_SIGNAL ( error ( int ) ), this, TQT_SLOT ( slotTransferError ( int ) ) );
mXMPPTransfer->accept ( offset, length );
}
}
void JabberFileTransfer::slotTransferRefused ( const Kopete::FileTransferInfo &transfer )
{
if ( (long)transfer.transferId () != mTransferId )
return;
kdDebug(JABBER_DEBUG_GLOBAL) << k_funcinfo << "Local user refused transfer from " << mXMPPTransfer->peer().full () << endl;
deleteLater ();
}
void JabberFileTransfer::slotTransferResult ()
{
if ( mKopeteTransfer->error () == KIO::ERR_USER_CANCELED )
{
kdDebug(JABBER_DEBUG_GLOBAL) << k_funcinfo << "Transfer with " << mXMPPTransfer->peer().full () << " has been canceled." << endl;
mXMPPTransfer->close ();
deleteLater ();
}
}
void JabberFileTransfer::slotTransferError ( int errorCode )
{
switch ( errorCode )
{
case XMPP::FileTransfer::ErrReject:
// user rejected the transfer request
mKopeteTransfer->slotError ( KIO::ERR_ACCESS_DENIED,
mXMPPTransfer->peer().full () );
break;
case XMPP::FileTransfer::ErrNeg:
// unable to negotiate a suitable connection for the file transfer with the user
mKopeteTransfer->slotError ( KIO::ERR_COULD_NOT_LOGIN,
mXMPPTransfer->peer().full () );
break;
case XMPP::FileTransfer::ErrConnect:
// could not connect to the user
mKopeteTransfer->slotError ( KIO::ERR_COULD_NOT_CONNECT,
mXMPPTransfer->peer().full () );
break;
case XMPP::FileTransfer::ErrStream:
// data stream was disrupted, probably cancelled
mKopeteTransfer->slotError ( KIO::ERR_CONNECTION_BROKEN,
mXMPPTransfer->peer().full () );
break;
default:
// unknown error
mKopeteTransfer->slotError ( KIO::ERR_UNKNOWN,
mXMPPTransfer->peer().full () );
break;
}
deleteLater ();
}
void JabberFileTransfer::slotIncomingDataReady ( const TQByteArray &data )
{
mBytesTransferred += data.size ();
mBytesToTransfer -= data.size ();
mKopeteTransfer->slotProcessed ( mBytesTransferred );
mLocalFile.writeBlock ( data );
if ( mBytesToTransfer <= 0 )
{
kdDebug(JABBER_DEBUG_GLOBAL) << k_funcinfo << "Transfer from " << mXMPPTransfer->peer().full () << " done." << endl;
mKopeteTransfer->slotComplete ();
deleteLater ();
}
}
void JabberFileTransfer::slotOutgoingConnected ()
{
kdDebug ( JABBER_DEBUG_GLOBAL ) << k_funcinfo << "Outgoing data connection is open." << endl;
mBytesTransferred = mXMPPTransfer->offset ();
mLocalFile.at ( mXMPPTransfer->offset () );
mBytesToTransfer = ( mXMPPTransfer->fileSize () > mXMPPTransfer->length () ) ? mXMPPTransfer->length () : mXMPPTransfer->fileSize ();
slotOutgoingBytesWritten ( 0 );
}
void JabberFileTransfer::slotOutgoingBytesWritten ( int nrWritten )
{
mBytesTransferred += nrWritten;
mBytesToTransfer -= nrWritten;
mKopeteTransfer->slotProcessed ( mBytesTransferred );
if ( mBytesToTransfer )
{
int nrToWrite = mXMPPTransfer->dataSizeNeeded ();
TQByteArray readBuffer ( nrToWrite );
mLocalFile.readBlock ( readBuffer.data (), nrToWrite );
mXMPPTransfer->writeFileData ( readBuffer );
}
else
{
kdDebug(JABBER_DEBUG_GLOBAL) << k_funcinfo << "Transfer to " << mXMPPTransfer->peer().full () << " done." << endl;
mKopeteTransfer->slotComplete ();
deleteLater ();
}
}
#include "jabberfiletransfer.moc"
|