]> icculus.org git repositories - dana/obconf.git/blob - src/theme.c
bug fix
[dana/obconf.git] / src / theme.c
1 #include "theme.h"
2 #include "main.h"
3 #include "gettext.h"
4
5 #include <errno.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 #include <unistd.h>
10 #include <zlib.h>
11 #include <libtar.h>
12
13 static gzFile gzf = NULL;
14
15 #define gtk_msg(type, args...) \
16 {                                                                        \
17     GtkWidget *msgw;                                                     \
18     msgw = gtk_message_dialog_new(GTK_WINDOW(mainwin),                   \
19                                   GTK_DIALOG_DESTROY_WITH_PARENT |       \
20                                   GTK_DIALOG_MODAL,                      \
21                                   type,                                  \
22                                   GTK_BUTTONS_OK,                        \
23                                   args);                                 \
24     gtk_dialog_run(GTK_DIALOG(msgw));                                    \
25     gtk_widget_destroy(msgw);                                            \
26 }
27
28 static int gzopen_frontend(const char *path, int oflags, int mode);
29 static int gzclose_frontend(int nothing);
30 static ssize_t gzread_frontend(int nothing, void *buf, size_t s);
31 static ssize_t gzwrite_frontend(int nothing, const void *buf, size_t s);
32 static gchar *get_theme_dir();
33 static gboolean change_dir(const gchar *dir);
34 static gboolean install_theme_to(gchar *theme, gchar *file, gchar *to);
35 static gchar* name_from_file(const gchar *path);
36 static gchar* name_from_dir(const gchar *dir);
37 static gboolean create_theme_archive(gchar *dir, gchar *name, gchar *to);
38
39 tartype_t funcs = {
40     (openfunc_t) gzopen_frontend,
41     (closefunc_t) gzclose_frontend,
42     (readfunc_t) gzread_frontend,
43     (writefunc_t) gzwrite_frontend
44 };
45
46 gchar* theme_install(gchar *path)
47 {
48     gchar *dest;
49     gchar *curdir;
50     gchar *name;
51
52     if (!(dest = get_theme_dir()))
53         return NULL;
54
55     curdir = g_get_current_dir();
56     if (!change_dir(dest)) {
57         g_free(curdir);
58         return NULL;
59     }
60
61     if (!(name = name_from_file(path)))
62         return NULL;
63
64     if (install_theme_to(name, path, dest))
65         gtk_msg(GTK_MESSAGE_INFO, _("\"%s\" was installed to %s"), name, dest);
66
67     g_free(dest);
68
69     change_dir(curdir);
70     g_free(curdir);
71
72     return name;
73 }
74
75 void theme_archive(gchar *path)
76 {
77     gchar *name;
78     gchar *dest;
79
80     if (!(name = name_from_dir(path)))
81         return;
82
83     {
84         gchar *file;
85         file = g_strdup_printf("%s.obt", name);
86         dest = g_build_path(G_DIR_SEPARATOR_S,
87                             g_get_current_dir(), file, NULL);
88         g_free(file);
89     }
90
91     if (create_theme_archive(path, name, dest))
92         gtk_msg(GTK_MESSAGE_INFO, _("\"%s\" was successfully created"),
93                 dest);
94
95     g_free(dest);
96     g_free(name);
97 }
98
99 static gboolean create_theme_archive(gchar *dir, gchar *name, gchar *to)
100 {
101     TAR *t;
102     gint r;
103
104     if (tar_open(&t, to, &funcs, O_WRONLY | O_CREAT, 0666, TAR_GNU) == -1) {
105         gtk_msg(GTK_MESSAGE_ERROR,
106                 _("Unable to create the file \"%s\": %s"),
107                 to, strerror(errno));
108         return;
109     }
110
111     r = tar_append_tree(t, dir, name);
112     tar_close(t);
113
114     if (r != 0) {
115         gtk_msg(GTK_MESSAGE_ERROR,
116                 _("Unable to create the theme archive \"%s\": %s"),
117                 to, strerror(errno));
118     }
119
120     return r == 0;
121 }
122
123 static gchar *get_theme_dir()
124 {
125     gchar *dir;
126     gint r;
127
128     dir = g_build_path(G_DIR_SEPARATOR_S, g_get_home_dir(), ".themes", NULL);
129     r = mkdir(dir, 0777);
130     if (r == -1 && errno != EEXIST) {
131         gtk_msg(GTK_MESSAGE_ERROR,
132                 _("Unable to create directory \"%s\": %s"),
133                 dir, strerror(errno));
134         g_free(dir);
135         dir = NULL;
136     }
137
138     return dir;
139 }
140
141 static gchar* name_from_dir(const gchar *dir)
142 {
143     gchar *rc;
144     struct stat st;
145     gboolean r;
146
147     rc = g_build_path(G_DIR_SEPARATOR_S, dir, "openbox-3", "themerc", NULL);
148
149     r = (stat(rc, &st) == 0 && S_ISREG(st.st_mode));
150     g_free(rc);
151
152     if (!r) {
153         gtk_msg(GTK_MESSAGE_ERROR,
154                 _("\"%s\" does not appear to be a valid Openbox theme directory"),
155                 dir);
156         return NULL;
157     }
158     return g_path_get_basename(dir);
159 }
160
161 static gchar* name_from_file(const gchar *path)
162 {
163     /* decipher the theme name from the file name */
164     gchar *fname = g_path_get_basename(path);
165     gint len = strlen(fname);
166     gchar *name = NULL;
167
168     if (len > 4 &&
169         (fname[len-4] == '.' && fname[len-3] == 'o' &&
170          fname[len-2] == 'b' && fname[len-1] == 't'))
171     {
172         fname[len-4] = '\0';
173         name = strdup(fname);
174         fname[len-4] = '.';
175     }
176
177     if (name == NULL)
178         gtk_msg(GTK_MESSAGE_ERROR,
179                 _("Unable to determine the theme's name from \"%s\".  File name should be ThemeName.obt."), fname);
180
181     return name;
182 }
183
184 static gboolean change_dir(const gchar *dir)
185 {
186     if (chdir(dir) == -1) {
187         gtk_msg(GTK_MESSAGE_ERROR, _("Unable to move to directory \"%s\": %s"),
188                 dir, strerror(errno));
189         return FALSE;
190     }
191     return TRUE;
192 }
193
194 static gboolean install_theme_to(gchar *theme, gchar *file, gchar *to)
195 {
196     TAR *t;
197     gchar *glob;
198     gint r;
199
200     if (tar_open(&t, file, &funcs, O_RDONLY, 0666, TAR_GNU) == -1) {
201         gtk_msg(GTK_MESSAGE_ERROR,
202                 _("Unable to open the file \"%s\": %s"),
203                 file, strerror(errno));
204         return FALSE;
205     }
206
207     glob = g_strdup_printf("%s/openbox-3/*", theme);
208     r = tar_extract_glob(t, glob, to);
209     g_free(glob);
210
211     tar_close(t);
212
213     if (r != 0)
214         gtk_msg(GTK_MESSAGE_ERROR,
215                 _("Unable to extract the file \"%s\".\nPlease ensure that \"%s\" is writable and that the file is a valid Openbox theme archive"),
216                 file, strerror(errno));
217
218     return r == 0;
219 }
220
221 static int gzopen_frontend(const char *path, int oflags, int mode)
222 {
223     int fd;
224     const char *gzflags;
225
226     if ((oflags & O_ACCMODE) == O_RDONLY)
227         gzflags = "rb";
228     else if ((oflags & O_ACCMODE) == O_WRONLY)
229         gzflags = "wb";
230     else
231         g_assert_not_reached();
232
233     if ((fd = open(path, oflags, mode)) < 0) return -1;
234     if (!(gzf = gzdopen(fd, gzflags))) return -1;
235     return 1;
236 }
237
238 static int gzclose_frontend(int nothing)
239 {
240     g_return_val_if_fail(gzf != NULL, 0);
241     return gzclose(gzf);
242 }
243
244 static ssize_t gzread_frontend(int nothing, void *buf, size_t s)
245 {
246     return gzread(gzf, buf, s);
247 }
248
249 static ssize_t gzwrite_frontend(int nothing, const void *buf, size_t s)
250 {
251     return gzwrite(gzf, buf, s);
252 }
253