MythTV  master
neteditorbase.cpp
Go to the documentation of this file.
1 #include <QDomDocument>
2 
3 // MythTV headers
4 #include <mythuibuttonlist.h>
5 #include <mythmainwindow.h>
6 #include <mythdialogbox.h>
7 #include <mythcontext.h>
8 #include <mythdbcon.h>
9 #include <mythdirs.h>
10 #include <netutils.h>
11 
12 // Tree headers
13 #include "neteditorbase.h"
14 #include "netcommon.h"
15 
16 #define LOC QString("NetEditorBase: ")
17 #define LOC_WARN QString("NetEditorBase, Warning: ")
18 #define LOC_ERR QString("NetEditorBase, Error: ")
19 
25  const QString &name) :
26  MythScreenType(parent, name)
27 {
28  m_popupStack = GetMythMainWindow()->GetStack("popup stack");
29 }
30 
32 {
33  if (m_manager)
34  {
35  m_manager->disconnect();
36  m_manager->deleteLater();
37  m_manager = nullptr;
38  }
39 
40  qDeleteAll(m_grabberList);
41  m_grabberList.clear();
42 
43  if (m_changed)
44  emit ItemsChanged();
45 }
46 
48 {
49  // Load the theme for this screen
50  bool foundtheme = LoadWindowFromXML("netvision-ui.xml", "treeeditor", this);
51 
52  if (!foundtheme)
53  return false;
54 
55  bool err = false;
56  UIUtilE::Assign(this, m_grabbers, "grabbers", &err);
57 
58  if (err)
59  {
60  LOG(VB_GENERAL, LOG_ERR, "Cannot load screen 'treeeditor'");
61  return false;
62  }
63 
64  connect(m_grabbers, SIGNAL(itemClicked(MythUIButtonListItem*)),
65  this, SLOT(ToggleItem(MythUIButtonListItem*)));
66 
68 
69  LoadData();
70 
71  return true;
72 }
73 
75 {
76  QString msg = tr("Querying Backend for Internet Content Sources...");
77  CreateBusyDialog(msg);
78 
79  m_manager = new QNetworkAccessManager();
80 
81  connect(m_manager, SIGNAL(finished(QNetworkReply*)),
82  SLOT(SlotLoadedData(void)));
83 
84  QUrl url(GetMythXMLURL() + "GetInternetSources");
85  m_reply = m_manager->get(QNetworkRequest(url));
86 }
87 
88 bool NetEditorBase::keyPressEvent(QKeyEvent *event)
89 {
90  if (GetFocusWidget()->keyPressEvent(event))
91  return true;
92 
93  QStringList actions;
94  bool handled = GetMythMainWindow()->TranslateKeyPress("Internet Video",
95  event, actions);
96 
97  if (!handled && MythScreenType::keyPressEvent(event))
98  handled = true;
99 
100  return handled;
101 }
102 
104 {
105  QDomDocument doc;
106  doc.setContent(m_reply->readAll());
107  QDomElement root = doc.documentElement();
108  QDomElement content = root.firstChildElement("InternetContent");
109  QDomElement grabber = content.firstChildElement("grabber");
110 
111  while (!grabber.isNull())
112  {
113  QString title, author, image, description, type, commandline;
114  double version;
115  bool search = false;
116  bool tree = false;
117 
118  title = grabber.firstChildElement("name").text();
119  commandline = grabber.firstChildElement("command").text();
120  author = grabber.firstChildElement("author").text();
121  image = grabber.firstChildElement("thumbnail").text();
122  type = grabber.firstChildElement("type").text();
123  description = grabber.firstChildElement("description").text();
124  version = grabber.firstChildElement("version").text().toDouble();
125 
126  QString searchstring = grabber.firstChildElement("search").text();
127 
128  if (!searchstring.isEmpty() &&
129  (searchstring.toLower() == "true" ||
130  searchstring.toLower() == "yes" ||
131  searchstring == "1"))
132  search = true;
133 
134  QString treestring = grabber.firstChildElement("tree").text();
135 
136  if (!treestring.isEmpty() &&
137  (treestring.toLower() == "true" ||
138  treestring.toLower() == "yes" ||
139  treestring == "1"))
140  tree = true;
141 
142  if (type.toLower() == "video" && Matches(search, tree))
143  {
144  LOG(VB_GENERAL, LOG_INFO,
145  QString("Found Source %1...").arg(title));
146  m_grabberList.append(new GrabberScript(title, image, VIDEO_FILE,
147  author, search, tree,
148  description, commandline,
149  version));
150  }
151 
152  grabber = grabber.nextSiblingElement("grabber");
153  }
154 
155  ParsedData();
156  }
157 
159 {
160  if (m_busyPopup)
161  {
162  m_busyPopup->Close();
163  m_busyPopup = nullptr;
164  }
165 
167 }
168 
169 void NetEditorBase::CreateBusyDialog(const QString& title)
170 {
171  if (m_busyPopup)
172  return;
173 
174  const QString& message = title;
175 
177  "mythvideobusydialog");
178 
179  if (m_busyPopup->Create())
181  else
182  {
183  delete m_busyPopup;
184  m_busyPopup = nullptr;
185  }
186 }
187 
189 {
190  for (GrabberScript::scriptList::iterator i = m_grabberList.begin();
191  i != m_grabberList.end(); ++i)
192  {
193  MythUIButtonListItem *item =
194  new MythUIButtonListItem(m_grabbers, (*i)->GetTitle());
195  item->SetText((*i)->GetTitle(), "title");
196  item->SetData(qVariantFromValue(*i));
197  QString img = (*i)->GetImage();
198  QString thumb;
199 
200  if (!img.startsWith("/") && !img.isEmpty())
201  thumb = QString("%1mythnetvision/icons/%2").arg(GetShareDir())
202  .arg((*i)->GetImage());
203  else
204  thumb = img;
205 
206  item->SetImage(thumb);
207  item->setCheckable(true);
209  QFileInfo fi((*i)->GetCommandline());
210 
211  if (FindGrabberInDB(fi.fileName()))
213  }
214 }
215 
217 {
218  if (!item)
219  return;
220 
221  GrabberScript *script = item->GetData().value<GrabberScript*>();
222 
223  if (!script)
224  return;
225 
226  bool checked = (item->state() == MythUIButtonListItem::FullChecked);
227 
228  if (!checked)
229  {
230  if (InsertInDB(script))
231  {
232  m_changed = true;
234  }
235  }
236  else if (RemoveFromDB(script))
237  {
238  m_changed = true;
240  }
241 }
bool TranslateKeyPress(const QString &context, QKeyEvent *e, QStringList &actions, bool allowJumps=true)
Get a list of actions for a keypress in the given context.
MythUIBusyDialog * m_busyPopup
Definition: neteditorbase.h:40
void CreateBusyDialog(const QString &title)
void SetData(QVariant data)
void ToggleItem(MythUIButtonListItem *item)
MythScreenStack * GetStack(const QString &stackname)
bool keyPressEvent(QKeyEvent *) override
Key event handler.
QNetworkReply * m_reply
Definition: neteditorbase.h:44
void ItemsChanged(void)
virtual bool RemoveFromDB(GrabberScript *script)=0
void BuildFocusList(void)
void SetImage(MythImage *image, const QString &name="")
Sets an image directly, should only be used in special circumstances since it bypasses the cache.
virtual void Close()
void setCheckable(bool flag)
virtual bool FindGrabberInDB(const QString &filename)=0
virtual void AddScreen(MythScreenType *screen, bool allowFade=true)
void FillGrabberButtonList()
bool Create(void) override
NetEditorBase(MythScreenStack *parent, const QString &name)
Creates a new NetEditorBase Screen.
QString GetShareDir(void)
Definition: mythdirs.cpp:222
void LoadData(void)
MythScreenStack * m_popupStack
Definition: neteditorbase.h:41
static bool Assign(ContainerType *container, UIType *&item, const QString &name, bool *err=nullptr)
Definition: mythuiutils.h:27
void SetText(const QString &text, const QString &name="", const QString &state="")
virtual bool Matches(bool search, bool tree)=0
bool Create(void) override
QNetworkAccessManager * m_manager
Definition: neteditorbase.h:43
const char * name
Definition: ParseText.cpp:328
QString GetMythXMLURL(void)
Definition: netcommon.cpp:32
CheckState state() const
static bool LoadWindowFromXML(const QString &xmlfile, const QString &windowname, MythUIType *parent)
MythUIType * GetFocusWidget(void) const
GrabberScript::scriptList m_grabberList
Definition: neteditorbase.h:38
MythMainWindow * GetMythMainWindow(void)
virtual bool InsertInDB(GrabberScript *script)=0
void SlotLoadedData(void)
bool keyPressEvent(QKeyEvent *) override
Key event handler.
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
MythUIButtonList * m_grabbers
Definition: neteditorbase.h:39
virtual ~NetEditorBase()
Screen in which all other widgets are contained and rendered.
void setChecked(CheckState state)