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.

198 lines
5.3KB

  1. /*
  2. mstatstab.cpp - A statistics dialog
  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 "configuration.h"
  17. #include "mmainwindow.h"
  18. #include "miscutils.h"
  19. #include "mstatstab.h"
  20. #include <tqcheckbox.h>
  21. #include <tqprocess.h>
  22. #include <tqregexp.h>
  23. #include <tqtextedit.h>
  24. #include <tqvbox.h>
  25. #include <tqvgroupbox.h>
  26. #include <tqwhatsthis.h>
  27. #include <kiconloader.h>
  28. #include <tdelocale.h>
  29. #include <kpushbutton.h>
  30. // public
  31. MStatsTab *MStatsTab::_instance = 0;
  32. MStatsTab::MStatsTab()
  33. : KDialogBase(
  34. ks_main,
  35. "MStatsTab",
  36. false, // modeless
  37. i18n("Statistics"),
  38. Close | Details,
  39. Close // default button
  40. ),
  41. _buf(TQString::null)
  42. {
  43. _process = new TQProcess(this);
  44. connect(_process, SIGNAL(processExited()), SLOT(slotProcessExit()));
  45. connect(_process, SIGNAL(readyReadStdout()), SLOT(slotReadStdout()));
  46. TQVBox *main = new TQVBox(this);
  47. setMainWidget(main);
  48. // output
  49. te_output = new TQTextEdit(main, "TQTextEdit::te_output");
  50. te_output->setMinimumSize(640, 320);
  51. te_output->setPaletteBackgroundColor(white);
  52. te_output->setPaletteForegroundColor(black);
  53. te_output->setReadOnly(true);
  54. te_output->setTextFormat(RichText); // allow HTML tags
  55. te_output->setWordWrap(TQTextEdit::NoWrap);
  56. // based on the "Linux User's Manual" (man w)
  57. TQWhatsThis::add(
  58. te_output,
  59. MiscUtils::HTML(i18n(
  60. "This view displays information about the users currently on the " \
  61. "machine, and their processes.<br>" \
  62. "The header shows how long the system has been running."
  63. ))
  64. );
  65. TQHBox *buttons = new TQHBox(main);
  66. // refresh
  67. b_refresh = new KPushButton(SmallIcon("reload"), i18n("Refresh"), buttons, "KPushButton::b_refresh");
  68. connect(b_refresh, SIGNAL(clicked()), SLOT(slotRefresh()));
  69. // options
  70. TQVGroupBox *options = new TQVGroupBox(i18n("Options"), main);
  71. // long format
  72. c_longFormat = new TQCheckBox(i18n("More information"), options);
  73. c_longFormat->setChecked(kshutdownrc->statsLongFormat);
  74. connect(c_longFormat, SIGNAL(clicked()), SLOT(slotRefresh()));
  75. MiscUtils::setHint(c_longFormat, i18n("Show login time, JCPU and PCPU times."));
  76. // toggle from field
  77. c_toggleFromField = new TQCheckBox(i18n("Toggle \"FROM\""), options);
  78. c_toggleFromField->setChecked(kshutdownrc->statsToggleFromField);
  79. MiscUtils::setHint(c_toggleFromField, i18n("Toggle the \"FROM\" (remote hostname) field."));
  80. connect(c_toggleFromField, SIGNAL(clicked()), SLOT(slotRefresh()));
  81. setButtonGuiItem(Details, KStdGuiItem::configure());
  82. setDetailsWidget(options);
  83. adjustSize();
  84. }
  85. MStatsTab::~MStatsTab()
  86. {
  87. killProcess();
  88. }
  89. // protected
  90. void MStatsTab::keyPressEvent(TQKeyEvent *e) {
  91. // Ctrl+R, F5 = Refresh
  92. if (
  93. b_refresh->isEnabled() &&
  94. (
  95. ((e->key() == Key_F5) && (e->state() == 0)) ||
  96. ((e->key() == Key_R) && (e->state() == ControlButton))
  97. )
  98. ) {
  99. slotRefresh();
  100. e->accept();
  101. }
  102. else {
  103. KDialogBase::keyPressEvent(e);
  104. }
  105. }
  106. // private
  107. void MStatsTab::killProcess() const
  108. {
  109. // kill previous process (if any)
  110. if (_process && _process->isRunning())
  111. _process->kill();
  112. }
  113. // private slots
  114. void MStatsTab::slotProcessExit() {
  115. _buf.replace("\n", "<br>");
  116. _buf.replace(" ", "&nbsp;");
  117. #define KS_HEADER(text) \
  118. _buf.replace(text, "<font color=\"#a0a0a0\">" text "</font>");
  119. #define KS_WARNING(text) \
  120. _buf.replace(TQRegExp("\\b" text "\\b"), "<font color=\"#ff0000\">" text "</font>");
  121. // underline header
  122. KS_HEADER("USER")
  123. KS_HEADER("TTY")
  124. KS_HEADER("FROM")
  125. KS_HEADER("LOGIN@")
  126. KS_HEADER("JCPU")
  127. KS_HEADER("PCPU")
  128. KS_HEADER("IDLE")
  129. KS_HEADER("WHAT")
  130. // highlight some important words
  131. // TODO: 2.0: add more keywords (e.g. backup)
  132. KS_WARNING("root")
  133. KS_WARNING("ssh")
  134. KS_WARNING("su")
  135. KS_WARNING("sudo")
  136. KS_WARNING("telnet")
  137. _buf.prepend("<pre style=\"font-family: " + TDEGlobalSettings::fixedFont().family() + "\"><b>");
  138. _buf.append("</b></pre>");
  139. te_output->setText(MiscUtils::HTML(_buf));
  140. b_refresh->setEnabled(true);
  141. }
  142. void MStatsTab::slotReadStdout() {
  143. _buf.append(_process->readStdout());
  144. }
  145. void MStatsTab::slotRefresh()
  146. {
  147. b_refresh->setEnabled(false);
  148. // update config
  149. kshutdownrc->statsLongFormat = c_longFormat->isChecked();
  150. kshutdownrc->statsToggleFromField = c_toggleFromField->isChecked();
  151. _buf = "";
  152. killProcess();
  153. _process->clearArguments();
  154. _process->addArgument("w");
  155. if (!kshutdownrc->statsLongFormat)
  156. _process->addArgument("-s"); // -s - short format
  157. if (kshutdownrc->statsToggleFromField)
  158. _process->addArgument("-f"); // -f - toggle "from" field
  159. if (!_process->start()) {
  160. _buf = "<b><font color=\"#ff0000\">" + i18n("Error") + "</font><br><br>";
  161. _buf.append(i18n("Command: %1").arg(_process->arguments().join(" ")));
  162. _buf.append("</b>");
  163. te_output->setText(_buf);
  164. b_refresh->setEnabled(true);
  165. }
  166. }
  167. #include "mstatstab.moc"