MythTV  master
mythnewsconfig.cpp
Go to the documentation of this file.
1 // QT Headers
2 #include <QFile>
3 #include <QStringList>
4 
5 // MythTV headers
6 #include <libmyth/mythcontext.h>
8 #include <libmythbase/mythdirs.h>
11 #include <libmythui/mythuitext.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 m_selectedSitesList;
29 };
30 
31 // ---------------------------------------------------
32 
33 MythNewsConfig::MythNewsConfig(MythScreenStack *parent, const QString &name)
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(), "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->m_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->m_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 
140 
141  BuildFocusList();
142 
144 
145  loadData();
146 
147  return true;
148 }
149 
151 {
152  QMutexLocker locker(&m_lock);
153 
154  for (auto & category : m_priv->m_categoryList)
155  {
156  auto *item = new MythUIButtonListItem(m_categoriesList,category.m_name);
157  item->SetData(QVariant::fromValue(&category));
158  if (!category.m_siteList.empty())
159  item->setDrawArrow(true);
160  }
162 }
163 
165 {
166  QMutexLocker locker(&m_lock);
167 
168  if (!item )
169  return;
170 
171  auto *site = item->GetData().value<NewsSiteItem*>();
172  if (!site)
173  return;
174 
175  bool checked = (item->state() == MythUIButtonListItem::FullChecked);
176 
177  if (!checked)
178  {
179  if (insertInDB(site))
180  {
181  site->m_inDB = true;
183  }
184  }
185  else
186  {
187  if (removeFromDB(site))
188  {
189  site->m_inDB = false;
191  }
192  }
193 }
194 
196 {
197  QMutexLocker locker(&m_lock);
198 
199  if (!item)
200  return;
201 
202  m_siteList->Reset();
203 
204  auto *cat = item->GetData().value<NewsCategory*>();
205  if (!cat)
206  return;
207 
208  for (auto & site : cat->m_siteList)
209  {
210  auto *newitem =
211  new MythUIButtonListItem(m_siteList, site.m_name, nullptr, true,
212  site.m_inDB ?
215  newitem->SetData(QVariant::fromValue(&site));
216  }
217 }
218 
219 bool MythNewsConfig::keyPressEvent(QKeyEvent *event)
220 {
221  if (GetFocusWidget()->keyPressEvent(event))
222  return true;
223 
224  QStringList actions;
225  bool handled = GetMythMainWindow()->TranslateKeyPress("News", event, actions);
226 
227  if (!handled && MythScreenType::keyPressEvent(event))
228  handled = true;
229 
230  return handled;
231 }
newsdbutil.h
MythNewsConfig::loadData
void loadData(void)
Definition: mythnewsconfig.cpp:150
NewsSiteItem::m_inDB
bool m_inDB
Definition: newssite.h:36
NewsCategory
Definition: newssite.h:41
mythuitext.h
insertInDB
bool insertInDB(RSSSite *site)
Definition: netutils.cpp:643
findInDB
bool findInDB(const QString &url, ArticleType type)
Definition: netutils.cpp:527
mythnewsconfig.h
NewsSiteItem::m_category
QString m_category
Definition: newssite.h:33
hardwareprofile.devicelist.cat
def cat(file_name)
Definition: devicelist.py:95
MythNewsConfigPriv::m_categoryList
NewsCategory::List m_categoryList
Definition: mythnewsconfig.cpp:27
MythUIButtonList::itemSelected
void itemSelected(MythUIButtonListItem *item)
MythScreenStack
Definition: mythscreenstack.h:16
mythdbcon.h
MythUIButtonListItem::FullChecked
@ FullChecked
Definition: mythuibuttonlist.h:48
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
MythScreenType
Screen in which all other widgets are contained and rendered.
Definition: mythscreentype.h:45
mythdirs.h
LOC
#define LOC
Definition: mythnewsconfig.cpp:18
NewsCategory::List
std::vector< NewsCategory > List
Definition: newssite.h:44
mythuibuttonlist.h
NewsSiteItem::m_podcast
bool m_podcast
Definition: newssite.h:37
MythScreenType::GetFocusWidget
MythUIType * GetFocusWidget(void) const
Definition: mythscreentype.cpp:113
NewsSiteItem
Definition: newssite.h:27
MythNewsConfigPriv::m_selectedSitesList
QStringList m_selectedSitesList
Definition: mythnewsconfig.cpp:28
MythUIButtonListItem
Definition: mythuibuttonlist.h:41
MythUIButtonList::itemClicked
void itemClicked(MythUIButtonListItem *item)
MythMainWindow::TranslateKeyPress
bool TranslateKeyPress(const QString &Context, QKeyEvent *Event, QStringList &Actions, bool AllowJumps=true)
Get a list of actions for a keypress in the given context.
Definition: mythmainwindow.cpp:1104
NewsSiteItem::m_url
QString m_url
Definition: newssite.h:34
MythScreenType::SetFocusWidget
bool SetFocusWidget(MythUIType *widget=nullptr)
Definition: mythscreentype.cpp:118
removeFromDB
bool removeFromDB(RSSSite *site)
Definition: netutils.cpp:685
GetShareDir
QString GetShareDir(void)
Definition: mythdirs.cpp:222
MythNewsConfigPriv
Definition: mythnewsconfig.cpp:24
MythScreenType::BuildFocusList
void BuildFocusList(void)
Definition: mythscreentype.cpp:206
MythNewsConfig::m_helpText
MythUIText * m_helpText
Definition: mythnewsconfig.h:46
MythUIButtonListItem::GetData
QVariant GetData()
Definition: mythuibuttonlist.cpp:3665
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:54
NewsSiteItem::m_name
QString m_name
Definition: newssite.h:32
UIUtilDisp::Assign
static bool Assign(ContainerType *container, UIType *&item, const QString &name, bool *err=nullptr)
Definition: mythuiutils.h:27
newssite.h
MythNewsConfig::m_lock
QRecursiveMutex m_lock
Definition: mythnewsconfig.h:39
MythScreenType::keyPressEvent
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
Definition: mythscreentype.cpp:404
MythNewsConfig::~MythNewsConfig
~MythNewsConfig() override
Definition: mythnewsconfig.cpp:41
MythNewsConfig::Create
bool Create(void) override
Definition: mythnewsconfig.cpp:115
XMLParseBase::LoadWindowFromXML
static bool LoadWindowFromXML(const QString &xmlfile, const QString &windowname, MythUIType *parent)
Definition: xmlparsebase.cpp:695
MythNewsConfig::m_priv
MythNewsConfigPriv * m_priv
Definition: mythnewsconfig.h:41
MythUIButtonListItem::state
CheckState state() const
Definition: mythuibuttonlist.cpp:3619
mythcontext.h
MythUIButtonList::Reset
void Reset() override
Reset the widget to it's original state, should not reset changes made by the theme.
Definition: mythuibuttonlist.cpp:116
GetMythMainWindow
MythMainWindow * GetMythMainWindow(void)
Definition: mythmainwindow.cpp:102
MythNewsConfig::keyPressEvent
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
Definition: mythnewsconfig.cpp:219
MythNewsConfig::toggleItem
void toggleItem(MythUIButtonListItem *item)
Definition: mythnewsconfig.cpp:164
MythNewsConfig::MythNewsConfig
MythNewsConfig(MythScreenStack *parent, const QString &name)
Definition: mythnewsconfig.cpp:33
NewsSiteItem::m_ico
QString m_ico
Definition: newssite.h:35
MythNewsConfig::m_categoriesList
MythUIButtonList * m_categoriesList
Definition: mythnewsconfig.h:43
MythUIButtonList::GetItemFirst
MythUIButtonListItem * GetItemFirst() const
Definition: mythuibuttonlist.cpp:1633
MythNewsConfig::m_siteList
MythUIButtonList * m_siteList
Definition: mythnewsconfig.h:44
build_compdb.filename
filename
Definition: build_compdb.py:21
mythmainwindow.h
MythUIButtonListItem::setChecked
void setChecked(CheckState state)
Definition: mythuibuttonlist.cpp:3629
MythNewsConfig::slotCategoryChanged
void slotCategoryChanged(MythUIButtonListItem *item)
Definition: mythnewsconfig.cpp:195
MythUIButtonListItem::NotChecked
@ NotChecked
Definition: mythuibuttonlist.h:46
MythNewsConfig::populateSites
void populateSites(void)
Definition: mythnewsconfig.cpp:46