eedf16b5803fe25a135968d9410568b51943b4cd
[eyes-widget] / src / themes.c
1 /* $Id: themes.c 2397 2007-01-17 17:46:35Z nick $
2  * 
3  * Copyright (C) 1999 Dave Camp <dave@davec.dhs.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include <config.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <dirent.h>
25 #include <limits.h>
26 #include <ctype.h>
27
28 #include <gtk/gtk.h>
29 #include "eyes.h"
30
31 gchar *theme_directories[] = {
32     THEMESDIR
33 };
34 #define NUM_THEME_DIRECTORIES 1
35
36 static void
37 parse_theme_file (EyesPluginContent *eyes, FILE *theme_file)
38 {
39     gchar line_buf [512]; /* prolly overkill */
40     gchar *token;
41     fgets (line_buf, 512, theme_file);
42     while (!feof (theme_file)) {
43         token = strtok (line_buf, "=");
44         if (strncmp (token, "wall-thickness",
45                  strlen ("wall-thickness")) == 0)
46         {
47             token += strlen ("wall-thickness");
48             while (!isdigit (*token))
49             {
50                 token++;
51             }
52             sscanf (token, "%d", &eyes->wall_thickness);
53         }
54         else if (strncmp (token, "num-eyes", strlen ("num-eyes")) == 0)
55         {
56             token += strlen ("num-eyes");
57             while (!isdigit (*token))
58             {
59                 token++;
60             }
61             sscanf (token, "%d", &eyes->num_eyes);
62         }
63         else if (strncmp (token, "eye-pixmap", strlen ("eye-pixmap")) == 0)
64         {
65             token = strtok (NULL, "\"");
66             token = strtok (NULL, "\"");
67             if (eyes->eye_filename != NULL)
68                 g_free (eyes->eye_filename);
69             eyes->eye_filename = g_strdup_printf ("%s%s",
70                                     eyes->theme_dir,
71                                     token);
72         }
73         else if (strncmp (token, "pupil-pixmap", strlen ("pupil-pixmap")) == 0)
74         {
75             token = strtok (NULL, "\"");
76             token = strtok (NULL, "\"");
77                         if (eyes->pupil_filename != NULL)
78                                 g_free (eyes->pupil_filename);
79                         eyes->pupil_filename
80                                 = g_strdup_printf ("%s%s",
81                                                    eyes->theme_dir,
82                                                    token);
83         }
84         fgets (line_buf, 512, theme_file);
85     }
86 }
87
88
89
90 void
91 load_theme (EyesPluginContent  *eyes,
92             const gchar *theme_dir)
93 {
94         FILE* theme_file;
95     gchar *file_name;
96
97     eyes->theme_dir = g_strdup_printf ("%s/", theme_dir);
98
99     file_name = g_strdup_printf("%s%s",theme_dir,"/config");
100     theme_file = fopen (file_name, "r");
101     if (theme_file == NULL) {
102         g_error ("Unable to open theme file.");
103     }
104
105     parse_theme_file (eyes, theme_file);
106     fclose (theme_file);
107
108     eyes->theme_name = g_strdup (theme_dir);
109
110     if (eyes->eye_image)
111         g_object_unref (eyes->eye_image);
112
113     eyes->eye_image = gdk_pixbuf_new_from_file (eyes->eye_filename, NULL);
114
115     if (eyes->pupil_image)
116         g_object_unref (eyes->pupil_image);
117
118     eyes->pupil_image = gdk_pixbuf_new_from_file (eyes->pupil_filename, NULL);
119
120         eyes->eye_height = gdk_pixbuf_get_height (eyes->eye_image);
121     eyes->eye_width = gdk_pixbuf_get_width (eyes->eye_image);
122     eyes->pupil_height = gdk_pixbuf_get_height (eyes->pupil_image);
123     eyes->pupil_width = gdk_pixbuf_get_width (eyes->pupil_image);
124
125     g_free (file_name);
126
127 }