summaryrefslogtreecommitdiff
path: root/capplets/theme-switcher/lister.c-42011
blob: 0ea737c459aa9013142de3acbf5bda9e6b9f430c (plain)
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
#include "da.h"

void
edit_file_to_use(gchar *file, gchar *theme)
{
  FILE *fin, *fout;
  gchar tmp[4096], buf[4096];
  gchar nextline = 0, hastheme = 0;
  
  srand(time(NULL));
  g_snprintf(tmp, sizeof(tmp), "/tmp/gtkrc_%i", rand());
  fout = fopen(tmp, "w");
  if (!fout)
    return;
  fin = fopen(file, "r");
  if (!fin)
    {
      fprintf(fout, "# -- THEME AUTO-WRITTEN DO NOT EDIT\n");
      fprintf(fout, "include \"%s\"\n\n", theme);
      cp(tmp, file);
      fclose(fout);
      return;
    }
  while (fgets(buf, sizeof(buf), fin))
    {
      if (!strcmp("# -- THEME AUTO-WRITTEN DO NOT EDIT\n", buf))
	hastheme = 1;
    }
  rewind(fin);
  if (!hastheme)
    {
      fprintf(fout, "# -- THEME AUTO-WRITTEN DO NOT EDIT\n");
      fprintf(fout, "include \"%s\"\n\n", theme);
      while (fgets(buf, sizeof(buf), fin))
	fprintf(fout, "%s", buf);
    }
  else
    {
      while (fgets(buf, sizeof(buf), fin))
	{
	  if (!nextline)
	    fprintf(fout, "%s", buf);
	  else
	    {
	      nextline = 0;
	      fprintf(fout, "include \"%s\"\n\n", theme);
	    }
	  if (!strcmp("# -- THEME AUTO-WRITTEN DO NOT EDIT\n", buf))
	    nextline = 1;
	}
    }
  fclose(fin);
  fclose(fout);
  cp(tmp, file);
  rm(tmp);
}

void 
set_tmp_rc()
{
  gchar s[4096], *home;
  
  home = getenv("HOME");
  if (!home)
    return;
  g_snprintf(s, sizeof(s), "%s/.gnome/gtkrc", home);
  srand(time(NULL));
  g_snprintf(gtkrc_tmp, sizeof(gtkrc_tmp), "/tmp/%i-gtkrc-%i", time(NULL), rand());
  cp(s, gtkrc_tmp);
}

void
use_theme(gchar *theme)
{
  gchar s[4096], *home;
  
  home = getenv("HOME");
  if (!home)
    return;
  g_snprintf(s, sizeof(s), "%s/.gnome/gtkrc", home);
  edit_file_to_use(s, theme);
}

void
test_theme(gchar *theme)
{
  edit_file_to_use(gtkrc_tmp, theme);
}

void
free_theme_list(ThemeEntry *list, gint number)
{
  gint i;
  
  for(i = 0; i < number; i++)
    {
      g_free(list[i].name);
      g_free(list[i].rc);
      g_free(list[i].readme);
      g_free(list[i].icon);
    }
  g_free(list);
}

ThemeEntry *
list_themes(gchar *dir, gint *number)
{
  gchar **dir_listing = NULL, tmp[4096];
  ThemeEntry *list = NULL;
  gint  i = 0, j = 0, num = 0;
  
  dir_listing = ls(dir, &num);
  for(i = 0; i < num; i++)
    {
      g_snprintf(tmp, sizeof(tmp), "%s/%s/gtk/gtkrc", dir, dir_listing[i]);
      if (isfile(tmp))
	{
	  list = g_realloc(list, sizeof(ThemeEntry) * ++j);
	  list[j - 1].name = g_strdup(dir_listing[i]);
	  list[j - 1].rc = g_strdup(tmp);
	  g_snprintf(tmp, sizeof(tmp), "%s/%s", dir, dir_listing[i]);
	  list[j - 1].dir = g_strdup(tmp);
	  g_snprintf(tmp, sizeof(tmp), "%s/%s/README.html", dir, dir_listing[i]);
	  list[j - 1].readme = g_strdup(tmp);
	  g_snprintf(tmp, sizeof(tmp), "%s/%s/ICON.png", dir, dir_listing[i]);
	  list[j - 1].icon = g_strdup(tmp);
	}
    }
  freestrlist(dir_listing, num);
  *number = j;
  return list;
}

ThemeEntry *
list_system_themes(gint *number)
{
  gchar *theme_dir = NULL;
  ThemeEntry *list  = NULL;
  
  theme_dir = gtk_rc_get_theme_dir();
  list = list_themes(theme_dir, number);
  g_free(theme_dir);
  return list;
}

ThemeEntry *
list_user_themes(gint *number)
{
  gchar *home = NULL;
  gchar *theme_dir = NULL;
  ThemeEntry *list  = NULL;
  
  home = getenv("HOME");
  if (!home)
    return NULL;

  if (!isdir(home))
    return NULL;
  
  theme_dir = g_malloc(strlen(home) + strlen("/.themes") + 1);
  sprintf(theme_dir, "%s%s", home, "/.themes");
  list = list_themes(theme_dir, number);
  g_free(theme_dir);
  return list;
}