[Gtk-sharp-list] gnome vfs application

McP mariano.cano@hispalinux.es
Mon, 22 Mar 2004 21:18:30 +0100


--=-LkY6WI3UUeDb68ZiTFRi
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

El lun, 22-03-2004 a las 15:49, Jeremy Wells escribió:
> Hi,
> I've been using Vfs.cs, posted on this list awhile ago, to get the mime
> types for files, but i'd also like to get the application associated
> with the file. unfortunatey writing this into the file is more
> complicated than i thought as it involves importing
> GnomeVFSMimeApplication, a struct, and i don't know how to go about
> doing that. Can anyone help me out with this?

I writed and used the attached file to get the mime type, default action
(GnomeVFSMimeApplication) and gnome icon of a file. It should be useful
for your purposes.

Jeremy:
Please contact with me if you improve it or find some bug.

note: The class it's not tested with the last cvs version. If it doesn't
compile it will be very easy to solve it.
-- 
McP <mariano.cano@hispalinux.es>

--=-LkY6WI3UUeDb68ZiTFRi
Content-Disposition: attachment; filename=GnomeIconLookup.cs
Content-Type: text/plain; name=GnomeIconLookup.cs; charset=UTF-8
Content-Transfer-Encoding: 7bit

/*
 * MonoTagEditor
 *
 * Copyright (C) 2003, Mariano Cano Pérez <mariano.cano@hispalinux.es>
 *
 * This file is part of MonoTagEditor.
 *
 * MonoTagEditor 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.
 *
 * MonoTagEditor 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 MonoTagEditor; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

using System;
using Gtk;
using GLib;
using System.Runtime.InteropServices;

public enum GnomeVFSMimeApplicationArgumentType
{
	Uris,
	Path,
	UrisForNonFiles
}

[StructLayout(LayoutKind.Sequential)]
public struct GnomeVFSMimeApplication
{
	public string id;
	public string name;
	public string command;
	public bool can_open_multiple_files;
	public GnomeVFSMimeApplicationArgumentType expects_uris;
	public List supported_uri_schemes;
	public bool requires_terminal;
	
	public IntPtr reserved1;
	public IntPtr reserved2;

	public static GnomeVFSMimeApplication Zero = new GnomeVFSMimeApplication ();
	
	public static GnomeVFSMimeApplication New (IntPtr raw)
	{
		if(raw == IntPtr.Zero)
		{
			return GnomeVFSMimeApplication.Zero;
		}
		GnomeVFSMimeApplication self = new GnomeVFSMimeApplication();
		self = (GnomeVFSMimeApplication) Marshal.PtrToStructure (raw, self.GetType ());
		return self;
	}

	public static bool operator == (GnomeVFSMimeApplication a, GnomeVFSMimeApplication b)
	{
		return a.Equals (b);
	}

	public static bool operator != (GnomeVFSMimeApplication a, GnomeVFSMimeApplication b)
	{
		return ! a.Equals (b);
	}

	public override bool Equals (object o)
	{
		 if (!(o is GnomeVFSMimeApplication))
			 return false;

		return ((GnomeVFSMimeApplication) o) == this;
	}
	
	public override int GetHashCode ()
	{
		return this.GetHashCode ();
	}
}

public enum GnomeIconLookupFlags
{
	None,
	EmbeddingText,
	SmallImages
}

public enum GnomeIconLookupResultsFlags
{
	None,
	Thumbnail
}

class GnomeIconLookup{

	[DllImport("libgnomeui-2")]
	static extern string gnome_icon_lookup (IntPtr icon_theme, 
						IntPtr thumbnail_factory,
						string uri,
						string custim_icon,
						IntPtr file_info,
						string mime_type,
						GnomeIconLookupFlags flags,
						ref GnomeIconLookupResultsFlags result);
	[DllImport("libgnomeui-2")]
	static extern string gnome_icon_theme_lookup_icon (IntPtr icon_theme,
						string icon_name,
						IconSize icon_size,
						ref IntPtr icon_data,
						ref int base_size);
	
	[DllImport("libgnomeui-2")]
	static extern void gnome_icon_data_free (IntPtr icon_data);
	
	[DllImport("libgnomeui-2")]
	static extern IntPtr gnome_icon_theme_new ();
	
	[DllImport("libgnomevfs-2")]
	static extern string gnome_vfs_mime_type_from_name (string mime_type);

	[DllImport("libgnomevfs-2")]
	static extern IntPtr gnome_vfs_application_registry_get_applications(string mime_type);
	
	[DllImport("libgnomevfs-2")]
	static extern IntPtr gnome_vfs_mime_get_default_application(string mime_type);
	
	public static string LookupMimeIcon(string mime,IconSize size)
	{
		string icon_name=String.Empty;
		string icon_path=String.Empty;
		GnomeIconLookupResultsFlags result=0;
		IntPtr icon_data=IntPtr.Zero;
		int base_size=0;
		IntPtr icon_theme = gnome_icon_theme_new ();
		
		if(icon_theme==IntPtr.Zero)
		{
			return String.Empty;
		}
		
		icon_name=gnome_icon_lookup(
			icon_theme,
			IntPtr.Zero,
			String.Empty,
			String.Empty,
			IntPtr.Zero,
			mime,
			GnomeIconLookupFlags.None, 
			ref result);
			
		if(icon_name.Length>0)
		{
			icon_path=gnome_icon_theme_lookup_icon (
				icon_theme,
				icon_name,
				size,
				ref icon_data,
				ref base_size);
			if(icon_data!=IntPtr.Zero)
				gnome_icon_data_free(icon_data);
		}	
		return icon_path;
	}
	
	public static string LookupFileIcon(string file,IconSize size)
	{
		string mime = gnome_vfs_mime_type_from_name(file);
		
		if(mime.Length>0)
			return LookupMimeIcon(mime,size);
		
		return String.Empty;
	}
	
	public static string GetMimeType(string file)
	{
		return gnome_vfs_mime_type_from_name(file);
	}
	
	public static GLib.List GetApplications(string mime_type)
	{
		IntPtr raw_ret = gnome_vfs_application_registry_get_applications(mime_type);
		GLib.List app = new GLib.List(raw_ret);
		return app;
	}
	
	public static GnomeVFSMimeApplication GetDefaultAction(string mime_type)
	{
		IntPtr ptr = gnome_vfs_mime_get_default_application(mime_type);
		GnomeVFSMimeApplication ret = GnomeVFSMimeApplication.New(ptr);
		return ret;
	}
}

--=-LkY6WI3UUeDb68ZiTFRi--