Change the way we "launch" files using their default application. Instead of trying to find the application ourselves, use the OS built in functions to open a file with its default application. We no longer show the name of the application that we will open the file with because we don't know.

svn: r11781
This commit is contained in:
Brian Matherly 2009-02-01 04:21:17 +00:00
parent 6fccf8d75f
commit 3259b393da
18 changed files with 107 additions and 261 deletions

View File

@ -1356,15 +1356,15 @@ class BaseDoc:
self.paper = paper_style
self._style_sheet = styles
self._creator = ""
self.print_req = 0
self.open_req = 0
self.init_called = False
self.type = "standard"
def init(self):
self.init_called = True
def print_requested(self):
self.print_req = 1
def open_requested(self):
self.open_req = 1
def set_creator(self, name):
"Set the owner name"

View File

@ -55,7 +55,7 @@ import Utils
import Bookmarks
import Mime
import gen.lib
from QuestionDialog import ErrorDialog
from Editors import EditMedia, DeleteMediaQuery
import Errors
from Filters.SideBar import MediaSidebarFilter
@ -221,19 +221,12 @@ class MediaView(PageView.ListView):
def view_media(self, obj):
"""
Launch external viewers based of mime types for the selected objects.
Launch external viewers for the selected objects.
"""
for handle in self.selected_handles():
ref_obj = self.dbstate.db.get_object_from_handle(handle)
mime_type = ref_obj.get_mime_type()
app = Mime.get_application(mime_type)
if app:
Utils.launch(app[0], Utils.media_path_full(self.dbstate.db,
ref_obj.get_path()))
else:
ErrorDialog(_("Cannot view %s") % ref_obj.get_path(),
_("GRAMPS cannot find an application that can view "
"a file type of %s.") % mime_type)
mpath = Utils.media_path_full(self.dbstate.db, ref_obj.get_path())
Utils.open_file_with_default_application(mpath)
def _column_editor(self, obj):
"""

View File

@ -57,8 +57,8 @@ from DisplayTabs._ButtonTab import ButtonTab
#
#
#-------------------------------------------------------------------------
def make_launcher(prog, path):
return lambda x: Utils.launch(prog, path)
def make_launcher(path):
return lambda x: Utils.open_file_with_default_application(path)
#-------------------------------------------------------------------------
#
@ -115,19 +115,15 @@ class GalleryTab(ButtonTab):
menu = gtk.Menu()
ref_obj = self.dbstate.db.get_object_from_handle(obj.ref)
mime_type = ref_obj.get_mime_type()
if mime_type:
app = Mime.get_application(mime_type)
if app:
item = gtk.MenuItem(_('Open with %s') % app[1])
item.connect('activate', make_launcher(app[0],
Utils.media_path_full(self.dbstate.db,
ref_obj.get_path())))
item.show()
menu.append(item)
item = gtk.SeparatorMenuItem()
item.show()
menu.append(item)
media_path = Utils.media_path_full(self.dbstate.db, ref_obj.get_path())
if media_path:
item = gtk.MenuItem(_('View'))
item.connect('activate', make_launcher(media_path))
item.show()
menu.append(item)
item = gtk.SeparatorMenuItem()
item.show()
menu.append(item)
for (needs_write_access, image, title, func) in itemlist:
if image:

View File

@ -206,11 +206,9 @@ class EditMedia(EditPrimary):
ref_obj = self.dbstate.db.get_object_from_handle(self.obj.handle)
if ref_obj:
mime_type = ref_obj.get_mime_type()
app = Mime.get_application(mime_type)
if app:
Utils.launch(app[0], Utils.media_path_full(self.dbstate.db,
ref_obj.get_path()))
media_path = Utils.media_path_full(self.dbstate.db,
ref_obj.get_path())
Utils.open_file_with_default_application(media_path)
def select_file(self, val):
self.determine_mime()

View File

@ -44,7 +44,7 @@ import const
import Config
import Mime
import ThumbNails
from Utils import media_path_full
import Utils
from gen.lib import NoteType
from DisplayTabs import (SourceEmbedList, AttrEmbedList, MediaBackRefList,
@ -102,7 +102,7 @@ class EditMediaRef(EditReference):
the path.
"""
self.mtype = self.source.get_mime_type()
fullpath = media_path_full(self.db, self.source.get_path())
fullpath = Utils.media_path_full(self.db, self.source.get_path())
self.pix = ThumbNails.get_thumbnail_image(fullpath,
self.mtype)
self.pixmap.set_from_pixbuf(self.pix)
@ -350,7 +350,7 @@ class EditMediaRef(EditReference):
self.subpixmap.hide()
else:
try:
fullpath = media_path_full(self.db, path)
fullpath = Utils.media_path_full(self.db, path)
pixbuf = gtk.gdk.pixbuf_new_from_file(fullpath)
width = pixbuf.get_width()
height = pixbuf.get_height()
@ -389,7 +389,8 @@ class EditMediaRef(EditReference):
def button_press_event(self, obj, event):
if event.button==1 and event.type == gtk.gdk._2BUTTON_PRESS:
self.view_media(obj)
photo_path = Utils.media_path_full(self.db, self.source.get_path())
Utils.open_file_with_default_application(photo_path)
def button_press_event_ref(self, widget, event):
"""
@ -489,13 +490,6 @@ class EditMediaRef(EditReference):
obj.update()
self.draw_preview()
def view_media(self, obj):
mime_type = self.source.get_mime_type()
app = Mime.get_application(mime_type)
if app:
from Utils import launch
launch(app[0], media_path_full(self.db, self.source.get_path()))
def _connect_signals(self):
self.define_cancel_button(self.top.get_widget('button84'))
self.define_ok_button(self.top.get_widget('button82'),self.save)

View File

@ -470,12 +470,8 @@ class EditPerson(EditPrimary):
menu = gtk.Menu()
menu.set_title(_("Media Object"))
obj = self.db.get_object_from_handle(photo.get_reference_handle())
mtype = obj.get_mime_type()
progname = Mime.get_application(mtype)
if progname and len(progname) > 1:
Utils.add_menuitem(menu, _("Open in %s") % progname[1],
photo, self._popup_view_photo)
if obj:
Utils.add_menuitem(menu, _("View"), photo, self._popup_view_photo)
Utils.add_menuitem(menu, _("Edit Object Properties"), photo,
self._popup_change_description)
menu.popup(None, None, None, event.button, event.time)
@ -488,8 +484,9 @@ class EditPerson(EditPrimary):
if media_list:
photo = media_list[0]
object_handle = photo.get_reference_handle()
Utils.view_photo(self.db.get_object_from_handle(object_handle),
self.db)
ref_obj = self.db.get_object_from_handle(object_handle)
photo_path = Utils.media_path_full(self.db, ref_obj.get_path())
Utils.open_file_with_default_application(photo_path)
def _popup_change_description(self, obj):
"""

View File

@ -95,7 +95,7 @@ class DocReportDialog(ReportDialog):
self.options.set_document(self.doc)
if self.print_report.get_active():
self.doc.print_requested()
self.doc.open_requested()
def doc_type_changed(self, obj):
"""This routine is called when the user selects a new file
@ -110,7 +110,7 @@ class DocReportDialog(ReportDialog):
self.print_report.set_label (label)
self.print_report.set_sensitive (True)
else:
self.print_report.set_label (_("Print a copy"))
self.print_report.set_label (_("Open with default viewer"))
self.print_report.set_sensitive (False)
# Is this to be a printed report or an electronic report

View File

@ -376,9 +376,8 @@ class GVDotDoc(GVDocBase):
dotfile.write(self._dot.getvalue())
dotfile.close()
if self.print_req:
app = Mime.get_application("text/x-graphviz")
Utils.launch(app[0], self._filename)
if self.open_req:
Utils.open_file_with_default_application(self._filename)
#-------------------------------------------------------------------------------
#
@ -422,9 +421,8 @@ class GVPsDoc(GVDocBase):
# Delete the temporary dot file
os.remove(tmp_dot)
if self.print_req:
app = Mime.get_application("application/postscript")
Utils.launch(app[0], self._filename)
if self.open_req:
Utils.open_file_with_default_application(self._filename)
#-------------------------------------------------------------------------------
#
@ -469,9 +467,8 @@ class GVSvgDoc(GVDocBase):
# Delete the temporary dot file
os.remove(tmp_dot)
if self.print_req:
app = Mime.get_application("image/svg")
Utils.launch(app[0], self._filename)
if self.open_req:
Utils.open_file_with_default_application(self._filename)
#-------------------------------------------------------------------------------
#
@ -516,9 +513,8 @@ class GVSvgzDoc(GVDocBase):
# Delete the temporary dot file
os.remove(tmp_dot)
if self.print_req:
app = Mime.get_application("image/svgz")
Utils.launch(app[0], self._filename)
if self.open_req:
Utils.open_file_with_default_application(self._filename)
#-------------------------------------------------------------------------------
#
@ -563,9 +559,8 @@ class GVPngDoc(GVDocBase):
# Delete the temporary dot file
os.remove(tmp_dot)
if self.print_req:
app = Mime.get_application("image/png")
Utils.launch(app[0], self._filename)
if self.open_req:
Utils.open_file_with_default_application(self._filename)
#-------------------------------------------------------------------------------
#
@ -610,9 +605,8 @@ class GVJpegDoc(GVDocBase):
# Delete the temporary dot file
os.remove(tmp_dot)
if self.print_req:
app = Mime.get_application("image/jpeg")
Utils.launch(app[0], self._filename)
if self.open_req:
Utils.open_file_with_default_application(self._filename)
#-------------------------------------------------------------------------------
#
@ -657,9 +651,8 @@ class GVGifDoc(GVDocBase):
# Delete the temporary dot file
os.remove(tmp_dot)
if self.print_req:
app = Mime.get_application("image/gif")
Utils.launch(app[0], self._filename)
if self.open_req:
Utils.open_file_with_default_application(self._filename)
#-------------------------------------------------------------------------------
#
@ -707,9 +700,8 @@ class GVPdfGvDoc(GVDocBase):
# Delete the temporary dot file
os.remove(tmp_dot)
if self.print_req:
app = Mime.get_application("application/pdf")
Utils.launch(app[0], self._filename)
if self.open_req:
Utils.open_file_with_default_application(self._filename)
#-------------------------------------------------------------------------------
#
@ -768,9 +760,8 @@ class GVPdfGsDoc(GVDocBase):
os.remove(tmp_ps)
os.remove(tmp_dot)
if self.print_req:
app = Mime.get_application("application/pdf")
Utils.launch(app[0], self._filename)
if self.open_req:
Utils.open_file_with_default_application(self._filename)
#-------------------------------------------------------------------------------
#
@ -1107,20 +1098,16 @@ class GraphvizReportDialog(ReportDialog):
ReportDialog.setup_report_options_frame(self)
def doc_type_changed(self, obj):
"""This routine is called when the user selects a new file
"""
This routine is called when the user selects a new file
formats for the report. It adjust the various dialog sections
to reflect the appropriate values for the currently selected
file format. For example, a HTML document doesn't need any
paper size/orientation options, but it does need a template
file. Those chances are made here."""
label = obj.get_printable()
if label:
self.print_report.set_label (label)
self.print_report.set_sensitive (True)
else:
self.print_report.set_label (_("Open with application"))
self.print_report.set_sensitive (False)
file. Those chances are made here.
"""
self.print_report.set_label (_("Open with default application"))
self.print_report.set_sensitive(True)
fname = self.target_fileentry.get_full_path(0)
(spath, ext) = os.path.splitext(fname)
@ -1142,7 +1129,7 @@ class GraphvizReportDialog(ReportDialog):
self.options.set_document(self.doc)
if self.print_report.get_active():
self.doc.print_requested()
self.doc.open_requested()
def on_ok_clicked(self, obj):
"""The user is satisfied with the dialog choices. Validate

View File

@ -262,18 +262,6 @@ def add_menuitem(menu, msg, obj, func):
#
#
#-------------------------------------------------------------------------
def view_photo(photo, db):
"""
photo is a mediaobject, this utility launches a viewing application
"""
mime_type = photo.get_mime_type()
try:
data = Mime.get_application(mime_type)
prog = data[0]
except:
return
launch(prog, media_path_full(db, photo.get_path()))
def find_file( filename):
# try the filename we got
try:
@ -1122,53 +1110,30 @@ class ProgressMeter:
Close the progress meter
"""
self.__dialog.destroy()
def launch(prog_str, path):
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)
# The string must be encoded using the filesystem encoding on Windows.
# Otherwise, files with non-ascii characters in their names will not
# open.
prog_str = prog_str.encode(sys.getfilesystemencoding())
subprocess.Popen(prog_str)
def open_file_with_default_application( file_path ):
"""
Launch a program to open an arbitrary file. The file will be opened using
whatever program is configured on the host as the default program for that
type of file.
@param file_path: The path to the file to be opened.
Example: "c:\foo.txt"
@type file_path: string
@return: nothing
"""
norm_path = os.path.normpath( file_path )
if os.sys.platform == 'win32':
norm_path = norm_path.encode(os.sys.getfilesystemencoding())
os.startfile(norm_path)
else:
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 item in subval:
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)
search = os.environ['PATH'].split(':')
for lpath in search:
prog = os.path.join(lpath, 'xdg-open')
if os.path.isfile(prog):
os.spawnvpe(os.P_NOWAIT, prog, [prog, norm_path], os.environ)
return
def profile(func, *args):
import hotshot.stats

View File

@ -1135,7 +1135,7 @@ class BookReportDialog(DocReportDialog):
self.doc.open(self.target_path)
if self.print_report.get_active():
self.doc.print_requested ()
self.doc.open_requested ()
def make_report(self):
"""The actual book report. Start it out, then go through the item list

View File

@ -153,10 +153,8 @@ class AsciiDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc):
def close(self):
self.f.close()
if self.print_req:
apptype = 'text/plain'
prog = Mime.get_application(apptype)
Utils.launch(prog[0],self.filename)
if self.open_req:
Utils.open_file_with_default_application(self.filename)
def get_usable_width(self):
return _WIDTH_IN_CHARS
@ -375,16 +373,6 @@ class AsciiDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc):
#------------------------------------------------------------------------
print_label = None
pmgr = PluginManager.get_instance()
try:
mprog = Mime.get_application("text/plain")
mtype = Mime.get_description('text/plain')
if Utils.search_for(mprog[0]):
print_label=_("Open in %s") % mprog[1]
else:
print_label=None
pmgr.register_text_doc(mtype, AsciiDoc, 1, 1, ".txt", print_label)
except:
pmgr.register_text_doc(_("Plain Text"), AsciiDoc, 1, 1, ".txt", None)
pmgr.register_text_doc(_("Plain Text"), AsciiDoc, 1, 1, ".txt",
_("Open with default viewer"))

View File

@ -336,10 +336,8 @@ class HtmlDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc):
self.f.close()
self.write_support_files()
if self.print_req:
apptype = 'text/html'
app = Mime.get_application(apptype)
Utils.launch(app[0],self.filename)
if self.open_req:
Utils.open_file_with_default_application(self.filename)
def write_support_files(self):
if self.map:
@ -482,18 +480,5 @@ class HtmlDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc):
#------------------------------------------------------------------------
print_label = None
pmgr = PluginManager.get_instance()
try:
prog = Mime.get_application("text/html")
mtype = Mime.get_description("text/html")
if Utils.search_for(prog[0]):
print_label=_("Open in %s") % prog[1]
else:
print_label=None
if mtype == _("unknown"):
mtype = _('HTML')
pmgr.register_text_doc(mtype, HtmlDoc, 0, 1, ".html", print_label)
except:
pmgr.register_text_doc(_('HTML'), HtmlDoc, 0, 1, ".html", None)
pmgr.register_text_doc(_('HTML'), HtmlDoc, 0, 1, ".html",
_("Open with default viewer"))

View File

@ -303,9 +303,8 @@ class LaTeXDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc):
self.f.write('\\end{enumerate}\n')
self.f.write('\n\\end{document}\n')
self.f.close()
if self.print_req:
app = Mime.get_application(_apptype)
Utils.launch(app[0], self.filename)
if self.open_req:
Utils.open_file_with_default_application(self.filename)
def end_page(self):
"""Issue a new page command"""

View File

@ -434,9 +434,8 @@ class ODFDoc(BaseDoc.BaseDoc, BaseDoc.TextDoc, BaseDoc.DrawDoc):
self._write_meta_file()
self._write_mimetype_file()
self._write_zip()
if self.print_req:
app = Mime.get_application(_apptype)
Utils.launch(app[0], self.filename)
if self.open_req:
Utils.open_file_with_default_application(self.filename)
def add_media_object(self, file_name, pos, x_cm, y_cm):
@ -1147,18 +1146,8 @@ class ODFDoc(BaseDoc.BaseDoc, BaseDoc.TextDoc, BaseDoc.DrawDoc):
# Register plugins
#
#--------------------------------------------------------------------------
print_label = None
print_label = _("Open with default viewer")
pmgr = PluginManager.get_instance()
try:
mprog = Mime.get_application(_apptype)
if Utils.search_for(mprog[0]):
print_label = _("Open in %(program_name)s") % { 'program_name':
mprog[1]}
else:
print_label = None
except:
print_label = None
pmgr.register_text_doc(_('Open Document Text'),
ODFDoc, 1, 1, ".odt", print_label)

View File

@ -46,22 +46,6 @@ def lrgb(grp):
def coords(grp):
return (gformat(grp[0]),gformat(grp[1]))
_apptype = 'application/postscript'
print_label = None
try:
# First try to find a viewer program
mprog = Mime.get_application(_apptype)
if Utils.search_for(mprog[0]):
print_label = _("Open in %(program_name)s") % {'program_name': mprog[1]}
except:
pass
if print_label is None:
# Second, try to print directly
if get_print_dialog_app() is not None:
print_label = _("Print a copy")
#-------------------------------------------------------------------------
#
@ -152,15 +136,8 @@ class PSDrawDoc(BaseDoc.BaseDoc,BaseDoc.DrawDoc):
self.f.write('%d\n' % self.page)
self.f.write('%%EOF\n')
self.f.close()
if self.print_req:
if print_label == _("Print a copy"):
run_print_dialog (self.filename)
elif print_label:
app = Mime.get_application(_apptype)
Utils.launch(app[0],self.filename)
else:
# This should never happen
print "Invalid print request"
if self.open_req:
Utils.open_file_with_default_application(self.filename)
def write_text(self,text,mark=None):
pass
@ -368,4 +345,5 @@ class PSDrawDoc(BaseDoc.BaseDoc,BaseDoc.DrawDoc):
self.f.write('grestore\n')
pmgr = PluginManager.get_instance()
pmgr.register_draw_doc(_("PostScript"), PSDrawDoc, 1, 1, ".ps", print_label);
pmgr.register_draw_doc(_("PostScript"), PSDrawDoc, 1, 1, ".ps",
_("Open with default viewer"))

View File

@ -125,9 +125,8 @@ class PdfDoc(CairoDoc):
fontmap.set_resolution(saved_resolution)
# load the result into an external viewer
if self.print_req:
app = Mime.get_application('application/pdf')
Utils.launch(app[0], self._filename)
if self.open_req:
Utils.open_file_with_default_application(self._filename)
#------------------------------------------------------------------------
#
@ -137,17 +136,8 @@ class PdfDoc(CairoDoc):
def register_docgen():
"""Register the docgen with the GRAMPS plugin system.
"""
try:
mprog = Mime.get_application("application/pdf")
mtype = Mime.get_description("application/pdf")
if Utils.search_for(mprog[0]):
print_label = _("Open in %s") % mprog[1]
else:
print_label = None
except:
mtype = _('PDF document')
print_label = None
mtype = _('PDF document')
print_label = _("Open with default viewer")
pmgr = PluginManager.get_instance()
pmgr.register_text_doc(mtype, PdfDoc, 1, 1, ".pdf", print_label)

View File

@ -129,12 +129,8 @@ class RTFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc):
self.f.write('}\n')
self.f.close()
if self.print_req:
try:
app = Mime.get_application(mime_type)[0]
Utils.launch(app,self.filename)
except:
pass
if self.open_req:
Utils.open_file_with_default_application(self.filename)
#--------------------------------------------------------------------
#
@ -443,14 +439,5 @@ class RTFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc):
#
#------------------------------------------------------------------------
pmgr = PluginManager.get_instance()
try:
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]
else:
print_label=None
pmgr.register_text_doc(mtype, RTFDoc, 1, 1, ".rtf", print_label)
except:
pmgr.register_text_doc(_('RTF document'), RTFDoc, 1, 1, ".rtf", None)
pmgr.register_text_doc(_('RTF document'), RTFDoc, 1, 1, ".rtf",
_("Open with default viewer"))

View File

@ -263,5 +263,5 @@ def units(val):
#
#-------------------------------------------------------------------------
pmgr = PluginManager.get_instance()
pmgr.register_draw_doc(_("SVG (Scalable Vector Graphics)"),
SvgDrawDoc, 1, 1, ".svg");
pmgr.register_draw_doc(_("SVG (Scalable Vector Graphics)"), SvgDrawDoc, 1, 1,
".svg", _("Open with default viewer"))