File indexing completed on 2024-03-24 15:43:47

0001 /***************************************************************************
0002                           cstatus.cpp -  manages status bar
0003     This file is a part of KMuddy distribution.
0004                              -------------------
0005     begin                : Ne Jul 7 2002
0006     copyright            : (C) 2002 by Tomas Mecir
0007     email                : kmuddy@kmuddy.com
0008  ***************************************************************************/
0009 
0010 /***************************************************************************
0011  *                                                                         *
0012  *   This program is free software; you can redistribute it and/or modify  *
0013  *   it under the terms of the GNU General Public License as published by  *
0014  *   the Free Software Foundation; either version 2 of the License, or     *
0015  *   (at your option) any later version.                                   *
0016  *                                                                         *
0017  ***************************************************************************/
0018 
0019 #include "cstatus.h"
0020 
0021 #include <QLabel>
0022 #include <QStatusBar>
0023 #include <QTextStream>
0024 #include <KLocalizedString>
0025 
0026 #include "cprofilesettings.h"
0027 
0028 cStatus::cStatus (int sess, QStatusBar *statusbar)
0029   : cActionBase ("status", sess)
0030 {
0031   sb = statusbar;
0032 
0033 //  QLabel *labelDimension, *labelTimer, *labelIdle, *labelConnected, *labelVariables, *labelPartial;
0034 
0035   labelPartial = new QLabel();
0036   sb->addWidget(labelPartial);
0037 
0038   labelVariables = new QLabel();
0039   labelVariables->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
0040   sb->addWidget(labelVariables, 20);
0041 
0042   labelConnected = new QLabel(" " + i18n ("Off-line") + " ");
0043   sb->addPermanentWidget(labelConnected);
0044 
0045   labelDimension = new QLabel(" ??x?? ");
0046   sb->addPermanentWidget(labelDimension);
0047 
0048   labelTimer = new QLabel(" 0:00:00 ");
0049   sb->addPermanentWidget(labelTimer);
0050   
0051   labelIdle = new QLabel(" " + i18n ("idle") + " 0:00 ");
0052   sb->addPermanentWidget(labelIdle);
0053 
0054   timerShown = true;
0055 
0056   //dimension will be signaled soon - don't bother with it now...
0057 
0058   timing = false;
0059   timer = new QTimer;
0060   timer1 = new QTimer;
0061   connect (timer, &QTimer::timeout, this, &cStatus::timerTick);
0062   connect (timer1, &QTimer::timeout, this, &cStatus::timer1Tick);
0063   timer->start (1000);
0064 
0065   timerTick ();
0066 
0067   addEventHandler ("connected", 200, PT_NOTHING);
0068   addEventHandler ("disconnected", 200, PT_NOTHING);
0069   addEventHandler ("got-prompt", 100, PT_STRING);
0070   addEventHandler ("partial-line", 100, PT_STRING);
0071   addEventHandler ("command-sent", 100, PT_STRING);
0072   addEventHandler ("dimensions-changed", 100, PT_INT);
0073 }
0074 
0075 cStatus::~cStatus()
0076 {
0077   removeEventHandler ("connected");
0078   removeEventHandler ("disconnected");
0079   removeEventHandler ("got-prompt");
0080   removeEventHandler ("partial-line");
0081   removeEventHandler ("command-sent");
0082   removeEventHandler ("dimensions-changed");
0083   
0084   timer->stop ();
0085   timer1->stop ();
0086   delete timer;
0087   delete timer1;
0088   
0089   delete sb;
0090 }
0091 
0092 void cStatus::eventNothingHandler (QString event, int)
0093 {
0094   if (event == "connected") {
0095     connected ();
0096     timerReset ();
0097   }
0098   else if (event == "disconnected") {
0099     disconnected ();
0100     timerStop ();
0101   }
0102 }
0103 
0104 void cStatus::eventStringHandler (QString event, int, QString &par1, const QString &)
0105 {
0106   if (event == "got-prompt") {
0107     partialLine (par1);
0108   }
0109   else if (event == "partial-line") {
0110     partialLine (par1);
0111   }
0112   else if (event == "command-sent") {
0113     gotCommand ();
0114   }
0115 }
0116 
0117 void cStatus::eventIntHandler (QString event, int, int par1, int par2)
0118 {
0119   if (event == "dimensions-changed") {
0120     dimensionsChanged (par1, par2);
0121   }
0122 }
0123 
0124 void cStatus::showTimer ()
0125 {
0126   if (!timerShown)
0127   {
0128     timerReset ();
0129     labelTimer->setText (" 0:00:00 ");
0130   }
0131   timerShown = true;
0132 }
0133 
0134 void cStatus::hideTimer ()
0135 {
0136   if (timerShown)
0137   {
0138     timerStop ();
0139     labelTimer->setText(QString());   //item is invisible when its text is empty
0140   }
0141   timerShown = false;
0142 }
0143 
0144 void cStatus::showMessage (const QString & message)
0145 {
0146   sb->showMessage (message, 2000);
0147 }
0148 
0149 void cStatus::dimensionsChanged (int x, int y)
0150 {
0151   QString s = " " + QString::number (x) + "x" + QString::number (y) + " ";
0152   labelDimension->setText(s);
0153 }
0154 
0155 void cStatus::timerStart ()
0156 {
0157   timing = true;
0158 }
0159 
0160 void cStatus::timerStop ()
0161 {
0162   timing = false;
0163 
0164   timer1->stop ();
0165   labelIdle->setText(QString());
0166 }
0167 
0168 void cStatus::timerReset ()
0169 {
0170   timing = true;
0171   conntime = 0;
0172   idletime1 = 0;
0173   if (timerShown)
0174     labelTimer->setText (" 0:00:00 ");
0175   labelIdle->setText (" " + i18n ("idle") + " 0:00");
0176   timer1->start (1000);
0177 }
0178 
0179 void cStatus::connected ()
0180 {
0181   labelConnected->setText (" " + i18n ("Connected") + " ");
0182   labelPartial->setText (QString());
0183   labelIdle->setText (" " + i18n ("idle") + " 0:00");
0184   showMessage (i18n ("Connected."));
0185 }
0186 
0187 void cStatus::disconnected ()
0188 {
0189   labelConnected->setText (" " + i18n ("Off-line") + " ");
0190   showMessage (i18n ("Disconnected."));
0191   labelPartial->setText (QString());
0192   invokeEvent ("message", sess(), i18n ("Connection has been closed."));
0193   timerStop ();
0194 }
0195 
0196 void cStatus::partialLine (const QString &line)
0197 {
0198   //partial line displayed if enabled in profile prefs, or if this is a quick
0199   //connection
0200   cProfileSettings *sett = settings ();
0201   if ((!sett) || sett->getBool ("prompt-status"))
0202     labelPartial->setText (line);
0203   else
0204     clearPartialLine();  // clear existing prompt, if any
0205 }
0206 
0207 void cStatus::clearPartialLine ()
0208 {
0209   labelPartial->setText (QString());
0210 }
0211 
0212 void cStatus::gotCommand ()
0213 {
0214   if (timing)
0215   {
0216     idletime1 = 0;
0217     timer1->stop ();
0218     timer1->start (1000);
0219     labelIdle->setText (" " + i18n ("idle") + " 0:00 ");
0220   }
0221 }
0222 
0223 const QString cStatus::connTimeString ()
0224 {
0225   int h = conntime / 3600;
0226   int s = conntime % 3600;
0227   int m = s / 60;
0228   s = s % 60;
0229   QString str;
0230   QTextStream ts(&str);
0231   ts << " " << h << ":" << qSetFieldWidth(2) << qSetPadChar('0') << m << qSetFieldWidth(1) << ":" << qSetFieldWidth(2) << s;
0232   return str;
0233 }
0234 
0235 void cStatus::displayVariables (const QString varText)
0236 {
0237   labelVariables->setText(varText);
0238 }
0239 
0240 void cStatus::timerTick ()
0241 {
0242   QString s2;
0243   if (timing)
0244   {
0245     conntime++;
0246 
0247     s2 = connTimeString ();
0248     labelTimer->setText (s2);
0249   }
0250 }
0251 
0252 void cStatus::timer1Tick ()
0253 {
0254   ++idletime1;
0255   
0256   int h, m, s;
0257   s = idletime1 % 60;
0258   m = ((idletime1 - s) / 60) % 60;
0259   h = (idletime1 - s) / 3600;
0260   
0261   QString ss;
0262   QTextStream(&ss) << " " << h << ":" << m << ":" << s << " ";
0263   if (h > 0)
0264     ss = QString(" %1:%2:%3 ").arg(h).arg(m, 2, 10, QChar('0')).arg(s, 2, 10, QChar('0'));
0265   else
0266     ss = QString(" %1:%2 ").arg(m, 2, 10, QChar('0')).arg(s, 2, 10, QChar('0'));
0267 
0268   labelIdle->setText (" " + i18n ("idle") + ss);
0269 }
0270 
0271 #include "moc_cstatus.cpp"