[polari/wip/fmuellner/tracker: 319/335] lib: Add PolariMessage type



commit 1a549bf81808704dce2fb23012c33e35b77b2346
Author: Florian Müllner <fmuellner gnome org>
Date:   Mon Jul 24 20:56:14 2017 +0200

    lib: Add PolariMessage type
    
    We have used a small message object to abstract differences between the
    telepathy-glib and telepathy-logger message types ever since we started
    to include backlogs. So far we just used a plain JS object, however we
    will soon need to represent messages in C code as well, so add a new
    boxed type that can replace the existing intermediate object.
    This will promote the existing telepathy-logger dependency from runtime-
    to build-time dependency, however this is temporary and we will soon get
    rid of the dependency altogether.

 meson.build                      |   1 +
 src/lib/polari-message-private.h |  40 +++++++++++
 src/lib/polari-message.c         | 144 +++++++++++++++++++++++++++++++++++++++
 src/lib/polari-message.h         |  54 +++++++++++++++
 src/meson.build                  |   7 +-
 5 files changed, 244 insertions(+), 2 deletions(-)
---
diff --git a/meson.build b/meson.build
index ef8e014..1409298 100644
--- a/meson.build
+++ b/meson.build
@@ -36,6 +36,7 @@ appstream_util = find_program('appstream-util', required: false)
 gio = dependency('gio-2.0', version: '>= 2.43.4')
 gtk3 = dependency('gtk+-3.0', version: '>= 3.21.6')
 telepathy_glib = dependency('telepathy-glib')
+telepathy_logger = dependency('telepathy-logger-0.2')
 girepository = dependency('gobject-introspection-1.0')
 gjs = dependency('gjs-1.0', version: '>= 1.53.90')
 
diff --git a/src/lib/polari-message-private.h b/src/lib/polari-message-private.h
new file mode 100644
index 0000000..4f6aa93
--- /dev/null
+++ b/src/lib/polari-message-private.h
@@ -0,0 +1,40 @@
+/* polari-message-private.h
+ *
+ * Copyright (C) 2017 Florian Müllner <fmuellner gnome org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef POLARI_MESSAGE_PRIVATE_H
+#define POLARI_MESSAGE_PRIVATE_H
+
+#include "polari-message.h"
+
+G_BEGIN_DECLS
+
+struct _PolariMessage
+{
+  GDateTime *time;
+  char *sender;
+  char *text;
+  gboolean is_action;
+  gboolean is_self;
+};
+
+PolariMessage *polari_message_new_empty (void);
+
+G_END_DECLS
+
+#endif /* POLARI_MESSAGE_PRIVATE_H */
+
diff --git a/src/lib/polari-message.c b/src/lib/polari-message.c
new file mode 100644
index 0000000..de079b1
--- /dev/null
+++ b/src/lib/polari-message.c
@@ -0,0 +1,144 @@
+/* polari-message.c
+ *
+ * Copyright (C) 2017 Florian Müllner <fmuellner gnome org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "polari-message-private.h"
+
+G_DEFINE_BOXED_TYPE (PolariMessage, polari_message, polari_message_copy, polari_message_free)
+
+PolariMessage *
+polari_message_new_empty ()
+{
+  return g_slice_new0 (PolariMessage);
+}
+
+PolariMessage *
+polari_message_new (const char *text,
+                    const char *sender,
+                    GDateTime  *time,
+                    gboolean    is_action,
+                    gboolean    is_self)
+{
+  PolariMessage *self;
+
+  self = polari_message_new_empty ();
+
+  self->text = g_strdup (text);
+  self->sender = g_strdup (sender);
+  self->time = g_date_time_ref (time);
+  self->is_action = is_action;
+  self->is_self = is_self;
+
+  return self;
+}
+
+PolariMessage *
+polari_message_new_from_tp_message (TpMessage *tp_message)
+{
+  PolariMessage *self;
+  char *text = tp_message_to_text (tp_message, NULL);
+  TpContact *sender = tp_signalled_message_get_sender (tp_message);
+  TpChannelTextMessageType type = tp_message_get_message_type (tp_message);
+  gint64 timestamp;
+  gboolean incoming;
+
+  timestamp = tp_message_get_sent_timestamp (tp_message);
+  if (timestamp == 0)
+    timestamp = tp_message_get_received_timestamp (tp_message);
+
+  tp_message_get_pending_message_id (tp_message, &incoming);
+
+  self = polari_message_new (text,
+                             tp_contact_get_alias (sender),
+                             g_date_time_new_from_unix_utc (timestamp),
+                             type == TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION,
+                             !incoming);
+  g_free (text);
+
+  return self;
+}
+
+PolariMessage *
+polari_message_new_from_tpl_event (TplEvent *event)
+{
+  TplTextEvent *text_event = TPL_TEXT_EVENT (event);
+  const char *text = tpl_text_event_get_message (text_event);
+  TplEntity *sender = tpl_event_get_sender (event);
+  gint64 timestamp = tpl_event_get_timestamp (event);
+  TpChannelTextMessageType type = tpl_text_event_get_message_type (text_event);
+  gboolean is_self = tpl_entity_get_entity_type (sender) == TPL_ENTITY_SELF;
+
+  return polari_message_new (text,
+                             tpl_entity_get_alias (sender),
+                             g_date_time_new_from_unix_utc (timestamp),
+                             type == TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION,
+                             is_self);
+}
+
+PolariMessage *
+polari_message_copy (PolariMessage *self)
+{
+  g_return_val_if_fail (self, NULL);
+
+  return polari_message_new (self->text,
+                             self->sender,
+                             self->time,
+                             self->is_action,
+                             self->is_self);
+}
+
+void
+polari_message_free (PolariMessage *self)
+{
+  g_return_if_fail (self);
+
+  g_free (self->text);
+  g_free (self->sender);
+  g_date_time_unref (self->time);
+
+  g_slice_free (PolariMessage, self);
+}
+
+const char *
+polari_message_get_text (PolariMessage *message)
+{
+  return message->text;
+}
+
+const char *
+polari_message_get_sender (PolariMessage *message)
+{
+  return message->sender;
+}
+
+GDateTime *
+polari_message_get_time (PolariMessage *message)
+{
+  return g_date_time_ref (message->time);
+}
+
+gboolean
+polari_message_is_action (PolariMessage *message)
+{
+  return message->is_action;
+}
+
+gboolean
+polari_message_is_self (PolariMessage *message)
+{
+  return message->is_self;
+}
diff --git a/src/lib/polari-message.h b/src/lib/polari-message.h
new file mode 100644
index 0000000..ff93672
--- /dev/null
+++ b/src/lib/polari-message.h
@@ -0,0 +1,54 @@
+/* polari-message.h
+ *
+ * Copyright (C) 2017 Florian Müllner <fmuellner gnome org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef POLARI_MESSAGE_H
+#define POLARI_MESSAGE_H
+
+#include <telepathy-glib/telepathy-glib.h>
+#include <telepathy-logger/telepathy-logger.h>
+
+G_BEGIN_DECLS
+
+GType polari_message_get_type (void) G_GNUC_CONST;
+#define POLARI_TYPE_MESSAGE (polari_message_get_type())
+
+typedef struct _PolariMessage PolariMessage;
+
+PolariMessage   *polari_message_new                 (const char *text,
+                                                     const char *sender,
+                                                     GDateTime  *time,
+                                                     gboolean    is_action,
+                                                     gboolean    is_self);
+PolariMessage   *polari_message_new_from_tp_message (TpMessage *tp_message);
+PolariMessage   *polari_message_new_from_tpl_event  (TplEvent  *tpl_event);
+
+PolariMessage   *polari_message_copy  (PolariMessage *self);
+void             polari_message_free  (PolariMessage *self);
+
+const char        *polari_message_get_text   (PolariMessage *message);
+const char        *polari_message_get_sender (PolariMessage *message);
+GDateTime         *polari_message_get_time   (PolariMessage *message);
+gboolean           polari_message_is_action  (PolariMessage *message);
+gboolean           polari_message_is_self    (PolariMessage *message);
+
+G_DEFINE_AUTOPTR_CLEANUP_FUNC (PolariMessage, polari_message_free)
+
+G_END_DECLS
+
+#endif /* POLARI_MESSAGE_H */
+
diff --git a/src/meson.build b/src/meson.build
index fb99191..1769f77 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -68,6 +68,8 @@ libsources = [
   'lib/polari-client-factory.h',
   'lib/polari-drag-helper.c',
   'lib/polari-drag-helper.h',
+  'lib/polari-message.h',
+  'lib/polari-message.c',
   'lib/polari-room.c',
   'lib/polari-room.h',
   'lib/polari-util.c',
@@ -75,6 +77,7 @@ libsources = [
 ]
 
 lib_nongir_sources = [
+  'lib/polari-message-private.h',
   'lib/polari-tp-autocleanup.h',
 ]
 
@@ -83,7 +86,7 @@ libargs = [
   '-DG_LOG_DOMAIN="Polari"'
 ]
 libpolari = shared_library('polari-1.0', libsources + lib_nongir_sources,
-  dependencies: [gio, gtk3, telepathy_glib],
+  dependencies: [gio, gtk3, telepathy_glib, telepathy_logger],
   c_args: libargs,
   install: true,
   install_dir: pkglibdir
@@ -101,7 +104,7 @@ gnome.generate_gir(libpolari,
   namespace: 'Polari',
   symbol_prefix: 'polari',
   identifier_prefix: 'Polari',
-  includes: ['Gio-2.0', 'Gtk-3.0', 'TelepathyGLib-0.12'],
+  includes: ['Gio-2.0', 'Gtk-3.0', 'TelepathyGLib-0.12', 'TelepathyLogger-0.2'],
   extra_args: '--quiet',
   install_dir_gir: girdir,
   install_dir_typelib: typelibdir,


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