summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/yahoo/libkyahoo/filetransfernotifiertask.cpp
blob: f48bd52d09c6ecec94a822e6598edb5893b472a8 (plain)
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
/*
    Kopete Yahoo Protocol
    Notifies about incoming filetransfers

    Copyright (c) 2006 AndrĂ© Duffeck <[email protected]>

    *************************************************************************
    *                                                                       *
    * This library is free software; you can redistribute it and/or         *
    * modify it under the terms of the GNU Lesser General Public            *
    * License as published by the Free Software Foundation; either          *
    * version 2 of the License, or (at your option) any later version.      *
    *                                                                       *
    *************************************************************************
*/

#include "filetransfernotifiertask.h"
#include "transfer.h"
#include "ymsgtransfer.h"
#include "yahootypes.h"
#include "client.h"

#include <tqstring.h>
#include <tqpixmap.h>
#include <kdebug.h>
//#include <kcodecs.h>

FileTransferNotifierTask::FileTransferNotifierTask(Task* tqparent) : Task(tqparent)
{
	kdDebug(YAHOO_RAW_DEBUG) ;
}

FileTransferNotifierTask::~FileTransferNotifierTask()
{

}

bool FileTransferNotifierTask::take( Transfer* transfer )
{
	if ( !forMe( transfer ) )
		return false;

	YMSGTransfer *t = static_cast<YMSGTransfer*>(transfer);

	if( t->service() == Yahoo::ServiceFileTransfer )
		parseFileTransfer( t );
	else if( t->service() == Yahoo::ServiceFileTransfer7 )
		parseFileTransfer7( t );
	else if( t->service() == Yahoo::ServicePeerToPeer )
		acceptFileTransfer( t );


	return true;
}

bool FileTransferNotifierTask::forMe( const Transfer *transfer ) const
{
	const YMSGTransfer *t = 0L;
	t = dynamic_cast<const YMSGTransfer*>(transfer);
	if (!t)
		return false;


	if( t->service() == Yahoo::ServiceP2PFileXfer ||
	    t->service() == Yahoo::ServicePeerToPeer ||
	    t->service() == Yahoo::ServiceFileTransfer ||
	    (t->service() == Yahoo::ServiceFileTransfer7 &&
	     t->firstParam(222).toInt() == 1)
	)
		return true;
	else
		return false;
}

void FileTransferNotifierTask::parseFileTransfer( YMSGTransfer *t )
{
	kdDebug(YAHOO_RAW_DEBUG) ;

	TQString from;		/* key = 4  */
	TQString to;		/* key = 5  */
	TQString url;		/* key = 20  */
	long expires;		/* key = 38  */
	TQString msg;		/* key = 14  */
	TQString filename;	/* key = 27  */
	unsigned long size;	/* key = 28  */

	from = t->firstParam( 4 );
	to = t->firstParam( 5 );
	url = t->firstParam( 20 );
	expires = t->firstParam( 38 ).toLong();
	msg = t->firstParam( 14 );
	filename = t->firstParam( 27 );
	size = t->firstParam( 28 ).toULong();



	if( from.startsWith( "FILE_TRANSFER_SYSTEM" ) )
	{
		client()->notifyError( "Fileupload result received.", msg, Client::Notice );
		return;
	}

	if( url.isEmpty() )
		return;


	unsigned int left = url.findRev( '/' ) + 1;
        unsigned int right = url.findRev( '?' );
	filename = url.mid( left, right - left );

	emit incomingFileTransfer( from, url, expires, msg, filename, size, TQPixmap() );
}

void FileTransferNotifierTask::parseFileTransfer7( YMSGTransfer *t )
{
	kdDebug(YAHOO_RAW_DEBUG) ;

	TQString from;		/* key = 4  */
	TQString to;		/* key = 5  */
	TQString url;		/* key = 20  */
	long expires;		/* key = 38  */
	TQString msg;		/* key = 14  */
	TQString filename;	/* key = 27  */
	unsigned long size;	/* key = 28  */
	TQByteArray preview;	/* key = 267 */
	TQPixmap previewPixmap;

	if( t->firstParam( 222 ).toInt() == 2 )
		return;					// user cancelled the file transfer

	from = t->firstParam( 4 );
	to = t->firstParam( 5 );
	url = t->firstParam( 265 );
	msg = t->firstParam( 14 );
	expires = t->firstParam( 38 ).toLong();
	filename = t->firstParam( 27 );
	size = t->firstParam( 28 ).toULong();

	// FIXME (same)
	//preview = TQByteArray::fromBase64( t->firstParam( 267 ) );

	if( preview.size() > 0 )
	{
		previewPixmap.loadFromData( preview );
	}

	emit incomingFileTransfer( from, url, expires, msg, filename, size, previewPixmap );
}

void FileTransferNotifierTask::acceptFileTransfer( YMSGTransfer *transfer )
{
	kdDebug(YAHOO_RAW_DEBUG) ;

	YMSGTransfer *t = new YMSGTransfer(Yahoo::ServicePeerToPeer);
	t->setId( client()->sessionID() );
	t->setParam( 4, client()->userId().local8Bit() );
	t->setParam( 5, transfer->firstParam( 4 ) );
	t->setParam( 11, transfer->firstParam( 11 ) );

	send( t );
}

#include "filetransfernotifiertask.moc"