From 6bab1e27908d283ca17d8ac09c7b61196b7025b9 Mon Sep 17 00:00:00 2001 From: Jonny Lamb Date: Fri, 21 Nov 2008 16:13:47 +0000 Subject: Added empathy_send_file* helper functions to empathy-utils. (Jonny Lamb) Signed-off-by: Jonny Lamb svn path=/trunk/; revision=1738 --- libempathy/empathy-utils.c | 129 +++++++++++++++++++++++++++++++++++++++++++++ libempathy/empathy-utils.h | 7 +++ 2 files changed, 136 insertions(+) (limited to 'libempathy') diff --git a/libempathy/empathy-utils.c b/libempathy/empathy-utils.c index 934fc9e2c..8fa56ad11 100644 --- a/libempathy/empathy-utils.c +++ b/libempathy/empathy-utils.c @@ -776,6 +776,135 @@ empathy_connection_request_channel (TpConnection *connection, weak_object); } +EmpathyFile * +empathy_send_file_from_stream (EmpathyContact *contact, + GInputStream *in_stream, + const gchar *filename, + guint64 size) +{ + MissionControl *mc; + McAccount *account; + TpConnection *connection; + guint handle; + gchar *object_path; + TpChannel *channel; + EmpathyTpFile *tp_file; + EmpathyFile *file; + GError *error = NULL; + GValue value = { 0 }; + + g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL); + g_return_val_if_fail (G_IS_INPUT_STREAM (in_stream), NULL); + g_return_val_if_fail (filename != NULL, NULL); + + DEBUG ("Sending %s from a stream to %s (size %llu)", + filename, empathy_contact_get_name (contact), size); + + mc = empathy_mission_control_new (); + account = empathy_contact_get_account (contact); + connection = mission_control_get_tpconnection (mc, account, NULL); + tp_connection_run_until_ready (connection, FALSE, NULL, NULL); + handle = empathy_contact_get_handle (contact); + + if (!tp_cli_connection_run_request_channel (connection, -1, + EMP_IFACE_CHANNEL_TYPE_FILE, + TP_HANDLE_TYPE_CONTACT, + handle, + FALSE, + &object_path, + &error, + NULL)) { + DEBUG ("Couldn't request channel: %s", + error ? error->message : "No error given"); + g_clear_error (&error); + g_object_unref (mc); + g_object_unref (connection); + return NULL; + } + + channel = tp_channel_new (connection, + object_path, + EMP_IFACE_CHANNEL_TYPE_FILE, + TP_HANDLE_TYPE_CONTACT, + handle, + NULL); + + /* TODO: this should go in NewChannel in the new API */ + + g_value_init (&value, G_TYPE_STRING); + g_value_set_string (&value, g_filename_display_basename (filename)); + tp_cli_dbus_properties_run_set (TP_PROXY (channel), + -1, EMP_IFACE_CHANNEL_TYPE_FILE, "Filename", + &value, NULL, NULL); + g_value_reset (&value); + + g_value_set_string (&value, "foobar"); + tp_cli_dbus_properties_run_set (TP_PROXY (channel), + -1, EMP_IFACE_CHANNEL_TYPE_FILE, "ContentMD5", + &value, NULL, NULL); + g_value_reset (&value); + + g_value_set_string (&value, "application/octet-stream"); + tp_cli_dbus_properties_run_set (TP_PROXY (channel), + -1, EMP_IFACE_CHANNEL_TYPE_FILE, "ContentType", + &value, NULL, NULL); + + g_value_unset (&value); + + g_value_init (&value, G_TYPE_UINT64); + g_value_set_uint64 (&value, size); + tp_cli_dbus_properties_run_set (TP_PROXY (channel), + -1, EMP_IFACE_CHANNEL_TYPE_FILE, "Size", + &value, NULL, NULL); + g_value_unset (&value); + + tp_file = empathy_tp_file_new (account, channel); + file = empathy_file_new (tp_file); + + if (file) { + empathy_file_set_input_stream (file, in_stream); + } + + g_object_unref (tp_file); + g_object_unref (mc); + g_object_unref (connection); + g_object_unref (channel); + g_free (object_path); + + return file; +} + +EmpathyFile * +empathy_send_file (EmpathyContact *contact, + GFile *gfile) +{ + GFileInfo *info; + guint64 size; + gchar *filename; + GInputStream *in_stream = NULL; + EmpathyFile *file; + + g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL); + g_return_val_if_fail (G_IS_FILE (gfile), NULL); + + filename = g_file_get_basename (gfile); + info = g_file_query_info (gfile, G_FILE_ATTRIBUTE_STANDARD_SIZE, 0, NULL, NULL); + size = info ? g_file_info_get_size (info) : EMPATHY_FILE_UNKNOWN_SIZE; + + DEBUG ("Sending %s to %s", + filename, empathy_contact_get_name (contact)); + + in_stream = G_INPUT_STREAM (g_file_read (gfile, NULL, NULL)); + file = empathy_send_file_from_stream (contact, in_stream, filename, size); + + g_object_unref (in_stream); + g_free (filename); + if (info) + g_object_unref (info); + + return file; +} + void empathy_init (void) { diff --git a/libempathy/empathy-utils.h b/libempathy/empathy-utils.h index afde4d3a7..7059f6330 100644 --- a/libempathy/empathy-utils.h +++ b/libempathy/empathy-utils.h @@ -36,6 +36,7 @@ #include #include "empathy-contact.h" +#include "empathy-file.h" G_BEGIN_DECLS @@ -122,6 +123,12 @@ void empathy_connection_request_channel (TpConnection *proxy, gpointer user_data, GDestroyNotify destroy, GObject *weak_object); +EmpathyFile * empathy_send_file_from_stream (EmpathyContact *contact, + GInputStream *in_stream, + const gchar *filename, + guint64 size); +EmpathyFile * empathy_send_file (EmpathyContact *contact, + GFile *file); void empathy_init (void); G_END_DECLS -- cgit v1.2.3