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