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  bool search = false;
115  bool tree = false;
116 
117  title = grabber.firstChildElement("name").text();
118  commandline = grabber.firstChildElement("command").text();
119  author = grabber.firstChildElement("author").text();
120  image = grabber.firstChildElement("thumbnail").text();
121  type = grabber.firstChildElement("type").text();
122  description = grabber.firstChildElement("description").text();
123  double version = grabber.firstChildElement("version").text().toDouble();
124 
125  QString searchstring = grabber.firstChildElement("search").text();
126 
127  if (!searchstring.isEmpty() &&
128  (searchstring.toLower() == "true" ||
129  searchstring.toLower() == "yes" ||
130  searchstring == "1"))
131  search = true;
132 
133  QString treestring = grabber.firstChildElement("tree").text();
134 
135  if (!treestring.isEmpty() &&
136  (treestring.toLower() == "true" ||
137  treestring.toLower() == "yes" ||
138  treestring == "1"))
139  tree = true;
140 
141  if (type.toLower() == "video" && Matches(search, tree))
142  {
143  LOG(VB_GENERAL, LOG_INFO,
144  QString("Found Source %1...").arg(title));
145  m_grabberList.append(new GrabberScript(title, image, VIDEO_FILE,
146  author, search, tree,
147  description, commandline,
148  version));
149  }
150 
151  grabber = grabber.nextSiblingElement("grabber");
152  }
153 
154  ParsedData();
155  }
156 
158 {
159  if (m_busyPopup)
160  {
161  m_busyPopup->Close();
162  m_busyPopup = nullptr;
163  }
164 
166 }
167 
168 void NetEditorBase::CreateBusyDialog(const QString& title)
169 {
170  if (m_busyPopup)
171  return;
172 
173  const QString& message = title;
174 
176  "mythvideobusydialog");
177 
178  if (m_busyPopup->Create())
180  else
181  {
182  delete m_busyPopup;
183  m_busyPopup = nullptr;
184  }
185 }
186 
188 {
189  for (GrabberScript::scriptList::iterator i = m_grabberList.begin();
190  i != m_grabberList.end(); ++i)
191  {
192  MythUIButtonListItem *item =
193  new MythUIButtonListItem(m_grabbers, (*i)->GetTitle());
194  item->SetText((*i)->GetTitle(), "title");
195  item->SetData(qVariantFromValue(*i));
196  QString img = (*i)->GetImage();
197  QString thumb;
198 
199  if (!img.startsWith("/") && !img.isEmpty())
200  thumb = QString("%1mythnetvision/icons/%2").arg(GetShareDir())
201  .arg((*i)->GetImage());
202  else
203  thumb = img;
204 
205  item->SetImage(thumb);
206  item->setCheckable(true);
208  QFileInfo fi((*i)->GetCommandline());
209 
210  if (FindGrabberInDB(fi.fileName()))
212  }
213 }
214 
216 {
217  if (!item)
218  return;
219 
220  GrabberScript *script = item->GetData().value<GrabberScript*>();
221 
222  if (!script)
223  return;
224 
225  bool checked = (item->state() == MythUIButtonListItem::FullChecked);
226 
227  if (!checked)
228  {
229  if (InsertInDB(script))
230  {
231  m_changed = true;
233  }
234  }
235  else if (RemoveFromDB(script))
236  {
237  m_changed = true;
239  }
240 }
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)