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