KVirc – next generation IRC client
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

358 lines
9.5 KiB

  1. //=============================================================================
  2. //
  3. // File : kvi_dcophelper.cpp
  4. // Created on Sat 20 Jan 2007 12:35:21 by Alexander Stillich
  5. //
  6. // This file is part of the KVIrc IRC client distribution
  7. // Copyright (C) 2005 Szymon Stefanek <pragma at kvirc dot net>
  8. // Copyright (C) 2007 Alexander Stillich <torque at pltn dot org>
  9. //
  10. // This program is FREE software. You can redistribute it and/or
  11. // modify it under the terms of the GNU General Public License
  12. // as published by the Free Software Foundation; either version 2
  13. // of the License, or (at your opinion) 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.
  18. // See the 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, write to the Free Software Foundation,
  22. // Inc. ,51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  23. //
  24. //=============================================================================
  25. #define __KVILIB__
  26. #include "kvi_dcophelper.h"
  27. #ifdef COMPILE_TDE_SUPPORT
  28. #include "dcopclient.h"
  29. #include <tqdatastream.h>
  30. #include <tqvaluelist.h>
  31. // must be included this way, since kvilib is built
  32. // before kvirc and symlinks to headers aren't set yet
  33. #include "../../kvirc/kernel/kvi_app.h"
  34. #include "kvi_thread.h"
  35. KviDCOPHelper::KviDCOPHelper(bool bStartApp, const KviTQCString &szAppId)
  36. {
  37. m_szAppId = szAppId;
  38. }
  39. KviDCOPHelper::~KviDCOPHelper()
  40. {
  41. }
  42. bool KviDCOPHelper::ensureAppRunning(const TQString &szApp)
  43. {
  44. if (findRunningApp(m_szAppId))
  45. return true;
  46. if (m_bStartApp)
  47. return startApp(m_szAppId,400);
  48. return false;
  49. }
  50. bool KviDCOPHelper::voidRetVoidDCOPCall(const KviTQCString &szObj,const KviTQCString &szFunc)
  51. {
  52. if(!ensureAppRunning(m_szAppId))return false;
  53. TQByteArray data;
  54. return g_pApp->dcopClient()->send(m_szAppId,szObj,szFunc,data);
  55. }
  56. bool KviDCOPHelper::voidRetBoolDCOPCall(const KviTQCString &szObj,const KviTQCString &szFunc,bool bVal)
  57. {
  58. if(!ensureAppRunning(m_szAppId))return false;
  59. TQByteArray data;
  60. TQDataStream arg(data, IO_WriteOnly);
  61. arg << bVal;
  62. return g_pApp->dcopClient()->send(m_szAppId,szObj,szFunc,data);
  63. }
  64. bool KviDCOPHelper::voidRetIntDCOPCall(const KviTQCString &szObj,const KviTQCString &szFunc,int iVal)
  65. {
  66. if(!ensureAppRunning(m_szAppId))return false;
  67. TQByteArray data;
  68. TQDataStream arg(data, IO_WriteOnly);
  69. arg << iVal;
  70. return g_pApp->dcopClient()->send(m_szAppId,szObj,szFunc,data);
  71. }
  72. bool KviDCOPHelper::voidRetIntBoolDCOPCall(const KviTQCString &szObj,const KviTQCString &szFunc,int iVal, bool bVal)
  73. {
  74. if(!ensureAppRunning(m_szAppId))return false;
  75. TQByteArray data;
  76. TQDataStream arg(data, IO_WriteOnly);
  77. arg << iVal;
  78. arg << bVal;
  79. return g_pApp->dcopClient()->send(m_szAppId,szObj,szFunc,data);
  80. }
  81. bool KviDCOPHelper::voidRetIntIntIntDCOPCall(const KviTQCString &szObj,const KviTQCString &szFunc,int iVal1, int iVal2, int iVal3)
  82. {
  83. if(!ensureAppRunning(m_szAppId))return false;
  84. TQByteArray data;
  85. TQDataStream arg(data, IO_WriteOnly);
  86. arg << iVal1;
  87. arg << iVal2;
  88. arg << iVal3;
  89. return g_pApp->dcopClient()->send(m_szAppId,szObj,szFunc,data);
  90. }
  91. bool KviDCOPHelper::voidRetFloatDCOPCall(const KviTQCString &szObj,const KviTQCString &szFunc,float fVal)
  92. {
  93. if(!ensureAppRunning(m_szAppId))return false;
  94. TQByteArray data;
  95. TQDataStream arg(data, IO_WriteOnly);
  96. arg << fVal;
  97. return g_pApp->dcopClient()->send(m_szAppId,szObj,szFunc,data);
  98. }
  99. bool KviDCOPHelper::voidRetStringDCOPCall(const KviTQCString &szObj,const KviTQCString &szFunc,const TQString &szVal)
  100. {
  101. if(!ensureAppRunning(m_szAppId))return false;
  102. TQByteArray data;
  103. TQDataStream arg(data, IO_WriteOnly);
  104. arg << szVal;
  105. return g_pApp->dcopClient()->send(m_szAppId,szObj,szFunc,data);
  106. }
  107. bool KviDCOPHelper::stringRetVoidDCOPCall(const KviTQCString &szObj,const KviTQCString &szFunc,TQString &szRet)
  108. {
  109. if(!ensureAppRunning(m_szAppId))return false;
  110. TQByteArray data, replyData;
  111. KviTQCString replyType;
  112. if(!g_pApp->dcopClient()->call(m_szAppId,szObj,szFunc,data,replyType,replyData))
  113. return false;
  114. TQDataStream reply( replyData, IO_ReadOnly );
  115. if(replyType == TQSTRING_OBJECT_NAME_STRING)
  116. {
  117. reply >> szRet;
  118. return true;
  119. }
  120. return false;
  121. }
  122. bool KviDCOPHelper::stringRetIntDCOPCall(const KviTQCString &szObj,const KviTQCString &szFunc,TQString &szRet,int iVal)
  123. {
  124. if(!ensureAppRunning(m_szAppId))return false;
  125. TQByteArray data, replyData;
  126. KviTQCString replyType;
  127. TQDataStream arg(data, IO_WriteOnly);
  128. arg << iVal;
  129. if(!g_pApp->dcopClient()->call(m_szAppId,szObj,szFunc,data,replyType,replyData))
  130. return false;
  131. TQDataStream reply( replyData, IO_ReadOnly );
  132. if(replyType == TQSTRING_OBJECT_NAME_STRING)
  133. {
  134. reply >> szRet;
  135. return true;
  136. }
  137. return false;
  138. }
  139. bool KviDCOPHelper::intRetVoidDCOPCall(const KviTQCString &szObj,const KviTQCString &szFunc,int &ret)
  140. {
  141. if(!ensureAppRunning(m_szAppId))return false;
  142. TQByteArray data, replyData;
  143. KviTQCString replyType;
  144. if(!g_pApp->dcopClient()->call(m_szAppId,szObj,szFunc,data,replyType,replyData))
  145. return false;
  146. TQDataStream reply( replyData, IO_ReadOnly );
  147. if(replyType == "int")
  148. {
  149. reply >> ret;
  150. return true;
  151. }
  152. return false;
  153. }
  154. bool KviDCOPHelper::intRetIntDCOPCall(const KviTQCString &szObj,const KviTQCString &szFunc,int &ret, int iVal)
  155. {
  156. if(!ensureAppRunning(m_szAppId))return false;
  157. TQByteArray data, replyData;
  158. KviTQCString replyType;
  159. TQDataStream arg(data, IO_WriteOnly);
  160. arg << iVal;
  161. if(!g_pApp->dcopClient()->call(m_szAppId,szObj,szFunc,data,replyType,replyData))
  162. return false;
  163. TQDataStream reply( replyData, IO_ReadOnly );
  164. if(replyType == "int")
  165. {
  166. reply >> ret;
  167. return true;
  168. }
  169. return false;
  170. }
  171. bool KviDCOPHelper::boolRetVoidDCOPCall(const KviTQCString &szObj,const KviTQCString &szFunc,bool &ret)
  172. {
  173. if(!ensureAppRunning(m_szAppId))return false;
  174. TQByteArray data, replyData;
  175. KviTQCString replyType;
  176. if(!g_pApp->dcopClient()->call(m_szAppId,szObj,szFunc,data,replyType,replyData))
  177. return false;
  178. TQDataStream reply( replyData, IO_ReadOnly );
  179. if(replyType == "bool")
  180. {
  181. reply >> ret;
  182. return true;
  183. }
  184. return false;
  185. }
  186. bool KviDCOPHelper::qvalueListIntRetIntDCOPCall(const KviTQCString &szObj,const KviTQCString &szFunc,KviValueList<int> &ret, int iVal)
  187. {
  188. if(!ensureAppRunning(m_szAppId))
  189. return false;
  190. TQByteArray data, replyData;
  191. KviTQCString replyType;
  192. TQDataStream arg(data, IO_WriteOnly);
  193. arg << iVal;
  194. if(!g_pApp->dcopClient()->call(m_szAppId,szObj,szFunc,data,replyType,replyData))
  195. return false;
  196. if(replyType != "TQValueList<int>")
  197. return false;
  198. TQDataStream replyStream(replyData, IO_ReadOnly);
  199. replyStream >> ret;
  200. return true;
  201. }
  202. bool KviDCOPHelper::qcstringListRetVoidDCOPCall(const KviTQCString &szObj,const KviTQCString &szFunc,KviQCStringList &ret)
  203. {
  204. TQByteArray data, replyData;
  205. KviTQCString replyType;
  206. if (!g_pApp->dcopClient()->call(m_szAppId,szObj,szFunc,data,replyType,replyData))
  207. return false;
  208. if (replyType != "KviQCStringList")
  209. return false;
  210. TQDataStream replyStream(replyData, IO_ReadOnly);
  211. replyStream >> ret;
  212. return true;
  213. }
  214. bool KviDCOPHelper::qcstringListRetIntDCOPCall(const KviTQCString &szObj,const KviTQCString &szFunc,KviQCStringList &ret, int iVal)
  215. {
  216. TQByteArray data, replyData;
  217. KviTQCString replyType;
  218. TQDataStream arg(data, IO_WriteOnly);
  219. arg << iVal;
  220. if (!g_pApp->dcopClient()->call(m_szAppId,szObj,szFunc,data,replyType,replyData))
  221. return false;
  222. if (replyType != "KviQCStringList")
  223. return false;
  224. TQDataStream replyStream(replyData, IO_ReadOnly);
  225. replyStream >> ret;
  226. return true;
  227. }
  228. bool KviDCOPHelper::findRunningApp(const TQString &szApp)
  229. {
  230. TQValueList<KviTQCString> allApps = g_pApp->dcopClient() ->registeredApplications();
  231. TQValueList<KviTQCString>::iterator iterator;
  232. KviTQCString sz = szApp.local8Bit();
  233. for (iterator = allApps.begin();iterator != allApps.end();iterator++)
  234. {
  235. if(*iterator == sz)
  236. return true;
  237. }
  238. return false;
  239. }
  240. int KviDCOPHelper::detectApp(const TQString &szApp,bool bStart,int iScoreWhenFound,int iScoreWhenStarted)
  241. {
  242. // dcop available
  243. if(!g_pApp->dcopClient())
  244. return 0;
  245. if(findRunningApp(szApp))
  246. return 95; // found a running app, no need to run further
  247. // no app found running
  248. if(bStart)
  249. {
  250. // try to start it
  251. if(!startApp(szApp,5000))
  252. return 10; // very low possibility
  253. return findRunningApp(szApp) ? 99 : 0; // try to find it again
  254. }
  255. return 30; // it still might be installed on the system but we're just unable to start it...
  256. }
  257. bool KviDCOPHelper::startApp(const TQString &szApp,int iWaitMSecs)
  258. {
  259. // we could use TDEApplication::startServiceByDesktopName here
  260. // but we want to be able to wait a defined amount of time
  261. TQStringList tmp;
  262. TQByteArray data, replyData;
  263. KviTQCString replyType;
  264. TQDataStream arg(data, IO_WriteOnly);
  265. arg << szApp << tmp;
  266. if(!g_pApp->dcopClient()->call(
  267. "klauncher",
  268. "klauncher",
  269. "start_service_by_desktop_name(TQString,TQStringList)",
  270. data,
  271. replyType,
  272. replyData))
  273. {
  274. return false;
  275. } else {
  276. TQDataStream reply(replyData, IO_ReadOnly);
  277. if(replyType != "serviceResult")return false;
  278. int result;
  279. KviTQCString dcopName;
  280. TQString error;
  281. reply >> result >> dcopName >> error;
  282. if(result != 0)return false;
  283. }
  284. // ok , we seem to have started it.. but it might take some seconds
  285. // for the app to get registered
  286. // we wait up to five seconds
  287. if(iWaitMSecs > 0)
  288. {
  289. int i = 0;
  290. while(i < iWaitMSecs)
  291. {
  292. if(findRunningApp(szApp))return true;
  293. KviThread::msleep(100);
  294. i += 100;
  295. }
  296. return findRunningApp(szApp);
  297. }
  298. return true;
  299. }
  300. #endif //COMPILE_TDE_SUPPORT