From fa98e2f1e8a51ad1c0c5d77eed460ff98a6a9818 Mon Sep 17 00:00:00 2001 From: Dana Jansens Date: Wed, 30 May 2007 00:26:05 +0000 Subject: [PATCH] halfway to creating theme archives --- src/handlers.c | 3 ++- src/theme.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++- src/theme.h | 2 ++ 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/src/handlers.c b/src/handlers.c index cdd5906..f27148c 100644 --- a/src/handlers.c +++ b/src/handlers.c @@ -1347,6 +1347,7 @@ void on_theme_archive_clicked(GtkButton *w, gpointer data) gtk_widget_destroy(d); if (path != NULL) { - g_print("ok %s\n", path); + theme_archive(path); + g_free(path); } } diff --git a/src/theme.c b/src/theme.c index 57cdc47..f173993 100644 --- a/src/theme.c +++ b/src/theme.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -32,6 +33,8 @@ static gchar *get_theme_dir(); static gboolean change_dir(const gchar *dir); static gboolean install_theme_to(gchar *theme, gchar *file, gchar *to); static gchar* name_from_file(const gchar *path); +static gchar* name_from_dir(const gchar *dir); +static gboolean create_theme_archive(gchar *dir, gchar *to); tartype_t funcs = { (openfunc_t) gzopen_frontend, @@ -69,6 +72,43 @@ gchar* theme_install(gchar *path) return name; } +void theme_archive(gchar *path) +{ + gchar *name; + gchar *dest; + + if (!(name = name_from_dir(path))) + return; + + { + gchar *file; + file = g_strdup_printf("%s.obt", name); + dest = g_build_path(G_DIR_SEPARATOR_S, g_get_current_dir(), file); + g_free(file); + } + + if (create_theme_archive(path, dest)) + gtk_msg(GTK_MESSAGE_INFO, _("\"%s\" was successfully created"), + dest); + + g_free(dest); + g_free(name); +} + +static gboolean create_theme_archive(gchar *dir, gchar *to) +{ + TAR *t; + + if (tar_open(&t, to, &funcs, 0, O_WRONLY, TAR_GNU) == -1) { + gtk_msg(GTK_MESSAGE_ERROR, + _("Unable to create the file \"%s\": %s"), + to, strerror(errno)); + return; + } + + tar_close(t); +} + static gchar *get_theme_dir() { gchar *dir; @@ -87,6 +127,26 @@ static gchar *get_theme_dir() return dir; } +static gchar* name_from_dir(const gchar *dir) +{ + gchar *rc; + struct stat st; + gboolean r; + + rc = g_build_path(G_DIR_SEPARATOR_S, dir, "openbox-3", "themerc"); + + r = (stat(rc, &st) == 0 && S_ISREG(st.st_mode)); + g_free(rc); + + if (!r) { + gtk_msg(GTK_MESSAGE_ERROR, + _("\"%s\" does not appear to be a valid Openbox theme directory"), + dir); + return NULL; + } + return g_path_get_basename(dir); +} + static gchar* name_from_file(const gchar *path) { /* decipher the theme name from the file name */ @@ -150,9 +210,17 @@ static gboolean install_theme_to(gchar *theme, gchar *file, gchar *to) static int gzopen_frontend(const char *path, int oflags, int mode) { int fd; + const char *gzflags; + + if (oflags & O_RDONLY) + gzflags = "rb"; + else if (oflags & O_WRONLY) + gzflags = "wb"; + else + g_assert_not_reached(); if ((fd = open(path, oflags, mode)) < 0) return -1; - if (!(gzf = gzdopen(fd, "rb"))) return -1; + if (!(gzf = gzdopen(fd, gzflags))) return -1; return 1; } diff --git a/src/theme.h b/src/theme.h index d678774..2d45b9c 100644 --- a/src/theme.h +++ b/src/theme.h @@ -5,4 +5,6 @@ gchar* theme_install(gchar *path); +void theme_archive(gchar *path); + #endif -- 2.39.2