yay for macros. code cleanup extraordinaire
[dana/obconf.git] / src / install.c
1 #include "install.h"
2 #include "main.h"
3 #include "gettext.h"
4
5 #include <errno.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 #include <zlib.h>
10 #include <libtar.h>
11
12 static gzFile gzf = NULL;
13
14 #define gtk_msg(type, args...) \
15 {                                                                        \
16     GtkWidget *msgw;                                                     \
17     msgw = gtk_message_dialog_new(GTK_WINDOW(mainwin),                   \
18                                   GTK_DIALOG_DESTROY_WITH_PARENT |       \
19                                   GTK_DIALOG_MODAL,                      \
20                                   type,                                  \
21                                   GTK_BUTTONS_OK,                        \
22                                   args);                                 \
23     gtk_dialog_run(GTK_DIALOG(msgw));                                    \
24     gtk_widget_destroy(msgw);                                            \
25 }
26
27 static int gzopen_frontend(const char *path, int oflags, int mode)
28 {
29     int fd;
30
31     if ((fd = open(path, oflags, mode)) < 0) return -1;
32     if (!(gzf = gzdopen(fd, "rb"))) return -1;
33     return 1;
34 }
35
36 static int gzclose_frontend(int nothing)
37 {
38     g_return_val_if_fail(gzf != NULL, 0);
39     return gzclose(gzf);
40 }
41
42 static ssize_t gzread_frontend(int nothing, void *buf, size_t s)
43 {
44     return gzread(gzf, buf, s);
45 }
46
47 static ssize_t gzwrite_frontend(int nothing, const void *buf, size_t s)
48 {
49     return gzwrite(gzf, buf, s);
50 }
51
52 tartype_t funcs = {
53     (openfunc_t) gzopen_frontend,
54     (closefunc_t) gzclose_frontend,
55     (readfunc_t) gzread_frontend,
56     (writefunc_t) gzwrite_frontend
57 };
58
59 gboolean install_theme(char *path, char *theme)
60 {
61     TAR *t;
62     gchar *dest;
63     gint r;
64     gchar *glob;
65     GtkWidget *w;
66
67     dest = g_build_path(G_DIR_SEPARATOR_S, g_get_home_dir(), ".themes", NULL);
68     r = mkdir(dest, 0777);
69     if (r == -1 && errno != EEXIST) {
70         gtk_msg(GTK_MESSAGE_ERROR,
71                 _("Unable to create directory \"%s\": %s"),
72                 dest, strerror(errno));
73         return FALSE;
74     }
75     if (chdir(dest) == -1) {
76         gtk_msg(GTK_MESSAGE_ERROR,
77                 _("Unable to move to directory \"%s\": %s"),
78                 dest, strerror(errno));
79         return FALSE;
80     }
81
82     if (tar_open(&t, path, &funcs, 0, O_RDONLY, TAR_GNU) == -1) {
83         gtk_msg(GTK_MESSAGE_ERROR,
84                 _("Unable to open the file \"%s\": %s"),
85                 path, strerror(errno));
86         return FALSE;
87     }
88
89     glob = g_strdup_printf("%s/openbox-3/*", theme);
90     r = tar_extract_glob(t, glob, dest);
91     g_free(glob);
92     tar_close(t);
93
94     if (r != 0) {
95         gtk_msg(GTK_MESSAGE_ERROR,
96                 _("Unable to extract the file \"%s\".\nIt does not appear to be a valid Openbox theme archive (in tar.gz format)."),
97                 path, strerror(errno));
98
99         g_free(dest);
100         return FALSE;
101     }
102
103     gtk_msg(GTK_MESSAGE_INFO, _("%s was installed to %s"), theme, dest);
104     gtk_dialog_run(GTK_DIALOG(w));
105     gtk_widget_destroy(w);
106
107     g_free(dest);
108
109     return TRUE;
110 }