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 using namespace std::chrono_literals;
35 
36 constexpr std::chrono::milliseconds STATUS_UPDATE_TIME { 10s }; // update the status every 10 seconds
37 constexpr std::chrono::milliseconds TIME_UPDATE_TIME { 1s }; // update the time every 1 second
38 
40 {
41  if (!LoadWindowFromXML("zoneminder-ui.xml", "functionpopup", this))
42  return false;
43 
44  bool err = false;
45  UIUtilE::Assign(this, m_captionText, "caption_text", &err);
46  UIUtilE::Assign(this, m_functionList, "function_list", &err);
47  UIUtilE::Assign(this, m_enabledCheck, "enable_check", &err);
48  UIUtilE::Assign(this, m_notificationCheck, "notification_check", &err);
49  UIUtilE::Assign(this, m_okButton, "ok_button", &err);
50 
51  if (err)
52  {
53  LOG(VB_GENERAL, LOG_ERR, "Cannot load screen 'functionpopup'");
54  return false;
55  }
56 
57  new MythUIButtonListItem(m_functionList, "Monitor");
58  new MythUIButtonListItem(m_functionList, "Modect");
59  new MythUIButtonListItem(m_functionList, "Nodect");
60  new MythUIButtonListItem(m_functionList, "Record");
61  new MythUIButtonListItem(m_functionList, "Mocord");
62  new MythUIButtonListItem(m_functionList, "None");
63 
64  m_functionList->MoveToNamedPosition(m_monitor->function);
65 
66  m_captionText->SetText(m_monitor->name);
67 
68  connect(m_okButton, &MythUIButton::Clicked, this, &FunctionDialog::setMonitorFunction);
69 
70  if (m_monitor->enabled)
71  m_enabledCheck->SetCheckState(MythUIStateType::Full);
72  else
73  m_enabledCheck->SetCheckState(MythUIStateType::Off);
74 
75 
76  if (m_monitor->showNotifications)
77  m_notificationCheck->SetCheckState(MythUIStateType::Full);
78  else
79  m_notificationCheck->SetCheckState(MythUIStateType::Off);
80 
81  BuildFocusList();
82 
83  SetFocusWidget(m_functionList);
84 
85  return true;
86 }
87 
89 {
90  QString function = m_functionList->GetValue();
91  bool enabled = (m_enabledCheck->GetCheckState() == MythUIStateType::Full);
92  bool showNotifications = (m_notificationCheck->GetCheckState() == MythUIStateType::Full);
93 
94  LOG(VB_GENERAL, LOG_INFO,
95  "Monitor id : " + QString::number(m_monitor->id) +
96  " function change " + m_monitor->function + " -> " + function +
97  ", enable value " + QString::number(m_monitor->enabled) + " -> " + QString::number(enabled) +
98  ", notification value " + QString::number(m_monitor->showNotifications) + " -> " + QString::number(showNotifications));
99 
100  m_monitor->showNotifications = showNotifications;
102 
103  if ((m_monitor->function == function) && (m_monitor->enabled == enabled))
104  {
105  LOG(VB_GENERAL, LOG_WARNING,
106  "Monitor Function/Enable values not changed so not updating.");
107  emit haveResult(false);
108  Close();
109  return;
110  }
111 
112  ZMClient::get()->setMonitorFunction(m_monitor->id, function, enabled);
113 
114  emit haveResult(true);
115 
116  Close();
117 }
118 
120 
122  :MythScreenType(parent, "zmconsole"),
123  m_popupStack(GetMythMainWindow()->GetStack("popup stack")),
124  m_timeTimer(new QTimer(this)), m_updateTimer(new QTimer(this))
125 {
126  m_timeFormat = gCoreContext->GetSetting("TimeFormat", "h:mm AP");
127 
128  connect(m_timeTimer, &QTimer::timeout, this,
130 
131  connect(m_updateTimer, &QTimer::timeout, this,
133 }
134 
136 {
137  delete m_timeTimer;
138 }
139 
141 {
142  // Load the theme for this screen
143  bool foundtheme = LoadWindowFromXML("zoneminder-ui.xml", "zmconsole", this);
144  if (!foundtheme)
145  return false;
146 
147  bool err = false;
148  UIUtilE::Assign(this, m_monitorList, "monitor_list", &err);
149  UIUtilE::Assign(this, m_statusText, "status_text", &err);
150  UIUtilE::Assign(this, m_timeText, "time_text", &err);
151  UIUtilE::Assign(this, m_dateText, "date_text", &err);
152  UIUtilE::Assign(this, m_loadText, "load_text", &err);
153  UIUtilE::Assign(this, m_diskText, "disk_text", &err);
154 
155  if (err)
156  {
157  LOG(VB_GENERAL, LOG_ERR, "Cannot load screen 'zmconsole'");
158  return false;
159  }
160 
161  BuildFocusList();
162 
164 
166  m_updateTimer->start(100ms);
167 
168  updateTime();
169 
170  return true;
171 }
172 
174 {
175  QString s = MythDate::current().toLocalTime().toString(m_timeFormat);
176 
177  if (s != m_timeText->GetText())
178  m_timeText->SetText(s);
179 
180  s = MythDate::current().toLocalTime().toString("dddd\ndd MMM yyyy");
181 
182  if (s != m_dateText->GetText())
183  m_dateText->SetText(s);
184 }
185 
187 {
188  m_updateTimer->stop();
189  getDaemonStatus();
192 }
193 
195 {
197 
198  if (m_daemonStatus.left(7) == "running")
199  {
200  m_statusText->SetFontState("running");
201  m_statusText->SetText(tr("Running"));
202  }
203  else
204  {
205  m_statusText->SetFontState("stopped");
206  m_statusText->SetText(tr("Stopped"));
207  }
208 
209  m_loadText->SetText("Load: " + m_cpuStat);
210  m_diskText->SetText("Disk: " + m_diskStat);
211 }
212 
214 {
216 }
217 
218 bool ZMConsole::keyPressEvent(QKeyEvent *event)
219 {
220  if (GetFocusWidget()->keyPressEvent(event))
221  return true;
222 
223  QStringList actions;
224  bool handled = GetMythMainWindow()->TranslateKeyPress("Global", event, actions);
225 
226  for (int i = 0; i < actions.size() && !handled; i++)
227  {
228  QString action = actions[i];
229  handled = true;
230 
231  if (action == "MENU")
232  {
234  }
235  else
236  handled = false;
237  }
238 
239  if (!handled && MythScreenType::keyPressEvent(event))
240  handled = true;
241 
242  return handled;
243 }
244 
246 {
247  auto *currentMonitor = m_monitorList->GetItemCurrent()->GetData().value<Monitor*>();
248  if (!currentMonitor)
249  return;
250 
251  m_functionDialog = new FunctionDialog(m_popupStack, currentMonitor);
252 
253  if (m_functionDialog->Create())
254  {
258  }
259 }
260 
262 {
264 
265  int pos = m_monitorList->GetCurrentPos();
266  m_monitorList->Reset();
267 
268  for (int x = 0; x < ZMClient::get()->getMonitorCount(); x++)
269  {
270  Monitor *monitor = ZMClient::get()->getMonitorAt(x);
271 
272  if (monitor)
273  {
274  auto *item = new MythUIButtonListItem(m_monitorList,
275  "", nullptr, true, MythUIButtonListItem::NotChecked);
276  item->SetData(QVariant::fromValue(monitor));
277  item->SetText(monitor->name, "name");
278  item->SetText(monitor->zmcStatus, "zmcstatus");
279  item->SetText(monitor->zmaStatus, "zmastatus");
280  item->SetText(QString("%1").arg(monitor->events), "eventcount");
281  }
282  }
283 
285 }
286 
287 void ZMConsole::functionChanged(bool changed)
288 {
289  if (changed)
290  updateStatus();
291 }
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:121
mythuitext.h
ZMConsole::m_diskStat
QString m_diskStat
Definition: zmconsole.h:95
ZMConsole::~ZMConsole
~ZMConsole() override
Definition: zmconsole.cpp:135
ZMConsole::functionChanged
void functionChanged(bool changed)
Definition: zmconsole.cpp:287
Monitor::name
QString name
Definition: zmdefines.h:107
FunctionDialog::Create
bool Create() override
Definition: zmconsole.cpp:39
ZMClient::getServerStatus
void getServerStatus(QString &status, QString &cpuStat, QString &diskStat)
Definition: zmclient.cpp:257
ZMClient::getMonitorCount
int getMonitorCount(void)
Definition: zmclient.cpp:839
Monitor::events
int events
Definition: zmdefines.h:114
ZMConsole::keyPressEvent
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
Definition: zmconsole.cpp:218
MythScreenStack
Definition: mythscreenstack.h:15
ZMConsole::m_cpuStat
QString m_cpuStat
Definition: zmconsole.h:94
arg
arg(title).arg(filename).arg(doDelete))
mythdbcon.h
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:213
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:186
ZMClient::saveNotificationMonitors
void saveNotificationMonitors(void)
Definition: zmclient.cpp:945
MythUIButtonListItem
Definition: mythuibuttonlist.h:27
zmclient.h
zmconsole.h
mythdate.h
ZMConsole::showEditFunctionPopup
void showEditFunctionPopup()
Definition: zmconsole.cpp:245
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:1114
MythUIButtonList::GetCurrentPos
int GetCurrentPos() const
Definition: mythuibuttonlist.h:198
ZMConsole::m_timeTimer
QTimer * m_timeTimer
Definition: zmconsole.h:90
MythScreenType::SetFocusWidget
bool SetFocusWidget(MythUIType *widget=nullptr)
Definition: mythscreentype.cpp:117
MythScreenType::BuildFocusList
void BuildFocusList(void)
Definition: mythscreentype.cpp:222
ZMConsole::m_statusText
MythUIText * m_statusText
Definition: zmconsole.h:81
STATUS_UPDATE_TIME
constexpr std::chrono::milliseconds STATUS_UPDATE_TIME
Definition: zmconsole.cpp:36
FunctionDialog::setMonitorFunction
void setMonitorFunction(void)
Definition: zmconsole.cpp:88
ZMConsole::getDaemonStatus
void getDaemonStatus()
Definition: zmconsole.cpp:194
MythUIStateType::Full
@ Full
Definition: mythuistatetype.h:25
ZMClient::get
static ZMClient * get(void)
Definition: zmclient.cpp:38
MythUIButtonListItem::GetData
QVariant GetData()
Definition: mythuibuttonlist.cpp:3580
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:140
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:414
ZMConsole::updateTime
void updateTime()
Definition: zmconsole.cpp:173
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:37
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:104
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
ZMConsole::updateMonitorList
void updateMonitorList()
Definition: zmconsole.cpp:261
Monitor
Definition: zmdefines.h:99
ZMConsole::m_timeText
MythUIText * m_timeText
Definition: zmconsole.h:82
ZMClient::getMonitorAt
Monitor * getMonitorAt(int pos)
Definition: zmclient.cpp:845
MythUIText::GetText
QString GetText(void) const
Definition: mythuitext.h:45
ZMClient::setMonitorFunction
void setMonitorFunction(int monitorID, const QString &function, bool enabled)
Definition: zmclient.cpp:932
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:52
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:919
MythUIButtonListItem::NotChecked
@ NotChecked
Definition: mythuibuttonlist.h:32