2 * @file ft.h File Transfer API
4 * @see @ref xfer-signals
9 * Purple is the legal property of its developers, whose names are too numerous
10 * to list here. Please refer to the COPYRIGHT file distributed with this
11 * source distribution.
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
30 /**************************************************************************/
31 /** Data Structures */
32 /**************************************************************************/
33 typedef struct _PurpleXfer PurpleXfer
;
41 * Types of file transfers.
45 PURPLE_XFER_UNKNOWN
= 0, /**< Unknown file transfer type. */
46 PURPLE_XFER_SEND
, /**< File sending. */
47 PURPLE_XFER_RECEIVE
/**< File receiving. */
52 * The different states of the xfer.
56 PURPLE_XFER_STATUS_UNKNOWN
= 0, /**< Unknown, the xfer may be null. */
57 PURPLE_XFER_STATUS_NOT_STARTED
, /**< It hasn't started yet. */
58 PURPLE_XFER_STATUS_ACCEPTED
, /**< Receive accepted, but destination file not selected yet */
59 PURPLE_XFER_STATUS_STARTED
, /**< purple_xfer_start has been called. */
60 PURPLE_XFER_STATUS_DONE
, /**< The xfer completed successfully. */
61 PURPLE_XFER_STATUS_CANCEL_LOCAL
, /**< The xfer was canceled by us. */
62 PURPLE_XFER_STATUS_CANCEL_REMOTE
/**< The xfer was canceled by the other end, or we couldn't connect. */
63 } PurpleXferStatusType
;
66 * File transfer UI operations.
68 * Any UI representing a file transfer must assign a filled-out
69 * PurpleXferUiOps structure to the purple_xfer.
73 void (*new_xfer
)(PurpleXfer
*xfer
);
74 void (*destroy
)(PurpleXfer
*xfer
);
75 void (*add_xfer
)(PurpleXfer
*xfer
);
76 void (*update_progress
)(PurpleXfer
*xfer
, double percent
);
77 void (*cancel_local
)(PurpleXfer
*xfer
);
78 void (*cancel_remote
)(PurpleXfer
*xfer
);
81 * UI op to write data received from the prpl. The UI must deal with the
82 * entire buffer and return size, or it is treated as an error.
84 * @param xfer The file transfer structure
85 * @param buffer The buffer to write
86 * @param size The size of the buffer
88 * @return size if the write was successful, or a value between 0 and
92 gssize (*ui_write
)(PurpleXfer
*xfer
, const guchar
*buffer
, gssize size
);
95 * UI op to read data to send to the prpl for a file transfer.
97 * @param xfer The file transfer structure
98 * @param buffer A pointer to a buffer. The UI must allocate this buffer.
99 * libpurple will free the data.
100 * @param size The maximum amount of data to put in the buffer.
102 * @returns The amount of data in the buffer, 0 if nothing is available,
103 * and a negative value if an error occurred and the transfer
104 * should be cancelled (libpurple will cancel).
107 gssize (*ui_read
)(PurpleXfer
*xfer
, guchar
**buffer
, gssize size
);
110 * Op to notify the UI that not all the data read in was written. The UI
111 * should re-enqueue this data and return it the next time read is called.
113 * This MUST be implemented if read and write are implemented.
115 * @param xfer The file transfer structure
116 * @param buffer A pointer to the beginning of the unwritten data.
117 * @param size The amount of unwritten data.
121 void (*data_not_sent
)(PurpleXfer
*xfer
, const guchar
*buffer
, gsize size
);
123 void (*_purple_reserved1
)(void);
127 * A core representation of a file transfer.
131 guint ref
; /**< The reference count. */
132 PurpleXferType type
; /**< The type of transfer. */
134 PurpleAccount
*account
; /**< The account. */
136 char *who
; /**< The person on the other end of the
139 char *message
; /**< A message sent with the request */
140 char *filename
; /**< The name sent over the network. */
141 char *local_filename
; /**< The name on the local hard drive. */
142 size_t size
; /**< The size of the file. */
144 FILE *dest_fp
; /**< The destination file pointer. */
146 char *remote_ip
; /**< The remote IP address. */
147 int local_port
; /**< The local port. */
148 int remote_port
; /**< The remote port. */
150 int fd
; /**< The socket file descriptor. */
151 int watcher
; /**< Watcher. */
153 size_t bytes_sent
; /**< The number of bytes sent. */
154 size_t bytes_remaining
; /**< The number of bytes remaining. */
155 time_t start_time
; /**< When the transfer of data began. */
156 time_t end_time
; /**< When the transfer of data ended. */
158 size_t current_buffer_size
; /**< This gradually increases for fast
159 network connections. */
161 PurpleXferStatusType status
; /**< File Transfer's status. */
163 /* I/O operations. */
166 void (*init
)(PurpleXfer
*xfer
);
167 void (*request_denied
)(PurpleXfer
*xfer
);
168 void (*start
)(PurpleXfer
*xfer
);
169 void (*end
)(PurpleXfer
*xfer
);
170 void (*cancel_send
)(PurpleXfer
*xfer
);
171 void (*cancel_recv
)(PurpleXfer
*xfer
);
172 gssize (*read
)(guchar
**buffer
, PurpleXfer
*xfer
);
173 gssize (*write
)(const guchar
*buffer
, size_t size
, PurpleXfer
*xfer
);
174 void (*ack
)(PurpleXfer
*xfer
, const guchar
*buffer
, size_t size
);
177 PurpleXferUiOps
*ui_ops
; /**< UI-specific operations. */
178 void *ui_data
; /**< UI-specific data. */
180 void *data
; /**< prpl-specific data. */
187 /**************************************************************************/
188 /** @name File Transfer API */
189 /**************************************************************************/
193 * Creates a new file transfer handle.
194 * This is called by prpls.
195 * The handle starts with a ref count of 1, and this reference
196 * is owned by the core. The prpl normally does not need to
197 * purple_xfer_ref or unref.
199 * @param account The account sending or receiving the file.
200 * @param type The type of file transfer.
201 * @param who The name of the remote user.
203 * @return A file transfer handle.
205 PurpleXfer
*purple_xfer_new(PurpleAccount
*account
,
206 PurpleXferType type
, const char *who
);
211 * @return all current xfers with refs
213 GList
*purple_xfers_get_all(void);
216 * Increases the reference count on a PurpleXfer.
217 * Please call purple_xfer_unref later.
219 * @param xfer A file transfer handle.
221 void purple_xfer_ref(PurpleXfer
*xfer
);
224 * Decreases the reference count on a PurpleXfer.
225 * If the reference reaches 0, purple_xfer_destroy (an internal function)
226 * will destroy the xfer. It calls the ui destroy cb first.
227 * Since the core keeps a ref on the xfer, only an erroneous call to
228 * this function will destroy the xfer while still in use.
230 * @param xfer A file transfer handle.
232 void purple_xfer_unref(PurpleXfer
*xfer
);
235 * Requests confirmation for a file transfer from the user. If receiving
236 * a file which is known at this point, this requests user to accept and
237 * save the file. If the filename is unknown (not set) this only requests user
238 * to accept the file transfer. In this case protocol must call this function
239 * again once the filename is available.
241 * @param xfer The file transfer to request confirmation on.
243 void purple_xfer_request(PurpleXfer
*xfer
);
246 * Called if the user accepts the file transfer request.
248 * @param xfer The file transfer.
249 * @param filename The filename.
251 void purple_xfer_request_accepted(PurpleXfer
*xfer
, const char *filename
);
254 * Called if the user rejects the file transfer request.
256 * @param xfer The file transfer.
258 void purple_xfer_request_denied(PurpleXfer
*xfer
);
261 * Returns the type of file transfer.
263 * @param xfer The file transfer.
265 * @return The type of the file transfer.
267 PurpleXferType
purple_xfer_get_type(const PurpleXfer
*xfer
);
270 * Returns the account the file transfer is using.
272 * @param xfer The file transfer.
274 * @return The account.
276 PurpleAccount
*purple_xfer_get_account(const PurpleXfer
*xfer
);
279 * Returns the name of the remote user.
281 * @param xfer The file transfer.
283 * @return The name of the remote user.
287 const char *purple_xfer_get_remote_user(const PurpleXfer
*xfer
);
290 * Returns the status of the xfer.
292 * @param xfer The file transfer.
294 * @return The status.
296 PurpleXferStatusType
purple_xfer_get_status(const PurpleXfer
*xfer
);
299 * Returns true if the file transfer was canceled.
301 * @param xfer The file transfer.
303 * @return Whether or not the transfer was canceled.
305 gboolean
purple_xfer_is_canceled(const PurpleXfer
*xfer
);
308 * Returns the completed state for a file transfer.
310 * @param xfer The file transfer.
312 * @return The completed state.
314 gboolean
purple_xfer_is_completed(const PurpleXfer
*xfer
);
317 * Returns the name of the file being sent or received.
319 * @param xfer The file transfer.
321 * @return The filename.
323 const char *purple_xfer_get_filename(const PurpleXfer
*xfer
);
326 * Returns the file's destination filename,
328 * @param xfer The file transfer.
330 * @return The destination filename.
332 const char *purple_xfer_get_local_filename(const PurpleXfer
*xfer
);
335 * Returns the number of bytes sent (or received) so far.
337 * @param xfer The file transfer.
339 * @return The number of bytes sent.
341 size_t purple_xfer_get_bytes_sent(const PurpleXfer
*xfer
);
344 * Returns the number of bytes remaining to send or receive.
346 * @param xfer The file transfer.
348 * @return The number of bytes remaining.
350 size_t purple_xfer_get_bytes_remaining(const PurpleXfer
*xfer
);
353 * Returns the size of the file being sent or received.
355 * @param xfer The file transfer.
357 * @return The total size of the file.
359 size_t purple_xfer_get_size(const PurpleXfer
*xfer
);
362 * Returns the current percentage of progress of the transfer.
364 * This is a number between 0 (0%) and 1 (100%).
366 * @param xfer The file transfer.
368 * @return The percentage complete.
370 double purple_xfer_get_progress(const PurpleXfer
*xfer
);
373 * Returns the local port number in the file transfer.
375 * @param xfer The file transfer.
377 * @return The port number on this end.
379 unsigned int purple_xfer_get_local_port(const PurpleXfer
*xfer
);
382 * Returns the remote IP address in the file transfer.
384 * @param xfer The file transfer.
386 * @return The IP address on the other end.
388 const char *purple_xfer_get_remote_ip(const PurpleXfer
*xfer
);
391 * Returns the remote port number in the file transfer.
393 * @param xfer The file transfer.
395 * @return The port number on the other end.
397 unsigned int purple_xfer_get_remote_port(const PurpleXfer
*xfer
);
400 * Returns the time the transfer of a file started.
402 * @param xfer The file transfer.
404 * @return The time when the transfer started.
407 time_t purple_xfer_get_start_time(const PurpleXfer
*xfer
);
410 * Returns the time the transfer of a file ended.
412 * @param xfer The file transfer.
414 * @return The time when the transfer ended.
417 time_t purple_xfer_get_end_time(const PurpleXfer
*xfer
);
420 * Sets the completed state for the file transfer.
422 * @param xfer The file transfer.
423 * @param completed The completed state.
425 void purple_xfer_set_completed(PurpleXfer
*xfer
, gboolean completed
);
428 * Sets the filename for the file transfer.
430 * @param xfer The file transfer.
431 * @param message The message.
433 void purple_xfer_set_message(PurpleXfer
*xfer
, const char *message
);
436 * Sets the filename for the file transfer.
438 * @param xfer The file transfer.
439 * @param filename The filename.
441 void purple_xfer_set_filename(PurpleXfer
*xfer
, const char *filename
);
444 * Sets the local filename for the file transfer.
446 * @param xfer The file transfer.
447 * @param filename The filename
449 void purple_xfer_set_local_filename(PurpleXfer
*xfer
, const char *filename
);
452 * Sets the size of the file in a file transfer.
454 * @param xfer The file transfer.
455 * @param size The size of the file.
457 void purple_xfer_set_size(PurpleXfer
*xfer
, size_t size
);
460 * Sets the current working position in the active file transfer. This
461 * can be used to jump backward in the file if the protocol detects
462 * that some bit of data needs to be resent or has been sent twice.
464 * It's used for pausing and resuming an oscar file transfer.
466 * @param xfer The file transfer.
467 * @param bytes_sent The new current position in the file. If we're
468 * sending a file then this is the byte that we will
469 * send. If we're receiving a file, this is the
470 * next byte that we expect to receive.
472 void purple_xfer_set_bytes_sent(PurpleXfer
*xfer
, size_t bytes_sent
);
475 * Returns the UI operations structure for a file transfer.
477 * @param xfer The file transfer.
479 * @return The UI operations structure.
481 PurpleXferUiOps
*purple_xfer_get_ui_ops(const PurpleXfer
*xfer
);
484 * Sets the read function for the file transfer.
486 * @param xfer The file transfer.
487 * @param fnc The read function.
489 void purple_xfer_set_read_fnc(PurpleXfer
*xfer
,
490 gssize (*fnc
)(guchar
**, PurpleXfer
*));
493 * Sets the write function for the file transfer.
495 * @param xfer The file transfer.
496 * @param fnc The write function.
498 void purple_xfer_set_write_fnc(PurpleXfer
*xfer
,
499 gssize (*fnc
)(const guchar
*, size_t, PurpleXfer
*));
502 * Sets the acknowledge function for the file transfer.
504 * @param xfer The file transfer.
505 * @param fnc The acknowledge function.
507 void purple_xfer_set_ack_fnc(PurpleXfer
*xfer
,
508 void (*fnc
)(PurpleXfer
*, const guchar
*, size_t));
511 * Sets the function to be called if the request is denied.
513 * @param xfer The file transfer.
514 * @param fnc The request denied prpl callback.
516 void purple_xfer_set_request_denied_fnc(PurpleXfer
*xfer
, void (*fnc
)(PurpleXfer
*));
519 * Sets the transfer initialization function for the file transfer.
521 * This function is required, and must call purple_xfer_start() with
522 * the necessary parameters. This will be called if the file transfer
523 * is accepted by the user.
525 * @param xfer The file transfer.
526 * @param fnc The transfer initialization function.
528 void purple_xfer_set_init_fnc(PurpleXfer
*xfer
, void (*fnc
)(PurpleXfer
*));
531 * Sets the start transfer function for the file transfer.
533 * @param xfer The file transfer.
534 * @param fnc The start transfer function.
536 void purple_xfer_set_start_fnc(PurpleXfer
*xfer
, void (*fnc
)(PurpleXfer
*));
539 * Sets the end transfer function for the file transfer.
541 * @param xfer The file transfer.
542 * @param fnc The end transfer function.
544 void purple_xfer_set_end_fnc(PurpleXfer
*xfer
, void (*fnc
)(PurpleXfer
*));
547 * Sets the cancel send function for the file transfer.
549 * @param xfer The file transfer.
550 * @param fnc The cancel send function.
552 void purple_xfer_set_cancel_send_fnc(PurpleXfer
*xfer
, void (*fnc
)(PurpleXfer
*));
555 * Sets the cancel receive function for the file transfer.
557 * @param xfer The file transfer.
558 * @param fnc The cancel receive function.
560 void purple_xfer_set_cancel_recv_fnc(PurpleXfer
*xfer
, void (*fnc
)(PurpleXfer
*));
563 * Reads in data from a file transfer stream.
565 * @param xfer The file transfer.
566 * @param buffer The buffer that will be created to contain the data.
568 * @return The number of bytes read, or -1.
570 gssize
purple_xfer_read(PurpleXfer
*xfer
, guchar
**buffer
);
573 * Writes data to a file transfer stream.
575 * @param xfer The file transfer.
576 * @param buffer The buffer to read the data from.
577 * @param size The number of bytes to write.
579 * @return The number of bytes written, or -1.
581 gssize
purple_xfer_write(PurpleXfer
*xfer
, const guchar
*buffer
, gsize size
);
584 * Starts a file transfer.
586 * Either @a fd must be specified <i>or</i> @a ip and @a port on a
587 * file receive transfer. On send, @a fd must be specified, and
588 * @a ip and @a port are ignored.
590 * Prior to libpurple 2.6.0, passing '0' to @a fd was special-cased to
591 * allow the protocol plugin to facilitate the file transfer itself. As of
592 * 2.6.0, this is supported (for backward compatibility), but will be
593 * removed in libpurple 3.0.0. If a prpl detects that the running libpurple
594 * is running 2.6.0 or higher, it should use the invalid fd '-1'.
596 * @param xfer The file transfer.
597 * @param fd The file descriptor for the socket.
598 * @param ip The IP address to connect to.
599 * @param port The port to connect to.
601 void purple_xfer_start(PurpleXfer
*xfer
, int fd
, const char *ip
,
605 * Ends a file transfer.
607 * @param xfer The file transfer.
609 void purple_xfer_end(PurpleXfer
*xfer
);
612 * Adds a new file transfer to the list of file transfers. Call this only
613 * if you are not using purple_xfer_start.
615 * @param xfer The file transfer.
617 void purple_xfer_add(PurpleXfer
*xfer
);
620 * Cancels a file transfer on the local end.
622 * @param xfer The file transfer.
624 void purple_xfer_cancel_local(PurpleXfer
*xfer
);
627 * Cancels a file transfer from the remote end.
629 * @param xfer The file transfer.
631 void purple_xfer_cancel_remote(PurpleXfer
*xfer
);
634 * Displays a file transfer-related error message.
636 * This is a wrapper around purple_notify_error(), which automatically
637 * specifies a title ("File transfer to <i>user</i> failed" or
638 * "File Transfer from <i>user</i> failed").
640 * @param type The type of file transfer.
641 * @param account The account sending or receiving the file.
642 * @param who The user on the other end of the transfer.
643 * @param msg The message to display.
645 void purple_xfer_error(PurpleXferType type
, PurpleAccount
*account
, const char *who
, const char *msg
);
648 * Updates file transfer progress.
650 * @param xfer The file transfer.
652 void purple_xfer_update_progress(PurpleXfer
*xfer
);
655 * Displays a file transfer-related message in the conversation window
657 * This is a wrapper around purple_conversation_write
659 * @param xfer The file transfer to which this message relates.
660 * @param message The message to display.
661 * @param is_error Is this an error message?.
663 void purple_xfer_conversation_write(PurpleXfer
*xfer
, char *message
, gboolean is_error
);
666 * Allows the UI to signal it's ready to send/receive data (depending on
667 * the direction of the file transfer. Used when the UI is providing
668 * read/write/data_not_sent UI ops.
670 * @param xfer The file transfer which is ready.
674 void purple_xfer_ui_ready(PurpleXfer
*xfer
);
677 * Allows the prpl to signal it's ready to send/receive data (depending on
678 * the direction of the file transfer. Used when the prpl provides read/write
679 * ops and cannot/does not provide a raw fd to the core.
681 * @param xfer The file transfer which is ready.
685 void purple_xfer_prpl_ready(PurpleXfer
*xfer
);
689 /**************************************************************************/
690 /** @name UI Registration Functions */
691 /**************************************************************************/
695 * Returns the handle to the file transfer subsystem
699 void *purple_xfers_get_handle(void);
702 * Initializes the file transfer subsystem
704 void purple_xfers_init(void);
707 * Uninitializes the file transfer subsystem
709 void purple_xfers_uninit(void);
712 * Sets the UI operations structure to be used in all purple file transfers.
714 * @param ops The UI operations structure.
716 void purple_xfers_set_ui_ops(PurpleXferUiOps
*ops
);
719 * Returns the UI operations structure to be used in all purple file transfers.
721 * @return The UI operations structure.
723 PurpleXferUiOps
*purple_xfers_get_ui_ops(void);
731 #endif /* _PURPLE_FT_H_ */