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