home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / system-config-printer / ppdsloader.py < prev    next >
Encoding:
Python Source  |  2010-09-28  |  7.0 KB  |  223 lines

  1. #!/usr/bin/env python
  2.  
  3. ## system-config-printer
  4.  
  5. ## Copyright (C) 2010 Red Hat, Inc.
  6. ## Author: Tim Waugh <twaugh@redhat.com>
  7.  
  8. ## This program is free software; you can redistribute it and/or modify
  9. ## it under the terms of the GNU General Public License as published by
  10. ## the Free Software Foundation; either version 2 of the License, or
  11. ## (at your option) any later version.
  12.  
  13. ## This program is distributed in the hope that it will be useful,
  14. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. ## GNU General Public License for more details.
  17.  
  18. ## You should have received a copy of the GNU General Public License
  19. ## along with this program; if not, write to the Free Software
  20. ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. import dbus
  23. import gtk
  24.  
  25. import asyncconn
  26. from debug import debugprint
  27.  
  28. _ = lambda x: x
  29. def set_gettext_function (fn):
  30.     global _
  31.     _ = fn
  32.  
  33. class PPDsLoader:
  34.     def __init__ (self, callback, device_id=None, parent=None,
  35.                   host=None, encryption=None):
  36.         debugprint ("+%s" % self)
  37.         self._callback = callback
  38.         self._device_id = device_id
  39.         self._parent = parent
  40.         self._host = host
  41.         self._encryption = encryption
  42.  
  43.         self._installed_files = []
  44.         self._conn = None
  45.         self._ppds = None
  46.  
  47.         try:
  48.             self._bus = dbus.SessionBus ()
  49.         except:
  50.             debugprint ("Failed to get session bus")
  51.             self._bus = None
  52.  
  53.         fmt = _("Searching")
  54.         self._dialog = gtk.MessageDialog (parent=parent,
  55.                                           flags=gtk.DIALOG_MODAL |
  56.                                           gtk.DIALOG_DESTROY_WITH_PARENT,
  57.                                           type=gtk.MESSAGE_INFO,
  58.                                           buttons=gtk.BUTTONS_CANCEL,
  59.                                           message_format=fmt)
  60.  
  61.         self._dialog.format_secondary_text (_("Searching for drivers"))
  62.  
  63.         self._dialog.connect ("response", self._dialog_response)
  64.  
  65.         if (device_id and self._bus and
  66.  
  67.             # Only try to install packages if we are configuring the
  68.             # local CUPS server.
  69.             (self._host == None or
  70.              self._host == "localhost" or
  71.              self._host[0] == '/')):
  72.             self._query_packagekit ()
  73.         else:
  74.             self._dialog.show_all ()
  75.             self._query_cups ()
  76.  
  77.     def __del__ (self):
  78.         debugprint ("-%s" % self)
  79.  
  80.     def destroy (self):
  81.         debugprint ("DESTROY: %s" % self)
  82.         if self._dialog:
  83.             self._dialog.destroy ()
  84.             self._dialog = None
  85.  
  86.         self._callback = None
  87.         self._parent = None
  88.  
  89.     def get_installed_files (self):
  90.         return self._installed_files
  91.  
  92.     def get_ppds (self):
  93.         return self._ppds
  94.  
  95.     def _dialog_response (self, dialog, response):
  96.         self._call_callback (None)
  97.  
  98.     def _query_packagekit (self):
  99.         debugprint ("Asking PackageKit to install drivers")
  100.         try:
  101.             xid = self._parent.window.xid
  102.         except:
  103.             xid = 0
  104.  
  105.         try:
  106.             obj = self._bus.get_object ("org.freedesktop.PackageKit",
  107.                                         "/org/freedesktop/PackageKit")
  108.             proxy = dbus.Interface (obj, "org.freedesktop.PackageKit.Modify")
  109.             proxy.InstallPrinterDrivers (xid, [self._device_id],
  110.                                          "hide-finished",
  111.                                          reply_handler=self._packagekit_reply,
  112.                                          error_handler=self._packagekit_error,
  113.                                          timeout=3600)
  114.         except Exception, e:
  115.             debugprint ("Failed to talk to PackageKit: %s" % e)
  116.             if self._dialog:
  117.                 self._dialog.show_all ()
  118.                 self._query_jockey ()
  119.  
  120.     def _packagekit_reply (self):
  121.         debugprint ("Got PackageKit reply")
  122.         if self._dialog:
  123.             self._dialog.show_all ()
  124.             self._query_jockey ()
  125.  
  126.     def _packagekit_error (self, exc):
  127.         debugprint ("Got PackageKit error: %s" % exc)
  128.         if self._dialog:
  129.             self._dialog.show_all ()
  130.             self._query_jockey ()
  131.  
  132.     def _query_jockey (self):
  133.         debugprint ("Asking Jockey to install drivers")
  134.         try:
  135.             obj = self._bus.get_object ("com.ubuntu.DeviceDriver", "/GUI")
  136.             jockey = dbus.Interface (obj, "com.ubuntu.DeviceDriver")
  137.             r = jockey.search_driver ("printer_deviceid:%s" % devid,
  138.                                       reply_handler=self._jockey_reply,
  139.                                       error_handler=self._jockey_error,
  140.                                       timeout=3600)
  141.         except Exception, e:
  142.             self._jockey_error (e)
  143.  
  144.     def _jockey_reply (self, result):
  145.         debugprint ("Got Jockey result: %s" % repr (result))
  146.         self._installed_files = result[1]
  147.         self._query_cups ()
  148.  
  149.     def _jockey_error (self, exc):
  150.         debugprint ("Got Jockey error: %s" % exc)
  151.         self._query_cups ()
  152.  
  153.     def _query_cups (self):
  154.         debugprint ("Asking CUPS for PPDs")
  155.         c = asyncconn.Connection (host=self._host, encryption=self._encryption,
  156.                                   reply_handler=self._cups_connect_reply,
  157.                                   error_handler=self._cups_error)
  158.         self._conn = c
  159.  
  160.     def _cups_connect_reply (self, conn, UNUSED):
  161.         if conn != self._conn:
  162.             conn.destroy ()
  163.             return
  164.  
  165.         conn._begin_operation (_("fetching PPDs"))
  166.         conn.getPPDs (reply_handler=self._cups_reply,
  167.                       error_handler=self._cups_error)
  168.  
  169.     def _cups_reply (self, conn, result):
  170.         if conn != self._conn:
  171.             conn.destroy ()
  172.             return
  173.  
  174.         conn.destroy ()
  175.         self._ppds = result
  176.         self._call_callback (None)
  177.  
  178.     def _cups_error (self, conn, exc):
  179.         if conn != self._conn:
  180.             conn.destroy ()
  181.             return
  182.  
  183.         conn.destroy ()
  184.         self._ppds = None
  185.         self._call_callback (exc)
  186.  
  187.     def _call_callback (self, exc):
  188.         if self._callback:
  189.             self._callback (self, exc)
  190.  
  191.  
  192. if __name__ == "__main__":
  193.     class Foo:
  194.         def __init__ (self):
  195.             w = gtk.Window ()
  196.             b = gtk.Button ("Go")
  197.             w.add (b)
  198.             b.connect ('clicked', self.go)
  199.             w.connect ('delete-event', gtk.main_quit)
  200.             w.show_all ()
  201.             self._window = w
  202.  
  203.         def go (self, button):
  204.             PPDsLoader (self.ppds_loaded, device_id="MFG:MFG;MDL:MDL;",
  205.                         parent=self._window)
  206.  
  207.         def ppds_loaded (self, ppdsloader, exc):
  208.             self._window.destroy ()
  209.             gtk.main_quit ()
  210.             print exc
  211.             ppds = ppdsloader.get_ppds ()
  212.             if ppds != None:
  213.                 print len (ppds)
  214.  
  215.             ppdsloader.destroy ()
  216.  
  217.     import gobject
  218.     from debug import set_debugging
  219.     set_debugging (True)
  220.     gobject.threads_init ()
  221.     Foo ()
  222.     gtk.main ()
  223.