MythTV  master
mythnewsconfig.cpp
Go to the documentation of this file.
1 // QT Headers
2 #include <QStringList>
3 #include <QFile>
4 
5 // MythTV headers
6 #include <mythuitext.h>
7 #include <mythuibuttonlist.h>
8 #include <mythcontext.h>
9 #include <mythdbcon.h>
10 #include <mythmainwindow.h>
11 #include <mythdirs.h>
12 
13 // MythNews headers
14 #include "mythnewsconfig.h"
15 #include "newsdbutil.h"
16 #include "newssite.h"
17 
18 #define LOC QString("MythNewsConfig: ")
19 #define LOC_WARN QString("MythNewsConfig, Warning: ")
20 #define LOC_ERR QString("MythNewsConfig, Error: ")
21 
22 // ---------------------------------------------------
23 
25 {
26  public:
28  QStringList selectedSitesList;
29 };
30 
31 // ---------------------------------------------------
32 
34  : MythScreenType(parent, name),
35  m_priv(new MythNewsConfigPriv),
36  m_updateFreq(gCoreContext->GetNumSetting("NewsUpdateFrequency", 30))
37 {
38  populateSites();
39 }
40 
42 {
43  delete m_priv;
44 }
45 
47 {
48  QMutexLocker locker(&m_lock);
49 
50  QString filename = QString("%1%2")
51  .arg(GetShareDir()).arg("mythnews/news-sites.xml");
52 
53  QFile xmlFile(filename);
54 
55  if (!xmlFile.exists() || !xmlFile.open(QIODevice::ReadOnly))
56  {
57  LOG(VB_GENERAL, LOG_ERR, LOC + "Cannot open news-sites.xml");
58  return;
59  }
60 
61  QString errorMsg;
62  int errorLine = 0;
63  int errorColumn = 0;
64 
65  QDomDocument domDoc;
66 
67  if (!domDoc.setContent(&xmlFile, false, &errorMsg,
68  &errorLine, &errorColumn))
69  {
70  LOG(VB_GENERAL, LOG_ERR, LOC +
71  "Could not read content of news-sites.xml" +
72  QString("\n\t\t\tError parsing %1").arg(filename) +
73  QString("\n\t\t\tat line: %1 column: %2 msg: %3")
74  .arg(errorLine).arg(errorColumn).arg(errorMsg));
75  return;
76  }
77 
78  m_priv->categoryList.clear();
79 
80  QDomNodeList catList =
81  domDoc.elementsByTagName(QString::fromUtf8("category"));
82 
83  QDomNode catNode;
84  QDomNode siteNode;
85  for (int i = 0; i < catList.count(); i++)
86  {
87  catNode = catList.item(i);
88 
90  cat.m_name = catNode.toElement().attribute("name");
91 
92  QDomNodeList siteList = catNode.childNodes();
93 
94  for (int j = 0; j < siteList.count(); j++)
95  {
96  siteNode = siteList.item(j);
97 
98  NewsSiteItem site = NewsSiteItem();
99  site.m_name = siteNode.namedItem(QString("title")).toElement().text();
100  site.m_category = cat.m_name;
101  site.m_url = siteNode.namedItem(QString("url")).toElement().text();
102  site.m_ico = siteNode.namedItem(QString("ico")).toElement().text();
103  site.m_podcast = false;
104  site.m_inDB = findInDB(site.m_name);
105 
106  cat.m_siteList.push_back(site);
107  }
108 
109  m_priv->categoryList.push_back(cat);
110  }
111 
112  xmlFile.close();
113 }
114 
116 {
117  QMutexLocker locker(&m_lock);
118 
119  // Load the theme for this screen
120  bool foundtheme = LoadWindowFromXML("news-ui.xml", "config", this);
121 
122  if (!foundtheme)
123  return false;
124 
125  bool err = false;
126  UIUtilE::Assign(this, m_categoriesList, "category", &err);
127  UIUtilE::Assign(this, m_siteList, "sites", &err);
128  UIUtilW::Assign(this, m_helpText, "help", &err);
129 
130  if (err)
131  {
132  LOG(VB_GENERAL, LOG_ERR, "Cannot load screen 'config'");
133  return false;
134  }
135 
136  connect(m_categoriesList, SIGNAL(itemSelected(MythUIButtonListItem*)),
138  connect(m_siteList, SIGNAL(itemClicked(MythUIButtonListItem*)),
139  this, SLOT(toggleItem(MythUIButtonListItem*)));
140 
141  BuildFocusList();
142 
144 
145  loadData();
146 
147  return true;
148 }
149 
151 {
152  QMutexLocker locker(&m_lock);
153 
154  NewsCategory::List::iterator it = m_priv->categoryList.begin();
155  for (; it != m_priv->categoryList.end(); ++it)
156  {
157  MythUIButtonListItem *item =
158  new MythUIButtonListItem(m_categoriesList, (*it).m_name);
159  item->SetData(qVariantFromValue(&(*it)));
160  if (!(*it).m_siteList.empty())
161  item->setDrawArrow(true);
162  }
164 }
165 
167 {
168  QMutexLocker locker(&m_lock);
169 
170  if (!item )
171  return;
172 
173  NewsSiteItem *site = item->GetData().value<NewsSiteItem*>();
174  if (!site)
175  return;
176 
177  bool checked = (item->state() == MythUIButtonListItem::FullChecked);
178 
179  if (!checked)
180  {
181  if (insertInDB(site))
182  {
183  site->m_inDB = true;
185  }
186  }
187  else
188  {
189  if (removeFromDB(site))
190  {
191  site->m_inDB = false;
193  }
194  }
195 }
196 
198 {
199  QMutexLocker locker(&m_lock);
200 
201  if (!item)
202  return;
203 
204  m_siteList->Reset();
205 
206  NewsCategory *cat = item->GetData().value<NewsCategory*>();
207  if (!cat)
208  return;
209 
210  NewsSiteItem::List::iterator it = cat->m_siteList.begin();
211  for (; it != cat->m_siteList.end(); ++it)
212  {
213  MythUIButtonListItem *newitem =
214  new MythUIButtonListItem(m_siteList, (*it).m_name, nullptr, true,
215  (*it).m_inDB ?
218  newitem->SetData(qVariantFromValue(&(*it)));
219  }
220 }
221 
222 bool MythNewsConfig::keyPressEvent(QKeyEvent *event)
223 {
224  if (GetFocusWidget()->keyPressEvent(event))
225  return true;
226 
227  QStringList actions;
228  bool handled = GetMythMainWindow()->TranslateKeyPress("News", event, actions);
229 
230  if (!handled && MythScreenType::keyPressEvent(event))
231  handled = true;
232 
233  return handled;
234 }
MythNewsConfig(MythScreenStack *parent, const QString &name)
QString m_category
Definition: newssite.h:31
bool TranslateKeyPress(const QString &context, QKeyEvent *e, QStringList &actions, bool allowJumps=true)
Get a list of actions for a keypress in the given context.
QStringList selectedSitesList
QString m_name
Definition: newssite.h:30
bool m_podcast
Definition: newssite.h:35
void SetData(QVariant data)
vector< NewsCategory > List
Definition: newssite.h:42
bool insertInDB(RSSSite *site)
Definition: netutils.cpp:640
void setDrawArrow(bool flag)
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
QString m_ico
Definition: newssite.h:33
void BuildFocusList(void)
MythUIButtonList * m_categoriesList
MythUIButtonList * m_siteList
MythNewsConfigPriv * m_priv
QString GetShareDir(void)
Definition: mythdirs.cpp:222
static bool Assign(ContainerType *container, UIType *&item, const QString &name, bool *err=nullptr)
Definition: mythuiutils.h:27
bool m_inDB
Definition: newssite.h:34
const char * name
Definition: ParseText.cpp:328
CheckState state() const
static bool LoadWindowFromXML(const QString &xmlfile, const QString &windowname, MythUIType *parent)
MythUIText * m_helpText
MythUIType * GetFocusWidget(void) const
void Reset() override
Reset the widget to it's original state, should not reset changes made by the theme.
MythMainWindow * GetMythMainWindow(void)
bool keyPressEvent(QKeyEvent *) override
Key event handler.
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
void populateSites(void)
bool findInDB(const QString &url, ArticleType type)
Definition: netutils.cpp:524
void toggleItem(MythUIButtonListItem *item)
bool removeFromDB(RSSSite *site)
Definition: netutils.cpp:682
NewsCategory::List categoryList
bool Create(void) override
bool SetFocusWidget(MythUIType *widget=nullptr)
QString m_url
Definition: newssite.h:32
MythUIButtonListItem * GetItemFirst() const
Screen in which all other widgets are contained and rendered.
#define LOC
bool keyPressEvent(QKeyEvent *) override
Key event handler.
void loadData(void)
void slotCategoryChanged(MythUIButtonListItem *item)
void setChecked(CheckState state)