graveman-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Graveman-cvs] Changes to graveman/current/src/config.c


From: sylvain cresto
Subject: [Graveman-cvs] Changes to graveman/current/src/config.c
Date: Thu, 10 Feb 2005 18:12:37 -0500

Index: graveman/current/src/config.c
diff -u graveman/current/src/config.c:1.11 graveman/current/src/config.c:1.12
--- graveman/current/src/config.c:1.11  Wed Feb  9 00:27:30 2005
+++ graveman/current/src/config.c       Thu Feb 10 23:12:31 2005
@@ -113,23 +113,6 @@
   return NULL;
 }
 
-/* recuperation du nom de fichier ou ecrire */
-gchar *get_config_file_name()
-{
-  const gchar *LHome = g_getenv("HOME");
-  gchar *Ldir = g_strdup_printf("%s/.%s", LHome, PACKAGE);
-  gchar *Lfile = g_strdup_printf("%s/.%s/%s.conf", LHome, PACKAGE, PACKAGE);
-
-  /* tentative de creation du repertoire si il n'existe pas deja */
-  if (g_file_test(Ldir, G_FILE_TEST_EXISTS) == FALSE) {
-    mkdir(Ldir, 00750);
-  }
-  g_free(Ldir);
-
-  return (Lfile);
-}
-
-
 /* callback ecriture fichier de conf */
 void foreachconfig(gpointer Akey, gpointer Avalue, gpointer Adata)
 {
@@ -170,13 +153,14 @@
 }
 
 /* ecriture du fichier de configuration */
-gint write_conf(gchar *AFichier, GError **Aerror)
+gboolean write_conf(GError **Aerror)
 {
   FILE *Lfic;
   gchar *Ltmp;
+  if (!get_config_file(W_OK)) return FALSE;
 
-  if (!(Lfic=fopen(AFichier, "w"))) {
-    Ltmp = g_strdup_printf(_("Cannot create %s: %s"), AFichier, 
g_strerror(errno));
+  if (!(Lfic=fopen(Gfileconf, "w"))) {
+    Ltmp = g_strdup_printf(_("Cannot create %s: %s"), Gfileconf, 
g_strerror(errno));
     g_set_error(Aerror, G_FILE_ERROR, g_file_error_from_errno(errno), Ltmp, 
g_strerror(errno)); 
     g_warning("%s", Ltmp);
     g_free(Ltmp);
@@ -196,7 +180,7 @@
 
   fclose(Lfic);
 
-  return EXIT_SUCCESS;
+  return TRUE;
 }
 
 /* initialisation de la configuration et des valeurs par default */
@@ -220,77 +204,82 @@
 
 /* on regarde si un fichier de configuration existe 
  * dans /usr/share/graveman/graveman.rc ou ~/.graveman/graveman.rc */
-gint is_config_valid()
-{
-  const gchar *LHome = g_getenv("HOME");
-  gchar LUserConf[1024], LGlobalConf[1024];
-
-  bzero(&LUserConf, sizeof(LUserConf));
-  bzero(&LGlobalConf, sizeof(LGlobalConf));
 
-  g_snprintf(LUserConf, sizeof(LUserConf)-1, "%s/.%s/%s.conf", LHome, PACKAGE, 
PACKAGE);
-  g_snprintf(LGlobalConf, sizeof(LGlobalConf)-1, "%s/%s/%s.conf", 
PACKAGE_DATA_DIR, PACKAGE, PACKAGE);
+/* construction de la liste des PATH dans lequel on va chercher la 
configuration */
+GSList *get_config_path()
+{
+  GSList *Lnewlist = NULL;
+  const gchar *Lenv;
+  gchar *Lpath;
+  gchar **Llistpath;
+  gint i;
 
-  if (*LUserConf && access(LUserConf, F_OK + R_OK + W_OK)==0) {
-    strcpy(Gfileconf, LUserConf);
-  } else if (*LGlobalConf && access(LGlobalConf, F_OK + R_OK + W_OK)==0) {
-    strcpy(Gfileconf, LGlobalConf);
+  if ((Lenv=g_getenv("XDG_CONFIG_HOME"))) {
+    Lpath = g_strdup_printf("%s/%s/%s.conf", Lenv, PACKAGE, PACKAGE);
+    Lnewlist = g_slist_append(Lnewlist, Lpath);
   }
 
-#ifdef DEBUG
-  if (*Gfileconf) {
-    g_message("une conf [%s] valide trouve !\n", Gfileconf);
-  } else {
-    g_message("pas de conf valide de trouve!\n");
+  if ((Lenv=g_getenv("HOME"))) {
+    Lpath = g_strdup_printf("%s/.config/%s/%s.conf", Lenv, PACKAGE, PACKAGE);
+    Lnewlist = g_slist_append(Lnewlist, Lpath);
   }
-#endif
 
-  return (*Gfileconf);
-}
+  if ((Lenv = g_getenv("XDG_CONFIG_DIRS"))) {
+    Llistpath = g_strsplit(Lenv, ":", 0);
 
-/* on cherche un programme dans le PATH */
-gchar *trouveUnProg(gchar *Anomprog)
-{
-  struct stat Lficinfo;
-  gchar Ltest[2048], *Lpath, *s, *p;
-  gchar *Lrespath = NULL;
+    for (i=0; Llistpath[i]; i++) {
+      Lpath = g_strdup_printf("%s/%s/%s.conf", Llistpath[i], PACKAGE, PACKAGE);
+      Lnewlist = g_slist_append(Lnewlist, Lpath);
 
-  Lpath = g_strdup(g_getenv("PATH"));
+      g_free(Lpath);
+    }
 
-  p = Lpath;
-  while (((s = strchr(p, ':'))) || p) {
-    if (s) *(s++)=0;
+    g_strfreev(Llistpath);
+  }
 
-    g_snprintf(Ltest, sizeof(Ltest)-1, "%s/%s", p, Anomprog);
-    if (0==stat(Ltest, &Lficinfo) && S_ISREG(Lficinfo.st_mode)) {
-      Lrespath = strdup(Ltest);
+  return Lnewlist;
+}
+
+gboolean get_config_file(gint Lmode)
+{
+  GSList *Llistconfig = get_config_path();
+  GSList *Lcurlist;
+  gboolean Ltrouve = FALSE;
+  gchar Lonlyname[1024];
+  gchar *s;
+  
+  *Gfileconf = 0; 
+  for (Lcurlist = Llistconfig; Lcurlist; Lcurlist = g_slist_next(Lcurlist)) {
+    if (W_OK == Lmode) {
+      g_strlcpy(Lonlyname, (gchar *)Lcurlist->data, sizeof(Lonlyname)-1);      
+      if (!(s=strrchr(Lonlyname, '/'))) continue;
+      *s=0;
+      if (FALSE == sc_mkdir(Lonlyname, 00750)) continue;
+
+      Ltrouve = TRUE;
+    } else if (access((gchar *)Lcurlist->data, F_OK)==0) {
+      Ltrouve = TRUE;
+    }
+    if (Ltrouve) {
+      g_strlcpy(Gfileconf, (gchar *) Lcurlist->data, sizeof(Gfileconf)-1);
       break;
-    } 
-    if (!s) break;
-    p=s;
+    }
   }
 
-  g_free(Lpath);
-
 #ifdef DEBUG
-  if (Lrespath) {
-    _DEB("on a trouve [%s]\n", Lrespath);
+  if (*Gfileconf) {
+    g_message("une conf [%s] valide trouve !\n", Gfileconf);
+  } else {
+    g_message("pas de conf valide de trouve!\n");
   }
 #endif
+    g_message("une conf [%s] valide trouve !\n", Gfileconf);
+  if (!Ltrouve)
+    g_strlcpy(Gfileconf, (gchar *) g_slist_nth_data(Llistconfig, 0), 
sizeof(Gfileconf)-1);
 
-  return Lrespath;
-}
-
-/* le repertoire est-il valide ? sinon renvoi celui par defaut */
-gchar *trouveUnPath(gchar *Anompath, gchar *Adefaultpath)
-{
-  struct stat Lficinfo;
+  g_slist_free(Llistconfig);
 
-  if (0==stat(Anompath, &Lficinfo) && S_ISDIR(Lficinfo.st_mode)) {
-    return strdup(Anompath);
-  }
-
-  return strdup(Adefaultpath);
+  return Ltrouve;
 }
 
 /* sauve une cle dans le hash de configuration */
@@ -391,7 +380,7 @@
 }
 
 /* gestion de la configuration (creation/chargement/sauvegarde...) */
-gint manage_config(GHashTable *Ahash, gshort Aop, GError **Aerror)
+gboolean manage_config(GHashTable *Ahash, gshort Aop, GError **Aerror)
 {
   TProgRequis *Lcurprog;
   gchar *Lprog;
@@ -407,7 +396,7 @@
     /* on recherche les programmes externes */
     for (Lcurprog = Glisteprogrequis; Lcurprog->entry; Lcurprog++) {
       if (Lcurprog->type == CONF_PROG) {
-        Lprog = trouveUnProg(Lcurprog->entry);
+        Lprog = g_find_program_in_path(Lcurprog->entry);
       } else {
         Lprog = g_strdup(Lcurprog->defaultvalue);
       }
@@ -435,14 +424,12 @@
     conf_store_int("height", Lheight);
   }
 
-  if (Aop & WRITE_CONFIG) {
-    gchar *Lconffile = get_config_file_name();
-    gint Lwrstat = write_conf(Lconffile, Aerror);
-    g_free(Lconffile);
-    return Lwrstat;
+  if (Aop & WRITE_CONFIG) {    
+printf("OK ICI MAN!\n");    
+    return write_conf(Aerror);
   }
 
-  return 1;
+  return TRUE;
 }
 
 /* renvoi une valeur du fichier de configuration */




reply via email to

[Prev in Thread] Current Thread [Next in Thread]