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>
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>
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.
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.
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
33 #include "tpaw-utils.h"
35 #include <glib/gi18n-lib.h>
38 #define DEBUG_FLAG TPAW_DEBUG_OTHER
39 #include "tpaw-debug.h"
41 #define TPAW_RECT_IS_ON_SCREEN(x,y,w,h) ((x) + (w) > 0 && \
43 (x) < gdk_screen_width () && \
44 (y) < gdk_screen_height ())
46 /* Change the RequestedPresence of a newly created account to ensure that it
47 * is actually connected. */
49 tpaw_connect_new_account (TpAccount *account,
50 TpAccountManager *account_manager)
52 TpConnectionPresenceType presence;
53 gchar *status, *message;
55 /* only force presence if presence was offline, unknown or unset */
56 presence = tp_account_get_requested_presence (account, NULL, NULL);
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);
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;
70 tp_account_request_presence_async (account, presence,
71 status, NULL, NULL, NULL);
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:
84 /* do nothing if the presence is not offline */
90 tpaw_protocol_icon_name (const gchar *protocol)
92 if (!tp_strdiff (protocol, "yahoojp"))
93 /* Yahoo Japan uses the same icon as Yahoo */
95 else if (!tp_strdiff (protocol, "simple"))
96 /* SIMPLE uses the same icon as SIP */
98 else if (!tp_strdiff (protocol, "sms"))
99 return g_strdup ("phone");
101 return g_strdup_printf ("im-%s", protocol);
105 tpaw_protocol_name_to_display_name (const gchar *proto_name)
110 const gchar *display;
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 },
133 for (i = 0; names[i].proto != NULL; i++)
135 if (!tp_strdiff (proto_name, names[i].proto))
137 if (names[i].translated)
138 return gettext (names[i].display);
140 return names[i].display;
148 tpaw_service_name_to_display_name (const gchar *service_name)
152 const gchar *service;
153 const gchar *display;
156 { "google-talk", N_("Google Talk"), FALSE },
157 { "facebook", N_("Facebook Chat"), TRUE },
161 for (i = 0; names[i].service != NULL; i++)
163 if (!tp_strdiff (service_name, names[i].service))
165 if (names[i].translated)
166 return gettext (names[i].display);
168 return names[i].display;
176 tpaw_make_color_whiter (GdkRGBA *color)
178 const GdkRGBA white = { 1.0, 1.0, 1.0, 1.0 };
180 color->red = (color->red + white.red) / 2;
181 color->green = (color->green + white.green) / 2;
182 color->blue = (color->blue + white.blue) / 2;
186 tpaw_xml_validate_from_resource (xmlDoc *doc,
187 const gchar *dtd_resourcename)
189 GBytes *resourcecontents;
190 gconstpointer resourcedata;
192 xmlParserInputBufferPtr buffer;
195 GError *error = NULL;
198 DEBUG ("Loading dtd resource %s", dtd_resourcename);
200 resourcecontents = g_resources_lookup_data (dtd_resourcename, G_RESOURCE_LOOKUP_FLAGS_NONE, &error);
203 g_warning ("Unable to load dtd resource '%s': %s", dtd_resourcename, error->message);
204 g_error_free (error);
207 resourcedata = g_bytes_get_data (resourcecontents, &resourcesize);
208 buffer = xmlParserInputBufferCreateStatic (resourcedata, resourcesize, XML_CHAR_ENCODING_UTF8);
210 memset (&cvp, 0, sizeof (cvp));
211 dtd = xmlIOParseDTD (NULL, buffer, XML_CHAR_ENCODING_UTF8);
212 ret = xmlValidateDtd (&cvp, doc, dtd);
215 g_bytes_unref (resourcecontents);
220 /* Takes care of moving the window to the current workspace. */
222 tpaw_window_present_with_time (GtkWindow *window,
225 GdkWindow *gdk_window;
227 g_return_if_fail (GTK_IS_WINDOW (window));
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));
238 /* Has no effect if the WM has viewports, like compiz */
239 gdk_x11_window_move_to_current_desktop (gdk_window);
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));
249 if (timestamp == GDK_CURRENT_TIME)
250 gtk_window_present (window);
252 gtk_window_present_with_time (window, timestamp);
256 tpaw_window_present (GtkWindow *window)
258 tpaw_window_present_with_time (window, gtk_get_current_event_time ());
262 tpaw_get_toplevel_window (GtkWidget *widget)
266 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
268 toplevel = gtk_widget_get_toplevel (widget);
269 if (GTK_IS_WINDOW (toplevel) &&
270 gtk_widget_is_toplevel (toplevel))
271 return GTK_WINDOW (toplevel);
276 /** tpaw_make_absolute_url_len:
280 * Same as #tpaw_make_absolute_url but for a limited string length
283 tpaw_make_absolute_url_len (const gchar *url,
286 g_return_val_if_fail (url != NULL, NULL);
288 if (g_str_has_prefix (url, "help:") ||
289 g_str_has_prefix (url, "mailto:") ||
291 return g_strndup (url, len);
293 if (strstr (url, "@"))
294 return g_strdup_printf ("mailto:%.*s", len, url);
296 return g_strdup_printf ("http://%.*s", len, url);
299 /** tpaw_make_absolute_url:
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".
307 * Returns: a newly allocated url with proper mailto: or http:// prefix, use
308 * g_free when your are done with it
311 tpaw_make_absolute_url (const gchar *url)
313 return tpaw_make_absolute_url_len (url, strlen (url));