Skip to content

Commit 032cae2

Browse files
committed
Plugins::WebBrowser: add export json file
1 parent c7c4209 commit 032cae2

File tree

5 files changed

+112
-13
lines changed

5 files changed

+112
-13
lines changed

Plugins/WebBrowser/History/FrmHistory.cpp

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -455,14 +455,26 @@ void CFrmHistory::slotImport()
455455
QString filename = QFileDialog::getOpenFileName(
456456
this, tr("Import histories"),
457457
RabbitCommon::CDir::Instance()->GetDirUserDocument(),
458-
tr("CSV file (*.csv);; All files (*.*)"));
458+
tr("JSON (*.json);; CSV file (*.csv);; All files (*.*)"));
459459

460460
if (!filename.isEmpty()) {
461-
if(m_pModelHistory->importFromCSV(filename)) {
462-
slotRefresh();
463-
QMessageBox::information(this, tr("Success"), tr("Histories import from csv file successfully"));
464-
} else {
465-
QMessageBox::warning(this, tr("Failure"), tr("Failed to import histories from csv file"));
461+
QFileInfo fi(filename);
462+
if(0 == fi.suffix().compare("json", Qt::CaseInsensitive)) {
463+
if (m_pModelHistory->importFromJson(filename)) {
464+
slotRefresh();
465+
QMessageBox::information(this, tr("Success"), tr("Histories import from json file successfully"));
466+
} else {
467+
QMessageBox::warning(this, tr("Failure"), tr("Failed to import histories from json file"));
468+
}
469+
return;
470+
}
471+
if(0 == fi.suffix().compare("csv", Qt::CaseInsensitive)) {
472+
if(m_pModelHistory->importFromCSV(filename)) {
473+
slotRefresh();
474+
QMessageBox::information(this, tr("Success"), tr("Histories import from csv file successfully"));
475+
} else {
476+
QMessageBox::warning(this, tr("Failure"), tr("Failed to import histories from csv file"));
477+
}
466478
}
467479
}
468480
}
@@ -472,13 +484,24 @@ void CFrmHistory::slotExport()
472484
QString filename = QFileDialog::getSaveFileName(
473485
this, tr("Export histories"),
474486
RabbitCommon::CDir::Instance()->GetDirUserDocument(),
475-
tr("CSV (*.csv);; All files (*.*)"));
487+
tr("JSON (*.json);; CSV (*.csv);; All files (*.*)"));
476488

477489
if (!filename.isEmpty()) {
478-
if (m_pModelHistory->exportToCSV(filename)) {
479-
QMessageBox::information(this, tr("Success"), tr("Histories exported to csv file successfully"));
480-
} else {
481-
QMessageBox::warning(this, tr("Failure"), tr("Failed to export histories to csv file"));
490+
QFileInfo fi(filename);
491+
if(0 == fi.suffix().compare("json", Qt::CaseInsensitive)) {
492+
if (m_pModelHistory->exportToJson(filename)) {
493+
QMessageBox::information(this, tr("Success"), tr("Histories exported to json file successfully"));
494+
} else {
495+
QMessageBox::warning(this, tr("Failure"), tr("Failed to export histories to json file"));
496+
}
497+
return;
498+
}
499+
if(0 == fi.suffix().compare("csv", Qt::CaseInsensitive)) {
500+
if (m_pModelHistory->exportToCSV(filename)) {
501+
QMessageBox::information(this, tr("Success"), tr("Histories exported to csv file successfully"));
502+
} else {
503+
QMessageBox::warning(this, tr("Failure"), tr("Failed to export histories to csv file"));
504+
}
482505
}
483506
}
484507
}

Plugins/WebBrowser/History/HistoryDatabase.cpp

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
#include <QDebug>
88
#include <QLoggingCategory>
99
#include <QCoreApplication>
10-
#include "RabbitCommonDir.h"
10+
#include <QJsonDocument>
11+
#include <QJsonObject>
12+
#include <QJsonArray>
1113
#include "HistoryDatabase.h"
1214

1315
static Q_LOGGING_CATEGORY(log, "DB.History")
@@ -441,6 +443,65 @@ QDateTime CHistoryDatabase::getLastVisitTime()
441443
return QDateTime();
442444
}
443445

446+
bool CHistoryDatabase::importFromJson(const QString &filename)
447+
{
448+
QFile file(filename);
449+
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
450+
return false;
451+
452+
do {
453+
QJsonDocument doc;
454+
doc = QJsonDocument::fromJson(file.readAll());
455+
if(!doc.isArray())
456+
break;
457+
auto array = doc.array();
458+
for(auto it = array.begin(); it != array.end(); it++) {
459+
auto o = it->toObject();
460+
QString url = o["url"].toString();
461+
QString title = o["title"].toString();
462+
QDateTime time = QDateTime::fromString(o["visit_time"].toString());
463+
qDebug(log) << "title:" << title << "url:" << url << "visit_time:" << time;
464+
bool ok = addHistoryEntry(url, title, time);
465+
if(!ok)
466+
qWarning(log) << "Failed to add history:" << o["title"].toString();
467+
}
468+
} while(0);
469+
470+
file.close();
471+
return true;
472+
}
473+
474+
bool CHistoryDatabase::exportToJson(const QString &filename)
475+
{
476+
QFile file(filename);
477+
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
478+
return false;
479+
480+
QTextStream out(&file);
481+
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
482+
out.setEncoding(QStringConverter::Utf8);
483+
#else
484+
out.setCodec("UTF-8");
485+
#endif
486+
out.setGenerateByteOrderMark(true); // 添加 UTF-8 BOM
487+
488+
QJsonDocument doc;
489+
QJsonArray list;
490+
auto items = getAllHistory();
491+
foreach(auto it, items) {
492+
QJsonObject title;
493+
title.insert("title", it.title);
494+
title.insert("url", it.url);
495+
title.insert("visit_time", it.visitTime.toString());
496+
list.append(title);
497+
}
498+
doc.setArray(list);
499+
out << doc.toJson();
500+
501+
file.close();
502+
return true;
503+
}
504+
444505
bool CHistoryDatabase::importFromCSV(const QString &filename)
445506
{
446507
QFile file(filename);

Plugins/WebBrowser/History/HistoryDatabase.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class CHistoryDatabase : public CDatabase
4949

5050
bool importFromCSV(const QString& filename);
5151
bool exportToCSV(const QString &filename);
52+
bool importFromJson(const QString& filename);
53+
bool exportToJson(const QString& filename);
5254
private:
5355
QString escapeForCsv(const QString &text);
5456
QString unescapeCsvField(const QString &field);

Plugins/WebBrowser/History/HistoryModel.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,15 @@ bool CHistoryModel::exportToCSV(const QString &filename)
181181
if(!m_pDatabase) return false;
182182
return m_pDatabase->exportToCSV(filename);
183183
}
184+
185+
bool CHistoryModel::importFromJson(const QString &filename)
186+
{
187+
if(!m_pDatabase) return false;
188+
return m_pDatabase->importFromJson(filename);
189+
}
190+
191+
bool CHistoryModel::exportToJson(const QString &filename)
192+
{
193+
if(!m_pDatabase) return false;
194+
return m_pDatabase->exportToJson(filename);
195+
}

Plugins/WebBrowser/History/HistoryModel.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ class CHistoryModel : public QAbstractTableModel
4343

4444
bool importFromCSV(const QString& filename);
4545
bool exportToCSV(const QString &filename);
46-
46+
bool importFromJson(const QString& filename);
47+
bool exportToJson(const QString& filename);
4748
private:
4849
QList<HistoryItem> m_historyItems;
4950
CHistoryDatabase* m_pDatabase;

0 commit comments

Comments
 (0)