/[graveman]/graveman/current/src/config.c
ViewVC logotype

Diff of /graveman/current/src/config.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.11 by scresto, Wed Feb 9 00:27:30 2005 UTC revision 1.12 by scresto, Thu Feb 10 23:12:31 2005 UTC
# Line 113  TProgRequis *getentrydef(gchar *Aentry) Line 113  TProgRequis *getentrydef(gchar *Aentry)
113    return NULL;    return NULL;
114  }  }
115    
 /* 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);  
 }  
   
   
116  /* callback ecriture fichier de conf */  /* callback ecriture fichier de conf */
117  void foreachconfig(gpointer Akey, gpointer Avalue, gpointer Adata)  void foreachconfig(gpointer Akey, gpointer Avalue, gpointer Adata)
118  {  {
# Line 170  void foreachdrives(gpointer Adrive, gpoi Line 153  void foreachdrives(gpointer Adrive, gpoi
153  }  }
154    
155  /* ecriture du fichier de configuration */  /* ecriture du fichier de configuration */
156  gint write_conf(gchar *AFichier, GError **Aerror)  gboolean write_conf(GError **Aerror)
157  {  {
158    FILE *Lfic;    FILE *Lfic;
159    gchar *Ltmp;    gchar *Ltmp;
160      if (!get_config_file(W_OK)) return FALSE;
161    
162    if (!(Lfic=fopen(AFichier, "w"))) {    if (!(Lfic=fopen(Gfileconf, "w"))) {
163      Ltmp = g_strdup_printf(_("Cannot create %s: %s"), AFichier, g_strerror(errno));      Ltmp = g_strdup_printf(_("Cannot create %s: %s"), Gfileconf, g_strerror(errno));
164      g_set_error(Aerror, G_FILE_ERROR, g_file_error_from_errno(errno), Ltmp, g_strerror(errno));      g_set_error(Aerror, G_FILE_ERROR, g_file_error_from_errno(errno), Ltmp, g_strerror(errno));
165      g_warning("%s", Ltmp);      g_warning("%s", Ltmp);
166      g_free(Ltmp);      g_free(Ltmp);
# Line 196  gint write_conf(gchar *AFichier, GError Line 180  gint write_conf(gchar *AFichier, GError
180    
181    fclose(Lfic);    fclose(Lfic);
182    
183    return EXIT_SUCCESS;    return TRUE;
184  }  }
185    
186  /* initialisation de la configuration et des valeurs par default */  /* initialisation de la configuration et des valeurs par default */
# Line 220  void config_init() Line 204  void config_init()
204    
205  /* on regarde si un fichier de configuration existe  /* on regarde si un fichier de configuration existe
206   * dans /usr/share/graveman/graveman.rc ou ~/.graveman/graveman.rc */   * 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));  
207    
208    g_snprintf(LUserConf, sizeof(LUserConf)-1, "%s/.%s/%s.conf", LHome, PACKAGE, PACKAGE);  /* construction de la liste des PATH dans lequel on va chercher la configuration */
209    g_snprintf(LGlobalConf, sizeof(LGlobalConf)-1, "%s/%s/%s.conf", PACKAGE_DATA_DIR, PACKAGE, PACKAGE);  GSList *get_config_path()
210    {
211      GSList *Lnewlist = NULL;
212      const gchar *Lenv;
213      gchar *Lpath;
214      gchar **Llistpath;
215      gint i;
216    
217    if (*LUserConf && access(LUserConf, F_OK + R_OK + W_OK)==0) {    if ((Lenv=g_getenv("XDG_CONFIG_HOME"))) {
218      strcpy(Gfileconf, LUserConf);      Lpath = g_strdup_printf("%s/%s/%s.conf", Lenv, PACKAGE, PACKAGE);
219    } else if (*LGlobalConf && access(LGlobalConf, F_OK + R_OK + W_OK)==0) {      Lnewlist = g_slist_append(Lnewlist, Lpath);
     strcpy(Gfileconf, LGlobalConf);  
220    }    }
221    
222  #ifdef DEBUG    if ((Lenv=g_getenv("HOME"))) {
223    if (*Gfileconf) {      Lpath = g_strdup_printf("%s/.config/%s/%s.conf", Lenv, PACKAGE, PACKAGE);
224      g_message("une conf [%s] valide trouve !\n", Gfileconf);      Lnewlist = g_slist_append(Lnewlist, Lpath);
   } else {  
     g_message("pas de conf valide de trouve!\n");  
225    }    }
 #endif  
226    
227    return (*Gfileconf);    if ((Lenv = g_getenv("XDG_CONFIG_DIRS"))) {
228  }      Llistpath = g_strsplit(Lenv, ":", 0);
229    
230  /* on cherche un programme dans le PATH */      for (i=0; Llistpath[i]; i++) {
231  gchar *trouveUnProg(gchar *Anomprog)        Lpath = g_strdup_printf("%s/%s/%s.conf", Llistpath[i], PACKAGE, PACKAGE);
232  {        Lnewlist = g_slist_append(Lnewlist, Lpath);
   struct stat Lficinfo;  
   gchar Ltest[2048], *Lpath, *s, *p;  
   gchar *Lrespath = NULL;  
233    
234    Lpath = g_strdup(g_getenv("PATH"));        g_free(Lpath);
235        }
236    
237    p = Lpath;      g_strfreev(Llistpath);
238    while (((s = strchr(p, ':'))) || p) {    }
     if (s) *(s++)=0;  
239    
240      g_snprintf(Ltest, sizeof(Ltest)-1, "%s/%s", p, Anomprog);    return Lnewlist;
241      if (0==stat(Ltest, &Lficinfo) && S_ISREG(Lficinfo.st_mode)) {  }
242        Lrespath = strdup(Ltest);  
243    gboolean get_config_file(gint Lmode)
244    {
245      GSList *Llistconfig = get_config_path();
246      GSList *Lcurlist;
247      gboolean Ltrouve = FALSE;
248      gchar Lonlyname[1024];
249      gchar *s;
250      
251      *Gfileconf = 0;
252      for (Lcurlist = Llistconfig; Lcurlist; Lcurlist = g_slist_next(Lcurlist)) {
253        if (W_OK == Lmode) {
254          g_strlcpy(Lonlyname, (gchar *)Lcurlist->data, sizeof(Lonlyname)-1);      
255          if (!(s=strrchr(Lonlyname, '/'))) continue;
256          *s=0;
257          if (FALSE == sc_mkdir(Lonlyname, 00750)) continue;
258    
259          Ltrouve = TRUE;
260        } else if (access((gchar *)Lcurlist->data, F_OK)==0) {
261          Ltrouve = TRUE;
262        }
263        if (Ltrouve) {
264          g_strlcpy(Gfileconf, (gchar *) Lcurlist->data, sizeof(Gfileconf)-1);
265        break;        break;
266      }      }
     if (!s) break;  
     p=s;  
267    }    }
268    
   g_free(Lpath);  
   
269  #ifdef DEBUG  #ifdef DEBUG
270    if (Lrespath) {    if (*Gfileconf) {
271      _DEB("on a trouve [%s]\n", Lrespath);      g_message("une conf [%s] valide trouve !\n", Gfileconf);
272      } else {
273        g_message("pas de conf valide de trouve!\n");
274    }    }
275  #endif  #endif
276        g_message("une conf [%s] valide trouve !\n", Gfileconf);
277      if (!Ltrouve)
278        g_strlcpy(Gfileconf, (gchar *) g_slist_nth_data(Llistconfig, 0), sizeof(Gfileconf)-1);
279    
280    return Lrespath;    g_slist_free(Llistconfig);
 }  
   
 /* le repertoire est-il valide ? sinon renvoi celui par defaut */  
 gchar *trouveUnPath(gchar *Anompath, gchar *Adefaultpath)  
 {  
   struct stat Lficinfo;  
281    
282    if (0==stat(Anompath, &Lficinfo) && S_ISDIR(Lficinfo.st_mode)) {    return Ltrouve;
     return strdup(Anompath);  
   }  
   
   return strdup(Adefaultpath);  
283  }  }
284    
285  /* sauve une cle dans le hash de configuration */  /* sauve une cle dans le hash de configuration */
# Line 391  void manage_saveonexit_value(GHashTable Line 380  void manage_saveonexit_value(GHashTable
380  }  }
381    
382  /* gestion de la configuration (creation/chargement/sauvegarde...) */  /* gestion de la configuration (creation/chargement/sauvegarde...) */
383  gint manage_config(GHashTable *Ahash, gshort Aop, GError **Aerror)  gboolean manage_config(GHashTable *Ahash, gshort Aop, GError **Aerror)
384  {  {
385    TProgRequis *Lcurprog;    TProgRequis *Lcurprog;
386    gchar *Lprog;    gchar *Lprog;
# Line 407  gint manage_config(GHashTable *Ahash, gs Line 396  gint manage_config(GHashTable *Ahash, gs
396      /* on recherche les programmes externes */      /* on recherche les programmes externes */
397      for (Lcurprog = Glisteprogrequis; Lcurprog->entry; Lcurprog++) {      for (Lcurprog = Glisteprogrequis; Lcurprog->entry; Lcurprog++) {
398        if (Lcurprog->type == CONF_PROG) {        if (Lcurprog->type == CONF_PROG) {
399          Lprog = trouveUnProg(Lcurprog->entry);          Lprog = g_find_program_in_path(Lcurprog->entry);
400        } else {        } else {
401          Lprog = g_strdup(Lcurprog->defaultvalue);          Lprog = g_strdup(Lcurprog->defaultvalue);
402        }        }
# Line 435  gint manage_config(GHashTable *Ahash, gs Line 424  gint manage_config(GHashTable *Ahash, gs
424      conf_store_int("height", Lheight);      conf_store_int("height", Lheight);
425    }    }
426    
427    if (Aop & WRITE_CONFIG) {    if (Aop & WRITE_CONFIG) {    
428      gchar *Lconffile = get_config_file_name();  printf("OK ICI MAN!\n");    
429      gint Lwrstat = write_conf(Lconffile, Aerror);      return write_conf(Aerror);
     g_free(Lconffile);  
     return Lwrstat;  
430    }    }
431    
432    return 1;    return TRUE;
433  }  }
434    
435  /* renvoi une valeur du fichier de configuration */  /* renvoi une valeur du fichier de configuration */

Legend:
Removed from v.1.11  
changed lines
  Added in v.1.12

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26