Add mime support for Windows platform

svn: r6911
This commit is contained in:
Brian Matherly 2006-06-18 20:58:25 +00:00
parent c6b026e6dd
commit 3f9adb3771
11 changed files with 239 additions and 56 deletions

View File

@ -1,3 +1,9 @@
2006-06-18 Brian Matherly <brian@gramps-project.org>
* src/Mime/__init__.py: Add WinMime
* src/Mime/_WinMime.py: Added
* src/Utils.py: make launch work in Windows
* src/docgen/*: use Utils.launch to start external viewers
2006-06-17 Don Allingham <don@gramps-project.org>
* src/Editors/_EditPerson.py: select given name field if the
surname field is already defined

153
src/Mime/_WinMime.py Normal file
View 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

View File

@ -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]

View File

@ -459,7 +459,10 @@ def gformat(val):
return return_val.replace(decimal_point,'.')
def search_for(name):
name = name.split()[0]
if name.startswith( '"' ):
name = name.split('"')[1]
else:
name = name.split()[0]
for i in os.environ['PATH'].split(':'):
fname = os.path.join(i,name)
if os.access(fname,os.X_OK) and not os.path.isdir(fname):
@ -991,32 +994,44 @@ class ProgressMeter:
self.ptop.destroy()
def launch(prog_str,path):
subval = {
'%F' : path,
'%f' : path,
'%u' : path,
'%U' : path,
'%n' : path,
'%N' : path,
}
prog_data = prog_str.split()
prog = prog_data[0]
prog_list = []
need_path = True
if sys.platform == "win32":
import subprocess
if prog_str.find("%1") != -1:
prog_str = prog_str.replace("%1",path)
else:
prog_str = '%s "%s"' %(prog_str,path)
subprocess.Popen(prog_str)
if len(prog_data) > 1:
for item in prog_data:
if subval.has_key(item):
need_path = False
value = subval[item]
else:
value = item
prog_list.append(value)
else:
prog_list = [prog_data[0]]
subval = {
'%F' : path,
'%f' : path,
'%u' : path,
'%U' : path,
'%n' : path,
'%N' : path,
}
prog_data = prog_str.split()
prog = prog_data[0]
prog_list = []
need_path = True
if len(prog_data) > 1:
for item in prog_data:
if subval.has_key(item):
need_path = False
value = subval[item]
else:
value = item
prog_list.append(value)
else:
prog_list = [prog_data[0]]
if need_path:
prog_list.append(path)
os.spawnvpe(os.P_NOWAIT, prog, prog_list, os.environ)
if need_path:
prog_list.append(path)
os.spawnvpe(os.P_NOWAIT, prog, prog_list, os.environ)

View File

@ -45,6 +45,7 @@ import ImgManip
import Mime
import Utils
mime_type = ""
#-------------------------------------------------------------------------
#
# Class Definitions
@ -181,11 +182,9 @@ class AbiWordDoc(BaseDoc.BaseDoc):
self.f.close()
if self.print_req:
apptype = 'application/x-abiword'
try:
app = Mime.get_application(apptype)[0]
os.environ["FILE"] = self.filename
os.system ('%s "$FILE" &' % app)
app = Mime.get_application(mime_type)[0]
Utils.launch(app,self.filename)
except:
pass
@ -330,8 +329,13 @@ class AbiWordDoc(BaseDoc.BaseDoc):
#--------------------------------------------------------------------------
try:
prog = Mime.get_application("application/x-abiword")
mtype = Mime.get_description('application/x-abiword')
if Mime.mime_type_is_defined("application/x-abiword"):
mime_type = "application/x-abiword"
elif Mime.mime_type_is_defined("application/abiword"):
mime_type = "application/abiword"
prog = Mime.get_application(mime_type)
mtype = Mime.get_description(mime_type)
if Utils.search_for(prog[0]):
print_label=_("Open in %s") % prog[1]

View File

@ -37,6 +37,8 @@ import BaseDoc
from PluginUtils import register_text_doc
import Errors
import Mime
import Utils
#------------------------------------------------------------------------
#
@ -154,8 +156,7 @@ class AsciiDoc(BaseDoc.BaseDoc):
if self.print_req:
apptype = 'text/plain'
prog = Mime.get_application(apptype)
os.environ["FILE"] = self.filename
os.system ('%s "$FILE" &' % prog[0])
Utils.launch(prog[0],self.filename)
def get_usable_width(self):
return _WIDTH_IN_CHARS
@ -381,8 +382,6 @@ class AsciiDoc(BaseDoc.BaseDoc):
#------------------------------------------------------------------------
print_label = None
try:
import Utils
mprog = Mime.get_application("text/plain")
mtype = Mime.get_description('text/plain')

View File

@ -43,6 +43,8 @@ import Errors
import BaseDoc
import QuestionDialog
import Mime
import Utils
#------------------------------------------------------------------------
#
@ -357,8 +359,7 @@ class HtmlDoc(BaseDoc.BaseDoc):
if self.print_req:
apptype = 'text/html'
app = Mime.get_application(apptype)
os.environ["FILE"] = self.filename
os.system ('%s "$FILE" &' % app[0])
Utils.launch(app[0],self.filename)
def write_support_files(self):
if self.map:
@ -496,9 +497,8 @@ class HtmlDoc(BaseDoc.BaseDoc):
# Register the document generator with the GRAMPS plugin system
#
#------------------------------------------------------------------------
print_label = None
try:
import Utils
prog = Mime.get_application("text/html")
mtype = Mime.get_description("text/html")
@ -506,6 +506,10 @@ try:
print_label=_("Open in %s") % prog[1]
else:
print_label=None
if mtype == _("unknown"):
mtype = _('HTML')
register_text_doc(mtype,HtmlDoc,1,0,1,".html", print_label)
except:
register_text_doc(_('HTML'),HtmlDoc,1,0,1,".html", None)

View File

@ -519,6 +519,10 @@ try:
print_label=_("Open in %s") % prog[1]
else:
print_label=None
if mtype == _("unknown"):
mtype = _('KWord')
register_text_doc(mtype, KwordDoc, 1, 1, 1, ".kwd", print_label)
except:
register_text_doc(_('KWord'), KwordDoc, 1, 1, 1, ".kwd", print_label)

View File

@ -45,6 +45,7 @@ from ReportBase import ReportUtils
import ImgManip
import FontScale
import Mime
import Utils
#-------------------------------------------------------------------------
#
@ -414,8 +415,7 @@ class ODFDoc(BaseDoc.BaseDoc):
self._write_zip()
if self.print_req:
app = Mime.get_application(_apptype)
os.environ["FILE"] = self.filename
os.system ('%s "$FILE" &' % app[0])
Utils.launch(app[0],self.filename)
def add_media_object(self,name,pos,x_cm,y_cm):
@ -1138,8 +1138,6 @@ class ODFDoc(BaseDoc.BaseDoc):
#--------------------------------------------------------------------------
print_label = None
try:
import Utils
mprog = Mime.get_application(_apptype)
mtype = Mime.get_description(_apptype)

View File

@ -382,8 +382,7 @@ class OpenOfficeDoc(BaseDoc.BaseDoc):
self._write_zip()
if self.print_req:
app = Mime.get_application(_apptype)
os.environ["FILE"] = self.filename
os.system ('%s "$FILE" &' % app[0])
Utils.launch(app[0],self.filename)
def add_media_object(self,name,pos,x_cm,y_cm):

View File

@ -38,6 +38,9 @@ from PluginUtils import register_text_doc
import ImgManip
import Errors
import Mime
import Utils
mime_type = "application/rtf"
#------------------------------------------------------------------------
#
@ -125,11 +128,9 @@ class RTFDoc(BaseDoc.BaseDoc):
self.f.close()
if self.print_req:
apptype = 'application/rtf'
try:
app = Mime.get_application(apptype)[0]
os.environ["FILE"] = self.filename
os.system ('%s "$FILE" &' % app)
app = Mime.get_application(mime_type)[0]
Utils.launch(app,self.filename)
except:
pass
@ -422,12 +423,9 @@ class RTFDoc(BaseDoc.BaseDoc):
# Register the document generator with the GRAMPS plugin system
#
#------------------------------------------------------------------------
try:
import Utils
mprog = Mime.get_application("application/rtf")
mtype = Mime.get_description("application/rtf")
mprog = Mime.get_application(mime_type)
mtype = Mime.get_description(mime_type)
if Utils.search_for(mprog[0]):
print_label=_("Open in %s") % mprog[1]