]> icculus.org git repositories - dana/obconf.git/blob - src/install.c
missing a var
[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 static int gzclose_frontend(int nothing);
29 static ssize_t gzread_frontend(int nothing, void *buf, size_t s);
30 static ssize_t gzwrite_frontend(int nothing, const void *buf, size_t s);
31 static gchar *get_theme_dir();
32
33 tartype_t funcs = {
34     (openfunc_t) gzopen_frontend,
35     (closefunc_t) gzclose_frontend,
36     (readfunc_t) gzread_frontend,
37     (writefunc_t) gzwrite_frontend
38 };
39
40 static gchar *get_theme_dir()
41 {
42     gchar *dir;
43     gint r;
44
45     dir = g_build_path(G_DIR_SEPARATOR_S, g_get_home_dir(), ".themes", NULL);
46     r = mkdir(dir, 0777);
47     if (r == -1 && errno != EEXIST) {
48         gtk_msg(GTK_MESSAGE_ERROR,
49                 _("Unable to create directory \"%s\": %s"),
50                 dir, strerror(errno));
51         g_free(dir);
52         dir = NULL;
53     }
54
55     return dir;
56 }
57
58 gboolean install_theme(char *path, char *theme)
59 {
60     TAR *t;
61     gchar *dest;
62     gint r;
63     gchar *glob;
64     gchar *curdir;
65
66     if (!(dest = get_theme_dir()))
67         return FALSE;
68
69     curdir = g_get_current_dir();
70     if (chdir(dest) == -1) {
71         gtk_msg(GTK_MESSAGE_ERROR,
72                 _("Unable to move to directory \"%s\": %s"),
73                 dest, strerror(errno));
74         g_free(curdir);
75         return FALSE;
76     }
77
78     if (tar_open(&t, path, &funcs, 0, O_RDONLY, TAR_GNU) == -1) {
79         gtk_msg(GTK_MESSAGE_ERROR,
80                 _("Unable to open the file \"%s\": %s"),
81                 path, strerror(errno));
82         chdir(curdir);
83         g_free(curdir);
84         return FALSE;
85     }
86
87     glob = g_strdup_printf("%s/openbox-3/*", theme);
88     r = tar_extract_glob(t, glob, dest);
89     g_free(glob);
90     tar_close(t);
91
92     if (r != 0) {
93         gtk_msg(GTK_MESSAGE_ERROR,
94                 _("Unable to extract the file \"%s\".\nIt does not appear to be a valid Openbox theme archive (in tar.gz format)."),
95                 path, strerror(errno));
96
97         g_free(dest);
98         chdir(curdir);
99         g_free(curdir);
100         return FALSE;
101     }
102
103     gtk_msg(GTK_MESSAGE_INFO, _("%s was installed to %s"), theme, dest);
104
105     g_free(dest);
106
107     chdir(curdir);
108     g_free(curdir);
109
110     return TRUE;
111 }
112
113 static int gzopen_frontend(const char *path, int oflags, int mode)
114 {
115     int fd;
116
117     if ((fd = open(path, oflags, mode)) < 0) return -1;
118     if (!(gzf = gzdopen(fd, "rb"))) return -1;
119     return 1;
120 }
121
122 static int gzclose_frontend(int nothing)
123 {
124     g_return_val_if_fail(gzf != NULL, 0);
125     return gzclose(gzf);
126 }
127
128 static ssize_t gzread_frontend(int nothing, void *buf, size_t s)
129 {
130     return gzread(gzf, buf, s);
131 }
132
133 static ssize_t gzwrite_frontend(int nothing, const void *buf, size_t s)
134 {
135     return gzwrite(gzf, buf, s);
136 }
137