00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include <iostream>
00016 #include <unistd.h>
00017 #include <cstdlib>
00018
00019
00020 #include <QTimer>
00021
00022
00023 #include <mythcontext.h>
00024 #include <mythdbcon.h>
00025 #include <mythmainwindow.h>
00026
00027
00028 #include "zmconsole.h"
00029 #include "zmclient.h"
00030
00031 const int STATUS_UPDATE_TIME = 1000 * 10;
00032 const int TIME_UPDATE_TIME = 1000 * 1;
00033
00034 FunctionDialog::FunctionDialog(MythScreenStack *parent, Monitor *monitor) :
00035 MythScreenType(parent, "functionpopup"), m_monitor(monitor),
00036 m_captionText(NULL), m_functionList(NULL),
00037 m_enabledCheck(NULL), m_okButton(NULL)
00038 {
00039 }
00040
00041 bool FunctionDialog::Create()
00042 {
00043 if (!LoadWindowFromXML("zoneminder-ui.xml", "functionpopup", this))
00044 return false;
00045
00046 bool err = false;
00047 UIUtilE::Assign(this, m_captionText, "caption_text", &err);
00048 UIUtilE::Assign(this, m_functionList, "function_list", &err);
00049 UIUtilE::Assign(this, m_enabledCheck, "enable_check", &err);
00050 UIUtilE::Assign(this, m_okButton, "ok_button", &err);
00051
00052 if (err)
00053 {
00054 LOG(VB_GENERAL, LOG_ERR, "Cannot load screen 'functionpopup'");
00055 return false;
00056 }
00057
00058 new MythUIButtonListItem(m_functionList, "Monitor");
00059 new MythUIButtonListItem(m_functionList, "Modect");
00060 new MythUIButtonListItem(m_functionList, "Nodect");
00061 new MythUIButtonListItem(m_functionList, "Record");
00062 new MythUIButtonListItem(m_functionList, "Mocord");
00063 new MythUIButtonListItem(m_functionList, "None");
00064
00065 m_functionList->MoveToNamedPosition(m_monitor->function);
00066
00067 m_captionText->SetText(m_monitor->name);
00068
00069 m_okButton->SetText(tr("OK"));
00070
00071 connect(m_okButton, SIGNAL(Clicked()), this, SLOT(setMonitorFunction()));
00072
00073 if (m_monitor->enabled)
00074 m_enabledCheck->SetCheckState(MythUIStateType::Full);
00075 else
00076 m_enabledCheck->SetCheckState(MythUIStateType::Off);
00077
00078 BuildFocusList();
00079
00080 SetFocusWidget(m_functionList);
00081
00082 return true;
00083 }
00084
00085 void FunctionDialog::setMonitorFunction(void)
00086 {
00087 QString function = m_functionList->GetValue();
00088 bool enabled = (m_enabledCheck->GetCheckState() == MythUIStateType::Full);
00089
00090 LOG(VB_GENERAL, LOG_INFO,
00091 "Monitor id : " + QString::number(m_monitor->id) +
00092 " function change " + m_monitor->function + " -> " + function +
00093 ", enable value " + QString::number(m_monitor->enabled) + " -> " +
00094 QString::number(enabled));
00095
00096 if (m_monitor->function == function && m_monitor->enabled == enabled)
00097 {
00098 LOG(VB_GENERAL, LOG_WARNING,
00099 "Monitor Function/Enable values not changed so not updating.");
00100 emit haveResult(false);
00101 Close();
00102 return;
00103 }
00104
00105 if (class ZMClient *zm = ZMClient::get())
00106 zm->setMonitorFunction(m_monitor->id, function, enabled);
00107
00108 emit haveResult(true);
00109
00110 Close();
00111 }
00112
00114
00115 ZMConsole::ZMConsole(MythScreenStack *parent)
00116 :MythScreenType(parent, "zmconsole")
00117 {
00118 m_monitorListSize = 0;
00119 m_currentMonitor = 0;
00120
00121 m_monitorList = NULL;
00122
00123 m_timeFormat = gCoreContext->GetSetting("TimeFormat", "h:mm AP");
00124
00125 m_timeTimer = new QTimer(this);
00126 connect(m_timeTimer, SIGNAL(timeout()), this,
00127 SLOT(updateTime()));
00128
00129 m_updateTimer = new QTimer(this);
00130 connect(m_updateTimer, SIGNAL(timeout()), this,
00131 SLOT(updateStatus()));
00132
00133 m_popupStack = GetMythMainWindow()->GetStack("popup stack");
00134 m_functionDialog = NULL;
00135 }
00136
00137 ZMConsole::~ZMConsole()
00138 {
00139 delete m_timeTimer;
00140
00141 if (m_monitorList)
00142 delete m_monitorList;
00143 }
00144
00145 bool ZMConsole::Create(void)
00146 {
00147 bool foundtheme = false;
00148
00149
00150 foundtheme = LoadWindowFromXML("zoneminder-ui.xml", "zmconsole", this);
00151
00152 if (!foundtheme)
00153 return false;
00154
00155 bool err = false;
00156 UIUtilE::Assign(this, m_monitor_list, "monitor_list", &err);
00157 UIUtilE::Assign(this, m_status_text, "status_text", &err);
00158 UIUtilE::Assign(this, m_time_text, "time_text", &err);
00159 UIUtilE::Assign(this, m_date_text, "date_text", &err);
00160 UIUtilE::Assign(this, m_load_text, "load_text", &err);
00161 UIUtilE::Assign(this, m_disk_text, "disk_text", &err);
00162
00163 if (err)
00164 {
00165 LOG(VB_GENERAL, LOG_ERR, "Cannot load screen 'zmconsole'");
00166 return false;
00167 }
00168
00169 BuildFocusList();
00170
00171 SetFocusWidget(m_monitor_list);
00172
00173 m_timeTimer->start(TIME_UPDATE_TIME);
00174 m_updateTimer->start(100);
00175
00176 updateTime();
00177
00178 return true;
00179 }
00180
00181 void ZMConsole::updateTime(void)
00182 {
00183 QString s = QTime::currentTime().toString(m_timeFormat);
00184
00185 if (s != m_time_text->GetText())
00186 m_time_text->SetText(s);
00187
00188 s = QDateTime::currentDateTime().toString("dddd\ndd MMM yyyy");
00189
00190 if (s != m_date_text->GetText())
00191 m_date_text->SetText(s);
00192 }
00193
00194 void ZMConsole::updateStatus()
00195 {
00196 m_updateTimer->stop();
00197 getDaemonStatus();
00198 getMonitorStatus();
00199 m_updateTimer->start(STATUS_UPDATE_TIME);
00200 }
00201
00202 void ZMConsole::getDaemonStatus(void)
00203 {
00204 if (class ZMClient *zm = ZMClient::get())
00205 {
00206 zm->getServerStatus(m_daemonStatus, m_cpuStat, m_diskStat);
00207
00208 if (m_daemonStatus.left(7) == "running")
00209 {
00210 m_status_text->SetFontState("running");
00211 m_status_text->SetText(tr("Running"));
00212 }
00213 else
00214 {
00215 m_status_text->SetFontState("stopped");
00216 m_status_text->SetText(tr("Stopped"));
00217 }
00218
00219 m_load_text->SetText("Load: " + m_cpuStat);
00220 m_disk_text->SetText("Disk: " + m_diskStat);
00221 }
00222 }
00223
00224 void ZMConsole::getMonitorStatus(void)
00225 {
00226 if (!m_monitorList)
00227 m_monitorList = new vector<Monitor*>;
00228
00229 if (class ZMClient *zm = ZMClient::get())
00230 {
00231 zm->getMonitorStatus(m_monitorList);
00232 updateMonitorList();
00233 }
00234 }
00235
00236 bool ZMConsole::keyPressEvent(QKeyEvent *event)
00237 {
00238 if (GetFocusWidget()->keyPressEvent(event))
00239 return true;
00240
00241 bool handled = false;
00242 QStringList actions;
00243 handled = GetMythMainWindow()->TranslateKeyPress("Global", event, actions);
00244
00245 for (int i = 0; i < actions.size() && !handled; i++)
00246 {
00247 QString action = actions[i];
00248 handled = true;
00249
00250 if (action == "MENU")
00251 {
00252 showEditFunctionPopup();
00253 }
00254 else
00255 handled = false;
00256 }
00257
00258 if (!handled && MythScreenType::keyPressEvent(event))
00259 handled = true;
00260
00261 return handled;
00262 }
00263
00264 void ZMConsole::showEditFunctionPopup()
00265 {
00266 Monitor *currentMonitor = NULL;
00267
00268 if (m_currentMonitor < (int) m_monitorList->size())
00269 currentMonitor = m_monitorList->at(m_currentMonitor);
00270
00271 if (!currentMonitor)
00272 return;
00273
00274 m_functionDialog = new FunctionDialog(m_popupStack, currentMonitor);
00275
00276 if (m_functionDialog->Create())
00277 {
00278 m_popupStack->AddScreen(m_functionDialog, false);
00279 connect(m_functionDialog, SIGNAL(haveResult(bool)),
00280 this, SLOT(functionChanged(bool)));
00281 }
00282 }
00283
00284 void ZMConsole::updateMonitorList()
00285 {
00286 int pos = m_monitor_list->GetCurrentPos();
00287 m_monitor_list->Reset();
00288
00289 for (uint x = 0; x < m_monitorList->size(); x++)
00290 {
00291 Monitor *monitor = m_monitorList->at(x);
00292
00293 MythUIButtonListItem *item = new MythUIButtonListItem(m_monitor_list,
00294 "", NULL, true, MythUIButtonListItem::NotChecked);
00295 item->SetText(monitor->name, "name");
00296 item->SetText(monitor->zmcStatus, "zmcstatus");
00297 item->SetText(monitor->zmaStatus, "zmastatus");
00298 item->SetText(QString("%1").arg(monitor->events), "eventcount");
00299 }
00300
00301 m_monitor_list->SetItemCurrent(pos);
00302 }
00303
00304 void ZMConsole::functionChanged(bool changed)
00305 {
00306 if (changed)
00307 updateStatus();
00308 }