12 static gzFile gzf = NULL;
14 #define gtk_msg(type, args...) \
17 msgw = gtk_message_dialog_new(GTK_WINDOW(mainwin), \
18 GTK_DIALOG_DESTROY_WITH_PARENT | \
23 gtk_dialog_run(GTK_DIALOG(msgw)); \
24 gtk_widget_destroy(msgw); \
27 static int gzopen_frontend(const char *path, int oflags, int mode);
28 static int gzclose_frontend(int nothing);
29 static ssize_t gzread_frontend(int nothing, void *buf, size_t s);
30 static ssize_t gzwrite_frontend(int nothing, const void *buf, size_t s);
31 static gchar *get_theme_dir();
32 static gboolean change_dir(const gchar *dir);
33 static gboolean install_theme_to(gchar *theme, gchar *file, gchar *to);
34 static gchar* name_from_file(const gchar *path);
37 (openfunc_t) gzopen_frontend,
38 (closefunc_t) gzclose_frontend,
39 (readfunc_t) gzread_frontend,
40 (writefunc_t) gzwrite_frontend
43 gchar* theme_install(gchar *path)
49 if (!(dest = get_theme_dir()))
52 curdir = g_get_current_dir();
53 if (!change_dir(dest)) {
58 if (!(name = name_from_file(path)))
61 if (install_theme_to(name, path, dest))
62 gtk_msg(GTK_MESSAGE_INFO, _("\"%s\" was installed to %s"), name, dest);
72 static gchar *get_theme_dir()
77 dir = g_build_path(G_DIR_SEPARATOR_S, g_get_home_dir(), ".themes", NULL);
79 if (r == -1 && errno != EEXIST) {
80 gtk_msg(GTK_MESSAGE_ERROR,
81 _("Unable to create directory \"%s\": %s"),
82 dir, strerror(errno));
90 static gchar* name_from_file(const gchar *path)
92 /* decipher the theme name from the file name */
93 gchar *fname = g_path_get_basename(path);
94 gint len = strlen(fname);
98 (fname[len-4] == '.' && fname[len-3] == 'o' &&
99 fname[len-2] == 'b' && fname[len-1] == 't'))
102 name = strdup(fname);
107 gtk_msg(GTK_MESSAGE_ERROR,
108 _("Unable to determine the theme's name from \"%s\". File name should be ThemeName.obt."), fname);
113 static gboolean change_dir(const gchar *dir)
115 if (chdir(dir) == -1) {
116 gtk_msg(GTK_MESSAGE_ERROR, _("Unable to move to directory \"%s\": %s"),
117 dir, strerror(errno));
123 static gboolean install_theme_to(gchar *theme, gchar *file, gchar *to)
129 if (tar_open(&t, file, &funcs, 0, O_RDONLY, TAR_GNU) == -1) {
130 gtk_msg(GTK_MESSAGE_ERROR,
131 _("Unable to open the file \"%s\": %s"),
132 file, strerror(errno));
136 glob = g_strdup_printf("%s/openbox-3/*", theme);
137 r = tar_extract_glob(t, glob, to);
143 gtk_msg(GTK_MESSAGE_ERROR,
144 _("Unable to extract the file \"%s\".\nIt does not appear to be a valid Openbox theme archive (in tar.gz format)."),
145 file, strerror(errno));
150 static int gzopen_frontend(const char *path, int oflags, int mode)
154 if ((fd = open(path, oflags, mode)) < 0) return -1;
155 if (!(gzf = gzdopen(fd, "rb"))) return -1;
159 static int gzclose_frontend(int nothing)
161 g_return_val_if_fail(gzf != NULL, 0);
165 static ssize_t gzread_frontend(int nothing, void *buf, size_t s)
167 return gzread(gzf, buf, s);
170 static ssize_t gzwrite_frontend(int nothing, const void *buf, size_t s)
172 return gzwrite(gzf, buf, s);