PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/konversation-1.4/src/dbus.cpp

#
C++ | 412 lines | 308 code | 78 blank | 26 comment | 20 complexity | 0d3c935099041b2e66ffc7e23937a197 MD5 | raw file
Possible License(s): AGPL-1.0, CC-BY-SA-3.0
  1. /*
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or
  5. (at your option) any later version.
  6. */
  7. /*
  8. The konversation DCOP interface class
  9. begin: Mar 7 2003
  10. copyright: (C) 2003 by Alex Zepeda
  11. email: zipzippy@sonic.net
  12. */
  13. #include "dbus.h"
  14. #include "application.h"
  15. #include "connectionmanager.h"
  16. #include "awaymanager.h"
  17. #include "channel.h"
  18. #include "identity.h"
  19. #include "server.h"
  20. #include <QDBusConnection>
  21. using namespace Konversation;
  22. DBus::DBus(QObject *parent) : QObject(parent)
  23. {
  24. QDBusConnection bus = QDBusConnection::sessionBus();
  25. bus.connect("org.freedesktop.ScreenSaver", "/ScreenSaver", "org.freedesktop.ScreenSaver", "ActiveChanged", this, SLOT(changeAwayStatus(bool)));
  26. }
  27. void DBus::raw(const QString& server,const QString& command)
  28. {
  29. kDebug();
  30. // send raw IRC protocol data
  31. emit dbusRaw(sterilizeUnicode(server), sterilizeUnicode(command));
  32. }
  33. QStringList DBus::listServers()
  34. {
  35. Application* konvApp = static_cast<Application*>(kapp);
  36. QStringList hosts;
  37. const QList<Server*> serverList = konvApp->getConnectionManager()->getServerList();
  38. foreach (Server* server, serverList)
  39. if (server) hosts << server->getServerName();
  40. return hosts;
  41. }
  42. QStringList DBus::listConnectedServers()
  43. {
  44. Application* konvApp = static_cast<Application*>(kapp);
  45. QStringList connectedHosts;
  46. const QList<Server*> serverList = konvApp->getConnectionManager()->getServerList();
  47. foreach (Server* server, serverList)
  48. if (server && server->isConnected()) connectedHosts << server->getServerName();
  49. return connectedHosts;
  50. }
  51. QStringList DBus::listJoinedChannels(const QString& serverName)
  52. {
  53. QStringList joinedChannels;
  54. ConnectionManager* connectionManager = Application::instance()->getConnectionManager();
  55. Server* server = connectionManager->getServerByName(serverName, ConnectionManager::MatchByIdThenName);
  56. if (server)
  57. {
  58. const QList<Channel*>& channelList = server->getChannelList();
  59. #if QT_VERSION >= QT_VERSION_CHECK(4, 7, 0)
  60. joinedChannels.reserve(channelList.size());
  61. #endif
  62. foreach(Channel* channel, channelList)
  63. {
  64. if (channel->joined())
  65. joinedChannels.append(channel->getName());
  66. }
  67. }
  68. return joinedChannels;
  69. }
  70. void DBus::setAway(const QString& awaymessage)
  71. {
  72. static_cast<Application*>(kapp)->getAwayManager()->requestAllAway(sterilizeUnicode(awaymessage));
  73. }
  74. void DBus::setBack()
  75. {
  76. static_cast<Application*>(kapp)->getAwayManager()->requestAllUnaway();
  77. }
  78. void DBus::sayToAll(const QString &message)
  79. {
  80. emit dbusMultiServerRaw("msg " + sterilizeUnicode(message));
  81. }
  82. void DBus::actionToAll(const QString &message)
  83. {
  84. emit dbusMultiServerRaw("me " + sterilizeUnicode(message));
  85. }
  86. void DBus::say(const QString& _server,const QString& _target,const QString& _command)
  87. {
  88. //Sadly, copy on write doesn't exist with QString::replace
  89. QString server(sterilizeUnicode(_server)), target(sterilizeUnicode(_target)), command(sterilizeUnicode(_command));
  90. // TODO: this just masks a greater problem - Server::addQuery will return a query for '' --argonel
  91. // TODO: other DCOP calls need argument checking too --argonel
  92. if (server.isEmpty() || target.isEmpty() || command.isEmpty())
  93. kDebug() << "DBus::say() requires 3 arguments.";
  94. else
  95. {
  96. command.replace('\n',"\\n");
  97. command.replace('\r',"\\r");
  98. target.remove('\n');
  99. target.remove('\r');
  100. server.remove('\n');
  101. server.remove('\r');
  102. // Act as if the user typed it
  103. emit dbusSay(server,target,command);
  104. }
  105. }
  106. void DBus::info(const QString& string)
  107. {
  108. kDebug();
  109. emit dbusInfo(sterilizeUnicode(string));
  110. }
  111. void DBus::debug(const QString& string)
  112. {
  113. kDebug();
  114. emit dbusInfo(i18n("Debug: %1", sterilizeUnicode(string)));
  115. }
  116. void DBus::error(const QString& string)
  117. {
  118. kDebug();
  119. emit dbusInfo(i18n("Error: %1", sterilizeUnicode(string)));
  120. }
  121. void DBus::insertMarkerLine()
  122. {
  123. emit dbusInsertMarkerLine();
  124. }
  125. void DBus::connectToServer(const QString& address, int port, const QString& channel, const QString& password)
  126. {
  127. emit connectTo(Konversation::SilentlyReuseConnection, sterilizeUnicode(address), QString::number(port), sterilizeUnicode(password), "", sterilizeUnicode(channel));
  128. }
  129. QString DBus::getNickname(const QString& server_Name)
  130. {
  131. QString serverName(sterilizeUnicode(server_Name));
  132. Server* server = Application::instance()->getConnectionManager()->getServerByName(serverName);
  133. if (!server)
  134. {
  135. error( i18n( "getNickname: Server %1 is not found.", serverName ) );
  136. return QString();
  137. }
  138. return server->getNickname();
  139. }
  140. QString DBus::getChannelEncoding(const QString& server, const QString& channel)
  141. {
  142. return Preferences::channelEncoding(sterilizeUnicode(server), sterilizeUnicode(channel));
  143. }
  144. void DBus::changeAwayStatus(bool away)
  145. {
  146. Application* konvApp = static_cast<Application*>(kapp);
  147. if (away)
  148. {
  149. konvApp->getAwayManager()->setManagedIdentitiesAway();
  150. }
  151. else
  152. {
  153. konvApp->getAwayManager()->setManagedIdentitiesUnaway();
  154. // Simulate user activity so the whole idle time calculation
  155. // logic is being restarted. This is needed as a DBus call
  156. // could be made without any user activity involved. Simulating
  157. // user activity then correctly causes the idle-time calculation
  158. // to be restarted completely (which means the user will only
  159. // get marked as "auto-away" if the configured idle-timeout has
  160. // expired).
  161. konvApp->getAwayManager()->simulateUserActivity();
  162. }
  163. }
  164. // Identity stuff
  165. IdentDBus::IdentDBus(QObject *parent)
  166. : QObject(parent)
  167. {
  168. }
  169. QStringList IdentDBus::listIdentities()
  170. {
  171. QStringList identities;
  172. IdentityList ids = Preferences::identityList();
  173. for(IdentityList::ConstIterator it = ids.constBegin(); it != ids.constEnd(); ++it)
  174. {
  175. identities.append((*it)->getName());
  176. }
  177. sterilizeUnicode(identities);
  178. return identities;
  179. }
  180. void IdentDBus::setrealName(const QString &_id_name, const QString& name)
  181. {
  182. QString id_name(sterilizeUnicode(_id_name));
  183. IdentityList ids = Preferences::identityList();
  184. for(IdentityList::iterator it = ids.begin(); it != ids.end(); ++it)
  185. {
  186. if ((*it)->getName() == id_name)
  187. {
  188. (*it)->setRealName(sterilizeUnicode(name));
  189. return;
  190. }
  191. }
  192. }
  193. QString IdentDBus::getrealName(const QString &_id_name)
  194. {
  195. QString id_name(sterilizeUnicode(_id_name));
  196. IdentityList ids = Preferences::identityList();
  197. for(IdentityList::ConstIterator it = ids.constBegin(); it != ids.constEnd(); ++it)
  198. {
  199. if ((*it)->getName() == id_name)
  200. {
  201. return sterilizeUnicode((*it)->getRealName());
  202. }
  203. }
  204. return QString();
  205. }
  206. void IdentDBus::setIdent(const QString &/*identity*/, const QString& /*ident*/)
  207. {
  208. //Preferences::identityByName(identity)->.setIdent(;
  209. }
  210. QString IdentDBus::getIdent(const QString &identity)
  211. {
  212. return sterilizeUnicode(Preferences::identityByName(sterilizeUnicode(identity))->getIdent());
  213. }
  214. void IdentDBus::setNickname(const QString &identity, int index,const QString& nick)
  215. {
  216. const Identity *i = Preferences::identityByName(sterilizeUnicode(identity)).data();
  217. const_cast<Identity *>(i)->setNickname(index, sterilizeUnicode(nick));
  218. static_cast<Application *>(kapp)->saveOptions(true);
  219. }
  220. QString IdentDBus::getNickname(const QString &identity, int index)
  221. {
  222. return sterilizeUnicode(Preferences::identityByName(sterilizeUnicode(identity))->getNickname(index));
  223. }
  224. void IdentDBus::setBot(const QString &identity, const QString& bot)
  225. {
  226. const Identity *i = Preferences::identityByName(sterilizeUnicode(identity)).data();
  227. const_cast<Identity *>(i)->setBot(sterilizeUnicode(bot));
  228. static_cast<Application *>(kapp)->saveOptions(true);
  229. }
  230. QString IdentDBus::getBot(const QString &identity)
  231. {
  232. return sterilizeUnicode(Preferences::identityByName(sterilizeUnicode(identity))->getBot());
  233. }
  234. void IdentDBus::setPassword(const QString &identity, const QString& password)
  235. {
  236. const Identity *i = Preferences::identityByName(sterilizeUnicode(identity)).data();
  237. const_cast<Identity *>(i)->setPassword(sterilizeUnicode(password));
  238. static_cast<Application *>(kapp)->saveOptions(true);
  239. }
  240. QString IdentDBus::getPassword(const QString &identity)
  241. {
  242. return sterilizeUnicode(Preferences::identityByName(sterilizeUnicode(identity))->getPassword());
  243. }
  244. void IdentDBus::setNicknameList(const QString &identity, const QStringList& newList)
  245. {
  246. const Identity *i = Preferences::identityByName(sterilizeUnicode(identity)).data();
  247. const_cast<Identity *>(i)->setNicknameList(sterilizeUnicode(newList));
  248. static_cast<Application *>(kapp)->saveOptions(true);
  249. }
  250. QStringList IdentDBus::getNicknameList(const QString &identity)
  251. {
  252. return sterilizeUnicode(Preferences::identityByName(sterilizeUnicode(identity))->getNicknameList());
  253. }
  254. void IdentDBus::setQuitReason(const QString &identity, const QString& reason)
  255. {
  256. const Identity *i = Preferences::identityByName(sterilizeUnicode(identity)).data();
  257. const_cast<Identity *>(i)->setQuitReason(sterilizeUnicode(reason));
  258. static_cast<Application *>(kapp)->saveOptions(true);
  259. }
  260. QString IdentDBus::getQuitReason(const QString &identity)
  261. {
  262. return sterilizeUnicode(Preferences::identityByName(sterilizeUnicode(identity))->getQuitReason());
  263. }
  264. void IdentDBus::setPartReason(const QString &identity, const QString& reason)
  265. {
  266. const Identity *i = Preferences::identityByName(sterilizeUnicode(identity)).data();
  267. const_cast<Identity *>(i)->setPartReason(sterilizeUnicode(reason));
  268. static_cast<Application *>(kapp)->saveOptions(true);
  269. }
  270. QString IdentDBus::getPartReason(const QString &identity)
  271. {
  272. return sterilizeUnicode(Preferences::identityByName(sterilizeUnicode(identity))->getPartReason());
  273. }
  274. void IdentDBus::setKickReason(const QString &identity, const QString& reason)
  275. {
  276. const Identity *i = Preferences::identityByName(sterilizeUnicode(identity)).data();
  277. const_cast<Identity *>(i)->setKickReason(sterilizeUnicode(reason));
  278. static_cast<Application *>(kapp)->saveOptions(true);
  279. }
  280. QString IdentDBus::getKickReason(const QString &identity)
  281. {
  282. return sterilizeUnicode(Preferences::identityByName(sterilizeUnicode(identity))->getKickReason());
  283. }
  284. void IdentDBus::setRunAwayCommands(const QString &identity, bool run)
  285. {
  286. const Identity *i = Preferences::identityByName(sterilizeUnicode(identity)).data();
  287. const_cast<Identity *>(i)->setRunAwayCommands(run);
  288. static_cast<Application *>(kapp)->saveOptions(true);
  289. }
  290. bool IdentDBus::getRunAwayCommands(const QString &identity)
  291. {
  292. return Preferences::identityByName(sterilizeUnicode(identity))->getRunAwayCommands();
  293. }
  294. void IdentDBus::setAwayCommand(const QString &identity, const QString& command)
  295. {
  296. const Identity *i = Preferences::identityByName(sterilizeUnicode(identity)).data();
  297. const_cast<Identity *>(i)->setAwayCommand(sterilizeUnicode(command));
  298. static_cast<Application *>(kapp)->saveOptions(true);
  299. }
  300. QString IdentDBus::getAwayCommand(const QString &identity)
  301. {
  302. return sterilizeUnicode(Preferences::identityByName(identity)->getAwayCommand());
  303. }
  304. void IdentDBus::setReturnCommand(const QString &identity, const QString& command)
  305. {
  306. const Identity *i = Preferences::identityByName(sterilizeUnicode(identity)).data();
  307. const_cast<Identity *>(i)->setReturnCommand(sterilizeUnicode(command));
  308. static_cast<Application *>(kapp)->saveOptions(true);
  309. }
  310. QString IdentDBus::getReturnCommand(const QString &identity)
  311. {
  312. return sterilizeUnicode(Preferences::identityByName(sterilizeUnicode(identity))->getReturnCommand());
  313. }
  314. void IdentDBus::setAwayMessage(const QString &identity, const QString& message)
  315. {
  316. const Identity *i = Preferences::identityByName(sterilizeUnicode(identity)).data();
  317. const_cast<Identity *>(i)->setAwayMessage(sterilizeUnicode(message));
  318. static_cast<Application *>(kapp)->saveOptions(true);
  319. }
  320. QString IdentDBus::getAwayMessage(const QString &identity)
  321. {
  322. return sterilizeUnicode(Preferences::identityByName(sterilizeUnicode(identity))->getAwayMessage());
  323. }
  324. void IdentDBus::setAwayNickname(const QString &identity, const QString& nickname)
  325. {
  326. const Identity *i = Preferences::identityByName(sterilizeUnicode(identity)).data();
  327. const_cast<Identity *>(i)->setAwayNickname(sterilizeUnicode(nickname));
  328. static_cast<Application *>(kapp)->saveOptions(true);
  329. }
  330. QString IdentDBus::getAwayNickname(const QString &identity)
  331. {
  332. return sterilizeUnicode(Preferences::identityByName(sterilizeUnicode(identity))->getAwayNickname());
  333. }
  334. #include "dbus.moc"