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*, 9);
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("-C");
101 argv[6] = g_strdup(parentdir);
102 argv[7] = g_strdup(glob);
104 if (g_spawn_sync(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
105 NULL, &errtxt, &exitcode, &e))
107 if (exitcode != EXIT_SUCCESS)
108 gtk_msg(GTK_MESSAGE_ERROR,
109 _("Unable to create the theme archive \"%s\".\nThe following errors were reported:\n%s"),
114 gtk_msg(GTK_MESSAGE_ERROR, _("Unable to run the \"tar\" command: %s"),
118 if (e) g_error_free(e);
122 return exitcode == EXIT_SUCCESS;
125 static gchar *get_theme_dir()
130 dir = g_build_path(G_DIR_SEPARATOR_S, g_get_home_dir(), ".themes", NULL);
131 r = mkdir(dir, 0777);
132 if (r == -1 && errno != EEXIST) {
133 gtk_msg(GTK_MESSAGE_ERROR,
134 _("Unable to create directory \"%s\": %s"),
135 dir, strerror(errno));
143 static gchar* name_from_dir(const gchar *dir)
149 rc = g_build_path(G_DIR_SEPARATOR_S, dir, "openbox-3", "themerc", NULL);
151 r = (stat(rc, &st) == 0 && S_ISREG(st.st_mode));
155 gtk_msg(GTK_MESSAGE_ERROR,
156 _("\"%s\" does not appear to be a valid Openbox theme directory"),
160 return g_path_get_basename(dir);
163 static gchar* name_from_file(const gchar *path)
165 /* decipher the theme name from the file name */
166 gchar *fname = g_path_get_basename(path);
167 gint len = strlen(fname);
171 (fname[len-4] == '.' && fname[len-3] == 'o' &&
172 fname[len-2] == 'b' && fname[len-1] == 't'))
175 name = g_strdup(fname);
180 gtk_msg(GTK_MESSAGE_ERROR,
181 _("Unable to determine the theme's name from \"%s\". File name should be ThemeName.obt."), fname);
186 static gboolean change_dir(const gchar *dir)
188 if (chdir(dir) == -1) {
189 gtk_msg(GTK_MESSAGE_ERROR, _("Unable to move to directory \"%s\": %s"),
190 dir, strerror(errno));
196 static gboolean install_theme_to(const gchar *name, const gchar *file,
201 gchar *errtxt = NULL;
205 glob = g_strdup_printf("%s/openbox-3/", name);
207 argv = g_new(gchar*, 9);
208 argv[0] = g_strdup("tar");
209 argv[1] = g_strdup("-x");
210 argv[2] = g_strdup("-z");
211 argv[3] = g_strdup("-f");
212 argv[4] = g_strdup(file);
213 argv[5] = g_strdup("-C");
214 argv[6] = g_strdup(to);
215 argv[7] = g_strdup(glob);
217 if (!g_spawn_sync(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
218 NULL, &errtxt, &exitcode, &e))
219 gtk_msg(GTK_MESSAGE_ERROR, _("Unable to run the \"tar\" command: %s"),
222 if (e) g_error_free(e);
224 if (exitcode != EXIT_SUCCESS)
225 gtk_msg(GTK_MESSAGE_ERROR,
226 _("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"),
231 return exitcode == EXIT_SUCCESS;