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.
 
 
 
 
 
 

225 lines
6.4 KiB

  1. //=============================================================================
  2. //
  3. // File : mp_dcopinterface.cpp
  4. // Created on Sun 27 Mar 2005 16:56:21 by Szymon Stefanek
  5. //
  6. // This file is part of the KVIrc IRC client distribution
  7. // Copyright (C) 2005 Szymon Stefanek <pragma at kvirc dot net>
  8. //
  9. // This program is FREE software. You can redistribute it and/or
  10. // modify it under the terms of the GNU General Public License
  11. // as published by the Free Software Foundation; either version 2
  12. // of the License, or (at your opinion) any later version.
  13. //
  14. // This program is distributed in the HOPE that it will be USEFUL,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  17. // See the GNU General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public License
  20. // along with this program. If not, write to the Free Software Foundation,
  21. // Inc. ,51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. //
  23. //=============================================================================
  24. #include "mp_dcopinterface.h"
  25. #ifdef COMPILE_TDE_SUPPORT
  26. #include "dcopclient.h"
  27. #include <tqdatastream.h>
  28. #include <tqvaluelist.h>
  29. #include "kvi_app.h"
  30. #include "kvi_thread.h"
  31. KviMediaPlayerDCOPInterface::KviMediaPlayerDCOPInterface(const KviTQCString &szAppId)
  32. : KviMediaPlayerInterface()
  33. {
  34. m_szAppId = szAppId;
  35. }
  36. KviMediaPlayerDCOPInterface::~KviMediaPlayerDCOPInterface()
  37. {
  38. }
  39. bool KviMediaPlayerDCOPInterface::ensureAppRunning(const TQString &szApp)
  40. {
  41. if(findRunningApp(m_szAppId))return true;
  42. return startApp(m_szAppId,400);
  43. }
  44. bool KviMediaPlayerDCOPInterface::simpleDCOPCall(const KviTQCString &szObj,const KviTQCString &szFunc)
  45. {
  46. if(!ensureAppRunning(m_szAppId))return false;
  47. TQByteArray data;
  48. return g_pApp->dcopClient()->send(m_szAppId,szObj,szFunc,data);
  49. }
  50. bool KviMediaPlayerDCOPInterface::boolDCOPCall(const KviTQCString &szObj,const KviTQCString &szFunc,bool bVal)
  51. {
  52. if(!ensureAppRunning(m_szAppId))return false;
  53. TQByteArray data;
  54. TQDataStream arg(data, IO_WriteOnly);
  55. arg << bVal;
  56. return g_pApp->dcopClient()->send(m_szAppId,szObj,szFunc,data);
  57. }
  58. bool KviMediaPlayerDCOPInterface::intDCOPCall(const KviTQCString &szObj,const KviTQCString &szFunc,int iVal)
  59. {
  60. if(!ensureAppRunning(m_szAppId))return false;
  61. TQByteArray data;
  62. TQDataStream arg(data, IO_WriteOnly);
  63. arg << iVal;
  64. return g_pApp->dcopClient()->send(m_szAppId,szObj,szFunc,data);
  65. }
  66. bool KviMediaPlayerDCOPInterface::floatDCOPCall(const KviTQCString &szObj,const KviTQCString &szFunc,float fVal)
  67. {
  68. if(!ensureAppRunning(m_szAppId))return false;
  69. TQByteArray data;
  70. TQDataStream arg(data, IO_WriteOnly);
  71. arg << fVal;
  72. return g_pApp->dcopClient()->send(m_szAppId,szObj,szFunc,data);
  73. }
  74. bool KviMediaPlayerDCOPInterface::stringDCOPCall(const KviTQCString &szObj,const KviTQCString &szFunc,TQString szVal)
  75. {
  76. if(!ensureAppRunning(m_szAppId))return false;
  77. TQByteArray data;
  78. TQDataStream arg(data, IO_WriteOnly);
  79. arg << szVal;
  80. return g_pApp->dcopClient()->send(m_szAppId,szObj,szFunc,data);
  81. }
  82. bool KviMediaPlayerDCOPInterface::stringRetDCOPCall(const KviTQCString &szObj,const KviTQCString &szFunc,TQString &ret)
  83. {
  84. if(!ensureAppRunning(m_szAppId))return false;
  85. TQString title;
  86. TQByteArray data, replyData;
  87. KviTQCString replyType;
  88. if(!g_pApp->dcopClient()->call(m_szAppId,szObj,szFunc,data,replyType,replyData))
  89. return false;
  90. TQDataStream reply( replyData, IO_ReadOnly );
  91. if(replyType == TQSTRING_OBJECT_NAME_STRING)
  92. {
  93. reply >> ret;
  94. return true;
  95. }
  96. return false;
  97. }
  98. bool KviMediaPlayerDCOPInterface::intRetDCOPCall(const KviTQCString &szObj,const KviTQCString &szFunc,int &ret)
  99. {
  100. if(!ensureAppRunning(m_szAppId))return false;
  101. TQString title;
  102. TQByteArray data, replyData;
  103. KviTQCString replyType;
  104. if(!g_pApp->dcopClient()->call(m_szAppId,szObj,szFunc,data,replyType,replyData))
  105. return false;
  106. TQDataStream reply( replyData, IO_ReadOnly );
  107. if(replyType == "int")
  108. {
  109. reply >> ret;
  110. return true;
  111. }
  112. return false;
  113. }
  114. bool KviMediaPlayerDCOPInterface::boolRetDCOPCall(const KviTQCString &szObj,const KviTQCString &szFunc,bool &ret)
  115. {
  116. if(!ensureAppRunning(m_szAppId))return false;
  117. TQString title;
  118. TQByteArray data, replyData;
  119. KviTQCString replyType;
  120. if(!g_pApp->dcopClient()->call(m_szAppId,szObj,szFunc,data,replyType,replyData))
  121. return false;
  122. TQDataStream reply( replyData, IO_ReadOnly );
  123. if(replyType == "bool")
  124. {
  125. reply >> ret;
  126. return true;
  127. }
  128. return false;
  129. }
  130. bool KviMediaPlayerDCOPInterface::findRunningApp(const TQString &szApp)
  131. {
  132. TQValueList<KviTQCString> allApps = g_pApp->dcopClient() ->registeredApplications();
  133. TQValueList<KviTQCString>::iterator iterator;
  134. KviTQCString sz = szApp.local8Bit();
  135. for(iterator = allApps.begin();iterator != allApps.end();iterator++)
  136. {
  137. if(*iterator == sz)return true;
  138. }
  139. return false;
  140. }
  141. int KviMediaPlayerDCOPInterface::detectApp(const TQString &szApp,bool bStart,int iScoreWhenFound,int iScoreWhenStarted)
  142. {
  143. // lookup the amarok interface
  144. if(!g_pApp->dcopClient())return 0;
  145. if(findRunningApp(szApp))return 95; // found a running amarok, no need to run further
  146. // no amarok app found
  147. if(bStart)
  148. {
  149. // try to start it
  150. if(!startApp(szApp,5000))return 10; // very low possibility
  151. return findRunningApp(szApp) ? 99 : 0; // try to find it again
  152. }
  153. return 30; // it still might be installed on the system but we're just unable to start it...
  154. }
  155. bool KviMediaPlayerDCOPInterface::startApp(const TQString &szApp,int iWaitMSecs)
  156. {
  157. // we could use TDEApplication::startServiceByDesktopName here
  158. // but we want to be able to wait a defined amount of time
  159. TQStringList tmp;
  160. TQByteArray data, replyData;
  161. KviTQCString replyType;
  162. TQDataStream arg(data, IO_WriteOnly);
  163. arg << szApp << tmp;
  164. if(!g_pApp->dcopClient()->call(
  165. "klauncher",
  166. "klauncher",
  167. "start_service_by_desktop_name(TQString,TQStringList)",
  168. data,
  169. replyType,
  170. replyData))
  171. {
  172. return false;
  173. } else {
  174. TQDataStream reply(replyData, IO_ReadOnly);
  175. if(replyType != "serviceResult")return false;
  176. int result;
  177. KviTQCString dcopName;
  178. TQString error;
  179. reply >> result >> dcopName >> error;
  180. if(result != 0)return false;
  181. }
  182. // ok , we seem to have started it.. but it might take some seconds
  183. // for the app to get registered
  184. // we wait up to five seconds
  185. if(iWaitMSecs > 0)
  186. {
  187. int i = 0;
  188. while(i < iWaitMSecs)
  189. {
  190. if(findRunningApp(szApp))return true;
  191. KviThread::msleep(100);
  192. i += 100;
  193. }
  194. return findRunningApp(szApp);
  195. }
  196. return true;
  197. }
  198. #endif //COMPILE_TDE_SUPPORT