]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-theme-adium.c
Updated Spanish translation
[empathy.git] / libempathy-gtk / empathy-theme-adium.c
index ee2b314fc4e66675dea928cba1327cf35792adfc..a870702ac975094ffb9ba27c746ecf7522f7aee8 100644 (file)
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2008-2012 Collabora Ltd.
+ * Copyright (C) 2012 Red Hat, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  */
 
 #include "config.h"
+#include "empathy-theme-adium.h"
 
-#include <string.h>
 #include <glib/gi18n-lib.h>
+#include <tp-account-widgets/tpaw-images.h>
+#include <tp-account-widgets/tpaw-time.h>
+#include <tp-account-widgets/tpaw-pixbuf-utils.h>
+#include <tp-account-widgets/tpaw-utils.h>
 
-#include <webkit/webkit.h>
-#include <telepathy-glib/telepathy-glib.h>
-
-#include <pango/pango.h>
-#include <gdk/gdk.h>
-
-#include <libempathy/empathy-gsettings.h>
-#include <libempathy/empathy-time.h>
-#include <libempathy/empathy-utils.h>
-
-#include "empathy-theme-adium.h"
+#include "empathy-gsettings.h"
+#include "empathy-images.h"
+#include "empathy-plist.h"
 #include "empathy-smiley-manager.h"
 #include "empathy-ui-utils.h"
-#include "empathy-plist.h"
-#include "empathy-images.h"
+#include "empathy-utils.h"
 #include "empathy-webkit-utils.h"
 
 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
-#include <libempathy/empathy-debug.h>
+#include "empathy-debug.h"
 
 #define BORING_DPI_DEFAULT 96
 
@@ -52,8 +48,11 @@ struct _EmpathyThemeAdiumPriv
 {
   EmpathyAdiumData *data;
   EmpathySmileyManager *smiley_manager;
+  EmpathyContact *first_contact;
   EmpathyContact *last_contact;
+  gint64 first_timestamp;
   gint64 last_timestamp;
+  gboolean first_is_backlog;
   gboolean last_is_backlog;
   guint pages_loading;
   /* Queue of QueuedItem*s containing an EmpathyMessage or string */
@@ -140,7 +139,8 @@ queue_item (GQueue *queue,
     guint type,
     EmpathyMessage *msg,
     const char *str,
-    gboolean should_highlight)
+    gboolean should_highlight,
+    gboolean prepend)
 {
   QueuedItem *item = g_slice_new0 (QueuedItem);
 
@@ -150,7 +150,10 @@ queue_item (GQueue *queue,
   item->str = g_strdup (str);
   item->should_highlight = should_highlight;
 
-  g_queue_push_tail (queue, item);
+  if (prepend)
+    g_queue_push_head (queue, item);
+  else
+    g_queue_push_tail (queue, item);
 
   return item;
 }
@@ -164,30 +167,6 @@ free_queued_item (QueuedItem *item)
   g_slice_free (QueuedItem, item);
 }
 
-static void
-theme_adium_update_enable_webkit_developer_tools (EmpathyThemeAdium *self)
-{
-  WebKitWebView *web_view = WEBKIT_WEB_VIEW (self);
-  gboolean enable_webkit_developer_tools;
-
-  enable_webkit_developer_tools = g_settings_get_boolean (
-      self->priv->gsettings_chat,
-      EMPATHY_PREFS_CHAT_WEBKIT_DEVELOPER_TOOLS);
-
-  g_object_set (G_OBJECT (webkit_web_view_get_settings (web_view)),
-      "enable-developer-extras", enable_webkit_developer_tools, NULL);
-}
-
-static void
-theme_adium_notify_enable_webkit_developer_tools_cb (GSettings *gsettings,
-    const gchar *key,
-    gpointer user_data)
-{
-  EmpathyThemeAdium *self = user_data;
-
-  theme_adium_update_enable_webkit_developer_tools (self);
-}
-
 static gboolean
 theme_adium_navigation_policy_decision_requested_cb (WebKitWebView *view,
     WebKitWebFrame *web_frame,
@@ -272,7 +251,7 @@ theme_adium_parse_body (EmpathyThemeAdium *self,
   const gchar *text,
   const gchar *token)
 {
-  EmpathyStringParser *parsers;
+  TpawStringParser *parsers;
   GString *string;
 
   /* Check if we have to parse smileys */
@@ -292,7 +271,7 @@ theme_adium_parse_body (EmpathyThemeAdium *self,
       "<span id=\"message-token-%s\">",
       token);
 
-  empathy_string_parser_substr (text, -1, parsers, string);
+  tpaw_string_parser_substr (text, -1, parsers, string);
 
   if (!tp_str_empty (token))
     g_string_append (string, "</span>");
@@ -542,9 +521,8 @@ nsdate_to_strftime (EmpathyAdiumData *data, const gchar *nsdate)
   return g_string_free (string, FALSE);
 }
 
-
 static void
-theme_adium_append_html (EmpathyThemeAdium *self,
+theme_adium_add_html (EmpathyThemeAdium *self,
     const gchar *func,
     const gchar *html,
     const gchar *message,
@@ -555,10 +533,13 @@ theme_adium_append_html (EmpathyThemeAdium *self,
     const gchar *message_classes,
     gint64 timestamp,
     gboolean is_backlog,
-    gboolean outgoing)
+    gboolean outgoing,
+    PangoDirection direction)
 {
+  GBytes *bytes;
   GString *string;
   const gchar *cur = NULL;
+  const gchar *js;
   gchar *script;
 
   /* Make some search-and-replace in the html code */
@@ -614,9 +595,22 @@ theme_adium_append_html (EmpathyThemeAdium *self,
         }
       else if (theme_adium_match (&cur, "%messageDirection%"))
         {
-          /* FIXME: The text direction of the message
-           * (either rtl or ltr)
-           */
+          switch (direction)
+            {
+              case PANGO_DIRECTION_LTR:
+              case PANGO_DIRECTION_TTB_LTR:
+              case PANGO_DIRECTION_WEAK_LTR:
+                replace = "ltr";
+                break;
+              case PANGO_DIRECTION_RTL:
+              case PANGO_DIRECTION_TTB_RTL:
+              case PANGO_DIRECTION_WEAK_RTL:
+                replace = "rtl";
+                break;
+              case PANGO_DIRECTION_NEUTRAL:
+              default:
+                break;
+            }
         }
       else if (theme_adium_match (&cur, "%senderDisplayName%"))
         {
@@ -656,20 +650,20 @@ theme_adium_append_html (EmpathyThemeAdium *self,
 
           strftime_format = nsdate_to_strftime (self->priv->data, format);
           if (is_backlog)
-            dup_replace = empathy_time_to_string_local (timestamp,
+            dup_replace = tpaw_time_to_string_local (timestamp,
               strftime_format ? strftime_format :
-              EMPATHY_TIME_DATE_FORMAT_DISPLAY_SHORT);
+              TPAW_TIME_DATE_FORMAT_DISPLAY_SHORT);
           else
-            dup_replace = empathy_time_to_string_local (timestamp,
+            dup_replace = tpaw_time_to_string_local (timestamp,
               strftime_format ? strftime_format :
-              EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
+              TPAW_TIME_FORMAT_DISPLAY_SHORT);
 
           replace = dup_replace;
         }
       else if (theme_adium_match (&cur, "%shortTime%"))
         {
-          dup_replace = empathy_time_to_string_local (timestamp,
-            EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
+          dup_replace = tpaw_time_to_string_local (timestamp,
+            TPAW_TIME_FORMAT_DISPLAY_SHORT);
           replace = dup_replace;
         }
       else if (theme_adium_match (&cur, "%service%"))
@@ -735,6 +729,17 @@ theme_adium_append_html (EmpathyThemeAdium *self,
     }
   g_string_append (string, "\")");
 
+  bytes = g_resources_lookup_data ("/org/gnome/Empathy/Chat/empathy-chat.js",
+      G_RESOURCE_LOOKUP_FLAGS_NONE,
+      NULL);
+
+  if (bytes != NULL)
+    {
+      js = (const gchar *) g_bytes_get_data (bytes, NULL);
+      g_string_prepend (string, js);
+      g_bytes_unref (bytes);
+    }
+
   script = g_string_free (string, FALSE);
   webkit_web_view_execute_script (WEBKIT_WEB_VIEW (self), script);
   g_free (script);
@@ -742,11 +747,12 @@ theme_adium_append_html (EmpathyThemeAdium *self,
 
 static void
 theme_adium_append_event_escaped (EmpathyThemeAdium *self,
-    const gchar *escaped)
+    const gchar *escaped,
+    PangoDirection direction)
 {
-  theme_adium_append_html (self, "appendMessage",
+  theme_adium_add_html (self, "appendMessage",
       self->priv->data->status_html, escaped, NULL, NULL, NULL,
-      NULL, "event", empathy_time_get_current (), FALSE, FALSE);
+      NULL, "event", tpaw_time_get_current (), FALSE, FALSE, direction);
 
   /* There is no last contact */
   if (self->priv->last_contact)
@@ -830,10 +836,55 @@ theme_adium_remove_all_focus_marks (EmpathyThemeAdium *self)
   theme_adium_remove_focus_marks (self, nodes);
 }
 
-void
-empathy_theme_adium_append_message (EmpathyThemeAdium *self,
+enum
+{
+  ADD_CONSECUTIVE_MSG_SCROLL = 0,
+  ADD_CONSECUTIVE_MSG_NO_SCROLL = 1,
+  ADD_MSG_SCROLL = 2,
+  ADD_MSG_NO_SCROLL = 3
+};
+
+/*
+ * theme_adium_add_message:
+ * @self: The #EmpathyThemeAdium used by the view.
+ * @msg: An #EmpathyMessage that is to be added to the view.
+ * @prev_contact: (out): The #EmpathyContact that sent the previous message.
+ * @prev_timestamp: (out): Timestamp of the previous message.
+ * @prev_is_backlog: (out): Whether the previous message was fetched
+ * from the logs.
+ * @should_highlight: Whether the message should be highlighted. eg.,
+ * if it matches the user's username in multi-user chat.
+ * @js_funcs: An array of JavaScript function names
+ *
+ * Shows @msg in the chat view by adding to @self. Addition is defined
+ * by the JavaScript functions listed in @js_funcs. Common examples
+ * are appending new incoming messages or prepending old messages from
+ * the logs.
+ *
+ * @js_funcs should be an array with exactly 4 entries. The entries
+ * should be the names of JavaScript functions that take the raw HTML
+ * that is to be added to the view as an argument and take the following
+ * actions, in this order:
+ * - add a new consecutive message and scroll to it if needed,
+ * - add a new consecutive message and do not scroll,
+ * - add a new non-consecutive message and scroll to it if needed, and
+ * - add a new non-consecutive message and do not scroll
+ *
+ * A message is considered to be consecutive with the previous one if
+ * all the following conditions are met:
+ * - senders are the same contact,
+ * - last message was recieved recently,
+ * - last message and this message both are/aren't backlog, and
+ * - DisableCombineConsecutive is not set in theme's settings
+ */
+static void
+theme_adium_add_message (EmpathyThemeAdium *self,
     EmpathyMessage *msg,
-    gboolean should_highlight)
+    EmpathyContact **prev_contact,
+    gint64 *prev_timestamp,
+    gboolean *prev_is_backlog,
+    gboolean should_highlight,
+    const gchar *js_funcs[])
 {
   EmpathyContact *sender;
   TpMessage *tp_msg;
@@ -851,18 +902,13 @@ empathy_theme_adium_append_message (EmpathyThemeAdium *self,
   gboolean is_backlog;
   gboolean consecutive;
   gboolean action;
+  PangoDirection direction;
 
-  if (self->priv->pages_loading != 0)
-    {
-      queue_item (&self->priv->message_queue, QUEUED_MESSAGE, msg, NULL,
-          should_highlight);
-      return;
-    }
 
   /* Get information */
   sender = empathy_message_get_sender (msg);
   account = empathy_contact_get_account (sender);
-  service_name = empathy_protocol_name_to_display_name
+  service_name = tpaw_protocol_name_to_display_name
     (tp_account_get_protocol_name (account));
   if (service_name == NULL)
     service_name = tp_account_get_protocol_name (account);
@@ -913,22 +959,17 @@ empathy_theme_adium_append_message (EmpathyThemeAdium *self,
         {
           if (!self->priv->data->default_avatar_filename)
             self->priv->data->default_avatar_filename =
-              empathy_filename_from_icon_name (EMPATHY_IMAGE_AVATAR_DEFAULT,
+              tpaw_filename_from_icon_name (TPAW_IMAGE_AVATAR_DEFAULT,
                        GTK_ICON_SIZE_DIALOG);
 
           avatar_filename = self->priv->data->default_avatar_filename;
         }
     }
 
-  /* We want to join this message with the last one if
-   * - senders are the same contact,
-   * - last message was recieved recently,
-   * - last message and this message both are/aren't backlog, and
-   * - DisableCombineConsecutive is not set in theme's settings */
   is_backlog = empathy_message_is_backlog (msg);
-  consecutive = empathy_contact_equal (self->priv->last_contact, sender) &&
-    (timestamp - self->priv->last_timestamp < MESSAGE_JOIN_PERIOD) &&
-    (is_backlog == self->priv->last_is_backlog) &&
+  consecutive = empathy_contact_equal (*prev_contact, sender) &&
+    (ABS (timestamp - *prev_timestamp) < MESSAGE_JOIN_PERIOD) &&
+    (is_backlog == *prev_is_backlog) &&
     !tp_asv_get_boolean (self->priv->data->info,
              "DisableCombineConsecutive", NULL);
 
@@ -969,7 +1010,7 @@ empathy_theme_adium_append_message (EmpathyThemeAdium *self,
    * status - the message is a status change
    * event - the message is a notification of something happening
    *         (for example, encryption being turned on)
-   * %status% - See %status% in theme_adium_append_html ()
+   * %status% - See %status% in theme_adium_add_html ()
    */
 
   /* This is slightly a hack, but it's the only way to add
@@ -991,10 +1032,11 @@ empathy_theme_adium_append_message (EmpathyThemeAdium *self,
 
   /* Define javascript function to use */
   if (consecutive)
-    func = self->priv->allow_scrolling ? "appendNextMessage" :
-      "appendNextMessageNoScroll";
+    func = self->priv->allow_scrolling ? js_funcs[ADD_CONSECUTIVE_MSG_SCROLL] :
+      js_funcs[ADD_CONSECUTIVE_MSG_NO_SCROLL];
   else
-    func = self->priv->allow_scrolling ? "appendMessage" : "appendMessageNoScroll";
+    func = self->priv->allow_scrolling ? js_funcs[ADD_MSG_SCROLL] :
+      js_funcs[ADD_MSG_NO_SCROLL];
 
   if (empathy_contact_is_user (sender))
     {
@@ -1024,38 +1066,64 @@ empathy_theme_adium_append_message (EmpathyThemeAdium *self,
           self->priv->data->in_content_html;
     }
 
-  theme_adium_append_html (self, func, html, body_escaped,
+  direction = pango_find_base_dir (empathy_message_get_body (msg), -1);
+
+  theme_adium_add_html (self, func, html, body_escaped,
       avatar_filename, name_escaped, contact_id,
       service_name, message_classes->str,
-      timestamp, is_backlog, empathy_contact_is_user (sender));
+      timestamp, is_backlog, empathy_contact_is_user (sender), direction);
 
   /* Keep the sender of the last displayed message */
-  if (self->priv->last_contact)
-    g_object_unref (self->priv->last_contact);
+  if (*prev_contact)
+    g_object_unref (*prev_contact);
 
-  self->priv->last_contact = g_object_ref (sender);
-  self->priv->last_timestamp = timestamp;
-  self->priv->last_is_backlog = is_backlog;
+  *prev_contact = g_object_ref (sender);
+  *prev_timestamp = timestamp;
+  *prev_is_backlog = is_backlog;
 
   g_free (body_escaped);
   g_free (name_escaped);
   g_string_free (message_classes, TRUE);
 }
 
+void
+empathy_theme_adium_append_message (EmpathyThemeAdium *self,
+    EmpathyMessage *msg,
+    gboolean should_highlight)
+{
+  const gchar *js_funcs[] = { "appendNextMessage",
+      "appendNextMessageNoScroll",
+      "appendMessage",
+      "appendMessageNoScroll" };
+
+  if (self->priv->pages_loading != 0)
+    {
+      queue_item (&self->priv->message_queue, QUEUED_MESSAGE, msg, NULL,
+          should_highlight, FALSE);
+      return;
+    }
+
+  theme_adium_add_message (self, msg, &self->priv->last_contact,
+      &self->priv->last_timestamp, &self->priv->last_is_backlog,
+      should_highlight, js_funcs);
+}
+
 void
 empathy_theme_adium_append_event (EmpathyThemeAdium *self,
     const gchar *str)
 {
   gchar *str_escaped;
+  PangoDirection direction;
 
   if (self->priv->pages_loading != 0)
     {
-      queue_item (&self->priv->message_queue, QUEUED_EVENT, NULL, str, FALSE);
+      queue_item (&self->priv->message_queue, QUEUED_EVENT, NULL, str, FALSE, FALSE);
       return;
     }
 
+  direction = pango_find_base_dir (str, -1);
   str_escaped = g_markup_escape_text (str, -1);
-  theme_adium_append_event_escaped (self, str_escaped);
+  theme_adium_append_event_escaped (self, str_escaped, direction);
   g_free (str_escaped);
 }
 
@@ -1064,7 +1132,32 @@ empathy_theme_adium_append_event_markup (EmpathyThemeAdium *self,
     const gchar *markup_text,
     const gchar *fallback_text)
 {
-  theme_adium_append_event_escaped (self, markup_text);
+  PangoDirection direction;
+
+  direction = pango_find_base_dir (fallback_text, -1);
+  theme_adium_append_event_escaped (self, markup_text, direction);
+}
+
+void
+empathy_theme_adium_prepend_message (EmpathyThemeAdium *self,
+    EmpathyMessage *msg,
+    gboolean should_highlight)
+{
+  const gchar *js_funcs[] = { "prependPrev",
+      "prependPrev",
+      "prepend",
+      "prepend" };
+
+  if (self->priv->pages_loading != 0)
+    {
+      queue_item (&self->priv->message_queue, QUEUED_MESSAGE, msg, NULL,
+          should_highlight, TRUE);
+      return;
+    }
+
+  theme_adium_add_message (self, msg, &self->priv->first_contact,
+      &self->priv->first_timestamp, &self->priv->first_is_backlog,
+      should_highlight, js_funcs);
 }
 
 void
@@ -1080,7 +1173,7 @@ empathy_theme_adium_edit_message (EmpathyThemeAdium *self,
 
   if (self->priv->pages_loading != 0)
     {
-      queue_item (&self->priv->message_queue, QUEUED_EDIT, message, NULL, FALSE);
+      queue_item (&self->priv->message_queue, QUEUED_EDIT, message, NULL, FALSE, FALSE);
       return;
     }
 
@@ -1119,7 +1212,7 @@ empathy_theme_adium_edit_message (EmpathyThemeAdium *self,
     }
 
   /* set a tooltip */
-  timestamp = empathy_time_to_string_local (
+  timestamp = tpaw_time_to_string_local (
     empathy_message_get_timestamp (message),
     "%H:%M:%S");
   tooltip = g_strdup_printf (_("Message edited at %s"), timestamp);
@@ -1156,7 +1249,7 @@ empathy_theme_adium_edit_message (EmpathyThemeAdium *self,
         }
 
       g_free (style);
-      gtk_icon_info_free (icon_info);
+      g_object_unref (icon_info);
     }
 
   goto finally;
@@ -1359,31 +1452,22 @@ theme_adium_context_menu_cb (EmpathyThemeAdium *self,
     gboolean triggered_with_keyboard,
     gpointer user_data)
 {
-  gboolean developer_tools_enabled;
-
-  g_object_get (
-      G_OBJECT (webkit_web_view_get_settings (WEBKIT_WEB_VIEW (self))),
-      "enable-developer-extras", &developer_tools_enabled, NULL);
+  GtkWidget *menu;
+  EmpathyWebKitMenuFlags flags = EMPATHY_WEBKIT_MENU_CLEAR;
 
-  /* We currently have no way to add an inspector menu
-   * item ourselves, so we disable our customized menu
-   * if the developer extras are enabled. */
-  if (!developer_tools_enabled)
-    {
-      GtkWidget *menu;
+  if (g_settings_get_boolean (self->priv->gsettings_chat,
+        EMPATHY_PREFS_CHAT_WEBKIT_DEVELOPER_TOOLS))
+    flags |= EMPATHY_WEBKIT_MENU_INSPECT;
 
-      menu = empathy_webkit_create_context_menu (
-        WEBKIT_WEB_VIEW (self), hit_test_result,
-        EMPATHY_WEBKIT_MENU_CLEAR);
+  menu = empathy_webkit_create_context_menu (
+    WEBKIT_WEB_VIEW (self), hit_test_result, flags);
 
-      gtk_widget_show_all (menu);
-      gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL, 3,
-          gtk_get_current_event_time ());
+  gtk_widget_show_all (menu);
 
-      return TRUE;
-    }
+  gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL, 3,
+      gtk_get_current_event_time ());
 
-  return FALSE;
+  return TRUE;
 }
 
 void
@@ -1460,6 +1544,8 @@ theme_adium_dispose (GObject *object)
       self->priv->smiley_manager = NULL;
     }
 
+  g_clear_object (&self->priv->first_contact);
+
   if (self->priv->last_contact)
     {
       g_object_unref (self->priv->last_contact);
@@ -1689,13 +1775,6 @@ empathy_theme_adium_init (EmpathyThemeAdium *self)
   self->priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
   self->priv->gsettings_desktop = g_settings_new (
     EMPATHY_PREFS_DESKTOP_INTERFACE_SCHEMA);
-
-  g_signal_connect (self->priv->gsettings_chat,
-    "changed::" EMPATHY_PREFS_CHAT_WEBKIT_DEVELOPER_TOOLS,
-    G_CALLBACK (theme_adium_notify_enable_webkit_developer_tools_cb),
-    self);
-
-  theme_adium_update_enable_webkit_developer_tools (self);
 }
 
 EmpathyThemeAdium *