Add mime support for Windows platform
svn: r6911
This commit is contained in:
153
src/Mime/_WinMime.py
Normal file
153
src/Mime/_WinMime.py
Normal file
@@ -0,0 +1,153 @@
|
||||
#
|
||||
# Gramps - a GTK+/GNOME based genealogy program
|
||||
#
|
||||
# Copyright (C) 2006 Brian Matherly
|
||||
#
|
||||
# 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, write to the Free Software
|
||||
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
#
|
||||
|
||||
# $Id:
|
||||
|
||||
#-------------------------------------------------------------------------
|
||||
#
|
||||
# Standard python modules
|
||||
#
|
||||
#-------------------------------------------------------------------------
|
||||
import os
|
||||
from _winreg import *
|
||||
from gettext import gettext as _
|
||||
|
||||
#-------------------------------------------------------------------------
|
||||
#
|
||||
# GNOME/GTK
|
||||
#
|
||||
#-------------------------------------------------------------------------
|
||||
import gtk
|
||||
|
||||
#-------------------------------------------------------------------------
|
||||
#
|
||||
# Gramps modules
|
||||
#
|
||||
#-------------------------------------------------------------------------
|
||||
import const
|
||||
import _PythonMime
|
||||
|
||||
|
||||
def get_application(type):
|
||||
"""Returns the application command and application name of the
|
||||
specified mime type"""
|
||||
extension = _get_extension(type)
|
||||
progId = _get_prog_id(extension)
|
||||
|
||||
if progId:
|
||||
# Find the application associated with this program ID
|
||||
aReg = ConnectRegistry(None,HKEY_CLASSES_ROOT)
|
||||
subkey = OpenKey(aReg, "%s\shell\open\command" % progId)
|
||||
n,command,type = EnumValue(subkey, 0)
|
||||
if type == REG_EXPAND_SZ:
|
||||
command = command.replace( '%SystemRoot%',
|
||||
os.getenv('SystemRoot') )
|
||||
|
||||
# TODO: figure out how to get a description of the application
|
||||
# use that instead of progId
|
||||
return (command,progId)
|
||||
|
||||
return None
|
||||
|
||||
def get_description(mime_type):
|
||||
"""Returns the description of the specfied mime type"""
|
||||
desc = None
|
||||
extension = _get_extension(mime_type)
|
||||
progId = _get_prog_id(extension)
|
||||
|
||||
if progId:
|
||||
aReg = ConnectRegistry(None,HKEY_CLASSES_ROOT)
|
||||
desc = QueryValue(aReg, progId)
|
||||
CloseKey(aReg)
|
||||
|
||||
if not desc:
|
||||
desc = _("unknown")
|
||||
|
||||
return desc
|
||||
|
||||
def get_type(file):
|
||||
"""Returns the mime type of the specified file"""
|
||||
return _PythonMime.get_type(file)
|
||||
|
||||
def mime_type_is_defined(mime_type):
|
||||
"""
|
||||
Return True if a description for a mime type exists.
|
||||
"""
|
||||
extension = _get_extension(mime_type)
|
||||
if extension:
|
||||
return True
|
||||
else:
|
||||
return _PythonMime.mime_type_is_defined(mime_type)
|
||||
|
||||
_icon_theme = gtk.icon_theme_get_default()
|
||||
|
||||
def find_mime_type_pixbuf(mime_type):
|
||||
return _PythonMime.find_mime_type_pixbuf(mime_type)
|
||||
|
||||
#-------------------------------------------------------------------------
|
||||
#
|
||||
# private functions
|
||||
#
|
||||
#-------------------------------------------------------------------------
|
||||
def _get_extension(mime_type):
|
||||
"""
|
||||
Return the extension associated with this mime type
|
||||
Return None if no association exists
|
||||
"""
|
||||
extension = None
|
||||
try:
|
||||
aReg = ConnectRegistry(None,HKEY_CLASSES_ROOT)
|
||||
subkey = OpenKey(aReg, "MIME\DataBase\Content Type")
|
||||
mimekey = OpenKey(subkey, mime_type)
|
||||
extension,type = QueryValueEx(mimekey, "Extension")
|
||||
CloseKey(mimekey)
|
||||
CloseKey(subkey)
|
||||
CloseKey(aReg)
|
||||
except:
|
||||
extension = None
|
||||
|
||||
if not extension:
|
||||
# Work around for Windows mime problems
|
||||
extmap = {
|
||||
'application/abiword' : '.abw',
|
||||
'application/rtf' : '.rtf',
|
||||
}
|
||||
if extmap.has_key(mime_type):
|
||||
extension = extmap[mime_type]
|
||||
|
||||
return extension
|
||||
|
||||
|
||||
def _get_prog_id(extension):
|
||||
"""
|
||||
Return the program ID associated with this extension
|
||||
Return None if no association exists
|
||||
"""
|
||||
if not extension:
|
||||
return None
|
||||
|
||||
try:
|
||||
aReg = ConnectRegistry(None,HKEY_CLASSES_ROOT)
|
||||
progId = QueryValue(aReg, extension)
|
||||
CloseKey(aReg)
|
||||
return progId
|
||||
except:
|
||||
return None
|
||||
|
@@ -21,7 +21,10 @@
|
||||
try:
|
||||
from _GnomeMime import *
|
||||
except:
|
||||
from _PythonMime import *
|
||||
try:
|
||||
from _WinMime import *
|
||||
except:
|
||||
from _PythonMime import *
|
||||
|
||||
def base_type(val):
|
||||
return val.split('/')[0]
|
||||
|
Reference in New Issue
Block a user