4444import logging
4545import argparse
4646import time
47+ import threading
4748
4849from pydbus import SessionBus
4950from datetime import datetime
5556class AlpacaService :
5657 """
5758 <node>
58- <interface name=' com.jeffser.Alpaca.Service' >
59- <method name=' IsRunning' >
60- <arg type='s' name=' result' direction=' out' />
59+ <interface name=" com.jeffser.Alpaca.Service" >
60+ <method name=" IsRunning" >
61+ <arg type="s" name=" result" direction=" out" />
6162 </method>
62- <method name=' Open' >
63- <arg type='s' name=' chat' direction='in' />
63+ <method name=" Open" >
64+ <arg type="s" name=" chat" direction="in" />
6465 </method>
65- <method name=' Create' >
66- <arg type='s' name=' chat' direction='in' />
66+ <method name=" Create" >
67+ <arg type="s" name=" chat" direction="in" />
6768 </method>
68- <method name=' Ask' >
69- <arg type='s' name=' message' direction='in' />
69+ <method name=" Ask" >
70+ <arg type="s" name=" message" direction="in" />
7071 </method>
71- <method name='Present'>
72- </method>
73- <method name='PresentAsk'>
74- </method>
75- <method name='PresentLive'>
76- </method>
77- <method name='Activity'>
78- <arg type='s' name='activity_name' direction='in'/>
72+ <method name="Present"/>
73+ <method name="PresentAsk"/>
74+ <method name="PresentLive"/>
75+ <method name="Activity">
76+ <arg type="s" name="activity_name" direction="in"/>
7977 </method>
8078 </interface>
8179 </node>
@@ -88,6 +86,7 @@ def IsRunning(self):
8886 return 'yeah'
8987
9088 def Present (self ):
89+ print ('present' )
9190 self .app .props .active_window .present ()
9291
9392 def PresentAsk (self ):
@@ -132,18 +131,16 @@ def __init__(self, version):
132131 self .version = version
133132 self .args = parser .parse_args ()
134133
135- def run_arguments (self ):
136- if sys .platform in ('linux' , 'linux2' ):
137- try :
138- close_at_end = True
139- app_service = SessionBus ().get ('com.jeffser.Alpaca.Service' )
140- except :
141- close_at_end = False
142- app_service = AlpacaService (self )
143- SessionBus ().publish ('com.jeffser.Alpaca.Service' , app_service )
144- else :
134+ def run_arguments (self , app_service = None ):
135+ if not app_service :
145136 app_service = AlpacaService (self )
137+ if sys .platform in ('linux' , 'linux2' ):
138+ bus = SessionBus ()
139+ dbus_proxy = bus .get ("org.freedesktop.DBus" , "/org/freedesktop/DBus" )
140+ if not dbus_proxy .NameHasOwner ('com.jeffser.Alpaca.Service' ):
141+ bus .publish ('com.jeffser.Alpaca.Service' , ('/com/jeffser/Alpaca/Service' , app_service ))
146142
143+ # Handle arguments
147144 if self .args .activity :
148145 app_service .Activity (self .args .activity )
149146 elif self .args .new_chat :
@@ -153,15 +150,12 @@ def run_arguments(self):
153150 app_service .PresentAsk ()
154151 elif self .args .ask :
155152 app_service .Ask (self .args .ask )
156- elif self .args .live_chat : # DEPRECATED
153+ elif self .args .live_chat : # DEPRECATED
157154 logger .warning ('--live-chat is deprecated, use --activity live-chat' )
158155 app_service .Activity ('live-chat' )
159156 else :
160157 app_service .Present ()
161158
162- if close_at_end :
163- sys .exit (0 )
164-
165159 def get_main_window (self , present :bool = True ):
166160 if present :
167161 self .main_alpaca_window .present ()
@@ -282,4 +276,14 @@ def main(version):
282276
283277 logger .info (f"Alpaca version: { version } " )
284278
285- return AlpacaApplication (version ).run ([])
279+ application = AlpacaApplication (version )
280+ # Check if Alpaca is already running and parse the args
281+ try :
282+ bus = SessionBus ()
283+ app_service = bus .get ('com.jeffser.Alpaca.Service' , '/com/jeffser/Alpaca/Service' )
284+ application .run_arguments (app_service ['com.jeffser.Alpaca.Service' ])
285+ sys .exit (0 )
286+ except Exception as e :
287+ pass
288+
289+ return application .run ([])
0 commit comments