11 #define gtk_msg(type, args...) \
14 msgw = gtk_message_dialog_new(GTK_WINDOW(mainwin), \
15 GTK_DIALOG_DESTROY_WITH_PARENT | \
20 gtk_dialog_run(GTK_DIALOG(msgw)); \
21 gtk_widget_destroy(msgw); \
24 static gchar *get_theme_dir();
25 static gboolean change_dir(const gchar *dir);
26 static gchar* name_from_file(const gchar *path);
27 static gchar* name_from_dir(const gchar *dir);
28 static gboolean install_theme_to(const gchar *name, const gchar *file,
30 static gboolean create_theme_archive(const gchar *dir, const gchar *name,
33 gchar* archive_install(const gchar *path)
38 if (!(dest = get_theme_dir()))
41 if (!(name = name_from_file(path)))
44 if (install_theme_to(name, path, dest)) {
45 gtk_msg(GTK_MESSAGE_INFO, _("\"%s\" was installed to %s"), name, dest);
56 void archive_create(const gchar *path)
61 if (!(name = name_from_dir(path)))
66 file = g_strdup_printf("%s.obt", name);
67 dest = g_build_path(G_DIR_SEPARATOR_S,
68 g_get_current_dir(), file, NULL);
72 if (create_theme_archive(path, name, dest))
73 gtk_msg(GTK_MESSAGE_INFO, _("\"%s\" was successfully created"),
80 static gboolean create_theme_archive(const gchar *dir, const gchar *name,
90 glob = g_strdup_printf("%s/openbox-3/", name);
92 parentdir = g_build_path(G_DIR_SEPARATOR_S, dir, "..", NULL);
94 argv = g_new(gchar*, 7);
95 argv[0] = g_strdup("tar");
96 argv[1] = g_strdup("-c");
97 argv[2] = g_strdup("-z");
98 argv[3] = g_strdup("-f");
99 argv[4] = g_strdup(to);
100 argv[5] = g_strdup(glob);
102 if (g_spawn_sync(parentdir, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
103 NULL, &errtxt, &exitcode, &e))
105 if (exitcode != EXIT_SUCCESS)
106 gtk_msg(GTK_MESSAGE_ERROR,
107 _("Unable to create the theme archive \"%s\".\nThe following errors were reported:\n%s"),
112 gtk_msg(GTK_MESSAGE_ERROR, _("Unable to run the \"tar\" command: %s"),
116 if (e) g_error_free(e);
120 return exitcode == EXIT_SUCCESS;
123 static gchar *get_theme_dir()
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));
141 static gchar* name_from_dir(const gchar *dir)
147 rc = g_build_path(G_DIR_SEPARATOR_S, dir, "openbox-3", "themerc", NULL);
149 r = (stat(rc, &st) == 0 && S_ISREG(st.st_mode));
153 gtk_msg(GTK_MESSAGE_ERROR,
154 _("\"%s\" does not appear to be a valid Openbox theme directory"),
158 return g_path_get_basename(dir);
161 static gchar* name_from_file(const gchar *path)
163 /* decipher the theme name from the file name */
164 gchar *fname = g_path_get_basename(path);
165 gint len = strlen(fname);
169 (fname[len-4] == '.' && fname[len-3] == 'o' &&
170 fname[len-2] == 'b' && fname[len-1] == 't'))
173 name = g_strdup(fname);
178 gtk_msg(GTK_MESSAGE_ERROR,
179 _("Unable to determine the theme's name from \"%s\". File name should be ThemeName.obt."), fname);
184 static gboolean change_dir(const gchar *dir)
186 if (chdir(dir) == -1) {
187 gtk_msg(GTK_MESSAGE_ERROR, _("Unable to move to directory \"%s\": %s"),
188 dir, strerror(errno));
194 static gboolean install_theme_to(const gchar *name, const gchar *file,
199 gchar *errtxt = NULL;
203 glob = g_strdup_printf("%s/openbox-3/", name);
205 argv = g_new(gchar*, 7);
206 argv[0] = g_strdup("tar");
207 argv[1] = g_strdup("-x");
208 argv[2] = g_strdup("-z");
209 argv[3] = g_strdup("-f");
210 argv[4] = g_strdup(file);
211 argv[5] = g_strdup(glob);
213 if (!g_spawn_sync(to, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
214 NULL, &errtxt, &exitcode, &e))
215 gtk_msg(GTK_MESSAGE_ERROR, _("Unable to run the \"tar\" command: %s"),
218 if (e) g_error_free(e);
220 if (exitcode != EXIT_SUCCESS)
221 gtk_msg(GTK_MESSAGE_ERROR,
222 _("Unable to extract the file \"%s\".\nPlease ensure that \"%s\" is writable and that the file is a valid Openbox theme archive.\nThe following errors were reported:\n%s"),
227 return exitcode == EXIT_SUCCESS;