:pserver:cvs@pserver.samba.org:/cvsroot - gnokii - Tue Dec 4 22:45 CET 2001
[gnokii.git] / xgnokii / xgnokii_cfg.c
1 /*
2
3   X G N O K I I
4
5   A Linux/Unix GUI for Nokia mobile phones.
6   Copyright (C) 1999 Pavel Janík ml., Hugh Blemings
7   & Ján Derfiòák <ja@mail.upjs.sk>.
8
9   Released under the terms of the GNU GPL, see file COPYING for more details.
10
11   Last modification: Mon Oct 10 1999
12   Modified by Jan Derfinak
13
14 */
15
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <ctype.h>
20
21 #include "xgnokii_cfg.h"
22 #include "xgnokii.h"
23
24 ConfigEntry config[] = {
25  {"name",      &(xgnokiiConfig.user.name)},
26  {"title",     &(xgnokiiConfig.user.title)},
27  {"company",   &(xgnokiiConfig.user.company)},
28  {"telephone", &(xgnokiiConfig.user.telephone)},
29  {"fax",       &(xgnokiiConfig.user.fax)},
30  {"email",     &(xgnokiiConfig.user.email)},
31  {"address",   &(xgnokiiConfig.user.address)},
32  {"viewer",    &(xgnokiiConfig.helpviewer)},
33  {"mailbox",   &(xgnokiiConfig.mailbox)},
34  {"simlen",    &(xgnokiiConfig.maxSIMLen)},
35  {"phonelen",  &(xgnokiiConfig.maxPhoneLen)},
36  {"",          NULL}
37 };
38
39
40 static void GetDefaultValues ()
41 {
42   gchar *homedir;
43   
44   xgnokiiConfig.user.name = g_strdup ("");
45   xgnokiiConfig.user.title = g_strdup ("");
46   xgnokiiConfig.user.company = g_strdup ("");
47   xgnokiiConfig.user.telephone = g_strdup ("");
48   xgnokiiConfig.user.fax = g_strdup ("");
49   xgnokiiConfig.user.email = g_strdup ("");
50   xgnokiiConfig.user.address = g_strdup ("");
51   xgnokiiConfig.helpviewer = g_strdup ("netscape");
52   if ((homedir = g_get_home_dir ()) == NULL)
53     homedir = "";
54   xgnokiiConfig.mailbox = g_strdup_printf ("%s/Mail/smsbox", homedir);
55   xgnokiiConfig.maxSIMLen = g_strdup ("14");
56   xgnokiiConfig.maxPhoneLen = g_strdup ("16");
57 }
58
59
60 void GUI_ReadXConfig ()
61 {
62   FILE *file;
63   gchar *line;
64   gchar *homedir;
65   gchar *rcfile;
66   gchar *current;
67   register gint len;
68   register gint i;
69
70   GetDefaultValues ();
71
72 #ifdef WIN32
73 /*  homedir = getenv("HOMEDRIVE");
74   g_strconcat(homedir, getenv("HOMEPATH"), NULL); */
75   homedir = g_get_home_dir ();
76   rcfile=g_strconcat(homedir, "\\_xgnokiirc", NULL);
77 #else
78   if ((homedir = g_get_home_dir ()) == NULL)
79   {
80     g_print (_("WARNING: Can't find HOME enviroment variable!\n"));
81     return;
82   }
83
84   if ((rcfile = g_strconcat (homedir, "/.xgnokiirc", NULL)) == NULL)
85   {
86     g_print (_("WARNING: Can't allocate memory for config reading!\n"));
87     return;
88   }
89 #endif
90
91   if ((file = fopen (rcfile, "r")) == NULL)
92   {
93     g_free (rcfile);
94     return;
95   }
96
97   g_free (rcfile);
98
99   if ((line = (char *) g_malloc (255)) == NULL)
100   {
101     g_print (_("WARNING: Can't allocate memory for config reading!\n"));
102     fclose (file);
103     return;
104   }
105
106   while (fgets (line, 255, file) != NULL)
107   {
108     gint v;
109     current = line;
110
111     /* Strip leading, trailing whitespace */
112     while (isspace ((gint) *current))
113       current++;
114
115     while ((strlen (current) > 0) && isspace ((gint) current[strlen (current) - 1]))
116       current[strlen (current) - 1] = '\0';
117
118     /* Ignore blank lines and comments */
119
120     if ((*current == '\n') || (*current == '\0') || (*current == '#'))
121       continue;
122
123     i = 0;
124     while (*config[i].key != '\0')
125     {
126       len = strlen (config[i].key);
127       if (g_strncasecmp (config[i].key, current, len) == 0)
128       {
129         current += len;
130         while (isspace ((int) *current))
131           current++;
132         if (*current == '=')
133         {
134           current++;
135           while(isspace ((int) *current))
136             current++;
137           g_free (*config[i].value);
138           switch (i)
139           {
140             case 3:
141             case 4: 
142               *config[i].value = g_strndup (current, max_phonebook_number_length);
143               break;
144
145             case 7:
146               *config[i].value = g_strndup (current, HTMLVIEWER_LENGTH);
147               break;
148
149             case 8:
150               *config[i].value = g_strndup (current, MAILBOX_LENGTH);
151               break;
152
153             case 9:
154             case 10:
155               v = atoi (current);
156               if ( v > 0 && v < 100 )
157                 *config[i].value = g_strndup (current, 3);
158               break;
159
160             default:
161               *config[i].value = g_strndup (current, MAX_BUSINESS_CARD_LENGTH);
162               break;
163           }
164         }
165       }
166       i++;
167     }
168   }
169
170   fclose (file);
171   g_free (line);
172 }
173
174
175 gint GUI_SaveXConfig ()
176 {
177   FILE *file;
178   gchar *line;
179   gchar *homedir;
180   gchar *rcfile;
181   register gint i;
182
183   if ((homedir = getenv ("HOME")) == NULL)
184   {
185     g_print (_("ERROR: Can't find HOME enviroment variable!\n"));
186     return (1);
187   }
188
189   if ((rcfile = g_strconcat (homedir, "/.xgnokiirc", NULL)) == NULL)
190   {
191     g_print (_("ERROR: Can't allocate memory for config writing!\n"));
192     return (2);
193   }
194
195   if ((file = fopen (rcfile, "w")) == NULL)
196   {
197     g_print (_("ERROR: Can't open file %s for writing!\n"), rcfile);
198     g_free (rcfile);
199     return (3);
200   }
201
202   g_free (rcfile);
203
204   i = 0;
205   while (*config[i].key != '\0')
206   {
207     if ((line = g_strdup_printf ("%s = %s\n", config[i].key, *config[i].value)) == NULL)
208     {
209       g_print (_("ERROR: Can't allocate memory for config writing!\n"));
210       fclose (file);
211       return (2);
212     }
213     if (fputs (line, file) == EOF)
214     {
215       g_print (_("ERROR: Can't write config file!\n"));
216       g_free (line);
217       fclose (file);
218       return (4);
219     }
220     g_free (line);
221     i++;
222   }
223
224   fclose (file);
225   return (0);
226 }