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
253
254
255
|
/*
* This file is part of libslab.
*
* Copyright (c) 2006 Novell, Inc.
*
* Libslab is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* Libslab is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
* more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with libslab; if not, write to the Free Software Foundation, Inc., 51
* Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "slab-mate-util.h"
#include "libslab-utils.h"
#include <gio/gio.h>
#include <string.h>
MateDesktopItem *
load_desktop_item_from_unknown (const gchar *id)
{
MateDesktopItem *item;
gchar *basename;
GError *error = NULL;
item = mate_desktop_item_new_from_uri (id, 0, &error);
if (! error)
return item;
else {
g_error_free (error);
error = NULL;
}
item = mate_desktop_item_new_from_file (id, 0, &error);
if (! error)
return item;
else {
g_error_free (error);
error = NULL;
}
item = mate_desktop_item_new_from_basename (id, 0, &error);
if (! error)
return item;
else {
g_error_free (error);
error = NULL;
}
basename = g_strrstr (id, "/");
if (basename) {
basename++;
item = mate_desktop_item_new_from_basename (basename, 0, &error);
if (! error)
return item;
else {
g_error_free (error);
error = NULL;
}
}
return NULL;
}
gboolean
open_desktop_item_exec (MateDesktopItem * desktop_item)
{
GError *error = NULL;
if (!desktop_item)
return FALSE;
mate_desktop_item_launch (desktop_item, NULL, MATE_DESKTOP_ITEM_LAUNCH_ONLY_ONE | MATE_DESKTOP_ITEM_LAUNCH_DO_NOT_REAP_CHILD, &error);
if (error)
{
g_warning ("error launching %s [%s]\n",
mate_desktop_item_get_location (desktop_item), error->message);
g_error_free (error);
return FALSE;
}
return TRUE;
}
gboolean
open_desktop_item_help (MateDesktopItem * desktop_item)
{
const gchar *doc_path;
gchar *help_uri;
GError *error;
if (!desktop_item)
return FALSE;
doc_path = mate_desktop_item_get_string (desktop_item, "DocPath");
if (doc_path)
{
help_uri = g_strdup_printf ("help:%s", doc_path);
error = NULL;
#if GTK_CHECK_VERSION (3, 22, 0)
if (!gtk_show_uri_on_window (NULL, help_uri, gtk_get_current_event_time (), &error))
#else
if (!gtk_show_uri (libslab_get_current_screen (), help_uri, gtk_get_current_event_time (), &error))
#endif
{
g_warning ("error opening %s [%s]\n", help_uri, error->message);
g_free (help_uri);
g_error_free (error);
return FALSE;
}
g_free (help_uri);
}
else
return FALSE;
return TRUE;
}
gboolean
slab_load_image (GtkImage * image, GtkIconSize size, const gchar * image_id)
{
GdkPixbuf *pixbuf;
gint width;
gint height;
gchar *id;
if (!image_id)
return FALSE;
id = g_strdup (image_id);
gtk_icon_size_lookup (size, &width, &height);
if (g_path_is_absolute (id))
pixbuf = gdk_pixbuf_new_from_file_at_size (id, width, height, NULL);
else
{
if ( /* file extensions are not copesetic with loading by "name" */
g_str_has_suffix (id, ".png") ||
g_str_has_suffix (id, ".svg") ||
g_str_has_suffix (id, ".xpm")
)
id[strlen (id) - 4] = '\0';
pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (), id, width, 0,
NULL);
}
if (pixbuf)
{
gtk_image_set_from_pixbuf (image, pixbuf);
g_object_unref (pixbuf);
g_free (id);
return TRUE;
}
else
{ /* This will make it show the "broken image" icon */
gtk_image_set_from_file (image, id);
g_free (id);
return FALSE;
}
}
gchar *
string_replace_once (const gchar * str_template, const gchar * key, const gchar * value)
{
GString *str_built;
gint pivot;
pivot = strstr (str_template, key) - str_template;
str_built = g_string_new_len (str_template, pivot);
g_string_append (str_built, value);
g_string_append (str_built, &str_template[pivot + strlen (key)]);
return g_string_free (str_built, FALSE);
}
void
spawn_process (const gchar *command)
{
gchar **argv;
GError *error = NULL;
if (!command || strlen (command) < 1)
return;
argv = g_strsplit (command, " ", -1);
g_spawn_async (NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, &error);
if (error)
{
g_warning ("error spawning [%s]: [%s]\n", command, error->message);
g_error_free (error);
}
g_strfreev (argv);
}
void
copy_file (const gchar * src_uri, const gchar * dst_uri)
{
GFile *src;
GFile *dst;
GError *error = NULL;
gboolean res;
src = g_file_new_for_uri (src_uri);
dst = g_file_new_for_uri (dst_uri);
res = g_file_copy (src, dst,
G_FILE_COPY_NONE,
NULL, NULL, NULL, &error);
if (!res)
{
g_warning ("error copying [%s] to [%s]: %s.", src_uri, dst_uri, error->message);
g_error_free (error);
}
g_object_unref (src);
g_object_unref (dst);
}
|