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 <chrono>
16 #include <cstdlib>
17 #include <iostream>
18 #include <unistd.h>
19 
20 // qt
21 #include <QTimer>
22 
23 // myth
24 #include <mythmainwindow.h>
25 #include <mythcontext.h>
26 #include <mythdbcon.h>
27 #include <mythdate.h>
28 #include <mythuitext.h>
29 
30 // zoneminder
31 #include "zmclient.h"
32 #include "zmconsole.h"
33 
34 constexpr std::chrono::milliseconds STATUS_UPDATE_TIME { 10s }; // update the status every 10 seconds
35 constexpr std::chrono::milliseconds TIME_UPDATE_TIME { 1s }; // update the time every 1 second
36 
38 {
39  if (!LoadWindowFromXML("zoneminder-ui.xml", "functionpopup", this))
40  return false;
41 
42  bool err = false;
43  UIUtilE::Assign(this, m_captionText, "caption_text", &err);
44  UIUtilE::Assign(this, m_functionList, "function_list", &err);
45  UIUtilE::Assign(this, m_enabledCheck, "enable_check", &err);
46  UIUtilE::Assign(this, m_notificationCheck, "notification_check", &err);
47  UIUtilE::Assign(this, m_okButton, "ok_button", &err);
48 
49  if (err)
50  {
51  LOG(VB_GENERAL, LOG_ERR, "Cannot load screen 'functionpopup'");
52  return false;
53  }
54 
55  new MythUIButtonListItem(m_functionList, "Monitor");
56  new MythUIButtonListItem(m_functionList, "Modect");
57  new MythUIButtonListItem(m_functionList, "Nodect");
58  new MythUIButtonListItem(m_functionList, "Record");
59  new MythUIButtonListItem(m_functionList, "Mocord");
61 
63 
65 
67 
68  if (m_monitor->enabled)
70  else
72 
73 
76  else
78 
80 
82 
83  return true;
84 }
85 
87 {
88  QString function = m_functionList->GetValue();
89  bool enabled = (m_enabledCheck->GetCheckState() == MythUIStateType::Full);
90  bool showNotifications = (m_notificationCheck->GetCheckState() == MythUIStateType::Full);
91 
92  LOG(VB_GENERAL, LOG_INFO,
93  "Monitor id : " + QString::number(m_monitor->id) +
94  " function change " + m_monitor->function + " -> " + function +
95  ", enable value " + QString::number(m_monitor->enabled) + " -> " + QString::number(enabled) +
96  ", notification value " + QString::number(m_monitor->showNotifications) + " -> " + QString::number(showNotifications));
97 
98  m_monitor->showNotifications = showNotifications;
100 
101  if ((m_monitor->function == function) && (m_monitor->enabled == enabled))
102  {
103  LOG(VB_GENERAL, LOG_WARNING,
104  "Monitor Function/Enable values not changed so not updating.");
105  emit haveResult(false);
106  Close();
107  return;
108  }
109 
110  ZMClient::get()->setMonitorFunction(m_monitor->id, function, enabled);
111 
112  emit haveResult(true);
113 
114  Close();
115 }
116 
118 
120  :MythScreenType(parent, "zmconsole"),
121  m_popupStack(GetMythMainWindow()->GetStack("popup stack")),
122  m_timeTimer(new QTimer(this)), m_updateTimer(new QTimer(this))
123 {
124  m_timeFormat = gCoreContext->GetSetting("TimeFormat", "h:mm AP");
125 
126  connect(m_timeTimer, &QTimer::timeout, this,
128 
129  connect(m_updateTimer, &QTimer::timeout, this,
131 }
132 
134 {
135  delete m_timeTimer;
136 }
137 
139 {
140  // Load the theme for this screen
141  bool foundtheme = LoadWindowFromXML("zoneminder-ui.xml", "zmconsole", this);
142  if (!foundtheme)
143  return false;
144 
145  bool err = false;
146  UIUtilE::Assign(this, m_monitorList, "monitor_list", &err);
147  UIUtilE::Assign(this, m_statusText, "status_text", &err);
148  UIUtilE::Assign(this, m_timeText, "time_text", &err);
149  UIUtilE::Assign(this, m_dateText, "date_text", &err);
150  UIUtilE::Assign(this, m_loadText, "load_text", &err);
151  UIUtilE::Assign(this, m_diskText, "disk_text", &err);
152 
153  if (err)
154  {
155  LOG(VB_GENERAL, LOG_ERR, "Cannot load screen 'zmconsole'");
156  return false;
157  }
158 
159  BuildFocusList();
160 
162 
164  m_updateTimer->start(100ms);
165 
166  updateTime();
167 
168  return true;
169 }
170 
172 {
173  QString s = MythDate::current().toLocalTime().toString(m_timeFormat);
174 
175  if (s != m_timeText->GetText())
176  m_timeText->SetText(s);
177 
178  s = MythDate::current().toLocalTime().toString("dddd\ndd MMM yyyy");
179 
180  if (s != m_dateText->GetText())
181  m_dateText->SetText(s);
182 }
183 
185 {
186  m_updateTimer->stop();
187  getDaemonStatus();
190 }
191 
193 {
195 
196  if (m_daemonStatus.left(7) == "running")
197  {
198  m_statusText->SetFontState("running");
199  m_statusText->SetText(tr("Running"));
200  }
201  else
202  {
203  m_statusText->SetFontState("stopped");
204  m_statusText->SetText(tr("Stopped"));
205  }
206 
207  m_loadText->SetText("Load: " + m_cpuStat);
208  m_diskText->SetText("Disk: " + m_diskStat);
209 }
210 
212 {
214 }
215 
216 bool ZMConsole::keyPressEvent(QKeyEvent *event)
217 {
218  if (GetFocusWidget()->keyPressEvent(event))
219  return true;
220 
221  QStringList actions;
222  bool handled = GetMythMainWindow()->TranslateKeyPress("Global", event, actions);
223 
224  for (int i = 0; i < actions.size() && !handled; i++)
225  {
226  QString action = actions[i];
227  handled = true;
228 
229  if (action == "MENU")
230  {
232  }
233  else
234  handled = false;
235  }
236 
237  if (!handled && MythScreenType::keyPressEvent(event))
238  handled = true;
239 
240  return handled;
241 }
242 
244 {
245  auto *currentMonitor = m_monitorList->GetItemCurrent()->GetData().value<Monitor*>();
246  if (!currentMonitor)
247  return;
248 
249  m_functionDialog = new FunctionDialog(m_popupStack, currentMonitor);
250 
251  if (m_functionDialog->Create())
252  {
256  }
257 }
258 
260 {
262 
263  int pos = m_monitorList->GetCurrentPos();
264  m_monitorList->Reset();
265 
266  for (int x = 0; x < ZMClient::get()->getMonitorCount(); x++)
267  {
268  Monitor *monitor = ZMClient::get()->getMonitorAt(x);
269 
270  if (monitor)
271  {
272  auto *item = new MythUIButtonListItem(m_monitorList,
273  "", nullptr, true, MythUIButtonListItem::NotChecked);
274  item->SetData(QVariant::fromValue(monitor));
275  item->SetText(monitor->name, "name");
276  item->SetText(monitor->zmcStatus, "zmcstatus");
277  item->SetText(monitor->zmaStatus, "zmastatus");
278  item->SetText(QString("%1").arg(monitor->events), "eventcount");
279  }
280  }
281 
283 }
284 
285 void ZMConsole::functionChanged(bool changed)
286 {
287  if (changed)
288  updateStatus();
289 }
MythUIButton::Clicked
void Clicked()
MythUIText::SetFontState
void SetFontState(const QString &state)
Definition: mythuitext.cpp:220
ZMConsole::m_diskText
MythUIText * m_diskText
Definition: zmconsole.h:85
MythUIButtonList::GetItemCurrent
MythUIButtonListItem * GetItemCurrent() const
Definition: mythuibuttonlist.cpp:1590
hardwareprofile.smolt.timeout
float timeout
Definition: smolt.py:103
FunctionDialog
Definition: zmconsole.h:31
ZMConsole::ZMConsole
ZMConsole(MythScreenStack *parent)
Definition: zmconsole.cpp:119
mythuitext.h
ZMConsole::m_diskStat
QString m_diskStat
Definition: zmconsole.h:95
ZMConsole::~ZMConsole
~ZMConsole() override
Definition: zmconsole.cpp:133
Monitor::function
QString function
Definition: zmdefines.h:109
ZMConsole::functionChanged
void functionChanged(bool changed)
Definition: zmconsole.cpp:285
Monitor::name
QString name
Definition: zmdefines.h:107
MythScreenType::Close
virtual void Close()
Definition: mythscreentype.cpp:385
FunctionDialog::Create
bool Create() override
Definition: zmconsole.cpp:37
ZMClient::getServerStatus
void getServerStatus(QString &status, QString &cpuStat, QString &diskStat)
Definition: zmclient.cpp:257
ZMClient::getMonitorCount
int getMonitorCount(void)
Definition: zmclient.cpp:838
Monitor::events
int events
Definition: zmdefines.h:114
ZMConsole::keyPressEvent
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
Definition: zmconsole.cpp:216
MythScreenStack
Definition: mythscreenstack.h:16
ZMConsole::m_cpuStat
QString m_cpuStat
Definition: zmconsole.h:94
mythdbcon.h
FunctionDialog::m_functionList
MythUIButtonList * m_functionList
Definition: zmconsole.h:50
ZMConsole::m_monitorList
MythUIButtonList * m_monitorList
Definition: zmconsole.h:79
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
MythScreenType
Screen in which all other widgets are contained and rendered.
Definition: mythscreentype.h:44
Monitor::zmcStatus
QString zmcStatus
Definition: zmdefines.h:112
ZMConsole::getMonitorStatus
void getMonitorStatus(void)
Definition: zmconsole.cpp:211
FunctionDialog::haveResult
void haveResult(bool)
MythDate::current
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:10
MythScreenType::GetFocusWidget
MythUIType * GetFocusWidget(void) const
Definition: mythscreentype.cpp:112
ZMConsole::updateStatus
void updateStatus()
Definition: zmconsole.cpp:184
Monitor::enabled
bool enabled
Definition: zmdefines.h:110
ZMClient::saveNotificationMonitors
void saveNotificationMonitors(void)
Definition: zmclient.cpp:944
MythUIButtonListItem
Definition: mythuibuttonlist.h:34
zmclient.h
zmconsole.h
mythdate.h
FunctionDialog::m_captionText
MythUIText * m_captionText
Definition: zmconsole.h:49
FunctionDialog::m_notificationCheck
MythUICheckBox * m_notificationCheck
Definition: zmconsole.h:52
ZMConsole::showEditFunctionPopup
void showEditFunctionPopup()
Definition: zmconsole.cpp:243
MythMainWindow::TranslateKeyPress
bool TranslateKeyPress(const QString &Context, QKeyEvent *Event, QStringList &Actions, bool AllowJumps=true)
Get a list of actions for a keypress in the given context.
Definition: mythmainwindow.cpp:1102
MythUIButtonList::GetCurrentPos
int GetCurrentPos() const
Definition: mythuibuttonlist.h:224
ZMConsole::m_timeTimer
QTimer * m_timeTimer
Definition: zmconsole.h:90
MythScreenType::SetFocusWidget
bool SetFocusWidget(MythUIType *widget=nullptr)
Definition: mythscreentype.cpp:117
FunctionDialog::m_okButton
MythUIButton * m_okButton
Definition: zmconsole.h:53
MythScreenType::BuildFocusList
void BuildFocusList(void)
Definition: mythscreentype.cpp:205
ZMConsole::m_statusText
MythUIText * m_statusText
Definition: zmconsole.h:81
STATUS_UPDATE_TIME
constexpr std::chrono::milliseconds STATUS_UPDATE_TIME
Definition: zmconsole.cpp:34
Monitor::showNotifications
bool showNotifications
Definition: zmdefines.h:121
FunctionDialog::m_enabledCheck
MythUICheckBox * m_enabledCheck
Definition: zmconsole.h:51
FunctionDialog::m_monitor
Monitor * m_monitor
Definition: zmconsole.h:48
FunctionDialog::setMonitorFunction
void setMonitorFunction(void)
Definition: zmconsole.cpp:86
ZMConsole::getDaemonStatus
void getDaemonStatus()
Definition: zmconsole.cpp:192
MythUIStateType::Full
@ Full
Definition: mythuistatetype.h:25
ZMClient::get
static ZMClient * get(void)
Definition: zmclient.cpp:38
MythUIButtonListItem::GetData
QVariant GetData()
Definition: mythuibuttonlist.cpp:3648
ZMConsole::m_popupStack
MythScreenStack * m_popupStack
Definition: zmconsole.h:88
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:60
Monitor::zmaStatus
QString zmaStatus
Definition: zmdefines.h:113
UIUtilDisp::Assign
static bool Assign(ContainerType *container, UIType *&item, const QString &name, bool *err=nullptr)
Definition: mythuiutils.h:27
ZMConsole::Create
bool Create(void) override
Definition: zmconsole.cpp:138
MythUIStateType::Off
@ Off
Definition: mythuistatetype.h:25
ZMClient::updateMonitorStatus
void updateMonitorStatus(void)
Definition: zmclient.cpp:277
MythScreenType::keyPressEvent
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
Definition: mythscreentype.cpp:397
ZMConsole::updateTime
void updateTime()
Definition: zmconsole.cpp:171
MythUICheckBox::SetCheckState
void SetCheckState(MythUIStateType::StateType state)
Definition: mythuicheckbox.cpp:66
XMLParseBase::LoadWindowFromXML
static bool LoadWindowFromXML(const QString &xmlfile, const QString &windowname, MythUIType *parent)
Definition: xmlparsebase.cpp:692
TIME_UPDATE_TIME
constexpr std::chrono::milliseconds TIME_UPDATE_TIME
Definition: zmconsole.cpp:35
MythUIButtonList::GetValue
virtual QString GetValue() const
Definition: mythuibuttonlist.cpp:1609
MythUIText::SetText
virtual void SetText(const QString &text)
Definition: mythuitext.cpp:133
mythcontext.h
ZMConsole::m_daemonStatus
QString m_daemonStatus
Definition: zmconsole.h:93
MythUIButtonList::Reset
void Reset() override
Reset the widget to it's original state, should not reset changes made by the theme.
Definition: mythuibuttonlist.cpp:114
GetMythMainWindow
MythMainWindow * GetMythMainWindow(void)
Definition: mythmainwindow.cpp:101
MythUIButtonList::MoveToNamedPosition
bool MoveToNamedPosition(const QString &position_name)
Definition: mythuibuttonlist.cpp:2282
MythUIButtonList::SetItemCurrent
void SetItemCurrent(MythUIButtonListItem *item)
Definition: mythuibuttonlist.cpp:1557
ZMConsole::m_timeFormat
QString m_timeFormat
Definition: zmconsole.h:91
build_compdb.action
action
Definition: build_compdb.py:9
Monitor::id
int id
Definition: zmdefines.h:106
ZMConsole::updateMonitorList
void updateMonitorList()
Definition: zmconsole.cpp:259
Monitor
Definition: zmdefines.h:99
ZMConsole::m_timeText
MythUIText * m_timeText
Definition: zmconsole.h:82
ZMClient::getMonitorAt
Monitor * getMonitorAt(int pos)
Definition: zmclient.cpp:844
MythUIText::GetText
QString GetText(void) const
Definition: mythuitext.h:45
MythUICheckBox::GetCheckState
MythUIStateType::StateType GetCheckState() const
Definition: mythuicheckbox.cpp:98
ZMClient::setMonitorFunction
void setMonitorFunction(int monitorID, const QString &function, bool enabled)
Definition: zmclient.cpp:931
ZMConsole::m_dateText
MythUIText * m_dateText
Definition: zmconsole.h:83
mythmainwindow.h
ZMConsole::m_functionDialog
FunctionDialog * m_functionDialog
Definition: zmconsole.h:87
MythScreenStack::AddScreen
virtual void AddScreen(MythScreenType *screen, bool allowFade=true)
Definition: mythscreenstack.cpp:50
ZMConsole::m_updateTimer
QTimer * m_updateTimer
Definition: zmconsole.h:97
ZMConsole::m_loadText
MythUIText * m_loadText
Definition: zmconsole.h:84
MythCoreContext::GetSetting
QString GetSetting(const QString &key, const QString &defaultval="")
Definition: mythcorecontext.cpp:922
MythUIButtonListItem::NotChecked
@ NotChecked
Definition: mythuibuttonlist.h:39