]> git.0d.be Git - empathy.git/blob - tp-account-widgets/tpaw-utils.c
70f40868e3f768f096addebe228776e8638d8898
[empathy.git] / tp-account-widgets / tpaw-utils.c
1 /*
2  * Copyright (C) 2007-2013 Collabora Ltd.
3  * Copyright (C) 2005-2006 Imendio AB
4  * Copyright (C) 2006 Xavier Claessens <xavier.claessens@gmail.com>
5  * Copyright (C) 2009 Steve Frécinaux <code@istique.net>
6  *
7  * Authors: Marco Barisione <marco.barisione@collabora.co.uk>
8  *          Guillaume Desmottes <guillaume.desmottes@collabora.co.uk>
9  *          Sjoerd Simons <sjoerd.simons@collabora.co.uk>
10  *          Xavier Claessens <xavier.claessens@collabora.co.uk>
11  *          Mikael Hallendal <micke@imendio.com>
12  *          Richard Hult <richard@imendio.com>
13  *          Martyn Russell <martyn@imendio.com>
14  *          Steve Frécinaux <code@istique.net>
15  *          Emanuele Aina <emanuele.aina@collabora.co.uk>
16  *
17  * This library is free software; you can redistribute it and/or
18  * modify it under the terms of the GNU Lesser General Public
19  * License as published by the Free Software Foundation; either
20  * version 2.1 of the License, or (at your option) any later version.
21  *
22  * This library is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25  * Lesser General Public License for more details.
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
30  */
31
32 #include "config.h"
33 #include "tpaw-utils.h"
34
35 #include <glib/gi18n-lib.h>
36 #include <gdk/gdkx.h>
37
38 #define DEBUG_FLAG TPAW_DEBUG_OTHER
39 #include "tpaw-debug.h"
40
41 #define TPAW_RECT_IS_ON_SCREEN(x,y,w,h) ((x) + (w) > 0 && \
42               (y) + (h) > 0 && \
43               (x) < gdk_screen_width () && \
44               (y) < gdk_screen_height ())
45
46 /* Change the RequestedPresence of a newly created account to ensure that it
47  * is actually connected. */
48 void
49 tpaw_connect_new_account (TpAccount *account,
50     TpAccountManager *account_manager)
51 {
52   TpConnectionPresenceType presence;
53   gchar *status, *message;
54
55   /* only force presence if presence was offline, unknown or unset */
56   presence = tp_account_get_requested_presence (account, NULL, NULL);
57   switch (presence)
58     {
59       case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
60       case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
61       case TP_CONNECTION_PRESENCE_TYPE_UNSET:
62         presence = tp_account_manager_get_most_available_presence (
63             account_manager, &status, &message);
64
65         if (presence == TP_CONNECTION_PRESENCE_TYPE_OFFLINE)
66           /* Global presence is offline; we force it so user doesn't have to
67            * manually change the presence to connect his new account. */
68           presence = TP_CONNECTION_PRESENCE_TYPE_AVAILABLE;
69
70         tp_account_request_presence_async (account, presence,
71             status, NULL, NULL, NULL);
72
73         g_free (status);
74         g_free (message);
75         break;
76
77        case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
78        case TP_CONNECTION_PRESENCE_TYPE_AWAY:
79        case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
80        case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
81        case TP_CONNECTION_PRESENCE_TYPE_BUSY:
82        case TP_CONNECTION_PRESENCE_TYPE_ERROR:
83        default:
84         /* do nothing if the presence is not offline */
85         break;
86     }
87 }
88
89 gchar *
90 tpaw_protocol_icon_name (const gchar *protocol)
91 {
92   if (!tp_strdiff (protocol, "yahoojp"))
93     /* Yahoo Japan uses the same icon as Yahoo */
94     protocol = "yahoo";
95   else if (!tp_strdiff (protocol, "simple"))
96     /* SIMPLE uses the same icon as SIP */
97     protocol = "sip";
98   else if (!tp_strdiff (protocol, "sms"))
99     return g_strdup ("phone");
100
101   return g_strdup_printf ("im-%s", protocol);
102 }
103
104 const char *
105 tpaw_protocol_name_to_display_name (const gchar *proto_name)
106 {
107   int i;
108   static struct {
109     const gchar *proto;
110     const gchar *display;
111     gboolean translated;
112   } names[] = {
113     { "jabber", "Jabber", FALSE },
114     { "msn", "Windows Live (MSN)", FALSE, },
115     { "local-xmpp", N_("People Nearby"), TRUE },
116     { "irc", "IRC", FALSE },
117     { "icq", "ICQ", FALSE },
118     { "aim", "AIM", FALSE },
119     { "yahoo", "Yahoo!", FALSE },
120     { "yahoojp", N_("Yahoo! Japan"), TRUE },
121     { "groupwise", "GroupWise", FALSE },
122     { "sip", "SIP", FALSE },
123     { "gadugadu", "Gadu-Gadu", FALSE },
124     { "mxit", "Mxit", FALSE },
125     { "myspace", "Myspace", FALSE },
126     { "sametime", "Sametime", FALSE },
127     { "skype-dbus", "Skype (D-BUS)", FALSE },
128     { "skype-x11", "Skype (X11)", FALSE },
129     { "zephyr", "Zephyr", FALSE },
130     { NULL, NULL }
131   };
132
133   for (i = 0; names[i].proto != NULL; i++)
134     {
135       if (!tp_strdiff (proto_name, names[i].proto))
136         {
137           if (names[i].translated)
138             return gettext (names[i].display);
139           else
140             return names[i].display;
141         }
142     }
143
144   return proto_name;
145 }
146
147 const char *
148 tpaw_service_name_to_display_name (const gchar *service_name)
149 {
150   int i;
151   static struct {
152     const gchar *service;
153     const gchar *display;
154     gboolean translated;
155   } names[] = {
156     { "google-talk", N_("Google Talk"), FALSE },
157     { "facebook", N_("Facebook Chat"), TRUE },
158     { NULL, NULL }
159   };
160
161   for (i = 0; names[i].service != NULL; i++)
162     {
163       if (!tp_strdiff (service_name, names[i].service))
164         {
165           if (names[i].translated)
166             return gettext (names[i].display);
167           else
168             return names[i].display;
169         }
170     }
171
172   return service_name;
173 }
174
175 void
176 tpaw_make_color_whiter (GdkRGBA *color)
177 {
178   const GdkRGBA white = { 1.0, 1.0, 1.0, 1.0 };
179
180   color->red = (color->red + white.red) / 2;
181   color->green = (color->green + white.green) / 2;
182   color->blue = (color->blue + white.blue) / 2;
183 }
184
185 gboolean
186 tpaw_xml_validate_from_resource (xmlDoc *doc,
187     const gchar *dtd_resourcename)
188 {
189   GBytes *resourcecontents;
190   gconstpointer resourcedata;
191   gsize resourcesize;
192   xmlParserInputBufferPtr buffer;
193   xmlValidCtxt  cvp;
194   xmlDtd *dtd;
195   GError *error = NULL;
196   gboolean ret;
197
198   DEBUG ("Loading dtd resource %s", dtd_resourcename);
199
200   resourcecontents = g_resources_lookup_data (dtd_resourcename, G_RESOURCE_LOOKUP_FLAGS_NONE, &error);
201   if (error != NULL)
202     {
203       g_warning ("Unable to load dtd resource '%s': %s", dtd_resourcename, error->message);
204       g_error_free (error);
205       return FALSE;
206     }
207   resourcedata = g_bytes_get_data (resourcecontents, &resourcesize);
208   buffer = xmlParserInputBufferCreateStatic (resourcedata, resourcesize, XML_CHAR_ENCODING_UTF8);
209
210   memset (&cvp, 0, sizeof (cvp));
211   dtd = xmlIOParseDTD (NULL, buffer, XML_CHAR_ENCODING_UTF8);
212   ret = xmlValidateDtd (&cvp, doc, dtd);
213
214   xmlFreeDtd (dtd);
215   g_bytes_unref (resourcecontents);
216
217   return ret;
218 }
219
220 /* Takes care of moving the window to the current workspace. */
221 void
222 tpaw_window_present_with_time (GtkWindow *window,
223     guint32 timestamp)
224 {
225   GdkWindow *gdk_window;
226
227   g_return_if_fail (GTK_IS_WINDOW (window));
228
229   /* Move the window to the current workspace before trying to show it.
230    * This is the behaviour people expect when clicking on the statusbar icon. */
231   gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
232
233   if (gdk_window)
234     {
235       gint x, y;
236       gint w, h;
237
238       /* Has no effect if the WM has viewports, like compiz */
239       gdk_x11_window_move_to_current_desktop (gdk_window);
240
241       /* If window is still off-screen, hide it to force it to
242        * reposition on the current workspace. */
243       gtk_window_get_position (window, &x, &y);
244       gtk_window_get_size (window, &w, &h);
245       if (!TPAW_RECT_IS_ON_SCREEN (x, y, w, h))
246         gtk_widget_hide (GTK_WIDGET (window));
247     }
248
249   if (timestamp == GDK_CURRENT_TIME)
250     gtk_window_present (window);
251   else
252     gtk_window_present_with_time (window, timestamp);
253 }
254
255 void
256 tpaw_window_present (GtkWindow *window)
257 {
258   tpaw_window_present_with_time (window, gtk_get_current_event_time ());
259 }
260
261 GtkWindow *
262 tpaw_get_toplevel_window (GtkWidget *widget)
263 {
264   GtkWidget *toplevel;
265
266   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
267
268   toplevel = gtk_widget_get_toplevel (widget);
269   if (GTK_IS_WINDOW (toplevel) &&
270       gtk_widget_is_toplevel (toplevel))
271     return GTK_WINDOW (toplevel);
272
273   return NULL;
274 }
275
276 /** tpaw_make_absolute_url_len:
277  * @url: an url
278  * @len: a length
279  *
280  * Same as #tpaw_make_absolute_url but for a limited string length
281  */
282 gchar *
283 tpaw_make_absolute_url_len (const gchar *url,
284     guint len)
285 {
286   g_return_val_if_fail (url != NULL, NULL);
287
288   if (g_str_has_prefix (url, "help:") ||
289       g_str_has_prefix (url, "mailto:") ||
290       strstr (url, ":/"))
291     return g_strndup (url, len);
292
293   if (strstr (url, "@"))
294     return g_strdup_printf ("mailto:%.*s", len, url);
295
296   return g_strdup_printf ("http://%.*s", len, url);
297 }
298
299 /** tpaw_make_absolute_url:
300  * @url: an url
301  *
302  * The URL opening code can't handle schemeless strings, so we try to be
303  * smart and add http if there is no scheme or doesn't look like a mail
304  * address. This should work in most cases, and let us click on strings
305  * like "www.gnome.org".
306  *
307  * Returns: a newly allocated url with proper mailto: or http:// prefix, use
308  * g_free when your are done with it
309  */
310 gchar *
311 tpaw_make_absolute_url (const gchar *url)
312 {
313   return tpaw_make_absolute_url_len (url, strlen (url));
314 }