gnome-control-center/capplets/screensaver/selection-dialog.c
Bradford Hovinen 148d8186a5 Free structures only if they were ever allocated
2000-08-07  Bradford Hovinen  <hovinen@helixcode.com>

	* screensaver-prefs-dialog.c (screensaver_prefs_dialog_destroy):
	Free structures only if they were ever allocated

	* selection-dialog.c (get_known_savers): Implement. Read the
	screensaver directory for a list of valid screensavers
	(place_screensaver_list): Use known_savers rather than XML data

	* preferences.c (screensaver_get_label): Implement. Copy from
	get_label in resources.c

	* prefs-widget.c (deselect_saver_cb): Toggle only if event was a
	mouse button press, not a keyboard press
	(prefs_widget_init): Change vertical size of preview window to 200
	(prefs_widget_init): Use a table for the buttons below the
	screensaver list
	(prefs_widget_init): Change label of edit button to Settings...
	(prefs_widget_init): Don't have description and preview frames
	expand, have selection frame expand

	* Makefile.am (screensaver_properties_capplet_LDADD): Don't link
	against glade
	(INCLUDES): Don't set glade compilation flags

	* screensaver-prefs-dialog.c (get_select_widget): Show menu item
	(populate_table): Don't skip hgroups that lack ids
	(place_number):
	(place_boolean): Attach items with GTK_FILL
	(get_check_button):
	(get_select_widget): Set object data to point back to dialog
	(populate_table): Pass dialog, remove extraneous arguments
	(get_check_button): Connect toggled signal from here
	(get_select_widget): Set data and connect activate signals from
	here
	(place_number): Set default from default attribute
	(get_spinbutton): Set step_increment, page_increment, and
	page_size on adjustment
	(get_spinbutton):
	(place_number): Set default to average of high and low if not
	supplied
	(get_argument_data): Add i18n support
	(read_boolean):
	(read_number):
	(read_select):
	(write_boolean):
	(write_number):
	(write_select): Check if id is NULL before trying to look up a node
	(screensaver_prefs_dialog_new): Make sure CLI arguments and XML
	data were read successfully before trying to build the widget
	(get_argument_data): Return pointer to document rather than root node
	(screensaver_prefs_dialog_new): Set argument_data from argument_doc
	(screensaver_prefs_dialog_class_init): Set parent_class
	(screensaver_prefs_dialog_destroy): Implement; free all structures
	(free_set_cb): Implement
	(write_number): Don't assume correct format on the output string;
	just replace the '%' with the appropriate argument
	(write_command_line): Free arg iff generated from number

2000-08-06  Bradford Hovinen  <hovinen@helixcode.com>

	* screensaver-prefs-dialog.c (get_screensaver_widget): Accept
	ScreensaverPrefsDialog as option
	(get_argument_data): Assume one file per screensaver
	(get_spinbutton):
	(get_check_button):
	(get_select_widget):
	(place_number):
	(place_boolean):
	(place_hgroup):
	(place_select):
	(populate_table): Implement
	(get_screensaver_widget): Rewrite to build screensaver config
	dialogs manually from the XML data
	(activate_option_cb):
	(toggle_check_cb): Pass dialog to set_widgets_sensitive
	(set_widgets_sensitive): Rewrite to use dialog->widget_db
	(write_boolean):
	(write_number):
	(write_select): Use widget_db
	(write_select):
	(write_command_line): Don't check no-output
	(write_command_line): Pass widget_db
	(read_boolean):
	(read_number):
	(read_select): Use widget_db
	(place_screensaver_properties): Pass dialog->widget_db to above
	functions
	(arg_mapping_exists): Use XML file rather than Glade definition
	(screensaver_prefs_dialog_new): Initialize settings_widget with
	the widget to be placed in the settings frame
	(place_screensaver_properties): Take xml node; process hgroups
	recursively
	(write_command_line): Process hgroups recursively
	(read_boolean):
	(read_number):
	(read_select): Check if set and set->value_widget are set before
	proceeding

	* screensaver-prefs-dialog.h (struct _PrefsDialogConfigOption):
	New structure
	(struct _ScreensaverPrefsDialog ): New member widget_db --
	key-value database of PrefsDialogConfigOptions indexed by id
	attribute from XML
2000-08-07 16:04:33 +00:00

310 lines
8.1 KiB
C

/* -*- mode: c; style: linux -*- */
/* selection-dialog.c
* Copyright (C) 2000 Helix Code, Inc.
*
* Written by Bradford Hovinen <hovinen@helixcode.com>
* Parts written by Jamie Zawinski <jwz@jwz.org>
*
* 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, 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 the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <ctype.h>
#include <gnome.h>
#include <parser.h>
#include "selection-dialog.h"
#include "preferences.h"
enum {
OK_CLICKED_SIGNAL,
LAST_SIGNAL
};
static gint selection_dialog_signals[LAST_SIGNAL] = { 0 };
typedef struct _saver_entry_t
{
gchar *name;
gchar *label;
} saver_entry_t;
GList *known_savers;
static void selection_dialog_init (SelectionDialog *dialog);
static void selection_dialog_class_init (SelectionDialogClass *dialog);
static void place_screensaver_list (SelectionDialog *dialog);
static void select_program_cb (GtkListItem *item,
SelectionDialog *dialog);
static void selection_dialog_ok_cb (GtkWidget *widget,
SelectionDialog *dialog);
static void selection_dialog_cancel_cb (GtkWidget *widget,
SelectionDialog *dialog);
static GList *get_known_savers (void);
guint
selection_dialog_get_type (void)
{
static guint selection_dialog_type = 0;
if (!selection_dialog_type) {
GtkTypeInfo selection_dialog_info = {
"SelectionDialog",
sizeof (SelectionDialog),
sizeof (SelectionDialogClass),
(GtkClassInitFunc) selection_dialog_class_init,
(GtkObjectInitFunc) selection_dialog_init,
(GtkArgSetFunc) NULL,
(GtkArgGetFunc) NULL
};
selection_dialog_type =
gtk_type_unique (gnome_dialog_get_type (),
&selection_dialog_info);
}
return selection_dialog_type;
}
static void
selection_dialog_init (SelectionDialog *dialog)
{
GtkWidget *label, *scrolled_window, *viewport;
GtkBox *vbox;
gtk_window_set_title (GTK_WINDOW (dialog), _("Add a new screensaver"));
gnome_dialog_append_button (GNOME_DIALOG (dialog),
GNOME_STOCK_BUTTON_OK);
gnome_dialog_append_button (GNOME_DIALOG (dialog),
GNOME_STOCK_BUTTON_CANCEL);
vbox = GTK_BOX (GNOME_DIALOG (dialog)->vbox);
label = gtk_label_new (_("Select the screensaver to run from " \
"the list below:"));
gtk_box_pack_start (vbox, label, FALSE, TRUE, 0);
scrolled_window = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
gtk_box_pack_start (vbox, scrolled_window, TRUE, TRUE, 0);
viewport = gtk_viewport_new (NULL, NULL);
gtk_container_add (GTK_CONTAINER (scrolled_window), viewport);
dialog->program_list = GTK_LIST (gtk_list_new ());
gtk_list_set_selection_mode (GTK_LIST (dialog->program_list),
GTK_SELECTION_SINGLE);
gtk_container_add (GTK_CONTAINER (viewport),
GTK_WIDGET (dialog->program_list));
gnome_dialog_button_connect (GNOME_DIALOG (dialog), 0,
GTK_SIGNAL_FUNC (selection_dialog_ok_cb),
dialog);
gnome_dialog_button_connect (GNOME_DIALOG (dialog), 1,
GTK_SIGNAL_FUNC
(selection_dialog_cancel_cb),
dialog);
gtk_widget_show_all (GNOME_DIALOG (dialog)->vbox);
}
static void
selection_dialog_class_init (SelectionDialogClass *class)
{
GtkObjectClass *object_class;
object_class = (GtkObjectClass *) class;
selection_dialog_signals[OK_CLICKED_SIGNAL] =
gtk_signal_new ("ok-clicked", GTK_RUN_FIRST,
object_class->type,
GTK_SIGNAL_OFFSET (SelectionDialogClass,
ok_clicked),
gtk_marshal_NONE__POINTER,
GTK_TYPE_NONE, 1, GTK_TYPE_POINTER);
gtk_object_class_add_signals (object_class,
selection_dialog_signals,
LAST_SIGNAL);
known_savers = get_known_savers ();
class->ok_clicked = NULL;
}
GtkWidget *
selection_dialog_new (PrefsWidget *prefs_widget)
{
GtkWidget *widget;
widget = gtk_type_new (selection_dialog_get_type ());
place_screensaver_list (SELECTION_DIALOG (widget));
gtk_widget_show (widget);
return widget;
}
static void
place_screensaver_list (SelectionDialog *dialog)
{
GtkWidget *item;
GList *item_list_head = NULL, *item_list_tail = NULL, *node;
gchar *label;
saver_entry_t *entry;
for (node = known_savers; node; node = node->next) {
entry = (saver_entry_t *) node->data;
item = gtk_list_item_new_with_label (entry->label);
gtk_widget_show (item);
gtk_object_set_data (GTK_OBJECT (item), "name", entry->name);
gtk_signal_connect (GTK_OBJECT (item), "select",
GTK_SIGNAL_FUNC (select_program_cb),
dialog);
item_list_tail = g_list_append (item_list_tail, item);
if (!item_list_head) item_list_head = item_list_tail;
item_list_tail = g_list_last (item_list_tail);
}
item = gtk_list_item_new_with_label (_("Custom"));
gtk_widget_show (item);
gtk_signal_connect (GTK_OBJECT (item), "select",
GTK_SIGNAL_FUNC (select_program_cb), dialog);
item_list_tail = g_list_append (item_list_tail, item);
if (!item_list_head) item_list_head = item_list_tail;
gtk_list_append_items (dialog->program_list, item_list_head);
gtk_list_select_item (dialog->program_list, 0);
}
static void
select_program_cb (GtkListItem *item, SelectionDialog *dialog)
{
dialog->selected_program_item = item;
dialog->selected_name =
gtk_object_get_data (GTK_OBJECT (item), "name");
}
static void
selection_dialog_ok_cb (GtkWidget *widget, SelectionDialog *dialog)
{
Screensaver *saver;
saver = screensaver_new ();
saver->label = g_strdup (_("New screensaver"));
if (dialog->selected_name) {
saver->name =
g_strdup (dialog->selected_name);
saver->command_line =
g_strconcat (saver->name, " -root", NULL);
}
gtk_signal_emit (GTK_OBJECT (dialog),
selection_dialog_signals[OK_CLICKED_SIGNAL], saver);
gnome_dialog_close (GNOME_DIALOG (dialog));
}
static void
selection_dialog_cancel_cb (GtkWidget *widget, SelectionDialog *dialog)
{
gnome_dialog_close (GNOME_DIALOG (dialog));
}
static gint
node_compare (gconstpointer a, gconstpointer b)
{
return strcmp (((saver_entry_t *) a)->label,
((saver_entry_t *) b)->label);
}
static GList *
get_known_savers (void)
{
GList *list_head, *list_tail;
DIR *parent_dir;
struct dirent *child_dir;
struct stat filedata;
gchar *fullpath;
saver_entry_t *entry;
char *tmp, *name;
if (known_savers) return known_savers;
parent_dir = opendir (SSPROP_DATADIR "/screensavers");
if (parent_dir == NULL)
return NULL;
list_head = list_tail = NULL;
while ((child_dir = readdir (parent_dir)) != NULL) {
if (child_dir->d_name[0] != '.') {
fullpath = g_concat_dir_and_file
(SSPROP_DATADIR "/screensavers",
child_dir->d_name);
if (stat (fullpath, &filedata) != -1) {
if (!S_ISDIR (filedata.st_mode)) {
name = g_strdup (child_dir->d_name);
tmp = strstr (name, ".xml");
if (tmp) {
*tmp = '\0';
entry = g_new0
(saver_entry_t, 1);
entry->name = name;
entry->label =
screensaver_get_label
(name);
} else {
g_free (name);
}
} else {
entry = NULL;
}
if (entry) {
list_tail = g_list_append
(list_tail, entry);
if (!list_head)
list_head = list_tail;
else
list_tail = list_tail->next;
}
}
g_free (fullpath);
}
}
closedir (parent_dir);
list_head = g_list_sort (list_head, node_compare);
}