예제 #1
0
파일: gappinfo.c 프로젝트: Andais/glib
/**
 * g_app_info_launch_default_for_uri:
 * @uri: the uri to show
 * @launch_context: an optional #GAppLaunchContext.
 * @error: a #GError.
 *
 * Utility function that launches the default application
 * registered to handle the specified uri. Synchronous I/O
 * is done on the uri to detect the type of the file if
 * required.
 * 
 * Returns: %TRUE on success, %FALSE on error.
 **/
gboolean
g_app_info_launch_default_for_uri (const char         *uri,
				   GAppLaunchContext  *launch_context,
				   GError            **error)
{
  GAppInfo *app_info;
  GFile *file;
  GList l;
  gboolean res;

  file = g_file_new_for_uri (uri);
  app_info = g_file_query_default_handler (file, NULL, error);
  g_object_unref (file);
  if (app_info == NULL)
    return FALSE;

  /* Use the uri, not the GFile, as the GFile roundtrip may
   * affect the uri which we don't want (for instance for a
   * mailto: uri).
   */
  l.data = (char *)uri;
  l.next = l.prev = NULL;
  res = g_app_info_launch_uris (app_info, &l,
				launch_context, error);

  g_object_unref (app_info);
  
  return res;
}
예제 #2
0
파일: gappinfo.c 프로젝트: sfeltman/glib
/**
 * g_app_info_launch_default_for_uri:
 * @uri: the uri to show
 * @launch_context: (allow-none): an optional #GAppLaunchContext.
 * @error: a #GError.
 *
 * Utility function that launches the default application
 * registered to handle the specified uri. Synchronous I/O
 * is done on the uri to detect the type of the file if
 * required.
 * 
 * Returns: %TRUE on success, %FALSE on error.
 **/
gboolean
g_app_info_launch_default_for_uri (const char         *uri,
				   GAppLaunchContext  *launch_context,
				   GError            **error)
{
  char *uri_scheme;
  GAppInfo *app_info = NULL;
  GList l;
  gboolean res;

  /* g_file_query_default_handler() calls
   * g_app_info_get_default_for_uri_scheme() too, but we have to do it
   * here anyway in case GFile can't parse @uri correctly.
   */
  uri_scheme = g_uri_parse_scheme (uri);
  if (uri_scheme && uri_scheme[0] != '\0')
    app_info = g_app_info_get_default_for_uri_scheme (uri_scheme);
  g_free (uri_scheme);

  if (!app_info)
    {
      GFile *file;

      file = g_file_new_for_uri (uri);
      app_info = g_file_query_default_handler (file, NULL, error);
      g_object_unref (file);
      if (app_info == NULL)
	return FALSE;

      /* We still use the original @uri rather than calling
       * g_file_get_uri(), because GFile might have modified the URI
       * in ways we don't want (eg, removing the fragment identifier
       * from a file: URI).
       */
    }

  l.data = (char *)uri;
  l.next = l.prev = NULL;
  res = g_app_info_launch_uris (app_info, &l,
				launch_context, error);

  g_object_unref (app_info);
  
  return res;
}
예제 #3
0
gboolean
open_file_with_filemanager (GtkWidget * window,
                            const gchar * file)
{
	GDesktopAppInfo * d_app_info;
	GKeyFile * key_file;
	GdkAppLaunchContext * ctx = NULL;
	GList * list = NULL;
	GAppInfo * g_app_info;
	GFile * g_file;
	gchar * command;
	gchar * contents;
	gchar * uri;
	gboolean result = TRUE;

	uri = g_filename_to_uri (file, NULL, NULL);
	list = g_list_prepend (list, uri);

	g_file = g_file_new_for_path (file);
	g_app_info = g_file_query_default_handler (g_file, NULL, NULL);

	if (strcmp (g_app_info_get_executable (g_app_info), "nautilus") == 0) {
		command = g_strconcat ("nautilus ",
		                       "--sm-disable ",
		                       "--no-desktop ",
		                       "--no-default-window ",
		                       NULL);
	}
	else {
		command = g_strconcat (g_app_info_get_executable (g_app_info),
		                       " ", NULL);
	}

	contents = g_strdup_printf ("[Desktop Entry]\n"
				    "Name=Nautilus\n"
				    "Icon=file-manager\n"
				    "Exec=%s\n"
				    "Terminal=false\n"
				    "StartupNotify=true\n"
				    "Type=Application\n",
				    command);
	key_file = g_key_file_new ();
	g_key_file_load_from_data (key_file, contents, strlen(contents), G_KEY_FILE_NONE, NULL);
	d_app_info = g_desktop_app_info_new_from_keyfile (key_file);
	
	if (d_app_info != NULL) {
		ctx = gdk_app_launch_context_new ();
		gdk_app_launch_context_set_screen (ctx, gtk_widget_get_screen (window));

		result = g_app_info_launch_uris (G_APP_INFO (d_app_info), list,  G_APP_LAUNCH_CONTEXT (ctx), NULL);
	}
	else {
		result = FALSE;
	}

	g_object_unref (g_app_info);
	g_object_unref (d_app_info);
	g_object_unref (g_file);
	g_object_unref (ctx);
	g_key_file_free (key_file);
	g_list_free (list);
	g_free (contents);
	g_free (command);
	g_free (uri);

	return result;
}