changeset 2328:224727e6c73d

if apply_timeout hasn't passed before cleanup, save password during cleanup; minor changes to saveconfig()
author Tomasz Mon <desowin@gmail.com>
date Fri, 18 Jan 2008 18:56:34 +0100
parents 5001f409c454
children 7b38e28464ee
files src/scrobbler/configure.c src/scrobbler/configure.h src/scrobbler/plugin.c
diffstat 3 files changed, 53 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/src/scrobbler/configure.c	Fri Jan 18 00:41:52 2008 +0200
+++ b/src/scrobbler/configure.c	Fri Jan 18 18:56:34 2008 +0100
@@ -44,50 +44,67 @@
         return buf;
 }
 
-static gboolean saveconfig(gpointer data)
+static void saveconfig(void)
 {
-        ConfigDb *cfgfile;
+    ConfigDb *cfgfile;
 
-        const char *uid = gtk_entry_get_text(GTK_ENTRY(entry1));
-        const char *pwd = gtk_entry_get_text(GTK_ENTRY(entry2));
-        const char *ge_uid = gtk_entry_get_text(GTK_ENTRY(ge_entry1));
-        const char *ge_pwd = gtk_entry_get_text(GTK_ENTRY(ge_entry2));
+    const char *uid = gtk_entry_get_text(GTK_ENTRY(entry1));
+    const char *pwd = gtk_entry_get_text(GTK_ENTRY(entry2));
+    const char *ge_uid = gtk_entry_get_text(GTK_ENTRY(ge_entry1));
+    const char *ge_pwd = gtk_entry_get_text(GTK_ENTRY(ge_entry2));
 
-        if ((cfgfile = aud_cfg_db_open()))
-	{
-                md5_state_t md5state;
-                unsigned char md5pword[16], ge_md5pword[16];
-
-                aud_cfg_db_set_string(cfgfile, "audioscrobbler", "username", (char *)uid);
-                aud_cfg_db_set_string(cfgfile, "audioscrobbler", "ge_username", (char *)ge_uid);
+    if ((cfgfile = aud_cfg_db_open())) {
+        md5_state_t md5state;
+        unsigned char md5pword[16], ge_md5pword[16];
 
-                if (pwd != NULL && pwd[0] != '\0' && strlen(pwd))
-		{
-                        md5_init(&md5state);
-                        md5_append(&md5state, (unsigned const char *)pwd, strlen(pwd));
-                        md5_finish(&md5state, md5pword);
-                        aud_cfg_db_set_string(cfgfile, "audioscrobbler", "password",
-                                        hexify((char*)md5pword, sizeof(md5pword)));
-                }
-
-                if (ge_pwd != NULL && ge_pwd[0] != '\0' && strlen(ge_pwd))
-		{
-                        md5_init(&md5state);
-                        md5_append(&md5state, (unsigned const char *)ge_pwd, strlen(ge_pwd));
-                        md5_finish(&md5state, ge_md5pword);
-                        aud_cfg_db_set_string(cfgfile, "audioscrobbler", "ge_password",
-                                        hexify((char*)ge_md5pword, sizeof(ge_md5pword)));
-                }
-
-                aud_cfg_db_close(cfgfile);
+        if (uid != NULL && uid[0] != '\0' && strlen(uid) &&
+            pwd != NULL && pwd[0] != '\0' && strlen(pwd))
+        {
+            aud_cfg_db_set_string(cfgfile, "audioscrobbler", "username", (char *)uid);
+            md5_init(&md5state);
+            md5_append(&md5state, (unsigned const char *)pwd, strlen(pwd));
+            md5_finish(&md5state, md5pword);
+            aud_cfg_db_set_string(cfgfile, "audioscrobbler", "password",
+                                 hexify((char*)md5pword, sizeof(md5pword)));
+        } else if (!uid || uid[0] == '\0') {
+            aud_cfg_db_set_string(cfgfile, "audioscrobbler", "username", "");
+            aud_cfg_db_set_string(cfgfile, "audioscrobbler", "password", "");
         }
 
+        if (ge_uid != NULL && ge_uid[0] != '\0' && strlen(ge_uid) &&
+            ge_pwd != NULL && ge_pwd[0] != '\0' && strlen(ge_pwd))
+        {
+            aud_cfg_db_set_string(cfgfile, "audioscrobbler", "ge_username", (char *)ge_uid);
+            md5_init(&md5state);
+            md5_append(&md5state, (unsigned const char *)ge_pwd, strlen(ge_pwd));
+            md5_finish(&md5state, ge_md5pword);
+            aud_cfg_db_set_string(cfgfile, "audioscrobbler", "ge_password",
+                                  hexify((char*)ge_md5pword, sizeof(ge_md5pword)));
+        } else if (!ge_uid || ge_uid[0] == '\0') {
+            aud_cfg_db_set_string(cfgfile, "audioscrobbler", "ge_username", "");
+            aud_cfg_db_set_string(cfgfile, "audioscrobbler", "ge_password", "");
+        }
+
+        aud_cfg_db_close(cfgfile);
+    }
+}
+
+static gboolean apply_config_changes(gpointer data) {
     apply_timeout = 0;
+    saveconfig();
     start();
     running = TRUE;
     return FALSE;
 }
 
+void configure_cleanup(void) {
+    if (apply_timeout) { /* config has been changed, but wasn't saved yet */
+        g_source_remove(apply_timeout);
+        apply_timeout = 0;
+        saveconfig();
+    }
+}
+
 static void
 entry_changed(GtkWidget *widget, gpointer data)
 {
@@ -99,7 +116,7 @@
     if (apply_timeout)
         g_source_remove(apply_timeout);
 
-    apply_timeout = g_timeout_add_seconds(10, (GSourceFunc) saveconfig, NULL);
+    apply_timeout = g_timeout_add_seconds(10, (GSourceFunc) apply_config_changes, NULL);
 }
 
 /* Generated by glade, sorta. */
--- a/src/scrobbler/configure.h	Fri Jan 18 00:41:52 2008 +0200
+++ b/src/scrobbler/configure.h	Fri Jan 18 18:56:34 2008 +0100
@@ -2,5 +2,6 @@
 #define _CONFIGURE_H_
 
 GtkWidget* create_cfgdlg (void);
+void configure_cleanup(void);
 
 #endif
--- a/src/scrobbler/plugin.c	Fri Jan 18 00:41:52 2008 +0200
+++ b/src/scrobbler/plugin.c	Fri Jan 18 18:56:34 2008 +0100
@@ -23,6 +23,7 @@
 #include <wchar.h>
 #include <sys/time.h>
 
+#include "plugin.h"
 #include "scrobbler.h"
 #include "gerpok.h"
 #include "gtkstuff.h"
@@ -30,7 +31,6 @@
 #include "fmt.h"
 #include "configure.h"
 
-#define XS_CS xmms_scrobbler.xmms_session
 #define XS_SLEEP 1
 #define HS_SLEEP 10
 
@@ -222,6 +222,7 @@
 static void cleanup(void)
 {
     stop();
+    configure_cleanup();
     aud_prefswin_page_destroy(cfgdlg);
 }