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