Greetings all,
I believed I had threatened this list with a patch for Roadster that
allows it to be compiled as a regular MySQL client program (i.e. talks
to a MySQL server rather than the embedded one), and while no one has
asked for the patch directly, I've seen several people trying to get
Roadster talking to an external MySQL server, so I am sending this patch
to the list.
It does basically two things: First, it checks for a .roadster
directory in the user's home directory, then for a "config" file in
there (e.g. ~/.roadster/config) and reads from that file. It expects to
see things in this format:
-----[.roadster/config]-----
# This is a comment.
[Database]
Type = mysql
Server = mysqlserver.local
Username = roadster
Password = password
Database = roadster
-----[end]-----
It uses the normal GTK g_key_file* routines, and loads into a
roadster_settings global structure (Which I hope will be useful for
future Roadster settings if necessary).
The Type key above can be one of the following: 'mysql' to use an
external MySQL server or 'embedded' to use the embedded server. If Type
is 'embedded', then the other keys are meaningless, but if it is set to
'mysql' then Server is the MySQL server to communicate with, Username is
the username to use, Password is the password, and Database is the
database Roadster data is contained in.
Currently my patch does not save the ~/.roadster/config file so the file
has to be created and edited by hand, but if you use the above as a
guide, it should work.
-KW
#
# old_revision [230daffffac0976ebcc326b46c1bb2d1a199e71a]
#
# add_file "src/settings.c"
# content [12cedfd87cbbdc637129cbc3ac145ed29098a637]
#
# add_file "src/settings.h"
# content [24960f78f16312794e8be6c552eb3a09334febfc]
#
# patch "src/Makefile.am"
# from [380a96816e30486fbc6507548c2ff2e5e752f659]
# to [f1eac94112e2447cdec24031caa74921a81b32f1]
#
# patch "src/db.c"
# from [c9bffcdd13c48a8a50d46a5a09a31dc11cad9c89]
# to [582d9e78e02d6539892f721f2461ad6f27797aad]
#
# patch "src/main.c"
# from [f204d5b6785f0078e5ae80e57839a9bd11c53588]
# to [3a1137eb7a3b11cb5ea3c0fb0c2fbca35687ed3a]
#
============================================================
--- src/settings.c 12cedfd87cbbdc637129cbc3ac145ed29098a637
+++ src/settings.c 12cedfd87cbbdc637129cbc3ac145ed29098a637
@@ -0,0 +1,121 @@
+/***************************************************************************
+ * settings.c
+ *
+ * Copyright 2006 Ben Carner < kwalker AT kobran DAWT 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 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 Library 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.
+ */
+
+#define _DEBUG 1
+
+#include "settings.h"
+#include <glib.h>
+
+r_settings_t roadster_settings; // Global struct, used elsewhere.
+
+// This is a very simple function to load config values from .roadster/config.
+// It has been many moons since my last C program, and I do not know GDK/GTK, so
+// do not laugh at my crappy code, help me make it better.
+int settings_load(void)
+{
+#ifdef USE_GNOME_VFS
+ GKeyFile *settings = g_key_file_new();
+ gchar *app_dir = g_strdup_printf("%s/.roadster", g_get_home_dir());
+ gchar *cfgfile = g_strdup_printf("%s/config", app_dir);
+ gchar *group = "Database";
+ gchar *key;
+
+ g_free(app_dir); // May do something with app_dir later, but for now, remove it.
+
+ // roadster_settings = g_slice_new(struct r_settings_t);
+
+ g_key_file_load_from_file(settings, cfgfile, G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
+ if(!g_key_file_has_group(settings, group))
+ g_error("Config file %s does not contain [%s].", cfgfile, group);
+
+ key = "Type";
+ if(!g_key_file_has_key(settings, group, key, NULL))
+ g_error("Config file %s missing %s key from %s section.", cfgfile, key, group);
+ else
+ roadster_settings.db_type = g_key_file_get_value(settings, group, key, NULL);
+ key = "Server";
+ if(!g_key_file_has_key(settings, group, key, NULL))
+ g_error("Config file %s missing %s key from %s section.", cfgfile, key, group);
+ else
+ roadster_settings.db_host = g_key_file_get_value(settings, group, key, NULL);
+ key = "Username";
+ if(!g_key_file_has_key(settings, group, key, NULL))
+ g_error("Config file %s missing %s key from %s section.", cfgfile, key, group);
+ else
+ roadster_settings.db_user = g_key_file_get_value(settings, group, key, NULL);
+ key = "Password";
+ if(!g_key_file_has_key(settings, group, key, NULL))
+ g_error("Config file %s missing %s key from %s section.", cfgfile, key, group);
+ else
+ roadster_settings.db_pass = g_key_file_get_value(settings, group, key, NULL);
+ key = "Database";
+ if(!g_key_file_has_key(settings, group, key, NULL))
+ g_error("Config file %s missing %s key from %s section.", cfgfile, key, group);
+ else
+ roadster_settings.db_name = g_key_file_get_value(settings, group, key, NULL);
+
+ g_key_file_free(settings);
+
+// #ifdef _DEBUG
+ g_print("Settings from config file:\n");
+ g_print("DB Type: %s\n", roadster_settings.db_type);
+ g_print("DB Host: %s\n", roadster_settings.db_host);
+ g_print("DB User: %s\n", roadster_settings.db_user);
+ g_print("DB Pass: (It's a secret! Shhh!)\n");
+ g_print("DB Name: %s\n", roadster_settings.db_name);
+ g_print("--------------------------\n");
+// #endif
+
+ return 1;
+#else
+ return 0;
+#endif
+}
+
+
+// This is a very simple function to save config values to .roadster/config.
+// It has been many moons since my last C program, and I do not know GDK/GTK, so
+// do not laugh at my crappy code, help me make it better.
+int settings_save(void)
+{
+#ifdef USE_GNOME_VFS
+ GKeyFile *settings = g_key_file_new();
+ gchar *app_dir = g_strdup_printf("%s/.roadster", g_get_home_dir());
+ gchar *cfgfile = g_strdup_printf("%s/config", app_dir);
+ gchar *group = "Database";
+
+ g_free(app_dir); // May do something with app_dir later, but for now, remove it.
+
+ g_key_file_set_value(settings, group, "Type", roadster_settings.db_type);
+ g_key_file_set_value(settings, group, "Server", roadster_settings.db_host);
+ g_key_file_set_value(settings, group, "Username", roadster_settings.db_user);
+ g_key_file_set_value(settings, group, "Password", roadster_settings.db_pass);
+ g_key_file_set_value(settings, group, "Database", roadster_settings.db_name);
+
+ // TODO: Actually save the settings to a file.
+ g_key_file_free(settings);
+
+ return 1;
+#else
+ return 0;
+#endif
+}
============================================================
--- src/settings.h 24960f78f16312794e8be6c552eb3a09334febfc
+++ src/settings.h 24960f78f16312794e8be6c552eb3a09334febfc
@@ -0,0 +1,41 @@
+/***************************************************************************
+ * settings.h
+ *
+ * Copyright 2006 Ben Carner < kwalker AT kobran DAWT 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 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 Library 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.
+ */
+
+#ifndef _SETTINGS_H
+#define _SETTINGS_H
+
+#include "main.h"
+
+typedef struct {
+ char *db_type; // Type of database to use ("embedded", "none", "mysql", others?)
+ char *db_host; // Host database resides on
+ char *db_user; // Username to access database
+ char *db_pass; // Password to access database
+ char *db_name; // Database name to use
+} r_settings_t;
+
+extern r_settings_t roadster_settings;
+
+int settings_load(void);
+int settings_save(void);
+
+#endif
============================================================
--- src/Makefile.am 380a96816e30486fbc6507548c2ff2e5e752f659
+++ src/Makefile.am f1eac94112e2447cdec24031caa74921a81b32f1
@@ -48,6 +48,7 @@ roadster_SOURCES = \
search_city.c\
search_coordinate.c\
search.c\
+ settings.c\
scenemanager.c\
glyph.c\
road.c\
============================================================
--- src/db.c c9bffcdd13c48a8a50d46a5a09a31dc11cad9c89
+++ src/db.c 582d9e78e02d6539892f721f2461ad6f27797aad
@@ -23,7 +23,7 @@
#include <mysql.h>
-#define HAVE_MYSQL_EMBED
+// #define HAVE_MYSQL_EMBED
#ifdef HAVE_MYSQL_EMBED
# include <mysql_embed.h>
============================================================
--- src/main.c f204d5b6785f0078e5ae80e57839a9bd11c53588
+++ src/main.c 3a1137eb7a3b11cb5ea3c0fb0c2fbca35687ed3a
@@ -40,7 +40,7 @@
#include "locationset.h"
#include "location.h"
#include "search.h"
-#include "search.h"
+#include "settings.h"
static gboolean main_init(void);
static void main_deinit(void);
@@ -51,10 +51,10 @@ int main (int argc, char *argv[])
int main (int argc, char *argv[])
{
- #ifdef ENABLE_NLS
- bindtextdomain(GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
- textdomain(PACKAGE);
- #endif
+#ifdef ENABLE_NLS
+ bindtextdomain(GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
+ textdomain(PACKAGE);
+#endif
gtk_init(&argc, &argv);
@@ -142,6 +142,9 @@ gboolean main_init(void)
g_free(pszApplicationDir);
#endif
+ g_print("initializing settings\n");
+ settings_load();
+
g_print("initializing road\n");
road_init();
@@ -160,7 +163,10 @@ gboolean main_init(void)
db_init();
g_print("connecting to db\n");
- db_connect(NULL, NULL, NULL, ""); // Connect to internal DB
+ if(g_ascii_strcasecmp(roadster_settings.db_type, "embedded") == 0)
+ db_connect(NULL, NULL, NULL, ""); // Connect to internal DB
+ else
+ db_connect(roadster_settings.db_host, roadster_settings.db_user, roadster_settings.db_pass, roadster_settings.db_name);
g_print("creating database tables\n");
db_create_tables();
_______________________________________________
roadster mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/roadster