PageRenderTime 86ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/schat/trayicon.cpp

http://schat.googlecode.com/
C++ | 284 lines | 186 code | 43 blank | 55 comment | 50 complexity | 0866ced6281992b369092c66433e0793 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  1. /* $Id: trayicon.cpp 1442 2011-02-18 11:08:15Z IMPOMEZIA $
  2. * IMPOMEZIA Simple Chat
  3. * Copyright Š 2008-2011 IMPOMEZIA <schat@impomezia.com>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <QApplication>
  19. #include <QDesktopServices>
  20. #include <QFile>
  21. #include <QProcess>
  22. #include <QSound>
  23. #include <QTimer>
  24. #include "abstractprofile.h"
  25. #include "protocol.h"
  26. #include "settings.h"
  27. #include "trayicon.h"
  28. /*!
  29. * \brief ??????????? ?????? TrayIcon.
  30. */
  31. TrayIcon::TrayIcon(QObject *parent)
  32. : QSystemTrayIcon(parent),
  33. m_deferredMessage(false),
  34. m_normal(true),
  35. m_lastCheckedVersion(SimpleSettings->getString("Updates/LastVersion")),
  36. m_settings(SimpleSettings)
  37. {
  38. setStatus(schat::StatusNormal);
  39. init();
  40. }
  41. /*!
  42. * ????????/????????? ????? ???????????.
  43. *
  44. * \param enable true - ????????, false - ?????????.
  45. */
  46. void TrayIcon::notice(bool enable)
  47. {
  48. if (enable && m_timer->isActive())
  49. return;
  50. if (!enable && !m_timer->isActive())
  51. return;
  52. if (enable) {
  53. m_timer->start();
  54. setIcon(m_noticeIcon);
  55. playSound();
  56. }
  57. else {
  58. m_timer->stop();
  59. setIcon(m_icon);
  60. }
  61. m_normal = !enable;
  62. }
  63. /*!
  64. * ??? ????????????? ????????? ???? ? ???????.
  65. */
  66. void TrayIcon::playSound(const QString &key, bool force)
  67. {
  68. if (m_settings->getBool("Sound/" + key + "Enable") && !m_soundQueue.contains(key))
  69. m_soundQueue.enqueue(key);
  70. if (force)
  71. playSound();
  72. }
  73. /*!
  74. * ????????? ?????? ???? ?? ????????? ? ????.
  75. */
  76. void TrayIcon::messageClicked()
  77. {
  78. if (m_message == TrayIcon::UpdateAvailable)
  79. #ifndef SCHAT_NO_UPDATE
  80. m_settings->updatesGet();
  81. #else
  82. QDesktopServices::openUrl(QUrl("http://impomezia.ru"));
  83. #endif
  84. }
  85. void TrayIcon::notify(int code)
  86. {
  87. switch (code) {
  88. case Settings::UpdateAvailable:
  89. updateAvailable();
  90. break;
  91. case Settings::UpdateAvailableForce:
  92. updateAvailable(true);
  93. break;
  94. #ifndef SCHAT_NO_UPDATE
  95. case Settings::UpdateReady:
  96. displayMessage(UpdateReady);
  97. break;
  98. #endif
  99. default:
  100. break;
  101. }
  102. }
  103. /*!
  104. * ????????? ????????? ??????? ????????????.
  105. *
  106. * \param status ????? ??????.
  107. */
  108. void TrayIcon::setStatus(quint32 status)
  109. {
  110. if (status == schat::StatusAway || status == schat::StatusAutoAway)
  111. m_icon = QIcon(":/images/schat16-away.png");
  112. else if (status == schat::StatusDnD)
  113. m_icon = QIcon(":/images/schat16-dnd.png");
  114. else if (Settings::isNewYear())
  115. m_icon = QIcon(":/images/schat16-ny.png");
  116. else
  117. m_icon = QIcon(":/images/schat16.png");
  118. if (status != schat::StatusAutoAway && status != schat::StatusDnD && m_deferredMessage) {
  119. displayMessage(m_message);
  120. }
  121. setIcon(m_icon);
  122. }
  123. /*!
  124. * ????????? ??????? ??????? \a m_timer.
  125. * ???????? ?????? ?? ???????????????.
  126. */
  127. void TrayIcon::timeout()
  128. {
  129. if (m_normal)
  130. setIcon(m_noticeIcon);
  131. else
  132. setIcon(m_icon);
  133. playSound();
  134. m_normal = !m_normal;
  135. }
  136. /*!
  137. * ??????????? ????????? ????????? ????.
  138. */
  139. void TrayIcon::displayMessage(Message message, bool force)
  140. {
  141. QString version = m_settings->getString("Updates/LastVersion");
  142. m_message = message;
  143. if (!force) {
  144. if (SimpleSettings->profile()->status() == schat::StatusAutoAway || SimpleSettings->profile()->status() == schat::StatusDnD) {
  145. m_deferredMessage = true;
  146. return;
  147. }
  148. }
  149. m_deferredMessage = false;
  150. if (message == UpdateAvailable) {
  151. #ifndef SCHAT_NO_UPDATE
  152. showMessage(
  153. tr("Update to version %1 is available").arg(version),
  154. tr("Click here to download update right now.\n"
  155. "File size: %1").arg(bytesToHuman(m_settings->getInt("Updates/DownloadSize"))),
  156. QSystemTrayIcon::Information,
  157. 60000);
  158. #else
  159. showMessage(
  160. tr("A new version %1 is available").arg(version),
  161. tr("Click here to go to a download page"),
  162. QSystemTrayIcon::Information,
  163. 60000);
  164. #endif
  165. }
  166. #ifndef SCHAT_NO_UPDATE
  167. else if (message == UpdateReady) {
  168. showMessage(
  169. tr("Everything is ready to install version %1").arg(version),
  170. tr("Click here to install the update right now."),
  171. QSystemTrayIcon::Information,
  172. 60000);
  173. }
  174. #endif
  175. }
  176. /*!
  177. * ????????????? ?????? ??????.
  178. */
  179. void TrayIcon::init()
  180. {
  181. setToolTip(QApplication::applicationName() + " " + QApplication::applicationVersion());
  182. m_noticeIcon = QIcon(":/images/balloon.png");
  183. m_timer = new QTimer(this);
  184. m_timer->setInterval(700);
  185. connect(m_timer, SIGNAL(timeout()), SLOT(timeout()));
  186. connect(m_settings, SIGNAL(changed(int)), SLOT(notify(int)));
  187. connect(this, SIGNAL(messageClicked()), SLOT(messageClicked()));
  188. connect(m_settings->profile(), SIGNAL(statusChanged(quint32)), SLOT(setStatus(quint32)));
  189. }
  190. /*!
  191. * ????????????? ????? ?? ??????? \a m_soundQueue.
  192. */
  193. void TrayIcon::playSound()
  194. {
  195. while (!m_soundQueue.isEmpty()) {
  196. QString key = m_soundQueue.dequeue();
  197. QString file = "/" + m_settings->getString("Sound/" + key);
  198. if (!m_soundCache.contains(key)) {
  199. QStringList sounds = m_settings->path(Settings::SoundsPath);
  200. for (int i = 0; i < sounds.size(); ++i) {
  201. if (QFile::exists(sounds.at(i) + file)) {
  202. m_soundCache.insert(key, sounds.at(i) + file);
  203. break;
  204. }
  205. }
  206. }
  207. if (!m_soundCache.contains(key))
  208. return;
  209. #ifdef Q_WS_X11
  210. if (m_settings->getBool("Sound/UseExternalCmd") && !m_settings->getString("Sound/ExternalCmd").isEmpty())
  211. QProcess::startDetached(m_settings->getString("Sound/ExternalCmd").arg(m_soundCache.value(key)));
  212. else
  213. #endif
  214. QSound::play(m_soundCache.value(key));
  215. }
  216. }
  217. /*!
  218. * ??????????? ? ??????????? ????? ??????.
  219. */
  220. void TrayIcon::updateAvailable(bool force)
  221. {
  222. QString version = m_settings->getString("Updates/LastVersion");
  223. if (m_lastCheckedVersion != version || force) {
  224. displayMessage(UpdateAvailable, force);
  225. m_lastCheckedVersion = version;
  226. }
  227. }
  228. /*!
  229. * ?????????? ??????, ?????????? ??????? ??? ???????? ?????? ??????.
  230. */
  231. #ifndef SCHAT_NO_UPDATE
  232. QString TrayIcon::bytesToHuman(int size)
  233. {
  234. if (size < 1024)
  235. return tr("%n Byte", "", size);
  236. else if (size < 1048576)
  237. return tr("%1 kB").arg((int) size / 1024);
  238. else
  239. return tr("%1 MB").arg((double) size / 1048576, 0, 'f', 2);
  240. }
  241. #endif