File: panel-keyfile.c

package info (click to toggle)
gnome-panel 3.58.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 29,616 kB
  • sloc: ansic: 74,929; sh: 5,496; xml: 2,760; makefile: 1,368; sed: 39; python: 3
file content (199 lines) | stat: -rw-r--r-- 4,774 bytes parent folder | download | duplicates (3)
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
/*
 * panel-keyfile.c: GKeyFile extensions
 *
 * Copyright (C) 2008 Novell, Inc.
 *
 * Based on code from panel-util.c (there was no copyright header at the time)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *	Vincent Untz <vuntz@gnome.org>
 */

#include <string.h>
#include <sys/stat.h>

#include <glib.h>
#include <gio/gio.h>

#include "panel-keyfile.h"

#define KEYFILE_TRUSTED_SHEBANG "#!/usr/bin/env xdg-open\n"

GKeyFile *
panel_key_file_new_desktop (void)
{
	GKeyFile *retval;

	retval = g_key_file_new ();

	//FIXME? g_key_file_set_string (retval, G_KEY_FILE_DESKTOP_GROUP, "Name", _("No Name"));
	g_key_file_set_string (retval, G_KEY_FILE_DESKTOP_GROUP, "Version", "1.0");

	return retval;
}

static void
_panel_key_file_make_executable (const gchar *path)
{
	GFile     *file;
	GFileInfo *info;
	guint32    current_perms;
	guint32    new_perms;

	file = g_file_new_for_path (path);

	info = g_file_query_info (file,
				  G_FILE_ATTRIBUTE_STANDARD_TYPE","
				  G_FILE_ATTRIBUTE_UNIX_MODE,
				  G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
				  NULL,
				  NULL);

	if (info == NULL) {
		g_warning ("Cannot mark %s executable", path);
		g_object_unref (file);
		return;
	}

	if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_UNIX_MODE)) {
		current_perms = g_file_info_get_attribute_uint32 (info,
								  G_FILE_ATTRIBUTE_UNIX_MODE);
		new_perms = current_perms | S_IXGRP | S_IXUSR | S_IXOTH;
		if ((current_perms != new_perms) &&
		    !g_file_set_attribute_uint32 (file,
			    			  G_FILE_ATTRIBUTE_UNIX_MODE,
						  new_perms,
						  G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
						  NULL, NULL))
			g_warning ("Cannot mark %s executable", path);
	}

	g_object_unref (info);
	g_object_unref (file);
}

//FIXME: kill this when bug #309224 is fixed
gboolean
panel_key_file_to_file (GKeyFile     *keyfile,
			const gchar  *file,
			GError      **error)
{
	gchar   *filename;
	GError  *write_error;
	gchar   *data;
	gsize    length;
	gboolean res;

	g_return_val_if_fail (keyfile != NULL, FALSE);
	g_return_val_if_fail (file != NULL, FALSE);

	write_error = NULL;
	data = g_key_file_to_data (keyfile, &length, &write_error);
	if (write_error) {
		g_propagate_error (error, write_error);
		return FALSE;
	}

	if (!g_path_is_absolute (file))
		filename = g_filename_from_uri (file, NULL, &write_error);
	else
		filename = g_filename_from_utf8 (file, -1, NULL, NULL,
						 &write_error);

	if (write_error) {
		g_propagate_error (error, write_error);
		g_free (filename);
		g_free (data);
		return FALSE;
	}

	if (!g_str_has_prefix (data, "#!")) {
		gchar *new_data;
		gsize  new_length;

		new_length = length + strlen (KEYFILE_TRUSTED_SHEBANG);
		new_data = g_malloc (new_length);

		strcpy (new_data, KEYFILE_TRUSTED_SHEBANG);
		memcpy (new_data + strlen (KEYFILE_TRUSTED_SHEBANG),
			data, length);

		g_free (data);
		data = new_data;
		length = new_length;
	}

	res = g_file_set_contents (filename, data, length, &write_error);

	if (write_error) {
		g_propagate_error (error, write_error);
		g_free (data);
		g_free (filename);
		return FALSE;
	}

	g_free (data);

	_panel_key_file_make_executable (filename);
	g_free (filename);

	return res;
}

gboolean
panel_key_file_get_boolean (GKeyFile    *keyfile,
			    const gchar *key,
			    gboolean     default_value)
{
	GError   *error;
	gboolean  retval;

	error = NULL;
	retval = g_key_file_get_boolean (keyfile, G_KEY_FILE_DESKTOP_GROUP, key, &error);
	if (error != NULL) {
		retval = default_value;
		g_error_free (error);
	}

	return retval;
}

void
panel_key_file_set_locale_string (GKeyFile    *keyfile,
				  const gchar *key,
				  const gchar *value)
{
	const char         *locale;
	const char * const *langs_pointer;
	int                 i;

	locale = NULL;
	langs_pointer = g_get_language_names ();
	for (i = 0; langs_pointer[i] != NULL; i++) {
		/* find first without encoding  */
		if (strchr (langs_pointer[i], '.') == NULL) {
			locale = langs_pointer[i]; 
			break;
		}
	}

	if (locale)
		g_key_file_set_locale_string (keyfile, G_KEY_FILE_DESKTOP_GROUP,
					      key, locale, value);
	else
		g_key_file_set_string (keyfile, G_KEY_FILE_DESKTOP_GROUP,
				       key, value);
}