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