2007-12-16 22:20:24 +00:00
|
|
|
/*
|
2009-02-16 23:16:14 +00:00
|
|
|
Copyright (C) 2007-2009 Christian Dywan <christian@twotoasts.de>
|
2009-04-25 13:57:00 +00:00
|
|
|
Copyright (C) 2009 Dale Whittaker <dayul@users.sf.net>
|
2007-12-16 22:20:24 +00:00
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
See the file COPYING for the full license text.
|
|
|
|
*/
|
|
|
|
|
2008-10-25 22:54:57 +00:00
|
|
|
#include "sokoke.h"
|
2009-02-21 21:36:03 +00:00
|
|
|
#include "midori-stock.h"
|
2008-10-25 22:54:57 +00:00
|
|
|
|
2008-08-15 00:03:55 +00:00
|
|
|
#if HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
2007-12-16 22:20:24 +00:00
|
|
|
|
2008-08-23 21:44:55 +00:00
|
|
|
#if HAVE_UNISTD_H
|
2008-06-01 13:15:38 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2009-02-16 23:16:14 +00:00
|
|
|
#include <stdlib.h>
|
2007-12-16 22:20:24 +00:00
|
|
|
#include <string.h>
|
2008-08-27 16:04:51 +00:00
|
|
|
|
2007-12-16 22:20:24 +00:00
|
|
|
#include <gdk/gdkkeysyms.h>
|
2008-03-22 02:38:23 +00:00
|
|
|
#include <glib/gi18n.h>
|
2008-04-20 04:41:26 +00:00
|
|
|
#include <glib/gprintf.h>
|
2009-04-11 03:14:53 +00:00
|
|
|
#include <glib/gstdio.h>
|
2008-04-20 04:41:26 +00:00
|
|
|
|
2009-02-16 23:16:14 +00:00
|
|
|
#if HAVE_LIBIDN
|
|
|
|
#include <stringprep.h>
|
|
|
|
#include <punycode.h>
|
|
|
|
#include <idna.h>
|
|
|
|
#endif
|
|
|
|
|
2008-12-24 23:41:11 +00:00
|
|
|
static gchar*
|
|
|
|
sokoke_js_string_utf8 (JSStringRef js_string)
|
|
|
|
{
|
|
|
|
size_t size_utf8;
|
|
|
|
gchar* string_utf8;
|
|
|
|
|
|
|
|
g_return_val_if_fail (js_string, NULL);
|
|
|
|
|
|
|
|
size_utf8 = JSStringGetMaximumUTF8CStringSize (js_string);
|
|
|
|
string_utf8 = g_new (gchar, size_utf8);
|
|
|
|
JSStringGetUTF8CString (js_string, string_utf8, size_utf8);
|
|
|
|
return string_utf8;
|
|
|
|
}
|
|
|
|
|
2009-04-13 20:40:57 +00:00
|
|
|
gchar*
|
2008-12-24 23:41:11 +00:00
|
|
|
sokoke_js_script_eval (JSContextRef js_context,
|
|
|
|
const gchar* script,
|
|
|
|
gchar** exception)
|
|
|
|
{
|
2009-04-13 20:40:57 +00:00
|
|
|
gchar* value;
|
|
|
|
JSStringRef js_value_string;
|
|
|
|
|
2008-12-24 23:41:11 +00:00
|
|
|
g_return_val_if_fail (js_context, FALSE);
|
|
|
|
g_return_val_if_fail (script, FALSE);
|
|
|
|
|
|
|
|
JSStringRef js_script = JSStringCreateWithUTF8CString (script);
|
|
|
|
JSValueRef js_exception = NULL;
|
|
|
|
JSValueRef js_value = JSEvaluateScript (js_context, js_script,
|
|
|
|
JSContextGetGlobalObject (js_context), NULL, 0, &js_exception);
|
|
|
|
if (!js_value && exception)
|
|
|
|
{
|
|
|
|
JSStringRef js_message = JSValueToStringCopy (js_context,
|
|
|
|
js_exception, NULL);
|
|
|
|
*exception = sokoke_js_string_utf8 (js_message);
|
|
|
|
JSStringRelease (js_message);
|
|
|
|
js_value = JSValueMakeNull (js_context);
|
|
|
|
}
|
|
|
|
JSStringRelease (js_script);
|
2009-04-13 20:40:57 +00:00
|
|
|
|
|
|
|
js_value_string = JSValueToStringCopy (js_context, js_value, NULL);
|
|
|
|
value = sokoke_js_string_utf8 (js_value_string);
|
|
|
|
JSStringRelease (js_value_string);
|
|
|
|
return value;
|
2008-12-24 23:41:11 +00:00
|
|
|
}
|
|
|
|
|
2008-07-16 20:32:37 +00:00
|
|
|
static void
|
|
|
|
error_dialog (const gchar* short_message,
|
|
|
|
const gchar* detailed_message)
|
|
|
|
{
|
|
|
|
GtkWidget* dialog = gtk_message_dialog_new (
|
2008-11-12 01:15:52 +00:00
|
|
|
NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", short_message);
|
2008-07-16 20:32:37 +00:00
|
|
|
gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
|
|
|
|
"%s", detailed_message);
|
|
|
|
gtk_widget_show (dialog);
|
|
|
|
g_signal_connect_swapped (dialog, "response",
|
|
|
|
G_CALLBACK (gtk_widget_destroy), dialog);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-04-20 17:10:13 +00:00
|
|
|
/**
|
|
|
|
* sokoke_show_uri:
|
|
|
|
* @screen: a #GdkScreen, or %NULL
|
|
|
|
* @uri: the URI to show
|
|
|
|
* @timestamp: the timestamp of the event
|
|
|
|
* @error: the location of a #GError, or %NULL
|
|
|
|
*
|
|
|
|
* Shows the specified URI with an appropriate application. This
|
|
|
|
* supports xdg-open, exo-open and gnome-open as fallbacks if
|
|
|
|
* GIO doesn't do the trick.
|
|
|
|
*
|
|
|
|
* Return value: %TRUE on success, %FALSE if an error occurred
|
|
|
|
**/
|
|
|
|
gboolean
|
|
|
|
sokoke_show_uri (GdkScreen* screen,
|
|
|
|
const gchar* uri,
|
|
|
|
guint32 timestamp,
|
|
|
|
GError** error)
|
|
|
|
{
|
|
|
|
const gchar* fallbacks [] = { "xdg-open", "exo-open", "gnome-open" };
|
|
|
|
gsize i;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GDK_IS_SCREEN (screen) || !screen, FALSE);
|
|
|
|
g_return_val_if_fail (uri != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (!error || !*error, FALSE);
|
|
|
|
|
|
|
|
if (gtk_show_uri (screen, uri, timestamp, error))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
for (i = 0; i < G_N_ELEMENTS (fallbacks); i++)
|
|
|
|
{
|
|
|
|
gchar* command = g_strconcat (fallbacks[i], " ", uri, NULL);
|
|
|
|
gboolean result = g_spawn_command_line_async (command, error);
|
|
|
|
g_free (command);
|
|
|
|
if (result)
|
|
|
|
return TRUE;
|
2009-04-24 18:13:57 +00:00
|
|
|
if (error)
|
|
|
|
*error = NULL;
|
2009-04-20 17:10:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2008-07-16 20:32:37 +00:00
|
|
|
gboolean
|
|
|
|
sokoke_spawn_program (const gchar* command,
|
2009-04-26 22:22:16 +00:00
|
|
|
const gchar* argument,
|
|
|
|
gboolean quote)
|
2008-07-16 20:32:37 +00:00
|
|
|
{
|
|
|
|
gchar* argument_escaped;
|
|
|
|
gchar* command_ready;
|
|
|
|
gchar** argv;
|
|
|
|
GError* error;
|
|
|
|
|
2008-10-25 22:54:57 +00:00
|
|
|
g_return_val_if_fail (command != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (argument != NULL, FALSE);
|
|
|
|
|
2009-04-26 22:22:16 +00:00
|
|
|
argument_escaped = quote ? g_shell_quote (argument) : g_strdup (argument);
|
2008-07-16 20:32:37 +00:00
|
|
|
if (strstr (command, "%s"))
|
|
|
|
command_ready = g_strdup_printf (command, argument_escaped);
|
|
|
|
else
|
|
|
|
command_ready = g_strconcat (command, " ", argument_escaped, NULL);
|
|
|
|
|
|
|
|
error = NULL;
|
|
|
|
if (!g_shell_parse_argv (command_ready, NULL, &argv, &error))
|
|
|
|
{
|
|
|
|
error_dialog (_("Could not run external program."), error->message);
|
|
|
|
g_error_free (error);
|
|
|
|
g_free (command_ready);
|
|
|
|
g_free (argument_escaped);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = NULL;
|
|
|
|
if (!g_spawn_async (NULL, argv, NULL,
|
|
|
|
(GSpawnFlags)G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
|
|
|
|
NULL, NULL, NULL, &error))
|
|
|
|
{
|
|
|
|
error_dialog (_("Could not run external program."), error->message);
|
|
|
|
g_error_free (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_strfreev (argv);
|
|
|
|
g_free (command_ready);
|
|
|
|
g_free (argument_escaped);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2009-02-16 23:16:14 +00:00
|
|
|
static gchar*
|
|
|
|
sokoke_idn_to_punycode (gchar* uri)
|
|
|
|
{
|
|
|
|
#if HAVE_LIBIDN
|
2009-02-23 22:44:30 +00:00
|
|
|
gchar* proto;
|
2009-02-16 23:16:14 +00:00
|
|
|
gchar* hostname;
|
2009-02-23 22:44:30 +00:00
|
|
|
gchar* path;
|
2009-02-16 23:16:14 +00:00
|
|
|
char *s;
|
|
|
|
uint32_t *q;
|
|
|
|
int rc;
|
|
|
|
gchar *result;
|
|
|
|
|
2009-02-23 22:44:30 +00:00
|
|
|
if ((proto = g_utf8_strchr (uri, -1, ':')))
|
|
|
|
{
|
2009-02-27 19:59:23 +00:00
|
|
|
gulong offset;
|
|
|
|
gchar* buffer;
|
|
|
|
|
|
|
|
/* 'file' URIs don't have a hostname */
|
|
|
|
if (!strcmp (proto, "file"))
|
|
|
|
return uri;
|
|
|
|
|
|
|
|
offset = g_utf8_pointer_to_offset (uri, proto);
|
|
|
|
buffer = g_malloc0 (offset + 1);
|
2009-02-23 22:44:30 +00:00
|
|
|
g_utf8_strncpy (buffer, uri, offset);
|
|
|
|
proto = buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
path = NULL;
|
|
|
|
if ((hostname = g_utf8_strchr (uri, -1, '/')))
|
|
|
|
{
|
|
|
|
if (hostname[1] == '/')
|
|
|
|
hostname += 2;
|
|
|
|
if ((path = g_utf8_strchr (hostname, -1, '/')))
|
|
|
|
{
|
|
|
|
gulong offset = g_utf8_pointer_to_offset (hostname, path);
|
2009-02-27 19:59:23 +00:00
|
|
|
gchar* buffer = g_malloc0 (offset + 1);
|
2009-02-23 22:44:30 +00:00
|
|
|
g_utf8_strncpy (buffer, hostname, offset);
|
|
|
|
hostname = buffer;
|
|
|
|
}
|
|
|
|
}
|
2009-02-16 23:16:14 +00:00
|
|
|
else
|
2009-02-27 19:59:23 +00:00
|
|
|
hostname = g_strdup (uri);
|
2009-02-16 23:16:14 +00:00
|
|
|
|
|
|
|
if (!(q = stringprep_utf8_to_ucs4 (hostname, -1, NULL)))
|
2009-02-27 19:59:23 +00:00
|
|
|
{
|
|
|
|
g_free (proto);
|
|
|
|
g_free (hostname);
|
2009-02-16 23:16:14 +00:00
|
|
|
return uri;
|
2009-02-27 19:59:23 +00:00
|
|
|
}
|
2009-02-16 23:16:14 +00:00
|
|
|
|
|
|
|
rc = idna_to_ascii_4z (q, &s, IDNA_ALLOW_UNASSIGNED);
|
|
|
|
free (q);
|
|
|
|
if (rc != IDNA_SUCCESS)
|
2009-02-27 19:59:23 +00:00
|
|
|
{
|
|
|
|
g_free (proto);
|
|
|
|
g_free (hostname);
|
2009-02-16 23:16:14 +00:00
|
|
|
return uri;
|
2009-02-27 19:59:23 +00:00
|
|
|
}
|
2009-02-16 23:16:14 +00:00
|
|
|
|
2009-02-23 22:44:30 +00:00
|
|
|
if (proto)
|
|
|
|
{
|
2009-03-31 01:28:01 +00:00
|
|
|
result = g_strconcat (proto, "://", s, path ? path : "", NULL);
|
2009-02-23 22:44:30 +00:00
|
|
|
g_free (proto);
|
|
|
|
if (path)
|
|
|
|
g_free (hostname);
|
|
|
|
}
|
2009-02-16 23:16:14 +00:00
|
|
|
else
|
|
|
|
result = g_strdup (s);
|
|
|
|
g_free (uri);
|
|
|
|
free (s);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
#else
|
|
|
|
return uri;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-04-19 22:39:36 +00:00
|
|
|
/**
|
|
|
|
* sokoke_search_uri:
|
|
|
|
* @uri: a search URI with or without %s
|
|
|
|
* @keywords: keywords
|
|
|
|
*
|
|
|
|
* Takes a search engine URI and inserts the specified
|
|
|
|
* keywords. The @keywords are percent encoded. If the
|
|
|
|
* search URI contains a %s they keywords are inserted
|
|
|
|
* in that place, otherwise appended to the URI.
|
|
|
|
*
|
|
|
|
* Return value: a newly allocated search URI
|
|
|
|
**/
|
|
|
|
gchar* sokoke_search_uri (const gchar* uri,
|
|
|
|
const gchar* keywords)
|
|
|
|
{
|
|
|
|
gchar* escaped;
|
|
|
|
gchar* search;
|
|
|
|
|
|
|
|
g_return_val_if_fail (uri != NULL, NULL);
|
|
|
|
g_return_val_if_fail (keywords != NULL, NULL);
|
|
|
|
|
|
|
|
escaped = g_uri_escape_string (keywords, " :/", TRUE);
|
|
|
|
if (strstr (uri, "%s"))
|
|
|
|
search = g_strdup_printf (uri, escaped);
|
|
|
|
else
|
|
|
|
search = g_strconcat (uri, escaped, NULL);
|
|
|
|
g_free (escaped);
|
|
|
|
return search;
|
|
|
|
}
|
|
|
|
|
2008-04-20 19:44:56 +00:00
|
|
|
gchar*
|
2008-08-25 23:19:38 +00:00
|
|
|
sokoke_magic_uri (const gchar* uri,
|
|
|
|
KatzeArray* search_engines)
|
2008-04-20 19:44:56 +00:00
|
|
|
{
|
2008-06-14 00:23:33 +00:00
|
|
|
gchar* current_dir;
|
|
|
|
gchar* result;
|
2008-11-14 01:46:23 +00:00
|
|
|
gchar** parts;
|
2008-06-14 00:23:33 +00:00
|
|
|
gchar* search;
|
|
|
|
const gchar* search_uri;
|
2008-08-23 16:32:53 +00:00
|
|
|
KatzeItem* item;
|
2008-06-14 00:23:33 +00:00
|
|
|
|
2008-06-19 22:06:12 +00:00
|
|
|
g_return_val_if_fail (uri, NULL);
|
2008-11-12 01:15:52 +00:00
|
|
|
g_return_val_if_fail (!search_engines ||
|
|
|
|
katze_array_is_a (search_engines, KATZE_TYPE_ITEM), NULL);
|
2008-06-19 22:06:12 +00:00
|
|
|
|
2009-03-26 22:46:36 +00:00
|
|
|
/* Just return if it's a javascript: or mailto: uri */
|
2009-04-02 20:48:07 +00:00
|
|
|
if (g_str_has_prefix (uri, "javascript:")
|
|
|
|
|| g_str_has_prefix (uri, "mailto:")
|
|
|
|
|| g_str_has_prefix (uri, "data:"))
|
2008-12-08 23:02:38 +00:00
|
|
|
return g_strdup (uri);
|
2008-06-14 00:23:33 +00:00
|
|
|
/* Add file:// if we have a local path */
|
2008-04-20 19:44:56 +00:00
|
|
|
if (g_path_is_absolute (uri))
|
|
|
|
return g_strconcat ("file://", uri, NULL);
|
2008-06-14 00:23:33 +00:00
|
|
|
/* Construct an absolute path if the file is relative */
|
2009-01-23 20:43:28 +00:00
|
|
|
if (g_file_test (uri, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))
|
2008-06-01 13:24:55 +00:00
|
|
|
{
|
2008-06-14 00:23:33 +00:00
|
|
|
current_dir = g_get_current_dir ();
|
|
|
|
result = g_strconcat ("file://", current_dir,
|
|
|
|
G_DIR_SEPARATOR_S, uri, NULL);
|
2008-06-01 13:24:55 +00:00
|
|
|
g_free (current_dir);
|
|
|
|
return result;
|
|
|
|
}
|
2009-03-31 01:28:01 +00:00
|
|
|
/* Do we have a protocol? */
|
|
|
|
if (g_strstr_len (uri, 8, "://"))
|
|
|
|
return sokoke_idn_to_punycode (g_strdup (uri));
|
|
|
|
|
|
|
|
/* Do we have a domain, ip address or localhost? */
|
2009-04-11 10:24:43 +00:00
|
|
|
search = NULL;
|
2009-04-12 12:00:32 +00:00
|
|
|
if (!strchr (uri, ' ') &&
|
|
|
|
((search = strchr (uri, ':')) || (search = strchr (uri, '@'))) &&
|
2009-04-11 10:24:43 +00:00
|
|
|
search[0] && !g_ascii_isalpha (search[1]))
|
2009-04-12 12:00:32 +00:00
|
|
|
return sokoke_idn_to_punycode (g_strconcat ("http://", uri, NULL));
|
2009-03-31 01:28:01 +00:00
|
|
|
if (!strcmp (uri, "localhost") || g_str_has_prefix (uri, "localhost/"))
|
|
|
|
return g_strconcat ("http://", uri, NULL);
|
|
|
|
parts = g_strsplit (uri, ".", 0);
|
|
|
|
if (!search && parts[0] && parts[1])
|
2008-04-20 19:44:56 +00:00
|
|
|
{
|
2009-03-31 01:28:01 +00:00
|
|
|
if (!(parts[1][1] == '\0' && !g_ascii_isalpha (parts[1][0])))
|
|
|
|
if (!strchr (parts[0], ' ') && !strchr (parts[1], ' '))
|
|
|
|
if ((search = g_strconcat ("http://", uri, NULL)))
|
|
|
|
{
|
|
|
|
g_strfreev (parts);
|
|
|
|
return sokoke_idn_to_punycode (search);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_strfreev (parts);
|
|
|
|
/* We don't want to search? So return early. */
|
|
|
|
if (!search_engines)
|
|
|
|
return g_strdup (uri);
|
|
|
|
search = NULL;
|
|
|
|
search_uri = NULL;
|
|
|
|
/* Do we have a keyword and a string? */
|
|
|
|
parts = g_strsplit (uri, " ", 2);
|
|
|
|
if (parts[0] && parts[1])
|
|
|
|
if ((item = katze_array_find_token (search_engines, parts[0])))
|
2009-01-02 22:15:01 +00:00
|
|
|
{
|
2009-03-31 01:28:01 +00:00
|
|
|
search_uri = katze_item_get_uri (item);
|
2009-04-19 22:39:36 +00:00
|
|
|
search = sokoke_search_uri (search_uri, parts[1]);
|
2009-01-02 22:15:01 +00:00
|
|
|
}
|
2009-03-31 01:28:01 +00:00
|
|
|
g_strfreev (parts);
|
|
|
|
return search;
|
2008-04-20 19:44:56 +00:00
|
|
|
}
|
|
|
|
|
2008-06-01 15:39:57 +00:00
|
|
|
void
|
|
|
|
sokoke_combo_box_add_strings (GtkComboBox* combobox,
|
|
|
|
const gchar* label_first, ...)
|
2007-12-16 22:20:24 +00:00
|
|
|
{
|
2008-11-16 01:00:17 +00:00
|
|
|
const gchar* label;
|
|
|
|
|
2008-06-14 00:23:33 +00:00
|
|
|
/* Add a number of strings to a combobox, terminated with NULL
|
|
|
|
This works only for text comboboxes */
|
2007-12-16 22:20:24 +00:00
|
|
|
va_list args;
|
2008-06-01 15:39:57 +00:00
|
|
|
va_start (args, label_first);
|
2007-12-16 22:20:24 +00:00
|
|
|
|
2008-06-01 15:39:57 +00:00
|
|
|
for (label = label_first; label; label = va_arg (args, const gchar*))
|
|
|
|
gtk_combo_box_append_text (combobox, label);
|
2007-12-16 22:20:24 +00:00
|
|
|
|
2008-06-01 15:39:57 +00:00
|
|
|
va_end (args);
|
2007-12-16 22:20:24 +00:00
|
|
|
}
|
|
|
|
|
2008-06-01 15:39:57 +00:00
|
|
|
void sokoke_widget_set_visible (GtkWidget* widget, gboolean visible)
|
2007-12-16 22:20:24 +00:00
|
|
|
{
|
2008-06-14 00:23:33 +00:00
|
|
|
/* Show or hide the widget */
|
2008-06-01 15:39:57 +00:00
|
|
|
if (visible)
|
|
|
|
gtk_widget_show (widget);
|
2007-12-16 22:20:24 +00:00
|
|
|
else
|
2008-06-01 15:39:57 +00:00
|
|
|
gtk_widget_hide (widget);
|
2007-12-16 22:20:24 +00:00
|
|
|
}
|
|
|
|
|
2008-06-01 15:39:57 +00:00
|
|
|
void
|
|
|
|
sokoke_container_show_children (GtkContainer* container)
|
2007-12-16 22:20:24 +00:00
|
|
|
{
|
2008-06-14 00:23:33 +00:00
|
|
|
/* Show every child but not the container itself */
|
2008-06-01 15:39:57 +00:00
|
|
|
gtk_container_foreach (container, (GtkCallback)(gtk_widget_show_all), NULL);
|
2007-12-16 22:20:24 +00:00
|
|
|
}
|
|
|
|
|
2008-03-29 21:38:59 +00:00
|
|
|
void
|
|
|
|
sokoke_widget_popup (GtkWidget* widget,
|
|
|
|
GtkMenu* menu,
|
|
|
|
GdkEventButton* event,
|
|
|
|
SokokeMenuPos pos)
|
2007-12-16 22:20:24 +00:00
|
|
|
{
|
2008-10-10 20:31:37 +00:00
|
|
|
katze_widget_popup (widget, menu, event, (KatzeMenuPos)pos);
|
2007-12-16 22:20:24 +00:00
|
|
|
}
|
|
|
|
|
2007-12-27 01:12:26 +00:00
|
|
|
typedef enum
|
2007-12-16 22:20:24 +00:00
|
|
|
{
|
2008-03-29 21:38:59 +00:00
|
|
|
SOKOKE_DESKTOP_UNTESTED,
|
|
|
|
SOKOKE_DESKTOP_XFCE,
|
2008-10-25 22:54:57 +00:00
|
|
|
SOKOKE_DESKTOP_OSX,
|
2008-03-29 21:38:59 +00:00
|
|
|
SOKOKE_DESKTOP_UNKNOWN
|
2007-12-27 01:12:26 +00:00
|
|
|
} SokokeDesktop;
|
2007-12-16 22:20:24 +00:00
|
|
|
|
2008-06-01 15:39:57 +00:00
|
|
|
static SokokeDesktop
|
|
|
|
sokoke_get_desktop (void)
|
2007-12-16 22:20:24 +00:00
|
|
|
{
|
2009-01-09 21:54:40 +00:00
|
|
|
#if HAVE_OSX
|
2008-10-25 22:54:57 +00:00
|
|
|
return SOKOKE_DESKTOP_OSX;
|
|
|
|
#else
|
2007-12-27 01:42:11 +00:00
|
|
|
static SokokeDesktop desktop = SOKOKE_DESKTOP_UNTESTED;
|
2008-06-01 15:39:57 +00:00
|
|
|
if (G_UNLIKELY (desktop == SOKOKE_DESKTOP_UNTESTED))
|
2007-12-27 01:42:11 +00:00
|
|
|
{
|
2008-06-14 00:23:33 +00:00
|
|
|
/* Are we running in Xfce? */
|
2008-11-28 23:16:57 +00:00
|
|
|
gint result;
|
|
|
|
gchar *out = NULL;
|
2009-04-27 15:46:04 +00:00
|
|
|
gchar *err = NULL;
|
2008-11-28 23:16:57 +00:00
|
|
|
gboolean success = g_spawn_command_line_sync ("xprop -root _DT_SAVE_MODE",
|
2009-04-27 15:46:04 +00:00
|
|
|
&out, &err, &result, NULL);
|
|
|
|
g_free (err);
|
2008-12-12 09:11:45 +00:00
|
|
|
if (success && ! result && out != NULL && strstr (out, "xfce4") != NULL)
|
2007-12-27 01:42:11 +00:00
|
|
|
desktop = SOKOKE_DESKTOP_XFCE;
|
|
|
|
else
|
|
|
|
desktop = SOKOKE_DESKTOP_UNKNOWN;
|
2008-12-12 09:11:45 +00:00
|
|
|
g_free (out);
|
2007-12-27 01:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return desktop;
|
2008-10-15 22:58:22 +00:00
|
|
|
#endif
|
2007-12-16 22:20:24 +00:00
|
|
|
}
|
|
|
|
|
2008-12-12 09:11:45 +00:00
|
|
|
/**
|
|
|
|
* sokoke_xfce_header_new:
|
|
|
|
* @icon: an icon name
|
|
|
|
* @title: the title of the header
|
|
|
|
*
|
|
|
|
* Creates an Xfce style header *if* Xfce is running.
|
|
|
|
*
|
|
|
|
* Return value: A #GtkWidget or %NULL
|
|
|
|
*
|
|
|
|
* Since 0.1.2 @icon may be NULL, and a default is used.
|
|
|
|
**/
|
2008-06-01 15:39:57 +00:00
|
|
|
GtkWidget*
|
|
|
|
sokoke_xfce_header_new (const gchar* icon,
|
|
|
|
const gchar* title)
|
2007-12-16 22:20:24 +00:00
|
|
|
{
|
2008-12-12 09:11:45 +00:00
|
|
|
g_return_val_if_fail (title, NULL);
|
|
|
|
|
2008-06-14 00:23:33 +00:00
|
|
|
/* Create an xfce header with icon and title
|
2008-08-10 17:54:22 +00:00
|
|
|
This returns NULL if the desktop is not Xfce */
|
2008-06-01 15:39:57 +00:00
|
|
|
if (sokoke_get_desktop () == SOKOKE_DESKTOP_XFCE)
|
2007-12-16 22:20:24 +00:00
|
|
|
{
|
2008-11-16 01:00:17 +00:00
|
|
|
GtkWidget* entry;
|
2007-12-27 01:12:26 +00:00
|
|
|
gchar* markup;
|
2008-11-16 01:00:17 +00:00
|
|
|
GtkWidget* xfce_heading;
|
|
|
|
GtkWidget* hbox;
|
|
|
|
GtkWidget* image;
|
|
|
|
GtkWidget* label;
|
2009-01-09 22:59:39 +00:00
|
|
|
GtkWidget* vbox;
|
|
|
|
GtkWidget* separator;
|
2008-11-16 01:00:17 +00:00
|
|
|
|
|
|
|
xfce_heading = gtk_event_box_new ();
|
|
|
|
entry = gtk_entry_new ();
|
2008-06-01 15:39:57 +00:00
|
|
|
gtk_widget_modify_bg (xfce_heading, GTK_STATE_NORMAL,
|
|
|
|
&entry->style->base[GTK_STATE_NORMAL]);
|
2008-11-16 01:00:17 +00:00
|
|
|
hbox = gtk_hbox_new (FALSE, 12);
|
2008-06-01 15:39:57 +00:00
|
|
|
gtk_container_set_border_width (GTK_CONTAINER (hbox), 6);
|
2008-12-12 09:11:45 +00:00
|
|
|
if (icon)
|
|
|
|
image = gtk_image_new_from_icon_name (icon, GTK_ICON_SIZE_DIALOG);
|
|
|
|
else
|
|
|
|
image = gtk_image_new_from_stock (GTK_STOCK_PREFERENCES,
|
|
|
|
GTK_ICON_SIZE_DIALOG);
|
2008-06-01 15:39:57 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
|
2008-11-16 01:00:17 +00:00
|
|
|
label = gtk_label_new (NULL);
|
2008-06-01 15:39:57 +00:00
|
|
|
gtk_widget_modify_fg (label, GTK_STATE_NORMAL
|
2007-12-16 22:20:24 +00:00
|
|
|
, &entry->style->text[GTK_STATE_NORMAL]);
|
2008-06-01 15:39:57 +00:00
|
|
|
markup = g_strdup_printf ("<span size='large' weight='bold'>%s</span>",
|
|
|
|
title);
|
|
|
|
gtk_label_set_markup (GTK_LABEL (label), markup);
|
|
|
|
gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
|
|
|
|
gtk_container_add (GTK_CONTAINER (xfce_heading), hbox);
|
|
|
|
g_free (markup);
|
2008-12-12 09:11:45 +00:00
|
|
|
gtk_widget_destroy (entry);
|
2009-01-09 22:59:39 +00:00
|
|
|
|
|
|
|
vbox = gtk_vbox_new (FALSE, 0);
|
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), xfce_heading, FALSE, FALSE, 0);
|
|
|
|
|
|
|
|
separator = gtk_hseparator_new ();
|
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), separator, FALSE, FALSE, 0);
|
|
|
|
|
|
|
|
return vbox;
|
2007-12-16 22:20:24 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-06-01 15:39:57 +00:00
|
|
|
GtkWidget*
|
2008-06-01 13:15:38 +00:00
|
|
|
sokoke_superuser_warning_new (void)
|
2007-12-16 22:20:24 +00:00
|
|
|
{
|
2008-06-14 00:23:33 +00:00
|
|
|
/* Create a horizontal bar with a security warning
|
|
|
|
This returns NULL if the user is no superuser */
|
2008-08-23 21:44:55 +00:00
|
|
|
#if HAVE_UNISTD_H
|
2008-06-14 00:23:33 +00:00
|
|
|
if (G_UNLIKELY (!geteuid ())) /* effective superuser? */
|
2007-12-16 22:20:24 +00:00
|
|
|
{
|
2008-11-16 01:00:17 +00:00
|
|
|
GtkWidget* hbox;
|
|
|
|
GtkWidget* label;
|
|
|
|
|
|
|
|
hbox = gtk_event_box_new ();
|
2008-06-01 13:15:38 +00:00
|
|
|
gtk_widget_modify_bg (hbox, GTK_STATE_NORMAL,
|
|
|
|
&hbox->style->bg[GTK_STATE_SELECTED]);
|
2009-01-04 18:21:25 +00:00
|
|
|
/* i18n: A superuser, or system administrator, may not be 'root' */
|
2008-11-16 01:00:17 +00:00
|
|
|
label = gtk_label_new (_("Warning: You are using a superuser account!"));
|
2008-06-01 13:15:38 +00:00
|
|
|
gtk_misc_set_padding (GTK_MISC (label), 0, 2);
|
|
|
|
gtk_widget_modify_fg (GTK_WIDGET (label), GTK_STATE_NORMAL,
|
|
|
|
>K_WIDGET (label)->style->fg[GTK_STATE_SELECTED]);
|
|
|
|
gtk_widget_show (label);
|
2008-09-26 21:13:46 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (hbox), GTK_WIDGET (label));
|
2008-06-01 13:15:38 +00:00
|
|
|
gtk_widget_show (hbox);
|
2007-12-16 22:20:24 +00:00
|
|
|
return hbox;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-06-01 15:39:57 +00:00
|
|
|
GtkWidget*
|
|
|
|
sokoke_hig_frame_new (const gchar* title)
|
2007-12-16 22:20:24 +00:00
|
|
|
{
|
2008-06-14 00:23:33 +00:00
|
|
|
/* Create a frame with no actual frame but a bold label and indentation */
|
2008-06-01 15:39:57 +00:00
|
|
|
GtkWidget* frame = gtk_frame_new (NULL);
|
|
|
|
gchar* title_bold = g_strdup_printf ("<b>%s</b>", title);
|
|
|
|
GtkWidget* label = gtk_label_new (NULL);
|
|
|
|
gtk_label_set_markup (GTK_LABEL (label), title_bold);
|
|
|
|
g_free (title_bold);
|
|
|
|
gtk_frame_set_label_widget (GTK_FRAME (frame), label);
|
|
|
|
gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_NONE);
|
2007-12-16 22:20:24 +00:00
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
2008-06-01 15:39:57 +00:00
|
|
|
void
|
|
|
|
sokoke_widget_set_pango_font_style (GtkWidget* widget,
|
|
|
|
PangoStyle style)
|
2007-12-16 22:20:24 +00:00
|
|
|
{
|
2008-06-14 00:23:33 +00:00
|
|
|
/* Conveniently change the pango font style
|
|
|
|
For some reason we need to reset if we actually want the normal style */
|
2008-06-01 15:39:57 +00:00
|
|
|
if (style == PANGO_STYLE_NORMAL)
|
|
|
|
gtk_widget_modify_font (widget, NULL);
|
2007-12-16 22:20:24 +00:00
|
|
|
else
|
|
|
|
{
|
2008-06-01 15:39:57 +00:00
|
|
|
PangoFontDescription* font_description = pango_font_description_new ();
|
|
|
|
pango_font_description_set_style (font_description, PANGO_STYLE_ITALIC);
|
|
|
|
gtk_widget_modify_font (widget, font_description);
|
|
|
|
pango_font_description_free (font_description);
|
2007-12-16 22:20:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-31 22:21:08 +00:00
|
|
|
static gboolean
|
|
|
|
sokoke_on_entry_focus_in_event (GtkEntry* entry,
|
|
|
|
GdkEventFocus* event,
|
|
|
|
gpointer userdata)
|
2007-12-16 22:20:24 +00:00
|
|
|
{
|
2008-06-15 23:13:27 +00:00
|
|
|
gint has_default = GPOINTER_TO_INT (
|
2008-05-31 22:21:08 +00:00
|
|
|
g_object_get_data (G_OBJECT (entry), "sokoke_has_default"));
|
2008-06-15 23:13:27 +00:00
|
|
|
if (has_default)
|
2007-12-16 22:20:24 +00:00
|
|
|
{
|
2008-05-31 22:21:08 +00:00
|
|
|
gtk_entry_set_text (entry, "");
|
2008-09-26 21:13:46 +00:00
|
|
|
g_object_set_data (G_OBJECT (entry), "sokoke_has_default",
|
2008-05-31 22:21:08 +00:00
|
|
|
GINT_TO_POINTER (0));
|
|
|
|
sokoke_widget_set_pango_font_style (GTK_WIDGET (entry),
|
|
|
|
PANGO_STYLE_NORMAL);
|
2007-12-16 22:20:24 +00:00
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2008-05-31 22:21:08 +00:00
|
|
|
static gboolean
|
|
|
|
sokoke_on_entry_focus_out_event (GtkEntry* entry,
|
|
|
|
GdkEventFocus* event,
|
|
|
|
gpointer userdata)
|
2007-12-16 22:20:24 +00:00
|
|
|
{
|
2008-05-31 22:21:08 +00:00
|
|
|
const gchar* text = gtk_entry_get_text (entry);
|
|
|
|
if (text && !*text)
|
2007-12-16 22:20:24 +00:00
|
|
|
{
|
2008-06-15 23:13:27 +00:00
|
|
|
const gchar* default_text = (const gchar*)g_object_get_data (
|
|
|
|
G_OBJECT (entry), "sokoke_default_text");
|
|
|
|
gtk_entry_set_text (entry, default_text);
|
2008-09-26 21:13:46 +00:00
|
|
|
g_object_set_data (G_OBJECT (entry),
|
2008-05-31 22:21:08 +00:00
|
|
|
"sokoke_has_default", GINT_TO_POINTER (1));
|
2008-06-01 15:39:57 +00:00
|
|
|
sokoke_widget_set_pango_font_style (GTK_WIDGET (entry),
|
2008-05-31 22:21:08 +00:00
|
|
|
PANGO_STYLE_ITALIC);
|
2007-12-16 22:20:24 +00:00
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2008-05-31 22:21:08 +00:00
|
|
|
void
|
|
|
|
sokoke_entry_set_default_text (GtkEntry* entry,
|
|
|
|
const gchar* default_text)
|
2007-12-16 22:20:24 +00:00
|
|
|
{
|
2008-06-14 00:23:33 +00:00
|
|
|
/* Note: The default text initially overwrites any previous text */
|
2008-05-31 22:21:08 +00:00
|
|
|
gchar* old_value = g_object_get_data (G_OBJECT (entry),
|
|
|
|
"sokoke_default_text");
|
|
|
|
if (!old_value)
|
2007-12-16 22:20:24 +00:00
|
|
|
{
|
2008-05-31 22:21:08 +00:00
|
|
|
g_object_set_data (G_OBJECT (entry), "sokoke_has_default",
|
|
|
|
GINT_TO_POINTER (1));
|
|
|
|
sokoke_widget_set_pango_font_style (GTK_WIDGET (entry),
|
|
|
|
PANGO_STYLE_ITALIC);
|
|
|
|
gtk_entry_set_text (entry, default_text);
|
2007-12-16 22:20:24 +00:00
|
|
|
}
|
2008-06-15 23:13:27 +00:00
|
|
|
else if (!GTK_WIDGET_HAS_FOCUS (GTK_WIDGET (entry)))
|
|
|
|
{
|
|
|
|
gint has_default = GPOINTER_TO_INT (
|
|
|
|
g_object_get_data (G_OBJECT (entry), "sokoke_has_default"));
|
|
|
|
if (has_default)
|
|
|
|
{
|
|
|
|
gtk_entry_set_text (entry, default_text);
|
|
|
|
sokoke_widget_set_pango_font_style (GTK_WIDGET (entry),
|
|
|
|
PANGO_STYLE_ITALIC);
|
|
|
|
}
|
|
|
|
}
|
2008-05-31 22:21:08 +00:00
|
|
|
g_object_set_data (G_OBJECT (entry), "sokoke_default_text",
|
|
|
|
(gpointer)default_text);
|
|
|
|
g_signal_connect (entry, "focus-in-event",
|
|
|
|
G_CALLBACK (sokoke_on_entry_focus_in_event), NULL);
|
|
|
|
g_signal_connect (entry, "focus-out-event",
|
|
|
|
G_CALLBACK (sokoke_on_entry_focus_out_event), NULL);
|
2007-12-16 22:20:24 +00:00
|
|
|
}
|
|
|
|
|
2008-06-01 15:39:57 +00:00
|
|
|
gchar*
|
|
|
|
sokoke_key_file_get_string_default (GKeyFile* key_file,
|
|
|
|
const gchar* group,
|
|
|
|
const gchar* key,
|
|
|
|
const gchar* default_value,
|
|
|
|
GError** error)
|
2007-12-16 22:20:24 +00:00
|
|
|
{
|
2008-06-01 15:39:57 +00:00
|
|
|
gchar* value = g_key_file_get_string (key_file, group, key, error);
|
|
|
|
return value == NULL ? g_strdup (default_value) : value;
|
2007-12-16 22:20:24 +00:00
|
|
|
}
|
|
|
|
|
2008-06-01 15:39:57 +00:00
|
|
|
gint
|
|
|
|
sokoke_key_file_get_integer_default (GKeyFile* key_file,
|
|
|
|
const gchar* group,
|
|
|
|
const gchar* key,
|
|
|
|
const gint default_value,
|
|
|
|
GError** error)
|
2007-12-16 22:20:24 +00:00
|
|
|
{
|
2008-06-01 15:39:57 +00:00
|
|
|
if (!g_key_file_has_key (key_file, group, key, NULL))
|
|
|
|
return default_value;
|
|
|
|
return g_key_file_get_integer (key_file, group, key, error);
|
2007-12-16 22:20:24 +00:00
|
|
|
}
|
|
|
|
|
2008-06-01 09:26:10 +00:00
|
|
|
gdouble
|
|
|
|
sokoke_key_file_get_double_default (GKeyFile* key_file,
|
|
|
|
const gchar* group,
|
|
|
|
const gchar* key,
|
|
|
|
const gdouble default_value,
|
|
|
|
GError** error)
|
|
|
|
{
|
|
|
|
if (!g_key_file_has_key (key_file, group, key, NULL))
|
|
|
|
return default_value;
|
|
|
|
return g_key_file_get_double (key_file, group, key, error);
|
|
|
|
}
|
|
|
|
|
2008-04-13 19:51:43 +00:00
|
|
|
gboolean
|
|
|
|
sokoke_key_file_get_boolean_default (GKeyFile* key_file,
|
|
|
|
const gchar* group,
|
|
|
|
const gchar* key,
|
|
|
|
const gboolean default_value,
|
|
|
|
GError** error)
|
|
|
|
{
|
|
|
|
if (!g_key_file_has_key (key_file, group, key, NULL))
|
|
|
|
return default_value;
|
|
|
|
return g_key_file_get_boolean (key_file, group, key, error);
|
|
|
|
}
|
|
|
|
|
2009-04-25 13:57:00 +00:00
|
|
|
gchar**
|
|
|
|
sokoke_key_file_get_string_list_default (GKeyFile* key_file,
|
|
|
|
const gchar* group,
|
|
|
|
const gchar* key,
|
|
|
|
gsize* length,
|
|
|
|
gchar** default_value,
|
|
|
|
gsize* default_length,
|
|
|
|
GError* error)
|
|
|
|
{
|
|
|
|
gchar** value = g_key_file_get_string_list (key_file, group, key, length, NULL);
|
|
|
|
if (!value)
|
|
|
|
{
|
|
|
|
value = g_strdupv (default_value);
|
|
|
|
if (length)
|
|
|
|
*length = *default_length;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2008-06-01 15:39:57 +00:00
|
|
|
gboolean
|
|
|
|
sokoke_key_file_save_to_file (GKeyFile* key_file,
|
|
|
|
const gchar* filename,
|
|
|
|
GError** error)
|
2007-12-16 22:20:24 +00:00
|
|
|
{
|
2008-11-16 01:00:17 +00:00
|
|
|
gchar* data;
|
|
|
|
FILE* fp;
|
|
|
|
|
|
|
|
data = g_key_file_to_data (key_file, NULL, error);
|
2008-06-01 15:39:57 +00:00
|
|
|
if (!data)
|
2007-12-16 22:20:24 +00:00
|
|
|
return FALSE;
|
2008-11-16 01:00:17 +00:00
|
|
|
|
2008-06-01 15:39:57 +00:00
|
|
|
if (!(fp = fopen (filename, "w")))
|
2007-12-16 22:20:24 +00:00
|
|
|
{
|
2008-06-01 15:39:57 +00:00
|
|
|
*error = g_error_new (G_FILE_ERROR, G_FILE_ERROR_ACCES,
|
|
|
|
_("Writing failed."));
|
2007-12-16 22:20:24 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2008-06-01 15:39:57 +00:00
|
|
|
fputs (data, fp);
|
|
|
|
fclose (fp);
|
|
|
|
g_free (data);
|
2007-12-16 22:20:24 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2008-06-01 15:39:57 +00:00
|
|
|
void
|
|
|
|
sokoke_widget_get_text_size (GtkWidget* widget,
|
|
|
|
const gchar* text,
|
|
|
|
gint* width,
|
|
|
|
gint* height)
|
2007-12-16 22:20:24 +00:00
|
|
|
{
|
2008-06-01 15:39:57 +00:00
|
|
|
PangoLayout* layout = gtk_widget_create_pango_layout (widget, text);
|
|
|
|
pango_layout_get_pixel_size (layout, width, height);
|
|
|
|
g_object_unref (layout);
|
2007-12-16 22:20:24 +00:00
|
|
|
}
|
2008-06-15 02:02:56 +00:00
|
|
|
|
2008-08-10 17:54:22 +00:00
|
|
|
/**
|
|
|
|
* sokoke_action_create_popup_menu_item:
|
|
|
|
* @action: a #GtkAction
|
|
|
|
*
|
2008-08-15 00:58:09 +00:00
|
|
|
* Creates a menu item from an action, just like
|
2008-08-29 01:20:31 +00:00
|
|
|
* gtk_action_create_menu_item(), but it won't
|
2008-08-10 17:54:22 +00:00
|
|
|
* display an accelerator.
|
|
|
|
*
|
2008-09-07 01:07:11 +00:00
|
|
|
* Note: This menu item is not a proxy and will
|
|
|
|
* not reflect any changes to the action.
|
|
|
|
*
|
2008-08-10 17:54:22 +00:00
|
|
|
* Return value: a new #GtkMenuItem
|
|
|
|
**/
|
|
|
|
GtkWidget*
|
|
|
|
sokoke_action_create_popup_menu_item (GtkAction* action)
|
|
|
|
{
|
|
|
|
GtkWidget* menuitem;
|
2008-09-07 01:07:11 +00:00
|
|
|
GtkWidget* icon;
|
|
|
|
gchar* label;
|
|
|
|
gchar* stock_id;
|
|
|
|
gchar* icon_name;
|
|
|
|
gboolean sensitive;
|
|
|
|
gboolean visible;
|
2008-08-10 17:54:22 +00:00
|
|
|
|
2008-08-29 01:20:31 +00:00
|
|
|
g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
|
|
|
|
|
2008-09-07 01:07:11 +00:00
|
|
|
g_object_get (action,
|
|
|
|
"label", &label,
|
|
|
|
"stock-id", &stock_id,
|
|
|
|
"icon-name", &icon_name,
|
|
|
|
"sensitive", &sensitive,
|
|
|
|
"visible", &visible,
|
|
|
|
NULL);
|
2008-10-12 20:09:49 +00:00
|
|
|
if (GTK_IS_TOGGLE_ACTION (action))
|
|
|
|
{
|
|
|
|
menuitem = gtk_check_menu_item_new_with_mnemonic (label);
|
|
|
|
gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menuitem),
|
|
|
|
gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
|
|
|
|
}
|
|
|
|
else if (stock_id)
|
2008-09-07 01:07:11 +00:00
|
|
|
{
|
|
|
|
if (label)
|
|
|
|
{
|
|
|
|
menuitem = gtk_image_menu_item_new_with_mnemonic (label);
|
|
|
|
icon = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_MENU);
|
|
|
|
gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), icon);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
menuitem = gtk_image_menu_item_new_with_mnemonic (label);
|
|
|
|
if (icon_name)
|
|
|
|
{
|
|
|
|
icon = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
|
|
|
|
gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), icon);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gtk_widget_set_sensitive (menuitem, sensitive);
|
|
|
|
sokoke_widget_set_visible (menuitem, visible);
|
|
|
|
g_signal_connect_swapped (menuitem, "activate",
|
|
|
|
G_CALLBACK (gtk_action_activate), action);
|
2008-08-10 17:54:22 +00:00
|
|
|
|
|
|
|
return menuitem;
|
|
|
|
}
|
2008-08-29 01:20:31 +00:00
|
|
|
|
2008-09-07 01:39:25 +00:00
|
|
|
/**
|
|
|
|
* sokoke_image_menu_item_new_ellipsized:
|
|
|
|
* @label: the text of the menu item
|
|
|
|
*
|
|
|
|
* Creates a new #GtkImageMenuItem containing an ellipsized label.
|
|
|
|
*
|
|
|
|
* Return value: a new #GtkImageMenuItem
|
|
|
|
**/
|
|
|
|
GtkWidget*
|
|
|
|
sokoke_image_menu_item_new_ellipsized (const gchar* label)
|
|
|
|
{
|
2008-10-10 20:31:37 +00:00
|
|
|
return katze_image_menu_item_new_ellipsized (label);
|
2008-09-07 01:39:25 +00:00
|
|
|
}
|
|
|
|
|
2008-10-23 23:35:34 +00:00
|
|
|
/**
|
2009-01-23 21:38:39 +00:00
|
|
|
* sokoke_time_t_to_julian:
|
|
|
|
* @timestamp: a time_t timestamp value
|
2008-10-23 23:35:34 +00:00
|
|
|
*
|
2009-01-23 21:38:39 +00:00
|
|
|
* Calculates a unix timestamp to a julian day value.
|
2008-10-23 23:35:34 +00:00
|
|
|
*
|
|
|
|
* Return value: an integer.
|
|
|
|
**/
|
2009-01-23 21:38:39 +00:00
|
|
|
gint64
|
|
|
|
sokoke_time_t_to_julian (const time_t* timestamp)
|
2008-10-23 23:35:34 +00:00
|
|
|
{
|
2009-01-23 21:38:39 +00:00
|
|
|
GDate* date;
|
|
|
|
gint64 julian;
|
2008-10-23 23:35:34 +00:00
|
|
|
|
2009-01-23 21:38:39 +00:00
|
|
|
date = g_date_new ();
|
2008-10-23 23:35:34 +00:00
|
|
|
|
2009-01-23 21:38:39 +00:00
|
|
|
g_date_set_time_t (date, *timestamp);
|
|
|
|
julian = (gint64)g_date_get_julian (date);
|
2008-10-23 23:35:34 +00:00
|
|
|
|
2009-01-23 21:38:39 +00:00
|
|
|
g_date_free (date);
|
2008-10-23 23:35:34 +00:00
|
|
|
|
2009-01-23 21:38:39 +00:00
|
|
|
return julian;
|
2008-10-23 23:35:34 +00:00
|
|
|
}
|
2009-02-21 21:36:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* sokoke_register_stock_items:
|
|
|
|
*
|
|
|
|
* Registers several custom stock items used throughout Midori.
|
|
|
|
**/
|
|
|
|
void
|
|
|
|
sokoke_register_stock_items (void)
|
|
|
|
{
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
const gchar* stock_id;
|
|
|
|
const gchar* label;
|
|
|
|
GdkModifierType modifier;
|
|
|
|
guint keyval;
|
|
|
|
const gchar* fallback;
|
|
|
|
} FatStockItem;
|
|
|
|
GtkIconSource* icon_source;
|
|
|
|
GtkIconSet* icon_set;
|
|
|
|
GtkIconFactory* factory = gtk_icon_factory_new ();
|
|
|
|
gsize i;
|
|
|
|
|
|
|
|
static FatStockItem items[] =
|
|
|
|
{
|
|
|
|
{ STOCK_EXTENSION, NULL, 0, 0, GTK_STOCK_CONVERT },
|
2009-04-17 01:16:37 +00:00
|
|
|
{ STOCK_IMAGE, NULL, 0, 0, GTK_STOCK_ORIENTATION_PORTRAIT },
|
2009-02-21 21:36:03 +00:00
|
|
|
{ STOCK_NEWS_FEED, NULL, 0, 0, GTK_STOCK_INDEX },
|
|
|
|
{ STOCK_SCRIPT, NULL, 0, 0, GTK_STOCK_EXECUTE },
|
|
|
|
{ STOCK_STYLE, NULL, 0, 0, GTK_STOCK_SELECT_COLOR },
|
|
|
|
{ STOCK_TRANSFER, NULL, 0, 0, GTK_STOCK_SAVE },
|
|
|
|
|
|
|
|
{ STOCK_BOOKMARK, N_("_Bookmark"), 0, 0, GTK_STOCK_FILE },
|
|
|
|
{ STOCK_BOOKMARKS, N_("_Bookmarks"), 0, 0, GTK_STOCK_DIRECTORY },
|
|
|
|
{ STOCK_BOOKMARK_ADD, N_("_Add Bookmark"), 0, 0, GTK_STOCK_ADD },
|
|
|
|
{ STOCK_CONSOLE, N_("_Console"), 0, 0, GTK_STOCK_DIALOG_WARNING },
|
|
|
|
{ STOCK_EXTENSIONS, N_("_Extensions"), 0, 0, GTK_STOCK_CONVERT },
|
|
|
|
{ STOCK_HISTORY, N_("_History"), 0, 0, GTK_STOCK_SORT_ASCENDING },
|
|
|
|
{ STOCK_HOMEPAGE, N_("_Homepage"), 0, 0, GTK_STOCK_HOME },
|
|
|
|
{ STOCK_SCRIPTS, N_("_Userscripts"), 0, 0, GTK_STOCK_EXECUTE },
|
|
|
|
{ STOCK_STYLES, N_("User_styles"), 0, 0, GTK_STOCK_SELECT_COLOR },
|
|
|
|
{ STOCK_TAB_NEW, N_("New _Tab"), 0, 0, GTK_STOCK_ADD },
|
|
|
|
{ STOCK_TRANSFERS, N_("_Transfers"), 0, 0, GTK_STOCK_SAVE },
|
2009-04-25 14:13:23 +00:00
|
|
|
{ STOCK_PLUGINS, N_("Netscape p_lugins"), 0, 0, GTK_STOCK_CONVERT },
|
2009-02-21 21:36:03 +00:00
|
|
|
{ STOCK_USER_TRASH, N_("_Closed Tabs and Windows"), 0, 0, "gtk-undo-ltr" },
|
|
|
|
{ STOCK_WINDOW_NEW, N_("New _Window"), 0, 0, GTK_STOCK_ADD },
|
|
|
|
};
|
|
|
|
|
|
|
|
for (i = 0; i < G_N_ELEMENTS (items); i++)
|
|
|
|
{
|
|
|
|
icon_set = gtk_icon_set_new ();
|
|
|
|
icon_source = gtk_icon_source_new ();
|
|
|
|
if (items[i].fallback)
|
|
|
|
{
|
|
|
|
gtk_icon_source_set_icon_name (icon_source, items[i].fallback);
|
|
|
|
items[i].fallback = NULL;
|
|
|
|
gtk_icon_set_add_source (icon_set, icon_source);
|
|
|
|
}
|
|
|
|
gtk_icon_source_set_icon_name (icon_source, items[i].stock_id);
|
|
|
|
gtk_icon_set_add_source (icon_set, icon_source);
|
|
|
|
gtk_icon_source_free (icon_source);
|
|
|
|
gtk_icon_factory_add (factory, items[i].stock_id, icon_set);
|
|
|
|
gtk_icon_set_unref (icon_set);
|
|
|
|
}
|
|
|
|
gtk_stock_add ((GtkStockItem*)items, G_N_ELEMENTS (items));
|
|
|
|
gtk_icon_factory_add_default (factory);
|
|
|
|
g_object_unref (factory);
|
|
|
|
}
|
2009-04-09 17:28:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* sokoke_set_config_dir:
|
|
|
|
* @new_config_dir: an absolute path, or %NULL
|
|
|
|
*
|
|
|
|
* Retrieves and/ or sets the base configuration folder.
|
|
|
|
*
|
|
|
|
* Return value: the configuration folder, or %NULL
|
|
|
|
**/
|
|
|
|
const gchar*
|
|
|
|
sokoke_set_config_dir (const gchar* new_config_dir)
|
|
|
|
{
|
|
|
|
static gchar* config_dir = NULL;
|
|
|
|
|
|
|
|
if (config_dir)
|
|
|
|
return config_dir;
|
|
|
|
|
|
|
|
if (!new_config_dir)
|
|
|
|
config_dir = g_build_filename (g_get_user_config_dir (),
|
|
|
|
PACKAGE_NAME, NULL);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (g_path_is_absolute (new_config_dir), NULL);
|
|
|
|
katze_assign (config_dir, g_strdup (new_config_dir));
|
|
|
|
}
|
|
|
|
|
|
|
|
return config_dir;
|
|
|
|
}
|
2009-04-11 03:14:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* sokoke_remove_path:
|
|
|
|
* @path: an absolute path
|
|
|
|
* @ignore_errors: keep removing even if an error occurred
|
|
|
|
*
|
|
|
|
* Removes the file at @path or the folder including any
|
|
|
|
* child folders and files if @path is a folder.
|
|
|
|
*
|
|
|
|
* If @ignore_errors is %TRUE and @path is a folder with
|
|
|
|
* children, one of which can't be removed, remaining
|
|
|
|
* children will be deleted nevertheless
|
|
|
|
* If @ignore_errors is %FALSE and @path is a folder, the
|
|
|
|
* removal process will cancel immediately.
|
|
|
|
*
|
|
|
|
* Return value: %TRUE on success, %FALSE if an error occurred
|
|
|
|
**/
|
|
|
|
gboolean
|
|
|
|
sokoke_remove_path (const gchar* path,
|
|
|
|
gboolean ignore_errors)
|
|
|
|
{
|
|
|
|
GDir* dir = g_dir_open (path, 0, NULL);
|
|
|
|
const gchar* name;
|
|
|
|
|
|
|
|
if (!dir)
|
|
|
|
return g_remove (path) == 0;
|
|
|
|
|
|
|
|
while ((name = g_dir_read_name (dir)))
|
|
|
|
{
|
|
|
|
gchar* sub_path = g_build_filename (path, name, NULL);
|
|
|
|
if (!sokoke_remove_path (sub_path, ignore_errors) && !ignore_errors)
|
|
|
|
return FALSE;
|
|
|
|
g_free (sub_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_dir_close (dir);
|
|
|
|
g_rmdir (path);
|
|
|
|
return TRUE;
|
|
|
|
}
|
2009-05-02 19:36:09 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
res_server_handler_cb (SoupServer* res_server,
|
|
|
|
SoupMessage* msg,
|
|
|
|
const gchar* path,
|
|
|
|
GHashTable* query,
|
|
|
|
SoupClientContext* client,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
if (g_str_has_prefix (path, "/res"))
|
|
|
|
{
|
|
|
|
gchar* filename = g_strconcat (DATADIR "/midori", path, NULL);
|
|
|
|
gchar* contents;
|
|
|
|
guint length;
|
|
|
|
|
|
|
|
if (g_file_get_contents (filename, &contents, &length, NULL))
|
|
|
|
{
|
|
|
|
/* FIXME: Support any MIME type */
|
|
|
|
soup_message_set_response (msg, "image/png", SOUP_MEMORY_TAKE,
|
|
|
|
contents, length);
|
|
|
|
soup_message_set_status (msg, 401);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
soup_message_set_status (msg, 404);
|
|
|
|
g_free (filename);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
soup_message_set_status (msg, 404);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SoupServer*
|
|
|
|
sokoke_get_res_server (void)
|
|
|
|
{
|
|
|
|
static SoupServer* res_server = NULL;
|
|
|
|
|
|
|
|
if (G_UNLIKELY (!res_server))
|
|
|
|
{
|
|
|
|
res_server = soup_server_new ("port", SOUP_ADDRESS_ANY_PORT, NULL);
|
|
|
|
soup_server_add_handler (res_server, "/",
|
|
|
|
res_server_handler_cb, NULL, NULL);
|
|
|
|
soup_server_run_async (res_server);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res_server;
|
|
|
|
}
|
2009-05-02 19:38:43 +00:00
|
|
|
|
|
|
|
gchar*
|
|
|
|
sokoke_replace_variables (const gchar* template,
|
|
|
|
const gchar* variable_first, ...)
|
|
|
|
{
|
|
|
|
gchar* result = g_strdup (template);
|
|
|
|
const gchar* variable;
|
|
|
|
|
|
|
|
va_list args;
|
|
|
|
va_start (args, variable_first);
|
|
|
|
|
|
|
|
for (variable = variable_first; variable; variable = va_arg (args, const gchar*))
|
|
|
|
{
|
|
|
|
const gchar* value = va_arg (args, const gchar*);
|
|
|
|
GRegex* regex = g_regex_new (variable, 0, 0, NULL);
|
|
|
|
gchar* replaced = result;
|
|
|
|
result = g_regex_replace (regex, replaced, -1, 0, value, 0, NULL);
|
|
|
|
g_free (replaced);
|
|
|
|
g_regex_unref (regex);
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end (args);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|