[polari/wip/fmuellner/tracker: 6/15] lib: Add convenience method to convert message to resource



commit 258ad8e2cdada266d8875696fbb111591d6e57a3
Author: Florian Müllner <fmuellner gnome org>
Date:   Thu Jul 27 17:25:22 2017 +0200

    lib: Add convenience method to convert message to resource

 src/lib/polari-message.c | 106 +++++++++++++++++++++++++++++++++++++++++++++++
 src/lib/polari-message.h |   6 +++
 2 files changed, 112 insertions(+)
---
diff --git a/src/lib/polari-message.c b/src/lib/polari-message.c
index de079b1..4328627 100644
--- a/src/lib/polari-message.c
+++ b/src/lib/polari-message.c
@@ -142,3 +142,109 @@ polari_message_is_self (PolariMessage *message)
 {
   return message->is_self;
 }
+
+static TrackerResource *
+create_account_resource (const char *id)
+{
+  TrackerResource *res;
+  char *uri;
+
+  uri = tracker_sparql_escape_uri_printf ("urn:account:%s", id);
+
+  res = tracker_resource_new (uri);
+
+  tracker_resource_set_uri (res, "rdf:type", "polari:Account");
+  tracker_resource_set_string (res, "polari:id", id);
+
+  g_free (uri);
+
+  return res;
+}
+
+static TrackerResource *
+create_channel_resource (const char *name,
+                         const char *account_id,
+                         gboolean    is_room)
+{
+  TrackerResource *res;
+  char *uri;
+
+  uri = tracker_sparql_escape_uri_printf ("urn:channel:%s:%s", account_id, name);
+
+  res = tracker_resource_new (uri);
+
+  tracker_resource_set_uri (res, "rdf:type", is_room ? "polari:Room"
+                                                     : "polari:Conversation");
+  tracker_resource_set_string (res, "polari:name", name);
+  tracker_resource_set_take_relation (res, "polari:account",
+                                      create_account_resource (account_id));
+
+  g_free (uri);
+
+  return res;
+}
+
+static TrackerResource *
+create_sender_resource (const char *nick,
+                        const char *account_id,
+                        gboolean    is_self)
+{
+  TrackerResource *res;
+  char *uri, *id;
+
+  id = g_ascii_strdown (nick, -1);
+  uri = tracker_sparql_escape_uri_printf ("urn:contact:%s:%s", account_id, id);
+
+  res = tracker_resource_new (uri);
+
+  tracker_resource_set_uri (res, "rdf:type", is_self ? "polari:SelfContact"
+                                                     : "polari:Contact");
+  tracker_resource_set_string (res, "polari:nick", nick);
+
+  g_free (id);
+  g_free (uri);
+
+  return res;
+}
+
+/**
+ * polari_message_to_tracker_resource:
+ *
+ * Returns: (transfer full):
+ */
+TrackerResource *
+polari_message_to_tracker_resource (PolariMessage *message,
+                                    const char    *account_id,
+                                    const char    *channel_name,
+                                    gboolean       is_room)
+{
+  TrackerResource *res, *rel;
+  char *uri, *time;
+
+  uri = tracker_sparql_get_uuid_urn ();
+
+  res = tracker_resource_new (uri);
+
+  if (polari_message_is_action (message))
+    tracker_resource_set_uri (res, "rdf:type", "polari:ActionMessage");
+  else
+    tracker_resource_set_uri (res, "rdf:type", "polari:Message");
+
+  time = g_date_time_format (polari_message_get_time (message), "%FT%H:%M:%S");
+  tracker_resource_set_string (res, "polari:time", time);
+
+  tracker_resource_set_string (res, "polari:text", polari_message_get_text (message));
+
+  rel = create_sender_resource (polari_message_get_sender (message),
+                                account_id,
+                                polari_message_is_self (message));
+  tracker_resource_set_take_relation (res, "polari:sender", rel);
+
+  rel = create_channel_resource (channel_name, account_id, is_room);
+  tracker_resource_set_take_relation (res, "polari:channel", rel);
+
+  g_free (uri);
+  g_free (time);
+
+  return res;
+}
diff --git a/src/lib/polari-message.h b/src/lib/polari-message.h
index ff93672..9deb9a9 100644
--- a/src/lib/polari-message.h
+++ b/src/lib/polari-message.h
@@ -19,6 +19,7 @@
 #ifndef POLARI_MESSAGE_H
 #define POLARI_MESSAGE_H
 
+#include <libtracker-sparql/tracker-sparql.h>
 #include <telepathy-glib/telepathy-glib.h>
 #include <telepathy-logger/telepathy-logger.h>
 
@@ -46,6 +47,11 @@ GDateTime         *polari_message_get_time   (PolariMessage *message);
 gboolean           polari_message_is_action  (PolariMessage *message);
 gboolean           polari_message_is_self    (PolariMessage *message);
 
+TrackerResource *polari_message_to_tracker_resource (PolariMessage *message,
+                                                     const char    *account_id,
+                                                     const char    *channel_name,
+                                                     gboolean       is_room);
+
 G_DEFINE_AUTOPTR_CLEANUP_FUNC (PolariMessage, polari_message_free)
 
 G_END_DECLS


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]