1 /*
2  * Copyright (C) 2017-2021 Graeme Gott <graeme@gottcode.org>
3  *
4  * This library is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this library.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "element.h"
19 
20 #include <libxfce4ui/libxfce4ui.h>
21 
22 using namespace WhiskerMenu;
23 
24 //-----------------------------------------------------------------------------
25 
set_icon(const gchar * icon,bool use_fallbacks)26 void Element::set_icon(const gchar* icon, bool use_fallbacks)
27 {
28 	if (m_icon)
29 	{
30 		g_object_unref(m_icon);
31 		m_icon = nullptr;
32 	}
33 
34 	if (G_UNLIKELY(!icon))
35 	{
36 		return;
37 	}
38 
39 	auto themed_icon_new = use_fallbacks ? &g_themed_icon_new_with_default_fallbacks : &g_themed_icon_new;
40 
41 	if (!g_path_is_absolute(icon))
42 	{
43 		const gchar* pos = g_strrstr(icon, ".");
44 		if (!pos)
45 		{
46 			m_icon = themed_icon_new(icon);
47 		}
48 		else
49 		{
50 			gchar* suffix = g_utf8_casefold(pos, -1);
51 			if ((g_strcmp0(suffix, ".png") == 0)
52 					|| (g_strcmp0(suffix, ".xpm") == 0)
53 					|| (g_strcmp0(suffix, ".svg") == 0)
54 					|| (g_strcmp0(suffix, ".svgz") == 0))
55 			{
56 				gchar* name = g_strndup(icon, pos - icon);
57 				m_icon = themed_icon_new(name);
58 				g_free(name);
59 			}
60 			else
61 			{
62 				m_icon = themed_icon_new(icon);
63 			}
64 			g_free(suffix);
65 		}
66 	}
67 	else
68 	{
69 		GFile* file = g_file_new_for_path(icon);
70 		m_icon = g_file_icon_new(file);
71 		g_object_unref(file);
72 	}
73 }
74 
75 //-----------------------------------------------------------------------------
76 
spawn(GdkScreen * screen,const gchar * command,const gchar * working_directory,gboolean startup_notify,const gchar * icon_name) const77 void Element::spawn(GdkScreen* screen, const gchar* command, const gchar* working_directory, gboolean startup_notify, const gchar* icon_name) const
78 {
79 	GError* error = nullptr;
80 	bool result = false;
81 
82 	gchar** argv;
83 	if (g_shell_parse_argv(command, nullptr, &argv, &error))
84 	{
85 #if LIBXFCE4UI_CHECK_VERSION(4,15,6)
86 		result = xfce_spawn(screen,
87 #else
88 		result = xfce_spawn_on_screen(screen,
89 #endif
90 				working_directory,
91 				argv,
92 				nullptr,
93 				G_SPAWN_SEARCH_PATH,
94 				startup_notify,
95 				gtk_get_current_event_time(),
96 				icon_name,
97 #if LIBXFCE4UI_CHECK_VERSION(4,15,6)
98 				true,
99 #endif
100 				&error);
101 		g_strfreev(argv);
102 	}
103 
104 	if (!result)
105 	{
106 		xfce_dialog_show_error(nullptr, error, _("Failed to execute command \"%s\"."), command);
107 		g_error_free(error);
108 	}
109 }
110 
111 //-----------------------------------------------------------------------------
112