Mercurial > pidgin
comparison libpurple/protocols/mxit/filexfer.c @ 32672:3828a61c44da
A boring and large patch so I can merge heads.
author | Elliott Sales de Andrade <qulogic@pidgin.im> |
---|---|
date | Fri, 23 Dec 2011 08:21:58 +0000 |
parents | 259bbfb423d4 |
children |
comparison
equal
deleted
inserted
replaced
32671:0e69949b3e61 | 32672:3828a61c44da |
---|---|
95 * | 95 * |
96 * @param xfer The file transfer object | 96 * @param xfer The file transfer object |
97 */ | 97 */ |
98 static void mxit_xfer_free( PurpleXfer* xfer ) | 98 static void mxit_xfer_free( PurpleXfer* xfer ) |
99 { | 99 { |
100 struct mxitxfer* mx = (struct mxitxfer*) xfer->data;; | 100 struct mxitxfer* mx = purple_xfer_get_protocol_data( xfer ); |
101 | 101 |
102 if ( mx ) { | 102 if ( mx ) { |
103 purple_xfer_set_protocol_data( xfer, NULL ); | |
103 g_free( mx ); | 104 g_free( mx ); |
104 xfer->data = NULL; | |
105 } | 105 } |
106 } | 106 } |
107 | 107 |
108 | 108 |
109 /*======================================================================================================================== | 109 /*======================================================================================================================== |
115 * | 115 * |
116 * @param xfer The file transfer object | 116 * @param xfer The file transfer object |
117 */ | 117 */ |
118 static void mxit_xfer_init( PurpleXfer* xfer ) | 118 static void mxit_xfer_init( PurpleXfer* xfer ) |
119 { | 119 { |
120 struct mxitxfer* mx = (struct mxitxfer*) xfer->data; | 120 struct mxitxfer* mx = purple_xfer_get_protocol_data( xfer ); |
121 | 121 |
122 purple_debug_info( MXIT_PLUGIN_ID, "mxit_xfer_init\n" ); | 122 purple_debug_info( MXIT_PLUGIN_ID, "mxit_xfer_init\n" ); |
123 | 123 |
124 if ( purple_xfer_get_type( xfer ) == PURPLE_XFER_SEND ) { | 124 if ( purple_xfer_get_type( xfer ) == PURPLE_XFER_SEND ) { |
125 /* we are trying to send a file to MXit */ | 125 /* we are trying to send a file to MXit */ |
126 | 126 |
127 if ( purple_xfer_get_size( xfer ) > CP_MAX_FILESIZE ) { | 127 if ( purple_xfer_get_size( xfer ) > ( CP_MAX_PACKET - 1000 ) ) { /* need to reserve some space for packet headers */ |
128 /* the file is too big */ | 128 /* the file is too big */ |
129 purple_xfer_error( xfer->type, xfer->account, xfer->who, _( "The file you are trying to send is too large!" ) ); | 129 purple_xfer_error( purple_xfer_get_type( xfer ), purple_xfer_get_account( xfer ), purple_xfer_get_remote_user( xfer ), _( "The file you are trying to send is too large!" ) ); |
130 purple_xfer_cancel_local( xfer ); | 130 purple_xfer_cancel_local( xfer ); |
131 return; | 131 return; |
132 } | 132 } |
133 | 133 |
134 /* start the file transfer */ | 134 /* start the file transfer */ |
137 else { | 137 else { |
138 /* | 138 /* |
139 * we have just accepted a file transfer request from MXit. send a confirmation | 139 * we have just accepted a file transfer request from MXit. send a confirmation |
140 * to the MXit server so that can send us the file | 140 * to the MXit server so that can send us the file |
141 */ | 141 */ |
142 mxit_send_file_accept( mx->session, mx->fileid, purple_xfer_get_size( xfer ), 0 ); | 142 mxit_send_file_accept( mx->session, mx->fileid, (int) purple_xfer_get_size( xfer ), 0 ); |
143 } | 143 } |
144 } | 144 } |
145 | 145 |
146 | 146 |
147 /*------------------------------------------------------------------------ | 147 /*------------------------------------------------------------------------ |
149 * | 149 * |
150 * @param xfer The file transfer object | 150 * @param xfer The file transfer object |
151 */ | 151 */ |
152 static void mxit_xfer_start( PurpleXfer* xfer ) | 152 static void mxit_xfer_start( PurpleXfer* xfer ) |
153 { | 153 { |
154 goffset filesize; | |
154 unsigned char* buffer; | 155 unsigned char* buffer; |
155 int size; | 156 int size; |
156 int wrote; | 157 int wrote; |
157 | 158 |
158 purple_debug_info( MXIT_PLUGIN_ID, "mxit_xfer_start\n" ); | 159 purple_debug_info( MXIT_PLUGIN_ID, "mxit_xfer_start\n" ); |
161 /* | 162 /* |
162 * the user wants to send a file to one of his contacts. we need to create | 163 * the user wants to send a file to one of his contacts. we need to create |
163 * a buffer and copy the file data into memory and then we can send it to | 164 * a buffer and copy the file data into memory and then we can send it to |
164 * the contact. we will send the whole file with one go. | 165 * the contact. we will send the whole file with one go. |
165 */ | 166 */ |
166 buffer = g_malloc( xfer->bytes_remaining ); | 167 filesize = purple_xfer_get_bytes_remaining( xfer ); |
167 size = fread( buffer, xfer->bytes_remaining, 1, xfer->dest_fp ); | 168 buffer = g_malloc( filesize ); |
168 | 169 size = fread( buffer, filesize, 1, xfer->dest_fp ); |
169 wrote = purple_xfer_write( xfer, buffer, xfer->bytes_remaining ); | 170 // TODO: If (size != 1) -> file read error |
171 | |
172 wrote = purple_xfer_write( xfer, buffer, filesize ); | |
170 if ( wrote > 0 ) | 173 if ( wrote > 0 ) |
171 purple_xfer_set_bytes_sent( xfer, wrote ); | 174 purple_xfer_set_bytes_sent( xfer, wrote ); |
172 | 175 |
173 /* free the buffer */ | 176 /* free the buffer */ |
174 g_free( buffer ); | 177 g_free( buffer ); |
213 * @param xfer The file transfer object | 216 * @param xfer The file transfer object |
214 * @return The amount of data actually sent | 217 * @return The amount of data actually sent |
215 */ | 218 */ |
216 static gssize mxit_xfer_write( const guchar* buffer, size_t size, PurpleXfer* xfer ) | 219 static gssize mxit_xfer_write( const guchar* buffer, size_t size, PurpleXfer* xfer ) |
217 { | 220 { |
218 struct mxitxfer* mx = (struct mxitxfer*) xfer->data; | 221 struct mxitxfer* mx = purple_xfer_get_protocol_data( xfer ); |
219 | 222 |
220 purple_debug_info( MXIT_PLUGIN_ID, "mxit_xfer_write\n" ); | 223 purple_debug_info( MXIT_PLUGIN_ID, "mxit_xfer_write\n" ); |
221 | 224 |
222 if ( !mx ) { | 225 if ( !mx ) { |
223 purple_debug_warning( MXIT_PLUGIN_ID, "mxit_xfer_write: invalid internal mxit xfer data\n" ); | 226 purple_debug_warning( MXIT_PLUGIN_ID, "mxit_xfer_write: invalid internal mxit xfer data\n" ); |
243 * | 246 * |
244 * @param xfer The file transfer object | 247 * @param xfer The file transfer object |
245 */ | 248 */ |
246 static void mxit_xfer_request_denied( PurpleXfer* xfer ) | 249 static void mxit_xfer_request_denied( PurpleXfer* xfer ) |
247 { | 250 { |
248 struct mxitxfer* mx = (struct mxitxfer*) xfer->data; | 251 struct mxitxfer* mx = purple_xfer_get_protocol_data( xfer ); |
249 | 252 |
250 purple_debug_info( MXIT_PLUGIN_ID, "mxit_xfer_request_denied\n" ); | 253 purple_debug_info( MXIT_PLUGIN_ID, "mxit_xfer_request_denied\n" ); |
251 | 254 |
252 /* send file reject packet to MXit server */ | 255 /* send file reject packet to MXit server */ |
253 mxit_send_file_reject( mx->session, mx->fileid ); | 256 mxit_send_file_reject( mx->session, mx->fileid ); |
293 * @param gc The connection object | 296 * @param gc The connection object |
294 * @param who The username of the recipient | 297 * @param who The username of the recipient |
295 */ | 298 */ |
296 PurpleXfer* mxit_xfer_new( PurpleConnection* gc, const char* who ) | 299 PurpleXfer* mxit_xfer_new( PurpleConnection* gc, const char* who ) |
297 { | 300 { |
298 struct MXitSession* session = (struct MXitSession*) gc->proto_data; | 301 struct MXitSession* session = purple_connection_get_protocol_data( gc ); |
299 PurpleXfer* xfer = NULL; | 302 PurpleXfer* xfer = NULL; |
300 struct mxitxfer* mx = NULL; | 303 struct mxitxfer* mx = NULL; |
301 | 304 |
302 /* (reference: "libpurple/ft.h") */ | 305 /* (reference: "libpurple/ft.h") */ |
303 xfer = purple_xfer_new( session->acc, PURPLE_XFER_SEND, who ); | 306 xfer = purple_xfer_new( session->acc, PURPLE_XFER_SEND, who ); |
304 | 307 |
305 /* create file info and attach it to the file transfer */ | 308 /* create file info and attach it to the file transfer */ |
306 mx = g_new0( struct mxitxfer, 1 ); | 309 mx = g_new0( struct mxitxfer, 1 ); |
307 mx->session = session; | 310 mx->session = session; |
308 xfer->data = mx; | 311 purple_xfer_set_protocol_data( xfer, mx ); |
309 | 312 |
310 /* configure callbacks (reference: "libpurple/ft.h") */ | 313 /* configure callbacks (reference: "libpurple/ft.h") */ |
311 purple_xfer_set_init_fnc( xfer, mxit_xfer_init ); | 314 purple_xfer_set_init_fnc( xfer, mxit_xfer_init ); |
312 purple_xfer_set_start_fnc( xfer, mxit_xfer_start ); | 315 purple_xfer_set_start_fnc( xfer, mxit_xfer_start ); |
313 purple_xfer_set_end_fnc( xfer, mxit_xfer_end ); | 316 purple_xfer_set_end_fnc( xfer, mxit_xfer_end ); |
360 if ( xfer ) { | 363 if ( xfer ) { |
361 /* create a new mxit xfer struct for internal use */ | 364 /* create a new mxit xfer struct for internal use */ |
362 mx = g_new0( struct mxitxfer, 1 ); | 365 mx = g_new0( struct mxitxfer, 1 ); |
363 mx->session = session; | 366 mx->session = session; |
364 memcpy( mx->fileid, fileid, MXIT_CHUNK_FILEID_LEN ); | 367 memcpy( mx->fileid, fileid, MXIT_CHUNK_FILEID_LEN ); |
365 xfer->data = mx; | 368 purple_xfer_set_protocol_data( xfer, mx ); |
366 | 369 |
367 purple_xfer_set_filename( xfer, filename ); | 370 purple_xfer_set_filename( xfer, filename ); |
368 if( filesize > 0 ) | 371 if( filesize > 0 ) |
369 purple_xfer_set_size( xfer, filesize ); | 372 purple_xfer_set_size( xfer, filesize ); |
370 | 373 |
393 | 396 |
394 item = purple_xfers_get_all(); /* list of all active transfers */ | 397 item = purple_xfers_get_all(); /* list of all active transfers */ |
395 while ( item ) { | 398 while ( item ) { |
396 xfer = item->data; | 399 xfer = item->data; |
397 | 400 |
398 if ( xfer->account == session->acc ) { | 401 if ( purple_xfer_get_account( xfer ) == session->acc ) { |
399 /* transfer is associated with this MXit account */ | 402 /* transfer is associated with this MXit account */ |
400 struct mxitxfer* mx = xfer->data; | 403 struct mxitxfer* mx = purple_xfer_get_protocol_data( xfer ); |
401 | 404 |
402 /* does the fileid match? */ | 405 /* does the fileid match? */ |
403 if ( ( mx ) && ( memcmp( mx->fileid, fileid, MXIT_CHUNK_FILEID_LEN ) == 0 ) ) | 406 if ( ( mx ) && ( memcmp( mx->fileid, fileid, MXIT_CHUNK_FILEID_LEN ) == 0 ) ) |
404 break; | 407 break; |
405 } | 408 } |
422 * @param datalen The size of the data | 425 * @param datalen The size of the data |
423 */ | 426 */ |
424 void mxit_xfer_rx_file( struct MXitSession* session, const char* fileid, const char* data, int datalen ) | 427 void mxit_xfer_rx_file( struct MXitSession* session, const char* fileid, const char* data, int datalen ) |
425 { | 428 { |
426 PurpleXfer* xfer = NULL; | 429 PurpleXfer* xfer = NULL; |
427 struct mxitxfer* mx = NULL; | |
428 | 430 |
429 purple_debug_info( MXIT_PLUGIN_ID, "mxit_xfer_rx_file: (size=%i)\n", datalen ); | 431 purple_debug_info( MXIT_PLUGIN_ID, "mxit_xfer_rx_file: (size=%i)\n", datalen ); |
430 | 432 |
431 /* find the file-transfer object */ | 433 /* find the file-transfer object */ |
432 xfer = find_mxit_xfer( session, fileid ); | 434 xfer = find_mxit_xfer( session, fileid ); |
433 if ( xfer ) { | 435 if ( xfer ) { |
434 mx = xfer->data; | |
435 | |
436 /* this is the transfer we have been looking for */ | 436 /* this is the transfer we have been looking for */ |
437 purple_xfer_ref( xfer ); | 437 purple_xfer_ref( xfer ); |
438 purple_xfer_start( xfer, -1, NULL, 0 ); | 438 purple_xfer_start( xfer, -1, NULL, 0 ); |
439 fwrite( data, datalen, 1, xfer->dest_fp ); | 439 fwrite( data, datalen, 1, xfer->dest_fp ); |
440 // TODO: Handle error from fwrite() | |
440 purple_xfer_unref( xfer ); | 441 purple_xfer_unref( xfer ); |
441 purple_xfer_set_completed( xfer, TRUE ); | 442 purple_xfer_set_completed( xfer, TRUE ); |
442 purple_xfer_end( xfer ); | 443 purple_xfer_end( xfer ); |
443 | 444 |
444 /* inform MXit that file was successfully received */ | 445 /* inform MXit that file was successfully received */ |