Package dtk :: Package ui :: Module unique_service

Source Code for Module dtk.ui.unique_service

 1  #! /usr/bin/env python 
 2  # -*- coding: utf-8 -*- 
 3   
 4  # Copyright (C) 2011 ~ 2012 Deepin, Inc. 
 5  #               2011 ~ 2012 Wang Yong 
 6  # 
 7  # Author:     Wang Yong <lazycat.manatee@gmail.com> 
 8  # Maintainer: Wang Yong <lazycat.manatee@gmail.com> 
 9  # 
10  # This program is free software: you can redistribute it and/or modify 
11  # it under the terms of the GNU General Public License as published by 
12  # the Free Software Foundation, either version 3 of the License, or 
13  # any later version. 
14  # 
15  # This program is distributed in the hope that it will be useful, 
16  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
17  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
18  # GNU General Public License for more details. 
19  # 
20  # You should have received a copy of the GNU General Public License 
21  # along with this program.  If not, see <http://www.gnu.org/licenses/>. 
22   
23  from dbus.mainloop.glib import DBusGMainLoop 
24  import dbus 
25  import dbus.service 
26   
27 -class UniqueService(dbus.service.Object):
28 """ 29 This class implement a dbus interface, which is used to ensure that the program or service is unique in the system. 30 """
31 - def __init__(self, 32 bus_name, 33 app_dbus_name, 34 app_object_name, 35 unique_callback=None):
36 """ 37 Initialise the class. 38 39 @param bus_name: the public service name of the service. 40 @param app_dbus_name: the public service name of the service. 41 @param app_object_name: the public service path of the service. 42 @param unique_callback: the callback which is invoked when the service is found already start. By default, it's None. 43 """ 44 dbus.service.Object.__init__(self, bus_name, app_object_name) 45 self.unique_callback = unique_callback 46 47 # Define DBus method. 48 def unique(self): 49 if self.unique_callback: 50 self.unique_callback()
51 52 # Below code export dbus method dyanmically. 53 # Don't use @dbus.service.method ! 54 setattr(UniqueService, 'unique', dbus.service.method(app_dbus_name)(unique))
55
56 -def is_exists(app_dbus_name, app_object_name):
57 """ 58 Check the program or service is already started by its app_dbus_name and app_object_name. 59 60 @param app_dbus_name: the public service name of the service. 61 @param app_object_name: the public service path of the service. 62 @return: If the service is already on, True is returned. Otherwise return False. 63 """ 64 DBusGMainLoop(set_as_default=True) # WARING: only use once in one process 65 66 # Init dbus. 67 bus = dbus.SessionBus() 68 if bus.request_name(app_dbus_name) != dbus.bus.REQUEST_NAME_REPLY_PRIMARY_OWNER: 69 method = bus.get_object(app_dbus_name, app_object_name).get_dbus_method("unique") 70 method() 71 72 return True 73 else: 74 return False
75