~ubuntu-branches/ubuntu/saucy/kopete/saucy-proposed

« back to all changes in this revision

Viewing changes to protocols/yahoo/libkyahoo/ymsgtransfer.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-06-21 02:22:39 UTC
  • Revision ID: package-import@ubuntu.com-20130621022239-63l3zc8p0nf26pt6
Tags: upstream-4.10.80
Import upstream version 4.10.80

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Kopete Yahoo Protocol
 
3
    Handles logging into to the Yahoo service
 
4
 
 
5
    Copyright (c) 2004 Duncan Mac-Vicar P. <duncan@kde.org>
 
6
 
 
7
    Copyright (c) 2005 André Duffeck <duffeck@kde.org>
 
8
 
 
9
    Kopete (c) 2002-2005 by the Kopete developers <kopete-devel@kde.org>
 
10
 
 
11
    *************************************************************************
 
12
    *                                                                       *
 
13
    * This library is free software; you can redistribute it and/or         *
 
14
    * modify it under the terms of the GNU Lesser General Public            *
 
15
    * License as published by the Free Software Foundation; either          *
 
16
    * version 2 of the License, or (at your option) any later version.      *
 
17
    *                                                                       *
 
18
    *************************************************************************
 
19
*/
 
20
 
 
21
#include <string>
 
22
 
 
23
#include "ymsgtransfer.h"
 
24
#include "yahootypes.h"
 
25
#include "kdebug.h"
 
26
 
 
27
#include <qdatastream.h>
 
28
#include <qmap.h>
 
29
#include <qstring.h>
 
30
#include <qstringlist.h>
 
31
 
 
32
 
 
33
using namespace Yahoo;
 
34
 
 
35
class YMSGTransferPrivate
 
36
{
 
37
public:
 
38
        int yflag;
 
39
        int version;
 
40
        int packetLength;
 
41
        Yahoo::Service service;
 
42
        Yahoo::Status status;
 
43
        unsigned int id;        
 
44
        ParamList data;
 
45
        bool valid;
 
46
};
 
47
 
 
48
YMSGTransfer::YMSGTransfer()
 
49
{
 
50
        d = new YMSGTransferPrivate;
 
51
        d->valid = true;
 
52
        d->id = 0;
 
53
        d-> status = Yahoo::StatusAvailable;
 
54
}
 
55
 
 
56
YMSGTransfer::YMSGTransfer(Yahoo::Service service)
 
57
{
 
58
        d = new YMSGTransferPrivate;
 
59
        d->valid = true;
 
60
        d->service = service;
 
61
        d->id = 0;
 
62
        d->status = Yahoo::StatusAvailable;
 
63
}
 
64
 
 
65
YMSGTransfer::YMSGTransfer(Yahoo::Service service, Yahoo::Status status)
 
66
{
 
67
        d = new YMSGTransferPrivate;
 
68
        d->valid = true;
 
69
        d->service = service;
 
70
        d->id = 0;
 
71
        d->status = status;
 
72
}
 
73
 
 
74
YMSGTransfer::~YMSGTransfer()
 
75
{
 
76
        delete d;
 
77
}
 
78
 
 
79
Transfer::TransferType YMSGTransfer::type()
 
80
{
 
81
        return Transfer::YMSGTransfer;
 
82
}
 
83
 
 
84
bool YMSGTransfer::isValid() const
 
85
{
 
86
        return d->valid;
 
87
}
 
88
 
 
89
Yahoo::Service YMSGTransfer::service() const
 
90
{
 
91
        return d->service;
 
92
}
 
93
 
 
94
void YMSGTransfer::setService(Yahoo::Service service)
 
95
{
 
96
        d->service = service;
 
97
}
 
98
 
 
99
Yahoo::Status YMSGTransfer::status() const
 
100
{
 
101
        return d->status;
 
102
}
 
103
 
 
104
void YMSGTransfer::setStatus(Yahoo::Status status)
 
105
{
 
106
        d->status = status;
 
107
}
 
108
 
 
109
unsigned int YMSGTransfer::id() const
 
110
{
 
111
        return d->id;
 
112
}
 
113
 
 
114
void YMSGTransfer::setId(unsigned int id)
 
115
{
 
116
        d->id = id;
 
117
}
 
118
 
 
119
int YMSGTransfer::packetLength() const
 
120
{
 
121
        return d->packetLength;
 
122
}
 
123
 
 
124
void YMSGTransfer::setPacketLength(int len)
 
125
{
 
126
        d->packetLength = len;
 
127
}
 
128
 
 
129
ParamList YMSGTransfer::paramList() const
 
130
{
 
131
        return d->data;
 
132
}
 
133
 
 
134
int YMSGTransfer::paramCount( int index ) const
 
135
{
 
136
        int cnt = 0;
 
137
        for (ParamList::ConstIterator it = d->data.constBegin(); it !=  d->data.constEnd(); ++it) 
 
138
        {
 
139
                if( (*it).first == index )
 
140
                        cnt++;
 
141
        }
 
142
        return cnt;
 
143
}
 
144
 
 
145
 
 
146
QByteArray YMSGTransfer::nthParam( int index, int occurrence ) const
 
147
{
 
148
        int cnt = 0;
 
149
        for (ParamList::ConstIterator it = d->data.constBegin(); it !=  d->data.constEnd(); ++it) 
 
150
        {
 
151
                if( (*it).first == index && cnt++ == occurrence)
 
152
                        return (*it).second;
 
153
        }
 
154
        return QByteArray();
 
155
}
 
156
 
 
157
QByteArray YMSGTransfer::nthParamSeparated( int index, int occurrence, int separator ) const
 
158
{
 
159
 
 
160
        int cnt = -1;
 
161
        for (ParamList::ConstIterator it = d->data.constBegin(); it !=  d->data.constEnd(); ++it) 
 
162
        {
 
163
                if( (*it).first == separator )
 
164
                        cnt++;
 
165
                if( (*it).first == index && cnt == occurrence)
 
166
                        return (*it).second;
 
167
        }
 
168
        return QByteArray();
 
169
}
 
170
 
 
171
QByteArray YMSGTransfer::firstParam( int index ) const
 
172
{
 
173
        for (ParamList::ConstIterator it = d->data.constBegin(); it !=  d->data.constEnd(); ++it) 
 
174
        {
 
175
                if( (*it).first == index )
 
176
                        return (*it).second;
 
177
        }
 
178
        return QByteArray();
 
179
}
 
180
 
 
181
void YMSGTransfer::setParam(int index, const QByteArray &data)
 
182
{
 
183
        d->data.append( Param( index, data ) );
 
184
}
 
185
 
 
186
void YMSGTransfer::setParam( int index, int data )
 
187
{
 
188
        d->data.append( Param( index, QString::number( data ).toLocal8Bit() ) );
 
189
}
 
190
 
 
191
int YMSGTransfer::length() const
 
192
{
 
193
        int len = 0;
 
194
        for (ParamList::ConstIterator it = d->data.constBegin(); it !=  d->data.constEnd(); ++it) 
 
195
        {
 
196
                len += QString::number( (*it).first ).length();
 
197
                len += 2;
 
198
                len += (*it).second.length();
 
199
                len += 2;
 
200
        }
 
201
        return len;
 
202
}
 
203
 
 
204
 
 
205
QByteArray YMSGTransfer::serialize() const
 
206
{
 
207
        /*
 
208
        <------- 4B -------><------- 4B -------><---2B--->
 
209
        +-------------------+-------------------+---------+
 
210
        |   Y   M   S   G   |      version      | pkt_len |
 
211
        +---------+---------+---------+---------+---------+
 
212
        | service |      status       |    session_id     |
 
213
        +---------+-------------------+-------------------+
 
214
        |                                                 |
 
215
        :                    D A T A                      :
 
216
        /                   0 - 65535*                   |
 
217
        +-------------------------------------------------+
 
218
        */
 
219
        
 
220
        int pos = 0;
 
221
        QByteArray buffer;
 
222
        QDataStream stream( &buffer, QIODevice::WriteOnly );
 
223
        
 
224
        stream << (qint8)'Y' << (qint8)'M' << (qint8)'S' << (qint8)'G';
 
225
        if( d->service == Yahoo::ServicePictureUpload )
 
226
                stream << (qint16)0x0f00;
 
227
        else
 
228
                stream << (qint16)0x000f;
 
229
        stream << (qint16)0x0000;
 
230
        if( d->service == Yahoo::ServicePictureUpload ||
 
231
                d->service == Yahoo::ServiceFileTransfer )
 
232
                stream << (qint16)(length()+4);
 
233
        else
 
234
                stream << (qint16)length();
 
235
        stream << (qint16)d->service;
 
236
        stream << (qint32)d->status;
 
237
        stream << (qint32)d->id;
 
238
        for (ParamList::ConstIterator it = d->data.constBegin(); it !=  d->data.constEnd(); ++it) 
 
239
        {
 
240
                kDebug(YAHOO_RAW_DEBUG) << " Serializing key " << (*it).first << " value " << (*it).second;
 
241
                stream.writeRawData ( QString::number( (*it).first ).toLocal8Bit(), QString::number( (*it).first ).length() );
 
242
                stream << (qint8)0xc0 << (qint8)0x80;
 
243
                stream.writeRawData( (*it).second, (*it).second.length() );
 
244
                stream << (qint8)0xc0 << (qint8)0x80;
 
245
        }
 
246
        kDebug(YAHOO_RAW_DEBUG) << " pos=" << pos << " (packet size)" << buffer;
 
247
        return buffer;
 
248
}
 
249