14 static gzFile gzf = NULL;
16 #define gtk_msg(type, args...) \
19 msgw = gtk_message_dialog_new(GTK_WINDOW(mainwin), \
20 GTK_DIALOG_DESTROY_WITH_PARENT | \
25 gtk_dialog_run(GTK_DIALOG(msgw)); \
26 gtk_widget_destroy(msgw); \
29 static int gzopen_frontend(const char *path, int oflags, int mode);
30 static int gzclose_frontend(int nothing);
31 static ssize_t gzread_frontend(int nothing, void *buf, size_t s);
32 static ssize_t gzwrite_frontend(int nothing, const void *buf, size_t s);
33 static gchar *get_theme_dir();
34 static gboolean change_dir(const gchar *dir);
35 static gboolean install_theme_to(gchar *theme, gchar *file, gchar *to);
36 static gchar* name_from_file(const gchar *path);
37 static gchar* name_from_dir(const gchar *dir);
38 static gboolean create_theme_archive(gchar *dir, gchar *name, gchar *to);
41 (openfunc_t) gzopen_frontend,
42 (closefunc_t) gzclose_frontend,
43 (readfunc_t) gzread_frontend,
44 (writefunc_t) gzwrite_frontend
47 gchar* theme_install(gchar *path)
52 if (!(dest = get_theme_dir()))
55 if (!(name = name_from_file(path)))
58 if (install_theme_to(name, path, dest)) {
59 gtk_msg(GTK_MESSAGE_INFO, _("\"%s\" was installed to %s"), name, dest);
70 void theme_archive(gchar *path)
75 if (!(name = name_from_dir(path)))
80 file = g_strdup_printf("%s.obt", name);
81 dest = g_build_path(G_DIR_SEPARATOR_S,
82 g_get_current_dir(), file, NULL);
86 if (create_theme_archive(path, name, dest))
87 gtk_msg(GTK_MESSAGE_INFO, _("\"%s\" was successfully created"),
94 static gboolean create_theme_archive(gchar *dir, gchar *name, gchar *to)
99 if (tar_open(&t, to, &funcs, O_WRONLY | O_CREAT, 0666, TAR_GNU) == -1) {
100 gtk_msg(GTK_MESSAGE_ERROR,
101 _("Unable to create the file \"%s\": %s"),
102 to, strerror(errno));
106 r = tar_append_tree(t, dir, name);
110 gtk_msg(GTK_MESSAGE_ERROR,
111 _("Unable to create the theme archive \"%s\": %s"),
112 to, strerror(errno));
118 static gchar *get_theme_dir()
123 dir = g_build_path(G_DIR_SEPARATOR_S, g_get_home_dir(), ".themes", NULL);
124 r = mkdir(dir, 0777);
125 if (r == -1 && errno != EEXIST) {
126 gtk_msg(GTK_MESSAGE_ERROR,
127 _("Unable to create directory \"%s\": %s"),
128 dir, strerror(errno));
136 static gchar* name_from_dir(const gchar *dir)
142 rc = g_build_path(G_DIR_SEPARATOR_S, dir, "openbox-3", "themerc", NULL);
144 r = (stat(rc, &st) == 0 && S_ISREG(st.st_mode));
148 gtk_msg(GTK_MESSAGE_ERROR,
149 _("\"%s\" does not appear to be a valid Openbox theme directory"),
153 return g_path_get_basename(dir);
156 static gchar* name_from_file(const gchar *path)
158 /* decipher the theme name from the file name */
159 gchar *fname = g_path_get_basename(path);
160 gint len = strlen(fname);
164 (fname[len-4] == '.' && fname[len-3] == 'o' &&
165 fname[len-2] == 'b' && fname[len-1] == 't'))
168 name = g_strdup(fname);
173 gtk_msg(GTK_MESSAGE_ERROR,
174 _("Unable to determine the theme's name from \"%s\". File name should be ThemeName.obt."), fname);
179 static gboolean change_dir(const gchar *dir)
181 if (chdir(dir) == -1) {
182 gtk_msg(GTK_MESSAGE_ERROR, _("Unable to move to directory \"%s\": %s"),
183 dir, strerror(errno));
189 static gboolean install_theme_to(gchar *theme, gchar *file, gchar *to)
196 if (tar_open(&t, file, &funcs, O_RDONLY, 0666, TAR_GNU) == -1) {
197 gtk_msg(GTK_MESSAGE_ERROR,
198 _("Unable to open the file \"%s\": %s"),
199 file, strerror(errno));
203 curdir = g_get_current_dir();
204 if (!change_dir(to)) {
210 glob = g_strdup_printf("%s/openbox-3/*", theme);
211 r = tar_extract_glob(t, glob, ".");
220 gtk_msg(GTK_MESSAGE_ERROR,
221 _("Unable to extract the file \"%s\".\nPlease ensure that \"%s\" is writable and that the file is a valid Openbox theme archive"),
222 file, strerror(errno));
227 static int gzopen_frontend(const char *path, int oflags, int mode)
232 if ((oflags & O_ACCMODE) == O_RDONLY)
234 else if ((oflags & O_ACCMODE) == O_WRONLY)
237 g_assert_not_reached();
239 if ((fd = open(path, oflags, mode)) < 0) return -1;
240 if (!(gzf = gzdopen(fd, gzflags))) return -1;
244 static int gzclose_frontend(int nothing)
246 g_return_val_if_fail(gzf != NULL, 0);
250 static ssize_t gzread_frontend(int nothing, void *buf, size_t s)
252 return gzread(gzf, buf, s);
255 static ssize_t gzwrite_frontend(int nothing, const void *buf, size_t s)
257 return gzwrite(gzf, buf, s);