1 /*
2  * themes.c
3  * vim: expandtab:ts=4:sts=4:sw=4
4  *
5  * Copyright (C) 2012 - 2019 James Booth <boothj5@gmail.com>
6  *
7  * This file is part of Profanity.
8  *
9  * Profanity is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * Profanity is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with Profanity.  If not, see <https://www.gnu.org/licenses/>.
21  *
22  * In addition, as a special exception, the copyright holders give permission to
23  * link the code of portions of this program with the OpenSSL library under
24  * certain conditions as described in each individual source file, and
25  * distribute linked combinations including the two.
26  *
27  * You must obey the GNU General Public License in all respects for all of the
28  * code used other than OpenSSL. If you modify file(s) with this exception, you
29  * may extend this exception to your version of the file(s), but you are not
30  * obligated to do so. If you do not wish to do so, delete this exception
31  * statement from your version. If you delete this exception statement from all
32  * source files in the program, then also delete it here.
33  *
34  */
35 
36 #include "config.h"
37 
38 #include <stdlib.h>
39 
40 #include <glib.h>
41 #include <glib/gstdio.h>
42 
43 #include "common.h"
44 #include "config/theme.h"
45 #include "config/files.h"
46 
47 static GKeyFile* themes;
48 
49 void
plugin_themes_init(void)50 plugin_themes_init(void)
51 {
52     char* themes_file = files_get_data_path(FILE_PLUGIN_THEMES);
53 
54     if (g_file_test(themes_file, G_FILE_TEST_EXISTS)) {
55         g_chmod(themes_file, S_IRUSR | S_IWUSR);
56     }
57 
58     themes = g_key_file_new();
59     g_key_file_load_from_file(themes, themes_file, G_KEY_FILE_KEEP_COMMENTS, NULL);
60 
61     gsize g_data_size;
62     gchar* g_data = g_key_file_to_data(themes, &g_data_size, NULL);
63     g_file_set_contents(themes_file, g_data, g_data_size, NULL);
64     g_chmod(themes_file, S_IRUSR | S_IWUSR);
65     g_free(g_data);
66     free(themes_file);
67 }
68 
69 void
plugin_themes_close(void)70 plugin_themes_close(void)
71 {
72     g_key_file_free(themes);
73     themes = NULL;
74 }
75 
76 theme_item_t
plugin_themes_get(const char * const group,const char * const key,const char * const def)77 plugin_themes_get(const char* const group, const char* const key, const char* const def)
78 {
79     if (group && key && g_key_file_has_key(themes, group, key, NULL)) {
80         gchar* result = g_key_file_get_string(themes, group, key, NULL);
81 
82         theme_item_t ret;
83 
84         if (g_strcmp0(result, "white") == 0)
85             ret = THEME_WHITE;
86         else if (g_strcmp0(result, "bold_white") == 0)
87             ret = THEME_WHITE_BOLD;
88         else if (g_strcmp0(result, "red") == 0)
89             ret = THEME_RED;
90         else if (g_strcmp0(result, "bold_red") == 0)
91             ret = THEME_RED_BOLD;
92         else if (g_strcmp0(result, "green") == 0)
93             ret = THEME_GREEN;
94         else if (g_strcmp0(result, "bold_green") == 0)
95             ret = THEME_GREEN_BOLD;
96         else if (g_strcmp0(result, "blue") == 0)
97             ret = THEME_BLUE;
98         else if (g_strcmp0(result, "bold_blue") == 0)
99             ret = THEME_BLUE_BOLD;
100         else if (g_strcmp0(result, "yellow") == 0)
101             ret = THEME_YELLOW;
102         else if (g_strcmp0(result, "bold_yellow") == 0)
103             ret = THEME_YELLOW_BOLD;
104         else if (g_strcmp0(result, "cyan") == 0)
105             ret = THEME_CYAN;
106         else if (g_strcmp0(result, "bold_cyan") == 0)
107             ret = THEME_CYAN_BOLD;
108         else if (g_strcmp0(result, "magenta") == 0)
109             ret = THEME_MAGENTA;
110         else if (g_strcmp0(result, "bold_magenta") == 0)
111             ret = THEME_MAGENTA_BOLD;
112         else if (g_strcmp0(result, "black") == 0)
113             ret = THEME_BLACK;
114         else if (g_strcmp0(result, "bold_black") == 0)
115             ret = THEME_BLACK_BOLD;
116 
117         else if (g_strcmp0(def, "white") == 0)
118             ret = THEME_WHITE;
119         else if (g_strcmp0(def, "bold_white") == 0)
120             ret = THEME_WHITE_BOLD;
121         else if (g_strcmp0(def, "red") == 0)
122             ret = THEME_RED;
123         else if (g_strcmp0(def, "bold_red") == 0)
124             ret = THEME_RED_BOLD;
125         else if (g_strcmp0(def, "green") == 0)
126             ret = THEME_GREEN;
127         else if (g_strcmp0(def, "bold_green") == 0)
128             ret = THEME_GREEN_BOLD;
129         else if (g_strcmp0(def, "blue") == 0)
130             ret = THEME_BLUE;
131         else if (g_strcmp0(def, "bold_blue") == 0)
132             ret = THEME_BLUE_BOLD;
133         else if (g_strcmp0(def, "yellow") == 0)
134             ret = THEME_YELLOW;
135         else if (g_strcmp0(def, "bold_yellow") == 0)
136             ret = THEME_YELLOW_BOLD;
137         else if (g_strcmp0(def, "cyan") == 0)
138             ret = THEME_CYAN;
139         else if (g_strcmp0(def, "bold_cyan") == 0)
140             ret = THEME_CYAN_BOLD;
141         else if (g_strcmp0(def, "magenta") == 0)
142             ret = THEME_MAGENTA;
143         else if (g_strcmp0(def, "bold_magenta") == 0)
144             ret = THEME_MAGENTA_BOLD;
145         else if (g_strcmp0(def, "black") == 0)
146             ret = THEME_BLACK;
147         else if (g_strcmp0(def, "bold_black") == 0)
148             ret = THEME_BLACK_BOLD;
149 
150         else
151             ret = THEME_TEXT;
152 
153         g_free(result);
154 
155         return ret;
156 
157     } else {
158         if (g_strcmp0(def, "white") == 0)
159             return THEME_WHITE;
160         else if (g_strcmp0(def, "bold_white") == 0)
161             return THEME_WHITE_BOLD;
162         else if (g_strcmp0(def, "red") == 0)
163             return THEME_RED;
164         else if (g_strcmp0(def, "bold_red") == 0)
165             return THEME_RED_BOLD;
166         else if (g_strcmp0(def, "green") == 0)
167             return THEME_GREEN;
168         else if (g_strcmp0(def, "bold_green") == 0)
169             return THEME_GREEN_BOLD;
170         else if (g_strcmp0(def, "blue") == 0)
171             return THEME_BLUE;
172         else if (g_strcmp0(def, "bold_blue") == 0)
173             return THEME_BLUE_BOLD;
174         else if (g_strcmp0(def, "yellow") == 0)
175             return THEME_YELLOW;
176         else if (g_strcmp0(def, "bold_yellow") == 0)
177             return THEME_YELLOW_BOLD;
178         else if (g_strcmp0(def, "cyan") == 0)
179             return THEME_CYAN;
180         else if (g_strcmp0(def, "bold_cyan") == 0)
181             return THEME_CYAN_BOLD;
182         else if (g_strcmp0(def, "magenta") == 0)
183             return THEME_MAGENTA;
184         else if (g_strcmp0(def, "bold_magenta") == 0)
185             return THEME_MAGENTA_BOLD;
186         else if (g_strcmp0(def, "black") == 0)
187             return THEME_BLACK;
188         else if (g_strcmp0(def, "bold_black") == 0)
189             return THEME_BLACK_BOLD;
190 
191         else
192             return THEME_TEXT;
193     }
194 }
195