1 /*
2  * Copyright (C) 2012 Stefano Karapetsas
3  * Copyright (C) 2012-2021 MATE Developers
4  * Authors: Stefano Karapetsas <stefano@karapetsas.com>
5  * All Rights Reserved
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 #include "appearance.h"
23 #include "wm-common.h"
24 #include "appearance-support.h"
25 
26 #include <glib.h>
27 #include <gio/gio.h>
28 
29 static gboolean
is_program_in_path(const char * program)30 is_program_in_path (const char *program)
31 {
32     char *tmp = g_find_program_in_path (program);
33     if (tmp != NULL)
34     {
35         g_free (tmp);
36         return TRUE;
37     }
38     else
39     {
40         return FALSE;
41     }
42 }
43 
44 static gboolean
metacity_is_running(void)45 metacity_is_running(void)
46 {
47     gboolean is_running = FALSE;
48     gchar *current_wm = NULL;
49 
50     current_wm = wm_common_get_current_window_manager ();
51 
52     is_running = (g_strcmp0(current_wm, WM_COMMON_METACITY) == 0) ||
53                  (g_strcmp0(current_wm, WM_COMMON_COMPIZ_OLD) == 0) ||
54                  (g_strcmp0(current_wm, WM_COMMON_COMPIZ) == 0);
55 
56     g_free (current_wm);
57 
58     return is_running;
59 }
60 
61 static void
metacity_theme_apply(const gchar * theme,const gchar * font)62 metacity_theme_apply(const gchar *theme, const gchar *font)
63 {
64     /* set theme, we use gconf and gsettings binaries to avoid schemas and versions issues */
65     if (is_program_in_path ("gconftool-2"))
66     {
67         gchar *gconf_cmd = NULL;
68 
69         gconf_cmd = g_strdup_printf("gconftool-2 --set --type string /apps/metacity/general/theme '%s'", theme);
70         g_spawn_command_line_async (gconf_cmd, NULL);
71         g_free (gconf_cmd);
72 
73         gconf_cmd = g_strdup_printf("gconftool-2 --set --type string /apps/metacity/general/titlebar_font '%s'", font);
74         g_spawn_command_line_async (gconf_cmd, NULL);
75         g_free (gconf_cmd);
76     }
77 
78     if (is_program_in_path ("gsettings"))
79     {
80         gchar *gsettings_cmd = NULL;
81 
82         /* for GNOME3 */
83         gsettings_cmd = g_strdup_printf("gsettings set org.gnome.desktop.wm.preferences theme '%s'", theme);
84         g_spawn_command_line_async (gsettings_cmd, NULL);
85         g_free (gsettings_cmd);
86 
87         gsettings_cmd = g_strdup_printf("gsettings set org.gnome.desktop.wm.preferences titlebar-font '%s'", font);
88         g_spawn_command_line_async (gsettings_cmd, NULL);
89         g_free (gsettings_cmd);
90 
91         /* for metacity >= 3.16 */
92         gsettings_cmd = g_strdup_printf("gsettings set org.gnome.metacity theme '%s'", theme);
93         g_spawn_command_line_async (gsettings_cmd, NULL);
94         g_free (gsettings_cmd);
95 
96         /* for metacity >= 3.20 */
97         gsettings_cmd = g_strdup_printf("gsettings set org.gnome.metacity.theme name '%s'", theme);
98         g_spawn_command_line_async (gsettings_cmd, NULL);
99         g_free (gsettings_cmd);
100     }
101 }
102 
103 static void
marco_theme_changed(GSettings * settings,gchar * key,AppearanceData * data)104 marco_theme_changed(GSettings *settings, gchar *key, AppearanceData* data)
105 {
106     gchar *theme = NULL;
107     gchar *font = NULL;
108     if (metacity_is_running ())
109     {
110         theme = g_settings_get_string (settings, MARCO_THEME_KEY);
111         font = g_settings_get_string (settings, WINDOW_TITLE_FONT_KEY);
112         metacity_theme_apply (theme, font);
113         g_free (theme);
114         g_free (font);
115     }
116 }
117 
118 void
support_init(AppearanceData * data)119 support_init(AppearanceData* data)
120 {
121     /* needed for wm_common_get_current_window_manager() */
122     wm_common_update_window ();
123     /* GSettings signals */
124     g_signal_connect (data->marco_settings, "changed::" MARCO_THEME_KEY,
125                       G_CALLBACK (marco_theme_changed), data);
126     g_signal_connect (data->marco_settings, "changed::" WINDOW_TITLE_FONT_KEY,
127                       G_CALLBACK (marco_theme_changed), data);
128     /* apply theme at start */
129     if (metacity_is_running ())
130         marco_theme_changed (data->marco_settings, NULL, data);
131 }
132 
133 void
support_shutdown(AppearanceData * data)134 support_shutdown(AppearanceData* data)
135 {
136     /* nothing to do */
137 }
138