]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-file.c
Removed account property of EmpathyTpFile as it's only actually used once and easy...
[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   EmpathyTpFile *cached_empathy_file;
282   EmpathyContact *contact;
283   GInputStream *in_stream;
284   GOutputStream *out_stream;
285   gboolean incoming;
286   gchar *filename;
287   EmpFileTransferState state;
288   EmpFileTransferStateChangeReason state_change_reason;
289   guint64 size;
290   guint64 transferred_bytes;
291   gint64 start_time;
292   gchar *unix_socket_path;
293   gchar *content_hash;
294   EmpFileHashType content_hash_type;
295   gchar *content_type;
296   gchar *description;
297   GCancellable *cancellable;
298 };
299
300 enum {
301   PROP_0,
302   PROP_CHANNEL,
303   PROP_STATE,
304   PROP_INCOMING,
305   PROP_FILENAME,
306   PROP_SIZE,
307   PROP_CONTENT_TYPE,
308   PROP_TRANSFERRED_BYTES,
309   PROP_CONTENT_HASH_TYPE,
310   PROP_CONTENT_HASH,
311   PROP_IN_STREAM,
312 };
313
314 G_DEFINE_TYPE (EmpathyTpFile, empathy_tp_file, G_TYPE_OBJECT);
315
316 static void
317 empathy_tp_file_init (EmpathyTpFile *tp_file)
318 {
319   EmpathyTpFilePriv *priv;
320
321   priv = G_TYPE_INSTANCE_GET_PRIVATE ((tp_file),
322       EMPATHY_TYPE_TP_FILE, EmpathyTpFilePriv);
323
324   tp_file->priv = priv;
325 }
326
327 static void
328 tp_file_destroy_cb (TpChannel *file_channel,
329                     EmpathyTpFile *tp_file)
330 {
331   DEBUG ("Channel Closed or CM crashed");
332
333   g_object_unref (tp_file->priv->channel);
334   tp_file->priv->channel = NULL;
335 }
336
337 static void
338 tp_file_finalize (GObject *object)
339 {
340   EmpathyTpFile *tp_file;
341
342   tp_file = EMPATHY_TP_FILE (object);
343
344   if (tp_file->priv->channel)
345     {
346       DEBUG ("Closing channel..");
347       g_signal_handlers_disconnect_by_func (tp_file->priv->channel,
348           tp_file_destroy_cb, object);
349       tp_cli_channel_call_close (tp_file->priv->channel, -1, NULL, NULL,
350           NULL, NULL);
351       if (G_IS_OBJECT (tp_file->priv->channel))
352         g_object_unref (tp_file->priv->channel);
353     }
354
355   if (tp_file->priv->factory)
356     {
357       g_object_unref (tp_file->priv->factory);
358     }
359   if (tp_file->priv->mc)
360     {
361       g_object_unref (tp_file->priv->mc);
362     }
363
364   g_free (tp_file->priv->id);
365   g_free (tp_file->priv->filename);
366   g_free (tp_file->priv->unix_socket_path);
367   g_free (tp_file->priv->description);
368   g_free (tp_file->priv->content_hash);
369   g_free (tp_file->priv->content_type);
370
371   if (tp_file->priv->in_stream)
372     g_object_unref (tp_file->priv->in_stream);
373
374   if (tp_file->priv->out_stream)
375     g_object_unref (tp_file->priv->out_stream);
376
377   if (tp_file->priv->contact)
378     g_object_unref (tp_file->priv->contact);
379
380   if (tp_file->priv->cancellable)
381     g_object_unref (tp_file->priv->cancellable);
382
383   G_OBJECT_CLASS (empathy_tp_file_parent_class)->finalize (object);
384 }
385
386 static void
387 tp_file_closed_cb (TpChannel *file_channel,
388                    EmpathyTpFile *tp_file,
389                    GObject *weak_object)
390 {
391   /* The channel is closed, do just like if the proxy was destroyed */
392   g_signal_handlers_disconnect_by_func (tp_file->priv->channel,
393       tp_file_destroy_cb,
394       tp_file);
395   tp_file_destroy_cb (file_channel, tp_file);
396 }
397
398 static gint64
399 get_time_msec (void)
400 {
401   GTimeVal tv;
402
403   g_get_current_time (&tv);
404   return ((gint64) tv.tv_sec) * 1000 + tv.tv_usec / 1000;
405 }
406
407 static gint
408 _get_local_socket (EmpathyTpFile *tp_file)
409 {
410   gint fd;
411   size_t path_len;
412   struct sockaddr_un addr;
413
414   if (G_STR_EMPTY (tp_file->priv->unix_socket_path))
415     return -1;
416
417   fd = socket (PF_UNIX, SOCK_STREAM, 0);
418   if (fd < 0)
419     return -1;
420
421   memset (&addr, 0, sizeof (addr));
422   addr.sun_family = AF_UNIX;
423   path_len = strlen (tp_file->priv->unix_socket_path);
424   strncpy (addr.sun_path, tp_file->priv->unix_socket_path, path_len);
425
426   if (connect (fd, (struct sockaddr*) &addr,
427       sizeof (addr)) < 0)
428     {
429       close (fd);
430       return -1;
431     }
432
433   return fd;
434 }
435
436 static void
437 send_tp_file (EmpathyTpFile *tp_file)
438 {
439   gint socket_fd;
440   GOutputStream *socket_stream;
441
442   DEBUG ("Sending file content: filename=%s",
443       tp_file->priv->filename);
444
445   g_return_if_fail (tp_file->priv->in_stream);
446
447   socket_fd = _get_local_socket (tp_file);
448   if (socket_fd < 0)
449     {
450       DEBUG ("failed to get local socket fd");
451       return;
452     }
453   DEBUG ("got local socket fd");
454   socket_stream = g_unix_output_stream_new (socket_fd, TRUE);
455
456   tp_file->priv->cancellable = g_cancellable_new ();
457
458   copy_stream (tp_file->priv->in_stream, socket_stream,
459       tp_file->priv->cancellable);
460
461   g_object_unref (socket_stream);
462 }
463
464 static void
465 receive_tp_file (EmpathyTpFile *tp_file)
466 {
467   GInputStream *socket_stream;
468   gint socket_fd;
469
470   socket_fd = _get_local_socket (tp_file);
471
472   if (socket_fd < 0)
473     return;
474
475   socket_stream = g_unix_input_stream_new (socket_fd, TRUE);
476
477   tp_file->priv->cancellable = g_cancellable_new ();
478
479   copy_stream (socket_stream, tp_file->priv->out_stream,
480       tp_file->priv->cancellable);
481
482   g_object_unref (socket_stream);
483 }
484
485 static void
486 tp_file_state_changed_cb (DBusGProxy *tp_file_iface,
487                           EmpFileTransferState state,
488                           EmpFileTransferStateChangeReason reason,
489                           EmpathyTpFile *tp_file)
490 {
491   DEBUG ("File transfer state changed: filename=%s, "
492       "old state=%u, state=%u, reason=%u",
493       tp_file->priv->filename, tp_file->priv->state, state, reason);
494
495   if (state == EMP_FILE_TRANSFER_STATE_OPEN)
496     tp_file->priv->start_time = get_time_msec ();
497
498   DEBUG ("state = %u, incoming = %s, in_stream = %s, out_stream = %s",
499       state, tp_file->priv->incoming ? "yes" : "no",
500       tp_file->priv->in_stream ? "present" : "not present",
501       tp_file->priv->out_stream ? "present" : "not present");
502
503   if (state == EMP_FILE_TRANSFER_STATE_OPEN && !tp_file->priv->incoming &&
504       tp_file->priv->in_stream)
505     send_tp_file (tp_file);
506   else if (state == EMP_FILE_TRANSFER_STATE_OPEN && tp_file->priv->incoming &&
507       tp_file->priv->out_stream)
508     receive_tp_file (tp_file);
509
510   tp_file->priv->state = state;
511   tp_file->priv->state_change_reason = reason;
512
513   g_object_notify (G_OBJECT (tp_file), "state");
514 }
515
516 static void
517 tp_file_transferred_bytes_changed_cb (TpProxy *proxy,
518                                       guint64 count,
519                                       EmpathyTpFile *tp_file,
520                                       GObject *weak_object)
521 {
522   if (tp_file->priv->transferred_bytes == count)
523     return;
524
525   tp_file->priv->transferred_bytes = count;
526
527   g_object_notify (G_OBJECT (tp_file), "transferred-bytes");
528 }
529
530 static GObject *
531 tp_file_constructor (GType type,
532                      guint n_props,
533                      GObjectConstructParam *props)
534 {
535   GObject *file_obj;
536   EmpathyTpFile *tp_file;
537   TpHandle handle;
538   GHashTable *properties;
539   McAccount *account;
540
541   file_obj = G_OBJECT_CLASS (empathy_tp_file_parent_class)->constructor (type,
542       n_props, props);
543
544   tp_file = EMPATHY_TP_FILE (file_obj);
545
546   tp_file->priv->factory = empathy_contact_factory_new ();
547   tp_file->priv->mc = empathy_mission_control_new ();
548
549   tp_cli_channel_connect_to_closed (tp_file->priv->channel,
550       (tp_cli_channel_signal_callback_closed) tp_file_closed_cb,
551       tp_file,
552       NULL, NULL, NULL);
553
554   emp_cli_channel_type_file_connect_to_file_transfer_state_changed (
555       TP_PROXY (tp_file->priv->channel),
556       (emp_cli_channel_type_file_signal_callback_file_transfer_state_changed)
557           tp_file_state_changed_cb,
558       tp_file,
559       NULL, NULL, NULL);
560
561   emp_cli_channel_type_file_connect_to_transferred_bytes_changed (
562       TP_PROXY (tp_file->priv->channel),
563       (emp_cli_channel_type_file_signal_callback_transferred_bytes_changed)
564           tp_file_transferred_bytes_changed_cb,
565       tp_file,
566       NULL, NULL, NULL);
567
568   account = empathy_channel_get_account (tp_file->priv->channel);
569
570   handle = tp_channel_get_handle (tp_file->priv->channel, NULL);
571   tp_file->priv->contact = empathy_contact_factory_get_from_handle (
572       tp_file->priv->factory, account, (guint) handle);
573
574   tp_cli_dbus_properties_run_get_all (tp_file->priv->channel,
575       -1, EMP_IFACE_CHANNEL_TYPE_FILE, &properties, NULL, NULL);
576
577   tp_file->priv->size = g_value_get_uint64 (
578       g_hash_table_lookup (properties, "Size"));
579
580   tp_file->priv->state = g_value_get_uint (
581       g_hash_table_lookup (properties, "State"));
582
583   /* Invalid reason, so empathy_file_get_state_change_reason() can give
584    * a warning if called for a not closed file transfer. */
585   tp_file->priv->state_change_reason = -1;
586
587   tp_file->priv->transferred_bytes = g_value_get_uint64 (
588       g_hash_table_lookup (properties, "TransferredBytes"));
589
590   tp_file->priv->filename = g_value_dup_string (
591       g_hash_table_lookup (properties, "Filename"));
592
593   tp_file->priv->content_hash = g_value_dup_string (
594       g_hash_table_lookup (properties, "ContentHash"));
595
596   tp_file->priv->description = g_value_dup_string (
597       g_hash_table_lookup (properties, "Description"));
598
599   if (tp_file->priv->state == EMP_FILE_TRANSFER_STATE_LOCAL_PENDING)
600     tp_file->priv->incoming = TRUE;
601
602   g_hash_table_destroy (properties);
603   g_object_unref (account);
604
605   return file_obj;
606 }
607
608 static void
609 tp_file_get_property (GObject *object,
610                       guint param_id,
611                       GValue *value,
612                       GParamSpec *pspec)
613 {
614   EmpathyTpFile *tp_file;
615
616   tp_file = EMPATHY_TP_FILE (object);
617
618   switch (param_id)
619     {
620       case PROP_CHANNEL:
621         g_value_set_object (value, tp_file->priv->channel);
622         break;
623       default:
624         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
625         break;
626     };
627 }
628
629 static void
630 tp_file_channel_set_dbus_property (gpointer proxy,
631                                    const gchar *property,
632                                    const GValue *value)
633 {
634         DEBUG ("Setting %s property", property);
635         tp_cli_dbus_properties_call_set (TP_PROXY (proxy), -1,
636             EMP_IFACE_CHANNEL_TYPE_FILE, property, value,
637             NULL, NULL, NULL, NULL);
638 }
639
640
641 static void
642 tp_file_set_property (GObject *object,
643                       guint param_id,
644                       const GValue *value,
645                       GParamSpec *pspec)
646 {
647   EmpathyTpFile *tp_file = (EmpathyTpFile *) object;
648   switch (param_id)
649     {
650       case PROP_CHANNEL:
651         tp_file->priv->channel = g_object_ref (g_value_get_object (value));
652         break;
653       case PROP_STATE:
654         tp_file->priv->state = g_value_get_uint (value);
655         break;
656       case PROP_INCOMING:
657         tp_file->priv->incoming = g_value_get_boolean (value);
658         break;
659       case PROP_FILENAME:
660         g_free (tp_file->priv->filename);
661         tp_file->priv->filename = g_value_dup_string (value);
662         tp_file_channel_set_dbus_property (tp_file->priv->channel,
663             "Filename", value);
664         break;
665       case PROP_SIZE:
666         tp_file->priv->size = g_value_get_uint64 (value);
667         tp_file_channel_set_dbus_property (tp_file->priv->channel,
668             "Size", value);
669         break;
670       case PROP_CONTENT_TYPE:
671         tp_file_channel_set_dbus_property (tp_file->priv->channel,
672             "ContentType", value);
673         g_free (tp_file->priv->content_type);
674         tp_file->priv->content_type = g_value_dup_string (value);
675         break;
676       case PROP_CONTENT_HASH:
677         tp_file_channel_set_dbus_property (tp_file->priv->channel,
678             "ContentHash", value);
679         g_free (tp_file->priv->content_hash);
680         tp_file->priv->content_hash = g_value_dup_string (value);
681         break;
682       case PROP_IN_STREAM:
683         if (tp_file->priv->in_stream)
684           g_object_unref (tp_file->priv->in_stream);
685         tp_file->priv->in_stream = g_object_ref (g_value_get_object (value));
686         break;
687       default:
688         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
689         break;
690     };
691 }
692
693 /**
694  * empathy_tp_file_new:
695  * @channel: a Telepathy channel
696  *
697  * Creates a new #EmpathyTpFile wrapping @channel.
698  *
699  * Returns: a new #EmpathyTpFile
700  */
701 EmpathyTpFile *
702 empathy_tp_file_new (TpChannel *channel)
703 {
704   g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
705
706   return g_object_new (EMPATHY_TYPE_TP_FILE,
707       "channel", channel,
708       NULL);
709 }
710
711 /**
712  * empathy_tp_file_get_id:
713  * @tp_file: an #EmpathyTpFile
714  *
715  * Returns the ID of @tp_file.
716  *
717  * Returns: the ID
718  */
719 const gchar *
720 empathy_tp_file_get_id (EmpathyTpFile *tp_file)
721 {
722   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
723
724   return tp_file->priv->id;
725 }
726
727 /**
728  * empathy_tp_file_get_channel
729  * @tp_file: an #EmpathyTpFile
730  *
731  * Returns the Telepathy file transfer channel
732  *
733  * Returns: the #TpChannel
734  */
735 TpChannel *
736 empathy_tp_file_get_channel (EmpathyTpFile *tp_file)
737 {
738   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
739
740   return tp_file->priv->channel;
741 }
742
743 static void
744 tp_file_method_cb (TpProxy *proxy,
745                    const GValue *address,
746                    const GError *error,
747                    gpointer user_data,
748                    GObject *weak_object)
749 {
750   EmpathyTpFile *tp_file = (EmpathyTpFile *) user_data;
751
752   if (error)
753     {
754       DEBUG ("Error: %s", error->message);
755       return;
756     }
757
758   if (tp_file->priv->unix_socket_path)
759     g_free (tp_file->priv->unix_socket_path);
760
761   tp_file->priv->unix_socket_path = g_value_dup_string (address);
762
763   DEBUG ("Got unix socket path: %s", tp_file->priv->unix_socket_path);
764 }
765
766
767 /**
768  * empathy_tp_file_accept:
769  * @tp_file: an #EmpathyTpFile
770  *
771  * Accepts a file transfer that's in the "local pending" state (i.e.
772  * EMP_FILE_TRANSFER_STATE_LOCAL_PENDING).
773  */
774 void
775 empathy_tp_file_accept (EmpathyTpFile *tp_file,
776                         guint64 offset)
777 {
778   GValue nothing = { 0 };
779
780   g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
781
782   g_return_if_fail (tp_file->priv->out_stream != NULL);
783
784   DEBUG ("Accepting file: filename=%s", tp_file->priv->filename);
785
786   g_value_init (&nothing, G_TYPE_STRING);
787   g_value_set_string (&nothing, "");
788
789   emp_cli_channel_type_file_call_accept_file (TP_PROXY (tp_file->priv->channel),
790       -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
791       &nothing, offset, tp_file_method_cb, tp_file, NULL, NULL);
792 }
793
794 /**
795  * empathy_tp_file_offer:
796  * @tp_file: an #EmpathyTpFile
797  *
798  * Offers a file transfer that's in the "not offered" state (i.e.
799  * EMP_FILE_TRANSFER_STATE_NOT_OFFERED).
800  */
801 void
802 empathy_tp_file_offer (EmpathyTpFile *tp_file)
803 {
804   GValue nothing = { 0 };
805
806   g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
807
808   g_value_init (&nothing, G_TYPE_STRING);
809   g_value_set_string (&nothing, "");
810
811   emp_cli_channel_type_file_call_offer_file (
812       TP_PROXY (tp_file->priv->channel), -1,
813       TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
814       &nothing, tp_file_method_cb, tp_file, NULL, NULL);
815 }
816
817 EmpathyContact *
818 empathy_tp_file_get_contact (EmpathyTpFile *tp_file)
819 {
820   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
821   return tp_file->priv->contact;
822 }
823
824 GInputStream *
825 empathy_tp_file_get_input_stream (EmpathyTpFile *tp_file)
826 {
827   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
828   return tp_file->priv->in_stream;
829 }
830
831 GOutputStream *
832 empathy_tp_file_get_output_stream (EmpathyTpFile *tp_file)
833 {
834   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
835   return tp_file->priv->out_stream;
836 }
837
838 const gchar *
839 empathy_tp_file_get_filename (EmpathyTpFile *tp_file)
840 {
841   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
842   return tp_file->priv->filename;
843 }
844
845 gboolean
846 empathy_tp_file_get_incoming (EmpathyTpFile *tp_file)
847 {
848   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), FALSE);
849   return tp_file->priv->incoming;
850 }
851
852 EmpFileTransferState
853 empathy_tp_file_get_state (EmpathyTpFile *tp_file)
854 {
855   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file),
856       EMP_FILE_TRANSFER_STATE_NONE);
857   return tp_file->priv->state;
858 }
859
860 EmpFileTransferStateChangeReason
861 empathy_tp_file_get_state_change_reason (EmpathyTpFile *tp_file)
862 {
863   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file),
864       EMP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE);
865   g_return_val_if_fail (tp_file->priv->state_change_reason >= 0,
866       EMP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE);
867
868   return tp_file->priv->state_change_reason;
869 }
870
871 guint64
872 empathy_tp_file_get_size (EmpathyTpFile *tp_file)
873 {
874   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file),
875       EMPATHY_TP_FILE_UNKNOWN_SIZE);
876   return tp_file->priv->size;
877 }
878
879 guint64
880 empathy_tp_file_get_transferred_bytes (EmpathyTpFile *tp_file)
881 {
882   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), 0);
883   return tp_file->priv->transferred_bytes;
884 }
885
886 gint
887 empathy_tp_file_get_remaining_time (EmpathyTpFile *tp_file)
888 {
889   gint64 curr_time, elapsed_time;
890   gdouble time_per_byte;
891   gdouble remaining_time;
892
893   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), -1);
894
895   if (tp_file->priv->size == EMPATHY_TP_FILE_UNKNOWN_SIZE)
896     return -1;
897
898   if (tp_file->priv->transferred_bytes == tp_file->priv->size)
899     return 0;
900
901   curr_time = get_time_msec ();
902   elapsed_time = curr_time - tp_file->priv->start_time;
903   time_per_byte = (gdouble) elapsed_time /
904       (gdouble) tp_file->priv->transferred_bytes;
905   remaining_time = (time_per_byte * (tp_file->priv->size -
906       tp_file->priv->transferred_bytes)) / 1000;
907
908   return (gint) (remaining_time + 0.5);
909 }
910
911 void
912 empathy_tp_file_cancel (EmpathyTpFile *tp_file)
913 {
914   g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
915
916   tp_cli_channel_call_close (tp_file->priv->channel, -1, NULL, NULL, NULL, NULL);
917
918   g_cancellable_cancel (tp_file->priv->cancellable);
919 }
920
921 void
922 empathy_tp_file_set_input_stream (EmpathyTpFile *tp_file,
923                                   GInputStream *in_stream)
924 {
925   g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
926   g_return_if_fail (G_IS_INPUT_STREAM (in_stream));
927
928   if (tp_file->priv->in_stream == in_stream)
929     return;
930
931   if (tp_file->priv->incoming)
932     g_warning ("Setting an input stream for incoming file "
933          "transfers is useless");
934
935   if (tp_file->priv->in_stream)
936     g_object_unref (tp_file->priv->in_stream);
937
938   if (in_stream)
939     g_object_ref (in_stream);
940
941   tp_file->priv->in_stream = in_stream;
942
943   g_object_notify (G_OBJECT (tp_file), "in-stream");
944 }
945
946 void
947 empathy_tp_file_set_output_stream (EmpathyTpFile *tp_file,
948                                    GOutputStream *out_stream)
949 {
950   g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
951   g_return_if_fail (G_IS_OUTPUT_STREAM (out_stream));
952
953   if (tp_file->priv->out_stream == out_stream)
954     return;
955
956   if (!tp_file->priv->incoming)
957     g_warning ("Setting an output stream for outgoing file "
958          "transfers is useless");
959
960   if (tp_file->priv->out_stream)
961     g_object_unref (tp_file->priv->out_stream);
962
963   if (out_stream)
964     g_object_ref (out_stream);
965
966   tp_file->priv->out_stream = out_stream;
967 }
968
969 void
970 empathy_tp_file_set_filename (EmpathyTpFile *tp_file,
971                               const gchar *filename)
972 {
973   g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
974   g_return_if_fail (filename != NULL);
975
976   if (tp_file->priv->filename && strcmp (filename,
977       tp_file->priv->filename) == 0)
978     return;
979
980   g_free (tp_file->priv->filename);
981   tp_file->priv->filename = g_strdup (filename);
982
983   g_object_notify (G_OBJECT (tp_file), "filename");
984 }
985
986 static void
987 empathy_tp_file_class_init (EmpathyTpFileClass *klass)
988 {
989   GObjectClass *object_class = G_OBJECT_CLASS (klass);
990
991   object_class->finalize = tp_file_finalize;
992   object_class->constructor = tp_file_constructor;
993   object_class->get_property = tp_file_get_property;
994   object_class->set_property = tp_file_set_property;
995
996   /* Construct-only properties */
997   g_object_class_install_property (object_class,
998       PROP_CHANNEL,
999       g_param_spec_object ("channel",
1000           "telepathy channel",
1001           "The file transfer channel",
1002           TP_TYPE_CHANNEL,
1003           G_PARAM_READWRITE |
1004           G_PARAM_CONSTRUCT_ONLY));
1005
1006   g_object_class_install_property (object_class,
1007       PROP_STATE,
1008       g_param_spec_uint ("state",
1009           "state of the transfer",
1010           "The file transfer state",
1011           0,
1012           G_MAXUINT,
1013           G_MAXUINT,
1014           G_PARAM_READWRITE |
1015           G_PARAM_CONSTRUCT));
1016
1017   g_object_class_install_property (object_class,
1018       PROP_INCOMING,
1019       g_param_spec_boolean ("incoming",
1020           "incoming",
1021           "Whether the transfer is incoming",
1022           FALSE,
1023           G_PARAM_READWRITE |
1024           G_PARAM_CONSTRUCT));
1025
1026   g_object_class_install_property (object_class,
1027       PROP_FILENAME,
1028       g_param_spec_string ("filename",
1029           "name of the transfer",
1030           "The file transfer filename",
1031           "",
1032           G_PARAM_READWRITE));
1033
1034   g_object_class_install_property (object_class,
1035       PROP_SIZE,
1036       g_param_spec_uint64 ("size",
1037           "size of the file",
1038           "The file transfer size",
1039           0,
1040           G_MAXUINT64,
1041           G_MAXUINT64,
1042           G_PARAM_READWRITE));
1043
1044   g_object_class_install_property (object_class,
1045       PROP_CONTENT_TYPE,
1046       g_param_spec_string ("content-type",
1047           "file transfer content-type",
1048           "The file transfer content-type",
1049           "",
1050           G_PARAM_READWRITE));
1051
1052   g_object_class_install_property (object_class,
1053       PROP_CONTENT_HASH_TYPE,
1054       g_param_spec_uint ("content-hash-type",
1055           "file transfer hash type",
1056           "The type of the file transfer hash",
1057           0,
1058           G_MAXUINT,
1059           0,
1060           G_PARAM_READWRITE));
1061
1062   g_object_class_install_property (object_class,
1063       PROP_CONTENT_HASH,
1064       g_param_spec_string ("content-hash",
1065           "file transfer hash",
1066           "The hash of the transfer's contents",
1067           "",
1068           G_PARAM_READWRITE));
1069
1070   g_object_class_install_property (object_class,
1071       PROP_TRANSFERRED_BYTES,
1072       g_param_spec_uint64 ("transferred-bytes",
1073           "bytes transferred",
1074           "The number of bytes transferred",
1075           0,
1076           G_MAXUINT64,
1077           0,
1078           G_PARAM_READWRITE));
1079
1080   g_object_class_install_property (object_class,
1081       PROP_IN_STREAM,
1082       g_param_spec_object ("in-stream",
1083           "transfer input stream",
1084           "The input stream for file transfer",
1085           G_TYPE_INPUT_STREAM,
1086           G_PARAM_READWRITE));
1087
1088   g_type_class_add_private (object_class, sizeof (EmpathyTpFilePriv));
1089 }