1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
/* gdict-utils.c - Utility functions for Gdict
*
* Copyright (C) 2005 Emmanuele Bassi <ebassi@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <glib.h>
#include <glib/gi18n-lib.h>
#include <gtk/gtk.h>
#include "gdict-context-private.h"
#include "gdict-debug.h"
#include "gdict-utils.h"
#include "gdict-version.h"
#include "gdict-private.h"
guint gdict_major_version = GDICT_MAJOR_VERSION;
guint gdict_minor_version = GDICT_MINOR_VERSION;
guint gdict_micro_version = GDICT_MICRO_VERSION;
guint gdict_debug_flags = 0; /* global gdict debug flag */
#ifdef GDICT_ENABLE_DEBUG
static const GDebugKey gdict_debug_keys[] = {
{ "misc", GDICT_DEBUG_MISC },
{ "context", GDICT_DEBUG_CONTEXT },
{ "dict", GDICT_DEBUG_DICT },
{ "source", GDICT_DEBUG_SOURCE },
{ "loader", GDICT_DEBUG_LOADER },
{ "chooser", GDICT_DEBUG_CHOOSER },
{ "defbux", GDICT_DEBUG_DEFBOX },
{ "speller", GDICT_DEBUG_SPELLER },
};
#endif /* GDICT_ENABLE_DEBUG */
static gboolean gdict_is_initialized = FALSE;
#ifdef GDICT_ENABLE_DEBUG
static gboolean
gdict_arg_debug_cb (const char *key,
const char *value,
gpointer user_data)
{
gdict_debug_flags |=
g_parse_debug_string (value,
gdict_debug_keys,
G_N_ELEMENTS (gdict_debug_keys));
return TRUE;
}
static gboolean
gdict_arg_no_debug_cb (const char *key,
const char *value,
gpointer user_data)
{
gdict_debug_flags &=
~g_parse_debug_string (value,
gdict_debug_keys,
G_N_ELEMENTS (gdict_debug_keys));
return TRUE;
}
#endif /* CLUTTER_ENABLE_DEBUG */
static GOptionEntry gdict_args[] = {
#ifdef GDICT_ENABLE_DEBUG
{ "gdict-debug", 0, 0, G_OPTION_ARG_CALLBACK, gdict_arg_debug_cb,
N_("GDict debugging flags to set"), N_("FLAGS") },
{ "gdict-no-debug", 0, 0, G_OPTION_ARG_CALLBACK, gdict_arg_no_debug_cb,
N_("GDict debugging flags to unset"), N_("FLAGS") },
#endif /* GDICT_ENABLE_DEBUG */
{ NULL, },
};
static gboolean
pre_parse_hook (GOptionContext *context,
GOptionGroup *group,
gpointer data,
GError **error)
{
const char *env_string;
if (gdict_is_initialized)
return TRUE;
#ifdef GDICT_ENABLE_DEBUG
env_string = g_getenv ("GDICT_DEBUG");
if (env_string != NULL)
{
gdict_debug_flags =
g_parse_debug_string (env_string,
gdict_debug_keys,
G_N_ELEMENTS (gdict_debug_keys));
}
#else
env_string = NULL;
#endif /* GDICT_ENABLE_DEBUG */
return TRUE;
}
static gboolean
post_parse_hook (GOptionContext *context,
GOptionGroup *group,
gpointer data,
GError **error)
{
gdict_is_initialized = TRUE;
return TRUE;
}
/**
* gdict_get_option_group:
*
* FIXME
*
* Return value: FIXME
*
* Since: 0.12
*/
GOptionGroup *
gdict_get_option_group (void)
{
GOptionGroup *group;
group = g_option_group_new ("gdict",
_("GDict Options"),
_("Show GDict Options"),
NULL,
NULL);
g_option_group_set_parse_hooks (group, pre_parse_hook, post_parse_hook);
g_option_group_add_entries (group, gdict_args);
g_option_group_set_translation_domain (group, GETTEXT_PACKAGE);
return group;
}
/**
* gdict_debug_init:
* @argc: FIXME
* @argv: FIXME
*
* FIXME
*
* Since: 0.12
*/
void
gdict_debug_init (gint *argc,
gchar ***argv)
{
GOptionContext *option_context;
GOptionGroup *gdict_group;
GError *error = NULL;
if (gdict_is_initialized)
return;
option_context = g_option_context_new (NULL);
g_option_context_set_ignore_unknown_options (option_context, TRUE);
g_option_context_set_help_enabled (option_context, FALSE);
gdict_group = gdict_get_option_group ();
g_option_context_set_main_group (option_context, gdict_group);
if (!g_option_context_parse (option_context, argc, argv, &error))
{
g_warning ("%s", error->message);
g_error_free (error);
}
g_option_context_free (option_context);
}
gboolean
gdict_check_version (guint required_major,
guint required_minor,
guint required_micro)
{
gint gdict_effective_micro = 100 * GDICT_MINOR_VERSION + GDICT_MICRO_VERSION;
gint required_effective_micro = 100 * required_minor + required_micro;
if (required_major > GDICT_MAJOR_VERSION)
return FALSE;
if (required_major < GDICT_MAJOR_VERSION)
return FALSE;
if (required_effective_micro < gdict_effective_micro)
return FALSE;
if (required_effective_micro > gdict_effective_micro)
return FALSE;
return TRUE;
}
/* gdict_has_ipv6: checks for the existence of the IPv6 extensions; if
* IPv6 support was not enabled, this function always return false
*/
gboolean
_gdict_has_ipv6 (void)
{
#ifdef ENABLE_IPV6
int s;
s = socket (AF_INET6, SOCK_STREAM, 0);
if (s != -1)
{
close(s);
return TRUE;
}
#endif
return FALSE;
}
/* shows an error dialog making it transient for @parent */
static void
show_error_dialog (GtkWindow *parent,
const gchar *message,
const gchar *detail)
{
GtkWidget *dialog;
dialog = gtk_message_dialog_new (parent,
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_OK,
"%s", message);
gtk_window_set_title (GTK_WINDOW (dialog), "");
if (detail)
gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
"%s", detail);
if (parent && gtk_window_get_group (parent))
gtk_window_group_add_window (gtk_window_get_group (parent), GTK_WINDOW (dialog));
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
}
/* find the toplevel widget for @widget */
static GtkWindow *
get_toplevel_window (GtkWidget *widget)
{
GtkWidget *toplevel;
toplevel = gtk_widget_get_toplevel (widget);
if (!gtk_widget_is_toplevel (toplevel))
return NULL;
else
return GTK_WINDOW (toplevel);
}
/**
* gdict_show_error_dialog:
* @widget: the widget that emits the error
* @title: the primary error message
* @message: the secondary error message or %NULL
*
* Creates and shows an error dialog bound to @widget.
*
* Since: 1.0
*/
void
_gdict_show_error_dialog (GtkWidget *widget,
const gchar *title,
const gchar *detail)
{
g_return_if_fail (GTK_IS_WIDGET (widget));
g_return_if_fail (title != NULL);
show_error_dialog (get_toplevel_window (widget), title, detail);
}
/**
* gdict_show_gerror_dialog:
* @widget: the widget that emits the error
* @title: the primary error message
* @error: a #GError
*
* Creates and shows an error dialog bound to @widget, using @error
* to fill the secondary text of the message dialog with the error
* message. Also takes care of freeing @error.
*
* Since: 1.0
*/
void
_gdict_show_gerror_dialog (GtkWidget *widget,
const gchar *title,
GError *error)
{
g_return_if_fail (GTK_IS_WIDGET (widget));
g_return_if_fail (title != NULL);
g_return_if_fail (error != NULL);
show_error_dialog (get_toplevel_window (widget), title, error->message);
g_error_free (error);
}
|