00001 #include <unistd.h>
00002 #include <iostream>
00003 #include <cstdlib>
00004
00005 using namespace std;
00006
00007
00008 #include <qlayout.h>
00009 #include <qhbox.h>
00010 #include <qfile.h>
00011
00012
00013 #include <mythtv/mythcontext.h>
00014 #include <mythtv/dialogbox.h>
00015 #include <mythtv/mythdialogs.h>
00016 #include <mythtv/mythdbcon.h>
00017
00018
00019 #include <logviewer.h>
00020
00021 const int DEFAULT_UPDATE_TIME = 5;
00022
00023 LogViewer::LogViewer(MythMainWindow *parent, const char *name)
00024 : MythDialog(parent, name)
00025 {
00026 m_updateTime = gContext->GetNumSetting("LogViewerUpdateTime", DEFAULT_UPDATE_TIME);
00027
00028 QVBoxLayout *vbox = new QVBoxLayout(this, (int)(15 * wmult));
00029 QHBoxLayout *hbox = new QHBoxLayout(vbox, (int)(0 * wmult));
00030
00031
00032 QString message = tr("Log Viewer");
00033 QLabel *label = new QLabel(message, this);
00034 QFont font = label->font();
00035 font.setPointSize(int (font.pointSize() * 1.2));
00036 font.setBold(true);
00037 label->setFont(font);
00038 label->setPaletteForegroundColor(QColor("yellow"));
00039 label->setBackgroundOrigin(WindowOrigin);
00040 label->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
00041 hbox->addWidget(label);
00042
00043 hbox = new QHBoxLayout(vbox, (int)(10 * wmult));
00044 m_autoupdateCheck = new MythCheckBox( this );
00045 m_autoupdateCheck->setBackgroundOrigin(WindowOrigin);
00046 m_autoupdateCheck->setChecked(true);
00047 m_autoupdateCheck->setText("Auto Update Frequency");
00048 hbox->addWidget(m_autoupdateCheck);
00049
00050 m_updateTimeSpin = new MythSpinBox( this );
00051 m_updateTimeSpin->setMinValue(1);
00052 m_updateTimeSpin->setValue(m_updateTime);
00053 hbox->addWidget(m_updateTimeSpin);
00054
00055 message = tr("Seconds");
00056 label = new QLabel(message, this);
00057 label->setBackgroundOrigin(WindowOrigin);
00058 label->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
00059 hbox->addWidget(label);
00060
00061
00062 hbox = new QHBoxLayout(vbox, (int)(10 * wmult));
00063
00064 m_listbox = new MythListBox( this );
00065 m_listbox->setBackgroundOrigin(WindowOrigin);
00066 m_listbox->setEnabled(true);
00067 font = m_listbox->font();
00068 font.setPointSize(gContext->GetNumSetting("LogViewerFontSize", 13));
00069 font.setBold(false);
00070 m_listbox->setFont(font);
00071 hbox->addWidget(m_listbox);
00072
00073
00074 hbox = new QHBoxLayout(vbox, (int)(10 * wmult));
00075
00076
00077 hbox = new QHBoxLayout(vbox, (int)(10 * wmult));
00078
00079 m_cancelButton = new MythPushButton( this, "cancel" );
00080 m_cancelButton->setBackgroundOrigin(WindowOrigin);
00081 m_cancelButton->setText( tr( "Cancel" ) );
00082 m_cancelButton->setEnabled(true);
00083
00084 hbox->addWidget(m_cancelButton);
00085
00086
00087 m_updateButton = new MythPushButton( this, "update" );
00088 m_updateButton->setBackgroundOrigin(WindowOrigin);
00089 m_updateButton->setText( tr( "Update" ) );
00090 m_updateButton->setEnabled(true);
00091 m_updateButton->setFocus();
00092
00093 hbox->addWidget(m_updateButton);
00094
00095
00096 m_exitButton = new MythPushButton( this, "exit" );
00097 m_exitButton->setBackgroundOrigin(WindowOrigin);
00098 m_exitButton->setText( tr( "Exit" ) );
00099 m_exitButton->setEnabled(true);
00100
00101 hbox->addWidget(m_exitButton);
00102
00103 connect(m_exitButton, SIGNAL(clicked()), this, SLOT(reject()));
00104 connect(m_cancelButton, SIGNAL(clicked()), this, SLOT(cancelClicked()));
00105 connect(m_updateButton, SIGNAL(clicked()), this, SLOT(updateClicked()));
00106 connect(m_autoupdateCheck, SIGNAL(toggled(bool)), this, SLOT(toggleAutoUpdate(bool)));
00107 connect(m_updateTimeSpin, SIGNAL(valueChanged(int)),
00108 this, SLOT(updateTimeChanged(int)));
00109
00110 m_updateTimer = NULL;
00111 m_updateTimer = new QTimer(this);
00112 connect(m_updateTimer, SIGNAL(timeout()), SLOT(updateTimerTimeout()) );
00113 m_updateTimer->start(500);
00114
00115 m_popupMenu = NULL;
00116 }
00117
00118 LogViewer::~LogViewer(void)
00119 {
00120 gContext->SaveSetting("LogViewerUpdateTime", m_updateTime);
00121 gContext->SaveSetting("LogViewerFontSize", m_listbox->font().pointSize());
00122
00123 if (m_updateTimer)
00124 delete m_updateTimer;
00125 }
00126
00127 void LogViewer::updateTimerTimeout()
00128 {
00129 updateClicked();
00130 }
00131
00132 void LogViewer::toggleAutoUpdate(bool checked)
00133 {
00134 if (checked)
00135 {
00136 m_updateTimeSpin->setEnabled(true);
00137 m_updateTimer->start(m_updateTime * 1000);
00138 }
00139 else
00140 {
00141 m_updateTimeSpin->setEnabled(false);
00142 m_updateTimer->stop();
00143 }
00144 }
00145
00146 void LogViewer::updateTimeChanged(int value)
00147 {
00148 m_updateTime = value;
00149 m_updateTimer->stop();
00150 m_updateTimer->changeInterval(value * 1000);
00151 }
00152
00153 void LogViewer::cancelClicked(void)
00154 {
00155 QString tempDir = gContext->GetSetting("MythArchiveTempDir", "");
00156
00157 system("echo Cancel > " + tempDir + "/logs/mythburncancel.lck" );
00158
00159 MythPopupBox::showOkPopup(gContext->GetMainWindow(),
00160 QObject::tr("Myth Burn"),
00161 QObject::tr("Background creation has been asked to stop.\n"
00162 "This may take a few minutes."));
00163 }
00164
00165 void LogViewer::updateClicked(void)
00166 {
00167 m_updateTimer->stop();
00168
00169 QStringList list;
00170 loadFile(m_currentLog, list, m_listbox->count());
00171
00172 if (list.count() > 0)
00173 {
00174 bool bUpdateCurrent = m_listbox->currentItem() == (int) m_listbox->count() - 1;
00175 m_listbox->insertStringList(list);
00176 if (bUpdateCurrent)
00177 m_listbox->setCurrentItem(m_listbox->count() - 1);
00178 }
00179
00180 bool bRunning = (getSetting("MythArchiveLastRunStatus") == "Running");
00181 m_cancelButton->setEnabled(bRunning);
00182 m_updateButton->setEnabled(bRunning);
00183
00184 if (m_autoupdateCheck->isChecked())
00185 m_updateTimer->start(m_updateTime * 1000);
00186 }
00187
00188 QString LogViewer::getSetting(const QString &key)
00189 {
00190
00191
00192 MSqlQuery query(MSqlQuery::InitCon());
00193 if (query.isConnected())
00194 {
00195 query.prepare("SELECT data FROM settings WHERE value = :VALUE "
00196 "AND hostname = :HOSTNAME ;");
00197 query.bindValue(":VALUE", key);
00198 query.bindValue(":HOSTNAME", gContext->GetHostName());
00199
00200 if (query.exec() && query.isActive() && query.size() > 0)
00201 {
00202 query.next();
00203 return query.value(0).toString();
00204 }
00205 }
00206 else
00207 {
00208 VERBOSE(VB_IMPORTANT,
00209 QString("Database not open while trying to load setting: %1")
00210 .arg(key));
00211 }
00212
00213 return QString("");
00214 }
00215
00216 bool LogViewer::loadFile(QString filename, QStringList &list, int startline)
00217 {
00218 list.clear();
00219
00220 QFile file(filename);
00221
00222 if (!file.exists())
00223 return false;
00224
00225 if (file.open( IO_ReadOnly ))
00226 {
00227 QString s;
00228 QTextStream stream(&file);
00229
00230
00231 while ( !stream.atEnd() && startline > 0)
00232 {
00233 stream.readLine();
00234 startline--;
00235 }
00236
00237
00238 while ( !stream.atEnd() )
00239 {
00240 s = stream.readLine();
00241 list.append(s);
00242 }
00243 file.close();
00244 }
00245 else
00246 return false;
00247
00248 return true;
00249 }
00250
00251 void LogViewer::keyPressEvent(QKeyEvent *e)
00252 {
00253 bool handled = false;
00254 QStringList actions;
00255 if (gContext->GetMainWindow()->TranslateKeyPress("qt", e, actions))
00256 {
00257 for (unsigned int i = 0; i < actions.size() && !handled; i++)
00258 {
00259 QString action = actions[i];
00260 if (action == "1")
00261 {
00262 handled = true;
00263 decreaseFontSize();
00264 }
00265 else if (action == "2")
00266 {
00267 handled = true;
00268 increaseFontSize();
00269 }
00270 else if (action == "3")
00271 {
00272 handled = true;
00273 showProgressLog();
00274 }
00275 else if (action == "4")
00276 {
00277 handled = true;
00278 showFullLog();
00279 }
00280 else if (action == "MENU")
00281 {
00282 handled = true;
00283 showMenu();
00284 }
00285 }
00286 }
00287
00288 if (!handled)
00289 MythDialog::keyPressEvent(e);
00290 }
00291
00292 void LogViewer::increaseFontSize(void)
00293 {
00294 closePopupMenu();
00295
00296 QFont font = m_listbox->font();
00297 font.setPointSize(font.pointSize() + 1);
00298 m_listbox->setFont(font);
00299 }
00300
00301 void LogViewer::decreaseFontSize(void)
00302 {
00303 closePopupMenu();
00304
00305 QFont font = m_listbox->font();
00306 font.setPointSize(font.pointSize() - 1);
00307 m_listbox->setFont(font);
00308 }
00309
00310 void LogViewer::setFilenames(const QString &progressLog, const QString &fullLog)
00311 {
00312 m_progressLog = progressLog;
00313 m_fullLog = fullLog;
00314 m_currentLog = progressLog;
00315 }
00316
00317 void LogViewer::showProgressLog(void)
00318 {
00319 closePopupMenu();
00320
00321 m_listbox->clear();
00322 m_currentLog = m_progressLog;
00323 updateClicked();
00324 }
00325
00326 void LogViewer::showFullLog(void)
00327 {
00328 closePopupMenu();
00329
00330 m_listbox->clear();
00331 m_currentLog = m_fullLog;
00332 updateClicked();
00333 }
00334
00335 void LogViewer::showMenu()
00336 {
00337 if (m_popupMenu)
00338 return;
00339
00340 m_popupMenu = new MythPopupBox(gContext->GetMainWindow(),
00341 "logviewer menu");
00342
00343 QButton *button = m_popupMenu->addButton(tr("Increase Font Size"), this,
00344 SLOT(increaseFontSize()));
00345
00346 m_popupMenu->addButton(tr("Decrease Font Size"), this,
00347 SLOT(decreaseFontSize()));
00348
00349 m_popupMenu->addButton(tr("Show Progress Log"), this,
00350 SLOT(showProgressLog()));
00351 m_popupMenu->addButton(tr("Show Full Log"), this,
00352 SLOT(showFullLog()));
00353 m_popupMenu->addButton(tr("Cancel"), this,
00354 SLOT(closePopupMenu()));
00355 m_popupMenu->ShowPopup(this, SLOT(closePopupMenu()));
00356
00357 button->setFocus();
00358 }
00359
00360 void LogViewer::closePopupMenu()
00361 {
00362 if (!m_popupMenu)
00363 return;
00364
00365 m_popupMenu->deleteLater();
00366 m_popupMenu = NULL;
00367 }