PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/actions/system/src/code/system.cpp

https://github.com/Jmgr/actionaz
C++ | 332 lines | 251 code | 58 blank | 23 comment | 24 complexity | d209fe18ad278081dab984b5928438f6 MD5 | raw file
  1. /*
  2. Actiona
  3. Copyright (C) 2005 Jonathan Mercier-Ganady
  4. Actiona is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Actiona is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. Contact: jmgr@jmgr.info
  15. */
  16. #include "system.hpp"
  17. #include "actiontools/code/rect.hpp"
  18. #include "../systemsession.hpp"
  19. #include <QStandardPaths>
  20. #include "systeminfo/qdeviceinfo.h"
  21. #include "systeminfo/qbatteryinfo.h"
  22. #include "systeminfo/qstorageinfo.h"
  23. #include <QLocale>
  24. #include <QDesktopServices>
  25. #include <QDesktopWidget>
  26. #include <QApplication>
  27. #include <QUrl>
  28. #include <QDir>
  29. #include <QDateTime>
  30. #include <cstdlib>
  31. #include <QScreen>
  32. #ifdef Q_OS_WIN
  33. #include <Windows.h>
  34. #include <LMCons.h>
  35. #endif
  36. namespace Code
  37. {
  38. QScriptValue System::constructor(QScriptContext *context, QScriptEngine *engine)
  39. {
  40. return CodeClass::constructor(new System, context, engine);
  41. }
  42. System::System()
  43. : CodeClass(),
  44. mSystemSession(new SystemSession)
  45. , mDeviceInfo(new QDeviceInfo(this)),
  46. mBatteryInfo(new QBatteryInfo(this)),
  47. mStorageInfo(new QStorageInfo_Custom(this))
  48. {
  49. }
  50. System::~System()
  51. {
  52. delete mSystemSession;
  53. }
  54. QString System::storageLocationPath(StorageLocation location) const
  55. {
  56. return QStandardPaths::locate(static_cast<QStandardPaths::StandardLocation>(location), QString(), QStandardPaths::LocateDirectory);
  57. }
  58. QString System::storageLocationName(StorageLocation location) const
  59. {
  60. return QStandardPaths::displayName(static_cast<QStandardPaths::StandardLocation>(location));
  61. }
  62. QScriptValue System::openUrl(const QString &url) const
  63. {
  64. if(!QDesktopServices::openUrl(QUrl(url)))
  65. throwError(QStringLiteral("OpenUrlError"), tr("Cannot open the url"));
  66. return thisObject();
  67. }
  68. int System::screenCount() const
  69. {
  70. return QGuiApplication::screens().size();
  71. }
  72. QScriptValue System::availableGeometry(int screen) const
  73. {
  74. auto screens = QGuiApplication::screens();
  75. if(screen < 0 || screen >= screens.size())
  76. {
  77. return Rect::constructor({}, engine());
  78. }
  79. return Rect::constructor(screens[screen]->availableGeometry(), engine());
  80. }
  81. QScriptValue System::screenGeometry(int screen) const
  82. {
  83. auto screens = QGuiApplication::screens();
  84. if(screen < 0 || screen >= screens.size())
  85. {
  86. return Rect::constructor({}, engine());
  87. }
  88. return Rect::constructor(screens[screen]->geometry(), engine());
  89. }
  90. int System::primaryScreen() const
  91. {
  92. auto screens = QGuiApplication::screens();
  93. for(int i = 0; i < screens.size(); i++)
  94. {
  95. if(screens[i] == QGuiApplication::primaryScreen())
  96. {
  97. return i;
  98. }
  99. }
  100. return 0;
  101. }
  102. bool System::isVirtualDesktop() const
  103. {
  104. auto primaryScreen = QGuiApplication::primaryScreen();
  105. return !primaryScreen->virtualSiblings().empty();
  106. }
  107. QString System::currentDirectory() const
  108. {
  109. return QDir::currentPath();
  110. }
  111. QString System::username() const
  112. {
  113. #ifdef Q_OS_WIN
  114. TCHAR buffer[UNLEN+1];
  115. DWORD size = sizeof(buffer);
  116. GetUserName(buffer, &size);
  117. return QString::fromWCharArray(buffer);
  118. #else
  119. return QString::fromLatin1(std::getenv("USER"));
  120. #endif
  121. }
  122. QString System::variable(const QString &name) const
  123. {
  124. return QString::fromLatin1(std::getenv(name.toLatin1().constData()));
  125. }
  126. qint64 System::timestamp() const
  127. {
  128. return QDateTime::currentDateTimeUtc().currentSecsSinceEpoch();
  129. }
  130. QString System::osName() const
  131. {
  132. #if defined(Q_OS_LINUX)
  133. return QStringLiteral("GNU/Linux");
  134. #elif defined(Q_OS_WIN)
  135. return QStringLiteral("Windows");
  136. #else
  137. return QStringLiteral("Unknown");
  138. #endif
  139. }
  140. QString System::version() const
  141. {
  142. return mDeviceInfo->version(QDeviceInfo::Os);
  143. }
  144. QString System::countryCode() const
  145. {
  146. QString localeName = QLocale::system().name();
  147. QStringList localeParts = localeName.split(QLatin1Char('_'));
  148. if(localeParts.size() >= 2)
  149. return localeParts[1];
  150. else
  151. return QString();
  152. }
  153. QString System::language() const
  154. {
  155. QString localeName = QLocale::system().name();
  156. QStringList localeParts = localeName.split(QLatin1Char('_'));
  157. if(localeParts.size() >= 2)
  158. return localeParts[0];
  159. else
  160. return QString();
  161. }
  162. QStringList System::logicalDrives() const
  163. {
  164. return mStorageInfo->allLogicalDrives();
  165. }
  166. qlonglong System::availableDiskSpace(const QString &drive) const
  167. {
  168. return mStorageInfo->availableDiskSpace(drive);
  169. }
  170. qlonglong System::totalDiskSpace(const QString &drive) const
  171. {
  172. return mStorageInfo->totalDiskSpace(drive);
  173. }
  174. System::DriveType System::driveType(const QString &drive) const
  175. {
  176. return static_cast<DriveType>(mStorageInfo->driveType(drive));
  177. }
  178. int System::colorDepth(int screenId) const
  179. {
  180. Q_UNUSED(screenId)
  181. return 0;// Sadly, no easy way to get this information
  182. //Qt 4: int screen = (screenId == -1 ? QApplication::desktop()->primaryScreen() : screenId);
  183. //Qt 4: return mSystemDisplayInfo->colorDepth(screen);
  184. }
  185. int System::displayBrightness(int screenId) const
  186. {
  187. Q_UNUSED(screenId)
  188. return 0;// Sadly, no easy way to get this information
  189. //Qt 4: int screen = (screenId == -1 ? QApplication::desktop()->primaryScreen() : screenId);
  190. //Qt 4: return mSystemDisplayInfo->displayBrightness(screen);
  191. }
  192. int System::batteryLevel() const
  193. {
  194. if(mBatteryInfo->batteryCount() == 0)
  195. return -1;
  196. if(mBatteryInfo->remainingCapacity() == -1 || mBatteryInfo->maximumCapacity() <= 0)
  197. return -1;
  198. return (mBatteryInfo->remainingCapacity() * 100) / mBatteryInfo->maximumCapacity();
  199. }
  200. System::PowerState System::powerState() const
  201. {
  202. if(mBatteryInfo->batteryCount() == 0)
  203. return UnknownState;
  204. switch(mBatteryInfo->chargingState())
  205. {
  206. case QBatteryInfo::Discharging:
  207. return BatteryPower;
  208. case QBatteryInfo::Charging:
  209. return WallPowerChargingBattery;
  210. case QBatteryInfo::IdleChargingState:
  211. return WallPower;
  212. default:
  213. return UnknownState;
  214. }
  215. }
  216. QString System::manufacturer() const
  217. {
  218. return mDeviceInfo->manufacturer();
  219. }
  220. QString System::model() const
  221. {
  222. return mDeviceInfo->model();
  223. }
  224. QString System::productName() const
  225. {
  226. return mDeviceInfo->productName();
  227. }
  228. QScriptValue System::logout(bool force) const
  229. {
  230. if(!mSystemSession->logout(force))
  231. throwError(QStringLiteral("LogoutError"), tr("Logout failed"));
  232. return thisObject();
  233. }
  234. QScriptValue System::restart(bool force) const
  235. {
  236. if(!mSystemSession->restart(force))
  237. throwError(QStringLiteral("RestartError"), tr("Restart failed"));
  238. return thisObject();
  239. }
  240. QScriptValue System::shutdown(bool force) const
  241. {
  242. if(!mSystemSession->shutdown(force))
  243. throwError(QStringLiteral("ShutdownError"), tr("Shutdown failed"));
  244. return thisObject();
  245. }
  246. QScriptValue System::suspend(bool force) const
  247. {
  248. if(!mSystemSession->suspend(force))
  249. throwError(QStringLiteral("SuspendError"), tr("Suspend failed"));
  250. return thisObject();
  251. }
  252. QScriptValue System::hibernate(bool force) const
  253. {
  254. if(!mSystemSession->hibernate(force))
  255. throwError(QStringLiteral("HibernateError"), tr("Hibernate failed"));
  256. return thisObject();
  257. }
  258. QScriptValue System::lockScreen() const
  259. {
  260. if(!mSystemSession->lockScreen())
  261. throwError(QStringLiteral("LockScreenError"), tr("Lock screen failed"));
  262. return thisObject();
  263. }
  264. QScriptValue System::startScreenSaver() const
  265. {
  266. if(!mSystemSession->startScreenSaver())
  267. throwError(QStringLiteral("StartScreenSaverError"), tr("Start screen saver failed"));
  268. return thisObject();
  269. }
  270. }