]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-file.c
Removed useless +0.5 in get_time_remaining. (Jonny Lamb)
[empathy.git] / libempathy / empathy-tp-file.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007-2008 Collabora Ltd.
4  * Copyright (C) 2007 Marco Barisione <marco@barisione.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * Authors: Marco Barisione <marco@barisione.org>
21  *          Jonny Lamb <jonny.lamb@collabora.co.uk>
22  */
23
24 #include <config.h>
25
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <sys/un.h>
31
32 #include <glib/gi18n.h>
33
34 #include <gio/gio.h>
35 #include <gio/gunixinputstream.h>
36 #include <gio/gunixoutputstream.h>
37
38 #include <libtelepathy/tp-conn.h>
39 #include <libtelepathy/tp-helpers.h>
40 #include <libtelepathy/tp-props-iface.h>
41
42 #include <telepathy-glib/proxy-subclass.h>
43
44 #include "empathy-tp-file.h"
45 #include "empathy-contact-factory.h"
46 #include "empathy-marshal.h"
47 #include "empathy-time.h"
48 #include "empathy-utils.h"
49
50 #define DEBUG_FLAG EMPATHY_DEBUG_FT
51 #include "empathy-debug.h"
52
53 /**
54  * SECTION:empathy-tp-file
55  * @short_description: File channel
56  * @see_also: #EmpathyTpFile, #EmpathyContact, empathy_send_file()
57  * @include: libempthy/empathy-tp-file.h
58  *
59  * The #EmpathyTpFile object represents a Telepathy file channel.
60  */
61
62 /**
63  * EMPATHY_TP_FILE_UNKNOWN_SIZE:
64  *
65  * Value used for the "size" or "estimated-size" properties when the size of
66  * the transferred file is unknown.
67  */
68
69 /* Functions to copy the content of a GInputStream to a GOutputStream */
70
71 #define N_BUFFERS 2
72 #define BUFFER_SIZE 4096
73
74 typedef struct {
75   GInputStream *in;
76   GOutputStream *out;
77   GCancellable  *cancellable;
78   char *buff[N_BUFFERS]; /* the temporary buffers */
79   gsize count[N_BUFFERS]; /* how many bytes are used in the buffers */
80   gboolean is_full[N_BUFFERS]; /* whether the buffers contain data */
81   gint curr_read; /* index of the buffer used for reading */
82   gint curr_write; /* index of the buffer used for writing */
83   gboolean is_reading; /* we are reading */
84   gboolean is_writing; /* we are writing */
85   guint n_closed; /* number of streams that have been closed */
86 } CopyData;
87
88 static void schedule_next (CopyData *copy);
89
90 static void
91 free_copy_data_if_closed (CopyData *copy)
92 {
93   gint i;
94
95   /* Free the data only if both the input and output streams have
96    * been closed. */
97   copy->n_closed++;
98   if (copy->n_closed < 2)
99     return;
100
101   if (copy->in != NULL)
102     g_object_unref (copy->in);
103
104   if (copy->out != NULL)
105     g_object_unref (copy->out);
106
107   for (i = 0; i < N_BUFFERS; i++)
108     g_free (copy->buff[i]);
109
110   g_object_unref (copy->cancellable);
111   g_free (copy);
112 }
113
114 static void
115 io_error (CopyData *copy,
116           GError *error)
117 {
118   g_cancellable_cancel (copy->cancellable);
119
120   if (error == NULL)
121     g_warning ("I/O error");
122   else if (error->domain == G_IO_ERROR && error->code == G_IO_ERROR_CANCELLED)
123     ; /* Ignore cancellations */
124   else
125     g_warning ("I/O error: %d: %s\n", error->code, error->message);
126
127   if (copy->in != NULL)
128     g_input_stream_close (copy->in, NULL, NULL);
129
130   if (copy->out != NULL)
131     g_output_stream_close (copy->out, NULL, NULL);
132
133   free_copy_data_if_closed (copy);
134 }
135
136 static void
137 close_done (GObject *source_object,
138             GAsyncResult *res,
139             gpointer user_data)
140 {
141   CopyData *copy = user_data;
142
143   g_object_unref (source_object);
144   free_copy_data_if_closed (copy);
145 }
146
147 static void
148 write_done_cb (GObject *source_object,
149                GAsyncResult *res,
150                gpointer user_data)
151 {
152   CopyData *copy = user_data;
153   gssize count_write;
154   GError *error = NULL;
155
156   count_write = g_output_stream_write_finish (copy->out, res, &error);
157
158   if (count_write <= 0)
159     {
160       io_error (copy, error);
161       g_error_free (error);
162       return;
163     }
164
165   copy->is_full[copy->curr_write] = FALSE;
166   copy->curr_write = (copy->curr_write + 1) % N_BUFFERS;
167   copy->is_writing = FALSE;
168
169   schedule_next (copy);
170 }
171
172 static void
173 read_done_cb (GObject *source_object,
174               GAsyncResult *res,
175               gpointer user_data)
176 {
177   CopyData *copy = user_data;
178   gssize count_read;
179   GError *error = NULL;
180
181   count_read = g_input_stream_read_finish (copy->in, res, &error);
182
183   if (count_read == 0)
184     {
185       g_input_stream_close_async (copy->in, 0, copy->cancellable,
186           close_done, copy);
187       copy->in = NULL;
188     }
189   else if (count_read < 0)
190     {
191       io_error (copy, error);
192       g_error_free (error);
193       return;
194     }
195
196   copy->count[copy->curr_read] = count_read;
197   copy->is_full[copy->curr_read] = TRUE;
198   copy->curr_read = (copy->curr_read + 1) % N_BUFFERS;
199   copy->is_reading = FALSE;
200
201   schedule_next (copy);
202 }
203
204 static void
205 schedule_next (CopyData *copy)
206 {
207   if (copy->in != NULL &&
208       !copy->is_reading &&
209       !copy->is_full[copy->curr_read])
210     {
211       /* We are not reading and the current buffer is empty, so
212        * start an async read. */
213       copy->is_reading = TRUE;
214       g_input_stream_read_async (copy->in,
215           copy->buff[copy->curr_read],
216           BUFFER_SIZE, 0, copy->cancellable,
217           read_done_cb, copy);
218     }
219
220   if (!copy->is_writing &&
221       copy->is_full[copy->curr_write])
222     {
223       if (copy->count[copy->curr_write] == 0)
224         {
225           /* The last read on the buffer read 0 bytes, this
226            * means that we got an EOF, so we can close
227            * the output channel. */
228           g_output_stream_close_async (copy->out, 0,
229               copy->cancellable,
230               close_done, copy);
231       copy->out = NULL;
232         }
233       else
234         {
235           /* We are not writing and the current buffer contains
236            * data, so start an async write. */
237           copy->is_writing = TRUE;
238           g_output_stream_write_async (copy->out,
239               copy->buff[copy->curr_write],
240               copy->count[copy->curr_write],
241               0, copy->cancellable,
242               write_done_cb, copy);
243         }
244     }
245 }
246
247 static void
248 copy_stream (GInputStream *in,
249              GOutputStream *out,
250              GCancellable *cancellable)
251 {
252   CopyData *copy;
253   gint i;
254
255   g_return_if_fail (in != NULL);
256   g_return_if_fail (out != NULL);
257
258   copy = g_new0 (CopyData, 1);
259   copy->in = g_object_ref (in);
260   copy->out = g_object_ref (out);
261
262   if (cancellable != NULL)
263     copy->cancellable = g_object_ref (cancellable);
264   else
265     copy->cancellable = g_cancellable_new ();
266
267   for (i = 0; i < N_BUFFERS; i++)
268     copy->buff[i] = g_malloc (BUFFER_SIZE);
269
270   schedule_next (copy);
271 }
272
273 /* EmpathyTpFile object */
274
275 struct _EmpathyTpFilePriv {
276   EmpathyContactFactory *factory;
277   gchar *id;
278   MissionControl *mc;
279   TpChannel *channel;
280
281   EmpathyContact *contact;
282   GInputStream *in_stream;
283   GOutputStream *out_stream;
284   gboolean incoming;
285   gchar *filename;
286   EmpFileTransferState state;
287   EmpFileTransferStateChangeReason state_change_reason;
288   guint64 size;
289   guint64 transferred_bytes;
290   time_t start_time;
291   gchar *unix_socket_path;
292   gchar *content_hash;
293   EmpFileHashType content_hash_type;
294   gchar *content_type;
295   gchar *description;
296   GCancellable *cancellable;
297 };
298
299 enum {
300   PROP_0,
301   PROP_CHANNEL,
302   PROP_STATE,
303   PROP_INCOMING,
304   PROP_FILENAME,
305   PROP_SIZE,
306   PROP_CONTENT_TYPE,
307   PROP_TRANSFERRED_BYTES,
308   PROP_CONTENT_HASH_TYPE,
309   PROP_CONTENT_HASH,
310   PROP_IN_STREAM,
311 };
312
313 G_DEFINE_TYPE (EmpathyTpFile, empathy_tp_file, G_TYPE_OBJECT);
314
315 static void
316 empathy_tp_file_init (EmpathyTpFile *tp_file)
317 {
318   EmpathyTpFilePriv *priv;
319
320   priv = G_TYPE_INSTANCE_GET_PRIVATE ((tp_file),
321       EMPATHY_TYPE_TP_FILE, EmpathyTpFilePriv);
322
323   tp_file->priv = priv;
324 }
325
326 static void
327 tp_file_destroy_cb (TpChannel *file_channel,
328                     EmpathyTpFile *tp_file)
329 {
330   DEBUG ("Channel Closed or CM crashed");
331
332   g_object_unref (tp_file->priv->channel);
333   tp_file->priv->channel = NULL;
334 }
335
336 static void
337 tp_file_finalize (GObject *object)
338 {
339   EmpathyTpFile *tp_file;
340
341   tp_file = EMPATHY_TP_FILE (object);
342
343   if (tp_file->priv->channel)
344     {
345       DEBUG ("Closing channel..");
346       g_signal_handlers_disconnect_by_func (tp_file->priv->channel,
347           tp_file_destroy_cb, object);
348       tp_cli_channel_call_close (tp_file->priv->channel, -1, NULL, NULL,
349           NULL, NULL);
350       g_object_unref (tp_file->priv->channel);
351     }
352
353   if (tp_file->priv->factory)
354     {
355       g_object_unref (tp_file->priv->factory);
356     }
357   if (tp_file->priv->mc)
358     {
359       g_object_unref (tp_file->priv->mc);
360     }
361
362   g_free (tp_file->priv->id);
363   g_free (tp_file->priv->filename);
364   g_free (tp_file->priv->unix_socket_path);
365   g_free (tp_file->priv->description);
366   g_free (tp_file->priv->content_hash);
367   g_free (tp_file->priv->content_type);
368
369   if (tp_file->priv->in_stream)
370     g_object_unref (tp_file->priv->in_stream);
371
372   if (tp_file->priv->out_stream)
373     g_object_unref (tp_file->priv->out_stream);
374
375   if (tp_file->priv->contact)
376     g_object_unref (tp_file->priv->contact);
377
378   if (tp_file->priv->cancellable)
379     g_object_unref (tp_file->priv->cancellable);
380
381   G_OBJECT_CLASS (empathy_tp_file_parent_class)->finalize (object);
382 }
383
384 static void
385 tp_file_closed_cb (TpChannel *file_channel,
386                    EmpathyTpFile *tp_file,
387                    GObject *weak_object)
388 {
389   /* The channel is closed, do just like if the proxy was destroyed */
390   g_signal_handlers_disconnect_by_func (tp_file->priv->channel,
391       tp_file_destroy_cb,
392       tp_file);
393   tp_file_destroy_cb (file_channel, tp_file);
394 }
395
396 static gint
397 _get_local_socket (EmpathyTpFile *tp_file)
398 {
399   gint fd;
400   size_t path_len;
401   struct sockaddr_un addr;
402
403   if (G_STR_EMPTY (tp_file->priv->unix_socket_path))
404     return -1;
405
406   fd = socket (PF_UNIX, SOCK_STREAM, 0);
407   if (fd < 0)
408     return -1;
409
410   memset (&addr, 0, sizeof (addr));
411   addr.sun_family = AF_UNIX;
412   path_len = strlen (tp_file->priv->unix_socket_path);
413   strncpy (addr.sun_path, tp_file->priv->unix_socket_path, path_len);
414
415   if (connect (fd, (struct sockaddr*) &addr,
416       sizeof (addr)) < 0)
417     {
418       close (fd);
419       return -1;
420     }
421
422   return fd;
423 }
424
425 static void
426 send_tp_file (EmpathyTpFile *tp_file)
427 {
428   gint socket_fd;
429   GOutputStream *socket_stream;
430
431   DEBUG ("Sending file content: filename=%s",
432       tp_file->priv->filename);
433
434   g_return_if_fail (tp_file->priv->in_stream);
435
436   socket_fd = _get_local_socket (tp_file);
437   if (socket_fd < 0)
438     {
439       DEBUG ("failed to get local socket fd");
440       return;
441     }
442   DEBUG ("got local socket fd");
443   socket_stream = g_unix_output_stream_new (socket_fd, TRUE);
444
445   tp_file->priv->cancellable = g_cancellable_new ();
446
447   copy_stream (tp_file->priv->in_stream, socket_stream,
448       tp_file->priv->cancellable);
449
450   g_object_unref (socket_stream);
451 }
452
453 static void
454 receive_tp_file (EmpathyTpFile *tp_file)
455 {
456   GInputStream *socket_stream;
457   gint socket_fd;
458
459   socket_fd = _get_local_socket (tp_file);
460
461   if (socket_fd < 0)
462     return;
463
464   socket_stream = g_unix_input_stream_new (socket_fd, TRUE);
465
466   tp_file->priv->cancellable = g_cancellable_new ();
467
468   copy_stream (socket_stream, tp_file->priv->out_stream,
469       tp_file->priv->cancellable);
470
471   g_object_unref (socket_stream);
472 }
473
474 static void
475 tp_file_state_changed_cb (DBusGProxy *tp_file_iface,
476                           EmpFileTransferState state,
477                           EmpFileTransferStateChangeReason reason,
478                           EmpathyTpFile *tp_file)
479 {
480   DEBUG ("File transfer state changed: filename=%s, "
481       "old state=%u, state=%u, reason=%u",
482       tp_file->priv->filename, tp_file->priv->state, state, reason);
483
484   if (state == EMP_FILE_TRANSFER_STATE_OPEN)
485     tp_file->priv->start_time = empathy_time_get_current ();
486
487   DEBUG ("state = %u, incoming = %s, in_stream = %s, out_stream = %s",
488       state, tp_file->priv->incoming ? "yes" : "no",
489       tp_file->priv->in_stream ? "present" : "not present",
490       tp_file->priv->out_stream ? "present" : "not present");
491
492   if (state == EMP_FILE_TRANSFER_STATE_OPEN && !tp_file->priv->incoming &&
493       tp_file->priv->in_stream)
494     send_tp_file (tp_file);
495   else if (state == EMP_FILE_TRANSFER_STATE_OPEN && tp_file->priv->incoming &&
496       tp_file->priv->out_stream)
497     receive_tp_file (tp_file);
498
499   tp_file->priv->state = state;
500   tp_file->priv->state_change_reason = reason;
501
502   g_object_notify (G_OBJECT (tp_file), "state");
503 }
504
505 static void
506 tp_file_transferred_bytes_changed_cb (TpProxy *proxy,
507                                       guint64 count,
508                                       EmpathyTpFile *tp_file,
509                                       GObject *weak_object)
510 {
511   if (tp_file->priv->transferred_bytes == count)
512     return;
513
514   tp_file->priv->transferred_bytes = count;
515
516   g_object_notify (G_OBJECT (tp_file), "transferred-bytes");
517 }
518
519 static GObject *
520 tp_file_constructor (GType type,
521                      guint n_props,
522                      GObjectConstructParam *props)
523 {
524   GObject *file_obj;
525   EmpathyTpFile *tp_file;
526   TpHandle handle;
527   GHashTable *properties;
528   McAccount *account;
529
530   file_obj = G_OBJECT_CLASS (empathy_tp_file_parent_class)->constructor (type,
531       n_props, props);
532
533   tp_file = EMPATHY_TP_FILE (file_obj);
534
535   tp_file->priv->factory = empathy_contact_factory_new ();
536   tp_file->priv->mc = empathy_mission_control_new ();
537
538   tp_cli_channel_connect_to_closed (tp_file->priv->channel,
539       (tp_cli_channel_signal_callback_closed) tp_file_closed_cb,
540       tp_file,
541       NULL, NULL, NULL);
542
543   emp_cli_channel_type_file_connect_to_file_transfer_state_changed (
544       TP_PROXY (tp_file->priv->channel),
545       (emp_cli_channel_type_file_signal_callback_file_transfer_state_changed)
546           tp_file_state_changed_cb,
547       tp_file,
548       NULL, NULL, NULL);
549
550   emp_cli_channel_type_file_connect_to_transferred_bytes_changed (
551       TP_PROXY (tp_file->priv->channel),
552       (emp_cli_channel_type_file_signal_callback_transferred_bytes_changed)
553           tp_file_transferred_bytes_changed_cb,
554       tp_file,
555       NULL, NULL, NULL);
556
557   account = empathy_channel_get_account (tp_file->priv->channel);
558
559   handle = tp_channel_get_handle (tp_file->priv->channel, NULL);
560   tp_file->priv->contact = empathy_contact_factory_get_from_handle (
561       tp_file->priv->factory, account, (guint) handle);
562
563   tp_cli_dbus_properties_run_get_all (tp_file->priv->channel,
564       -1, EMP_IFACE_CHANNEL_TYPE_FILE, &properties, NULL, NULL);
565
566   tp_file->priv->size = g_value_get_uint64 (
567       g_hash_table_lookup (properties, "Size"));
568
569   tp_file->priv->state = g_value_get_uint (
570       g_hash_table_lookup (properties, "State"));
571
572   tp_file->priv->state_change_reason =
573       EMP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE;
574
575   tp_file->priv->transferred_bytes = g_value_get_uint64 (
576       g_hash_table_lookup (properties, "TransferredBytes"));
577
578   tp_file->priv->filename = g_value_dup_string (
579       g_hash_table_lookup (properties, "Filename"));
580
581   tp_file->priv->content_hash = g_value_dup_string (
582       g_hash_table_lookup (properties, "ContentHash"));
583
584   tp_file->priv->description = g_value_dup_string (
585       g_hash_table_lookup (properties, "Description"));
586
587   if (tp_file->priv->state == EMP_FILE_TRANSFER_STATE_LOCAL_PENDING)
588     tp_file->priv->incoming = TRUE;
589
590   g_hash_table_destroy (properties);
591   g_object_unref (account);
592
593   return file_obj;
594 }
595
596 static void
597 tp_file_get_property (GObject *object,
598                       guint param_id,
599                       GValue *value,
600                       GParamSpec *pspec)
601 {
602   EmpathyTpFile *tp_file;
603
604   tp_file = EMPATHY_TP_FILE (object);
605
606   switch (param_id)
607     {
608       case PROP_CHANNEL:
609         g_value_set_object (value, tp_file->priv->channel);
610         break;
611       default:
612         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
613         break;
614     };
615 }
616
617 static void
618 tp_file_channel_set_dbus_property (gpointer proxy,
619                                    const gchar *property,
620                                    const GValue *value)
621 {
622         DEBUG ("Setting %s property", property);
623         tp_cli_dbus_properties_call_set (TP_PROXY (proxy), -1,
624             EMP_IFACE_CHANNEL_TYPE_FILE, property, value,
625             NULL, NULL, NULL, NULL);
626 }
627
628
629 static void
630 tp_file_set_property (GObject *object,
631                       guint param_id,
632                       const GValue *value,
633                       GParamSpec *pspec)
634 {
635   EmpathyTpFile *tp_file = (EmpathyTpFile *) object;
636   switch (param_id)
637     {
638       case PROP_CHANNEL:
639         tp_file->priv->channel = g_object_ref (g_value_get_object (value));
640         break;
641       case PROP_STATE:
642         tp_file->priv->state = g_value_get_uint (value);
643         break;
644       case PROP_INCOMING:
645         tp_file->priv->incoming = g_value_get_boolean (value);
646         break;
647       case PROP_FILENAME:
648         g_free (tp_file->priv->filename);
649         tp_file->priv->filename = g_value_dup_string (value);
650         tp_file_channel_set_dbus_property (tp_file->priv->channel,
651             "Filename", value);
652         break;
653       case PROP_SIZE:
654         tp_file->priv->size = g_value_get_uint64 (value);
655         tp_file_channel_set_dbus_property (tp_file->priv->channel,
656             "Size", value);
657         break;
658       case PROP_CONTENT_TYPE:
659         tp_file_channel_set_dbus_property (tp_file->priv->channel,
660             "ContentType", value);
661         g_free (tp_file->priv->content_type);
662         tp_file->priv->content_type = g_value_dup_string (value);
663         break;
664       case PROP_CONTENT_HASH:
665         tp_file_channel_set_dbus_property (tp_file->priv->channel,
666             "ContentHash", value);
667         g_free (tp_file->priv->content_hash);
668         tp_file->priv->content_hash = g_value_dup_string (value);
669         break;
670       case PROP_IN_STREAM:
671         if (tp_file->priv->in_stream)
672           g_object_unref (tp_file->priv->in_stream);
673         tp_file->priv->in_stream = g_object_ref (g_value_get_object (value));
674         break;
675       default:
676         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
677         break;
678     };
679 }
680
681 /**
682  * empathy_tp_file_new:
683  * @channel: a Telepathy channel
684  *
685  * Creates a new #EmpathyTpFile wrapping @channel.
686  *
687  * Returns: a new #EmpathyTpFile
688  */
689 EmpathyTpFile *
690 empathy_tp_file_new (TpChannel *channel)
691 {
692   g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
693
694   return g_object_new (EMPATHY_TYPE_TP_FILE,
695       "channel", channel,
696       NULL);
697 }
698
699 /**
700  * empathy_tp_file_get_id:
701  * @tp_file: an #EmpathyTpFile
702  *
703  * Returns the ID of @tp_file.
704  *
705  * Returns: the ID
706  */
707 const gchar *
708 empathy_tp_file_get_id (EmpathyTpFile *tp_file)
709 {
710   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
711
712   return tp_file->priv->id;
713 }
714
715 /**
716  * empathy_tp_file_get_channel
717  * @tp_file: an #EmpathyTpFile
718  *
719  * Returns the Telepathy file transfer channel
720  *
721  * Returns: the #TpChannel
722  */
723 TpChannel *
724 empathy_tp_file_get_channel (EmpathyTpFile *tp_file)
725 {
726   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
727
728   return tp_file->priv->channel;
729 }
730
731 static void
732 tp_file_method_cb (TpProxy *proxy,
733                    const GValue *address,
734                    const GError *error,
735                    gpointer user_data,
736                    GObject *weak_object)
737 {
738   EmpathyTpFile *tp_file = (EmpathyTpFile *) user_data;
739
740   if (error)
741     {
742       DEBUG ("Error: %s", error->message);
743       return;
744     }
745
746   if (tp_file->priv->unix_socket_path)
747     g_free (tp_file->priv->unix_socket_path);
748
749   tp_file->priv->unix_socket_path = g_value_dup_string (address);
750
751   DEBUG ("Got unix socket path: %s", tp_file->priv->unix_socket_path);
752 }
753
754
755 /**
756  * empathy_tp_file_accept:
757  * @tp_file: an #EmpathyTpFile
758  *
759  * Accepts a file transfer that's in the "local pending" state (i.e.
760  * EMP_FILE_TRANSFER_STATE_LOCAL_PENDING).
761  */
762 void
763 empathy_tp_file_accept (EmpathyTpFile *tp_file,
764                         guint64 offset)
765 {
766   GValue nothing = { 0 };
767
768   g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
769
770   g_return_if_fail (tp_file->priv->out_stream != NULL);
771
772   DEBUG ("Accepting file: filename=%s", tp_file->priv->filename);
773
774   g_value_init (&nothing, G_TYPE_STRING);
775   g_value_set_string (&nothing, "");
776
777   emp_cli_channel_type_file_call_accept_file (TP_PROXY (tp_file->priv->channel),
778       -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
779       &nothing, offset, tp_file_method_cb, tp_file, NULL, NULL);
780 }
781
782 /**
783  * empathy_tp_file_offer:
784  * @tp_file: an #EmpathyTpFile
785  *
786  * Offers a file transfer that's in the "not offered" state (i.e.
787  * EMP_FILE_TRANSFER_STATE_NOT_OFFERED).
788  */
789 void
790 empathy_tp_file_offer (EmpathyTpFile *tp_file)
791 {
792   GValue nothing = { 0 };
793
794   g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
795
796   g_value_init (&nothing, G_TYPE_STRING);
797   g_value_set_string (&nothing, "");
798
799   emp_cli_channel_type_file_call_offer_file (
800       TP_PROXY (tp_file->priv->channel), -1,
801       TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
802       &nothing, tp_file_method_cb, tp_file, NULL, NULL);
803 }
804
805 EmpathyContact *
806 empathy_tp_file_get_contact (EmpathyTpFile *tp_file)
807 {
808   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
809   return tp_file->priv->contact;
810 }
811
812 GInputStream *
813 empathy_tp_file_get_input_stream (EmpathyTpFile *tp_file)
814 {
815   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
816   return tp_file->priv->in_stream;
817 }
818
819 GOutputStream *
820 empathy_tp_file_get_output_stream (EmpathyTpFile *tp_file)
821 {
822   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
823   return tp_file->priv->out_stream;
824 }
825
826 const gchar *
827 empathy_tp_file_get_filename (EmpathyTpFile *tp_file)
828 {
829   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
830   return tp_file->priv->filename;
831 }
832
833 gboolean
834 empathy_tp_file_get_incoming (EmpathyTpFile *tp_file)
835 {
836   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), FALSE);
837   return tp_file->priv->incoming;
838 }
839
840 EmpFileTransferState
841 empathy_tp_file_get_state (EmpathyTpFile *tp_file)
842 {
843   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file),
844       EMP_FILE_TRANSFER_STATE_NONE);
845   return tp_file->priv->state;
846 }
847
848 EmpFileTransferStateChangeReason
849 empathy_tp_file_get_state_change_reason (EmpathyTpFile *tp_file)
850 {
851   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file),
852       EMP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE);
853
854   return tp_file->priv->state_change_reason;
855 }
856
857 guint64
858 empathy_tp_file_get_size (EmpathyTpFile *tp_file)
859 {
860   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file),
861       EMPATHY_TP_FILE_UNKNOWN_SIZE);
862   return tp_file->priv->size;
863 }
864
865 guint64
866 empathy_tp_file_get_transferred_bytes (EmpathyTpFile *tp_file)
867 {
868   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), 0);
869   return tp_file->priv->transferred_bytes;
870 }
871
872 gint
873 empathy_tp_file_get_remaining_time (EmpathyTpFile *tp_file)
874 {
875   time_t curr_time, elapsed_time;
876   gdouble time_per_byte;
877   gdouble remaining_time;
878
879   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), -1);
880
881   if (tp_file->priv->size == EMPATHY_TP_FILE_UNKNOWN_SIZE)
882     return -1;
883
884   if (tp_file->priv->transferred_bytes == tp_file->priv->size)
885     return 0;
886
887   curr_time = empathy_time_get_current ();
888   elapsed_time = curr_time - tp_file->priv->start_time;
889   time_per_byte = (gdouble) elapsed_time /
890       (gdouble) tp_file->priv->transferred_bytes;
891   remaining_time = time_per_byte * (tp_file->priv->size -
892       tp_file->priv->transferred_bytes);
893
894   return (gint) remaining_time;
895 }
896
897 void
898 empathy_tp_file_cancel (EmpathyTpFile *tp_file)
899 {
900   g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
901
902   tp_cli_channel_call_close (tp_file->priv->channel, -1, NULL, NULL, NULL, NULL);
903
904   g_cancellable_cancel (tp_file->priv->cancellable);
905 }
906
907 void
908 empathy_tp_file_set_input_stream (EmpathyTpFile *tp_file,
909                                   GInputStream *in_stream)
910 {
911   g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
912   g_return_if_fail (G_IS_INPUT_STREAM (in_stream));
913
914   if (tp_file->priv->in_stream == in_stream)
915     return;
916
917   if (tp_file->priv->incoming)
918     g_warning ("Setting an input stream for incoming file "
919          "transfers is useless");
920
921   if (tp_file->priv->in_stream)
922     g_object_unref (tp_file->priv->in_stream);
923
924   if (in_stream)
925     g_object_ref (in_stream);
926
927   tp_file->priv->in_stream = in_stream;
928
929   g_object_notify (G_OBJECT (tp_file), "in-stream");
930 }
931
932 void
933 empathy_tp_file_set_output_stream (EmpathyTpFile *tp_file,
934                                    GOutputStream *out_stream)
935 {
936   g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
937   g_return_if_fail (G_IS_OUTPUT_STREAM (out_stream));
938
939   if (tp_file->priv->out_stream == out_stream)
940     return;
941
942   if (!tp_file->priv->incoming)
943     g_warning ("Setting an output stream for outgoing file "
944          "transfers is useless");
945
946   if (tp_file->priv->out_stream)
947     g_object_unref (tp_file->priv->out_stream);
948
949   if (out_stream)
950     g_object_ref (out_stream);
951
952   tp_file->priv->out_stream = out_stream;
953 }
954
955 void
956 empathy_tp_file_set_filename (EmpathyTpFile *tp_file,
957                               const gchar *filename)
958 {
959   g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
960   g_return_if_fail (filename != NULL);
961
962   if (tp_file->priv->filename && strcmp (filename,
963       tp_file->priv->filename) == 0)
964     return;
965
966   g_free (tp_file->priv->filename);
967   tp_file->priv->filename = g_strdup (filename);
968
969   g_object_notify (G_OBJECT (tp_file), "filename");
970 }
971
972 static void
973 empathy_tp_file_class_init (EmpathyTpFileClass *klass)
974 {
975   GObjectClass *object_class = G_OBJECT_CLASS (klass);
976
977   object_class->finalize = tp_file_finalize;
978   object_class->constructor = tp_file_constructor;
979   object_class->get_property = tp_file_get_property;
980   object_class->set_property = tp_file_set_property;
981
982   /* Construct-only properties */
983   g_object_class_install_property (object_class,
984       PROP_CHANNEL,
985       g_param_spec_object ("channel",
986           "telepathy channel",
987           "The file transfer channel",
988           TP_TYPE_CHANNEL,
989           G_PARAM_READWRITE |
990           G_PARAM_CONSTRUCT_ONLY));
991
992   g_object_class_install_property (object_class,
993       PROP_STATE,
994       g_param_spec_uint ("state",
995           "state of the transfer",
996           "The file transfer state",
997           0,
998           G_MAXUINT,
999           G_MAXUINT,
1000           G_PARAM_READWRITE |
1001           G_PARAM_CONSTRUCT));
1002
1003   g_object_class_install_property (object_class,
1004       PROP_INCOMING,
1005       g_param_spec_boolean ("incoming",
1006           "incoming",
1007           "Whether the transfer is incoming",
1008           FALSE,
1009           G_PARAM_READWRITE |
1010           G_PARAM_CONSTRUCT));
1011
1012   g_object_class_install_property (object_class,
1013       PROP_FILENAME,
1014       g_param_spec_string ("filename",
1015           "name of the transfer",
1016           "The file transfer filename",
1017           "",
1018           G_PARAM_READWRITE));
1019
1020   g_object_class_install_property (object_class,
1021       PROP_SIZE,
1022       g_param_spec_uint64 ("size",
1023           "size of the file",
1024           "The file transfer size",
1025           0,
1026           G_MAXUINT64,
1027           G_MAXUINT64,
1028           G_PARAM_READWRITE));
1029
1030   g_object_class_install_property (object_class,
1031       PROP_CONTENT_TYPE,
1032       g_param_spec_string ("content-type",
1033           "file transfer content-type",
1034           "The file transfer content-type",
1035           "",
1036           G_PARAM_READWRITE));
1037
1038   g_object_class_install_property (object_class,
1039       PROP_CONTENT_HASH_TYPE,
1040       g_param_spec_uint ("content-hash-type",
1041           "file transfer hash type",
1042           "The type of the file transfer hash",
1043           0,
1044           G_MAXUINT,
1045           0,
1046           G_PARAM_READWRITE));
1047
1048   g_object_class_install_property (object_class,
1049       PROP_CONTENT_HASH,
1050       g_param_spec_string ("content-hash",
1051           "file transfer hash",
1052           "The hash of the transfer's contents",
1053           "",
1054           G_PARAM_READWRITE));
1055
1056   g_object_class_install_property (object_class,
1057       PROP_TRANSFERRED_BYTES,
1058       g_param_spec_uint64 ("transferred-bytes",
1059           "bytes transferred",
1060           "The number of bytes transferred",
1061           0,
1062           G_MAXUINT64,
1063           0,
1064           G_PARAM_READWRITE));
1065
1066   g_object_class_install_property (object_class,
1067       PROP_IN_STREAM,
1068       g_param_spec_object ("in-stream",
1069           "transfer input stream",
1070           "The input stream for file transfer",
1071           G_TYPE_INPUT_STREAM,
1072           G_PARAM_READWRITE));
1073
1074   g_type_class_add_private (object_class, sizeof (EmpathyTpFilePriv));
1075 }