Finish theme installation support.
authorHong Jen Yee (PCMan) <pcman.tw@gmail.com>
Tue, 3 Aug 2010 11:20:51 +0000 (19:20 +0800)
committerHong Jen Yee (PCMan) <pcman.tw@gmail.com>
Tue, 3 Aug 2010 11:23:24 +0000 (19:23 +0800)
configure.ac
src/icon-theme.c
src/icon-theme.h
src/utils.c

index 69eb510..5948a6f 100644 (file)
@@ -25,9 +25,9 @@ AC_HEADER_STDC
 # intltool
 IT_PROG_INTLTOOL([0.40.0])
 
-PKG_CHECK_MODULES(XLIB, "x11")
-AC_SUBST(XLIB_CFLAGS)
-AC_SUBST(XLIB_LIBS)
+PKG_CHECK_MODULES(XLIB, "x11")
+AC_SUBST(XLIB_CFLAGS)
+AC_SUBST(XLIB_LIBS)
 
 pkg_modules="gtk+-2.0 >= 2.12.0"
 PKG_CHECK_MODULES(GTK, [$pkg_modules])
@@ -45,10 +45,10 @@ AC_SUBST(GTK_LIBS)
 # AC_SUBST(MENU_CACHE_LIBS)
 
 AC_ARG_ENABLE(debug,
-       [AC_HELP_STRING([--enable-debug],
-               [build libfm with debug support @<:@default=no@:>@])],
-       [enable_debug="${enableval}"],
-       [enable_debug=no]
+    [AC_HELP_STRING([--enable-debug],
+        [build libfm with debug support @<:@default=no@:>@])],
+    [enable_debug="${enableval}"],
+    [enable_debug=no]
 )
 if test "$enable_debug" = "yes"; then
     # turn on debug and disable optimization
index 5dfd7a2..2faa7f8 100644 (file)
@@ -43,7 +43,7 @@ static void icon_theme_free(IconTheme* theme)
     g_slice_free(IconTheme, theme);
 }
 
-static void load_icon_themes_from_dir(const char* theme_dir, GKeyFile* kf)
+void load_icon_themes_from_dir(const char* theme_dir, GKeyFile* kf)
 {
     GDir* dir = g_dir_open(theme_dir, 0, NULL);
     if(dir)
@@ -87,7 +87,8 @@ static void load_icon_themes_from_dir(const char* theme_dir, GKeyFile* kf)
                 g_free(cursor_subdir);
 
                 if(theme->has_icon || theme->has_cursor)
-                    app.icon_themes = g_slist_prepend(app.icon_themes, theme);
+                    app.icon_themes = g_slist_insert_sorted(app.icon_themes, theme,
+                                            (GCompareFunc)icon_theme_cmp_disp_name);
                 else /* this dir contains no icon or cursor theme, drop it. */
                     icon_theme_free(theme);
             }
@@ -118,8 +119,6 @@ static void load_icon_themes()
         g_free(dir_path);
     }
     g_key_file_free(kf);
-
-    app.icon_themes = g_slist_sort(app.icon_themes, (GCompareFunc)icon_theme_cmp_disp_name);
 }
 
 static void icon_sizes_init(GtkBuilder* b)
index 81841ed..b454b7b 100644 (file)
@@ -36,6 +36,10 @@ typedef struct
 }IconTheme;
 
 void icon_theme_init(GtkBuilder* b);
+void load_icon_themes_from_dir(const char* theme_dir, GKeyFile* kf);
+
+gint icon_theme_cmp_name(IconTheme* t, const char* name);
+gint icon_theme_cmp_disp_name(IconTheme* t1, IconTheme* t2);
 
 G_END_DECLS
 
index e754acf..89f2868 100644 (file)
@@ -28,6 +28,8 @@
 #include <stdlib.h>
 #include <glib/gstdio.h>
 
+#include "icon-theme.h"
+
 static void on_pid_exit(GPid pid, gint status, gpointer user_data)
 {
     GtkDialog* dlg = GTK_DIALOG(user_data);
@@ -81,6 +83,30 @@ gboolean show_progress_for_pid(GtkWindow* parent, const char* title, const char*
     return (res == GTK_RESPONSE_OK);
 }
 
+static void insert_theme_to_models(IconTheme* theme)
+{
+    int icon_theme_pos = 0;
+    int cursor_theme_pos = 0;
+    GSList* l;
+    GtkTreeIter it;
+
+    for(l = app.icon_themes; l; l=l->next)
+    {
+        IconTheme* theme2 = (IconTheme*)l->data;
+        if(l->data == theme)
+            break;
+        if(theme2->has_icon)
+            ++icon_theme_pos;
+        if(theme2->has_cursor)
+            ++cursor_theme_pos;
+    }
+    if(theme->has_icon)
+        gtk_list_store_insert_with_values(app.icon_theme_store, &it, icon_theme_pos, 0, theme->disp_name, 1, theme, -1);
+
+    if(theme->has_cursor)
+        gtk_list_store_insert_with_values(app.cursor_theme_store, &it, cursor_theme_pos, 0, theme->disp_name, 1, theme, -1);
+}
+
 static gboolean install_icon_theme_package(const char* package_path)
 {
     GPid pid = -1;
@@ -96,6 +122,9 @@ static gboolean install_icon_theme_package(const char* package_path)
         NULL
     };
 
+    if(g_mkdir_with_parents(user_icons_dir, 0700) == -1)
+        return FALSE;
+
     if(!mkdtemp(tmp_dir))
         return FALSE;
 
@@ -117,24 +146,38 @@ static gboolean install_icon_theme_package(const char* package_path)
         if(show_progress_for_pid(app.dlg, "Install themes", "Installing...", pid))
         {
             /* move files in tmp_dir to user_icons_dir */
-            GDir* dir = g_dir_open(tmp_dir, 0, NULL);
+            GDir* dir;
+            GKeyFile* kf = g_key_file_new();
+
+            /* convert the themes in the dir to IconTheme structs and add them to app.icon_themes list */
+            load_icon_themes_from_dir(tmp_dir, kf);
+            g_key_file_free(kf);
+
+            /* now really move this themes to ~/.icons dir and also update the GUI */
+            dir = g_dir_open(tmp_dir, 0, NULL);
             if(dir)
             {
                 char* name;
                 while(name = g_dir_read_name(dir))
                 {
-                    g_debug("name = %s", name);
                     char* index_theme = g_build_filename(tmp_dir, name, "index.theme", NULL);
                     gboolean is_theme = g_file_test(index_theme, G_FILE_TEST_EXISTS);
                     g_free(index_theme);
                     if(is_theme)
                     {
                         char* theme_tmp = g_build_filename(tmp_dir, name, NULL);
-                        char* theme_target = g_build_filename(tmp_dir, name, NULL);
+                        char* theme_target = g_build_filename(user_icons_dir, name, NULL);
                         if(g_rename(theme_tmp, theme_target) == 0)
                         {
                             /* the theme is already installed to ~/.icons */
-                            /* update UI */
+                            GSList* l= g_slist_find_custom(app.icon_themes, name, (GCompareFunc)icon_theme_cmp_name);
+                            if(l)
+                            {
+                                IconTheme* theme = (IconTheme*)l->data;
+                                g_debug("installed theme: %p, %s", theme, theme->name);
+                                /* update UI */
+                                insert_theme_to_models(theme);
+                            }
                         }
                         else
                         {
@@ -145,6 +188,11 @@ static gboolean install_icon_theme_package(const char* package_path)
                     }
                 }
                 g_dir_close(dir);
+
+                /* remove remaining files. FIXME: will this cause problems? */
+                name = g_strdup_printf("rm -rf '%s'", tmp_dir);
+                g_spawn_command_line_sync(name, NULL, NULL, NULL, NULL);
+                g_free(name);
             }
         }
     }