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_dir(const gchar *dir);
27 static gchar* install_theme_to(const gchar *file, const gchar *to);
28 static gboolean create_theme_archive(const gchar *dir, const gchar *name,
31 gchar* archive_install(const gchar *path)
36 if (!(dest = get_theme_dir()))
39 if ((name = install_theme_to(path, dest))) {
40 gtk_msg(GTK_MESSAGE_INFO, _("\"%s\" was installed to %s"), name, dest);
48 void archive_create(const gchar *path)
53 if (!(name = name_from_dir(path)))
58 file = g_strdup_printf("%s.obt", name);
59 dest = g_build_path(G_DIR_SEPARATOR_S,
60 g_get_current_dir(), file, NULL);
64 if (create_theme_archive(path, name, dest))
65 gtk_msg(GTK_MESSAGE_INFO, _("\"%s\" was successfully created"),
72 static gboolean create_theme_archive(const gchar *dir, const gchar *name,
82 glob = g_strdup_printf("%s/openbox-3/", name);
84 parentdir = g_build_path(G_DIR_SEPARATOR_S, dir, "..", NULL);
86 argv = g_new(gchar*, 9);
87 argv[0] = g_strdup("tar");
88 argv[1] = g_strdup("-c");
89 argv[2] = g_strdup("-z");
90 argv[3] = g_strdup("-f");
91 argv[4] = g_strdup(to);
92 argv[5] = g_strdup("-C");
93 argv[6] = g_strdup(parentdir);
94 argv[7] = g_strdup(glob);
96 if (g_spawn_sync(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
97 NULL, &errtxt, &exitcode, &e))
99 if (exitcode != EXIT_SUCCESS)
100 gtk_msg(GTK_MESSAGE_ERROR,
101 _("Unable to create the theme archive \"%s\".\nThe following errors were reported:\n%s"),
106 gtk_msg(GTK_MESSAGE_ERROR, _("Unable to run the \"tar\" command: %s"),
110 if (e) g_error_free(e);
114 return exitcode == EXIT_SUCCESS;
117 static gchar *get_theme_dir()
122 dir = g_build_path(G_DIR_SEPARATOR_S, g_get_home_dir(), ".themes", NULL);
123 r = mkdir(dir, 0777);
124 if (r == -1 && errno != EEXIST) {
125 gtk_msg(GTK_MESSAGE_ERROR,
126 _("Unable to create directory \"%s\": %s"),
127 dir, strerror(errno));
135 static gchar* name_from_dir(const gchar *dir)
141 rc = g_build_path(G_DIR_SEPARATOR_S, dir, "openbox-3", "themerc", NULL);
143 r = (stat(rc, &st) == 0 && S_ISREG(st.st_mode));
147 gtk_msg(GTK_MESSAGE_ERROR,
148 _("\"%s\" does not appear to be a valid Openbox theme directory"),
152 return g_path_get_basename(dir);
155 static gboolean change_dir(const gchar *dir)
157 if (chdir(dir) == -1) {
158 gtk_msg(GTK_MESSAGE_ERROR, _("Unable to move to directory \"%s\": %s"),
159 dir, strerror(errno));
165 static gchar* install_theme_to(const gchar *file, const gchar *to)
169 gchar *errtxt = NULL, *outtxt = NULL;
174 glob = g_strdup_printf("*/openbox-3/", name);
176 argv = g_new(gchar*, 11);
177 argv[0] = g_strdup("tar");
178 argv[1] = g_strdup("-x");
179 argv[2] = g_strdup("-v");
180 argv[3] = g_strdup("-z");
181 argv[4] = g_strdup("--wildcards");
182 argv[5] = g_strdup("-f");
183 argv[6] = g_strdup(file);
184 argv[7] = g_strdup("-C");
185 argv[8] = g_strdup(to);
186 argv[9] = g_strdup(glob);
188 if (!g_spawn_sync(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
189 &outtxt, &errtxt, &exitcode, &e))
190 gtk_msg(GTK_MESSAGE_ERROR, _("Unable to run the \"tar\" command: %s"),
193 if (e) g_error_free(e);
195 if (exitcode != EXIT_SUCCESS)
196 gtk_msg(GTK_MESSAGE_ERROR,
197 _("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"),
200 if (exitcode == EXIT_SUCCESS) {
201 gchar **lines = g_strsplit(outtxt, "\n", 0);
203 for (it = lines; *it; ++it) {
205 gboolean found = FALSE;
207 while (*l && !found) {
208 if (!strcmp(l, "/openbox-3/")) {
209 *l = '\0'; /* cut the string */
216 name = g_strdup(*it);