screenlets.services

Package screenlets :: Module services |
[hide private]
[frames] | no frames]

# Source Code for Module screenlets.services

      1  # This program is free software: you can redistribute it and/or modify
      2  # it under the terms of the GNU General Public License as published by
      3  # the Free Software Foundation, either version 3 of the License, or
      4  # (at your option) any later version.
      5  #
      6  # This program is distributed in the hope that it will be useful,
      7  # but WITHOUT ANY WARRANTY; without even the implied warranty of
      8  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      9  # GNU General Public License for more details.
     10  #
     11  # You should have received a copy of the GNU General Public License
     12  # along with this program.  If not, see <http://www.gnu.org/licenses/>.
     13
     14  # The services-module contains the ScreenletService-class and a set of utility
     15  # functions to work with Screenlet-services from within other applications.
     16  #
     17  # (c) 2007 by RYX (Rico Pfaus)
     18  #
     19  # TODO:
     20  # - add missing default actions and signals (similar for all screenlets)
     21  # - maybe abstract the dbus-related stuff and create subclasses which implement
     22  #   different communication methods? Later.
     23  # - get_available_services() ... get list with names/ids of services
     24  #
     25
     26
     27  import dbus
     28  import dbus.service
     29  if getattr(dbus, 'version', (0,0,0)) >= (0,41,0):
     30          if getattr(dbus, 'version', (0,0,0)) <= (0,80,0):
     31
     32                  import dbus.glib
     33          else:
     34
     35                  from dbus.mainloop.glib import DBusGMainLoop
     36                  DBusGMainLoop(set_as_default=True)
     37  import gettext
     38  import screenlets
     39
     40  gettext.textdomain('screenlets')
     41  gettext.bindtextdomain('screenlets',screenlets.INSTALL_PREFIX +  '/share/locale')
     42
     43 -def _(s):
     44          return gettext.gettext(s)
     45
     46  # quick access to dbus decorator-method (to avoid importing dbus in screenlets
     47  # and keep the possibility to create custom decorators in the future)
     48  action = dbus.service.method
     49  signal = dbus.service.signal
     50
     51
     52  # service base-class
     53 -class ScreenletService (dbus.service.Object):
     54          """The ScreenletService contains the boilerplate code for creating new
     55          dbus-objects for Screenlets. Subclasses can easily implement new functions
     56          to allow controlling and managing Screenlets through DBus in any way
     57          imaginable. This class should implement the default actions available for
     58          all screenlets - add, delete, get, set, (move?)"""
     59
     60          BUS     = 'org.screenlets' #'org.freedesktop.Screenlets'
     61          PATH    = '/org/screenlets/' #'/org/freedesktop/Screenlets/'
     62          IFACE   = 'org.screenlets.ScreenletService' #'org.freedesktop.ScreenletService'
     63
     64 -        def __init__ (self, screenlet, name, id=None):
     65                  # check types and vals
     66                  if name == '':
     67                          raise Exception('No name set in ScreenletService.__init__!');
     68                  # init props
     69                  self.screenlet  = screenlet
     70                  self.name               = name
     71                  self.BUS                = self.BUS + '.' + name
     72                  self.objpath    = self.PATH + name
     73                  if id:
     74                          self.objpath += '/' + id                # add id to path, if set
     75                  # call super
     76                  dbus.service.Object.__init__(self, dbus.service.BusName(self.BUS,
     77                          bus=dbus.SessionBus(), do_not_queue=True), self.objpath)
     78
     79          @action(IFACE)
     80 -        def test (self):
     81                  print "TEST: %s" % str(self.screenlet)
     82
     83          @action(IFACE)
     84 -        def debug (self, string):
     85                  """Dump a string to the console."""
     86                  print "DEBUG: %s" % string
     87
     88          @action(IFACE)
     89 -        def add (self, id):
     90                  """Ask the assigned Screenlet to add a new instance of itself to
     91                  its session. The new Screenlet will have the ID defined by 'id'.
     92                  The ID of the new instance is returned, so you can auto-generate an ID
     93                  by passing an empty string. The function returns None if adding a
     94                  new instance failed for some reason."""
     95                  sl = self.screenlet.session.create_instance(id)
     96                  sl.finish_loading()
     97                  if sl != None:
     98                          return sl.id
     99                  return False
    100
    101          @action(IFACE)
    102 -        def get (self, id, attrib):
    103                  """Ask the assigned Screenlet to return the given attribute's value. If
    104                  'id' is defined, the instance with the given id will be accessed,
    105                  else the main instance is used. Protected attributes are not returned
    106                  by this function.
    107                  TODO: Throw exception on error? ... could be abused to crash the app"""
    108                  if id:
    109                          sl = self.screenlet.session.get_instance_by_id(id)
    110                          if not sl:
    111                                  sl = self.screenlet
    112                          o = sl.get_option_by_name(attrib)
    113                          try:
    114                                  if not o.protected:
    115                                          return getattr(sl, attrib)
    116                                  else:
    117                                          print "Cannot get/set protected options through service."
    118                                          return None
    119                          except AttributeError:
    120                                  print 'Error getting attribute'
    121                                  return None
    122          @action(IFACE)
    123 -        def get_first_instance (self):
    124                  """Get the ID of the first existing instance of the assigned
    125                  Screenlet (within the screenlet's active session)."""
    126                  if len(self.screenlet.session.instances):
    127                          return self.screenlet.session.instances[0].id
    128                  return None
    129
    130          @action(IFACE)
    131 -        def list_instances (self):
    132                  """Return a list with IDs of all existing instances of the assigned
    133                  Screenlet (within the screenlet's active session)."""
    134                  lst = []
    135                  for sl in self.screenlet.session.instances:
    136                          lst.append (sl.id)
    137                  return lst
    138
    139          @action(IFACE)
    140 -        def quit (self):
    141                  """Quit all instances of the screenlet. Similar to selecting Quit
    142                  from the menu."""
    143                  self.screenlet.destroy(self.screenlet.window)
    144
    145          @action(IFACE)
    146 -        def set (self, id, attrib, value):
    147                  """Ask the assigned Screenlet to set the given attribute to 'value'. The
    148                  instance with the given id will be accessed. """
    149                  sl = self.screenlet.session.get_instance_by_id(id)
    150                  if sl == None:
    151                          raise Exception('Trying to access invalid instance "%s".' % id)
    152                  if sl.get_option_by_name(attrib) == None:
    153                          raise Exception('Trying to access invalid option "%s".' % attrib)
    154                  else:
    155                          try:
    156                                  o = sl.get_option_by_name(attrib)
    157                                  if not o.protected:
    158                                          setattr(sl, attrib, value)
    159                                  else:
    160                                          print "Cannot get/set protected options through service."
    161                          except: pass
    162
    163          @signal(IFACE)
    164 -        def instance_added (self, id):
    165                  """This signal gets emitted whenever a new instance of the assigned
    166                  Screenlet gets added."""
    167
    168          @signal(IFACE)
    169 -        def instance_removed (self, id):
    170                  """This signal gets emitted whenever an instance of the assigned
    171                  Screenlet gets removed."""
    172
    173
    174 -def get_service_by_name (name, interface=ScreenletService.IFACE):
    175          """This currently returns a dbus.Interface-object for remote-accessing the
    176          ScreenletService through dbus, but that may change in the future to some
    177          more abstracted system with support for multiple IPC-backends."""
    178          bus = dbus.SessionBus()
    179          if bus:
    180                  try:
    181                          path = ScreenletService.PATH + name
    182                          proxy_obj = bus.get_object(ScreenletService.BUS + '.' + name, path)
    183                          if proxy_obj:
    184                                  #return dbus.Interface(proxy_obj, ScreenletService.IFACE)
    185                                  return dbus.Interface(proxy_obj, interface)
    186                  except Exception, ex:
    187                          print "Error in screenlets.services.get_service_by_name: %s" % str(ex)
    188          return None
    189
    190 -def service_is_running (name):
    191          """Checks if the given service is available (ie. the given Screenlet has at
    192          least one running instance) and returns True or False."""
    193          bus = dbus.SessionBus()
    194          if bus:
    195                  try:
    196                          path = ScreenletService.PATH + name
    197                          if bus.get_object(ScreenletService.BUS + '.' + name, path):
    198                                  return True
    199                  except Exception:
    200                          pass
    201          return False
    202
    203  #print is_service_running('Flower')
    204