summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCosimo Alfarano <cosimo.alfarano@collabora.co.uk>2010-01-05 18:01:16 +0000
committerCosimo Alfarano <cosimo.alfarano@collabora.co.uk>2010-01-05 18:01:16 +0000
commitbc271b0002d24871451354296007b3fe62249117 (patch)
treeaef43588e231b6dd5936703c798609b4599f0e58 /src
parent1fc9816fa509822525285dc2ce03b707111ceff8 (diff)
downloadtelepathy-logger-bc271b0002d24871451354296007b3fe62249117.tar.gz
DBus LastChats method working
* added a working LastChats method to the DBus service * bug fixing
Diffstat (limited to 'src')
-rw-r--r--src/test_api.c38
-rw-r--r--src/tpl-dbus-service.c150
-rw-r--r--src/tpl-dbus-service.h11
-rw-r--r--src/tpl-dbus-service.xml7
-rw-r--r--src/tpl-log-entry.c6
5 files changed, 144 insertions, 68 deletions
diff --git a/src/test_api.c b/src/test_api.c
index 0cdff38..2d3b542 100644
--- a/src/test_api.c
+++ b/src/test_api.c
@@ -29,52 +29,54 @@
#include <tpl-dbus-service-client.h>
#include <tpl-dbus-service.h>
+#include <tpl-time.h>
+#define ACCOUNT_PATH "/org/freedesktop/Telepathy/Account/gabble/jabber/cosimo_2ealfarano_40collabora_2eco_2euk0"
+#define CHAT_ID "echo@test.collabora.co.uk"
static GMainLoop *loop = NULL;
-static void cb (DBusGProxy *proxy, char *OUT_str_ret, GError *error, gpointer userdata) {
+static void
+cb (DBusGProxy *proxy, GPtrArray *retval, GError *error,
+ gpointer userdata)
+{
if(error!=NULL) {
g_error("ERROR: %s\n", error->message);
return;
}
- g_message("answer it: %s\n", OUT_str_ret);
+ for(guint i=0; i<retval->len; ++i) {
+ GValueArray *values = g_ptr_array_index(retval, i);
+ GValue *sender = g_value_array_get_nth(values, 0);
+ GValue *message = g_value_array_get_nth(values, 1);
+ GValue *timestamp = g_value_array_get_nth(values, 2);
+ g_message("[%s] <%s> %s\n",
+ tpl_time_to_string_local(g_value_get_uint(timestamp), "%Y-%m-%d %H:%M.%S"),
+ g_value_get_string(sender),
+ g_value_get_string(message));
+ }
}
int main(int argc, char *argv[])
{
DBusGProxy *proxy;
- gchar *result;
+// gchar *result;
DBusGConnection *connection;
GError *error=NULL;
g_type_init ();
- //connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
connection = tp_get_bus();
proxy = dbus_g_proxy_new_for_name (connection,
TPL_DBUS_SRV_WELL_KNOWN_BUS_NAME,
TPL_DBUS_SRV_OBJECT_PATH,
TPL_DBUS_SRV_WELL_KNOWN_BUS_NAME);
-
- if (!org_freedesktop_Telepathy_TelepathyLoggerService_last_messages
- (proxy, "/org/freedesktop/Telepathy/Account/gabble/jabber/cosimo_2ealfarano_40collabora_2eco_2euk0", "echo@test.collabora.co.uk", FALSE, &result, &error))
- {
- g_warning ("Woops remote method failed: %s", error->message);
- g_error_free (error);
- return 1;
- }
- g_message("RESULT: %s\n", result);
-
- if (!org_freedesktop_Telepathy_TelepathyLoggerService_last_messages_async
- (proxy, "/org/freedesktop/Telepathy/Account/gabble/jabber/cosimo_2ealfarano_40collabora_2eco_2euk0", "echofoo", FALSE, cb, NULL))
+ if (!org_freedesktop_Telepathy_TelepathyLoggerService_last_chats_async
+ (proxy, ACCOUNT_PATH, CHAT_ID, FALSE, 11, cb, NULL))
{
g_warning ("Async Woops remote method failed: %s", error->message);
g_error_free (error);
return 1;
}
-// g_object_unref (proxy);
-
loop = g_main_loop_new (NULL, FALSE);
g_main_loop_run (loop);
diff --git a/src/tpl-dbus-service.c b/src/tpl-dbus-service.c
index f838116..33201aa 100644
--- a/src/tpl-dbus-service.c
+++ b/src/tpl-dbus-service.c
@@ -28,10 +28,15 @@
#include <tpl-dbus-service.h>
#include <tpl-log-manager.h>
+#define DBUS_STRUCT_STRING_STRING_UINT \
+ (dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING, G_TYPE_STRING, G_TYPE_UINT, G_TYPE_INVALID))
+
+
static gboolean
-tpl_dbus_service_last_messages (TplDBusService *self,
+tpl_dbus_service_last_chats (TplDBusService *self,
gchar const* account_path, gchar const* identifier,
- gboolean is_chatroom, gchar **answer, GError **error);
+ gboolean is_chatroom, guint lines, DBusGMethodInvocation *context);
+
#include <tpl-dbus-service-server.h>
@@ -81,73 +86,134 @@ TplDBusService *tpl_dbus_service_new (void)
return g_object_new(TPL_TYPE_DBUS_SERVICE, NULL);
}
+/*
+static TplDBusServiceChatMessage *
+tpl_dbus_service_chat_message_new(void)
+{
+ return g_slice_alloc0 (sizeof (TplDBusServiceChatMessage));
+}
+
+void
+tpl_dbus_service_chat_message_free(TplDBusServiceChatMessage *data)
+{
+ g_free(data);
+}
+*/
static gboolean
-tpl_dbus_service_last_messages (TplDBusService *self,
+_pack_last_chats_answer (GList *data, GPtrArray **array)
+{
+ guint data_idx;
+ GPtrArray *retval;
+
+ (*array) = g_ptr_array_new_with_free_func ((GDestroyNotify) g_value_array_free);
+ retval = *array;
+
+ for(data_idx=0; data_idx<g_list_length(data); ++data_idx)
+ {
+ TplLogEntry *log = g_list_nth_data (data, data_idx);
+
+ GValue *value = g_new0(GValue, 1);
+
+ gchar *message = g_strdup (
+ tpl_log_entry_text_get_message (
+ log->entry.text)
+ );
+ gchar *sender = g_strdup (
+ tpl_contact_get_identifier(
+ tpl_log_entry_text_get_sender (
+ log->entry.text)) );
+ guint timestamp = tpl_log_entry_get_timestamp (log);
+
+
+ g_value_init(value, DBUS_STRUCT_STRING_STRING_UINT);
+ g_value_take_boxed (value, dbus_g_type_specialized_construct
+ (DBUS_STRUCT_STRING_STRING_UINT));
+
+ dbus_g_type_struct_set (value, 0, sender,
+ 1, message,
+ 2, timestamp,
+ G_MAXUINT);
+ g_ptr_array_add (retval, g_value_get_boxed (value));
+ g_free (value);
+
+ g_debug ("retval[%d]=\"[%d] <%s>: %s\"\n", data_idx,
+ timestamp,
+ sender,
+ message);
+ }
+ return TRUE;
+}
+
+static gboolean
+tpl_dbus_service_last_chats (TplDBusService *self,
gchar const* account_path, gchar const* identifier,
- gboolean is_chatroom, gchar **answer, GError **error)
+ gboolean is_chatroom, guint lines, DBusGMethodInvocation *context)
{
+ guint dates_idx;
+ gint msgs_idx;
+ GError *error=NULL;
TpAccount *account;
DBusGConnection *dbus;
TpDBusDaemon *tp_dbus;
GList *ret=NULL;
- guint lines = 0;
+ GPtrArray *answer;
+ guint left_lines = lines;
- g_message("TPL DBUS A: %s %s\n", account_path, identifier);
+ g_return_val_if_fail(context!=NULL, FALSE);
dbus = tp_get_bus();
- tp_dbus = tp_dbus_daemon_new(dbus);
-
- account = tp_account_new(tp_dbus, account_path, error);
- if (!account) {
- g_error("during TpAccount creation: %s\n",
- (*error)->message);
+ tp_dbus = tp_dbus_daemon_new(dbus);
+
+ account = tp_account_new(tp_dbus, account_path, &error);
+ if (error!=NULL) {
+ g_error("TpAccount creation: %s\n",
+ error->message);
+ dbus_g_method_return_error(context, error);
+ g_error_free(error);
g_object_unref(tp_dbus);
+ g_object_unref(dbus);
return FALSE;
}
-
GList *dates = tpl_log_manager_get_dates(self->manager, account, identifier, is_chatroom);
if(!dates) {
- g_set_error(error,
+ g_set_error(&error,
TPL_DBUS_SERVICE_ERROR,
TPL_DBUS_SERVICE_ERROR_GENERIC,
"Error during date list retrieving");
+ g_error(error->message);
+ dbus_g_method_return_error(context, error);
+ g_error_free(error);
+ g_object_unref(tp_dbus);
+ g_object_unref(dbus);
return FALSE;
}
+ dates = g_list_reverse(dates);
- for(guint i=g_list_length(dates); i>0 && lines<=5; --i) {
- gchar *date = g_list_nth_data(dates, i-1);
- g_message("%d: %s\n", i, date);
+ for(dates_idx=0; dates_idx<g_list_length(dates) && left_lines>0; ++dates_idx) {
+ gchar *date = g_list_nth_data(dates, dates_idx);
GList *messages = tpl_log_manager_get_messages_for_date(self->manager,
account, identifier, is_chatroom, date);
- gint msgs_len = g_list_length(messages);
- // get the last 5 messages or less if lentgh<5
- gint msg_guard = msgs_len>=6 ? 6 : 0;
- for(gint m=msgs_len-1; m>=msg_guard && lines<=5; --m) {
- TplLogEntry *entry = g_list_nth_data(messages, m);
- g_message("CONSIDERING: %s\n", tpl_log_entry_text_get_message(entry->entry.text));
- ret = g_list_append(ret, entry);
- lines+=1;
+ guint msgs_len = g_list_length (messages);
+ gint guard = (msgs_len>=left_lines ? left_lines : msgs_len);
+
+ for(msgs_idx=msgs_len-1; guard>0 && left_lines>0; --guard, --msgs_idx) {
+ TplLogEntry *log = g_list_nth_data(messages, msgs_idx);
+ g_object_ref(log);
+ ret = g_list_prepend(ret, log);
+ left_lines-=1;
}
+ g_list_foreach(messages, (GFunc) g_object_unref, NULL); // unref TplLogEntries
}
+ g_list_foreach(dates, (GFunc) g_free, NULL);
- for(guint i=0;i<g_list_length(ret);++i) {
- TplLogEntry *entry = g_list_nth_data (ret, i);
- g_message("RET %d: %s\n", i, tpl_log_entry_text_get_message( entry->entry.text));
- }
- *answer = g_strdup_printf("%s %s %s %s %s",
- tpl_log_entry_text_get_message(((TplLogEntry*) g_list_nth_data(ret,0))->entry.text ),
- tpl_log_entry_text_get_message(((TplLogEntry*) g_list_nth_data(ret,1))->entry.text ),
- tpl_log_entry_text_get_message(((TplLogEntry*) g_list_nth_data(ret,2))->entry.text ),
- tpl_log_entry_text_get_message(((TplLogEntry*) g_list_nth_data(ret,3))->entry.text ),
- tpl_log_entry_text_get_message(((TplLogEntry*) g_list_nth_data(ret,4))->entry.text )
- );
- return TRUE;
-}
+ _pack_last_chats_answer(ret, &answer);
+ g_list_foreach(ret, (GFunc) g_object_unref, NULL);
-GQuark
-tpl_dbus_service_error_quark (void)
-{
- return g_quark_from_static_string ("tpl-dbus-service-error-quark");
+ dbus_g_method_return(context, answer);
+
+ g_object_unref(tp_dbus);
+
+ return TRUE;
}
diff --git a/src/tpl-dbus-service.h b/src/tpl-dbus-service.h
index 4d13c21..fe69d93 100644
--- a/src/tpl-dbus-service.h
+++ b/src/tpl-dbus-service.h
@@ -39,10 +39,7 @@ G_BEGIN_DECLS
#define TPL_IS_DBUS_SERVICE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TPL_TYPE_DBUS_SERVICE))
#define TPL_DBUS_SERVICE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TPL_TYPE_DBUS_SERVICE, TplDBusServiceClass))
-#define TPL_DBUS_SERVICE_ERROR tpl_dbus_service_error_quark ()
-GQuark
-tpl_dbus_service_error_quark (void);
-
+#define TPL_DBUS_SERVICE_ERROR g_quark_from_static_string ("tpl-dbus-service-error-quark")
typedef enum {
TPL_DBUS_SERVICE_ERROR_GENERIC
} TplDBusServiceErrorCode;
@@ -58,6 +55,12 @@ typedef struct {
GObjectClass parent_class;
} TplDBusServiceClass;
+typedef struct {
+ long unsigned timestamp;
+ gchar *sender;
+ gchar *message;
+} TplDBusServiceChatMessage;
+
GType tpl_dbus_service_get_type (void);
TplDBusService *tpl_dbus_service_new (void);
diff --git a/src/tpl-dbus-service.xml b/src/tpl-dbus-service.xml
index 425a3c5..be4fe2c 100644
--- a/src/tpl-dbus-service.xml
+++ b/src/tpl-dbus-service.xml
@@ -1,11 +1,14 @@
<?xml version="1.0" encoding="UTF-8" ?>
<node name="/org/freedesktop/Telepathy/TelepathyLoggerService">
<interface name="org.freedesktop.Telepathy.TelepathyLoggerService">
- <method name="LastMessages">
+ <method name="LastChats">
+ <annotation name="org.freedesktop.DBus.GLib.Async" value=""/>
<arg type="o" name="account" direction="in" />
<arg type="s" name="identifier" direction="in" />
<arg type="b" name="is_chatroom" direction="in" />
- <arg type="s" name="answer" direction="out" />
+ <arg type="u" name="lines" direction="in" />
+ <!-- ARRAY of (TIMESTAMP SENDER MESSAGE) -->
+ <arg type="a(ssu)" name="answer" direction="out" />
</method>
</interface>
</node>
diff --git a/src/tpl-log-entry.c b/src/tpl-log-entry.c
index 097cf06..c311d0a 100644
--- a/src/tpl-log-entry.c
+++ b/src/tpl-log-entry.c
@@ -23,6 +23,8 @@
#include <tpl-log-entry.h>
+#define DEBUG(...)
+
G_DEFINE_TYPE (TplLogEntry, tpl_log_entry, G_TYPE_OBJECT)
@@ -35,14 +37,14 @@ tpl_log_entry_dispose (GObject *obj)
{
TplLogEntry *self = TPL_LOG_ENTRY(obj);
- g_debug("TplLogEntry: disposing\n");
+ DEBUG("TplLogEntry: disposing\n");
tpl_object_unref_if_not_null (tpl_log_entry_get_entry(self));
self->entry.generic = NULL;
G_OBJECT_CLASS (tpl_log_entry_parent_class)->finalize (obj);
- g_debug("TplLogEntry: disposed\n");
+ DEBUG("TplLogEntry: disposed\n");
}