]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-file.c
Merge branch 'spelling-578399'
[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-lib.h>
33
34 #include <gio/gio.h>
35 #include <gio/gunixinputstream.h>
36 #include <gio/gunixoutputstream.h>
37
38 #include <telepathy-glib/proxy-subclass.h>
39 #include <telepathy-glib/util.h>
40
41 #include "empathy-tp-file.h"
42 #include "empathy-tp-contact-factory.h"
43 #include "empathy-marshal.h"
44 #include "empathy-time.h"
45 #include "empathy-utils.h"
46
47 #include "extensions/extensions.h"
48
49 #define DEBUG_FLAG EMPATHY_DEBUG_FT
50 #include "empathy-debug.h"
51
52 /**
53  * SECTION:empathy-tp-file
54  * @short_description: File channel
55  * @see_also: #EmpathyTpFile, #EmpathyContact, empathy_dispatcher_send_file()
56  * @include: libempthy/empathy-tp-file.h
57  *
58  * The #EmpathyTpFile object represents a Telepathy file channel.
59  */
60
61 /**
62  * EMPATHY_TP_FILE_UNKNOWN_SIZE:
63  *
64  * Value used for the "size" or "estimated-size" properties when the size of
65  * the transferred file is unknown.
66  */
67
68 /* Functions to copy the content of a GInputStream to a GOutputStream */
69
70 #define N_BUFFERS 2
71 #define BUFFER_SIZE 4096
72
73 typedef struct {
74   GInputStream *in;
75   GOutputStream *out;
76   GCancellable  *cancellable;
77   char *buff[N_BUFFERS]; /* the temporary buffers */
78   gsize count[N_BUFFERS]; /* how many bytes are used in the buffers */
79   gboolean is_full[N_BUFFERS]; /* whether the buffers contain data */
80   gint curr_read; /* index of the buffer used for reading */
81   gint curr_write; /* index of the buffer used for writing */
82   gboolean is_reading; /* we are reading */
83   gboolean is_writing; /* we are writing */
84   guint n_closed; /* number of streams that have been closed */
85   gint ref_count;
86 } CopyData;
87
88 static void schedule_next (CopyData *copy);
89
90 static void
91 copy_data_unref (CopyData *copy)
92 {
93   if (--copy->ref_count == 0)
94     {
95       gint i;
96
97       /* Free the data only if both the input and output streams have
98        * been closed. */
99       copy->n_closed++;
100       if (copy->n_closed < 2)
101         return;
102
103       if (copy->in != NULL)
104         g_object_unref (copy->in);
105
106       if (copy->out != NULL)
107         g_object_unref (copy->out);
108
109       for (i = 0; i < N_BUFFERS; i++)
110         g_free (copy->buff[i]);
111
112       g_object_unref (copy->cancellable);
113       g_free (copy);
114     }
115 }
116
117 static void
118 io_error (CopyData *copy,
119           GError *error)
120 {
121   g_cancellable_cancel (copy->cancellable);
122
123   if (error == NULL)
124     g_warning ("I/O error");
125   else if (error->domain == G_IO_ERROR && error->code == G_IO_ERROR_CANCELLED)
126     ; /* Ignore cancellations */
127   else
128     g_warning ("I/O error: %d: %s\n", error->code, error->message);
129
130   if (copy->in != NULL)
131     g_input_stream_close (copy->in, NULL, NULL);
132
133   if (copy->out != NULL)
134     g_output_stream_close (copy->out, NULL, NULL);
135
136   copy_data_unref (copy);
137 }
138
139 static void
140 close_done (GObject *source_object,
141             GAsyncResult *res,
142             gpointer user_data)
143 {
144   CopyData *copy = user_data;
145
146   g_object_unref (source_object);
147   copy_data_unref (copy);
148 }
149
150 static void
151 write_done_cb (GObject *source_object,
152                GAsyncResult *res,
153                gpointer user_data)
154 {
155   CopyData *copy = user_data;
156   gssize count_write;
157   GError *error = NULL;
158
159   count_write = g_output_stream_write_finish (copy->out, res, &error);
160
161   if (count_write <= 0)
162     {
163       io_error (copy, error);
164       g_error_free (error);
165       return;
166     }
167
168   copy->is_full[copy->curr_write] = FALSE;
169   copy->curr_write = (copy->curr_write + 1) % N_BUFFERS;
170   copy->is_writing = FALSE;
171
172   schedule_next (copy);
173 }
174
175 static void
176 read_done_cb (GObject *source_object,
177               GAsyncResult *res,
178               gpointer user_data)
179 {
180   CopyData *copy = user_data;
181   gssize count_read;
182   GError *error = NULL;
183
184   count_read = g_input_stream_read_finish (copy->in, res, &error);
185
186   if (count_read == 0)
187     {
188       g_input_stream_close_async (copy->in, 0, copy->cancellable,
189           close_done, copy);
190       copy->in = NULL;
191     }
192   else if (count_read < 0)
193     {
194       io_error (copy, error);
195       g_error_free (error);
196       return;
197     }
198
199   copy->count[copy->curr_read] = count_read;
200   copy->is_full[copy->curr_read] = TRUE;
201   copy->curr_read = (copy->curr_read + 1) % N_BUFFERS;
202   copy->is_reading = FALSE;
203
204   schedule_next (copy);
205 }
206
207 static void
208 schedule_next (CopyData *copy)
209 {
210   if (copy->in != NULL &&
211       !copy->is_reading &&
212       !copy->is_full[copy->curr_read])
213     {
214       /* We are not reading and the current buffer is empty, so
215        * start an async read. */
216       copy->is_reading = TRUE;
217       g_input_stream_read_async (copy->in,
218           copy->buff[copy->curr_read],
219           BUFFER_SIZE, 0, copy->cancellable,
220           read_done_cb, copy);
221     }
222
223   if (!copy->is_writing &&
224       copy->is_full[copy->curr_write])
225     {
226       if (copy->count[copy->curr_write] == 0)
227         {
228           /* The last read on the buffer read 0 bytes, this
229            * means that we got an EOF, so we can close
230            * the output channel. */
231           g_output_stream_close_async (copy->out, 0,
232               copy->cancellable,
233               close_done, copy);
234       copy->out = NULL;
235         }
236       else
237         {
238           /* We are not writing and the current buffer contains
239            * data, so start an async write. */
240           copy->is_writing = TRUE;
241           g_output_stream_write_async (copy->out,
242               copy->buff[copy->curr_write],
243               copy->count[copy->curr_write],
244               0, copy->cancellable,
245               write_done_cb, copy);
246         }
247     }
248 }
249
250 static void
251 copy_stream (GInputStream *in,
252              GOutputStream *out,
253              GCancellable *cancellable)
254 {
255   CopyData *copy;
256   gint i;
257
258   g_return_if_fail (in != NULL);
259   g_return_if_fail (out != NULL);
260
261   copy = g_new0 (CopyData, 1);
262   copy->in = g_object_ref (in);
263   copy->out = g_object_ref (out);
264   copy->ref_count = 1;
265
266   if (cancellable != NULL)
267     copy->cancellable = g_object_ref (cancellable);
268   else
269     copy->cancellable = g_cancellable_new ();
270
271   for (i = 0; i < N_BUFFERS; i++)
272     copy->buff[i] = g_malloc (BUFFER_SIZE);
273
274   schedule_next (copy);
275 }
276
277 /* EmpathyTpFile object */
278
279 struct _EmpathyTpFilePriv {
280   EmpathyTpContactFactory *factory;
281   MissionControl *mc;
282   TpChannel *channel;
283   gboolean ready;
284
285   EmpathyContact *contact;
286   GInputStream *in_stream;
287   GOutputStream *out_stream;
288
289   /* org.freedesktop.Telepathy.Channel.Type.FileTransfer D-Bus properties */
290   TpFileTransferState state;
291   gchar *content_type;
292   gchar *filename;
293   guint64 size;
294   TpFileHashType content_hash_type;
295   gchar *content_hash;
296   gchar *description;
297   guint64 transferred_bytes;
298
299   gboolean incoming;
300   TpFileTransferStateChangeReason state_change_reason;
301   time_t start_time;
302   GValue *socket_address;
303   GCancellable *cancellable;
304 };
305
306 enum {
307   PROP_0,
308   PROP_CHANNEL,
309   PROP_STATE,
310   PROP_INCOMING,
311   PROP_READY,
312   PROP_FILENAME,
313   PROP_SIZE,
314   PROP_CONTENT_TYPE,
315   PROP_TRANSFERRED_BYTES,
316   PROP_CONTENT_HASH_TYPE,
317   PROP_CONTENT_HASH,
318 };
319
320 G_DEFINE_TYPE (EmpathyTpFile, empathy_tp_file, G_TYPE_OBJECT);
321
322 static void
323 empathy_tp_file_init (EmpathyTpFile *tp_file)
324 {
325   EmpathyTpFilePriv *priv;
326
327   priv = G_TYPE_INSTANCE_GET_PRIVATE ((tp_file),
328       EMPATHY_TYPE_TP_FILE, EmpathyTpFilePriv);
329
330   tp_file->priv = priv;
331 }
332
333 static void
334 tp_file_invalidated_cb (TpProxy       *proxy,
335                         guint          domain,
336                         gint           code,
337                         gchar         *message,
338                         EmpathyTpFile *tp_file)
339 {
340   DEBUG ("Channel invalidated: %s", message);
341
342   if (tp_file->priv->state != TP_FILE_TRANSFER_STATE_COMPLETED &&
343       tp_file->priv->state != TP_FILE_TRANSFER_STATE_CANCELLED)
344     {
345       /* The channel is not in a finished state, an error occured */
346       tp_file->priv->state = TP_FILE_TRANSFER_STATE_CANCELLED;
347       tp_file->priv->state_change_reason =
348           TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_ERROR;
349       g_object_notify (G_OBJECT (tp_file), "state");
350     }
351 }
352
353 static void
354 tp_file_finalize (GObject *object)
355 {
356   EmpathyTpFile *tp_file = EMPATHY_TP_FILE (object);
357
358   if (tp_file->priv->channel)
359     {
360       g_signal_handlers_disconnect_by_func (tp_file->priv->channel,
361           tp_file_invalidated_cb, object);
362       g_object_unref (tp_file->priv->channel);
363       tp_file->priv->channel = NULL;
364     }
365
366   if (tp_file->priv->factory)
367     {
368       g_object_unref (tp_file->priv->factory);
369     }
370   if (tp_file->priv->mc)
371     {
372       g_object_unref (tp_file->priv->mc);
373     }
374
375   g_free (tp_file->priv->filename);
376   if (tp_file->priv->socket_address != NULL)
377     tp_g_value_slice_free (tp_file->priv->socket_address);
378   g_free (tp_file->priv->description);
379   g_free (tp_file->priv->content_hash);
380   g_free (tp_file->priv->content_type);
381
382   if (tp_file->priv->in_stream)
383     g_object_unref (tp_file->priv->in_stream);
384
385   if (tp_file->priv->out_stream)
386     g_object_unref (tp_file->priv->out_stream);
387
388   if (tp_file->priv->contact)
389     g_object_unref (tp_file->priv->contact);
390
391   if (tp_file->priv->cancellable)
392     g_object_unref (tp_file->priv->cancellable);
393
394   G_OBJECT_CLASS (empathy_tp_file_parent_class)->finalize (object);
395 }
396
397 static void
398 tp_file_start_transfer (EmpathyTpFile *tp_file)
399 {
400   gint fd;
401   struct sockaddr_un addr;
402   GArray *array;
403
404   fd = socket (PF_UNIX, SOCK_STREAM, 0);
405   if (fd < 0)
406     {
407       DEBUG ("Failed to create socket, closing channel");
408       empathy_tp_file_cancel (tp_file);
409       return;
410     }
411
412   array = g_value_get_boxed (tp_file->priv->socket_address);
413
414   memset (&addr, 0, sizeof (addr));
415   addr.sun_family = AF_UNIX;
416   strncpy (addr.sun_path, array->data, array->len);
417
418   if (connect (fd, (struct sockaddr*) &addr, sizeof (addr)) < 0)
419     {
420       DEBUG ("Failed to connect socket, closing channel");
421       empathy_tp_file_cancel (tp_file);
422       close (fd);
423       return;
424     }
425
426   DEBUG ("Start the transfer");
427
428   tp_file->priv->start_time = empathy_time_get_current ();
429   tp_file->priv->cancellable = g_cancellable_new ();
430   if (tp_file->priv->incoming)
431     {
432       GInputStream *socket_stream;
433
434       socket_stream = g_unix_input_stream_new (fd, TRUE);
435       copy_stream (socket_stream, tp_file->priv->out_stream,
436           tp_file->priv->cancellable);
437       g_object_unref (socket_stream);
438     }
439   else
440     {
441       GOutputStream *socket_stream;
442
443       socket_stream = g_unix_output_stream_new (fd, TRUE);
444       copy_stream (tp_file->priv->in_stream, socket_stream,
445           tp_file->priv->cancellable);
446       g_object_unref (socket_stream);
447     }
448 }
449
450 static void
451 tp_file_state_changed_cb (TpChannel *channel,
452                           guint state,
453                           guint reason,
454                           gpointer user_data,
455                           GObject *weak_object)
456 {
457   EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
458
459   if (state == tp_file->priv->state)
460     return;
461
462   DEBUG ("File transfer state changed:\n"
463       "\tfilename = %s, old state = %u, state = %u, reason = %u\n"
464       "\tincoming = %s, in_stream = %s, out_stream = %s",
465       tp_file->priv->filename, tp_file->priv->state, state, reason,
466       tp_file->priv->incoming ? "yes" : "no",
467       tp_file->priv->in_stream ? "present" : "not present",
468       tp_file->priv->out_stream ? "present" : "not present");
469
470   /* If the channel is open AND we have the socket path, we can start the
471    * transfer. The socket path could be NULL if we are not doing the actual
472    * data transfer but are just an observer for the channel. */
473   if (state == TP_FILE_TRANSFER_STATE_OPEN &&
474       tp_file->priv->socket_address != NULL)
475     tp_file_start_transfer (tp_file);
476
477   tp_file->priv->state = state;
478   tp_file->priv->state_change_reason = reason;
479
480   g_object_notify (G_OBJECT (tp_file), "state");
481 }
482
483 static void
484 tp_file_transferred_bytes_changed_cb (TpChannel *channel,
485                                       guint64 count,
486                                       gpointer user_data,
487                                       GObject *weak_object)
488 {
489   EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
490
491   if (tp_file->priv->transferred_bytes == count)
492     return;
493
494   tp_file->priv->transferred_bytes = count;
495   g_object_notify (G_OBJECT (tp_file), "transferred-bytes");
496 }
497
498 static void
499 tp_file_check_if_ready (EmpathyTpFile *tp_file)
500 {
501   if (tp_file->priv->ready || tp_file->priv->contact == NULL ||
502       tp_file->priv->state == 0)
503     return;
504
505   tp_file->priv->ready = TRUE;
506   g_object_notify (G_OBJECT (tp_file), "ready");
507 }
508
509 static void
510 tp_file_got_contact_cb (EmpathyTpContactFactory *factory,
511                         EmpathyContact *contact,
512                         const GError *error,
513                         gpointer user_data,
514                         GObject *weak_object)
515 {
516   EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
517
518   if (error)
519     {
520       DEBUG ("Error: %s", error->message);
521       empathy_tp_file_close (tp_file);
522       return;
523     }
524
525   tp_file->priv->contact = g_object_ref (contact);
526   tp_file_check_if_ready (tp_file);
527 }
528
529 static void
530 tp_file_get_all_cb (TpProxy *proxy,
531                     GHashTable *properties,
532                     const GError *error,
533                     gpointer user_data,
534                     GObject *file_obj)
535 {
536   EmpathyTpFile *tp_file = EMPATHY_TP_FILE (file_obj);
537
538   if (error)
539     {
540       DEBUG ("Error: %s", error->message);
541       tp_cli_channel_call_close (tp_file->priv->channel, -1, NULL, NULL, NULL,
542           NULL);
543       return;
544     }
545
546   tp_file->priv->size = g_value_get_uint64 (
547       g_hash_table_lookup (properties, "Size"));
548   g_object_notify (file_obj, "size");
549
550   tp_file->priv->state = g_value_get_uint (
551       g_hash_table_lookup (properties, "State"));
552   g_object_notify (file_obj, "state");
553
554   tp_file->priv->transferred_bytes = g_value_get_uint64 (
555       g_hash_table_lookup (properties, "TransferredBytes"));
556   g_object_notify (file_obj, "transferred-bytes");
557
558   tp_file->priv->filename = g_value_dup_string (
559       g_hash_table_lookup (properties, "Filename"));
560   g_object_notify (file_obj, "filename");
561
562   tp_file->priv->content_hash = g_value_dup_string (
563       g_hash_table_lookup (properties, "ContentHash"));
564   g_object_notify (file_obj, "content-hash");
565
566   tp_file->priv->content_hash_type = g_value_get_uint (
567       g_hash_table_lookup (properties, "ContentHashType"));
568   g_object_notify (file_obj, "content-hash-type");
569
570   tp_file->priv->content_type = g_value_dup_string (
571       g_hash_table_lookup (properties, "ContentType"));
572   g_object_notify (file_obj, "content-type");
573
574   tp_file->priv->description = g_value_dup_string (
575       g_hash_table_lookup (properties, "Description"));
576
577   tp_file_check_if_ready (tp_file);
578 }
579
580 static void
581 tp_file_get_requested_cb (TpProxy *proxy,
582                           const GValue *requested,
583                           const GError *error,
584                           gpointer user_data,
585                           GObject *weak_object)
586 {
587   EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
588
589   if (error)
590     {
591       DEBUG ("Error: %s", error->message);
592       tp_cli_channel_call_close (tp_file->priv->channel, -1, NULL, NULL, NULL,
593           NULL);
594       return;
595     }
596
597   tp_file->priv->incoming = !g_value_get_boolean (requested);
598   g_object_notify (G_OBJECT (tp_file), "incoming");
599
600   tp_file_check_if_ready (tp_file);
601 }
602
603 static GObject *
604 tp_file_constructor (GType type,
605                      guint n_props,
606                      GObjectConstructParam *props)
607 {
608   GObject *file_obj;
609   EmpathyTpFile *tp_file;
610   TpHandle handle;
611   TpConnection *connection;
612
613   file_obj = G_OBJECT_CLASS (empathy_tp_file_parent_class)->constructor (type,
614       n_props, props);
615
616   tp_file = EMPATHY_TP_FILE (file_obj);
617
618   connection = tp_channel_borrow_connection (tp_file->priv->channel);
619   tp_file->priv->factory = empathy_tp_contact_factory_dup_singleton (connection);
620   tp_file->priv->mc = empathy_mission_control_dup_singleton ();
621   tp_file->priv->state_change_reason =
622       TP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE;
623
624   g_signal_connect (tp_file->priv->channel, "invalidated",
625     G_CALLBACK (tp_file_invalidated_cb), tp_file);
626
627   tp_cli_channel_type_file_transfer_connect_to_file_transfer_state_changed (
628       tp_file->priv->channel, tp_file_state_changed_cb, NULL, NULL,
629       G_OBJECT (tp_file), NULL);
630
631   tp_cli_channel_type_file_transfer_connect_to_transferred_bytes_changed (
632       tp_file->priv->channel, tp_file_transferred_bytes_changed_cb,
633       NULL, NULL, G_OBJECT (tp_file), NULL);
634
635   tp_cli_dbus_properties_call_get (tp_file->priv->channel, -1,
636       TP_IFACE_CHANNEL, "Requested",
637       tp_file_get_requested_cb, NULL, NULL, file_obj);
638
639   tp_cli_dbus_properties_call_get_all (tp_file->priv->channel, -1,
640       TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
641       tp_file_get_all_cb, NULL, NULL, file_obj);
642
643   handle = tp_channel_get_handle (tp_file->priv->channel, NULL);
644   empathy_tp_contact_factory_get_from_handle (tp_file->priv->factory,
645       handle, tp_file_got_contact_cb, NULL, NULL, file_obj);
646
647   return file_obj;
648 }
649
650 static void
651 tp_file_get_property (GObject *object,
652                       guint param_id,
653                       GValue *value,
654                       GParamSpec *pspec)
655 {
656   EmpathyTpFile *tp_file;
657
658   tp_file = EMPATHY_TP_FILE (object);
659
660   switch (param_id)
661     {
662       case PROP_CHANNEL:
663         g_value_set_object (value, tp_file->priv->channel);
664         break;
665       case PROP_INCOMING:
666         g_value_set_boolean (value, tp_file->priv->incoming);
667         break;
668       case PROP_STATE:
669         g_value_set_uint (value, tp_file->priv->state);
670         break;
671       case PROP_CONTENT_TYPE:
672         g_value_set_string (value, tp_file->priv->content_type);
673         break;
674       case PROP_FILENAME:
675         g_value_set_string (value, tp_file->priv->filename);
676         break;
677       case PROP_SIZE:
678         g_value_set_uint64 (value, tp_file->priv->size);
679         break;
680       case PROP_CONTENT_HASH_TYPE:
681         g_value_set_uint (value, tp_file->priv->content_hash_type);
682         break;
683       case PROP_CONTENT_HASH:
684         g_value_set_string (value, tp_file->priv->content_hash);
685         break;
686       case PROP_TRANSFERRED_BYTES:
687         g_value_set_uint64 (value, tp_file->priv->transferred_bytes);
688         break;
689       default:
690         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
691         break;
692     };
693 }
694
695 static void
696 tp_file_channel_set_dbus_property (gpointer proxy,
697                                    const gchar *property,
698                                    const GValue *value)
699 {
700         DEBUG ("Setting %s property", property);
701         tp_cli_dbus_properties_call_set (TP_PROXY (proxy), -1,
702             TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, property, value,
703             NULL, NULL, NULL, NULL);
704 }
705
706 static void
707 tp_file_set_property (GObject *object,
708                       guint param_id,
709                       const GValue *value,
710                       GParamSpec *pspec)
711 {
712   EmpathyTpFile *tp_file = (EmpathyTpFile *) object;
713   switch (param_id)
714     {
715       case PROP_CHANNEL:
716         tp_file->priv->channel = g_object_ref (g_value_get_object (value));
717         break;
718       case PROP_STATE:
719         tp_file->priv->state = g_value_get_uint (value);
720         break;
721       case PROP_INCOMING:
722         tp_file->priv->incoming = g_value_get_boolean (value);
723         break;
724       case PROP_FILENAME:
725         g_free (tp_file->priv->filename);
726         tp_file->priv->filename = g_value_dup_string (value);
727         tp_file_channel_set_dbus_property (tp_file->priv->channel,
728             "Filename", value);
729         break;
730       case PROP_SIZE:
731         tp_file->priv->size = g_value_get_uint64 (value);
732         tp_file_channel_set_dbus_property (tp_file->priv->channel,
733             "Size", value);
734         break;
735       case PROP_CONTENT_TYPE:
736         tp_file_channel_set_dbus_property (tp_file->priv->channel,
737             "ContentType", value);
738         g_free (tp_file->priv->content_type);
739         tp_file->priv->content_type = g_value_dup_string (value);
740         break;
741       case PROP_CONTENT_HASH:
742         tp_file_channel_set_dbus_property (tp_file->priv->channel,
743             "ContentHash", value);
744         g_free (tp_file->priv->content_hash);
745         tp_file->priv->content_hash = g_value_dup_string (value);
746         break;
747       default:
748         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
749         break;
750     };
751 }
752
753 static GHashTable *ft_table = NULL;
754
755 static void
756 tp_file_weak_notify_cb (gpointer channel,
757                         GObject *tp_file)
758 {
759   g_hash_table_remove (ft_table, channel);
760 }
761
762 /**
763  * empathy_tp_file_new:
764  * @channel: a Telepathy channel
765  *
766  * Creates a new #EmpathyTpFile wrapping @channel, or return a new ref to an
767  * existing #EmpathyTpFile for that channel.
768  *
769  * Returns: a new #EmpathyTpFile
770  */
771 EmpathyTpFile *
772 empathy_tp_file_new (TpChannel *channel)
773 {
774   EmpathyTpFile *tp_file;
775
776   g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
777
778   if (ft_table != NULL)
779     {
780       tp_file = g_hash_table_lookup (ft_table, channel);
781       if (tp_file != NULL) {
782         return g_object_ref (tp_file);
783       }
784     }
785   else
786     ft_table = g_hash_table_new_full (empathy_proxy_hash,
787       empathy_proxy_equal, (GDestroyNotify) g_object_unref, NULL);
788
789   tp_file = g_object_new (EMPATHY_TYPE_TP_FILE,
790       "channel", channel,
791       NULL);
792
793   g_hash_table_insert (ft_table, g_object_ref (channel), tp_file);
794   g_object_weak_ref (G_OBJECT (tp_file), tp_file_weak_notify_cb, channel);
795
796   return tp_file;
797 }
798
799 /**
800  * empathy_tp_file_get_channel
801  * @tp_file: an #EmpathyTpFile
802  *
803  * Returns the Telepathy file transfer channel
804  *
805  * Returns: the #TpChannel
806  */
807 TpChannel *
808 empathy_tp_file_get_channel (EmpathyTpFile *tp_file)
809 {
810   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
811
812   return tp_file->priv->channel;
813 }
814
815 static void
816 tp_file_method_cb (TpChannel *channel,
817                    const GValue *address,
818                    const GError *error,
819                    gpointer user_data,
820                    GObject *weak_object)
821 {
822   EmpathyTpFile *tp_file = (EmpathyTpFile *) weak_object;
823   GArray *array;
824
825   if (error)
826     {
827       DEBUG ("Error: %s", error->message);
828       empathy_tp_file_cancel (tp_file);
829       return;
830     }
831
832   if (G_VALUE_TYPE (address) == DBUS_TYPE_G_UCHAR_ARRAY)
833     {
834       tp_file->priv->socket_address = tp_g_value_slice_dup (address);
835     }
836   else if (G_VALUE_TYPE (address) == G_TYPE_STRING)
837     {
838       /* Old bugged version of telepathy-salut used to store the address
839        * as a 's' instead of an 'ay' */
840       const gchar *path;
841
842       path = g_value_get_string (address);
843       array = g_array_sized_new (TRUE, FALSE, sizeof (gchar), strlen (path));
844       g_array_insert_vals (array, 0, path, strlen (path));
845
846       tp_file->priv->socket_address = tp_g_value_slice_new (
847           DBUS_TYPE_G_UCHAR_ARRAY);
848       g_value_set_boxed (tp_file->priv->socket_address, array);
849
850       g_array_free (array, TRUE);
851     }
852   else
853     {
854       DEBUG ("Wrong address type: %s", G_VALUE_TYPE_NAME (address));
855       empathy_tp_file_cancel (tp_file);
856       return;
857     }
858
859   array = g_value_get_boxed (tp_file->priv->socket_address);
860   DEBUG ("Got unix socket path: %s", array->data);
861
862   if (tp_file->priv->state == TP_FILE_TRANSFER_STATE_OPEN)
863     tp_file_start_transfer (tp_file);
864 }
865
866 /**
867  * empathy_tp_file_accept:
868  * @tp_file: an #EmpathyTpFile
869  * @offset: position where to start the transfer
870  * @gfile: a #GFile where to write transfered data
871  * @error: a #GError set if there is an error when opening @gfile
872  *
873  * Accepts a file transfer that's in the "local pending" state (i.e.
874  * TP_FILE_TRANSFER_STATE_LOCAL_PENDING).
875  */
876 void
877 empathy_tp_file_accept (EmpathyTpFile *tp_file,
878                         guint64 offset,
879                         GFile *gfile,
880                         GError **error)
881 {
882   GValue nothing = { 0 };
883
884   g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
885   g_return_if_fail (G_IS_FILE (gfile));
886
887   tp_file->priv->out_stream = G_OUTPUT_STREAM (g_file_replace (gfile, NULL,
888         FALSE, 0, NULL, error));
889   if (error && *error)
890     return;
891
892   g_free (tp_file->priv->filename);
893   tp_file->priv->filename = g_file_get_basename (gfile);
894   g_object_notify (G_OBJECT (tp_file), "filename");
895
896   DEBUG ("Accepting file: filename=%s", tp_file->priv->filename);
897
898   g_value_init (&nothing, G_TYPE_STRING);
899   g_value_set_static_string (&nothing, "");
900
901   tp_cli_channel_type_file_transfer_call_accept_file (tp_file->priv->channel,
902       -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
903       &nothing, offset, tp_file_method_cb, NULL, NULL, G_OBJECT (tp_file));
904 }
905
906 /**
907  * empathy_tp_file_offer:
908  * @tp_file: an #EmpathyTpFile
909  * @gfile: a #GFile where to read the data to transfer
910  * @error: a #GError set if there is an error when opening @gfile
911  *
912  * Offers a file transfer that's in the "not offered" state (i.e.
913  * TP_FILE_TRANSFER_STATE_NOT_OFFERED).
914  */
915 void
916 empathy_tp_file_offer (EmpathyTpFile *tp_file, GFile *gfile, GError **error)
917 {
918   GValue nothing = { 0 };
919
920   g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
921
922   tp_file->priv->in_stream = G_INPUT_STREAM (g_file_read (gfile, NULL, error));
923   if (error && *error)
924         return;
925
926   g_value_init (&nothing, G_TYPE_STRING);
927   g_value_set_static_string (&nothing, "");
928
929   tp_cli_channel_type_file_transfer_call_provide_file (tp_file->priv->channel,
930       -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
931       &nothing, tp_file_method_cb, NULL, NULL, G_OBJECT (tp_file));
932 }
933
934 EmpathyContact *
935 empathy_tp_file_get_contact (EmpathyTpFile *tp_file)
936 {
937   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
938   return tp_file->priv->contact;
939 }
940
941 const gchar *
942 empathy_tp_file_get_filename (EmpathyTpFile *tp_file)
943 {
944   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
945   return tp_file->priv->filename;
946 }
947
948 gboolean
949 empathy_tp_file_is_incoming (EmpathyTpFile *tp_file)
950 {
951   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), FALSE);
952   return tp_file->priv->incoming;
953 }
954
955 TpFileTransferState
956 empathy_tp_file_get_state (EmpathyTpFile *tp_file,
957                            TpFileTransferStateChangeReason *reason)
958 {
959   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file),
960       TP_FILE_TRANSFER_STATE_NONE);
961
962   if (reason != NULL)
963     *reason = tp_file->priv->state_change_reason;
964
965   return tp_file->priv->state;
966 }
967
968 guint64
969 empathy_tp_file_get_size (EmpathyTpFile *tp_file)
970 {
971   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file),
972       EMPATHY_TP_FILE_UNKNOWN_SIZE);
973   return tp_file->priv->size;
974 }
975
976 guint64
977 empathy_tp_file_get_transferred_bytes (EmpathyTpFile *tp_file)
978 {
979   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), 0);
980   return tp_file->priv->transferred_bytes;
981 }
982
983 gint
984 empathy_tp_file_get_remaining_time (EmpathyTpFile *tp_file)
985 {
986   time_t curr_time, elapsed_time;
987   gdouble time_per_byte;
988   gdouble remaining_time;
989
990   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), -1);
991
992   if (tp_file->priv->size == EMPATHY_TP_FILE_UNKNOWN_SIZE)
993     return -1;
994
995   if (tp_file->priv->transferred_bytes == tp_file->priv->size)
996     return 0;
997
998   curr_time = empathy_time_get_current ();
999   elapsed_time = curr_time - tp_file->priv->start_time;
1000   time_per_byte = (gdouble) elapsed_time /
1001       (gdouble) tp_file->priv->transferred_bytes;
1002   remaining_time = time_per_byte * (tp_file->priv->size -
1003       tp_file->priv->transferred_bytes);
1004
1005   return (gint) remaining_time;
1006 }
1007
1008 const gchar *
1009 empathy_tp_file_get_content_type (EmpathyTpFile *tp_file)
1010 {
1011   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
1012   return tp_file->priv->content_type;
1013 }
1014
1015 void
1016 empathy_tp_file_cancel (EmpathyTpFile *tp_file)
1017 {
1018   g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
1019
1020   DEBUG ("Closing channel..");
1021   tp_cli_channel_call_close (tp_file->priv->channel, -1,
1022     NULL, NULL, NULL, NULL);
1023
1024   if (tp_file->priv->cancellable != NULL)
1025     g_cancellable_cancel (tp_file->priv->cancellable);
1026 }
1027
1028 void
1029 empathy_tp_file_close (EmpathyTpFile *tp_file)
1030 {
1031   empathy_tp_file_cancel (tp_file);
1032 }
1033
1034 gboolean
1035 empathy_tp_file_is_ready (EmpathyTpFile *tp_file)
1036 {
1037   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), FALSE);
1038
1039   return tp_file->priv->ready;
1040 }
1041
1042 static void
1043 empathy_tp_file_class_init (EmpathyTpFileClass *klass)
1044 {
1045   GObjectClass *object_class = G_OBJECT_CLASS (klass);
1046
1047   object_class->finalize = tp_file_finalize;
1048   object_class->constructor = tp_file_constructor;
1049   object_class->get_property = tp_file_get_property;
1050   object_class->set_property = tp_file_set_property;
1051
1052   /* Construct-only properties */
1053   g_object_class_install_property (object_class,
1054       PROP_CHANNEL,
1055       g_param_spec_object ("channel",
1056           "telepathy channel",
1057           "The file transfer channel",
1058           TP_TYPE_CHANNEL,
1059           G_PARAM_READWRITE |
1060           G_PARAM_CONSTRUCT_ONLY));
1061
1062   g_object_class_install_property (object_class,
1063       PROP_STATE,
1064       g_param_spec_uint ("state",
1065           "state of the transfer",
1066           "The file transfer state",
1067           0,
1068           G_MAXUINT,
1069           G_MAXUINT,
1070           G_PARAM_READWRITE |
1071           G_PARAM_CONSTRUCT));
1072
1073   g_object_class_install_property (object_class,
1074       PROP_INCOMING,
1075       g_param_spec_boolean ("incoming",
1076           "incoming",
1077           "Whether the transfer is incoming",
1078           FALSE,
1079           G_PARAM_READWRITE |
1080           G_PARAM_CONSTRUCT));
1081
1082   g_object_class_install_property (object_class,
1083       PROP_READY,
1084       g_param_spec_boolean ("ready",
1085           "ready",
1086           "Whether the object is ready",
1087           FALSE,
1088           G_PARAM_READWRITE |
1089           G_PARAM_CONSTRUCT));
1090
1091   g_object_class_install_property (object_class,
1092       PROP_FILENAME,
1093       g_param_spec_string ("filename",
1094           "name of the transfer",
1095           "The file transfer filename",
1096           "",
1097           G_PARAM_READWRITE));
1098
1099   g_object_class_install_property (object_class,
1100       PROP_SIZE,
1101       g_param_spec_uint64 ("size",
1102           "size of the file",
1103           "The file transfer size",
1104           0,
1105           G_MAXUINT64,
1106           G_MAXUINT64,
1107           G_PARAM_READWRITE));
1108
1109   g_object_class_install_property (object_class,
1110       PROP_CONTENT_TYPE,
1111       g_param_spec_string ("content-type",
1112           "file transfer content-type",
1113           "The file transfer content-type",
1114           "",
1115           G_PARAM_READWRITE));
1116
1117   g_object_class_install_property (object_class,
1118       PROP_CONTENT_HASH_TYPE,
1119       g_param_spec_uint ("content-hash-type",
1120           "file transfer hash type",
1121           "The type of the file transfer hash",
1122           0,
1123           G_MAXUINT,
1124           0,
1125           G_PARAM_READWRITE));
1126
1127   g_object_class_install_property (object_class,
1128       PROP_CONTENT_HASH,
1129       g_param_spec_string ("content-hash",
1130           "file transfer hash",
1131           "The hash of the transfer's contents",
1132           "",
1133           G_PARAM_READWRITE));
1134
1135   g_object_class_install_property (object_class,
1136       PROP_TRANSFERRED_BYTES,
1137       g_param_spec_uint64 ("transferred-bytes",
1138           "bytes transferred",
1139           "The number of bytes transferred",
1140           0,
1141           G_MAXUINT64,
1142           0,
1143           G_PARAM_READWRITE));
1144
1145   g_type_class_add_private (object_class, sizeof (EmpathyTpFilePriv));
1146 }
1147