home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / bin / gnome-app-install < prev    next >
Encoding:
Text File  |  2006-08-28  |  3.8 KB  |  138 lines

  1. #!/usr/bin/python
  2.  
  3. # Copyright (C) 2004-2005 Ross Burton <ross@burtonini.com>
  4. #               2005 Canonical
  5. #
  6. # This program is free software; you can redistribute it and/or modify it under
  7. # the terms of the GNU General Public License as published by the Free Software
  8. # Foundation; either version 2 of the License, or (at your option) any later
  9. # version.
  10. #
  11. # This program is distributed in the hope that it will be useful, but WITHOUT
  12. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  13. # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  14. # details.
  15. #
  16. # You should have received a copy of the GNU General Public License along with
  17. # this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  18. # Place, Suite 330, Boston, MA 02111-1307 USA
  19.  
  20. import sys
  21. import gdbm
  22. import gconf
  23. import errno
  24.  
  25. uri = None
  26.  
  27. class MimeSearchInfo:
  28.     def approved(self, component, package):
  29.         return (self._wl_components.has_key(component) or
  30.             self._wl_packages.has_key(package))
  31.  
  32.     def retry_open(self):
  33.         if uri:
  34.             import gnomevfs
  35.             gnomevfs.url_show(uri)
  36.  
  37.     def __init__(self, string, datadir, desktopdir, duri=None):
  38.         self.string = string
  39.  
  40.         # read the packages whitelist (from the files)
  41.         dict = {}
  42.         for d in (datadir, '/etc/gnome-app-install'):
  43.             try: f = open(d+'/mime-whitelist-packages')
  44.             except IOError, e:
  45.                 if e.errno == errno.ENOENT: continue
  46.                 raise
  47.             for l in f:
  48.                 v = l.strip()
  49.                 if v.startswith('#'): continue
  50.                 dict[v] = True
  51.         self._wl_packages = dict
  52.  
  53.         # read the component whitelist (from gconf)
  54.         client = gconf.client_get_default()
  55.         l = client.get_list("/apps/gnome-app-install"+
  56.                 "/mime-whitelist-components",
  57.                 gconf.VALUE_STRING)
  58.         dict = {}
  59.         for v in l: dict[v] = True
  60.         self._wl_components = dict
  61.  
  62.         # look up our entry and bomb if not found
  63.         db = gdbm.open(desktopdir+"/gai-mime-map.gdbm", 'rfu')
  64.         try: value = db[string]
  65.         except KeyError: value = ''
  66.  
  67.         any = False
  68.         for e in value.split():
  69.             (component,package) = e.split('/',1)
  70.             if self.approved(component,package): return
  71.             any = True
  72.  
  73.         if uri:
  74.             from gettext import gettext as _
  75.             import gtk
  76.             import AppInstall
  77.  
  78.             if any: (msg,abbrev) = (_(
  79. "Cannot open %s:"
  80. " No application suitable for automatic installation is available"
  81. " for handling this kind of file."
  82.                     ) % duri,
  83. _("Error: no suitable application"))
  84.             else: (msg,abbrev) = (_(
  85. "Cannot open %s: "
  86. " No application is known for this kind of file."
  87.                     ) % duri,
  88. _("Error: no application found"))
  89.             dlg = gtk.MessageDialog(None, gtk.DIALOG_MODAL,
  90.                 gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, msg)
  91.             dlg.set_title(abbrev)
  92.             dlg.run()
  93.             dlg.destroy()
  94.             sys.exit(6)
  95.         else:
  96.             print >>sys.stderr, "not offering packages for "+string
  97.             if any:
  98.                 print >>sys.stderr, "only unapproved: "+value
  99.                 sys.exit(5)
  100.             else:
  101.                 print >>sys.stderr, "no entry in mime map"
  102.                 sys.exit(4)
  103.  
  104. if __name__ == "__main__":
  105.  
  106.     #datadir = "/tmp/xxx/share/gnome-app-install"    
  107.     datadir = "/usr/share/gnome-app-install"
  108.     desktopdir = "/usr/share/app-install"
  109.  
  110.     # I tried using optparse here but:
  111.     #    $ time python -c 'import optparse'
  112.     #    real    0m0.134s
  113.     #    user    0m0.120s
  114.     #    sys     0m0.012s
  115.     def badusage():
  116.         print >>sys.stderr, "gnome-app-install bad usage"
  117.         sys.exit(127)
  118.  
  119.     if len(sys.argv)==1:
  120.         msi = None
  121.     elif sys.argv[1].startswith('--mime-type='):
  122.         if len(sys.argv)==2: duri = None
  123.         elif len(sys.argv)==3: uri = sys.argv[2]; duri = uri
  124.         elif len(sys.argv)==4: (uri,duri) = sys.argv[2:]
  125.         else: badusage()
  126.  
  127.         mime_type = sys.argv[1][12:]
  128.         msi = MimeSearchInfo(mime_type, datadir, desktopdir, duri)
  129.     else: badusage()
  130.  
  131. # We have already bombed out if the quick test fails.  We do this
  132. #  import only now so that quick tests are really quick.
  133. from AppInstall.AppInstall import AppInstall
  134.  
  135. if __name__ == "__main__":
  136.         app = AppInstall(datadir, desktopdir, sys.argv, mime_search=msi)
  137.     app.run()
  138.