MythTV master
browserdbutil.cpp
Go to the documentation of this file.
1// Qt
2#include <QSqlError>
3
4// MythTV
10
11// mythbrowser
12#include "bookmarkmanager.h"
13#include "browserdbutil.h"
14
15const QString currentDatabaseVersion = "1003";
16const QString MythBrowserVersionName = "BrowserDBSchemaVer";
17
19{
20 QString dbver = gCoreContext->GetSetting("BrowserDBSchemaVer");
21
22 if (dbver == currentDatabaseVersion)
23 return true;
24
25 if (dbver == "")
26 {
27 LOG(VB_GENERAL, LOG_NOTICE,
28 "Inserting MythBrowser initial database information.");
29
30 DBUpdates updates
31 {
32 "DROP TABLE IF EXISTS websites;",
33 "CREATE TABLE websites ("
34 "id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY, "
35 "category VARCHAR(100) NOT NULL, "
36 "name VARCHAR(100) NOT NULL, "
37 "url VARCHAR(255) NOT NULL);"
38 };
40 updates, "1000", dbver))
41 return false;
42 }
43
44 if (dbver == "1000")
45 {
46 DBUpdates updates
47 {
48 "UPDATE settings SET data = 'Internal' WHERE data LIKE '%mythbrowser' AND value = 'WebBrowserCommand';"
49 };
51 updates, "1001", dbver))
52 return false;
53 }
54
55 if (dbver == "1001")
56 {
57 DBUpdates updates
58 {
59 "DELETE FROM keybindings "
60 " WHERE action = 'DELETETAB' AND context = 'Browser';"
61 };
63 updates, "1002", dbver))
64 return false;
65 }
66
67 if (dbver == "1002")
68 {
69 DBUpdates updates
70 {
71 "ALTER TABLE `websites` ADD `homepage` BOOL NOT NULL;"
72 };
74 updates, "1003", dbver))
75 return false;
76 }
77
78 return true;
79}
80
81bool FindInDB(const QString &category, const QString& name)
82{
84 query.prepare("SELECT name FROM websites "
85 "WHERE category = :CATEGORY AND name = :NAME ;");
86 query.bindValue(":CATEGORY", category);
87 query.bindValue(":NAME", name);
88 if (!query.exec())
89 {
90 MythDB::DBError("mythbrowser: find in db", query);
91 return false;
92 }
93
94 return (query.size() > 0);
95}
96
98{
100 query.prepare("UPDATE `websites` SET `homepage` = '0' WHERE `homepage` = '1';");
101
102 return query.exec();
103}
104
106{
108 query.prepare("UPDATE `websites` SET `homepage` = '1' "
109 "WHERE `category` = :CATEGORY "
110 "AND `name` = :NAME;");
111 query.bindValue(":CATEGORY", site->m_category);
112 query.bindValue(":NAME", site->m_name);
113
114 return query.exec();
115}
116
118{
119 if (!site)
120 return false;
121
122 return InsertInDB(site->m_category, site->m_name, site->m_url, site->m_isHomepage);
123}
124
125bool InsertInDB(const QString &category,
126 const QString &name,
127 const QString &url,
128 const bool isHomepage)
129{
130 if (category.isEmpty() || name.isEmpty() || url.isEmpty())
131 return false;
132
133 if (FindInDB(category, name))
134 return false;
135
136 QString _url = url.trimmed();
137 if (!_url.startsWith("http://") && !_url.startsWith("https://") &&
138 !_url.startsWith("file:/"))
139 _url.prepend("http://");
140
141 _url.replace("&amp;","&");
142
144 query.prepare("INSERT INTO websites (category, name, url, homepage) "
145 "VALUES(:CATEGORY, :NAME, :URL, :HOMEPAGE);");
146 query.bindValue(":CATEGORY", category);
147 query.bindValue(":NAME", name);
148 query.bindValue(":URL", _url);
149 query.bindValue(":HOMEPAGE", isHomepage);
150 if (!query.exec())
151 {
152 MythDB::DBError("mythbrowser: inserting in DB", query);
153 return false;
154 }
155
156 return (query.numRowsAffected() > 0);
157}
158
160{
161 if (!site)
162 return false;
163
164 return RemoveFromDB(site->m_category, site->m_name);
165}
166
167bool RemoveFromDB(const QString &category, const QString &name)
168{
170 query.prepare("DELETE FROM websites "
171 "WHERE category = :CATEGORY AND name = :NAME;");
172 query.bindValue(":CATEGORY", category);
173 query.bindValue(":NAME", name);
174 if (!query.exec())
175 {
176 MythDB::DBError("mythbrowser: delete from db", query);
177 return false;
178 }
179
180 return (query.numRowsAffected() > 0);
181}
182
183int GetCategoryList(QStringList &list)
184{
186 query.prepare("SELECT DISTINCT category FROM websites "
187 "ORDER BY category;");
188
189 if (!query.exec())
190 {
191 MythDB::DBError("mythbrowser: get category list", query);
192 return 0;
193 }
194
195 while (query.next())
196 {
197 list << query.value(0).toString();
198 }
199
200 return list.size();
201}
202
203int GetSiteList(QList<Bookmark*> &siteList)
204{
205 while (!siteList.isEmpty())
206 delete siteList.takeFirst();
207
209
210 if (!query.exec("SELECT category, name, url, homepage FROM websites "
211 "ORDER BY category, name"))
212 {
213 LOG(VB_GENERAL, LOG_ERR, "BookmarkManager: Error in loading from DB");
214 }
215 else
216 {
217 std::shared_ptr<MythSortHelper>sh = getMythSortHelper();
218 while (query.next())
219 {
220 auto *site = new Bookmark();
221 site->m_category = query.value(0).toString();
222 site->m_name = query.value(1).toString();
223 site->m_sortName = sh->doTitle(site->m_name);
224 site->m_url = query.value(2).toString();
225 site->m_isHomepage = query.value(3).toBool();
226 site->m_selected = false;
227 siteList.append(site);
228 }
229 std::sort(siteList.begin(), siteList.end(), Bookmark::sortByName);
230 }
231
232 return siteList.size();
233}
const QString currentDatabaseVersion
bool UpgradeBrowserDatabaseSchema(void)
int GetSiteList(QList< Bookmark * > &siteList)
bool InsertInDB(Bookmark *site)
bool RemoveFromDB(Bookmark *site)
int GetCategoryList(QStringList &list)
bool FindInDB(const QString &category, const QString &name)
bool UpdateHomepageInDB(Bookmark *site)
const QString MythBrowserVersionName
bool ResetHomepageFromDB()
QString m_category
QString m_name
static bool sortByName(Bookmark *a, Bookmark *b)
QString m_url
bool m_isHomepage
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:128
bool prepare(const QString &query)
QSqlQuery::prepare() is not thread safe in Qt <= 3.3.2.
Definition: mythdbcon.cpp:837
QVariant value(int i) const
Definition: mythdbcon.h:204
int size(void) const
Definition: mythdbcon.h:214
int numRowsAffected() const
Definition: mythdbcon.h:217
bool exec(void)
Wrap QSqlQuery::exec() so we can display SQL.
Definition: mythdbcon.cpp:618
void bindValue(const QString &placeholder, const QVariant &val)
Add a single binding.
Definition: mythdbcon.cpp:888
bool next(void)
Wrap QSqlQuery::next() so we can display the query results.
Definition: mythdbcon.cpp:812
static MSqlQueryInfo InitCon(ConnectionReuse _reuse=kNormalConnection)
Only use this in combination with MSqlQuery constructor.
Definition: mythdbcon.cpp:550
QString GetSetting(const QString &key, const QString &defaultval="")
static void DBError(const QString &where, const MSqlQuery &query)
Definition: mythdb.cpp:226
bool performActualUpdate(const QString &component, const QString &versionkey, const DBUpdates &updates, const QString &version, QString &dbver)
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
std::vector< std::string > DBUpdates
Definition: mythdbcheck.h:9
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
std::shared_ptr< MythSortHelper > getMythSortHelper(void)
Get a pointer to the MythSortHelper singleton.