]> icculus.org git repositories - dana/obconf.git/blob - src/theme.c
use tar and gzip commands instead of libtar and zlib
[dana/obconf.git] / src / theme.c
1 #include "theme.h"
2 #include "main.h"
3 #include "gettext.h"
4
5 #include <string.h>
6 #include <errno.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <unistd.h>
10
11 #define gtk_msg(type, args...) \
12 {                                                                        \
13     GtkWidget *msgw;                                                     \
14     msgw = gtk_message_dialog_new(GTK_WINDOW(mainwin),                   \
15                                   GTK_DIALOG_DESTROY_WITH_PARENT |       \
16                                   GTK_DIALOG_MODAL,                      \
17                                   type,                                  \
18                                   GTK_BUTTONS_OK,                        \
19                                   args);                                 \
20     gtk_dialog_run(GTK_DIALOG(msgw));                                    \
21     gtk_widget_destroy(msgw);                                            \
22 }
23
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,
29                                  const gchar *to);
30 static gboolean create_theme_archive(const gchar *dir, const gchar *name,
31                                      const gchar *to);
32
33 gchar* theme_install(gchar *path)
34 {
35     gchar *dest;
36     gchar *name;
37
38     if (!(dest = get_theme_dir()))
39         return NULL;
40
41     if (!(name = name_from_file(path)))
42         return NULL;
43
44     if (install_theme_to(name, path, dest)) {
45         gtk_msg(GTK_MESSAGE_INFO, _("\"%s\" was installed to %s"), name, dest);
46     } else {
47         g_free(name);
48         name = NULL;
49     }
50
51     g_free(dest);
52
53     return name;
54 }
55
56 void theme_archive(gchar *path)
57 {
58     gchar *name;
59     gchar *dest;
60
61     if (!(name = name_from_dir(path)))
62         return;
63
64     {
65         gchar *file;
66         file = g_strdup_printf("%s.obt", name);
67         dest = g_build_path(G_DIR_SEPARATOR_S,
68                             g_get_current_dir(), file, NULL);
69         g_free(file);
70     }
71
72     if (create_theme_archive(path, name, dest))
73         gtk_msg(GTK_MESSAGE_INFO, _("\"%s\" was successfully created"),
74                 dest);
75
76     g_free(dest);
77     g_free(name);
78 }
79
80 static gboolean create_theme_archive(const gchar *dir, const gchar *name,
81                                      const gchar *to)
82 {
83     gchar *glob;
84     gchar **argv;
85     gchar *errtxt = NULL;
86     gchar *parentdir;
87     gint exitcode;
88     GError *e = NULL;
89
90     glob = g_strdup_printf("%s/openbox-3/", name);
91
92     parentdir = g_build_path(G_DIR_SEPARATOR_S, dir, "..", NULL);
93
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);
101     argv[6] = NULL;
102     if (g_spawn_sync(parentdir, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
103                      NULL, &errtxt, &exitcode, &e))
104     {
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"),
108                     to, errtxt);
109
110     }
111     else
112         gtk_msg(GTK_MESSAGE_ERROR, _("Unable to run the \"tar\" command: %s"),
113                 e->message);
114
115     g_strfreev(argv);
116     if (e) g_error_free(e);
117     g_free(errtxt);
118     g_free(parentdir);
119     g_free(glob);
120     return exitcode == EXIT_SUCCESS;
121 }
122
123 static gchar *get_theme_dir()
124 {
125     gchar *dir;
126     gint r;
127
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));
134         g_free(dir);
135         dir = NULL;
136     }
137
138     return dir;
139 }
140
141 static gchar* name_from_dir(const gchar *dir)
142 {
143     gchar *rc;
144     struct stat st;
145     gboolean r;
146
147     rc = g_build_path(G_DIR_SEPARATOR_S, dir, "openbox-3", "themerc", NULL);
148
149     r = (stat(rc, &st) == 0 && S_ISREG(st.st_mode));
150     g_free(rc);
151
152     if (!r) {
153         gtk_msg(GTK_MESSAGE_ERROR,
154                 _("\"%s\" does not appear to be a valid Openbox theme directory"),
155                 dir);
156         return NULL;
157     }
158     return g_path_get_basename(dir);
159 }
160
161 static gchar* name_from_file(const gchar *path)
162 {
163     /* decipher the theme name from the file name */
164     gchar *fname = g_path_get_basename(path);
165     gint len = strlen(fname);
166     gchar *name = NULL;
167
168     if (len > 4 &&
169         (fname[len-4] == '.' && fname[len-3] == 'o' &&
170          fname[len-2] == 'b' && fname[len-1] == 't'))
171     {
172         fname[len-4] = '\0';
173         name = g_strdup(fname);
174         fname[len-4] = '.';
175     }
176
177     if (name == NULL)
178         gtk_msg(GTK_MESSAGE_ERROR,
179                 _("Unable to determine the theme's name from \"%s\".  File name should be ThemeName.obt."), fname);
180
181     return name;
182 }
183
184 static gboolean change_dir(const gchar *dir)
185 {
186     if (chdir(dir) == -1) {
187         gtk_msg(GTK_MESSAGE_ERROR, _("Unable to move to directory \"%s\": %s"),
188                 dir, strerror(errno));
189         return FALSE;
190     }
191     return TRUE;
192 }
193
194 static gboolean install_theme_to(const gchar *name, const gchar *file,
195                                  const gchar *to)
196 {
197     gchar *glob;
198     gchar **argv;
199     gchar *errtxt = NULL;
200     gint exitcode;
201     GError *e = NULL;
202
203     glob = g_strdup_printf("%s/openbox-3/", name);
204
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);
212     argv[6] = NULL;
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"),
216                 e->message);
217     g_strfreev(argv);
218     if (e) g_error_free(e);
219
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"),
223                 file, to, errtxt);
224
225     g_free(errtxt);
226     g_free(glob);
227     return exitcode == EXIT_SUCCESS;
228 }