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