PageRenderTime 63ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/Thermos/SystemTray.cpp

https://bitbucket.org/jimmyD/thermos
C++ | 461 lines | 345 code | 86 blank | 30 comment | 51 complexity | 7dcc4a4502350d30a5958414e464b9f2 MD5 | raw file
  1. //-----------------------------------------------------------------
  2. // SystemTray Object
  3. // C++ Source - SystemTray.cpp - version v1.0 (2012-04-30)
  4. // Thermos v1.0.0
  5. //-----------------------------------------------------------------
  6. //-----------------------------------------------------------------
  7. // Include Files
  8. //-----------------------------------------------------------------
  9. #include "SystemTray.h"
  10. //-----------------------------------------------------------------
  11. // SystemTray methods
  12. //-----------------------------------------------------------------
  13. SystemTray::SystemTray(Hardware * hardware, Language * lang, Settings * settings)
  14. {
  15. currentMessage = 1;
  16. language = lang;
  17. music = 0;
  18. this->settings = settings;
  19. this->hardware = hardware;
  20. this->hardware->addObserver(this);
  21. //Set system tray logo
  22. this->setIcon(QIcon("logo/thermos.png"));
  23. openWindow = false;
  24. contextMenu = new QMenu();
  25. //Make actions
  26. exitAction = new QAction(lang->gettranslation("Exit"), this);
  27. logsAction = new QAction(lang->gettranslation("Open log dir"), this);
  28. openAction = new QAction(lang->gettranslation("Open"), this);
  29. //Add actions to systemtray menu
  30. contextMenu->addAction(openAction);
  31. contextMenu->addAction(logsAction);
  32. contextMenu->addAction(exitAction);
  33. this->setContextMenu(contextMenu);
  34. //Connect the action to a slot (methode in this class)
  35. connect(exitAction, SIGNAL(triggered()), this, SLOT(programClose()));
  36. connect(logsAction, SIGNAL(triggered()), this, SLOT(openLogDir()));
  37. connect(openAction, SIGNAL(triggered()), this, SLOT(open()));
  38. connect(this, SIGNAL(messageClicked()), this, SLOT(message()));
  39. connect(this, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(systemTrayAction(QSystemTrayIcon::ActivationReason)));
  40. this->show();
  41. }
  42. SystemTray::~SystemTray(void)
  43. {
  44. this->hide();
  45. this->hardware->deleteObserver(this);
  46. delete exitAction;
  47. delete logsAction;
  48. delete openAction;
  49. delete contextMenu;
  50. }
  51. void SystemTray::systemTrayAction(QSystemTrayIcon::ActivationReason reason)
  52. {
  53. //System tray is triggered
  54. switch (reason)
  55. {
  56. case QSystemTrayIcon::Trigger:
  57. message();
  58. break;
  59. case QSystemTrayIcon::DoubleClick:
  60. open();
  61. break;
  62. default:
  63. break;
  64. }
  65. }
  66. void SystemTray::programClose()
  67. {
  68. //Close Thermos
  69. QApplication::exit(0);
  70. }
  71. void SystemTray::openLogDir()
  72. {
  73. //If log dir is exist open it
  74. if(QDir("logs").exists())
  75. {
  76. QDesktopServices::openUrl(QUrl("file:///" + QDir("logs").absolutePath(), QUrl::TolerantMode));
  77. }
  78. else
  79. {
  80. //Make logs dir and open it
  81. QDir().mkdir("logs");
  82. QDesktopServices::openUrl(QUrl("file:///" + QDir("logs").absolutePath(), QUrl::TolerantMode));
  83. }
  84. }
  85. void SystemTray::open()
  86. {
  87. //Open main window
  88. openWindow = true;
  89. QApplication::exit(1);
  90. }
  91. bool SystemTray::openMainWindow()
  92. {
  93. return openWindow;
  94. }
  95. void SystemTray::setMemory(int total, int free, int used)
  96. {
  97. //Set memoryText to text that comes in the systemtray message
  98. memoryText = "";
  99. memoryText.append(language->gettranslation("Total"));
  100. memoryText.append(": ");
  101. //Change MB to GB or TB
  102. if(total/1024/1024 > 1)
  103. {
  104. memoryText.append(QString::number((float)total/1024/1024));
  105. memoryText.append(" TB\n");
  106. }
  107. else if(total/1024 > 1)
  108. {
  109. memoryText.append(QString::number((float)total/1024));
  110. memoryText.append(" GB\n");
  111. }
  112. else
  113. {
  114. memoryText.append(QString::number(total));
  115. memoryText.append(" MB\n");
  116. }
  117. memoryText.append(language->gettranslation("Used"));
  118. memoryText.append(": ");
  119. //Change MB to GB or TB
  120. if(used/1024/1024 > 1)
  121. {
  122. memoryText.append(QString::number((float)used/1024/1024));
  123. memoryText.append(" TB\n");
  124. }
  125. else if(used/1024 > 1)
  126. {
  127. memoryText.append(QString::number((float)used/1024));
  128. memoryText.append(" GB\n");
  129. }
  130. else
  131. {
  132. memoryText.append(QString::number(used));
  133. memoryText.append(" MB\n");
  134. }
  135. memoryText.append(language->gettranslation("Free"));
  136. memoryText.append(": ");
  137. //Change MB to GB or TB
  138. if(free/1024/1024 > 1)
  139. {
  140. memoryText.append(QString::number((float)free/1024/1024));
  141. memoryText.append(" TB\n");
  142. }
  143. else if(free/1024 > 1)
  144. {
  145. memoryText.append(QString::number((float)free/1024));
  146. memoryText.append(" GB\n");
  147. }
  148. else
  149. {
  150. memoryText.append(QString::number(free));
  151. memoryText.append(" MB\n");
  152. }
  153. }
  154. void SystemTray::setCPU(int speed, int totalCores, int logicalCores, int Load, string name)
  155. {
  156. CPUtext = "";
  157. CPUtext.append(language->gettranslation("Name"));
  158. CPUtext.append(": ");
  159. CPUtext.append(QString::fromStdString(name));
  160. CPUtext.append("\n");
  161. CPUtext.append(language->gettranslation("Speed"));
  162. CPUtext.append(": ");
  163. CPUtext.append(QString::number(speed));
  164. CPUtext.append("MHz\n");
  165. CPUtext.append(language->gettranslation("Total cores"));
  166. CPUtext.append(": ");
  167. CPUtext.append(QString::number(totalCores));
  168. CPUtext.append("\n");
  169. CPUtext.append(language->gettranslation("Total logical cores"));
  170. CPUtext.append(": ");
  171. CPUtext.append(QString::number(logicalCores));
  172. CPUtext.append("\n");
  173. CPUtext.append(language->gettranslation("Load"));
  174. CPUtext.append(": ");
  175. CPUtext.append(QString::number(Load));
  176. CPUtext.append("%\n");
  177. }
  178. void SystemTray::setGPU(string * modelNames, int * totalCores, int * totalMemory, int * temperatures, int * coreUse, int gpuCount)
  179. {
  180. GPUText = "";
  181. for(int i =0; i < gpuCount; i++)
  182. {
  183. GPUText.append(language->gettranslation("Name: "));
  184. GPUText.append(QString::fromStdString(modelNames[i]));
  185. GPUText.append("\n");
  186. GPUText.append(language->gettranslation("Total cores: "));
  187. GPUText.append(QString::number(totalCores[i]));
  188. GPUText.append("\n");
  189. GPUText.append(language->gettranslation("Total memory: "));
  190. GPUText.append(QString::number(totalMemory[i]));
  191. GPUText.append(" MB\n");
  192. GPUText.append(language->gettranslation("Temperature: "));
  193. //Change temperature notation
  194. if(!settings->getDegreeCelsius())
  195. {
  196. temperatures[i] = (9/5)*temperatures[i]+32;
  197. GPUText.append(QString::number(temperatures[i]));
  198. GPUText.append(" °F\n");
  199. }
  200. else
  201. {
  202. GPUText.append(QString::number(temperatures[i]));
  203. GPUText.append(" °C\n");
  204. }
  205. GPUText.append(language->gettranslation("Used: "));
  206. GPUText.append(QString::number(gpuCount));
  207. GPUText.append(" %\n\n");
  208. }
  209. }
  210. void SystemTray::setGPU(int totalAdapters, int * temperatures, int * load, int * totalMemory, int* maxFanSpeed, int * minFanSpeed, int * fanSpeed, int * ClockSpeed, int* memoryClock, float * voltage, string * modelName, string * memoryType)
  211. {
  212. GPUText = "";
  213. if(totalAdapters != 0)
  214. {
  215. for(int i =0; i < totalAdapters; i++)
  216. {
  217. GPUText.append(language->gettranslation("Name: "));
  218. GPUText.append(QString::fromStdString(modelName[i]));
  219. GPUText.append("\n");
  220. GPUText.append(language->gettranslation("Total memory: "));
  221. GPUText.append(QString::number(totalMemory[i]));
  222. GPUText.append(" MB\n");
  223. GPUText.append(language->gettranslation("Temperature: "));
  224. //Change temperature notation
  225. if(!settings->getDegreeCelsius())
  226. {
  227. temperatures[i] = (9/5)*temperatures[i]+32;
  228. GPUText.append(QString::number(temperatures[i]));
  229. GPUText.append(" °F\n");
  230. }
  231. else
  232. {
  233. GPUText.append(QString::number(temperatures[i]));
  234. GPUText.append(" °C\n");
  235. }
  236. GPUText.append(language->gettranslation("Load: "));
  237. GPUText.append(QString::number(load[i]));
  238. GPUText.append(" %\n");
  239. GPUText.append(language->gettranslation("Maximum fan speed: "));
  240. GPUText.append(QString::number(maxFanSpeed[i]));
  241. GPUText.append(language->gettranslation(" RPM"));
  242. GPUText.append("\n");
  243. GPUText.append(language->gettranslation("Minimum fan speed: "));
  244. GPUText.append(QString::number(minFanSpeed[i]));
  245. GPUText.append(language->gettranslation(" RPM"));
  246. GPUText.append("\n");
  247. GPUText.append(language->gettranslation("Currect fan speed: "));
  248. GPUText.append(QString::number(fanSpeed[i]));
  249. GPUText.append(language->gettranslation(" RPM"));
  250. GPUText.append("\n");
  251. GPUText.append(language->gettranslation("Clock speed: "));
  252. GPUText.append(QString::number(ClockSpeed[i]));
  253. GPUText.append(language->gettranslation(" MHz"));
  254. GPUText.append("\n");
  255. GPUText.append(language->gettranslation("Memory speed: "));
  256. GPUText.append(QString::number(memoryClock[i]));
  257. GPUText.append(language->gettranslation(" MHz"));
  258. GPUText.append("\n");
  259. GPUText.append(language->gettranslation("Voltage: "));
  260. GPUText.append(QString::number(voltage[i]));
  261. GPUText.append(language->gettranslation(" V"));
  262. GPUText.append("\n");
  263. GPUText.append(language->gettranslation("Memory type: "));
  264. GPUText.append(QString::fromStdString(memoryType[i]));
  265. GPUText.append("\n\n");
  266. if(temperatures[i] > settings->getHighTemperatureAbove())
  267. {
  268. if(music != 0 && music->state() != Phonon::StoppedState)
  269. {
  270. music->stop();
  271. music = 0;
  272. }
  273. music =
  274. Phonon::createPlayer(Phonon::MusicCategory,
  275. Phonon::MediaSource("alarm.mp3"));
  276. music->play();
  277. }
  278. else if(music != 0 && music->state() != Phonon::StoppedState)
  279. {
  280. music->stop();
  281. music = 0;
  282. }
  283. }
  284. }
  285. }
  286. void SystemTray::setHardDisk(vector<string> driveLetter,vector<string> label, vector<unsigned __int64> capacity, vector<unsigned __int64> freeSpace, vector<unsigned __int64> usedSpace)
  287. {
  288. HDText ="";
  289. for(int i =1; i < label.size(); i++)
  290. {
  291. HDText.append(QString::fromStdString(label[i]));
  292. HDText.append(" (");
  293. HDText.append(QString::fromStdString(driveLetter[i]));
  294. HDText.append(")\n");
  295. HDText.append(language->gettranslation("Capacity"));
  296. HDText.append(": ");
  297. //Change MB to GB or TB
  298. if(capacity[i]/1024/1024 > 1)
  299. {
  300. HDText.append(QString::number(capacity[i]/1024/1024));
  301. HDText.append(" TB\n");
  302. }
  303. else if(capacity[i]/1024 > 1)
  304. {
  305. HDText.append(QString::number(capacity[i]/1024));
  306. HDText.append(" GB\n");
  307. }
  308. else
  309. {
  310. HDText.append(QString::number(capacity[i]));
  311. HDText.append(" MB\n");
  312. }
  313. HDText.append(language->gettranslation("Used"));
  314. HDText.append(": ");
  315. //Change MB to GB or TB
  316. if(usedSpace[i]/1024/1024 > 1)
  317. {
  318. HDText.append(QString::number(usedSpace[i]/1024/1024));
  319. HDText.append(" TB\n");
  320. }
  321. else if(usedSpace[i]/1024 > 1)
  322. {
  323. HDText.append(QString::number(usedSpace[i]/1024));
  324. HDText.append(" GB\n");
  325. }
  326. else
  327. {
  328. HDText.append(QString::number(usedSpace[i]));
  329. HDText.append(" MB\n");
  330. }
  331. HDText.append(language->gettranslation("Free"));
  332. HDText.append(": ");
  333. //Change MB to GB or TB
  334. if(freeSpace[i]/1024/1024 > 1)
  335. {
  336. HDText.append(QString::number(freeSpace[i]/1024/1024));
  337. HDText.append(" TB\n");
  338. }
  339. else if(freeSpace[i]/1024 > 1)
  340. {
  341. HDText.append(QString::number(freeSpace[i]/1024));
  342. HDText.append(" GB\n");
  343. }
  344. else
  345. {
  346. HDText.append(QString::number(freeSpace[i]));
  347. HDText.append(" MB\n");
  348. }
  349. HDText.append("\n");
  350. }
  351. }
  352. void SystemTray::message()
  353. {
  354. //Show message in message balloon
  355. if(currentMessage == 1)
  356. {
  357. this->showMessage(language->gettranslation("Memory"), memoryText, QSystemTrayIcon::NoIcon, 30000);
  358. currentMessage = 2;
  359. }
  360. else if(currentMessage == 2)
  361. {
  362. this->showMessage(language->gettranslation("CPU"), CPUtext, QSystemTrayIcon::NoIcon, 30000);
  363. currentMessage = 3;
  364. }
  365. else if(currentMessage == 3)
  366. {
  367. this->showMessage(language->gettranslation("GPU"), GPUText, QSystemTrayIcon::NoIcon, 30000);
  368. currentMessage = 4;
  369. }
  370. else
  371. {
  372. this->showMessage(language->gettranslation("HD"), HDText, QSystemTrayIcon::NoIcon, 30000);
  373. currentMessage = 1;
  374. }
  375. }