13 static gzFile gzf = NULL;
15 #define gtk_msg(type, args...) \
18 msgw = gtk_message_dialog_new(GTK_WINDOW(mainwin), \
19 GTK_DIALOG_DESTROY_WITH_PARENT | \
24 gtk_dialog_run(GTK_DIALOG(msgw)); \
25 gtk_widget_destroy(msgw); \
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 *to);
40 (openfunc_t) gzopen_frontend,
41 (closefunc_t) gzclose_frontend,
42 (readfunc_t) gzread_frontend,
43 (writefunc_t) gzwrite_frontend
46 gchar* theme_install(gchar *path)
52 if (!(dest = get_theme_dir()))
55 curdir = g_get_current_dir();
56 if (!change_dir(dest)) {
61 if (!(name = name_from_file(path)))
64 if (install_theme_to(name, path, dest))
65 gtk_msg(GTK_MESSAGE_INFO, _("\"%s\" was installed to %s"), name, dest);
75 void theme_archive(gchar *path)
80 if (!(name = name_from_dir(path)))
85 file = g_strdup_printf("%s.obt", name);
86 dest = g_build_path(G_DIR_SEPARATOR_S, g_get_current_dir(), file);
90 if (create_theme_archive(path, dest))
91 gtk_msg(GTK_MESSAGE_INFO, _("\"%s\" was successfully created"),
98 static gboolean create_theme_archive(gchar *dir, gchar *to)
102 if (tar_open(&t, to, &funcs, 0, O_WRONLY, TAR_GNU) == -1) {
103 gtk_msg(GTK_MESSAGE_ERROR,
104 _("Unable to create the file \"%s\": %s"),
105 to, strerror(errno));
112 static gchar *get_theme_dir()
117 dir = g_build_path(G_DIR_SEPARATOR_S, g_get_home_dir(), ".themes", NULL);
118 r = mkdir(dir, 0777);
119 if (r == -1 && errno != EEXIST) {
120 gtk_msg(GTK_MESSAGE_ERROR,
121 _("Unable to create directory \"%s\": %s"),
122 dir, strerror(errno));
130 static gchar* name_from_dir(const gchar *dir)
136 rc = g_build_path(G_DIR_SEPARATOR_S, dir, "openbox-3", "themerc");
138 r = (stat(rc, &st) == 0 && S_ISREG(st.st_mode));
142 gtk_msg(GTK_MESSAGE_ERROR,
143 _("\"%s\" does not appear to be a valid Openbox theme directory"),
147 return g_path_get_basename(dir);
150 static gchar* name_from_file(const gchar *path)
152 /* decipher the theme name from the file name */
153 gchar *fname = g_path_get_basename(path);
154 gint len = strlen(fname);
158 (fname[len-4] == '.' && fname[len-3] == 'o' &&
159 fname[len-2] == 'b' && fname[len-1] == 't'))
162 name = strdup(fname);
167 gtk_msg(GTK_MESSAGE_ERROR,
168 _("Unable to determine the theme's name from \"%s\". File name should be ThemeName.obt."), fname);
173 static gboolean change_dir(const gchar *dir)
175 if (chdir(dir) == -1) {
176 gtk_msg(GTK_MESSAGE_ERROR, _("Unable to move to directory \"%s\": %s"),
177 dir, strerror(errno));
183 static gboolean install_theme_to(gchar *theme, gchar *file, gchar *to)
189 if (tar_open(&t, file, &funcs, 0, O_RDONLY, TAR_GNU) == -1) {
190 gtk_msg(GTK_MESSAGE_ERROR,
191 _("Unable to open the file \"%s\": %s"),
192 file, strerror(errno));
196 glob = g_strdup_printf("%s/openbox-3/*", theme);
197 r = tar_extract_glob(t, glob, to);
203 gtk_msg(GTK_MESSAGE_ERROR,
204 _("Unable to extract the file \"%s\".\nIt does not appear to be a valid Openbox theme archive (in tar.gz format)."),
205 file, strerror(errno));
210 static int gzopen_frontend(const char *path, int oflags, int mode)
215 if (oflags & O_RDONLY)
217 else if (oflags & O_WRONLY)
220 g_assert_not_reached();
222 if ((fd = open(path, oflags, mode)) < 0) return -1;
223 if (!(gzf = gzdopen(fd, gzflags))) return -1;
227 static int gzclose_frontend(int nothing)
229 g_return_val_if_fail(gzf != NULL, 0);
233 static ssize_t gzread_frontend(int nothing, void *buf, size_t s)
235 return gzread(gzf, buf, s);
238 static ssize_t gzwrite_frontend(int nothing, const void *buf, size_t s)
240 return gzwrite(gzf, buf, s);