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