File: configuration.c

package info (click to toggle)
xfce4-sensors-plugin 0.10.99.5~svn-r4998-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 2,032 kB
  • ctags: 307
  • sloc: sh: 9,815; ansic: 3,174; makefile: 100
file content (381 lines) | stat: -rw-r--r-- 12,382 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/* File configuration.c
 *
 *  Copyright 2004-2007 Fabian Nowak (timystery@arcor.de)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

/* Note for programmers and editors: Try to use 4 spaces instead of Tab! */


#include "configuration.h"

#include <stdlib.h>


gint
get_Id_from_address (gint chipnumber, gint addr, t_sensors *sensors)
{
    gint feature;
    t_chip *chip;
    t_chipfeature *chipfeature;

    TRACE ("enters get_Id_from_address");

    chip = (t_chip *) g_ptr_array_index (sensors->chips, chipnumber);

    for (feature=0; feature<chip->num_features; feature++) {
        chipfeature = g_ptr_array_index(chip->chip_features, feature);
        if (addr == chipfeature->address) {
            TRACE ("leaves get_Id_from_address with %d", feature);
            return feature;
        }
    }

    TRACE ("leaves get_Id_from_address with -1");

    return (gint) -1;
}


/* Write the configuration at exit */
void
sensors_write_config (XfcePanelPlugin *plugin, t_sensors *sensors)
{
    XfceRc *rc;
    char *file, *tmp, rc_chip[8], feature[20];
    int i, j;
    t_chip *chip;
    t_chipfeature *chipfeature;

    TRACE ("enters sensors_write_config");

    if (!(file = xfce_panel_plugin_save_location (plugin, TRUE))) {
        TRACE ("leaves sensors_write_config: No file location specified.");
        return;
    }

    unlink (file);

    rc = xfce_rc_simple_open (file, FALSE);
    g_free (file);

    if (!rc) {
        TRACE ("leaves sensors_write_config: No rc file opened");
        return;
    }

    xfce_rc_set_group (rc, "General");

    xfce_rc_write_bool_entry (rc, "Show_Title", sensors->show_title);

    xfce_rc_write_bool_entry (rc, "Show_Labels", sensors->show_labels);

    xfce_rc_write_bool_entry (rc, "Use_Bar_UI", sensors->display_values_graphically);

    xfce_rc_write_bool_entry (rc, "Show_Colored_Bars", sensors->show_colored_bars);

    xfce_rc_write_int_entry (rc, "Scale", sensors->scale);

    xfce_rc_write_entry (rc, "Font_Size", sensors->font_size);

    xfce_rc_write_int_entry (rc, "Font_Size_Numerical",
                                sensors->font_size_numerical);

    xfce_rc_write_int_entry (rc, "Update_Interval", sensors->sensors_refresh_time);

    xfce_rc_write_bool_entry (rc, "Exec_Command", sensors->exec_command);

    xfce_rc_write_bool_entry (rc, "Show_Units", sensors->show_units);

    xfce_rc_write_bool_entry(rc, "Small_Spacings", sensors->show_smallspacings);

    xfce_rc_write_entry (rc, "Command_Name", sensors->command_name);

    xfce_rc_write_int_entry (rc, "Number_Chips", sensors->num_sensorchips);

    xfce_rc_write_bool_entry (rc, "Suppress_Hddtemp_Message", sensors->suppressmessage);


    for (i=0; i<sensors->num_sensorchips; i++) {

        chip = (t_chip *) g_ptr_array_index(sensors->chips, i);
        g_assert (chip!=NULL);

        g_snprintf (rc_chip, 8, "Chip%d", i);

        xfce_rc_set_group (rc, rc_chip);

        xfce_rc_write_entry (rc, "Name", chip->sensorId);

        /* number of sensors is still limited */
        xfce_rc_write_int_entry (rc, "Number", i);

        for (j=0; j<chip->num_features; j++) {
            chipfeature = g_ptr_array_index(chip->chip_features, j);
            g_assert (chipfeature!=NULL);

            if (chipfeature->show == TRUE) {

               g_snprintf (feature, 20, "%s_Feature%d", rc_chip, j);

               xfce_rc_set_group (rc, feature);

               xfce_rc_write_int_entry (rc, "Id", get_Id_from_address(i, j, sensors));

               /* only use this if no hddtemp sensor */
               /* or do only use this , if it is an lmsensors device. whatever. */
               if ( strcmp(chip->sensorId, _("Hard disks")) != 0 ) /* chip->name? */
                    xfce_rc_write_int_entry (rc, "Address", j);
                else
                    xfce_rc_write_entry (rc, "DeviceName", chipfeature->devicename);

               xfce_rc_write_entry (rc, "Name", chipfeature->name);

               xfce_rc_write_entry (rc, "Color", chipfeature->color);

               xfce_rc_write_bool_entry (rc, "Show", chipfeature->show);

               tmp = g_strdup_printf("%.2f", chipfeature->min_value);
               xfce_rc_write_entry (rc, "Min", tmp);
               g_free (tmp);

               tmp = g_strdup_printf("%.2f", chipfeature->max_value);
               xfce_rc_write_entry (rc, "Max", tmp);
               g_free (tmp);
            } /* end if */

        } /* end for j */

    } /* end for i */

    xfce_rc_close (rc);

    TRACE ("leaves sensors_write_config");
}


void
sensors_read_general_config (XfceRc *rc, t_sensors *sensors)
{
    const char *value;
    gint num_chips;

    TRACE ("enters sensors_read_general_config");

    if (xfce_rc_has_group (rc, "General") ) {

        xfce_rc_set_group (rc, "General");

        sensors->show_title = xfce_rc_read_bool_entry (rc, "Show_Title", TRUE);

        sensors->show_labels = xfce_rc_read_bool_entry (rc, "Show_Labels", TRUE);

        sensors->display_values_graphically = xfce_rc_read_bool_entry (rc, "Use_Bar_UI", FALSE);

        sensors->show_colored_bars = xfce_rc_read_bool_entry (rc, "Show_Colored_Bars", FALSE);

        sensors->scale = xfce_rc_read_int_entry (rc, "Scale", 0);

        if ((value = xfce_rc_read_entry (rc, "Font_Size", NULL)) && *value) {
            sensors->font_size = g_strdup(value);
        }

        sensors->font_size_numerical = xfce_rc_read_int_entry (rc,
                                                 "Font_Size_Numerical", 2);

        sensors->sensors_refresh_time = xfce_rc_read_int_entry (rc, "Update_Interval",
                                                  60);

        sensors->exec_command = xfce_rc_read_bool_entry (rc, "Exec_Command", TRUE);

        sensors->show_units= xfce_rc_read_bool_entry (rc, "Show_Units", TRUE);

        sensors->show_smallspacings= xfce_rc_read_bool_entry (rc, "Small_Spacings", FALSE);

        if ((value = xfce_rc_read_entry (rc, "Command_Name", NULL)) && *value) {
            sensors->command_name = g_strdup (value);
        }

        if (!sensors->suppressmessage)
            sensors->suppressmessage = xfce_rc_read_bool_entry (rc, "Suppress_Hddtemp_Message", FALSE);

        num_chips = xfce_rc_read_int_entry (rc, "Number_Chips", 0);
        /* or could use 1 or the always existent dummy entry */
    }

    TRACE ("leaves sensors_read_general_config");
}

void
sensors_read_preliminary_config (XfcePanelPlugin *plugin, t_sensors *sensors)
{
    char *file;
    XfceRc *rc;

    TRACE ("enters sensors_read_preliminary_config");

    if (!(file = xfce_panel_plugin_lookup_rc_file (plugin)))
        return;

    rc = xfce_rc_simple_open (file, TRUE);
    g_free (file);

    if (!rc)
        return;

    if (xfce_rc_has_group (rc, "General") ) {
        xfce_rc_set_group (rc, "General");
        sensors->suppressmessage = xfce_rc_read_bool_entry (rc, "Suppress_Hddtemp_Message", FALSE);
    }

    TRACE ("leaves sensors_read_preliminary_config");
}

/* Read the configuration file at init */
void
sensors_read_config (XfcePanelPlugin *plugin, t_sensors *sensors)
{
    const char *value;
    char *file;
    XfceRc *rc;
    int i, j;
    char rc_chip[8], feature[20];
    gchar* sensorName=NULL;
    gint num_sensorchip, id, address;
    t_chip *chip;
    t_chipfeature *chipfeature;

    TRACE ("enters sensors_read_config");

    if (!(file = xfce_panel_plugin_lookup_rc_file (plugin)))
        return;

    rc = xfce_rc_simple_open (file, TRUE);
    g_free (file);

    if (!rc)
        return;

    sensors_read_general_config (rc, sensors);

    for (i = 0; i<sensors->num_sensorchips; i++) {
        chip = (t_chip *) g_ptr_array_index (sensors->chips, i);
        if (chip==NULL)
            break;

        g_snprintf (rc_chip, 8, "Chip%d", i);

        if (xfce_rc_has_group (rc, rc_chip)) {

            xfce_rc_set_group (rc, rc_chip);

            num_sensorchip=0;

            if ((value = xfce_rc_read_entry (rc, "Name", NULL)) && *value) {
                sensorName = g_strdup (value);
            }

            num_sensorchip = (gint) xfce_rc_read_int_entry (rc, "Number", 0);

            /* assert that file does not contain more information
              than does exist on system */
              /* ??? At least, it works. */
            g_return_if_fail (num_sensorchip < sensors->num_sensorchips);

            /* now featuring enhanced string comparison */
            g_assert (chip!=NULL);
            if ( strcmp(chip->sensorId, sensorName)==0 ) {

                for (j=0; j<chip->num_features; j++) {
                    chipfeature = (t_chipfeature *) g_ptr_array_index (
                                                        chip->chip_features,
                                                        j);
                    g_assert (chipfeature!=NULL);

                    g_snprintf (feature, 20, "%s_Feature%d", rc_chip, j);

                    if (xfce_rc_has_group (rc, feature)) {
                        xfce_rc_set_group (rc, feature);

                        id=0; address=0;

                        id = (gint) xfce_rc_read_int_entry (rc, "Id", 0);

                        if ( strcmp(chip->sensorId, _("Hard disks")) != 0 )
                            address = (gint) xfce_rc_read_int_entry (rc, "Address", 0);
                        else

                         /* FIXME: compare strings, or also have hddtmep and acpi store numeric values */

                        /* assert correctly saved file */
                        if (strcmp(chip->sensorId, _("Hard disks")) != 0) { /* chip->name? */
                            chipfeature = g_ptr_array_index(chip->chip_features, id);
                            /* FIXME: it might be necessary to use sensors->addresses here */
                            /* g_return_if_fail
                                (chipfeature->address == address); */
                            if (chipfeature->address != address)
                                continue;
                        }
                        else if ((value = xfce_rc_read_entry (rc, "DeviceName", NULL))
                            && *value) {
                            chipfeature->devicename = g_strdup(value);
                            /* g_free (value); */
                        }

                        if ((value = xfce_rc_read_entry (rc, "Name", NULL))
                                && *value) {
                            chipfeature->name = g_strdup (value);
                            /* g_free (value); */
                        }

                        if ((value = xfce_rc_read_entry (rc, "Color", NULL))
                                && *value) {
                            chipfeature->color = g_strdup (value);
                            /* g_free (value); */
                        }

                        chipfeature->show =
                            xfce_rc_read_bool_entry (rc, "Show", FALSE);

                        if ((value = xfce_rc_read_entry (rc, "Min", NULL))
                                && *value)
                            chipfeature->min_value = atof (value);

                        if ((value = xfce_rc_read_entry (rc, "Max", NULL))
                                && *value)
                            chipfeature->max_value = atof (value);


                    } /* end if */

                } /* end for */

            } /* end if */

            g_free (sensorName);

        } /* end if */

    } /* end for */

    xfce_rc_close (rc);

    if (!sensors->exec_command) {
        g_signal_handler_block ( G_OBJECT(sensors->eventbox), sensors->doubleclick_id );
    }

    TRACE ("leaves sensors_read_config");
}