MythTV  master
mythzoneminder.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 // C++
16 #include <iostream>
17 #include <unistd.h>
18 
19 // MythTV
20 #include <libmyth/mythcontext.h>
23 #include <libmythbase/mythversion.h>
26 #include <libmythui/mythuihelper.h>
27 
28 //zone minder
29 #include "zmsettings.h"
30 #include "zmconsole.h"
31 #include "zmliveplayer.h"
32 #include "zmevents.h"
33 #include "zmclient.h"
34 #include "zmminiplayer.h"
35 #include "alarmnotifythread.h"
36 
37 static bool checkConnection(void)
38 {
39  if (!ZMClient::get()->connected())
40  {
42  return false;
43  }
44 
45  return true;
46 }
47 
48 static void runZMConsole(void)
49 {
50  if (!checkConnection())
51  return;
52 
54 
55  auto *console = new ZMConsole(mainStack);
56 
57  if (console->Create())
58  mainStack->AddScreen(console);
59 }
60 
61 static void runZMLiveView(void)
62 {
63  if (!checkConnection())
64  return;
65 
66 
68 
69  auto *player = new ZMLivePlayer(mainStack);
70 
71  if (player->Create())
72  mainStack->AddScreen(player);
73 }
74 
75 static void runZMEventView(void)
76 {
77  if (!checkConnection())
78  return;
79 
81 
82  auto *events = new ZMEvents(mainStack);
83 
84  if (events->Create())
85  mainStack->AddScreen(events);
86 }
87 
88 static void runZMMiniPlayer(void)
89 {
90  if (!ZMClient::get()->isMiniPlayerEnabled())
91  return;
92 
93  if (!checkConnection())
94  return;
95 
97 
98  auto *miniPlayer = new ZMMiniPlayer(mainStack);
99 
100  if (miniPlayer->Create())
101  mainStack->AddScreen(miniPlayer);
102 }
103 
104 // these point to the the mainmenu callback if found
105 static void (*m_callback)(void *, QString &) = nullptr;
106 static void *m_callbackdata = nullptr;
107 
108 static void ZoneMinderCallback([[maybe_unused]] void *data, QString &selection)
109 {
110  QString sel = selection.toLower();
111 
112  if (sel == "zm_console")
113  runZMConsole();
114  else if (sel == "zm_live_viewer")
115  runZMLiveView();
116  else if (sel == "zm_event_viewer")
117  runZMEventView();
118  else
119  {
120  // if we have found the mainmenu callback
121  // pass the selection on to it
122  if (m_callback && m_callbackdata)
123  m_callback(m_callbackdata, selection);
124  }
125 }
126 
127 static int runMenu(const QString& which_menu)
128 {
129  QString themedir = GetMythUI()->GetThemeDir();
130 
131  // find the 'mainmenu' MythThemedMenu so we can use the callback from it
132  MythThemedMenu *mainMenu = nullptr;
133  QObject *parentObject = GetMythMainWindow()->GetMainStack()->GetTopScreen();
134 
135  while (parentObject)
136  {
137  mainMenu = qobject_cast<MythThemedMenu *>(parentObject);
138 
139  if (mainMenu && mainMenu->objectName() == "mainmenu")
140  break;
141 
142  parentObject = parentObject->parent();
143  }
144 
145  auto *diag = new MythThemedMenu(
146  themedir, which_menu, GetMythMainWindow()->GetMainStack(),
147  "zoneminder menu");
148 
149  // save the callback from the main menu
150  if (mainMenu)
151  mainMenu->getCallback(&m_callback, &m_callbackdata);
152  else
153  {
154  m_callback = nullptr;
155  m_callbackdata = nullptr;
156  }
157 
158  diag->setCallback(ZoneMinderCallback, nullptr);
159  diag->setKillable();
160 
161  if (diag->foundTheme())
162  {
164  return 0;
165  }
166  LOG(VB_GENERAL, LOG_ERR, QString("Couldn't find menu %1 or theme %2")
167  .arg(which_menu, themedir));
168  delete diag;
169  return -1;
170 }
171 
172 static void setupKeys(void)
173 {
174  REG_JUMP(QT_TRANSLATE_NOOP("MythControls", "ZoneMinder Console"),
175  "", "", runZMConsole);
176  REG_JUMP(QT_TRANSLATE_NOOP("MythControls", "ZoneMinder Live View"),
177  "", "", runZMLiveView);
178  REG_JUMP(QT_TRANSLATE_NOOP("MythControls", "ZoneMinder Events"),
179  "", "", runZMEventView);
180  REG_JUMPEX(QT_TRANSLATE_NOOP("MythControls", "ZoneMinder Mini Live View"),
181  "", "", runZMMiniPlayer, false);
182 }
183 
184 int mythplugin_init(const char *libversion)
185 {
186  if (!MythCoreContext::TestPluginVersion("mythzoneminder",
187  libversion,
188  MYTH_BINARY_VERSION))
189  return -1;
190 
191  // setup a connection to the mythzmserver
192  (void) checkConnection();
193 
194  setupKeys();
195 
196  // create the alarm polling thread
198 
199  return 0;
200 }
201 
202 int mythplugin_run(void)
203 {
204 
205  return runMenu("zonemindermenu.xml");
206 }
207 
209 {
211  auto *ssd = new StandardSettingDialog(mainStack, "zonemindersettings",
212  new ZMSettings());
213 
214  if (ssd->Create())
215  mainStack->AddScreen(ssd);
216  else
217  delete ssd;
218 
219  return 0;
220 }
221 
223 {
225  delete AlarmNotifyThread::get();
226  delete ZMClient::get();
227 }
228 
themedir
static QString themedir
Definition: mythdirs.cpp:23
MythMainWindow::GetMainStack
MythScreenStack * GetMainStack()
Definition: mythmainwindow.cpp:318
MThread::start
void start(QThread::Priority p=QThread::InheritPriority)
Tell MThread to start running the thread in the near future.
Definition: mthread.cpp:283
setupKeys
static void setupKeys(void)
Definition: mythzoneminder.cpp:172
runZMConsole
static void runZMConsole(void)
Definition: mythzoneminder.cpp:48
MythScreenStack
Definition: mythscreenstack.h:16
ZMClient::setupZMClient
static bool setupZMClient(void)
Definition: zmclient.cpp:44
checkConnection
static bool checkConnection(void)
Definition: mythzoneminder.cpp:37
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
MythCoreContext::TestPluginVersion
static bool TestPluginVersion(const QString &name, const QString &libversion, const QString &pluginversion)
Definition: mythcorecontext.cpp:2079
ZMMiniPlayer
Definition: zmminiplayer.h:11
AlarmNotifyThread::stop
void stop(void)
Definition: alarmnotifythread.cpp:30
zmliveplayer.h
alarmnotifythread.h
zmclient.h
zmconsole.h
StandardSettingDialog
Definition: standardsettings.h:468
m_callback
static void(* m_callback)(void *, QString &)
Definition: mythzoneminder.cpp:105
AlarmNotifyThread::get
static AlarmNotifyThread * get(void)
Definition: alarmnotifythread.cpp:13
mythlogging.h
zmevents.h
MythUIThemeHelper::GetThemeDir
QString GetThemeDir()
Definition: mythuithemehelper.cpp:141
ZMEvents
Definition: zmevents.h:30
mythplugin_destroy
void mythplugin_destroy(void)
Definition: mythzoneminder.cpp:222
mythplugin_init
int mythplugin_init(const char *libversion)
Definition: mythzoneminder.cpp:184
ZMLivePlayer
Definition: zmliveplayer.h:60
mythplugin_config
int mythplugin_config(void)
Definition: mythzoneminder.cpp:208
runZMLiveView
static void runZMLiveView(void)
Definition: mythzoneminder.cpp:61
runZMMiniPlayer
static void runZMMiniPlayer(void)
Definition: mythzoneminder.cpp:88
ZMClient::get
static ZMClient * get(void)
Definition: zmclient.cpp:37
ZMConsole
Definition: zmconsole.h:56
mythpluginapi.h
zmminiplayer.h
mythuihelper.h
runZMEventView
static void runZMEventView(void)
Definition: mythzoneminder.cpp:75
ZoneMinderCallback
static void ZoneMinderCallback([[maybe_unused]] void *data, QString &selection)
Definition: mythzoneminder.cpp:108
MythThemedMenu::getCallback
void getCallback(void(**lcallback)(void *, QString &), void **data)
Get the themed menus callback function and data for that function.
Definition: myththemedmenu.cpp:148
mythplugin_run
int mythplugin_run(void)
Definition: mythzoneminder.cpp:202
REG_JUMP
static void REG_JUMP(const QString &Destination, const QString &Description, const QString &Key, void(*Callback)(void))
Definition: mythmainwindow.h:188
mythcontext.h
GetMythMainWindow
MythMainWindow * GetMythMainWindow(void)
Definition: mythmainwindow.cpp:104
m_callbackdata
static void * m_callbackdata
Definition: mythzoneminder.cpp:106
REG_JUMPEX
static void REG_JUMPEX(const QString &Destination, const QString &Description, const QString &Key, void(*Callback)(void), bool ExitToMain)
Definition: mythmainwindow.h:202
MythThemedMenu
Themed menu class, used for main menus in MythTV frontend.
Definition: myththemedmenu.h:57
myththemedmenu.h
GetMythUI
MythUIHelper * GetMythUI()
Definition: mythuihelper.cpp:66
runMenu
static int runMenu(const QString &which_menu)
Definition: mythzoneminder.cpp:127
mythmainwindow.h
zmsettings.h
MythScreenStack::AddScreen
virtual void AddScreen(MythScreenType *screen, bool allowFade=true)
Definition: mythscreenstack.cpp:52
MythScreenStack::GetTopScreen
virtual MythScreenType * GetTopScreen(void) const
Definition: mythscreenstack.cpp:182
ZMSettings
Definition: zmsettings.h:12