summaryrefslogtreecommitdiff
path: root/src/bin/e_theme.c
blob: 0d8bf85f523f2e4c1b953fe35fc3aef378e88762 (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
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
#include "e.h"

static void       e_theme_handler_set(void *data EINA_UNUSED, Evas_Object *obj EINA_UNUSED, const char *path);
static int        e_theme_handler_test(void *data EINA_UNUSED, Evas_Object *obj EINA_UNUSED, const char *path);

static E_Fm2_Mime_Handler *theme_hdl = NULL;

/* externally accessible functions */

EINTERN int
e_theme_init(void)
{
   /* Register mime handler */
   theme_hdl = e_fm2_mime_handler_new(_("Set As Theme"), "preferences-desktop-theme",
                                      e_theme_handler_set, NULL,
                                      e_theme_handler_test, NULL);
   if (theme_hdl) e_fm2_mime_handler_glob_add(theme_hdl, "*.edj");
   return 1;
}

EINTERN int
e_theme_shutdown(void)
{
   if (theme_hdl)
     {
        e_fm2_mime_handler_glob_del(theme_hdl, "*.edj");
        e_fm2_mime_handler_free(theme_hdl);
     }
   return 1;
}

E_API Eina_List *
e_theme_collection_items_find(const char *base EINA_UNUSED, const char *collname)
{
   Eina_List *list, *list2 = NULL, *l;
   const char *s, *s2;
   size_t len = strlen(collname);

   list = elm_theme_group_base_list(NULL, collname);
   if (!list) return NULL;
   EINA_LIST_FREE(list, s)
     {
        char *trans, *p, *p2;
        size_t slen;

        slen = strlen(s);
        if (slen <= len) goto done;
        trans = alloca(slen + 1);
        if (!trans) goto done;
        memcpy(trans, s, slen + 1);
        p = trans + len;
        if (*p != '/') goto done;
        p++;
        if (*p)
          {
             p2 = strchr(p, '/');
             if (p2) *p2 = 0;
             EINA_LIST_FOREACH(list2, l, s2)
               {
                  if (!strcmp(s2, p)) break;
               }
             if (!l) list2 = eina_list_append(list2, eina_stringshare_add(p));
          }
done:
        eina_stringshare_del(s);
     }
   return list2;
}

E_API int
e_theme_edje_object_set(Evas_Object *o, const char *category EINA_UNUSED, const char *group)
{
   const char *file;

   file = elm_theme_group_path_find(NULL, group);
   if (!file) return 0;
   edje_object_file_set(o, file, group);
   return 1;
}

E_API const char *
e_theme_edje_file_get(const char *category EINA_UNUSED, const char *group)
{
   const char *file = elm_theme_group_path_find(NULL, group);
   if (!file) return "";
   return file;
}

E_API const char *
e_theme_edje_icon_fallback_file_get(const char *group)
{
   const char *file;

   if ((e_config->icon_theme) && (!strncmp(group, "e/icons", 7))) return "";
   file = elm_theme_group_path_find(NULL, group);
   if (!file) return "";
   return file;
}

E_API int
e_theme_transition_find(const char *transition)
{
   Eina_List *trans = NULL;
   int found = 0;
   const char *str;

   trans = e_theme_collection_items_find(NULL, "e/transitions");
   if (eina_list_search_sorted(trans, EINA_COMPARE_CB(strcmp), transition))
     found = 1;
   EINA_LIST_FREE(trans, str) eina_stringshare_del(str);
   return found;
}

E_API Eina_List *
e_theme_transition_list(void)
{
   return e_theme_collection_items_find(NULL, "e/transitions");
}

E_API int
e_theme_border_find(const char *border)
{
   Eina_List *bds = NULL;
   int found = 0;
   const char *str;

   bds = e_theme_collection_items_find(NULL, "e/widgets/border");
   if (eina_list_search_sorted(bds, EINA_COMPARE_CB(strcmp), border))
     found = 1;
   EINA_LIST_FREE(bds, str) eina_stringshare_del(str);
   return found;
}

E_API Eina_List *
e_theme_border_list(void)
{
   return e_theme_collection_items_find(NULL, "e/widgets/border");
}

E_API int
e_theme_shelf_find(const char *shelf)
{
   Eina_List *shelfs = NULL;
   int found = 0;
   const char *str;

   shelfs = e_theme_collection_items_find(NULL, "e/shelf");
   if (eina_list_search_sorted(shelfs, EINA_COMPARE_CB(strcmp), shelf))
     found = 1;
   EINA_LIST_FREE(shelfs, str) eina_stringshare_del(str);
   return found;
}

E_API Eina_List *
e_theme_shelf_list(void)
{
   return e_theme_collection_items_find(NULL, "e/shelf");
}

E_API int
e_theme_comp_frame_find(const char *comp)
{
   Eina_List *comps = NULL;
   int found = 0;
   const char *str;

   comps = e_theme_collection_items_find(NULL, "e/comp/frame");
   if (eina_list_search_sorted(comps, EINA_COMPARE_CB(strcmp), comp))
     found = 1;
   EINA_LIST_FREE(comps, str) eina_stringshare_del(str);
   return found;
}

E_API Eina_List *
e_theme_comp_frame_list(void)
{
   return e_theme_collection_items_find(NULL, "e/comp/frame");
}

/* local subsystem functions */
static void
e_theme_handler_set(void *data EINA_UNUSED, Evas_Object *obj EINA_UNUSED, const char *path)
{
   E_Action *a;
   char buf[PATH_MAX];
   int copy = 1;

   if (!path) return;
   /* if not in system dir or user dir, copy to user dir */
   snprintf(buf, sizeof(buf), "%s", elm_theme_system_dir_get());
   if (!strncmp(buf, path, strlen(buf))) copy = 0;
   if (copy)
     {
        snprintf(buf, sizeof(buf), "%s", elm_theme_user_dir_get());
        if (!strncmp(buf, path, strlen(buf))) copy = 0;
     }
   if (copy)
     {
        const char *file;
        char *name;

        file = ecore_file_file_get(path);
        name = ecore_file_strip_ext(file);

        if (name)
          {
             double tim = ecore_time_unix_get();

             snprintf(buf, sizeof(buf), "%s/%s-%f.edj",
                      elm_theme_user_dir_get(), name, tim);
             if (!ecore_file_exists(buf))
               {
                  ecore_file_cp(path, buf);
                  snprintf(buf, sizeof(buf), "%s-%f", name, tim);
                  elm_theme_set(NULL, buf);
               }
             else
               elm_theme_set(NULL, path);
             elm_config_all_flush();
             elm_config_save();
             free(name);
          }
     }
   else
     {
        const char *file;
        char *name;

        file = ecore_file_file_get(path);
        name = ecore_file_strip_ext(file);
        if (name)
          {
             elm_theme_set(NULL, name);
             elm_config_all_flush();
             elm_config_save();
             free(name);
          }
     }

   e_config_save_queue();
   a = e_action_find("restart");
   if ((a) && (a->func.go)) a->func.go(NULL, NULL);
}

static int
e_theme_handler_test(void *data EINA_UNUSED, Evas_Object *obj EINA_UNUSED, const char *path)
{
   if (!path) return 0;
   if (!edje_file_group_exists(path, "e/widgets/border/default/border"))
     return 0;
   return 1;
}