home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / AppInstall / activation.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-10-12  |  19.8 KB  |  487 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import sys
  5. import gdbm
  6. import gconf
  7. import errno
  8. import gettext
  9. import os
  10. import os.path as os
  11. from gettext import gettext as _
  12. from optparse import OptionParser
  13.  
  14. class ActivationStyle:
  15.     
  16.     def __init__(self):
  17.         self.selectFilter = None
  18.         self.menuFilter = None
  19.         self.isInstallerOnly = False
  20.  
  21.     
  22.     def isSpecific(self):
  23.         ''' return True if we are we in a not-normal mode '''
  24.         return False
  25.  
  26.     
  27.     def menuCacheName(self):
  28.         ''' Each activation-mode can have a different cached menu
  29.             This is useful for e.g. codec activation style because
  30.             there are only very few codecs so it makes sense to read
  31.             only the subset of the menu with codec information
  32.         '''
  33.         return 'menu.p'
  34.  
  35.     
  36.     def searchTerms(self):
  37.         pass
  38.  
  39.     
  40.     def isApproved(self, component, package):
  41.         return True
  42.  
  43.     
  44.     def userApprovedNotify(self):
  45.         pass
  46.  
  47.     
  48.     def changesSuccessfulNotify(self):
  49.         pass
  50.  
  51.     
  52.     def quitHook(self):
  53.         pass
  54.  
  55.     
  56.     def modifyUserInterface(self, app):
  57.         pass
  58.  
  59.     
  60.     def getMenuFilter(self):
  61.         return self._menu_filter
  62.  
  63.     
  64.     def preRun(self):
  65.         return True
  66.  
  67.     
  68.     def autoClose(self):
  69.         ''' auto close on successful install/remove '''
  70.         return False
  71.  
  72.  
  73.  
  74. class SearchActivationStyle(ActivationStyle):
  75.     
  76.     def __init__(self, dictname, options):
  77.         self._dn = dictname
  78.         self._cachedir = options.cachedir
  79.         self._userapproved = False
  80.         self._changessuccessful = False
  81.         self.menuFilter = None
  82.         dict = { }
  83.         for d in (options.datadir, '/etc/gnome-app-install'):
  84.             
  85.             try:
  86.                 f = open(d + '/packages-whitelist')
  87.             except IOError:
  88.                 e = None
  89.                 if e.errno == errno.ENOENT:
  90.                     continue
  91.                 
  92.                 raise 
  93.  
  94.             for l in f:
  95.                 v = l.strip()
  96.                 if v.startswith('#'):
  97.                     continue
  98.                 
  99.                 dict[v] = True
  100.             
  101.         
  102.         self._wl_packages = dict
  103.         
  104.         try:
  105.             client = gconf.client_get_default()
  106.             l = client.get_list('/apps/gnome-app-install' + '/mime-whitelist-components', gconf.VALUE_STRING)
  107.         except gobject.GError:
  108.             e = None
  109.             print 'Error in gconf: %s' % e
  110.             l = [
  111.                 'main']
  112.  
  113.         dict = { }
  114.         for v in l:
  115.             dict[v] = True
  116.         
  117.         self._wl_components = dict
  118.         db_name = self._cachedir + '/gai-' + self._dn + '-map.gdbm'
  119.         self._db = gdbm.open(db_name, 'rfu')
  120.  
  121.     
  122.     def menuCacheName(self):
  123.         return 'mime_menu.p'
  124.  
  125.     
  126.     def lookup(self, string):
  127.         
  128.         try:
  129.             value = self._db[string]
  130.         except KeyError:
  131.             value = ''
  132.  
  133.         unapproved = False
  134.         for e in value.split():
  135.             (component, package) = e.split('/', 1)
  136.             if self.isApproved(component, package):
  137.                 return (True, True, None, None)
  138.             unapproved = True
  139.         
  140.         if unapproved:
  141.             abbrev = _('no suitable application')
  142.             msg = _('No application suitable for automatic installation is available for handling this kind of file.')
  143.         else:
  144.             abbrev = _('no application found')
  145.             msg = _('No application is known for this kind of file.')
  146.         return (False, unapproved, abbrev, msg)
  147.  
  148.     
  149.     def isApproved(self, component, package):
  150.         if not self._wl_components.has_key(component):
  151.             pass
  152.         return self._wl_packages.has_key(package)
  153.  
  154.     
  155.     def isSpecific(self):
  156.         return True
  157.  
  158.     
  159.     def userApprovedNotify(self):
  160.         self._userapproved = True
  161.  
  162.     
  163.     def changesSuccessfulNotify(self):
  164.         self._changessuccessful = True
  165.  
  166.     
  167.     def modifyUserInterface(self, app):
  168.         app.scrolledwindow_left.hide()
  169.         app.label_progress.set_markup('<big><b>%s</b></big>\n\n%s' % (_('Searching for appropriate applications'), _('Please wait. This might take a minute or two.')))
  170.  
  171.  
  172.  
  173. class CodecSearchActivationStyle(SearchActivationStyle):
  174.     
  175.     def __init__(self, options, args):
  176.         SearchActivationStyle.__init__(self, 'codec', options)
  177.         self._codecs = []
  178.         self._args = args
  179.         self.options = options
  180.         self.menuFilter = 0
  181.         self.isInstallerOnly = True
  182.  
  183.     
  184.     def _parseArgs(self):
  185.         ''' helper that tries to decode the commandline'''
  186.         for arg in self._args:
  187.             
  188.             try:
  189.                 (origin, version, app, descr, search_token) = arg.split('|')
  190.             except ValueError:
  191.                 e = None
  192.                 sys.stderr.write("invalid commandline '%s' (%s)\n" % (arg, e))
  193.                 return False
  194.  
  195.             self.addSearchTerm('%s:%s' % (version, search_token))
  196.         
  197.         return True
  198.  
  199.     
  200.     def preRun(self):
  201.         if self._parseArgs():
  202.             if not askConfirmation(_('Search for suitable codec?'), _('The required software to play this file is not installed. You need to install suitable codecs to play media files. Do you want to search for a codec that supports the selected file?\n\nThe search will also include software which is not officially supported.'), self.options.transient_for):
  203.                 sys.exit(4)
  204.             
  205.         else:
  206.             import gtk
  207.             abbrev = _('Invalid commandline')
  208.             msg = _("'%s' does not understand the commandline argument '%s'" % (sys.argv[0], self._args))
  209.             dlg = gtk.MessageDialog(None, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, abbrev)
  210.             dlg.format_secondary_text(msg)
  211.             dlg.run()
  212.             dlg.destroy()
  213.             sys.exit(1)
  214.         return True
  215.  
  216.     
  217.     def modifyUserInterface(self, app):
  218.         import gtk
  219.         import distros
  220.         SearchActivationStyle.modifyUserInterface(self, app)
  221.         app.textview_description.show_message('', _('Some countries allow patents on software, and freely redistributable software like Ubuntu cannot pay for patent licenses. If you are in one of these jurisdictions, you can buy licensed media playback plug-ins from the Canonical Store. Otherwise, select a free plug-in above to install it.'))
  222.         app.button_help.hide()
  223.         app.treeview_packages.set_headers_visible(False)
  224.         app.treeview_packages.column_app_popcon.set_visible(False)
  225.         app.label_progress.set_markup('<big><b>%s</b></big>\n\n%s' % (_('Searching for appropriate codecs'), _('Please wait. This might take a minute or two.')))
  226.         app.hbox_search_show.hide()
  227.         app.button_ok.set_label(_('_Install'))
  228.         app.window_main.set_title(_('Install Media Plug-ins'))
  229.         app.window_main.set_property('default_width', 500)
  230.         app.window_main.set_property('default_height', 400)
  231.         col = app.treeview_packages.get_column(1)
  232.         col.set_title(_('Codec'))
  233.         model = app.treeview_packages.get_model()
  234.         model.set_sort_column_id(2, gtk.SORT_DESCENDING)
  235.         distro = distros.get_distro()
  236.         (label, url) = distro.get_codec_information_link()
  237.         if label is not None and url is not None:
  238.             button = gtk.Button(label)
  239.             button.uri = url
  240.             button.connect('clicked', self.uri_clicked)
  241.             button.show()
  242.             app.hbox_help.pack_end(button)
  243.         
  244.  
  245.     
  246.     def uri_clicked(self, button):
  247.         import subprocess
  248.         for opener in [
  249.             'gnome-open',
  250.             'sensible-browser',
  251.             'xdg-open',
  252.             'x-www-browser']:
  253.             for d in os.environ['PATH'].split(':'):
  254.                 if os.path.exists(os.path.join(d, opener)):
  255.                     print 'found ', opener
  256.                     subprocess.call([
  257.                         opener,
  258.                         button.uri])
  259.                     return None
  260.             
  261.         
  262.  
  263.     
  264.     def menuCacheName(self):
  265.         return 'codec_menu.p'
  266.  
  267.     
  268.     def addSearchTerm(self, string):
  269.         string = string.replace(', ', ',')
  270.         string = string.split(' ')[0]
  271.         self._codecs.append(string)
  272.         (ok, unapproved, abbrev, msg) = self.lookup(string.split(',')[0])
  273.         if ok:
  274.             return None
  275.         print >>sys.stderr, abbrev
  276.         sys.exit(1)
  277.  
  278.     
  279.     def searchTerms(self):
  280.         return self._codecs
  281.  
  282.     
  283.     def selectFilter(self, menu):
  284.         return menu._codecMatch
  285.  
  286.     
  287.     def quitHook(self):
  288.         if not self._userapproved:
  289.             sys.exit(4)
  290.         
  291.         if not self._changessuccessful:
  292.             sys.exit(2)
  293.         
  294.  
  295.     
  296.     def autoClose(self):
  297.         ''' auto close the app on successful codec installs '''
  298.         return True
  299.  
  300.  
  301.  
  302. class MimeSearchActivationStyle(SearchActivationStyle):
  303.     
  304.     def __init__(self, options, uri, duri):
  305.         SearchActivationStyle.__init__(self, 'mime', options)
  306.         self._uri = uri
  307.         self._duri = duri
  308.         self._string = options.mime_type
  309.         self.isInstallerOnly = True
  310.  
  311.     
  312.     def preRun(self):
  313.         (ok, unapproved, abbrev, msg) = self.lookup(self._string)
  314.         if ok:
  315.             return True
  316.         if self._uri:
  317.             import gtk
  318.             import AppInstall
  319.             import os.path as os
  320.             header = _('"%s" cannot be opened') % os.path.basename(self._duri)
  321.             dlg = gtk.MessageDialog(None, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, header)
  322.             dlg.format_secondary_text(msg)
  323.             dlg.set_title(header)
  324.             dlg.run()
  325.             dlg.destroy()
  326.             print >>sys.stderr, abbrev
  327.             sys.exit(6)
  328.         else:
  329.             print >>sys.stderr, 'not offering packages for %s' % self._string
  330.             if unapproved:
  331.                 print >>sys.stderr, 'only unapproved: %s' % self._string
  332.                 sys.exit(5)
  333.             else:
  334.                 print >>sys.stderr, 'no entry in mime map'
  335.                 sys.exit(4)
  336.  
  337.     
  338.     def searchTerms(self):
  339.         return [
  340.             self._string]
  341.  
  342.     
  343.     def selectFilter(self, menu):
  344.         return menu._mimeMatch
  345.  
  346.     
  347.     def quitHook(self):
  348.         if self._uri and self._changessuccessful:
  349.             import gnomevfs
  350.             gnomevfs.url_show(self._uri)
  351.         
  352.  
  353.     
  354.     def modifyUserInterface(self, app):
  355.         import xdg.Mime as xdg
  356.         import os.path as os
  357.         import gtk
  358.         mime = xdg.Mime.lookup(self._string)
  359.         SearchActivationStyle.modifyUserInterface(self, app)
  360.         app.label_progress.set_markup('<big><b>%s</b></big>\n\n%s' % (_('Searching for appropriate applications'), _("A list of applications that can handle documents of the type '%s' will be created") % mime.get_comment()))
  361.         app.button_ok.set_label(_('_Install'))
  362.         if self._uri:
  363.             app.window_main.set_title(_('Install applications to open "%s"') % os.path.basename(self._duri))
  364.         else:
  365.             app.window_main.set_title(_('Install applications'))
  366.         app.window_main.set_property('default_width', 500)
  367.         model = app.treeview_packages.get_model()
  368.         model.set_sort_column_id(2, gtk.SORT_DESCENDING)
  369.  
  370.  
  371.  
  372. def askConfirmation(summary, msg, transient_for = None):
  373.     import gtk
  374.     dlg = gtk.MessageDialog(None, gtk.DIALOG_MODAL, gtk.MESSAGE_QUESTION, gtk.BUTTONS_CANCEL, summary)
  375.     dlg.format_secondary_text(msg)
  376.     btn = dlg.add_button(_('_Search'), gtk.RESPONSE_YES)
  377.     btn.grab_focus()
  378.     if not transient_for:
  379.         dlg.set_title(summary)
  380.     
  381.     if transient_for:
  382.         parent = gtk.gdk.window_foreign_new(transient_for)
  383.         if parent:
  384.             dlg.realize()
  385.             dlg.window.set_transient_for(parent)
  386.         
  387.     
  388.     res = dlg.run()
  389.     dlg.destroy()
  390.     while gtk.events_pending():
  391.         gtk.main_iteration()
  392.     if res == gtk.RESPONSE_YES:
  393.         return True
  394.     return False
  395.  
  396.  
  397. class XULExtensionsActivationStyle(MimeSearchActivationStyle):
  398.     
  399.     def __init__(self, options):
  400.         MimeSearchActivationStyle.__init__(self, options, uri = None, duri = None)
  401.         self._string = 'application/x-debian-xul-extension-%s' % options.xul_extensions.lower()
  402.  
  403.     
  404.     def isApproved(self, component, package):
  405.         return True
  406.  
  407.     
  408.     def quitHook(self):
  409.         if self._changessuccessful:
  410.             print >>sys.stderr, 'changed extensions'
  411.             sys.exit(0)
  412.         else:
  413.             print >>sys.stderr, 'did not change anything'
  414.             sys.exit(1)
  415.  
  416.     
  417.     def modifyUserInterface(self, app):
  418.         SearchActivationStyle.modifyUserInterface(self, app)
  419.         app.label_progress.set_markup('<big><b>%s</b></big>\n\n%s' % (_('Searching for extensions'), _('Extensions allow you to add new features to your application.')))
  420.         app.window_main.set_title(_('Install/Remove Extensions'))
  421.         col = app.treeview_packages.get_column(1)
  422.         col.set_title(_('Extension'))
  423.  
  424.     
  425.     def preRun(self):
  426.         return True
  427.  
  428.  
  429.  
  430. def main():
  431.     app = 'gnome-app-install'
  432.     gettext.textdomain(app)
  433.     gettext.bindtextdomain(app)
  434.     parser = OptionParser()
  435.     parser.add_option('', '--mime-type', default = None, action = 'store', type = 'string', dest = 'mime_type', help = 'Show only applications that handle the given file type')
  436.     parser.add_option('', '--transient-for', default = None, action = 'store', type = 'int', dest = 'transient_for', help = 'Start as a child window of the given window (Only needed by developers')
  437.     parser.add_option('', '--data-dir', default = '/usr/share/gnome-app-install', action = 'store', type = 'string', dest = 'datadir', help = 'Load data from the given directory (Only needed by developers)')
  438.     parser.add_option('', '--desktopdir', default = '/usr/share/app-install', action = 'store', type = 'string', dest = 'desktopdir', help = 'Read the desktop files from the given directory (Only needed by developers)')
  439.     parser.add_option('', '--cachedir', default = '/var/cache/app-install', action = 'store', type = 'string', dest = 'cachedir', help = 'Use the given directory for the cache (Only needed by developers)')
  440.     parser.add_option('', '--xul-extensions', default = None, action = 'store', type = 'string', dest = 'xul_extensions', help = 'Start as installer for XUL extensions')
  441.     parser.add_option('', '--addon-cd', default = None, action = 'store', type = 'string', dest = 'addon_cd', help = 'Start as installer for an addon cd')
  442.     parser.add_option('', '--selftest', action = 'store_true', dest = 'selftest', help = 'Perform self tests (Only needed by developers)')
  443.     parser.add_option('', '--profile', action = 'store', type = 'string', dest = 'profile', default = None, help = 'Store profiling data in the given file (Only needed by developers)')
  444.     parser.add_option('', '--test-mode', action = 'store_true', dest = 'test_mode', help = 'Run in a sepcial test mode(Only needed by developers)')
  445.     (options, args) = parser.parse_args()
  446.     if options.selftest:
  447.         AppInstall = AppInstall
  448.         import AppInstall
  449.         app = AppInstall(options, ActivationStyle())
  450.         while True:
  451.             model = app.treeview_packages.get_model()
  452.             it = model.get_iter_root()
  453.             (name, item, popcon) = model[it]
  454.             app.applyChanges([
  455.                 item], [])
  456.     
  457.     if sys.argv[0].split('/')[-1] == 'gstreamer-codec-install':
  458.         style = CodecSearchActivationStyle(options, args)
  459.     elif options.mime_type:
  460.         uri = None
  461.         duri = None
  462.         if len(args) > 1:
  463.             uri = args[0]
  464.             duri = args[1]
  465.         elif len(args) > 0:
  466.             uri = args[0]
  467.             duri = uri
  468.         
  469.         style = MimeSearchActivationStyle(options, uri, duri)
  470.     elif options.xul_extensions:
  471.         style = XULExtensionsActivationStyle(options)
  472.     else:
  473.         style = ActivationStyle()
  474.     if style.preRun():
  475.         AppInstall = AppInstall
  476.         import AppInstall
  477.         app = AppInstall(options, style)
  478.         if options.profile:
  479.             import hotshot
  480.             prof = hotshot.Profile(options.profile)
  481.             print prof.runcall(app.run)
  482.             prof.close()
  483.         else:
  484.             app.run()
  485.     
  486.  
  487.