Skip to content

Commit 66abe3e

Browse files
committed
Plugins::WebBrowser: modify WebBrowser.ini directory
1 parent ade69b8 commit 66abe3e

7 files changed

Lines changed: 152 additions & 54 deletions

File tree

Plugins/WebBrowser/DatabaseUrl.cpp

Lines changed: 69 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,9 @@ CDatabaseUrl::CDatabaseUrl(QObject *parent)
1414

1515
bool CDatabaseUrl::OnInitializeDatabase()
1616
{
17-
QSqlQuery query(GetDatabase());
18-
19-
// 创建历史记录表
20-
bool success = query.exec(
21-
"CREATE TABLE IF NOT EXISTS url ("
22-
" id INTEGER PRIMARY KEY AUTOINCREMENT,"
23-
" url TEXT UNIQUE NOT NULL,"
24-
" title TEXT,"
25-
" icon INTEGER DEFAULT 0,"
26-
" visit_time DATETIME DEFAULT CURRENT_TIMESTAMP"
27-
")"
28-
);
29-
30-
if (!success) {
31-
qCritical(log) << "Failed to create url table:" << query.lastError().text();
32-
return false;
33-
}
34-
35-
// 创建索引
36-
query.exec("CREATE INDEX IF NOT EXISTS idx_url_url ON url(url)");
37-
17+
bool success = false;
18+
success = CDatabase::OnInitializeDatabase();
19+
if(!success) return success;
3820
m_iconDB.SetDatabase(GetDatabase());
3921
success = m_iconDB.OnInitializeDatabase();
4022
return success;
@@ -269,7 +251,9 @@ QList<int> CDatabaseUrl::GetDomain(const QString &szDomain)
269251
ret << query.value(0).toInt();
270252
}
271253
} else {
272-
qCritical(log) << "Failed to get domain:" << query.lastError().text();
254+
qCritical(log) << "Failed to get domain:"
255+
<< query.lastError().text()
256+
<< query.executedQuery();
273257
}
274258

275259
return ret;
@@ -299,6 +283,10 @@ QList<CDatabaseUrl::UrlItem> CDatabaseUrl::Search(const QString &keyword)
299283
item.icon = m_iconDB.GetIcon(query.value(4).toInt());
300284
items.append(item);
301285
}
286+
} else {
287+
qCritical(log) << "Failed to search:"
288+
<< query.lastError().text()
289+
<< query.executedQuery();
302290
}
303291

304292
return items;
@@ -313,3 +301,62 @@ bool CDatabaseUrl::ImportFromJson(const QJsonObject &obj)
313301
{
314302
return true;
315303
}
304+
305+
bool CDatabaseUrl::OnInitializeSqliteDatabase()
306+
{
307+
QSqlQuery query(GetDatabase());
308+
309+
// 创建历史记录表
310+
bool success = query.exec(
311+
"CREATE TABLE IF NOT EXISTS url ("
312+
" id INTEGER PRIMARY KEY AUTOINCREMENT,"
313+
" url TEXT UNIQUE NOT NULL,"
314+
" title TEXT,"
315+
" icon INTEGER DEFAULT 0,"
316+
" visit_time DATETIME DEFAULT CURRENT_TIMESTAMP"
317+
")"
318+
);
319+
320+
if (!success) {
321+
qCritical(log) << "Failed to create url table:"
322+
<< query.lastError().text()
323+
<< query.executedQuery();
324+
return false;
325+
}
326+
327+
// 创建索引
328+
success = query.exec("CREATE INDEX IF NOT EXISTS idx_url_url ON url(url)");
329+
if (!success) {
330+
qWarning(log) << "Failed to create index idx_url_url:"
331+
<< query.lastError().text()
332+
<< query.executedQuery();
333+
}
334+
335+
return true;
336+
}
337+
338+
bool CDatabaseUrl::OnInitializeMySqlDatabase()
339+
{
340+
QSqlQuery query(GetDatabase());
341+
342+
// 创建历史记录表
343+
bool success = query.exec(
344+
"CREATE TABLE IF NOT EXISTS url ("
345+
" id INTEGER PRIMARY KEY AUTO_INCREMENT,"
346+
" url TEXT NOT NULL,"
347+
" title TEXT,"
348+
" icon INTEGER DEFAULT 0,"
349+
" visit_time DATETIME DEFAULT CURRENT_TIMESTAMP,"
350+
" INDEX idx_url_url (url)"
351+
")"
352+
);
353+
354+
if (!success) {
355+
qCritical(log) << "Failed to create url table:"
356+
<< query.lastError().text()
357+
<< query.executedQuery();
358+
return false;
359+
}
360+
361+
return success;
362+
}

Plugins/WebBrowser/DatabaseUrl.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,10 @@ class CDatabaseUrl : public CDatabase
4141

4242
private:
4343
CDatabaseIcon m_iconDB;
44+
45+
// CDatabase interface
46+
protected:
47+
virtual bool OnInitializeSqliteDatabase() override;
48+
virtual bool OnInitializeMySqlDatabase() override;
4449
};
4550

Plugins/WebBrowser/FrmWebBrowser.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,12 +468,12 @@ QWebEngineProfile* CFrmWebBrowser::GetProfile(bool offTheRecord)
468468
if(m_profile)
469469
return m_profile.get();
470470

471-
QSettings set(RabbitCommon::CDir::Instance()->GetDirUserData()
471+
QSettings set(RabbitCommon::CDir::Instance()->GetDirUserConfig()
472472
+ QDir::separator() + "WebBrowser.ini",
473473
QSettings::IniFormat);
474474
QString name = "io.github.KangLin.RabbitRemoteControl";
475475
#if QT_VERSION >= QT_VERSION_CHECK(6, 9, 0)
476-
name += QLatin1StringView(qWebEngineChromiumVersion());
476+
name += "_" + QLatin1StringView(qWebEngineChromiumVersion());
477477
name = set.value("Profile/Name", name).toString();
478478
QWebEngineProfileBuilder profileBuilder;
479479
m_profile.reset(profileBuilder.createProfile(name));

Plugins/WebBrowser/FrmWebView.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ void CFrmWebView::slotCertificateError(QWebEngineCertificateError error)
321321
}
322322

323323
error.defer();
324-
QSettings set(RabbitCommon::CDir::Instance()->GetDirUserData()
324+
QSettings set(RabbitCommon::CDir::Instance()->GetDirUserConfig()
325325
+ QDir::separator() + "WebBrowser.ini",
326326
QSettings::IniFormat);
327327
if(set.value("Certificate/Error/Accept" + error.url().toString(), false).toBool()) {

Plugins/WebBrowser/History/HistoryDatabase.cpp

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -70,26 +70,9 @@ CHistoryDatabase::~CHistoryDatabase()
7070

7171
bool CHistoryDatabase::OnInitializeDatabase()
7272
{
73-
QSqlQuery query(GetDatabase());
74-
75-
// 创建历史记录表
76-
bool success = query.exec(
77-
"CREATE TABLE IF NOT EXISTS history ("
78-
" id INTEGER PRIMARY KEY AUTOINCREMENT,"
79-
" url INTEGER NOT NULL,"
80-
" visit_time DATETIME DEFAULT CURRENT_TIMESTAMP"
81-
")"
82-
);
83-
84-
if (!success) {
85-
qCritical(log) << "Failed to create table:" << query.lastError().text();
86-
return false;
87-
}
88-
89-
// 创建索引
90-
query.exec("CREATE INDEX IF NOT EXISTS idx_history_url ON history(url)");
91-
query.exec("CREATE INDEX IF NOT EXISTS idx_history_time ON history(visit_time)");
92-
73+
bool success = false;
74+
success = CDatabase::OnInitializeDatabase();
75+
if(!success) return false;
9376
m_UrlDB.SetDatabase(GetDatabase());
9477
m_UrlDB.OnInitializeDatabase();
9578
return success;
@@ -761,3 +744,61 @@ bool CHistoryDatabase::ImportFromJson(const QJsonObject &obj)
761744
{
762745
return true;
763746
}
747+
748+
bool CHistoryDatabase::OnInitializeSqliteDatabase()
749+
{
750+
QSqlQuery query(GetDatabase());
751+
752+
// 创建历史记录表
753+
bool success = query.exec(
754+
"CREATE TABLE IF NOT EXISTS history ("
755+
" id INTEGER PRIMARY KEY AUTOINCREMENT,"
756+
" url INTEGER NOT NULL,"
757+
" visit_time DATETIME DEFAULT CURRENT_TIMESTAMP"
758+
")"
759+
);
760+
761+
if (!success) {
762+
qCritical(log) << "Failed to create table:" << query.lastError().text();
763+
return false;
764+
}
765+
766+
// 创建索引
767+
success = query.exec("CREATE INDEX IF NOT EXISTS idx_history_url ON history(url)");
768+
if (!success) {
769+
qWarning(log) << "Failed to create index idx_history_url:"
770+
<< query.lastError().text()
771+
<< query.executedQuery();
772+
}
773+
success = query.exec("CREATE INDEX IF NOT EXISTS idx_history_time ON history(visit_time)");
774+
if (!success) {
775+
qWarning(log) << "Failed to create index idx_history_time:"
776+
<< query.lastError().text()
777+
<< query.executedQuery();
778+
}
779+
780+
return true;
781+
}
782+
783+
bool CHistoryDatabase::OnInitializeMySqlDatabase()
784+
{
785+
QSqlQuery query(GetDatabase());
786+
787+
// 创建历史记录表
788+
bool success = query.exec(
789+
"CREATE TABLE IF NOT EXISTS history ("
790+
" id INTEGER PRIMARY KEY AUTO_INCREMENT,"
791+
" url INTEGER NOT NULL,"
792+
" visit_time DATETIME DEFAULT CURRENT_TIMESTAMP,"
793+
" INDEX idx_history_url (url),"
794+
" INDEX idx_history_time (visit_time)"
795+
")"
796+
);
797+
798+
if (!success) {
799+
qCritical(log) << "Failed to create table:" << query.lastError().text();
800+
return false;
801+
}
802+
803+
return true;
804+
}

Plugins/WebBrowser/History/HistoryDatabase.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ class CHistoryDatabase : public CDatabase
6565

6666
virtual bool ExportToJson(QJsonObject &obj) override;
6767
virtual bool ImportFromJson(const QJsonObject &obj) override;
68+
69+
// CDatabase interface
70+
protected:
71+
virtual bool OnInitializeSqliteDatabase() override;
72+
virtual bool OnInitializeMySqlDatabase() override;
6873
};
6974

7075
void enableSqlTrace(const QString& connectionName = QSqlDatabase::defaultConnection);

Src/Database.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ bool CDatabase::OpenDatabase(CParameterDatabase *pPara)
4747
bool bRet = false;
4848
if(!pPara) {
4949
bRet = OpenSQLiteDatabase();
50-
if(!bRet) return false;
51-
return OnInitializeDatabase();
50+
return bRet;
5251
}
5352

5453
m_pPara = pPara;
@@ -79,10 +78,8 @@ bool CDatabase::OpenDatabase(CParameterDatabase *pPara)
7978
qDebug(log) << "Multiple result sets:" << driver->hasFeature(QSqlDriver::MultipleResultSets);
8079
qDebug(log) << "Cancel query:" << driver->hasFeature(QSqlDriver::CancelQuery);
8180
}
82-
83-
if(!bRet) return false;
84-
85-
return OnInitializeDatabase();
81+
82+
return bRet;
8683
}
8784

8885
bool CDatabase::OpenSQLiteDatabase(
@@ -119,7 +116,8 @@ bool CDatabase::OpenSQLiteDatabase(
119116
qInfo(log) << "Open sqlite database connect:"
120117
<< m_database.connectionName()
121118
<< "database name:" << m_database.databaseName();
122-
return true;
119+
120+
return OnInitializeDatabase();
123121
}
124122

125123
bool CDatabase::OpenMySqlDatabase(CParameterDatabase *pPara)
@@ -166,7 +164,8 @@ bool CDatabase::OpenMySqlDatabase(CParameterDatabase *pPara)
166164
qInfo(log) << "Open mysql database connect:"
167165
<< m_database.connectionName()
168166
<< "database name:" << m_database.databaseName();
169-
return true;
167+
168+
return OnInitializeDatabase();
170169
}
171170

172171
bool CDatabase::OpenODBCDatabase(CParameterDatabase* pPara)
@@ -207,7 +206,8 @@ bool CDatabase::OpenODBCDatabase(CParameterDatabase* pPara)
207206
qInfo(log) << "Open odbc database connect:"
208207
<< m_database.connectionName()
209208
<< "database name:" << m_database.databaseName();
210-
return true;
209+
210+
return OnInitializeDatabase();
211211
}
212212

213213
bool CDatabase::OnInitializeDatabase()

0 commit comments

Comments
 (0)