/*------------------------------------------------------------------------- * Copyright (c) 1999-2004 Kenneth W. Sodemann (stuffle@mac.com) *------------------------------------------------------------------------- * util * * Synopsis: * Various utility functions that could come in handy in more than * one module in this system. * * $Id: util.c,v 1.26 2004/09/07 02:23:03 stuffle Exp $ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to * Free Software Foundation, Inc. * 59 Temple Place, Suite 330 * Boston, MA 02111-1307 USA * *------------------------------------------------------------------------- */ #ifdef HAVE_CONFIG_H # include #endif #include #include #include #include #include #include #include #include #include "defs.h" #include "props.h" #include "util.h" void destroy_name_pk_list (GList *list) { GList *list_iter; if (list != NULL) { list_iter = g_list_first (list); while (list_iter) { g_free (((name_pk_struct *)list_iter->data)->name); g_free (list_iter->data); list_iter = list_iter->next; } g_list_free (list); } } name_pk_struct* find_name_pk_from_pk (GList *list, gint pk) { assert (list); list = g_list_first (list); while (list) { if (pk == ((name_pk_struct *)list->data)->pk) { return (name_pk_struct *)list->data; } list = list->next; } return NULL; } name_pk_struct* find_name_pk_from_name (GList *list, const gchar *name) { assert (list); assert (name); list = g_list_first (list); while (list) { if (strcmp (((name_pk_struct *)list->data)->name, name) == 0) { return (name_pk_struct *)list->data; } list = list->next; } return NULL; } gint find_pk_from_name (GList *list, const gchar *name) { name_pk_struct *tmp; if ((tmp = find_name_pk_from_name (list, name)) != NULL) { return tmp->pk; } return INVALID_PK; } gchar* find_name_from_pk (GList *list, gint pk) { name_pk_struct *tmp; if ((tmp = find_name_pk_from_pk (list, pk)) != NULL) { return tmp->name; } return NULL; } void destroy_name_id_list (GList *list) { GList *list_iter; if (list != NULL) { list_iter = g_list_first (list); while (list_iter) { g_free (((name_id_struct *)list_iter->data)->name); g_free (((name_id_struct *)list_iter->data)->id); g_free (list_iter->data); list_iter = list_iter->next; } g_list_free (list); } } name_id_struct* find_name_id_from_id (GList *list, const gchar *key) { assert (list); assert (key); list = g_list_first (list); while (list) { if (strcmp (((name_id_struct *)list->data)->id, key) == 0) { return (name_id_struct *)list->data; } list = list->next; } return NULL; } name_id_struct* find_name_id_from_name (GList *list, const gchar *key) { assert (list); assert (key); list = g_list_first (list); while (list) { if (strcmp (((name_id_struct *)list->data)->name, key) == 0) { return (name_id_struct *)list->data; } list = list->next; } return NULL; } gchar * find_name_from_id (GList *list, const gchar *key) { name_id_struct *tmp; if ((tmp = find_name_id_from_id (list, key)) != NULL) { return tmp->name; } return NULL; } gchar * find_id_from_name (GList *list, const gchar *key) { name_id_struct *tmp; if ((tmp = find_name_id_from_name (list, key)) != NULL) { return tmp->id; } return NULL; } /* * NOTE: * * We are doing a little trickery here to get a completely independent * process. We are fork()'ing twice, and only waiting on the first * fork(). */ gint spawn_viewer (const gchar *viewer, const gchar *file_name) { gint pid; gint status; pid = fork(); if (pid == -1) { syslog (LOG_ERR, "spawning viewer: first fork() failed: %m"); return -1; } if (pid == 0) { pid = fork(); if (pid == -1) { syslog (LOG_ERR, "spawning viewer: second fork() failed: %m"); _exit (-1); } if (pid == 0) { if (execlp (viewer, viewer, file_name, NULL) == -1) { syslog (LOG_ERR, "spawning viewer: execlp() failed: %m"); _exit (-1); } } _exit (0); } if (waitpid (pid, &status, 0) == -1) { syslog (LOG_ERR, "spawning viewer: bad status from waitpid(): %m"); return -2; } return status; } void save_dlg_size (GConfClient *client, GtkWidget *dlg, const gchar *name) { GString *cfg; gint x; gint y; if (remember_window_sizes (client) && GDK_IS_DRAWABLE (dlg->window)) { cfg = g_string_new (""); gdk_drawable_get_size (GDK_DRAWABLE (dlg->window), &x, &y); g_string_printf (cfg, "%s/%s/%s/%s", BASE_KEY, WINSTATES, name, X_SIZE); gconf_client_set_int (client, cfg->str, x, NULL); g_string_printf (cfg, "%s/%s/%s/%s", BASE_KEY, WINSTATES, name, Y_SIZE); gconf_client_set_int (client, cfg->str, y, NULL); g_string_free (cfg, TRUE); } return; } void set_dlg_size (GConfClient *client, GtkWidget *dlg, const gchar *name, gint x_def, gint y_def) { GError *error = NULL; GString *cfg; gint x; gint y; if (remember_window_sizes (client)) { cfg = g_string_new (""); g_string_printf (cfg, "%s/%s/%s/%s", BASE_KEY, WINSTATES, name, X_SIZE); x = gconf_client_get_int (client, cfg->str, &error); if (error != NULL) { g_warning (error->message); g_error_free (error); error = NULL; x = x_def; } if (x == 0) { x = x_def; } g_string_printf (cfg, "%s/%s/%s/%s", BASE_KEY, WINSTATES, name, Y_SIZE); y = gconf_client_get_int (client, cfg->str, &error); if (error != NULL) { g_warning (error->message); g_error_free (error); error = NULL; y = y_def; } if (y == 0) { y = y_def; } g_string_free (cfg, TRUE); } else { x = x_def; y = y_def; } gtk_window_set_default_size (GTK_WINDOW (dlg), x, y); return; }