]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-file.c
rename empathy_tp_file_get_incoming to empathy_tp_file_is_incoming
[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 <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 #define DEBUG_FLAG EMPATHY_DEBUG_FT
47 #include "empathy-debug.h"
48
49 /**
50  * SECTION:empathy-tp-file
51  * @short_description: File channel
52  * @see_also: #EmpathyTpFile, #EmpathyContact, empathy_dispatcher_send_file()
53  * @include: libempthy/empathy-tp-file.h
54  *
55  * The #EmpathyTpFile object represents a Telepathy file channel.
56  */
57
58 /**
59  * EMPATHY_TP_FILE_UNKNOWN_SIZE:
60  *
61  * Value used for the "size" or "estimated-size" properties when the size of
62  * the transferred file is unknown.
63  */
64
65 /* Functions to copy the content of a GInputStream to a GOutputStream */
66
67 #define N_BUFFERS 2
68 #define BUFFER_SIZE 4096
69
70 typedef struct {
71   GInputStream *in;
72   GOutputStream *out;
73   GCancellable  *cancellable;
74   char *buff[N_BUFFERS]; /* the temporary buffers */
75   gsize count[N_BUFFERS]; /* how many bytes are used in the buffers */
76   gboolean is_full[N_BUFFERS]; /* whether the buffers contain data */
77   gint curr_read; /* index of the buffer used for reading */
78   gint curr_write; /* index of the buffer used for writing */
79   gboolean is_reading; /* we are reading */
80   gboolean is_writing; /* we are writing */
81   guint n_closed; /* number of streams that have been closed */
82   gint ref_count;
83 } CopyData;
84
85 static void schedule_next (CopyData *copy);
86
87 static void
88 copy_data_unref (CopyData *copy)
89 {
90   if (--copy->ref_count == 0)
91     {
92       gint i;
93
94       /* Free the data only if both the input and output streams have
95        * been closed. */
96       copy->n_closed++;
97       if (copy->n_closed < 2)
98         return;
99
100       if (copy->in != NULL)
101         g_object_unref (copy->in);
102
103       if (copy->out != NULL)
104         g_object_unref (copy->out);
105
106       for (i = 0; i < N_BUFFERS; i++)
107         g_free (copy->buff[i]);
108
109       g_object_unref (copy->cancellable);
110       g_free (copy);
111     }
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   copy_data_unref (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   copy_data_unref (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   copy->ref_count = 1;
262
263   if (cancellable != NULL)
264     copy->cancellable = g_object_ref (cancellable);
265   else
266     copy->cancellable = g_cancellable_new ();
267
268   for (i = 0; i < N_BUFFERS; i++)
269     copy->buff[i] = g_malloc (BUFFER_SIZE);
270
271   schedule_next (copy);
272 }
273
274 /* EmpathyTpFile object */
275
276 struct _EmpathyTpFilePriv {
277   EmpathyContactFactory *factory;
278   gchar *id;
279   MissionControl *mc;
280   TpChannel *channel;
281
282   EmpathyContact *contact;
283   GInputStream *in_stream;
284   GOutputStream *out_stream;
285
286   /* org.freedesktop.Telepathy.Channel.Type.FileTransfer D-Bus properties */
287   EmpFileTransferState state;
288   gchar *content_type;
289   gchar *filename;
290   guint64 size;
291   EmpFileHashType content_hash_type;
292   gchar *content_hash;
293   gchar *description;
294   guint64 transferred_bytes;
295
296   gboolean incoming;
297   EmpFileTransferStateChangeReason state_change_reason;
298   time_t start_time;
299   gchar *unix_socket_path;
300   GCancellable *cancellable;
301 };
302
303 enum {
304   PROP_0,
305   PROP_CHANNEL,
306   PROP_STATE,
307   PROP_INCOMING,
308   PROP_FILENAME,
309   PROP_SIZE,
310   PROP_CONTENT_TYPE,
311   PROP_TRANSFERRED_BYTES,
312   PROP_CONTENT_HASH_TYPE,
313   PROP_CONTENT_HASH,
314 };
315
316 G_DEFINE_TYPE (EmpathyTpFile, empathy_tp_file, G_TYPE_OBJECT);
317
318 static void
319 empathy_tp_file_init (EmpathyTpFile *tp_file)
320 {
321   EmpathyTpFilePriv *priv;
322
323   priv = G_TYPE_INSTANCE_GET_PRIVATE ((tp_file),
324       EMPATHY_TYPE_TP_FILE, EmpathyTpFilePriv);
325
326   tp_file->priv = priv;
327 }
328
329 static void
330 tp_file_destroy_cb (TpChannel *file_channel,
331                     EmpathyTpFile *tp_file)
332 {
333   DEBUG ("Channel Closed or CM crashed");
334
335   g_object_unref (tp_file->priv->channel);
336   tp_file->priv->channel = NULL;
337 }
338
339 static void
340 tp_file_finalize (GObject *object)
341 {
342   EmpathyTpFile *tp_file;
343
344   tp_file = EMPATHY_TP_FILE (object);
345
346   if (tp_file->priv->channel)
347     {
348       DEBUG ("Closing channel..");
349       g_signal_handlers_disconnect_by_func (tp_file->priv->channel,
350           tp_file_destroy_cb, object);
351       tp_cli_channel_call_close (tp_file->priv->channel, -1, NULL, NULL,
352           NULL, NULL);
353       g_object_unref (tp_file->priv->channel);
354     }
355
356   if (tp_file->priv->factory)
357     {
358       g_object_unref (tp_file->priv->factory);
359     }
360   if (tp_file->priv->mc)
361     {
362       g_object_unref (tp_file->priv->mc);
363     }
364
365   g_free (tp_file->priv->id);
366   g_free (tp_file->priv->filename);
367   g_free (tp_file->priv->unix_socket_path);
368   g_free (tp_file->priv->description);
369   g_free (tp_file->priv->content_hash);
370   g_free (tp_file->priv->content_type);
371
372   if (tp_file->priv->in_stream)
373     g_object_unref (tp_file->priv->in_stream);
374
375   if (tp_file->priv->out_stream)
376     g_object_unref (tp_file->priv->out_stream);
377
378   if (tp_file->priv->contact)
379     g_object_unref (tp_file->priv->contact);
380
381   if (tp_file->priv->cancellable)
382     g_object_unref (tp_file->priv->cancellable);
383
384   G_OBJECT_CLASS (empathy_tp_file_parent_class)->finalize (object);
385 }
386
387 static void
388 tp_file_closed_cb (TpChannel *file_channel,
389                    EmpathyTpFile *tp_file,
390                    GObject *weak_object)
391 {
392   /* The channel is closed, do just like if the proxy was destroyed */
393   g_signal_handlers_disconnect_by_func (tp_file->priv->channel,
394       tp_file_destroy_cb,
395       tp_file);
396   tp_file_destroy_cb (file_channel, tp_file);
397 }
398
399 static gint
400 _get_local_socket (EmpathyTpFile *tp_file)
401 {
402   gint fd;
403   size_t path_len;
404   struct sockaddr_un addr;
405
406   if (G_STR_EMPTY (tp_file->priv->unix_socket_path))
407     return -1;
408
409   fd = socket (PF_UNIX, SOCK_STREAM, 0);
410   if (fd < 0)
411     return -1;
412
413   memset (&addr, 0, sizeof (addr));
414   addr.sun_family = AF_UNIX;
415   path_len = strlen (tp_file->priv->unix_socket_path);
416   strncpy (addr.sun_path, tp_file->priv->unix_socket_path, path_len);
417
418   if (connect (fd, (struct sockaddr*) &addr,
419       sizeof (addr)) < 0)
420     {
421       close (fd);
422       return -1;
423     }
424
425   return fd;
426 }
427
428 static void
429 send_tp_file (EmpathyTpFile *tp_file)
430 {
431   gint socket_fd;
432   GOutputStream *socket_stream;
433
434   DEBUG ("Sending file content: filename=%s",
435       tp_file->priv->filename);
436
437   g_return_if_fail (tp_file->priv->in_stream);
438
439   socket_fd = _get_local_socket (tp_file);
440   if (socket_fd < 0)
441     {
442       DEBUG ("failed to get local socket fd");
443       return;
444     }
445   DEBUG ("got local socket fd");
446   socket_stream = g_unix_output_stream_new (socket_fd, TRUE);
447
448   tp_file->priv->cancellable = g_cancellable_new ();
449
450   copy_stream (tp_file->priv->in_stream, socket_stream,
451       tp_file->priv->cancellable);
452
453   g_object_unref (socket_stream);
454 }
455
456 static void
457 receive_tp_file (EmpathyTpFile *tp_file)
458 {
459   GInputStream *socket_stream;
460   gint socket_fd;
461
462   socket_fd = _get_local_socket (tp_file);
463
464   if (socket_fd < 0)
465     return;
466
467   socket_stream = g_unix_input_stream_new (socket_fd, TRUE);
468
469   tp_file->priv->cancellable = g_cancellable_new ();
470
471   copy_stream (socket_stream, tp_file->priv->out_stream,
472       tp_file->priv->cancellable);
473
474   g_object_unref (socket_stream);
475 }
476
477 static void
478 tp_file_state_changed_cb (DBusGProxy *tp_file_iface,
479                           EmpFileTransferState state,
480                           EmpFileTransferStateChangeReason reason,
481                           EmpathyTpFile *tp_file)
482 {
483   DEBUG ("File transfer state changed: filename=%s, "
484       "old state=%u, state=%u, reason=%u",
485       tp_file->priv->filename, tp_file->priv->state, state, reason);
486
487   if (state == EMP_FILE_TRANSFER_STATE_OPEN)
488     tp_file->priv->start_time = empathy_time_get_current ();
489
490   DEBUG ("state = %u, incoming = %s, in_stream = %s, out_stream = %s",
491       state, tp_file->priv->incoming ? "yes" : "no",
492       tp_file->priv->in_stream ? "present" : "not present",
493       tp_file->priv->out_stream ? "present" : "not present");
494
495   if (state == EMP_FILE_TRANSFER_STATE_OPEN && !tp_file->priv->incoming &&
496       tp_file->priv->in_stream)
497     send_tp_file (tp_file);
498   else if (state == EMP_FILE_TRANSFER_STATE_OPEN && tp_file->priv->incoming &&
499       tp_file->priv->out_stream)
500     receive_tp_file (tp_file);
501
502   tp_file->priv->state = state;
503   tp_file->priv->state_change_reason = reason;
504
505   g_object_notify (G_OBJECT (tp_file), "state");
506 }
507
508 static void
509 tp_file_transferred_bytes_changed_cb (TpProxy *proxy,
510                                       guint64 count,
511                                       EmpathyTpFile *tp_file,
512                                       GObject *weak_object)
513 {
514   if (tp_file->priv->transferred_bytes == count)
515     return;
516
517   tp_file->priv->transferred_bytes = count;
518
519   g_object_notify (G_OBJECT (tp_file), "transferred-bytes");
520 }
521
522 static GObject *
523 tp_file_constructor (GType type,
524                      guint n_props,
525                      GObjectConstructParam *props)
526 {
527   GObject *file_obj;
528   EmpathyTpFile *tp_file;
529   TpHandle handle;
530   GHashTable *properties;
531   McAccount *account;
532   GValue *requested;
533
534   file_obj = G_OBJECT_CLASS (empathy_tp_file_parent_class)->constructor (type,
535       n_props, props);
536
537   tp_file = EMPATHY_TP_FILE (file_obj);
538
539   tp_file->priv->factory = empathy_contact_factory_new ();
540   tp_file->priv->mc = empathy_mission_control_new ();
541
542   tp_cli_channel_connect_to_closed (tp_file->priv->channel,
543       (tp_cli_channel_signal_callback_closed) tp_file_closed_cb,
544       tp_file,
545       NULL, NULL, NULL);
546
547   emp_cli_channel_type_file_transfer_connect_to_file_transfer_state_changed (
548       TP_PROXY (tp_file->priv->channel),
549       (emp_cli_channel_type_file_transfer_signal_callback_file_transfer_state_changed)
550           tp_file_state_changed_cb,
551       tp_file,
552       NULL, NULL, NULL);
553
554   emp_cli_channel_type_file_transfer_connect_to_transferred_bytes_changed (
555       TP_PROXY (tp_file->priv->channel),
556       (emp_cli_channel_type_file_transfer_signal_callback_transferred_bytes_changed)
557           tp_file_transferred_bytes_changed_cb,
558       tp_file,
559       NULL, NULL, NULL);
560
561   account = empathy_channel_get_account (tp_file->priv->channel);
562
563   handle = tp_channel_get_handle (tp_file->priv->channel, NULL);
564   tp_file->priv->contact = empathy_contact_factory_get_from_handle (
565       tp_file->priv->factory, account, (guint) handle);
566
567   tp_cli_dbus_properties_run_get_all (tp_file->priv->channel,
568       -1, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, &properties, NULL, NULL);
569
570   tp_cli_dbus_properties_run_get (tp_file->priv->channel,
571       -1, TP_IFACE_CHANNEL, "Requested", &requested, NULL, NULL);
572
573   tp_file->priv->size = g_value_get_uint64 (
574       g_hash_table_lookup (properties, "Size"));
575
576   tp_file->priv->state = g_value_get_uint (
577       g_hash_table_lookup (properties, "State"));
578
579   tp_file->priv->state_change_reason =
580       EMP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE;
581
582   tp_file->priv->transferred_bytes = g_value_get_uint64 (
583       g_hash_table_lookup (properties, "TransferredBytes"));
584
585   tp_file->priv->filename = g_value_dup_string (
586       g_hash_table_lookup (properties, "Filename"));
587
588   tp_file->priv->content_hash = g_value_dup_string (
589       g_hash_table_lookup (properties, "ContentHash"));
590
591   tp_file->priv->content_hash_type = g_value_get_uint (
592       g_hash_table_lookup (properties, "ContentHashType"));
593
594   tp_file->priv->content_type = g_value_dup_string (
595       g_hash_table_lookup (properties, "ContentType"));
596
597   tp_file->priv->description = g_value_dup_string (
598       g_hash_table_lookup (properties, "Description"));
599
600   tp_file->priv->incoming = !g_value_get_boolean (requested);
601
602   g_hash_table_destroy (properties);
603   g_object_unref (account);
604   g_value_unset (requested);
605
606   return file_obj;
607 }
608
609 static void
610 tp_file_get_property (GObject *object,
611                       guint param_id,
612                       GValue *value,
613                       GParamSpec *pspec)
614 {
615   EmpathyTpFile *tp_file;
616
617   tp_file = EMPATHY_TP_FILE (object);
618
619   switch (param_id)
620     {
621       case PROP_CHANNEL:
622         g_value_set_object (value, tp_file->priv->channel);
623         break;
624       case PROP_INCOMING:
625         g_value_set_boolean (value, tp_file->priv->incoming);
626         break;
627       case PROP_STATE:
628         g_value_set_uint (value, tp_file->priv->state);
629         break;
630       case PROP_CONTENT_TYPE:
631         g_value_set_string (value, tp_file->priv->content_type);
632         break;
633       case PROP_FILENAME:
634         g_value_set_string (value, tp_file->priv->filename);
635         break;
636       case PROP_SIZE:
637         g_value_set_uint64 (value, tp_file->priv->size);
638         break;
639       case PROP_CONTENT_HASH_TYPE:
640         g_value_set_uint (value, tp_file->priv->content_hash_type);
641         break;
642       case PROP_CONTENT_HASH:
643         g_value_set_string (value, tp_file->priv->content_hash);
644         break;
645       case PROP_TRANSFERRED_BYTES:
646         g_value_set_uint64 (value, tp_file->priv->transferred_bytes);
647         break;
648       default:
649         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
650         break;
651     };
652 }
653
654 static void
655 tp_file_channel_set_dbus_property (gpointer proxy,
656                                    const gchar *property,
657                                    const GValue *value)
658 {
659         DEBUG ("Setting %s property", property);
660         tp_cli_dbus_properties_call_set (TP_PROXY (proxy), -1,
661             EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, property, value,
662             NULL, NULL, NULL, NULL);
663 }
664
665
666 static void
667 tp_file_set_property (GObject *object,
668                       guint param_id,
669                       const GValue *value,
670                       GParamSpec *pspec)
671 {
672   EmpathyTpFile *tp_file = (EmpathyTpFile *) object;
673   switch (param_id)
674     {
675       case PROP_CHANNEL:
676         tp_file->priv->channel = g_object_ref (g_value_get_object (value));
677         break;
678       case PROP_STATE:
679         tp_file->priv->state = g_value_get_uint (value);
680         break;
681       case PROP_INCOMING:
682         tp_file->priv->incoming = g_value_get_boolean (value);
683         break;
684       case PROP_FILENAME:
685         g_free (tp_file->priv->filename);
686         tp_file->priv->filename = g_value_dup_string (value);
687         tp_file_channel_set_dbus_property (tp_file->priv->channel,
688             "Filename", value);
689         break;
690       case PROP_SIZE:
691         tp_file->priv->size = g_value_get_uint64 (value);
692         tp_file_channel_set_dbus_property (tp_file->priv->channel,
693             "Size", value);
694         break;
695       case PROP_CONTENT_TYPE:
696         tp_file_channel_set_dbus_property (tp_file->priv->channel,
697             "ContentType", value);
698         g_free (tp_file->priv->content_type);
699         tp_file->priv->content_type = g_value_dup_string (value);
700         break;
701       case PROP_CONTENT_HASH:
702         tp_file_channel_set_dbus_property (tp_file->priv->channel,
703             "ContentHash", value);
704         g_free (tp_file->priv->content_hash);
705         tp_file->priv->content_hash = g_value_dup_string (value);
706         break;
707       default:
708         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
709         break;
710     };
711 }
712
713 /**
714  * empathy_tp_file_new:
715  * @channel: a Telepathy channel
716  *
717  * Creates a new #EmpathyTpFile wrapping @channel.
718  *
719  * Returns: a new #EmpathyTpFile
720  */
721 EmpathyTpFile *
722 empathy_tp_file_new (TpChannel *channel)
723 {
724   g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
725
726   return g_object_new (EMPATHY_TYPE_TP_FILE,
727       "channel", channel,
728       NULL);
729 }
730
731 /**
732  * empathy_tp_file_get_id:
733  * @tp_file: an #EmpathyTpFile
734  *
735  * Returns the ID of @tp_file.
736  *
737  * Returns: the ID
738  */
739 const gchar *
740 empathy_tp_file_get_id (EmpathyTpFile *tp_file)
741 {
742   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
743
744   return tp_file->priv->id;
745 }
746
747 /**
748  * empathy_tp_file_get_channel
749  * @tp_file: an #EmpathyTpFile
750  *
751  * Returns the Telepathy file transfer channel
752  *
753  * Returns: the #TpChannel
754  */
755 TpChannel *
756 empathy_tp_file_get_channel (EmpathyTpFile *tp_file)
757 {
758   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
759
760   return tp_file->priv->channel;
761 }
762
763 static void
764 tp_file_method_cb (TpProxy *proxy,
765                    const GValue *address,
766                    const GError *error,
767                    gpointer user_data,
768                    GObject *weak_object)
769 {
770   EmpathyTpFile *tp_file = (EmpathyTpFile *) weak_object;
771
772   if (error)
773     {
774       DEBUG ("Error: %s", error->message);
775       return;
776     }
777
778   if (tp_file->priv->unix_socket_path)
779     g_free (tp_file->priv->unix_socket_path);
780
781   tp_file->priv->unix_socket_path = g_value_dup_string (address);
782
783   DEBUG ("Got unix socket path: %s", tp_file->priv->unix_socket_path);
784 }
785
786
787 /**
788  * empathy_tp_file_accept:
789  * @tp_file: an #EmpathyTpFile
790  * @offset: position where to start the transfer
791  * @gfile: a #GFile where to write transfered data
792  * @error: a #GError set if there is an error when opening @gfile
793  *
794  * Accepts a file transfer that's in the "local pending" state (i.e.
795  * EMP_FILE_TRANSFER_STATE_LOCAL_PENDING).
796  */
797 void
798 empathy_tp_file_accept (EmpathyTpFile *tp_file,
799                         guint64 offset,
800                         GFile *gfile,
801                         GError **error)
802 {
803   GValue nothing = { 0 };
804
805   g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
806   g_return_if_fail (G_IS_FILE (gfile));
807
808   tp_file->priv->out_stream = G_OUTPUT_STREAM (g_file_replace (gfile, NULL,
809         FALSE, 0, NULL, error));
810   if (error && *error)
811     return;
812
813   tp_file->priv->filename = g_file_get_basename (gfile);
814   g_object_notify (G_OBJECT (tp_file), "filename");
815
816   DEBUG ("Accepting file: filename=%s", tp_file->priv->filename);
817
818   g_value_init (&nothing, G_TYPE_STRING);
819   g_value_set_static_string (&nothing, "");
820
821   emp_cli_channel_type_file_transfer_call_accept_file (TP_PROXY (
822       tp_file->priv->channel),
823       -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
824       &nothing, offset, tp_file_method_cb, NULL, NULL, G_OBJECT (tp_file));
825 }
826
827 /**
828  * empathy_tp_file_offer:
829  * @tp_file: an #EmpathyTpFile
830  * @gfile: a #GFile where to read the data to transfer
831  * @error: a #GError set if there is an error when opening @gfile
832  *
833  * Offers a file transfer that's in the "not offered" state (i.e.
834  * EMP_FILE_TRANSFER_STATE_NOT_OFFERED).
835  */
836 void
837 empathy_tp_file_offer (EmpathyTpFile *tp_file, GFile *gfile, GError **error)
838 {
839   GValue nothing = { 0 };
840
841   g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
842
843   tp_file->priv->in_stream = G_INPUT_STREAM (g_file_read (gfile, NULL, error));
844   if (error && *error)
845         return;
846
847   g_value_init (&nothing, G_TYPE_STRING);
848   g_value_set_static_string (&nothing, "");
849
850   emp_cli_channel_type_file_transfer_call_provide_file (
851       TP_PROXY (tp_file->priv->channel), -1,
852       TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
853       &nothing, tp_file_method_cb, NULL, NULL, G_OBJECT (tp_file));
854 }
855
856 EmpathyContact *
857 empathy_tp_file_get_contact (EmpathyTpFile *tp_file)
858 {
859   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
860   return tp_file->priv->contact;
861 }
862
863 const gchar *
864 empathy_tp_file_get_filename (EmpathyTpFile *tp_file)
865 {
866   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
867   return tp_file->priv->filename;
868 }
869
870 gboolean
871 empathy_tp_file_is_incoming (EmpathyTpFile *tp_file)
872 {
873   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), FALSE);
874   return tp_file->priv->incoming;
875 }
876
877 EmpFileTransferState
878 empathy_tp_file_get_state (EmpathyTpFile *tp_file)
879 {
880   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file),
881       EMP_FILE_TRANSFER_STATE_NONE);
882   return tp_file->priv->state;
883 }
884
885 EmpFileTransferStateChangeReason
886 empathy_tp_file_get_state_change_reason (EmpathyTpFile *tp_file)
887 {
888   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file),
889       EMP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE);
890
891   return tp_file->priv->state_change_reason;
892 }
893
894 guint64
895 empathy_tp_file_get_size (EmpathyTpFile *tp_file)
896 {
897   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file),
898       EMPATHY_TP_FILE_UNKNOWN_SIZE);
899   return tp_file->priv->size;
900 }
901
902 guint64
903 empathy_tp_file_get_transferred_bytes (EmpathyTpFile *tp_file)
904 {
905   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), 0);
906   return tp_file->priv->transferred_bytes;
907 }
908
909 gint
910 empathy_tp_file_get_remaining_time (EmpathyTpFile *tp_file)
911 {
912   time_t curr_time, elapsed_time;
913   gdouble time_per_byte;
914   gdouble remaining_time;
915
916   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), -1);
917
918   if (tp_file->priv->size == EMPATHY_TP_FILE_UNKNOWN_SIZE)
919     return -1;
920
921   if (tp_file->priv->transferred_bytes == tp_file->priv->size)
922     return 0;
923
924   curr_time = empathy_time_get_current ();
925   elapsed_time = curr_time - tp_file->priv->start_time;
926   time_per_byte = (gdouble) elapsed_time /
927       (gdouble) tp_file->priv->transferred_bytes;
928   remaining_time = time_per_byte * (tp_file->priv->size -
929       tp_file->priv->transferred_bytes);
930
931   return (gint) remaining_time;
932 }
933
934 void
935 empathy_tp_file_cancel (EmpathyTpFile *tp_file)
936 {
937   g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
938
939   tp_cli_channel_call_close (tp_file->priv->channel, -1, NULL, NULL, NULL, NULL);
940
941   g_cancellable_cancel (tp_file->priv->cancellable);
942 }
943
944 static void
945 empathy_tp_file_class_init (EmpathyTpFileClass *klass)
946 {
947   GObjectClass *object_class = G_OBJECT_CLASS (klass);
948
949   object_class->finalize = tp_file_finalize;
950   object_class->constructor = tp_file_constructor;
951   object_class->get_property = tp_file_get_property;
952   object_class->set_property = tp_file_set_property;
953
954   /* Construct-only properties */
955   g_object_class_install_property (object_class,
956       PROP_CHANNEL,
957       g_param_spec_object ("channel",
958           "telepathy channel",
959           "The file transfer channel",
960           TP_TYPE_CHANNEL,
961           G_PARAM_READWRITE |
962           G_PARAM_CONSTRUCT_ONLY));
963
964   g_object_class_install_property (object_class,
965       PROP_STATE,
966       g_param_spec_uint ("state",
967           "state of the transfer",
968           "The file transfer state",
969           0,
970           G_MAXUINT,
971           G_MAXUINT,
972           G_PARAM_READWRITE |
973           G_PARAM_CONSTRUCT));
974
975   g_object_class_install_property (object_class,
976       PROP_INCOMING,
977       g_param_spec_boolean ("incoming",
978           "incoming",
979           "Whether the transfer is incoming",
980           FALSE,
981           G_PARAM_READWRITE |
982           G_PARAM_CONSTRUCT));
983
984   g_object_class_install_property (object_class,
985       PROP_FILENAME,
986       g_param_spec_string ("filename",
987           "name of the transfer",
988           "The file transfer filename",
989           "",
990           G_PARAM_READWRITE));
991
992   g_object_class_install_property (object_class,
993       PROP_SIZE,
994       g_param_spec_uint64 ("size",
995           "size of the file",
996           "The file transfer size",
997           0,
998           G_MAXUINT64,
999           G_MAXUINT64,
1000           G_PARAM_READWRITE));
1001
1002   g_object_class_install_property (object_class,
1003       PROP_CONTENT_TYPE,
1004       g_param_spec_string ("content-type",
1005           "file transfer content-type",
1006           "The file transfer content-type",
1007           "",
1008           G_PARAM_READWRITE));
1009
1010   g_object_class_install_property (object_class,
1011       PROP_CONTENT_HASH_TYPE,
1012       g_param_spec_uint ("content-hash-type",
1013           "file transfer hash type",
1014           "The type of the file transfer hash",
1015           0,
1016           G_MAXUINT,
1017           0,
1018           G_PARAM_READWRITE));
1019
1020   g_object_class_install_property (object_class,
1021       PROP_CONTENT_HASH,
1022       g_param_spec_string ("content-hash",
1023           "file transfer hash",
1024           "The hash of the transfer's contents",
1025           "",
1026           G_PARAM_READWRITE));
1027
1028   g_object_class_install_property (object_class,
1029       PROP_TRANSFERRED_BYTES,
1030       g_param_spec_uint64 ("transferred-bytes",
1031           "bytes transferred",
1032           "The number of bytes transferred",
1033           0,
1034           G_MAXUINT64,
1035           0,
1036           G_PARAM_READWRITE));
1037
1038   g_type_class_add_private (object_class, sizeof (EmpathyTpFilePriv));
1039 }