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