1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
/*
 * Copyright (C) 2007, 2010 The GNOME Foundation
 * Written by Thomas Wood <thos@gnome.org>
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
#include "appearance.h"

#include <string.h>
#include <pango/pango.h>
#include <glib/gi18n.h>
#include <gio/gio.h>

#include "theme-util.h"
#include "ctkrc-utils.h"
#include "theme-thumbnail.h"
#include "capplet-util.h"

#define GSETTINGS_SETTINGS "GSETTINGS_SETTINGS"
#define GSETTINGS_KEY      "GSETTINGS_KEY"
#define THEME_DATA         "THEME_DATA"

typedef void (* ThumbnailGenFunc) (void               *type,
				   ThemeThumbnailFunc  theme,
				   AppearanceData     *data,
				   GDestroyNotify     *destroy);

typedef struct {
  AppearanceData *data;
  GdkPixbuf *thumbnail;
} ThemeConvData;

static void update_message_area (AppearanceData *data);
static void create_thumbnail (const gchar *name, GdkPixbuf *default_thumb, AppearanceData *data);

static const gchar *symbolic_names[NUM_SYMBOLIC_COLORS] = {
  "fg_color", "bg_color",
  "text_color", "base_color",
  "selected_fg_color", "selected_bg_color",
  "tooltip_fg_color", "tooltip_bg_color"
};

static gchar *
find_string_in_model (CtkTreeModel *model, const gchar *value, gint column)
{
  CtkTreeIter iter;
  gboolean valid;
  gchar *path = NULL, *test;

  if (!value)
    return NULL;

  for (valid = ctk_tree_model_get_iter_first (model, &iter); valid;
       valid = ctk_tree_model_iter_next (model, &iter))
  {
    ctk_tree_model_get (model, &iter, column, &test, -1);

    if (test)
    {
      gint cmp = strcmp (test, value);
      g_free (test);

      if (!cmp)
      {
        path = ctk_tree_model_get_string_from_iter (model, &iter);
        break;
      }
    }
  }

  return path;
}

static void
treeview_gsettings_changed_callback (GSettings *settings, gchar *key, CtkTreeView *list)
{
  CtkTreeModel *store;
  gchar *curr_value;
  gchar *path;

  /* find value in model */
  curr_value = g_settings_get_string (settings, key);
  store = ctk_tree_view_get_model (list);

  path = find_string_in_model (store, curr_value, COL_NAME);

  /* Add a temporary item if we can't find a match
   * TODO: delete this item if it is no longer selected?
   */
  if (!path)
  {
    CtkListStore *list_store;
    CtkTreeIter iter, sort_iter;
    ThemeConvData *conv;

    list_store = CTK_LIST_STORE (ctk_tree_model_sort_get_model (CTK_TREE_MODEL_SORT (store)));

    conv = g_object_get_data (G_OBJECT(list), THEME_DATA);
    ctk_list_store_insert_with_values (list_store, &iter, 0,
                                       COL_LABEL, curr_value,
                                       COL_NAME, curr_value,
                                       COL_THUMBNAIL, conv->thumbnail,
                                       -1);
    /* convert the tree store iter for use with the sort model */
    ctk_tree_model_sort_convert_child_iter_to_iter (CTK_TREE_MODEL_SORT (store),
                                                    &sort_iter, &iter);
    path = ctk_tree_model_get_string_from_iter (store, &sort_iter);

    create_thumbnail (curr_value, conv->thumbnail, conv->data);
  }
  /* select the new gsettings theme in treeview */
  CtkTreeSelection *selection = ctk_tree_view_get_selection (list);
  CtkTreePath *treepath = ctk_tree_path_new_from_string (path);
  ctk_tree_selection_select_path (selection, treepath);
  ctk_tree_view_scroll_to_cell (list, treepath, NULL, FALSE, 0, 0);
  ctk_tree_path_free (treepath);
}

static void
treeview_selection_changed_callback (CtkTreeSelection *selection, guint data)
{
  GSettings *settings;
  gchar *key;
  CtkTreeIter iter;
  CtkTreeModel *model;
  CtkTreeView *list;

  list = ctk_tree_selection_get_tree_view (selection);

  settings = g_object_get_data (G_OBJECT (list), GSETTINGS_SETTINGS);
  key = g_object_get_data (G_OBJECT (list), GSETTINGS_KEY);

  if (ctk_tree_selection_get_selected (selection, &model, &iter)) {
    gchar *list_value;

    ctk_tree_model_get (model, &iter, COL_NAME, &list_value, -1);

    if (list_value) {
      g_settings_set_string (settings, key, list_value);
    }
  }
}

static gint
cursor_theme_sort_func (CtkTreeModel *model,
                        CtkTreeIter *a,
                        CtkTreeIter *b,
                        gpointer user_data)
{
  gchar *a_label = NULL;
  gchar *b_label = NULL;
  const gchar *default_label;
  gint result;

  ctk_tree_model_get (model, a, COL_LABEL, &a_label, -1);
  ctk_tree_model_get (model, b, COL_LABEL, &b_label, -1);

  default_label = _("Default Pointer");

  if (!strcmp (a_label, default_label))
    result = -1;
  else if (!strcmp (b_label, default_label))
    result = 1;
  else
    result = strcmp (a_label, b_label);

  g_free (a_label);
  g_free (b_label);

  return result;
}

static void
style_message_area_response_cb (CtkWidget *w,
                                gint response_id,
                                AppearanceData *data)
{
  CtkSettings *settings = ctk_settings_get_default ();
  gchar *theme;
  gchar *engine_path;

  g_object_get (settings, "ctk-theme-name", &theme, NULL);
  engine_path = ctk_theme_info_missing_engine (theme, FALSE);
  g_free (theme);

  if (engine_path != NULL) {
    theme_install_file (CTK_WINDOW (ctk_widget_get_toplevel (data->style_message_area)),
                        engine_path);
    g_free (engine_path);
  }
  update_message_area (data);
}

static void update_message_area(AppearanceData* data)
{
	CtkSettings* settings = ctk_settings_get_default();
	gchar* theme = NULL;
	gchar* engine;

	g_object_get(settings, "ctk-theme-name", &theme, NULL);
	engine = ctk_theme_info_missing_engine(theme, TRUE);
	g_free(theme);

	if (data->style_message_area == NULL)
	{
		CtkWidget* hbox;
		CtkWidget* parent;
		CtkWidget* icon;
		CtkWidget* content;

		if (engine == NULL)
		{
			return;
		}

		data->style_message_area = ctk_info_bar_new ();

		g_signal_connect (data->style_message_area, "response", (GCallback) style_message_area_response_cb, data);

		data->style_install_button = ctk_info_bar_add_button(CTK_INFO_BAR (data->style_message_area), _("Install"), CTK_RESPONSE_APPLY);

		data->style_message_label = ctk_label_new (NULL);
		ctk_label_set_line_wrap (CTK_LABEL (data->style_message_label), TRUE);
		ctk_label_set_xalign (CTK_LABEL (data->style_message_label), 0.0);

		hbox = ctk_box_new (CTK_ORIENTATION_HORIZONTAL, 9);
		icon = ctk_image_new_from_icon_name ("dialog-warning", CTK_ICON_SIZE_DIALOG);
		ctk_widget_set_halign (icon, CTK_ALIGN_CENTER);
		ctk_widget_set_valign (icon, CTK_ALIGN_START);
		ctk_box_pack_start (CTK_BOX (hbox), icon, FALSE, FALSE, 0);
		ctk_box_pack_start (CTK_BOX (hbox), data->style_message_label, TRUE, TRUE, 0);
		content = ctk_info_bar_get_content_area (CTK_INFO_BAR (data->style_message_area));
		ctk_container_add (CTK_CONTAINER (content), hbox);
		ctk_widget_show_all (data->style_message_area);
		ctk_widget_set_no_show_all (data->style_message_area, TRUE);

		parent = appearance_capplet_get_widget (data, "ctk_themes_vbox");
		ctk_box_pack_start (CTK_BOX (parent), data->style_message_area, FALSE, FALSE, 0);
	}

  ctk_widget_hide(data->style_message_area);
}

static void
update_color_buttons_from_string (const gchar *color_scheme, AppearanceData *data)
{
  CdkRGBA colors[NUM_SYMBOLIC_COLORS];
  CtkWidget *widget;
  gint i;

  if (!cafe_theme_color_scheme_parse (color_scheme, colors))
    return;

  /* now set all the buttons to the correct settings */
  for (i = 0; i < NUM_SYMBOLIC_COLORS; ++i) {
    widget = appearance_capplet_get_widget (data, symbolic_names[i]);
    ctk_color_chooser_set_rgba (CTK_COLOR_CHOOSER (widget), &colors[i]);
  }
}

static void
update_color_buttons_from_settings (CtkSettings *settings,
                                    AppearanceData *data)
{
  gchar *scheme, *setting;

  scheme = g_settings_get_string (data->interface_settings, COLOR_SCHEME_KEY);
  g_object_get (settings, "ctk-color-scheme", &setting, NULL);

  if (scheme == NULL || strcmp (scheme, "") == 0)
    ctk_widget_set_sensitive (appearance_capplet_get_widget (data, "color_scheme_defaults_button"), FALSE);

  g_free (scheme);
  update_color_buttons_from_string (setting, data);
  g_free (setting);
}

static void
color_scheme_changed (GObject    *settings,
                      GParamSpec *pspec,
                      AppearanceData  *data)
{
  update_color_buttons_from_settings (CTK_SETTINGS (settings), data);
}

static void
check_color_schemes_enabled (CtkSettings *settings,
                             AppearanceData *data)
{
  gchar *theme = NULL;
  gchar *filename;
  GSList *symbolic_colors = NULL;
  gboolean enable_colors = FALSE;
  gint i;

  g_object_get (settings, "ctk-theme-name", &theme, NULL);
  filename = ctkrc_find_named (theme);
  g_free (theme);

  ctkrc_get_details (filename, NULL, &symbolic_colors);
  g_free (filename);

  for (i = 0; i < NUM_SYMBOLIC_COLORS; ++i) {
    gboolean found;

    found = (g_slist_find_custom (symbolic_colors, symbolic_names[i], (GCompareFunc) strcmp) != NULL);
    ctk_widget_set_sensitive (appearance_capplet_get_widget (data, symbolic_names[i]), found);

    enable_colors |= found;
  }

  g_slist_foreach (symbolic_colors, (GFunc) g_free, NULL);
  g_slist_free (symbolic_colors);

  ctk_widget_set_sensitive (appearance_capplet_get_widget (data, "color_scheme_table"), enable_colors);
  ctk_widget_set_sensitive (appearance_capplet_get_widget (data, "color_scheme_defaults_button"), enable_colors);

  if (enable_colors)
    ctk_widget_hide (appearance_capplet_get_widget (data, "color_scheme_message_hbox"));
  else
    ctk_widget_show (appearance_capplet_get_widget (data, "color_scheme_message_hbox"));
}

static void
color_button_clicked_cb (CtkWidget *colorbutton, AppearanceData *data)
{
  CtkWidget *widget;
  CdkRGBA color;
  GString *scheme = g_string_new (NULL);
  gchar *colstr;
  gchar *old_scheme = NULL;
  gint i;

  for (i = 0; i < NUM_SYMBOLIC_COLORS; ++i) {
    widget = appearance_capplet_get_widget (data, symbolic_names[i]);
    ctk_color_chooser_get_rgba (CTK_COLOR_CHOOSER (widget), &color);

    colstr = cdk_rgba_to_string (&color);
    g_string_append_printf (scheme, "%s:%s\n", symbolic_names[i], colstr);
    g_free (colstr);
  }
  /* remove the last newline */
  g_string_truncate (scheme, scheme->len - 1);

  /* verify that the scheme really has changed */
  g_object_get (ctk_settings_get_default (), "ctk-color-scheme", &old_scheme, NULL);

  if (!cafe_theme_color_scheme_equal (old_scheme, scheme->str)) {
    g_settings_set_string (data->interface_settings, COLOR_SCHEME_KEY, scheme->str);

    ctk_widget_set_sensitive (appearance_capplet_get_widget (data, "color_scheme_defaults_button"), TRUE);
  }
  g_free (old_scheme);
  g_string_free (scheme, TRUE);
}

static void
color_scheme_defaults_button_clicked_cb (CtkWidget *button, AppearanceData *data)
{
  g_settings_reset (data->interface_settings, COLOR_SCHEME_KEY);
  ctk_widget_set_sensitive (appearance_capplet_get_widget (data, "color_scheme_defaults_button"), FALSE);
}

static void
style_response_cb (CtkDialog *dialog, gint response_id)
{
  if (response_id == CTK_RESPONSE_HELP) {
    capplet_help (CTK_WINDOW (dialog), "goscustdesk-61");
  } else {
    ctk_widget_hide (CTK_WIDGET (dialog));
  }
}

static void
ctk_theme_changed (GSettings *settings, gchar *key, AppearanceData *data)
{
  CafeThemeInfo *theme = NULL;
  gchar *name;
  CtkSettings *ctksettings = ctk_settings_get_default ();

  name = g_settings_get_string (settings, key);
  if (name) {
    gchar *current;

    theme = cafe_theme_info_find (name);

    /* Manually update CtkSettings to new ctk+ theme.
     * This will eventually happen anyway, but we need the
     * info for the color scheme updates already. */
    g_object_get (ctksettings, "ctk-theme-name", &current, NULL);

    if (strcmp (current, name) != 0) {
      g_object_set (ctksettings, "ctk-theme-name", name, NULL);
      update_message_area (data);
    }

    g_free (current);

    check_color_schemes_enabled (ctksettings, data);
    update_color_buttons_from_settings (ctksettings, data);
    g_free (name);
  }

  ctk_widget_set_sensitive (appearance_capplet_get_widget (data, "ctk_themes_delete"),
			    theme_is_writable (theme));
}

static void
window_theme_changed (GSettings *settings, gchar *key, AppearanceData *data)
{
  CafeThemeInfo *theme = NULL;
  gchar *name;

  name = g_settings_get_string (settings, key);
  if (name)
  {
    theme = cafe_theme_info_find (name);
    g_free (name);
  }

  ctk_widget_set_sensitive (appearance_capplet_get_widget (data, "window_themes_delete"),
			    theme_is_writable (theme));
}

static void
icon_theme_changed (GSettings *settings, gchar *key, AppearanceData *data)
{
  CafeThemeIconInfo *theme = NULL;
  gchar *name;

  name = g_settings_get_string (settings, key);
  if (name) {
    theme = cafe_theme_icon_info_find (name);
    g_free (name);
}

  ctk_widget_set_sensitive (appearance_capplet_get_widget (data, "icon_themes_delete"),
			    theme_is_writable (theme));
}

static void
cursor_size_changed_cb (int size, AppearanceData *data)
{
  g_settings_set_int (data->mouse_settings, CURSOR_SIZE_KEY, size);
}

static void
cursor_size_scale_value_changed_cb (CtkRange *range, AppearanceData *data)
{
  CafeThemeCursorInfo *theme;
  gchar *name;

  name = g_settings_get_string (data->mouse_settings, CURSOR_THEME_KEY);
  if (name == NULL)
    return;

  theme = cafe_theme_cursor_info_find (name);
  g_free (name);

  if (theme) {
    gint size;

    size = g_array_index (theme->sizes, gint, (int) ctk_range_get_value (range));
    cursor_size_changed_cb (size, data);
  }
}

static void
update_cursor_size_scale (CafeThemeCursorInfo *theme,
                          AppearanceData *data)
{
  CtkWidget *cursor_size_scale;
  CtkWidget *cursor_size_label;
  CtkWidget *cursor_size_small_label;
  CtkWidget *cursor_size_large_label;
  gboolean sensitive;
  gint size, gsettings_size;

  cursor_size_scale = appearance_capplet_get_widget (data, "cursor_size_scale");
  cursor_size_label = appearance_capplet_get_widget (data, "cursor_size_label");
  cursor_size_small_label = appearance_capplet_get_widget (data, "cursor_size_small_label");
  cursor_size_large_label = appearance_capplet_get_widget (data, "cursor_size_large_label");

  sensitive = theme && theme->sizes->len > 1;
  ctk_widget_set_sensitive (cursor_size_scale, sensitive);
  ctk_widget_set_sensitive (cursor_size_label, sensitive);
  ctk_widget_set_sensitive (cursor_size_small_label, sensitive);
  ctk_widget_set_sensitive (cursor_size_large_label, sensitive);

  gsettings_size = g_settings_get_int (data->mouse_settings, CURSOR_SIZE_KEY);

  if (sensitive) {
    CtkAdjustment *adjustment;
    gint i, index;
    CtkRange *range = CTK_RANGE (cursor_size_scale);

    adjustment = ctk_range_get_adjustment (range);
    g_object_set (adjustment, "upper", (gdouble) theme->sizes->len - 1, NULL);


    /* fallback if the gsettings value is bigger than all available sizes;
       use the largest we have */
    index = theme->sizes->len - 1;

    /* set the slider to the cursor size which matches the gsettings setting best  */
    for (i = 0; i < theme->sizes->len; i++) {
      size = g_array_index (theme->sizes, gint, i);

      if (size == gsettings_size) {
      	index = i;
        break;
      } else if (size > gsettings_size) {
        if (i == 0) {
          index = 0;
        } else {
          gint diff, diff_to_last;

          diff = size - gsettings_size;
          diff_to_last = gsettings_size - g_array_index (theme->sizes, gint, i - 1);

          index = (diff < diff_to_last) ? i : i - 1;
        }
        break;
      }
    }

    ctk_range_set_value (range, (gdouble) index);

    size = g_array_index (theme->sizes, gint, index);
  } else {
    if (theme && theme->sizes->len > 0)
      size = g_array_index (theme->sizes, gint, 0);
    else
      size = 24;
  }

  if (size != gsettings_size)
    cursor_size_changed_cb (size, data);
}

static void
cursor_theme_changed (GSettings *settings, gchar *key, AppearanceData *data)
{
  CafeThemeCursorInfo *theme = NULL;
  gchar *name;

  name = g_settings_get_string (settings, key);
  if (name) {
    theme = cafe_theme_cursor_info_find (name);
    g_free (name);
  }

  update_cursor_size_scale (theme, data);

  ctk_widget_set_sensitive (appearance_capplet_get_widget (data, "cursor_themes_delete"),
			    theme_is_writable (theme));

}

static void
generic_theme_delete (const gchar *tv_name, ThemeType type, AppearanceData *data)
{
  CtkTreeView *treeview = CTK_TREE_VIEW (appearance_capplet_get_widget (data, tv_name));
  CtkTreeSelection *selection = ctk_tree_view_get_selection (treeview);
  CtkTreeModel *model;
  CtkTreeIter iter;

  if (ctk_tree_selection_get_selected (selection, &model, &iter)) {
    gchar *name;

    ctk_tree_model_get (model, &iter, COL_NAME, &name, -1);

    if (name != NULL && theme_delete (name, type)) {
      /* remove theme from the model, too */
      CtkTreeIter child;
      CtkTreePath *path;

      path = ctk_tree_model_get_path (model, &iter);
      ctk_tree_model_sort_convert_iter_to_child_iter (
          CTK_TREE_MODEL_SORT (model), &child, &iter);
      ctk_list_store_remove (CTK_LIST_STORE (
          ctk_tree_model_sort_get_model (CTK_TREE_MODEL_SORT (model))), &child);

      if (ctk_tree_model_get_iter (model, &iter, path) ||
          theme_model_iter_last (model, &iter)) {
        ctk_tree_path_free (path);
        path = ctk_tree_model_get_path (model, &iter);
	ctk_tree_selection_select_path (selection, path);
	ctk_tree_view_scroll_to_cell (treeview, path, NULL, FALSE, 0, 0);
      }
      ctk_tree_path_free (path);
    }
    g_free (name);
  }
}

static void
ctk_theme_delete_cb (CtkWidget *button, AppearanceData *data)
{
  generic_theme_delete ("ctk_themes_list", THEME_TYPE_CTK, data);
}

static void
window_theme_delete_cb (CtkWidget *button, AppearanceData *data)
{
  generic_theme_delete ("window_themes_list", THEME_TYPE_WINDOW, data);
}

static void
icon_theme_delete_cb (CtkWidget *button, AppearanceData *data)
{
  generic_theme_delete ("icon_themes_list", THEME_TYPE_ICON, data);
}

static void
cursor_theme_delete_cb (CtkWidget *button, AppearanceData *data)
{
  generic_theme_delete ("cursor_themes_list", THEME_TYPE_CURSOR, data);
}

static void
add_to_treeview (const gchar *tv_name,
		 const gchar *theme_name,
		 const gchar *theme_label,
		 GdkPixbuf *theme_thumbnail,
		 AppearanceData *data)
{
  CtkTreeView *treeview;
  CtkListStore *model;

  treeview = CTK_TREE_VIEW (appearance_capplet_get_widget (data, tv_name));
  model = CTK_LIST_STORE (
          ctk_tree_model_sort_get_model (
          CTK_TREE_MODEL_SORT (ctk_tree_view_get_model (treeview))));

  ctk_list_store_insert_with_values (model, NULL, 0,
          COL_LABEL, theme_label,
          COL_NAME, theme_name,
          COL_THUMBNAIL, theme_thumbnail,
          -1);
}

static void
remove_from_treeview (const gchar *tv_name,
		      const gchar *theme_name,
		      AppearanceData *data)
{
  CtkTreeView *treeview;
  CtkListStore *model;
  CtkTreeIter iter;

  treeview = CTK_TREE_VIEW (appearance_capplet_get_widget (data, tv_name));
  model = CTK_LIST_STORE (
          ctk_tree_model_sort_get_model (
          CTK_TREE_MODEL_SORT (ctk_tree_view_get_model (treeview))));

  if (theme_find_in_model (CTK_TREE_MODEL (model), theme_name, &iter))
    ctk_list_store_remove (model, &iter);
}

static void
update_in_treeview (const gchar *tv_name,
		    const gchar *theme_name,
		    const gchar *theme_label,
		    AppearanceData *data)
{
  CtkTreeView *treeview;
  CtkListStore *model;
  CtkTreeIter iter;

  treeview = CTK_TREE_VIEW (appearance_capplet_get_widget (data, tv_name));
  model = CTK_LIST_STORE (
          ctk_tree_model_sort_get_model (
          CTK_TREE_MODEL_SORT (ctk_tree_view_get_model (treeview))));

  if (theme_find_in_model (CTK_TREE_MODEL (model), theme_name, &iter)) {
    ctk_list_store_set (model, &iter,
          COL_LABEL, theme_label,
          COL_NAME, theme_name,
          -1);
  }
}

static void
update_thumbnail_in_treeview (const gchar *tv_name,
		    const gchar *theme_name,
		    GdkPixbuf *theme_thumbnail,
		    AppearanceData *data)
{
  CtkTreeView *treeview;
  CtkListStore *model;
  CtkTreeIter iter;

  if (theme_thumbnail == NULL)
    return;

  treeview = CTK_TREE_VIEW (appearance_capplet_get_widget (data, tv_name));
  model = CTK_LIST_STORE (
          ctk_tree_model_sort_get_model (
          CTK_TREE_MODEL_SORT (ctk_tree_view_get_model (treeview))));

  if (theme_find_in_model (CTK_TREE_MODEL (model), theme_name, &iter)) {
    ctk_list_store_set (model, &iter,
          COL_THUMBNAIL, theme_thumbnail,
          -1);
  }
}

static void
ctk_theme_thumbnail_cb (GdkPixbuf *pixbuf,
                        gchar *theme_name,<--- Parameter 'theme_name' can be declared as pointer to const
                        AppearanceData *data)
{
  update_thumbnail_in_treeview ("ctk_themes_list", theme_name, pixbuf, data);
}

static void
croma_theme_thumbnail_cb (GdkPixbuf *pixbuf,
                             gchar *theme_name,<--- Parameter 'theme_name' can be declared as pointer to const
                             AppearanceData *data)
{
  update_thumbnail_in_treeview ("window_themes_list", theme_name, pixbuf, data);
}

static void
icon_theme_thumbnail_cb (GdkPixbuf *pixbuf,
                         gchar *theme_name,<--- Parameter 'theme_name' can be declared as pointer to const
                         AppearanceData *data)
{
  update_thumbnail_in_treeview ("icon_themes_list", theme_name, pixbuf, data);
}

static void
create_thumbnail (const gchar *name, GdkPixbuf *default_thumb, AppearanceData *data)<--- Parameter 'default_thumb' can be declared as pointer to const
{
  if (default_thumb == data->icon_theme_icon) {
    CafeThemeIconInfo *info;
    info = cafe_theme_icon_info_find (name);
    if (info != NULL) {
      generate_icon_theme_thumbnail_async (info,
          (ThemeThumbnailFunc) icon_theme_thumbnail_cb, data, NULL);
    }
  } else if (default_thumb == data->ctk_theme_icon) {
    CafeThemeInfo *info;
    info = cafe_theme_info_find (name);
    if (info != NULL && info->has_ctk) {
      generate_ctk_theme_thumbnail_async (info,
          (ThemeThumbnailFunc) ctk_theme_thumbnail_cb, data, NULL);
    }
  } else if (default_thumb == data->window_theme_icon) {
    CafeThemeInfo *info;
    info = cafe_theme_info_find (name);
    if (info != NULL && info->has_croma) {
      generate_croma_theme_thumbnail_async (info,
          (ThemeThumbnailFunc) croma_theme_thumbnail_cb, data, NULL);
    }
  }
}

static void
changed_on_disk_cb (CafeThemeCommonInfo *theme,
		    CafeThemeChangeType  change_type,
                    CafeThemeElement     element_type,
		    AppearanceData       *data)
{
  if (theme->type == CAFE_THEME_TYPE_REGULAR) {
    CafeThemeInfo *info = (CafeThemeInfo *) theme;

    if (change_type == CAFE_THEME_CHANGE_DELETED) {
      if (element_type & CAFE_THEME_CTK_2)
        remove_from_treeview ("ctk_themes_list", info->name, data);
      if (element_type & CAFE_THEME_CROMA)
        remove_from_treeview ("window_themes_list", info->name, data);

    } else {
      if (element_type & CAFE_THEME_CTK_2) {
        if (change_type == CAFE_THEME_CHANGE_CREATED)
          add_to_treeview ("ctk_themes_list", info->name, info->name, data->ctk_theme_icon, data);
        else if (change_type == CAFE_THEME_CHANGE_CHANGED)
          update_in_treeview ("ctk_themes_list", info->name, info->name, data);

        generate_ctk_theme_thumbnail_async (info,
            (ThemeThumbnailFunc) ctk_theme_thumbnail_cb, data, NULL);
      }

      if (element_type & CAFE_THEME_CROMA) {
        if (change_type == CAFE_THEME_CHANGE_CREATED)
          add_to_treeview ("window_themes_list", info->name, info->name, data->window_theme_icon, data);
        else if (change_type == CAFE_THEME_CHANGE_CHANGED)
          update_in_treeview ("window_themes_list", info->name, info->name, data);

        generate_croma_theme_thumbnail_async (info,
            (ThemeThumbnailFunc) croma_theme_thumbnail_cb, data, NULL);
      }
    }

  } else if (theme->type == CAFE_THEME_TYPE_ICON) {
    CafeThemeIconInfo *info = (CafeThemeIconInfo *) theme;

    if (change_type == CAFE_THEME_CHANGE_DELETED) {
      remove_from_treeview ("icon_themes_list", info->name, data);
    } else {
      if (change_type == CAFE_THEME_CHANGE_CREATED)
        add_to_treeview ("icon_themes_list", info->name, info->readable_name, data->icon_theme_icon, data);
      else if (change_type == CAFE_THEME_CHANGE_CHANGED)
        update_in_treeview ("icon_themes_list", info->name, info->readable_name, data);

      generate_icon_theme_thumbnail_async (info,
          (ThemeThumbnailFunc) icon_theme_thumbnail_cb, data, NULL);
    }

  } else if (theme->type == CAFE_THEME_TYPE_CURSOR) {
    CafeThemeCursorInfo *info = (CafeThemeCursorInfo *) theme;

    if (change_type == CAFE_THEME_CHANGE_DELETED) {
      remove_from_treeview ("cursor_themes_list", info->name, data);
    } else {
      if (change_type == CAFE_THEME_CHANGE_CREATED)
        add_to_treeview ("cursor_themes_list", info->name, info->readable_name, info->thumbnail, data);
      else if (change_type == CAFE_THEME_CHANGE_CHANGED)
        update_in_treeview ("cursor_themes_list", info->name, info->readable_name, data);
    }
  }
}

static void
prepare_list (AppearanceData *data, CtkWidget *list, ThemeType type, GCallback callback)
{
  CtkListStore *store;
  GList *l, *themes = NULL;
  CtkCellRenderer *renderer;
  CtkTreeViewColumn *column;
  CtkTreeModel *sort_model;
  GdkPixbuf *thumbnail;
  const gchar *key;
  ThumbnailGenFunc generator;
  ThemeThumbnailFunc thumb_cb;
  ThemeConvData *conv_data;
  GSettings *settings;

  switch (type)
  {
    case THEME_TYPE_CTK:
      themes = cafe_theme_info_find_by_type (CAFE_THEME_CTK_2);
      thumbnail = data->ctk_theme_icon;
      settings = data->interface_settings;
      key = CTK_THEME_KEY;
      generator = (ThumbnailGenFunc) generate_ctk_theme_thumbnail_async;
      thumb_cb = (ThemeThumbnailFunc) ctk_theme_thumbnail_cb;<--- You might need to cast the function pointer here
      break;

    case THEME_TYPE_WINDOW:
      themes = cafe_theme_info_find_by_type (CAFE_THEME_CROMA);
      thumbnail = data->window_theme_icon;
      settings = data->croma_settings;
      key = CROMA_THEME_KEY;
      generator = (ThumbnailGenFunc) generate_croma_theme_thumbnail_async;
      thumb_cb = (ThemeThumbnailFunc) croma_theme_thumbnail_cb;<--- You might need to cast the function pointer here
      break;

    case THEME_TYPE_ICON:
      themes = cafe_theme_icon_info_find_all ();
      thumbnail = data->icon_theme_icon;
      settings = data->interface_settings;
      key = ICON_THEME_KEY;
      generator = (ThumbnailGenFunc) generate_icon_theme_thumbnail_async;
      thumb_cb = (ThemeThumbnailFunc) icon_theme_thumbnail_cb;<--- You might need to cast the function pointer here
      break;

    case THEME_TYPE_CURSOR:
      themes = cafe_theme_cursor_info_find_all ();
      thumbnail = NULL;
      settings = data->mouse_settings;
      key = CURSOR_THEME_KEY;
      generator = NULL;<--- Assignment 'generator=NULL', assigned value is 0
      thumb_cb = NULL;
      break;

    default:
      /* we don't deal with any other type of themes here */
      return;
  }

  store = ctk_list_store_new (NUM_COLS, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_STRING);

  for (l = themes; l; l = g_list_next (l))
  {
    CafeThemeCommonInfo *theme = (CafeThemeCommonInfo *) l->data;
    CtkTreeIter i;

    if (type == THEME_TYPE_CURSOR) {
      thumbnail = ((CafeThemeCursorInfo *) theme)->thumbnail;
    } else {
      generator (theme, thumb_cb, data, NULL);<--- Null pointer dereference
    }

    ctk_list_store_insert_with_values (store, &i, 0,
                                       COL_LABEL, theme->readable_name,
                                       COL_NAME, theme->name,
                                       COL_THUMBNAIL, thumbnail,
                                       -1);

    if (type == THEME_TYPE_CURSOR && thumbnail) {
      g_object_unref (thumbnail);
      thumbnail = NULL;
    }
  }
  g_list_free (themes);

  sort_model = ctk_tree_model_sort_new_with_model (CTK_TREE_MODEL (store));
  ctk_tree_sortable_set_sort_column_id (CTK_TREE_SORTABLE (sort_model),
                                        COL_LABEL, CTK_SORT_ASCENDING);

  if (type == THEME_TYPE_CURSOR)
    ctk_tree_sortable_set_sort_func (CTK_TREE_SORTABLE (sort_model), COL_LABEL,
                                     (CtkTreeIterCompareFunc) cursor_theme_sort_func,
                                     NULL, NULL);

  ctk_tree_view_set_model (CTK_TREE_VIEW (list), CTK_TREE_MODEL (sort_model));

  renderer = ctk_cell_renderer_pixbuf_new ();
  g_object_set (renderer, "xpad", 3, "ypad", 3, NULL);

  column = ctk_tree_view_column_new ();
  ctk_tree_view_column_pack_start (column, renderer, FALSE);
  ctk_tree_view_column_add_attribute (column, renderer, "pixbuf", COL_THUMBNAIL);
  ctk_tree_view_append_column (CTK_TREE_VIEW (list), column);

  renderer = ctk_cell_renderer_text_new ();

  column = ctk_tree_view_column_new ();
  ctk_tree_view_column_pack_start (column, renderer, FALSE);
  ctk_tree_view_column_add_attribute (column, renderer, "text", COL_LABEL);
  ctk_tree_view_append_column (CTK_TREE_VIEW (list), column);

  conv_data = g_new (ThemeConvData, 1);
  conv_data->data = data;
  conv_data->thumbnail = thumbnail;

  /* set useful data for callbacks */
  g_object_set_data_full (G_OBJECT (list), THEME_DATA, conv_data, g_free);
  g_object_set_data (G_OBJECT (list), GSETTINGS_SETTINGS, settings);
  g_object_set_data_full (G_OBJECT (list), GSETTINGS_KEY, g_strdup(key), g_free);

  /* select in treeview the theme set in gsettings */
  CtkTreeModel *treemodel;
  treemodel = ctk_tree_view_get_model (CTK_TREE_VIEW (list));
  gchar *theme = g_settings_get_string (settings, key);
  gchar *path = find_string_in_model (treemodel, theme, COL_NAME);
  if (path)
  {
    CtkTreeSelection *selection = ctk_tree_view_get_selection (CTK_TREE_VIEW (list));
    CtkTreePath *treepath = ctk_tree_path_new_from_string (path);
    ctk_tree_selection_select_path (selection, treepath);
    ctk_tree_view_scroll_to_cell (CTK_TREE_VIEW (list), treepath, NULL, FALSE, 0, 0);
    ctk_tree_path_free (treepath);
    g_free (path);
  }
  if (theme)
    g_free (theme);

  /* connect to gsettings change event */
  gchar *signal_name = g_strdup_printf("changed::%s", key);
  g_signal_connect (settings, signal_name,
      G_CALLBACK (treeview_gsettings_changed_callback), list);
  g_signal_connect (settings, signal_name, callback, data);
  g_free (signal_name);

  /* connect to treeview change event */
  g_signal_connect (ctk_tree_view_get_selection (CTK_TREE_VIEW (list)),
      "changed", G_CALLBACK (treeview_selection_changed_callback), list);
}

void
style_init (AppearanceData *data)
{
  CtkSettings *settings;
  CtkWidget *w;
  gchar *label;
  gint i;

  data->ctk_theme_icon = gdk_pixbuf_new_from_file (CAFECC_PIXMAP_DIR "/ctk-theme-thumbnailing.png", NULL);
  data->window_theme_icon = gdk_pixbuf_new_from_file (CAFECC_PIXMAP_DIR "/window-theme-thumbnailing.png", NULL);
  data->icon_theme_icon = gdk_pixbuf_new_from_file (CAFECC_PIXMAP_DIR "/icon-theme-thumbnailing.png", NULL);
  data->style_message_area = NULL;
  data->style_message_label = NULL;
  data->style_install_button = NULL;

  w = appearance_capplet_get_widget (data, "theme_details");
  g_signal_connect (w, "response", (GCallback) style_response_cb, NULL);
  g_signal_connect (w, "delete_event", (GCallback) ctk_true, NULL);

  prepare_list (data, appearance_capplet_get_widget (data, "window_themes_list"), THEME_TYPE_WINDOW, (GCallback) window_theme_changed);
  prepare_list (data, appearance_capplet_get_widget (data, "ctk_themes_list"), THEME_TYPE_CTK, (GCallback) ctk_theme_changed);
  prepare_list (data, appearance_capplet_get_widget (data, "icon_themes_list"), THEME_TYPE_ICON, (GCallback) icon_theme_changed);
  prepare_list (data, appearance_capplet_get_widget (data, "cursor_themes_list"), THEME_TYPE_CURSOR, (GCallback) cursor_theme_changed);

  window_theme_changed (data->croma_settings, CROMA_THEME_KEY, data);
  ctk_theme_changed (data->interface_settings, CTK_THEME_KEY, data);
  icon_theme_changed (data->interface_settings, ICON_THEME_KEY, data);
  cursor_theme_changed (data->mouse_settings, CURSOR_THEME_KEY, data);

  CtkNotebook *style_nb = CTK_NOTEBOOK (appearance_capplet_get_widget (data, "notebook2"));
  ctk_notebook_remove_page (style_nb, 1);

  w = appearance_capplet_get_widget (data, "color_scheme_message_hbox");
  ctk_widget_set_no_show_all (w, TRUE);

  w = appearance_capplet_get_widget (data, "color_scheme_defaults_button");
  ctk_button_set_image (CTK_BUTTON (w),
                        ctk_image_new_from_icon_name ("document-revert",
                                                      CTK_ICON_SIZE_BUTTON));

  settings = ctk_settings_get_default ();
  g_signal_connect (settings, "notify::ctk-color-scheme", (GCallback) color_scheme_changed, data);

  w = appearance_capplet_get_widget (data, "cursor_size_scale");
  g_signal_connect (w, "value-changed", (GCallback) cursor_size_scale_value_changed_cb, data);

  w = appearance_capplet_get_widget (data, "cursor_size_small_label");
  label = g_strdup_printf ("<small><i>%s</i></small>", ctk_label_get_text (CTK_LABEL (w)));
  ctk_label_set_markup (CTK_LABEL (w), label);
  g_free (label);

  w = appearance_capplet_get_widget (data, "cursor_size_large_label");
  label = g_strdup_printf ("<small><i>%s</i></small>", ctk_label_get_text (CTK_LABEL (w)));
  ctk_label_set_markup (CTK_LABEL (w), label);
  g_free (label);

  /* connect signals */
  /* color buttons */
  for (i = 0; i < NUM_SYMBOLIC_COLORS; ++i)
    g_signal_connect (appearance_capplet_get_widget (data, symbolic_names[i]), "color-set", (GCallback) color_button_clicked_cb, data);

  /* revert button */
  g_signal_connect (appearance_capplet_get_widget (data, "color_scheme_defaults_button"), "clicked", (GCallback) color_scheme_defaults_button_clicked_cb, data);
  /* delete buttons */
  g_signal_connect (appearance_capplet_get_widget (data, "ctk_themes_delete"), "clicked", (GCallback) ctk_theme_delete_cb, data);
  g_signal_connect (appearance_capplet_get_widget (data, "window_themes_delete"), "clicked", (GCallback) window_theme_delete_cb, data);
  g_signal_connect (appearance_capplet_get_widget (data, "icon_themes_delete"), "clicked", (GCallback) icon_theme_delete_cb, data);
  g_signal_connect (appearance_capplet_get_widget (data, "cursor_themes_delete"), "clicked", (GCallback) cursor_theme_delete_cb, data);

  update_message_area (data);
  cafe_theme_info_register_theme_change ((ThemeChangedCallback) changed_on_disk_cb, data);
}

void
style_shutdown (AppearanceData *data)
{
  if (data->ctk_theme_icon)
    g_object_unref (data->ctk_theme_icon);
  if (data->window_theme_icon)
    g_object_unref (data->window_theme_icon);
  if (data->icon_theme_icon)
    g_object_unref (data->icon_theme_icon);
}