MythTV  master
zmconsole.cpp
Go to the documentation of this file.
1 /* ============================================================
2  * This program is free software; you can redistribute it
3  * and/or modify it under the terms of the GNU General
4  * Public License as published bythe Free Software Foundation;
5  * either version 2, or (at your option)
6  * any later version.
7  *
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  *
13  * ============================================================ */
14 
15 #include <iostream>
16 #include <unistd.h>
17 #include <cstdlib>
18 
19 // qt
20 #include <QTimer>
21 
22 // myth
23 #include <mythmainwindow.h>
24 #include <mythcontext.h>
25 #include <mythdbcon.h>
26 #include <mythdate.h>
27 #include <mythuitext.h>
28 
29 // zoneminder
30 #include "zmconsole.h"
31 #include "zmclient.h"
32 
33 const int STATUS_UPDATE_TIME = 1000 * 10; // update the status every 10 seconds
34 const int TIME_UPDATE_TIME = 1000 * 1; // update the time every 1 second
35 
37 {
38  if (!LoadWindowFromXML("zoneminder-ui.xml", "functionpopup", this))
39  return false;
40 
41  bool err = false;
42  UIUtilE::Assign(this, m_captionText, "caption_text", &err);
43  UIUtilE::Assign(this, m_functionList, "function_list", &err);
44  UIUtilE::Assign(this, m_enabledCheck, "enable_check", &err);
45  UIUtilE::Assign(this, m_notificationCheck, "notification_check", &err);
46  UIUtilE::Assign(this, m_okButton, "ok_button", &err);
47 
48  if (err)
49  {
50  LOG(VB_GENERAL, LOG_ERR, "Cannot load screen 'functionpopup'");
51  return false;
52  }
53 
54  new MythUIButtonListItem(m_functionList, "Monitor");
55  new MythUIButtonListItem(m_functionList, "Modect");
56  new MythUIButtonListItem(m_functionList, "Nodect");
57  new MythUIButtonListItem(m_functionList, "Record");
58  new MythUIButtonListItem(m_functionList, "Mocord");
60 
62 
64 
65  connect(m_okButton, SIGNAL(Clicked()), this, SLOT(setMonitorFunction()));
66 
67  if (m_monitor->enabled)
69  else
71 
72 
75  else
77 
79 
81 
82  return true;
83 }
84 
86 {
87  QString function = m_functionList->GetValue();
88  bool enabled = (m_enabledCheck->GetCheckState() == MythUIStateType::Full);
89  bool showNotifications = (m_notificationCheck->GetCheckState() == MythUIStateType::Full);
90 
91  LOG(VB_GENERAL, LOG_INFO,
92  "Monitor id : " + QString::number(m_monitor->id) +
93  " function change " + m_monitor->function + " -> " + function +
94  ", enable value " + QString::number(m_monitor->enabled) + " -> " + QString::number(enabled) +
95  ", notification value " + QString::number(m_monitor->showNotifications) + " -> " + QString::number(showNotifications));
96 
97  m_monitor->showNotifications = showNotifications;
99 
100  if ((m_monitor->function == function) && (m_monitor->enabled == enabled))
101  {
102  LOG(VB_GENERAL, LOG_WARNING,
103  "Monitor Function/Enable values not changed so not updating.");
104  emit haveResult(false);
105  Close();
106  return;
107  }
108 
109  ZMClient::get()->setMonitorFunction(m_monitor->id, function, enabled);
110 
111  emit haveResult(true);
112 
113  Close();
114 }
115 
117 
119  :MythScreenType(parent, "zmconsole"),
120  m_popupStack(GetMythMainWindow()->GetStack("popup stack")),
121  m_timeTimer(new QTimer(this)), m_updateTimer(new QTimer(this))
122 {
123  m_timeFormat = gCoreContext->GetSetting("TimeFormat", "h:mm AP");
124 
125  connect(m_timeTimer, SIGNAL(timeout()), this,
126  SLOT(updateTime()));
127 
128  connect(m_updateTimer, SIGNAL(timeout()), this,
129  SLOT(updateStatus()));
130 }
131 
133 {
134  delete m_timeTimer;
135 }
136 
138 {
139  // Load the theme for this screen
140  bool foundtheme = LoadWindowFromXML("zoneminder-ui.xml", "zmconsole", this);
141  if (!foundtheme)
142  return false;
143 
144  bool err = false;
145  UIUtilE::Assign(this, m_monitor_list, "monitor_list", &err);
146  UIUtilE::Assign(this, m_status_text, "status_text", &err);
147  UIUtilE::Assign(this, m_time_text, "time_text", &err);
148  UIUtilE::Assign(this, m_date_text, "date_text", &err);
149  UIUtilE::Assign(this, m_load_text, "load_text", &err);
150  UIUtilE::Assign(this, m_disk_text, "disk_text", &err);
151 
152  if (err)
153  {
154  LOG(VB_GENERAL, LOG_ERR, "Cannot load screen 'zmconsole'");
155  return false;
156  }
157 
158  BuildFocusList();
159 
161 
163  m_updateTimer->start(100);
164 
165  updateTime();
166 
167  return true;
168 }
169 
171 {
172  QString s = MythDate::current().toLocalTime().toString(m_timeFormat);
173 
174  if (s != m_time_text->GetText())
175  m_time_text->SetText(s);
176 
177  s = MythDate::current().toLocalTime().toString("dddd\ndd MMM yyyy");
178 
179  if (s != m_date_text->GetText())
180  m_date_text->SetText(s);
181 }
182 
184 {
185  m_updateTimer->stop();
186  getDaemonStatus();
189 }
190 
192 {
194 
195  if (m_daemonStatus.left(7) == "running")
196  {
197  m_status_text->SetFontState("running");
198  m_status_text->SetText(tr("Running"));
199  }
200  else
201  {
202  m_status_text->SetFontState("stopped");
203  m_status_text->SetText(tr("Stopped"));
204  }
205 
206  m_load_text->SetText("Load: " + m_cpuStat);
207  m_disk_text->SetText("Disk: " + m_diskStat);
208 }
209 
211 {
213 }
214 
215 bool ZMConsole::keyPressEvent(QKeyEvent *event)
216 {
217  if (GetFocusWidget()->keyPressEvent(event))
218  return true;
219 
220  QStringList actions;
221  bool handled = GetMythMainWindow()->TranslateKeyPress("Global", event, actions);
222 
223  for (int i = 0; i < actions.size() && !handled; i++)
224  {
225  QString action = actions[i];
226  handled = true;
227 
228  if (action == "MENU")
229  {
231  }
232  else
233  handled = false;
234  }
235 
236  if (!handled && MythScreenType::keyPressEvent(event))
237  handled = true;
238 
239  return handled;
240 }
241 
243 {
244  Monitor *currentMonitor = m_monitor_list->GetItemCurrent()->GetData().value<Monitor*>();
245  if (!currentMonitor)
246  return;
247 
248  m_functionDialog = new FunctionDialog(m_popupStack, currentMonitor);
249 
250  if (m_functionDialog->Create())
251  {
253  connect(m_functionDialog, SIGNAL(haveResult(bool)),
254  this, SLOT(functionChanged(bool)));
255  }
256 }
257 
259 {
261 
262  int pos = m_monitor_list->GetCurrentPos();
264 
265  for (int x = 0; x < ZMClient::get()->getMonitorCount(); x++)
266  {
267  Monitor *monitor = ZMClient::get()->getMonitorAt(x);
268 
269  if (monitor)
270  {
272  "", nullptr, true, MythUIButtonListItem::NotChecked);
273  item->SetData(qVariantFromValue(monitor));
274  item->SetText(monitor->name, "name");
275  item->SetText(monitor->zmcStatus, "zmcstatus");
276  item->SetText(monitor->zmaStatus, "zmastatus");
277  item->SetText(QString("%1").arg(monitor->events), "eventcount");
278  }
279  }
280 
282 }
283 
284 void ZMConsole::functionChanged(bool changed)
285 {
286  if (changed)
287  updateStatus();
288 }
MythUIStateType::StateType GetCheckState() const
Monitor * m_monitor
Definition: zmconsole.h:48
bool TranslateKeyPress(const QString &context, QKeyEvent *e, QStringList &actions, bool allowJumps=true)
Get a list of actions for a keypress in the given context.
void functionChanged(bool changed)
Definition: zmconsole.cpp:284
MythUIButton * m_okButton
Definition: zmconsole.h:53
QString m_cpuStat
Definition: zmconsole.h:94
void SetData(QVariant data)
void setMonitorFunction(const int monitorID, const QString &function, const bool enabled)
Definition: zmclient.cpp:934
Monitor * getMonitorAt(int pos)
Definition: zmclient.cpp:847
QString zmaStatus
Definition: zmdefines.h:118
MythUICheckBox * m_enabledCheck
Definition: zmconsole.h:51
QString m_daemonStatus
Definition: zmconsole.h:93
bool Create() override
Definition: zmconsole.cpp:36
virtual void SetText(const QString &text)
Definition: mythuitext.cpp:136
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
bool Create(void) override
Definition: zmconsole.cpp:137
void showEditFunctionPopup()
Definition: zmconsole.cpp:242
void getServerStatus(QString &status, QString &cpuStat, QString &diskStat)
Definition: zmclient.cpp:258
bool enabled
Definition: zmdefines.h:115
const int TIME_UPDATE_TIME
Definition: zmconsole.cpp:34
void BuildFocusList(void)
bool MoveToNamedPosition(const QString &position_name)
QString GetText(void) const
Definition: mythuitext.h:43
void getDaemonStatus()
Definition: zmconsole.cpp:191
virtual void Close()
MythUIText * m_captionText
Definition: zmconsole.h:49
void updateStatus()
Definition: zmconsole.cpp:183
MythUIText * m_load_text
Definition: zmconsole.h:84
const int STATUS_UPDATE_TIME
Definition: zmconsole.cpp:33
bool keyPressEvent(QKeyEvent *) override
Key event handler.
Definition: zmconsole.cpp:215
QString m_diskStat
Definition: zmconsole.h:95
virtual void AddScreen(MythScreenType *screen, bool allowFade=true)
int getMonitorCount(void)
Definition: zmclient.cpp:841
QString m_timeFormat
Definition: zmconsole.h:91
void getMonitorStatus(void)
Definition: zmconsole.cpp:210
int id
Definition: zmdefines.h:111
QString function
Definition: zmdefines.h:114
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:10
QString GetSetting(const QString &key, const QString &defaultval="")
void updateMonitorStatus(void)
Definition: zmclient.cpp:278
static ZMClient * get(void)
Definition: zmclient.cpp:38
MythUIButtonList * m_functionList
Definition: zmconsole.h:50
static bool Assign(ContainerType *container, UIType *&item, const QString &name, bool *err=nullptr)
Definition: mythuiutils.h:27
ZMConsole(MythScreenStack *parent)
Definition: zmconsole.cpp:118
MythScreenStack * m_popupStack
Definition: zmconsole.h:88
FunctionDialog * m_functionDialog
Definition: zmconsole.h:87
void SetText(const QString &text, const QString &name="", const QString &state="")
QString zmcStatus
Definition: zmdefines.h:117
void saveNotificationMonitors(void)
Definition: zmclient.cpp:947
static bool LoadWindowFromXML(const QString &xmlfile, const QString &windowname, MythUIType *parent)
MythUIType * GetFocusWidget(void) const
MythUICheckBox * m_notificationCheck
Definition: zmconsole.h:52
void Reset() override
Reset the widget to it's original state, should not reset changes made by the theme.
MythMainWindow * GetMythMainWindow(void)
virtual QString GetValue() const
MythUIText * m_time_text
Definition: zmconsole.h:82
void updateTime()
Definition: zmconsole.cpp:170
bool keyPressEvent(QKeyEvent *) override
Key event handler.
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
QTimer * m_updateTimer
Definition: zmconsole.h:97
MythUIText * m_date_text
Definition: zmconsole.h:83
QTimer * m_timeTimer
Definition: zmconsole.h:90
MythUIText * m_disk_text
Definition: zmconsole.h:85
void updateMonitorList()
Definition: zmconsole.cpp:258
void SetCheckState(MythUIStateType::StateType state)
void SetItemCurrent(MythUIButtonListItem *item)
bool SetFocusWidget(MythUIType *widget=nullptr)
MythUIText * m_status_text
Definition: zmconsole.h:81
Screen in which all other widgets are contained and rendered.
int GetCurrentPos() const
void SetFontState(const QString &)
Definition: mythuitext.cpp:225
void setMonitorFunction(void)
Definition: zmconsole.cpp:85
void haveResult(bool)
bool showNotifications
Definition: zmdefines.h:126
MythUIButtonList * m_monitor_list
Definition: zmconsole.h:79
QString name
Definition: zmdefines.h:112
int events
Definition: zmdefines.h:119
MythUIButtonListItem * GetItemCurrent() const