KShutdown – graphical shutdown utility
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.
 
 
 

394 lines
10 KiB

  1. /*
  2. main.cpp - KShutDown
  3. Copyright (C) 2003 Konrad Twardowski <kdtonline@poczta.onet.pl>
  4. This program 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 2 of the License, or
  7. (at your option) any later version.
  8. This program 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, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. #include "actions.h"
  17. #include "configuration.h"
  18. #include "extras.h"
  19. #include "miscutils.h"
  20. #include "mmainwindow.h"
  21. #include "msystemtray.h"
  22. #include <tqfileinfo.h>
  23. #include <tdeaboutdata.h>
  24. #include <tdecmdlineargs.h>
  25. #include <tdelocale.h>
  26. #include <tdemessagebox.h>
  27. #include <kuniqueapplication.h>
  28. // 0.1.x, 0.3.x, etc - Unstable, Beta
  29. // 0.2.x, 0.4.x, etc - Stable
  30. TQString version = "1.0.4";
  31. TDEAboutData aboutData(
  32. "kshutdown", // internal name
  33. "KShutDown", // full name
  34. version.utf8(),
  35. I18N_NOOP("A Shut Down Utility for TDE"), // description
  36. TDEAboutData::License_GPL_V2, // license
  37. "(C) 2003-3000 Konrad Twardowski", // copyright
  38. "", // text
  39. "http://kshutdown.sourceforge.net/", // project page
  40. "kdtonline@poczta.onet.pl" // bugs
  41. );
  42. static TDECmdLineOptions options[] =
  43. {
  44. { "s", 0, 0 },
  45. { "shutdown", I18N_NOOP("Turn off computer"), 0 },
  46. { "h", 0, 0 },
  47. { "halt", I18N_NOOP("Turn off computer"), 0 },
  48. { "r", 0, 0 },
  49. { "reboot", I18N_NOOP("Restart computer"), 0 },
  50. { "k", 0, 0 },
  51. { "lock", I18N_NOOP("Lock session"), 0 },
  52. { "l", 0, 0 },
  53. { "logout", I18N_NOOP("End current session"), 0 },
  54. { "e", 0, 0 },
  55. { "extra <file>", I18N_NOOP("Execute \"Extras\" command (.desktop file)"), 0 },
  56. { "confirm", I18N_NOOP("Confirm command line action"), 0 },
  57. { "standard", I18N_NOOP("Show standard logout dialog"), 0 },
  58. { "c", 0, 0 },
  59. { "cancel", I18N_NOOP("Cancel an active action"), 0 },
  60. { "init", I18N_NOOP("Don't show window at startup"), 0 },
  61. { "test", I18N_NOOP("Enable test mode"), 0 },
  62. { "default", I18N_NOOP("Disable test mode"), 0 },
  63. { "+[time]", I18N_NOOP("Time; Examples: 01:30 - absolute time (HH:MM); " \
  64. "10 - number of minutes to wait from now"), 0 },
  65. TDECmdLineLastOption
  66. };
  67. /** @short KShutDown application. */
  68. class KShutDownApplication: public KUniqueApplication
  69. {
  70. public:
  71. /**
  72. * Constructor.
  73. */
  74. KShutDownApplication()
  75. : KUniqueApplication(),
  76. _confirm(false),
  77. isTimeArg(false),
  78. now(false),
  79. timeArgIsValid(false),
  80. timeArg()
  81. {
  82. }
  83. /**
  84. * Initializes the main window, and checks command line arguments.
  85. */
  86. virtual int newInstance();
  87. private:
  88. enum Mode {
  89. Mode_Visible = -1, // show the main window at startup
  90. Mode_Hidden = -2, // hide the main window at startup
  91. Mode_Ok = 0, // success
  92. Mode_Error = 1 // misc. failure
  93. };
  94. bool
  95. _confirm,
  96. isTimeArg,
  97. now,
  98. timeArgIsValid;
  99. TQString timeArg;
  100. int doExecAction(const Action::Type action);
  101. int doProcessArgs(const TDECmdLineArgs *args);
  102. };
  103. // public
  104. int KShutDownApplication::newInstance()
  105. {
  106. // autostart
  107. if (kapp->isRestored()) {
  108. TDEConfig *config = kapp->config();
  109. config->setGroup("KShutDown");
  110. if (!config->readBoolEntry("Autostart", false)) {
  111. quit();
  112. return 0;
  113. }
  114. }
  115. static bool doShow = false;
  116. bool doRaise = ks_main;
  117. // create main window (only one)
  118. if (!ks_main) {
  119. (void)new MMainWindow();
  120. kapp->setMainWidget(ks_main);
  121. connect(
  122. this, SIGNAL(aboutToQuit()),
  123. ks_main, SLOT(deleteLater()));
  124. }
  125. // check command line args
  126. TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();
  127. int retval = doProcessArgs(args);
  128. args->clear();
  129. if (MSystemTray::mode() != MSystemTray::Always) {
  130. ks_main->show();
  131. if (doRaise)
  132. ks_main->makeVisible();
  133. doShow = true;
  134. return Mode_Ok;
  135. }
  136. // gui mode
  137. if ((retval == Mode_Visible) || (retval == Mode_Hidden)) {
  138. if ((retval == Mode_Visible) && (!kapp->isRestored() || doShow)) {
  139. ks_main->show();
  140. if (doRaise)
  141. ks_main->makeVisible();
  142. }
  143. doShow = true;
  144. return Mode_Ok;
  145. }
  146. return retval; // command line exit code
  147. }
  148. // private
  149. int KShutDownApplication::doExecAction(const Action::Type action) {
  150. if (!ks_main)
  151. return Mode_Error;
  152. // use time arg.
  153. if (timeArgIsValid) {
  154. ks_main->cancel(); // cancel action (if any)
  155. if (!now) {
  156. ks_main->setAction(action);
  157. ks_main->start(_confirm);
  158. return Mode_Ok;
  159. }
  160. }
  161. if (_confirm) {
  162. if (ks_actions->execConfirm(action))
  163. return Mode_Ok;
  164. }
  165. else {
  166. if (ks_actions->exec(action, action != Action::LockScreen))
  167. return Mode_Ok;
  168. }
  169. MSystemTray::setMode((MSystemTray::Mode)kshutdownrc->systemTray);
  170. return Mode_Error;
  171. }
  172. int KShutDownApplication::doProcessArgs(const TDECmdLineArgs *args) {
  173. if (!ks_main)
  174. return Mode_Error;
  175. _confirm = args->isSet("confirm");
  176. isTimeArg = false;
  177. now = false;
  178. timeArg = "";
  179. timeArgIsValid = false;
  180. // read time arg.
  181. bool absoluteTime = false;
  182. int minutes = 0;
  183. TQDateTime dt = TQDateTime::currentDateTime();
  184. if (args->count()) {
  185. timeArg = args->arg(0); // first arg. is a "time" arg.
  186. if (!timeArg.isEmpty()) {
  187. isTimeArg = true;
  188. if (timeArg.upper() == "NOW") {
  189. now = true;
  190. timeArgIsValid = true;
  191. }
  192. else {
  193. bool ok = false;
  194. minutes = timeArg.toInt(&ok); // MM?
  195. if (ok) {
  196. if (minutes == 0) {
  197. now = true;
  198. timeArgIsValid = true;
  199. }
  200. }
  201. else {
  202. absoluteTime = true;
  203. // FIXME: 2.0: AM/PM
  204. TQTime t = TQTime::fromString(timeArg);
  205. if (t.isValid() && !t.isNull()) { // HH:MM[:SS]?
  206. ok = true;
  207. dt.setTime(t);
  208. }
  209. }
  210. if (!ok || !dt.isValid() || dt.isNull()) {
  211. KMessageBox::error(
  212. 0,
  213. MiscUtils::HTML(i18n("Invalid time: <b>%1</b>").arg(timeArg))
  214. );
  215. }
  216. else {
  217. timeArgIsValid = true;
  218. }
  219. }
  220. }
  221. }
  222. // test mode
  223. if (args->isSet("test"))
  224. ks_main->setTestMode(true);
  225. // normal mode
  226. if (args->isSet("default"))
  227. ks_main->setTestMode(false);
  228. // cancel
  229. if (args->isSet("cancel")) {
  230. ks_main->cancel();
  231. return Mode_Ok;
  232. }
  233. // standard logout dialog
  234. if (args->isSet("standard")) {
  235. kapp->requestShutDown(
  236. TDEApplication::ShutdownConfirmYes,
  237. TDEApplication::ShutdownTypeDefault,
  238. TDEApplication::ShutdownModeDefault
  239. );
  240. return Mode_Ok;
  241. }
  242. // do nothing ..
  243. if (isTimeArg && !timeArgIsValid)
  244. return Mode_Error;
  245. // setup main window
  246. if (timeArgIsValid) {
  247. if (!now) {
  248. if (MiscUtils::isRestricted("tab_time"))
  249. return Mode_Error;
  250. ks_main->cancel(); // cancel action before "setDelayType"
  251. if (absoluteTime)
  252. {
  253. ks_main->setDelayType(MMainWindow::DelayType_DateTime);
  254. ks_main->setDate(dt.date());
  255. ks_main->setTime(dt.time());
  256. }
  257. else
  258. {
  259. ks_main->setDelayType(MMainWindow::DelayType_TimeFromNow);
  260. ks_main->setTime(TQTime().addSecs(minutes * 60));
  261. }
  262. }
  263. ks_main->makeVisible();
  264. // exec action below ..
  265. }
  266. // extra
  267. if (args->isSet("extra")) {
  268. TQString extrasCommand = args->getOption("extra");
  269. if (!extrasCommand.isEmpty()) {
  270. TQFileInfo extra(extrasCommand);
  271. if (extra.exists()) {
  272. ks_extras->setAction(extra.filePath(), extra.baseName());
  273. return doExecAction(Action::Extras);
  274. }
  275. else {
  276. tqWarning("ERROR: File \"%s\" does not exist", extrasCommand.latin1());
  277. }
  278. }
  279. return Mode_Error;
  280. }
  281. // halt
  282. if (args->isSet("shutdown") || args->isSet("halt"))
  283. return doExecAction(Action::ShutDown);
  284. // reboot
  285. if (args->isSet("reboot"))
  286. return doExecAction(Action::Reboot);
  287. // lock
  288. if (args->isSet("lock"))
  289. return doExecAction(Action::LockScreen);
  290. // logout
  291. if (args->isSet("logout"))
  292. return doExecAction(Action::Logout);
  293. if (args->isSet("init"))
  294. return Mode_Hidden; // init window, but don't show it
  295. return Mode_Visible; // gui mode
  296. }
  297. // main
  298. int main(int argc, char **argv)
  299. {
  300. // TODO: 2.0: init on demand?
  301. aboutData.addCredit("Konrad Twardowski", "Author, Maintainer", "kdtonline@poczta.onet.pl", "http://www.kdtonline.prv.pl/");
  302. aboutData.addCredit("Caryn \"Hellchick\" Law", "Female Quake III Arena Announcer Voice Files", "hellchick ( at ) planetquake.com", "http://www.planetquake.com/voxfeminae/");
  303. aboutData.addCredit("Arend van Beelen jr.", "Ideas", "", "http://www.liacs.nl/~dvbeelen");
  304. aboutData.addCredit("Bram Schoenmakers", "Dutch translation", "bram_s ( at ) softhome.net");
  305. aboutData.addCredit("Charles Barcza", "Hungarian translation, blackPanther-Linux RPM", "kbarcza ( at ) blackpanther.hu", "http://www.blackpanther.hu/");
  306. aboutData.addCredit("Daniel Nylander", "Swedish translation");
  307. aboutData.addCredit("Elias Probst", "Gentoo ebuilds, German translation", "elias.probst ( at ) gmx.de");
  308. aboutData.addCredit("Giovanni Venturi", "Italian translation", "jumpyj ( at ) tiscali.it");
  309. aboutData.addCredit("Gregorio Guidi", "Patches");
  310. aboutData.addCredit("Guido Tack", "Ideas", "", "http://www.ps.uni-sb.de/~tack");
  311. aboutData.addCredit("Jozef Riha", "Slovak translation", "zefo ( at ) seznam.cz");
  312. aboutData.addCredit("Karol Adamczyk [rampage]", "Gentoo ebuild");
  313. aboutData.addCredit("Matrix", "SuSE RPM", "matrix ( at ) ehelp.pl");
  314. aboutData.addCredit("Michael Goettsche", "Bug reports");
  315. aboutData.addCredit("Paulo Zambon", "Portuguese Brazil translation", "pzambon ( at ) astornet.com.br");
  316. aboutData.addCredit("Philipp Weissenbacher");
  317. aboutData.addCredit("Piotr Budny");
  318. aboutData.addCredit("Quique", "Spanish translation", "tquique ( at ) sindominio.net");
  319. aboutData.addCredit("Robert Kratky", "Czech translation", "kratky ( at ) rob.cz");
  320. aboutData.addCredit("Romain Beauxis", "Debian Package", "", "http://www.cti.ecp.fr/~beauxir5/debian/");
  321. aboutData.addCredit("Spider (ALT Linux)", "Russian translation");
  322. aboutData.addCredit("Stephen Ellwood", "Panel Applet, User Interface");
  323. aboutData.addCredit("Zdenko Podobny", "Slovak translation, Mandrake RPM", "zdpo ( at ) mailbox.sk");
  324. aboutData.addCredit("KDE-APPS.org", "", "", "http://www.kde-apps.org/");
  325. aboutData.addCredit("Lock/Logout Applet Team");
  326. aboutData.addCredit("SourceForge.net", "", "", "http://www.sourceforge.net/");
  327. // init command line
  328. TDECmdLineArgs::init(argc, argv, &aboutData);
  329. TDECmdLineArgs::addCmdLineOptions(options);
  330. if (!KUniqueApplication::start())
  331. return 0;
  332. // init application
  333. KShutDownApplication software;
  334. // main loop
  335. return software.exec();
  336. }