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