Load and save config
authorThomas Thurman <tthurman@gnome.org>
Sun, 30 Aug 2009 17:27:09 +0000 (13:27 -0400)
committerThomas Thurman <tthurman@gnome.org>
Sun, 30 Aug 2009 17:27:09 +0000 (13:27 -0400)
belltower.c

index e73934d..78ed46f 100644 (file)
 #include <dbus/dbus-glib.h>
 
 #define MAX_FIELDS 50
+#define CONFIG_GENERAL_GROUP "General"
+#define CONFIG_SEEN_CREDITS_KEY "seen_credits"
+#define CONFIG_DIRECTORY "/home/user/.config/belltower"
+#define CONFIG_FILENAME CONFIG_DIRECTORY "/belltower.ini"
 
 GtkWidget *window;
 LocationGPSDevice *device;
 GKeyFile *static_content;
+GKeyFile *config;
 
 typedef enum {
   /** stop scanning the database */
@@ -96,6 +101,55 @@ show_message (char *message)
   gtk_widget_destroy (GTK_WIDGET (note));
 }
 
+/**
+ * Loads the content of the static and dynamic data files.
+ * Possibly puts up a warning if we can't load the static file.
+ */
+static void
+load_config (void)
+{
+  static_content = g_key_file_new ();
+
+  if (!g_key_file_load_from_file (static_content,
+                                 "/usr/share/belltower/static.ini",
+                                 G_KEY_FILE_NONE,
+                                 NULL))
+    {
+      show_message ("Could not load static content.  Attempting to continue.");
+    }
+
+  config = g_key_file_new ();
+  /* it doesn't matter if this fails */
+  g_key_file_load_from_file (config,
+                            CONFIG_FILENAME,
+                            G_KEY_FILE_KEEP_COMMENTS,
+                            NULL);
+}
+
+/**
+ * Saves the dynamic data file to disk.
+ * Puts up a message if there was any error.
+ */
+static void
+save_config (void)
+{
+  gchar *data;
+
+  g_mkdir_with_parents (CONFIG_DIRECTORY, 0700);
+
+  data = g_key_file_to_data (config, NULL, NULL);
+
+  if (!g_file_set_contents (CONFIG_FILENAME,
+                           data,
+                           -1,
+                           NULL))
+    {
+      show_message ("Could not write config file.");
+    }
+
+  g_free (data);
+}
+
 static gint
 distance_to_tower (tower *details)
 {
@@ -855,24 +909,6 @@ recent_towers (void)
 }
 
 /**
- * Loads the content of the static data file.  Possibly puts
- * up a warning if we can't load it.
- */
-static void
-load_static_content (void)
-{
-  static_content = g_key_file_new ();
-
-  if (!g_key_file_load_from_file (static_content,
-                                 "/usr/share/belltower/static.ini",
-                                 G_KEY_FILE_NONE,
-                                 NULL))
-    {
-      show_message ("Could not load static content.  Attempting to continue.");
-    }
-}
-
-/**
  * Displays a web page.
  * (Perhaps this should be merged with show_browser().)
  *
@@ -900,6 +936,16 @@ show_credits (GtkButton *source,
   gboolean from_button = (source!=NULL);
   GtkWidget *dialog, *label, *button;
 
+  if (!from_button &&
+      g_key_file_get_boolean (config,
+                             CONFIG_GENERAL_GROUP,
+                             CONFIG_SEEN_CREDITS_KEY,
+                             NULL))
+    {
+      return;
+    }
+                             
+
   dialog = gtk_dialog_new_with_buttons ("Credits",
                                        GTK_WINDOW (window),
                                        GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
@@ -937,6 +983,12 @@ show_credits (GtkButton *source,
                    TRUE, TRUE, 0);
   
   gtk_widget_show_all (GTK_WIDGET (dialog));
+
+  g_key_file_set_boolean (config,
+                         CONFIG_GENERAL_GROUP,
+                         CONFIG_SEEN_CREDITS_KEY,
+                         TRUE);
+  save_config ();
 }
 
 int
@@ -982,7 +1034,7 @@ main(int argc, char **argv)
   gtk_container_add (GTK_CONTAINER (window), hbox);
   gtk_widget_show_all (GTK_WIDGET (window));
 
-  load_static_content ();
+  load_config ();
   show_credits (NULL, NULL);
 
   gtk_main ();