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