]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-theme-adium.c
Merge branch 'fix-585882'
[empathy.git] / libempathy-gtk / empathy-theme-adium.c
index d5ea2ed5283be533fb1b54d0340530213cee9868..3b4a895e4d2d6bd0c9e40467e3c9a6259d5ba91a 100644 (file)
@@ -52,6 +52,7 @@ typedef struct {
        EmpathySmileyManager *smiley_manager;
        EmpathyContact       *last_contact;
        time_t                last_timestamp;
+       gboolean              last_is_backlog;
        gboolean              page_loaded;
        GList                *message_queue;
        gchar                *hovered_uri;
@@ -67,12 +68,20 @@ struct _EmpathyAdiumData {
        gchar *template_html;
        gchar *in_content_html;
        gsize  in_content_len;
+       gchar *in_context_html;
+       gsize  in_context_len;
        gchar *in_nextcontent_html;
        gsize  in_nextcontent_len;
+       gchar *in_nextcontext_html;
+       gsize  in_nextcontext_len;
        gchar *out_content_html;
        gsize  out_content_len;
+       gchar *out_context_html;
+       gsize  out_context_len;
        gchar *out_nextcontent_html;
        gsize  out_nextcontent_len;
+       gchar *out_nextcontext_html;
+       gsize  out_nextcontext_len;
        gchar *status_html;
        gsize  status_len;
        GHashTable *info;
@@ -474,7 +483,7 @@ theme_adium_append_message (EmpathyChatView *view,
        EmpathyThemeAdium     *theme = EMPATHY_THEME_ADIUM (view);
        EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
        EmpathyContact        *sender;
-       McAccount             *account;
+       EmpathyAccount        *account;
        McProfile             *account_profile;
        gchar                 *dup_body = NULL;
        const gchar           *body;
@@ -487,7 +496,8 @@ theme_adium_append_message (EmpathyChatView *view,
        gsize                  len = 0;
        const gchar           *func;
        const gchar           *service_name;
-       const gchar           *message_classes = NULL;
+       GString               *message_classes = NULL;
+       gboolean              is_backlog;
 
        if (!priv->page_loaded) {
                priv->message_queue = g_list_prepend (priv->message_queue,
@@ -498,7 +508,7 @@ theme_adium_append_message (EmpathyChatView *view,
        /* Get information */
        sender = empathy_message_get_sender (msg);
        account = empathy_contact_get_account (sender);
-       account_profile = mc_account_get_profile (account);
+       account_profile = empathy_account_get_profile (account);
        service_name = mc_profile_get_display_name (account_profile);
        timestamp = empathy_message_get_timestamp (msg);
        body = empathy_message_get_body (msg);
@@ -541,45 +551,102 @@ theme_adium_append_message (EmpathyChatView *view,
                }
        }
 
+       is_backlog = empathy_message_is_backlog (msg);
+
        /* Get the right html/func to add the message */
        func = "appendMessage";
+
+       message_classes = g_string_new ("message");
+
+       /* eventually append the "history" class */
+       if (is_backlog) {
+               g_string_append (message_classes, " history");
+       }
+
+       /* check the sender of the message and append the appropriate class */
+       if (empathy_contact_is_user (sender)) {
+               g_string_append (message_classes, " outgoing");
+       }
+       else {
+               g_string_append (message_classes, " incoming");
+       }
+
        /*
         * To mimick Adium's behavior, we only want to join messages
-        * sent within a 5 minute time frame.
+        * sent by the same contact within a 5 minute time frame.
         */
        if (empathy_contact_equal (priv->last_contact, sender) &&
-           (timestamp - priv->last_timestamp < MESSAGE_JOIN_PERIOD)) {
+           (timestamp - priv->last_timestamp < MESSAGE_JOIN_PERIOD) &&
+           (is_backlog == priv->last_is_backlog)) {
+               /* the messages can be appended */
                func = "appendNextMessage";
+               g_string_append (message_classes, " consecutive");
+
+               /* check who is the sender of the message to use the correct html file */
                if (empathy_contact_is_user (sender)) {
-                       message_classes = "consecutive incoming message";
-                       html = priv->data->out_nextcontent_html;
-                       len = priv->data->out_nextcontent_len;
+                       /* check if this is a backlog message and use NextContext.html */
+                       if (is_backlog) {
+                               html = priv->data->out_nextcontext_html;
+                               len = priv->data->out_nextcontext_len;
+                       }
+
+                       /*
+                        * html is null if this is not a backlog message or
+                        * if we have to fallback (NextContext.html missing).
+                        * use NextContent.html
+                        */
+                       if (html == NULL) {
+                               html = priv->data->out_nextcontent_html;
+                               len = priv->data->out_nextcontent_len;
+                       }
                }
-               if (!html) {
-                       message_classes = "consecutive message outgoing";
-                       html = priv->data->in_nextcontent_html;
-                       len = priv->data->in_nextcontent_len;
+               else {
+                       if (is_backlog) {
+                               html = priv->data->in_nextcontext_html;
+                               len = priv->data->in_nextcontext_len;
+                       }
+
+                       if (html == NULL) {
+                               html = priv->data->in_nextcontent_html;
+                               len = priv->data->in_nextcontent_len;
+                       }
                }
        }
-       if (!html) {
+
+       /*
+        * we have html == NULL here if:
+        * 1. the message didn't have to be appended because
+        *    the sender was different or the timestamp was too far
+        * 2. NextContent.html file does not exist, so we must
+        *    not forget to fallback to the correct Content.html
+        */
+       if (html == NULL) {
                if (empathy_contact_is_user (sender)) {
-                       if (!message_classes) {
-                               message_classes = "incoming message";
+                       if (is_backlog) {
+                               html = priv->data->out_context_html;
+                               len = priv->data->out_context_len;
+                       }
+
+                       if (html == NULL) {
+                               html = priv->data->out_content_html;
+                               len = priv->data->out_content_len;
                        }
-                       html = priv->data->out_content_html;
-                       len = priv->data->out_content_len;
                }
-               if (!html) {
-                       if (!message_classes) {
-                               message_classes = "message outgoing";
+               else {
+                       if (is_backlog) {
+                               html = priv->data->in_context_html;
+                               len = priv->data->in_context_len;
+                       }
+
+                       if (html == NULL) {
+                               html = priv->data->in_content_html;
+                               len = priv->data->in_content_len;
                        }
-                       html = priv->data->in_content_html;
-                       len = priv->data->in_content_len;
                }
        }
 
        theme_adium_append_html (theme, func, html, len, body, avatar_filename,
-                                name, contact_id, service_name, message_classes,
+                                name, contact_id, service_name, message_classes->str,
                                 timestamp);
 
        /* Keep the sender of the last displayed message */
@@ -588,8 +655,10 @@ theme_adium_append_message (EmpathyChatView *view,
        }
        priv->last_contact = g_object_ref (sender);
        priv->last_timestamp = timestamp;
+       priv->last_is_backlog = is_backlog;
 
        g_free (dup_body);
+       g_string_free (message_classes, TRUE);
 }
 
 static void
@@ -646,6 +715,13 @@ theme_adium_clear (EmpathyChatView *view)
                                          priv->data->template_html,
                                          basedir_uri);
        g_free (basedir_uri);
+
+       /* Clear last contact to avoid trying to add a 'joined'
+        * message when we don't have an insertion point. */
+       if (priv->last_contact) {
+               g_object_unref (priv->last_contact);
+               priv->last_contact = NULL;
+       }
 }
 
 static gboolean
@@ -899,6 +975,15 @@ empathy_adium_path_is_valid (const gchar *path)
        gboolean ret;
        gchar   *file;
 
+       /* The theme is not valid if there is no Info.plist */
+       file = g_build_filename (path, "Contents", "Info.plist",
+                                NULL);
+       ret = g_file_test (file, G_FILE_TEST_EXISTS);
+       g_free (file);
+
+       if (ret == FALSE)
+               return ret;
+
        /* We ship a default Template.html as fallback if there is any problem
         * with the one inside the theme. The only other required file is
         * Content.html for incoming messages (outgoing fallback to use
@@ -924,10 +1009,15 @@ empathy_adium_info_new (const gchar *path)
        value = empathy_plist_parse_from_file (file);
        g_free (file);
 
-       if (value) {
-               info = g_value_dup_boxed (value);
-               tp_g_value_slice_free (value);
-       }
+       if (value == NULL)
+               return NULL;
+
+       info = g_value_dup_boxed (value);
+       tp_g_value_slice_free (value);
+
+       /* Insert the theme's path into the hash table,
+        * keys have to be dupped */
+       tp_asv_set_string (info, g_strdup ("path"), path);
 
        return info;
 }
@@ -980,6 +1070,14 @@ empathy_adium_data_new_with_info (const gchar *path, GHashTable *info)
        g_file_get_contents (file, &data->in_nextcontent_html, &data->in_nextcontent_len, NULL);
        g_free (file);
 
+       file = g_build_filename (data->basedir, "Incoming", "Context.html", NULL);
+       g_file_get_contents (file, &data->in_context_html, &data->in_context_len, NULL);
+       g_free (file);
+
+       file = g_build_filename (data->basedir, "Incoming", "NextContext.html", NULL);
+       g_file_get_contents (file, &data->in_nextcontext_html, &data->in_nextcontext_len, NULL);
+       g_free (file);
+
        file = g_build_filename (data->basedir, "Outgoing", "Content.html", NULL);
        g_file_get_contents (file, &data->out_content_html, &data->out_content_len, NULL);
        g_free (file);
@@ -988,6 +1086,14 @@ empathy_adium_data_new_with_info (const gchar *path, GHashTable *info)
        g_file_get_contents (file, &data->out_nextcontent_html, &data->out_nextcontent_len, NULL);
        g_free (file);
 
+       file = g_build_filename (data->basedir, "Outgoing", "Context.html", NULL);
+       g_file_get_contents (file, &data->out_context_html, &data->out_context_len, NULL);
+       g_free (file);
+
+       file = g_build_filename (data->basedir, "Outgoing", "NextContext.html", NULL);
+       g_file_get_contents (file, &data->out_nextcontext_html, &data->out_nextcontext_len, NULL);
+       g_free (file);
+
        file = g_build_filename (data->basedir, "Status.html", NULL);
        g_file_get_contents (file, &data->status_html, &data->status_len, NULL);
        g_free (file);
@@ -1091,7 +1197,7 @@ empathy_adium_data_ref (EmpathyAdiumData *data)
 {
        g_return_val_if_fail (data != NULL, NULL);
 
-       data->ref_count++;
+       g_atomic_int_inc (&data->ref_count);
 
        return data;
 }
@@ -1101,15 +1207,18 @@ empathy_adium_data_unref (EmpathyAdiumData *data)
 {
        g_return_if_fail (data != NULL);
 
-       data->ref_count--;
-       if (data->ref_count == 0) {
+       if (g_atomic_int_dec_and_test (&data->ref_count)) {
                g_free (data->path);
                g_free (data->basedir);
                g_free (data->template_html);
                g_free (data->in_content_html);
                g_free (data->in_nextcontent_html);
+               g_free (data->in_context_html);
+               g_free (data->in_nextcontext_html);
                g_free (data->out_content_html);
                g_free (data->out_nextcontent_html);
+               g_free (data->out_context_html);
+               g_free (data->out_nextcontext_html);
                g_free (data->default_avatar_filename);
                g_free (data->default_incoming_avatar_filename);
                g_free (data->default_outgoing_avatar_filename);