gnome-panel r11568 - in trunk/gnome-panel: . libpanel-util



Author: vuntz
Date: Sun Mar 15 13:06:30 2009
New Revision: 11568
URL: http://svn.gnome.org/viewvc/gnome-panel?rev=11568&view=rev

Log:
2009-03-15  Vincent Untz  <vuntz gnome org>

	Mark desktop files as trusted when it makes sense.

	* libpanel-util/panel-keyfile.[ch]: (_panel_key_file_make_executable):
	new, mark a file as executable
	(panel_key_file_to_file): prepend a #! header to the desktop file if
	there's none, and mark the file as executable
	(panel_key_file_copy_and_mark_trusted): new, does what it says :-)
	* menu.c: (add_app_to_desktop): update to use
	panel_key_file_copy_and_mark_trusted() instead of g_file_copy() so that
	the copied file is marked as trusted


Modified:
   trunk/gnome-panel/ChangeLog
   trunk/gnome-panel/libpanel-util/panel-keyfile.c
   trunk/gnome-panel/libpanel-util/panel-keyfile.h
   trunk/gnome-panel/menu.c

Modified: trunk/gnome-panel/libpanel-util/panel-keyfile.c
==============================================================================
--- trunk/gnome-panel/libpanel-util/panel-keyfile.c	(original)
+++ trunk/gnome-panel/libpanel-util/panel-keyfile.c	Sun Mar 15 13:06:30 2009
@@ -25,12 +25,15 @@
  */
 
 #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)
 {
@@ -45,6 +48,46 @@
 	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,
@@ -79,16 +122,36 @@
 		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);
-	g_free (filename);
 
 	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;
 }
 
@@ -144,6 +207,30 @@
 }
 
 gboolean
+panel_key_file_copy_and_mark_trusted (const char  *source_path,
+				      const char  *target_path,
+				      GError     **error)
+{
+	GKeyFile *key_file;
+	gboolean  res = FALSE;
+
+	key_file = g_key_file_new ();
+	res = g_key_file_load_from_file (key_file, source_path, 
+					 G_KEY_FILE_KEEP_COMMENTS|G_KEY_FILE_KEEP_TRANSLATIONS,
+					 error);
+	if (!res) {
+		g_key_file_free (key_file);
+		return FALSE;
+	}
+
+	res = panel_key_file_to_file (key_file, target_path, error);
+
+	g_key_file_free (key_file);
+
+	return res;
+}
+
+gboolean
 panel_key_file_get_boolean (GKeyFile    *keyfile,
 			    const gchar *key,
 			    gboolean     default_value)

Modified: trunk/gnome-panel/libpanel-util/panel-keyfile.h
==============================================================================
--- trunk/gnome-panel/libpanel-util/panel-keyfile.h	(original)
+++ trunk/gnome-panel/libpanel-util/panel-keyfile.h	Sun Mar 15 13:06:30 2009
@@ -41,6 +41,10 @@
 				       GKeyFileFlags   flags,
 				       GError        **error);
 
+gboolean panel_key_file_copy_and_mark_trusted (const char  *source_path,
+					       const char  *target_path,
+					       GError     **error);
+
 gboolean panel_key_file_get_boolean   (GKeyFile       *keyfile,
 				       const gchar    *key,
 				       gboolean        default_value);

Modified: trunk/gnome-panel/menu.c
==============================================================================
--- trunk/gnome-panel/menu.c	(original)
+++ trunk/gnome-panel/menu.c	Sun Mar 15 13:06:30 2009
@@ -31,6 +31,8 @@
 #include <gdk/gdkkeysyms.h>
 #include <gconf/gconf-client.h>
 
+#include <libpanel-util/panel-keyfile.h>
+
 #include "launcher.h"
 #include "panel-util.h"
 #include "panel.h"
@@ -696,39 +698,38 @@
 add_app_to_desktop (GtkWidget      *item,
 		    GMenuTreeEntry *entry)
 {
-	GFile *source;
-	GFile *target;
-	GError *error;
-	char *target_dir;
-	char *target_uri;
-	char *source_uri;
+	char       *source_uri;
+	const char *source;
+	char       *target_dir;
+	char       *target_uri;
+	char       *target;
+	GError     *error;
 
 	g_return_if_fail (entry != NULL);
 
-	source = g_file_new_for_path (gmenu_tree_entry_get_desktop_file_path (entry));
-
 	if (desktop_is_home_dir ()) {
 		target_dir = g_build_filename (g_get_home_dir (), NULL);
 	} else {
 		target_dir = g_strdup (g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP));
 	}
 
-	source_uri = g_file_get_uri (source);
+	source = gmenu_tree_entry_get_desktop_file_path (entry);
+	source_uri = g_filename_to_uri (source, NULL, NULL);
+
 	target_uri = panel_make_unique_desktop_uri (target_dir, source_uri);
-	g_free (source_uri);
 	g_free (target_dir);
+	g_free (source_uri);
+
 	g_return_if_fail (target_uri != NULL);
 
-	target = g_file_new_for_uri (target_uri);
+	target = g_filename_from_uri (target_uri, NULL, NULL);
 	g_free (target_uri);
 
 	error = NULL;
-	g_file_copy (source, target, G_FILE_COPY_NONE,
-		     NULL, NULL, NULL, &error);
+	panel_key_file_copy_and_mark_trusted (source, target, &error);
+
+	g_free (target);
 
-	g_object_unref (source);
-	g_object_unref (target);
-	
 	if (error != NULL) {
 		g_warning ("Problem while copying launcher to desktop: %s",
 			   error->message);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]