Add default filename and divelist font to prefs structure

.. and add the usual logic to not save the default values.

This also simplifies the initial system-specific setup of both of these:
since we have defaults for all the preferences that get set up at
startup, we can just initialize those defaults to the system-specific
fonts then and there.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Linus Torvalds 2013-01-11 17:07:22 -08:00 committed by Dirk Hohndel
parent 954290c70b
commit 6a10700ca5
11 changed files with 84 additions and 89 deletions

2
dive.h
View file

@ -569,9 +569,7 @@ typedef enum {
UTF8_FONT_WITH_STARS UTF8_FONT_WITH_STARS
} os_feature_t; } os_feature_t;
extern const char *default_filename;
extern const char *existing_filename; extern const char *existing_filename;
extern const char *subsurface_default_filename(void);
extern const char *subsurface_gettext_domainpath(char *); extern const char *subsurface_gettext_domainpath(char *);
extern gboolean subsurface_os_feature_available(os_feature_t); extern gboolean subsurface_os_feature_available(os_feature_t);
extern void subsurface_command_line_init(gint *, gchar ***); extern void subsurface_command_line_init(gint *, gchar ***);

View file

@ -2659,7 +2659,7 @@ GtkWidget *dive_list_create(void)
); );
dive_list.model = dive_list.treemodel; dive_list.model = dive_list.treemodel;
dive_list.tree_view = gtk_tree_view_new_with_model(TREEMODEL(dive_list)); dive_list.tree_view = gtk_tree_view_new_with_model(TREEMODEL(dive_list));
set_divelist_font(divelist_font); set_divelist_font(prefs.divelist_font);
selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view)); selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view));

2
file.c
View file

@ -255,7 +255,7 @@ void parse_file(const char *filename, GError **error, gboolean possible_default_
if (readfile(filename, &mem) < 0) { if (readfile(filename, &mem) < 0) {
/* we don't want to display an error if this was the default file */ /* we don't want to display an error if this was the default file */
if (default_filename && ! strcmp(filename, default_filename)) if (prefs.default_filename && ! strcmp(filename, prefs.default_filename))
return; return;
g_warning(_("Failed to read '%s'.\n"), filename); g_warning(_("Failed to read '%s'.\n"), filename);

View file

@ -33,8 +33,6 @@ GtkWidget *notebook;
int error_count; int error_count;
const char *existing_filename; const char *existing_filename;
const char *divelist_font;
const char *default_filename;
char *nicknamestring; char *nicknamestring;
@ -129,7 +127,7 @@ static void file_save_as(GtkWidget *w, gpointer data)
current_dir = g_path_get_dirname(existing_filename); current_dir = g_path_get_dirname(existing_filename);
current_file = g_path_get_basename(existing_filename); current_file = g_path_get_basename(existing_filename);
} else { } else {
const char *current_default = subsurface_default_filename(); const char *current_default = prefs.default_filename;
current_dir = g_path_get_dirname(current_default); current_dir = g_path_get_dirname(current_default);
current_file = g_path_get_basename(current_default); current_file = g_path_get_basename(current_default);
} }
@ -159,7 +157,7 @@ static void file_save(GtkWidget *w, gpointer data)
if (!existing_filename) if (!existing_filename)
return file_save_as(w, data); return file_save_as(w, data);
current_default = subsurface_default_filename(); current_default = prefs.default_filename;
if (strcmp(existing_filename, current_default) == 0) { if (strcmp(existing_filename, current_default) == 0) {
/* if we are using the default filename the directory /* if we are using the default filename the directory
* that we are creating the file in may not exist */ * that we are creating the file in may not exist */
@ -255,7 +253,7 @@ static void file_open(GtkWidget *w, gpointer data)
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
NULL); NULL);
current_default = subsurface_default_filename(); current_default = prefs.default_filename;
gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), current_default); gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), current_default);
/* when opening the data file we should allow only one file to be chosen */ /* when opening the data file we should allow only one file to be chosen */
gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), FALSE); gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), FALSE);
@ -496,7 +494,7 @@ static void pick_default_file(GtkWidget *w, GtkButton *button)
gtk_widget_set_sensitive(parent, FALSE); gtk_widget_set_sensitive(parent, FALSE);
gtk_window_set_transient_for(GTK_WINDOW(fs_dialog), GTK_WINDOW(parent)); gtk_window_set_transient_for(GTK_WINDOW(fs_dialog), GTK_WINDOW(parent));
current_default = subsurface_default_filename(); current_default = prefs.default_filename;
current_def_dir = g_path_get_dirname(current_default); current_def_dir = g_path_get_dirname(current_default);
current_def_file = g_path_get_basename(current_default); current_def_file = g_path_get_basename(current_default);
@ -522,7 +520,6 @@ static void pick_default_file(GtkWidget *w, GtkButton *button)
free(current_def_dir); free(current_def_dir);
free(current_def_file); free(current_def_file);
free((void *)current_default);
gtk_widget_destroy(fs_dialog); gtk_widget_destroy(fs_dialog);
gtk_widget_set_sensitive(parent, TRUE); gtk_widget_set_sensitive(parent, TRUE);
@ -622,7 +619,7 @@ static void preferences_dialog(GtkWidget *w, gpointer data)
frame = gtk_frame_new(_("Divelist Font")); frame = gtk_frame_new(_("Divelist Font"));
gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5); gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
font = gtk_font_button_new_with_font(divelist_font); font = gtk_font_button_new_with_font(prefs.divelist_font);
gtk_container_add(GTK_CONTAINER(frame),font); gtk_container_add(GTK_CONTAINER(frame),font);
frame = gtk_frame_new(_("Misc. Options")); frame = gtk_frame_new(_("Misc. Options"));
@ -635,7 +632,7 @@ static void preferences_dialog(GtkWidget *w, gpointer data)
gtk_box_pack_start(GTK_BOX(box), frame, FALSE, FALSE, 5); gtk_box_pack_start(GTK_BOX(box), frame, FALSE, FALSE, 5);
box = gtk_hbox_new(FALSE, 6); box = gtk_hbox_new(FALSE, 6);
gtk_container_add(GTK_CONTAINER(frame), box); gtk_container_add(GTK_CONTAINER(frame), box);
current_default = subsurface_default_filename(); current_default = prefs.default_filename;
xmlfile_button = gtk_button_new_with_label(current_default); xmlfile_button = gtk_button_new_with_label(current_default);
g_signal_connect(G_OBJECT(xmlfile_button), "clicked", g_signal_connect(G_OBJECT(xmlfile_button), "clicked",
G_CALLBACK(pick_default_file), xmlfile_button); G_CALLBACK(pick_default_file), xmlfile_button);
@ -773,10 +770,8 @@ static void preferences_dialog(GtkWidget *w, gpointer data)
/* Make sure to flush any modified old dive data with old units */ /* Make sure to flush any modified old dive data with old units */
update_dive(NULL); update_dive(NULL);
if (divelist_font) prefs.divelist_font = strdup(gtk_font_button_get_font_name(GTK_FONT_BUTTON(font)));
free((void *)divelist_font); set_divelist_font(prefs.divelist_font);
divelist_font = strdup(gtk_font_button_get_font_name(GTK_FONT_BUTTON(font)));
set_divelist_font(divelist_font);
po2_threshold_text = gtk_entry_get_text(GTK_ENTRY(entry_po2)); po2_threshold_text = gtk_entry_get_text(GTK_ENTRY(entry_po2));
sscanf(po2_threshold_text, "%lf", &prefs.pp_graphs.po2_threshold); sscanf(po2_threshold_text, "%lf", &prefs.pp_graphs.po2_threshold);
pn2_threshold_text = gtk_entry_get_text(GTK_ENTRY(entry_pn2)); pn2_threshold_text = gtk_entry_get_text(GTK_ENTRY(entry_pn2));
@ -805,8 +800,7 @@ static void preferences_dialog(GtkWidget *w, gpointer data)
} }
if (strcmp(current_default, new_default)) { if (strcmp(current_default, new_default)) {
free((void *)default_filename); prefs.default_filename = new_default;
default_filename = new_default;
} }
save_preferences(); save_preferences();
@ -815,7 +809,6 @@ static void preferences_dialog(GtkWidget *w, gpointer data)
set_gf(prefs.gflow, prefs.gfhigh); set_gf(prefs.gflow, prefs.gfhigh);
update_screen(); update_screen();
} }
free((void *)current_default);
gtk_widget_destroy(dialog); gtk_widget_destroy(dialog);
} }
@ -1319,8 +1312,6 @@ void run_ui(void)
void exit_ui(void) void exit_ui(void)
{ {
subsurface_close_conf(); subsurface_close_conf();
if (default_filename)
free((char *)default_filename);
if (existing_filename) if (existing_filename)
free((void *)existing_filename); free((void *)existing_filename);
if (default_dive_computer_device) if (default_dive_computer_device)
@ -1573,9 +1564,8 @@ void import_files(GtkWidget *w, gpointer data)
if (existing_filename) { if (existing_filename) {
current_def_dir = g_path_get_dirname(existing_filename); current_def_dir = g_path_get_dirname(existing_filename);
} else { } else {
current_default = subsurface_default_filename(); current_default = prefs.default_filename;
current_def_dir = g_path_get_dirname(current_default); current_def_dir = g_path_get_dirname(current_default);
free((void *)current_default);
} }
/* it's possible that the directory doesn't exist (especially for the default) /* it's possible that the directory doesn't exist (especially for the default)

28
linux.c
View file

@ -5,7 +5,7 @@
#include <gconf/gconf-client.h> #include <gconf/gconf-client.h>
#include <string.h> #include <string.h>
#define DIVELIST_DEFAULT_FONT "Sans 8" const char system_divelist_default_font[] = "Sans 8";
GConfClient *gconf; GConfClient *gconf;
@ -131,22 +131,18 @@ const char *subsurface_icon_name()
return "subsurface.svg"; return "subsurface.svg";
} }
const char *subsurface_default_filename() const char *system_default_filename(void)
{ {
if (default_filename) { const char *home, *user;
return strdup(default_filename); char *buffer;
} else { int len;
const char *home, *user;
char *buffer;
int len;
home = g_get_home_dir(); home = g_get_home_dir();
user = g_get_user_name(); user = g_get_user_name();
len = strlen(home) + strlen(user) + 17; len = strlen(home) + strlen(user) + 17;
buffer = malloc(len); buffer = malloc(len);
snprintf(buffer, len, "%s/subsurface/%s.xml", home, user); snprintf(buffer, len, "%s/subsurface/%s.xml", home, user);
return buffer; return buffer;
}
} }
const char *subsurface_gettext_domainpath(char *argv0) const char *subsurface_gettext_domainpath(char *argv0)
@ -163,8 +159,6 @@ const char *subsurface_gettext_domainpath(char *argv0)
void subsurface_ui_setup(GtkSettings *settings, GtkWidget *menubar, void subsurface_ui_setup(GtkSettings *settings, GtkWidget *menubar,
GtkWidget *vbox, GtkUIManager *ui_manager) GtkWidget *vbox, GtkUIManager *ui_manager)
{ {
if (!divelist_font)
divelist_font = strdup(DIVELIST_DEFAULT_FONT);
gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
} }

29
macos.c
View file

@ -19,7 +19,8 @@ static GtkOSXApplication *osx_app;
#define SUBSURFACE_PREFERENCES CFSTR("org.hohndel.subsurface") #define SUBSURFACE_PREFERENCES CFSTR("org.hohndel.subsurface")
#define ICON_NAME "Subsurface.icns" #define ICON_NAME "Subsurface.icns"
#define UI_FONT "Arial Unicode MS 12" #define UI_FONT "Arial Unicode MS 12"
#define DIVELIST_MAC_DEFAULT_FONT "Arial Unicode MS 9"
const char system_divelist_default_font[] = "Arial Unicode MS 9";
void subsurface_open_conf(void) void subsurface_open_conf(void)
{ {
@ -134,22 +135,18 @@ const char *subsurface_icon_name()
return path; return path;
} }
const char *subsurface_default_filename() const char *system_default_filename(void)
{ {
if (default_filename) { const char *home, *user;
return strdup(default_filename); char *buffer;
} else { int len;
const char *home, *user;
char *buffer;
int len;
home = g_get_home_dir(); home = g_get_home_dir();
user = g_get_user_name(); user = g_get_user_name();
len = strlen(home) + strlen(user) + 45; len = strlen(home) + strlen(user) + 45;
buffer = malloc(len); buffer = malloc(len);
snprintf(buffer, len, "%s/Library/Application Support/Subsurface/%s.xml", home, user); snprintf(buffer, len, "%s/Library/Application Support/Subsurface/%s.xml", home, user);
return buffer; return buffer;
}
} }
const char *subsurface_gettext_domainpath(char *argv0) const char *subsurface_gettext_domainpath(char *argv0)
@ -176,8 +173,6 @@ void subsurface_ui_setup(GtkSettings *settings, GtkWidget *menubar,
{ {
GtkWidget *menu_item, *sep; GtkWidget *menu_item, *sep;
if (!divelist_font)
divelist_font = strdup(DIVELIST_MAC_DEFAULT_FONT);
g_object_set(G_OBJECT(settings), "gtk-font-name", UI_FONT, NULL); g_object_set(G_OBJECT(settings), "gtk-font-name", UI_FONT, NULL);
osx_app = g_object_new(GTK_TYPE_OSX_APPLICATION, NULL); osx_app = g_object_new(GTK_TYPE_OSX_APPLICATION, NULL);

11
main.c
View file

@ -268,8 +268,12 @@ void renumber_dives(int nr)
*/ */
static void setup_system_prefs(void) static void setup_system_prefs(void)
{ {
const char *env = getenv("LC_MEASUREMENT"); const char *env;
default_prefs.divelist_font = strdup(system_divelist_default_font);
default_prefs.default_filename = strdup(system_default_filename());
env = getenv("LC_MEASUREMENT");
if (!env) if (!env)
env = getenv("LC_ALL"); env = getenv("LC_ALL");
if (!env) if (!env)
@ -307,7 +311,7 @@ int main(int argc, char **argv)
#if DEBUGFILE > 1 #if DEBUGFILE > 1
debugfile = stderr; debugfile = stderr;
#elif defined(DEBUGFILE) #elif defined(DEBUGFILE)
debugfilename = (char *)subsurface_default_filename(); debugfilename = strdup(prefs.default_filename);
strncpy(debugfilename + strlen(debugfilename) - 3, "log", 3); strncpy(debugfilename + strlen(debugfilename) - 3, "log", 3);
if (g_mkdir_with_parents(g_path_get_dirname(debugfilename), 0664) != 0 || if (g_mkdir_with_parents(g_path_get_dirname(debugfilename), 0664) != 0 ||
(debugfile = g_fopen(debugfilename, "w")) == NULL) (debugfile = g_fopen(debugfilename, "w")) == NULL)
@ -339,12 +343,11 @@ int main(int argc, char **argv)
} }
if (no_filenames) { if (no_filenames) {
GError *error = NULL; GError *error = NULL;
const char *filename = subsurface_default_filename(); const char *filename = prefs.default_filename;
parse_file(filename, &error, TRUE); parse_file(filename, &error, TRUE);
/* don't report errors - this file may not exist, but make /* don't report errors - this file may not exist, but make
sure we remember this as the filename in use */ sure we remember this as the filename in use */
set_filename(filename, FALSE); set_filename(filename, FALSE);
free((void *)filename);
} }
report_dives(imported, FALSE); report_dives(imported, FALSE);
if (dive_table.nr == 0) if (dive_table.nr == 0)

6
pref.h
View file

@ -30,6 +30,8 @@ struct preferences {
gboolean calc_ceiling_3m_incr; gboolean calc_ceiling_3m_incr;
double gflow; double gflow;
double gfhigh; double gfhigh;
const char *divelist_font;
const char *default_filename;
}; };
extern struct preferences prefs, default_prefs; extern struct preferences prefs, default_prefs;
@ -45,8 +47,8 @@ extern int subsurface_get_conf_bool(char *name);
extern void subsurface_flush_conf(void); extern void subsurface_flush_conf(void);
extern void subsurface_close_conf(void); extern void subsurface_close_conf(void);
/* Misc preferences - should we have defaults for these too? */ extern const char system_divelist_default_font[];
extern const char *divelist_font; extern const char *system_default_filename();
extern void load_preferences(void); extern void load_preferences(void);
extern void save_preferences(void); extern void save_preferences(void);

26
prefs.c
View file

@ -1,3 +1,5 @@
#include <string.h>
#include "dive.h" #include "dive.h"
static void set_bool_conf(char *name, gboolean value, gboolean def) static void set_bool_conf(char *name, gboolean value, gboolean def)
@ -13,6 +15,16 @@ static void set_bool_conf(char *name, gboolean value, gboolean def)
#define SAVE_UNIT(name, field, value) __SAVE_BOOLEAN(name, units.field, value) #define SAVE_UNIT(name, field, value) __SAVE_BOOLEAN(name, units.field, value)
#define SAVE_BOOL(name, field) __SAVE_BOOLEAN(name, field, TRUE) #define SAVE_BOOL(name, field) __SAVE_BOOLEAN(name, field, TRUE)
static void set_string_conf(char *name, const char *value, const char *def)
{
if (!strcmp(value, def)) {
subsurface_unset_conf(name);
return;
}
subsurface_set_conf(name, value);
}
#define SAVE_STRING(name, field) set_string_conf(name, prefs.field, default_prefs.field)
/* We don't really save doubles */ /* We don't really save doubles */
static void save_double_conf(char *name, double _val, double _def) static void save_double_conf(char *name, double _val, double _def)
{ {
@ -63,7 +75,7 @@ void save_preferences(void)
SAVE_BOOL("OTU", visible_cols.otu); SAVE_BOOL("OTU", visible_cols.otu);
SAVE_BOOL("MAXCNS", visible_cols.maxcns); SAVE_BOOL("MAXCNS", visible_cols.maxcns);
subsurface_set_conf("divelist_font", divelist_font); SAVE_STRING("divelist_font", divelist_font);
SAVE_BOOL("po2graph", pp_graphs.po2); SAVE_BOOL("po2graph", pp_graphs.po2);
SAVE_BOOL("pn2graph", pp_graphs.pn2); SAVE_BOOL("pn2graph", pp_graphs.pn2);
@ -80,7 +92,7 @@ void save_preferences(void)
SAVE_PERCENT("gflow", gflow); SAVE_PERCENT("gflow", gflow);
SAVE_PERCENT("gfhigh", gfhigh); SAVE_PERCENT("gfhigh", gfhigh);
subsurface_set_conf("default_filename", default_filename); SAVE_STRING("default_filename", default_filename);
/* Flush the changes out to the system */ /* Flush the changes out to the system */
subsurface_flush_conf(); subsurface_flush_conf();
@ -153,7 +165,13 @@ void load_preferences(void)
free((void *)conf_value); free((void *)conf_value);
} }
set_gf(prefs.gflow, prefs.gfhigh); set_gf(prefs.gflow, prefs.gfhigh);
divelist_font = subsurface_get_conf("divelist_font");
default_filename = subsurface_get_conf("default_filename"); conf_value = subsurface_get_conf("divelist_font");
if (conf_value)
prefs.divelist_font = conf_value;
conf_value = subsurface_get_conf("default_filename");
if (conf_value)
prefs.default_filename = conf_value;
} }

View file

@ -154,7 +154,7 @@ static void init_tree()
GtkTreeViewColumn *column; GtkTreeViewColumn *column;
GtkTreeStore *store; GtkTreeStore *store;
int i; int i;
PangoFontDescription *font_desc = pango_font_description_from_string(divelist_font); PangoFontDescription *font_desc = pango_font_description_from_string(prefs.divelist_font);
gtk_widget_modify_font(yearly_tree, font_desc); gtk_widget_modify_font(yearly_tree, font_desc);
pango_font_description_free(font_desc); pango_font_description_free(font_desc);

View file

@ -4,7 +4,8 @@
#include "display-gtk.h" #include "display-gtk.h"
#include <windows.h> #include <windows.h>
#include <shlobj.h> #include <shlobj.h>
#define DIVELIST_DEFAULT_FONT "Sans 8"
const char system_divelist_default_font[] = "Sans 8";
static HKEY hkey; static HKEY hkey;
@ -208,26 +209,22 @@ const char *subsurface_icon_name()
return "subsurface.ico"; return "subsurface.ico";
} }
const char *subsurface_default_filename() const char *system_default_filename(void)
{ {
if (default_filename) { char datapath[MAX_PATH];
return strdup(default_filename); const char *user;
} else { char *buffer;
char datapath[MAX_PATH]; int len;
const char *user;
char *buffer;
int len;
user = g_get_user_name(); user = g_get_user_name();
if (! SUCCEEDED(SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, 0, datapath))) { if (! SUCCEEDED(SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, 0, datapath))) {
datapath[0] = '.'; datapath[0] = '.';
datapath[1] = '\0'; datapath[1] = '\0';
}
len = strlen(datapath) + strlen(user) + 17;
buffer = malloc(len);
snprintf(buffer, len, "%s\\Subsurface\\%s.xml", datapath, user);
return buffer;
} }
len = strlen(datapath) + strlen(user) + 17;
buffer = malloc(len);
snprintf(buffer, len, "%s\\Subsurface\\%s.xml", datapath, user);
return buffer;
} }
const char *subsurface_gettext_domainpath(char *argv0) const char *subsurface_gettext_domainpath(char *argv0)
@ -244,8 +241,6 @@ const char *subsurface_gettext_domainpath(char *argv0)
void subsurface_ui_setup(GtkSettings *settings, GtkWidget *menubar, void subsurface_ui_setup(GtkSettings *settings, GtkWidget *menubar,
GtkWidget *vbox, GtkUIManager *ui_manager) GtkWidget *vbox, GtkUIManager *ui_manager)
{ {
if (!divelist_font)
divelist_font = strdup(DIVELIST_DEFAULT_FONT);
gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
} }