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