玉兔远程控制 0.1.0-bate6
载入中...
搜索中...
未找到
HistoryModel.cpp
1// Author: Kang Lin <kl222@126.com>
2
3#include <QLoggingCategory>
4#include <QLocale>
5#include "HistoryModel.h"
6
7static Q_LOGGING_CATEGORY(log, "WebBrowser.History.Model")
9 : QAbstractTableModel(parent)
10 , m_pPara(pPara)
11{
12 qDebug(log) << Q_FUNC_INFO;
13 // Call refresh() by user
14 // if (m_pDatabase && m_pDatabase->isOpen()) {
15 // refresh();
16 // }
17 m_pDatabase = CHistoryDatabase::Instance();
18}
19
20CHistoryModel::~CHistoryModel()
21{
22 qDebug(log) << Q_FUNC_INFO;
23}
24
25QVariant CHistoryModel::headerData(int section, Qt::Orientation orientation, int role) const
26{
27 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
28 switch (section) {
29 case ColumnTitle: return tr("Title");
30 case ColumnUrl: return tr("Url");
31 case ColumnVisitTime: return tr("Visit Time");
32 default:
33 break;
34 }
35 }
36
37 return QVariant();
38}
39
40int CHistoryModel::rowCount(const QModelIndex &parent) const
41{
42 Q_UNUSED(parent);
43 return m_historyItems.count();
44}
45
46int CHistoryModel::columnCount(const QModelIndex &parent) const
47{
48 Q_UNUSED(parent);
49 return ColumnCount;
50}
51
52QVariant CHistoryModel::data(const QModelIndex &index, int role) const
53{
54
55 if (!index.isValid() || index.row() >= m_historyItems.count())
56 return QVariant();
57
58 if(!m_pDatabase) {
59 qCritical(log) << "The m_pDatabase is nullptr";
60 return QVariant();
61 }
62
63 const HistoryItem &item = m_historyItems.at(index.row());
64
65 switch (role) {
66 case Qt::DisplayRole:
67 switch (index.column()) {
68 case ColumnTitle:
69 return item.title.isEmpty() ? tr("Untitled") : item.title;
70 case ColumnUrl:
71 return item.url;
72 case ColumnVisitTime:
73 return item.visitTime.toString(QLocale::system().dateFormat());
74 }
75 break;
76
77 case Qt::DecorationRole:
78 if (index.column() == ColumnTitle) {
79 return item.icon;
80 }
81 break;
82
83 case Qt::ToolTipRole:
84 return QString(tr("Title: %1\nUrl: %2\nVisit Time: %3")
85 .arg(item.title)
86 .arg(item.url)
87 .arg(item.visitTime.toString(QLocale::system().dateFormat())));
88
89 case Qt::UserRole:
90 return item.url; // 返回URL用于导航
91 }
92
93 return QVariant();
94}
95
96void CHistoryModel::refresh()
97{
98 if(!m_pDatabase) {
99 qCritical(log) << "The m_pDatabase is nullptr";
100 return;
101 }
102 beginResetModel();
103 int nLimit = 100;
104 if(m_pPara)
105 nLimit = m_pPara->GetDatabaseViewLimit();
106 m_historyItems = m_pDatabase->getAllHistory(nLimit); // 最多500条
107 endResetModel();
108}
109
110void CHistoryModel::refresh(const QDate &start, const QDate &end)
111{
112 if(!m_pDatabase) {
113 qCritical(log) << "The m_pDatabase is nullptr";
114 return;
115 }
116 beginResetModel();
117 int nLimit = 100;
118 if(m_pPara)
119 nLimit = m_pPara->GetDatabaseViewLimit();
120 m_historyItems = m_pDatabase->getHistoryByDate(start, end, nLimit); // 最多500条
121 endResetModel();
122}
123
124bool CHistoryModel::removeRows(int row, int count, const QModelIndex &parent)
125{
126 if (row < 0 || row + count > m_historyItems.count())
127 return false;
128
129 if(!m_pDatabase) {
130 qCritical(log) << "The m_pDatabase is nullptr";
131 return false;
132 }
133
134 beginRemoveRows(parent, row, row + count - 1);
135
136 for (int i = row + count - 1; i >= row; --i) {
137 m_pDatabase->deleteHistoryEntry(m_historyItems.at(i).id);
138 m_historyItems.removeAt(i);
139 }
140
141 endRemoveRows();
142 return true;
143}
144
145HistoryItem CHistoryModel::getItem(const QModelIndex &index) const
146{
147 if (index.isValid() && index.row() < m_historyItems.count()) {
148 return m_historyItems.at(index.row());
149 }
150 return HistoryItem();
151}
152
153bool CHistoryModel::removeDomainItems(const QString &szDomain)
154{
155 if(m_pDatabase) {
156 m_pDatabase->deleteDomainEntries(szDomain);
157 refresh();
158 return true;
159 }
160 return false;
161}
162
163bool CHistoryModel::removeItems(const QString &url)
164{
165 if(m_pDatabase) {
166 m_pDatabase->deleteHistoryEntry(url);
167 refresh();
168 return true;
169 }
170 return false;
171}
172
173bool CHistoryModel::importFromCSV(const QString &filename)
174{
175 if(!m_pDatabase) return false;
176 return m_pDatabase->importFromCSV(filename);
177}
178
179bool CHistoryModel::exportToCSV(const QString &filename)
180{
181 if(!m_pDatabase) return false;
182 return m_pDatabase->exportToCSV(filename);
183}
184
185bool CHistoryModel::importFromJson(const QString &filename)
186{
187 if(!m_pDatabase) return false;
188 return m_pDatabase->importFromJson(filename);
189}
190
191bool CHistoryModel::exportToJson(const QString &filename)
192{
193 if(!m_pDatabase) return false;
194 return m_pDatabase->exportToJson(filename);
195}