Adding upstream version 0.2.1.
[debian/lxappearance.git] / src / main-dlg.c
index 2eb3c46..04a210f 100644 (file)
 #include "main-dlg-ui.h"
 #include "glade-support.h"
 
-#define GET_WIDGET( name )  name = lookup_widget( dlg, #name )
+enum {
+    COL_DISP_NAME,
+    COL_NAME,
+    N_COLS
+};
+
+#define GET_WIDGET_WITH_TYPE(name, type) name = type( lookup_widget( dlg, #name ))
 
 #define INIT_LIST(name, prop) \
-    GET_WIDGET( name##_view ); \
-    name##_list = init_tree_view( name##_view, G_CALLBACK(on_list_sel_changed), prop ); \
+    GET_WIDGET_WITH_TYPE( name##_view, GTK_TREE_VIEW ); \
+    name##_list = init_tree_view( name##_view, G_CALLBACK(on_list_sel_changed), prop); \
     load_##name##s( name##_list, name##_name );
 
-#define enable_apply()      gtk_dialog_set_response_sensitive( main_dlg, GTK_RESPONSE_APPLY, TRUE )
-#define disable_apply()      gtk_dialog_set_response_sensitive( main_dlg, GTK_RESPONSE_APPLY, FALSE )
+#define enable_apply()      gtk_dialog_set_response_sensitive(GTK_DIALOG (main_dlg), GTK_RESPONSE_APPLY, TRUE )
+#define disable_apply()      gtk_dialog_set_response_sensitive(GTK_DIALOG (main_dlg), GTK_RESPONSE_APPLY, FALSE )
 
 extern gboolean under_lxde;    /* wether lxde-xsettings daemon is active */
 
@@ -121,7 +127,7 @@ static void reload_demo_process()
         demo_pid = 0;
     }
 
-    g_snprintf( wid, 16, "%ld", gtk_socket_get_id(demo_socket) );
+    g_snprintf( wid, 16, "%ld", gtk_socket_get_id(GTK_SOCKET (demo_socket)) );
 
     argv[0] = g_get_prgname();
     argv[1] = "demo";
@@ -184,7 +190,8 @@ static void write_lxde_config()
 
        if( ! ret )
        {
-               const char** dir, **dirs = g_get_system_data_dirs();
+               const gchar* const * dir;
+               const gchar* const * dirs = g_get_system_data_dirs();
                create_lxde_config_dir();
                /* load system-wide config file */
                for( dir = dirs; *dir; ++dir )
@@ -226,9 +233,9 @@ static void on_list_sel_changed( GtkTreeSelection* sel, const char* prop )
     if( gtk_tree_selection_get_selected( sel, &model, &it ) )
     {
         char* name;
-        gtk_tree_model_get( model, &it, 0, &name, -1 );
+        gtk_tree_model_get( model, &it, COL_NAME, &name, -1 );
 
-        if( model == gtk_theme_list )   /* gtk+ theme */
+        if( model == GTK_TREE_MODEL (gtk_theme_list) )   /* gtk+ theme */
         {
             if( name && gtk_theme_name && 0 == strcmp( name, gtk_theme_name ) )
                 goto out;
@@ -238,7 +245,7 @@ static void on_list_sel_changed( GtkTreeSelection* sel, const char* prop )
             if( under_lxde )
                                g_object_set( gtk_settings_get_default(), "gtk-theme-name", name, NULL );
         }
-        else if( model == icon_theme_list )   /* icon theme */
+        else if( model == GTK_TREE_MODEL (icon_theme_list) )   /* icon theme */
         {
             if( name && icon_theme_name && 0 == strcmp( name, icon_theme_name ) )
                 goto out;
@@ -249,17 +256,16 @@ static void on_list_sel_changed( GtkTreeSelection* sel, const char* prop )
                                g_object_set( gtk_settings_get_default(), "gtk-icon-theme-name", name, NULL );
         }
 
-               if( under_lxde )
-               {
-                       enable_apply();
-               }
-               else
-               {
-                       write_rc_file( tmp_rc_file );
-                       //gtk_rc_reparse_all_for_settings(gtk_settings_get_default(), TRUE);
-                       reload_demo_process();
-               }
-
+       if( under_lxde )
+       {
+               enable_apply();
+       }
+       else
+       {
+               write_rc_file( tmp_rc_file );
+               //gtk_rc_reparse_all_for_settings(gtk_settings_get_default(), TRUE);
+               reload_demo_process();
+       }
         return;
     out:
         g_free( name );
@@ -270,9 +276,9 @@ static gint sort_func( GtkTreeModel* model, GtkTreeIter* it1, GtkTreeIter* it2,
 {
        char* str1, *str2;
        int ret;
-       gtk_tree_model_get( model, it1, 0, &str1, -1 );
-       gtk_tree_model_get( model, it2, 0, &str2, -1 );
-       ret = g_ascii_strcasecmp( str1, str2 );
+       gtk_tree_model_get( model, it1, COL_DISP_NAME, &str1, -1 );
+       gtk_tree_model_get( model, it2, COL_DISP_NAME, &str2, -1 );
+       ret = g_utf8_collate( str1, str2 );
        g_free( str1 );
        g_free( str2 );
        return ret;
@@ -283,45 +289,87 @@ static GtkListStore* init_tree_view( GtkTreeView* view, GCallback on_sel_changed
     GtkTreeViewColumn* col;
     GtkListStore* list;
     GtkTreeSelection* sel;
-    col = gtk_tree_view_column_new_with_attributes( NULL, gtk_cell_renderer_text_new(), "text", 0, NULL );
+    int text_col = strcmp(prop, "gtk-theme-name") ? COL_DISP_NAME : COL_NAME;
+
+    col = gtk_tree_view_column_new_with_attributes( NULL, gtk_cell_renderer_text_new(),
+                                                    "text", text_col, NULL );
     gtk_tree_view_append_column( view, col );
+
     sel = gtk_tree_view_get_selection(view);
-    g_signal_connect( sel, "changed", on_sel_changed, prop );
+    g_signal_connect( sel, "changed", on_sel_changed, GINT_TO_POINTER(prop) );
 
-    list = gtk_list_store_new( 1, G_TYPE_STRING );
-    gtk_tree_sortable_set_sort_func( (GtkTreeSortable*)list, 0, sort_func, NULL, NULL );
+    list = gtk_list_store_new( N_COLS, G_TYPE_STRING, G_TYPE_STRING );
+    gtk_tree_sortable_set_sort_func( (GtkTreeSortable*)list, text_col, sort_func, NULL, NULL );
     gtk_tree_view_set_model( view, (GtkTreeModel*)list );
     g_object_unref( list );
     return list;
 }
 
+typedef gboolean (*ThemeFunc)(const char* file, const char* dir, const char* name, GtkListStore* list, GtkTreeIter* it);
+
 static void load_themes_from_dir( GtkListStore* list,
-                                                    const char* dir_path,
-                                                    const char* lookup,
-                                                    GtkTreeSelection* sel,
-                                                    const char* init_sel )
+                                  const char* dir_path,
+                                  const char* lookup,
+                                  GtkTreeSelection* sel,
+                                  const char* init_sel,
+                                  ThemeFunc theme_func )
 {
     GDir* dir;
     if( dir = g_dir_open( dir_path, 0, NULL ) )
     {
-        char* name;
+        const char* name;
         while( name = g_dir_read_name( dir ) )
         {
             char* file = g_build_filename( dir_path, name, lookup, NULL );
             if( g_file_test( file, G_FILE_TEST_EXISTS ) )
             {
+                gboolean add = TRUE;
                 GtkTreeIter it;
-                gtk_list_store_append( list, &it );
-                gtk_list_store_set( list, &it, 0, name, -1 );
-                if( 0 == strcmp( name, init_sel ) )
+
+                /* prevent duplication */
+                if( gtk_tree_model_get_iter_first(GTK_TREE_MODEL (list), &it ) )
                 {
-                    GtkTreeView* view;
-                    GtkTreePath* tp;
-                    gtk_tree_selection_select_iter( sel, &it );
-                    view = gtk_tree_selection_get_tree_view( sel );
-                    tp = gtk_tree_model_get_path( (GtkTreeModel*)list, &it );
-                    gtk_tree_view_scroll_to_cell( view, tp, NULL, FALSE, 0, 0 );
-                    gtk_tree_path_free( tp );
+                    char* _name;
+                    do {
+                        _name = NULL;
+                        gtk_tree_model_get(GTK_TREE_MODEL (list), &it, COL_NAME, &_name, -1);
+                        if( _name && strcmp(_name, name) == 0 )
+                        {
+                            add = FALSE;
+                            g_free(_name);
+                            break;
+                        }
+                        g_free(_name);
+                    }
+                    while( gtk_tree_model_iter_next(GTK_TREE_MODEL (list), &it ) );
+                }
+
+                if( add )
+                {
+                    gtk_list_store_append( list, &it );
+                    gtk_list_store_set( list, &it, COL_NAME, name, -1 );
+
+                    if( theme_func )
+                    {
+                        if( ! theme_func(file, (char *) dir, name, list, &it) )
+                            add = FALSE;
+                    }
+
+                    if( add )
+                    {
+                        if( 0 == strcmp( name, init_sel ) )
+                        {
+                            GtkTreeView* view;
+                            GtkTreePath* tp;
+                            gtk_tree_selection_select_iter( sel, &it );
+                            view = gtk_tree_selection_get_tree_view( sel );
+                            tp = gtk_tree_model_get_path( (GtkTreeModel*)list, &it );
+                            gtk_tree_view_scroll_to_cell( view, tp, NULL, FALSE, 0, 0 );
+                            gtk_tree_path_free( tp );
+                        }
+                    }
+                    else
+                        gtk_list_store_remove( list, &it );
                 }
             }
             g_free( file );
@@ -331,10 +379,11 @@ static void load_themes_from_dir( GtkListStore* list,
 }
 
 static void load_from_data_dirs( GtkListStore* list,
-                                                const char* relative_path,
-                                                const char* lookup,
-                                                GtkTreeSelection* sel,
-                                                const char* init_sel  )
+                                 const char* relative_path,
+                                 const char* lookup,
+                                 GtkTreeSelection* sel,
+                                 const char* init_sel,
+                                 ThemeFunc theme_func )
 {
     const char* const *dirs = g_get_system_data_dirs();
     const char* const *dir;
@@ -342,21 +391,59 @@ static void load_from_data_dirs( GtkListStore* list,
     for( dir = dirs; *dir; ++dir )
     {
         dir_path = g_build_filename( *dir, relative_path, NULL );
-        load_themes_from_dir( list, dir_path, lookup, sel, init_sel );
+        load_themes_from_dir( list, dir_path, lookup, sel, init_sel, theme_func );
         g_free( dir_path );
     }
     dir_path = g_build_filename( g_get_user_data_dir(), relative_path, NULL );
-    load_themes_from_dir( list, dir_path, lookup, sel, init_sel );
+    load_themes_from_dir( list, dir_path, lookup, sel, init_sel, theme_func );
     g_free( dir_path );
 }
 
+static gboolean icon_theme_func(const char* file, const char* dir, const char* name, GtkListStore* list, GtkTreeIter* it)
+{
+    GKeyFile* kf;
+    char* disp_name = NULL;
+    if( g_str_has_prefix( name, "default." ) )
+        return FALSE;
+
+    kf = g_key_file_new();
+    if( g_key_file_load_from_file(kf, file, 0, NULL) )
+    {
+        if( g_key_file_has_key(kf, "Icon Theme", "Directories", NULL)
+            && ! g_key_file_get_boolean(kf, "Icon Theme", "Hidden", NULL) )
+        {
+            disp_name = g_key_file_get_locale_string(kf, "Icon Theme", "Name", NULL, NULL);
+            gtk_list_store_set( list, it, COL_DISP_NAME, disp_name ? disp_name : name, -1 );
+        }
+    }
+    g_key_file_free(kf);
+    return disp_name != NULL;
+}
+
+static gboolean cursor_theme_func(const char* dir, const char* name, const char* lookup)
+{
+    char* ret = NULL;
+/*
+    GKeyFile* kf = g_key_file_new();
+    if( g_key_file_load_from_file(kf, lookup, 0, NULL) )
+    {
+        if( g_key_file_has_key(kf, "Icon Theme", "Directories", NULL) )
+        {
+            ret = g_key_file_get_locale_string(kf, "Icon Theme", "Name", NULL, NULL);
+        }
+    }
+    g_key_file_free(kf);
+*/
+    return ret != NULL;
+}
+
 static void load_gtk_themes( GtkListStore* list, const char* cur_sel )
 {
     char* path;
     GtkTreeSelection* sel = gtk_tree_view_get_selection( gtk_theme_view );
-    load_from_data_dirs( list, "themes", "gtk-2.0", sel, cur_sel );
+    load_from_data_dirs( list, "themes", "gtk-2.0", sel, cur_sel, NULL );
     path = g_build_filename( g_get_home_dir(), ".themes", NULL );
-    load_themes_from_dir( list, path, "gtk-2.0", sel, cur_sel );
+    load_themes_from_dir( list, path, "gtk-2.0", sel, cur_sel, NULL );
     g_free( path );
     gtk_tree_sortable_set_sort_column_id( (GtkTreeSortable*)list, 0, GTK_SORT_ASCENDING );
 }
@@ -365,9 +452,9 @@ static void load_icon_themes( GtkListStore* list, const char* cur_sel )
 {
     char* path;
     GtkTreeSelection* sel = gtk_tree_view_get_selection( icon_theme_view );
-    load_from_data_dirs( list, "icons", "index.theme", sel, cur_sel );
+    load_from_data_dirs( list, "icons", "index.theme", sel, cur_sel, icon_theme_func );
     path = g_build_filename( g_get_home_dir(), ".icons", NULL );
-    load_themes_from_dir( list, path, "index.theme", sel, cur_sel );
+    load_themes_from_dir( list, path, "index.theme", sel, cur_sel, icon_theme_func );
     g_free( path );
     gtk_tree_sortable_set_sort_column_id( (GtkTreeSortable*)list, 0, GTK_SORT_ASCENDING );
 }
@@ -379,7 +466,7 @@ static void load_fonts( GtkListStore* list )
 }
 */
 
-gboolean center_win( GtkWindow* dlg )
+gboolean center_win( GtkWidget* dlg )
 {
     gtk_widget_show( dlg );
     return FALSE;
@@ -437,7 +524,7 @@ void main_dlg_init( GtkWidget* dlg )
 
     gtk_combo_box_set_active( (GtkComboBox*)lookup_widget(dlg, "tb_style"), tb_style < 4 ? tb_style : 3 );
 
-    GET_WIDGET( demo_box );
+    GET_WIDGET_WITH_TYPE( demo_box, GTK_WIDGET );
     gtk_widget_show( demo_box );
 
        if( under_lxde )
@@ -539,12 +626,12 @@ on_install_theme_clicked               (GtkButton       *button,
     gtk_file_filter_add_pattern( filter, "*.tar.bz2" );
     gtk_file_filter_set_name( filter, _("*.tar.gz, *.tar.bz2 (Icon Theme)") );
 
-    gtk_file_chooser_add_filter( fc, filter );
-    gtk_file_chooser_set_filter( (GtkFileChooser*)fc, filter );
+    gtk_file_chooser_add_filter( GTK_FILE_CHOOSER(fc), filter );
+    gtk_file_chooser_set_filter( GTK_FILE_CHOOSER(fc), filter );
 
     if( gtk_dialog_run( (GtkDialog*)fc ) == GTK_RESPONSE_OK )
     {
-        char* file = gtk_file_chooser_get_filename( fc );
+        char* file = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER(fc) );
         char* argv[]={
             PACKAGE_DATA_DIR"/lxappearance/install-icon-theme.sh",
             file, NULL };