Falkon Develop
Cross-platform Qt-based web browser
qmlhistory.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "qmlhistory.h"
19#include "mainapplication.h"
20#include "history.h"
21#include "sqldatabase.h"
22#include "qml/qmlstaticdata.h"
23#include <QQmlEngine>
24
25QmlHistory::QmlHistory(QObject *parent)
26 : QObject(parent)
27{
28 connect(mApp->history(), &History::historyEntryAdded, this, [this](const HistoryEntry &entry){
29 QmlHistoryItem *historyItem = QmlStaticData::instance().getHistoryItem(entry);
30 Q_EMIT visited(historyItem);
31 });
32
33 connect(mApp->history(), &History::historyEntryDeleted, this, [this](const HistoryEntry &entry){
34 QmlHistoryItem *historyItem = QmlStaticData::instance().getHistoryItem(entry);
35 Q_EMIT visitRemoved(historyItem);
36 });
37}
38
39QList<QObject*> QmlHistory::search(const QString &text)
40{
41 QList<QObject*> list;
42 const QList<HistoryEntry> result = mApp->history()->searchHistoryEntry(text);
43 list.reserve(result.size());
44 for (const HistoryEntry &entry : result) {
45 auto item = QmlStaticData::instance().getHistoryItem(entry);
46 list.append(item);
47 }
48 return list;
49}
50
51int QmlHistory::getVisits(const QString &url)
52{
53 HistoryEntry entry = mApp->history()->getHistoryEntry(url);
54 return entry.count;
55}
56
57void QmlHistory::addUrl(const QVariantMap &map)
58{
59 if (!map.contains(QSL("title")) || !map.contains(QSL("url"))) {
60 qWarning() << "Error:" << "wrong arguments passed to" << __FUNCTION__;
61 return;
62 }
63 QString title = map.value(QSL("title")).toString();
64 const QString url = map.value(QSL("url")).toString();
65
66 title = title.isEmpty() ? url : title;
67
68 mApp->history()->addHistoryEntry(QUrl::fromEncoded(url.toUtf8()), title);
69}
70
71void QmlHistory::deleteUrl(const QString &url)
72{
73 mApp->history()->deleteHistoryEntry(url);
74}
75
76void QmlHistory::deleteRange(const QVariantMap &map)
77{
78 if (!map.contains(QSL("startTime")) || !map.contains(QSL("endTime"))) {
79 qWarning() << "Error:" << "wrong arguments passed to" << __FUNCTION__;
80 return;
81 }
82 const qlonglong startTime = map.value(QSL("startTime")).toLongLong();
83 const qlonglong endTime = map.value(QSL("endTime")).toLongLong();
84
85 const QList<int> entries = mApp->history()->indexesFromTimeRange(startTime, endTime);
86 for (int index : entries) {
87 mApp->history()->deleteHistoryEntry(index);
88 }
89}
90
92{
93 mApp->history()->clearHistory();
94}
void historyEntryDeleted(const HistoryEntry &entry)
void historyEntryAdded(const HistoryEntry &entry)
Q_INVOKABLE void deleteAll()
Clears all the history.
Definition: qmlhistory.cpp:91
Q_INVOKABLE void addUrl(const QVariantMap &map)
Add url to the history.
Definition: qmlhistory.cpp:57
Q_INVOKABLE void deleteUrl(const QString &url)
Deletes a url from the history.
Definition: qmlhistory.cpp:71
Q_INVOKABLE void deleteRange(const QVariantMap &map)
Deletes history entries within the given range.
Definition: qmlhistory.cpp:76
QmlHistory(QObject *parent=nullptr)
Definition: qmlhistory.cpp:25
Q_INVOKABLE QList< QObject * > search(const QString &text)
Searches History Entries against a search query.
Definition: qmlhistory.cpp:39
Q_INVOKABLE int getVisits(const QString &url)
Get the visit count of a url.
Definition: qmlhistory.cpp:51
QmlHistoryItem * getHistoryItem(const HistoryEntry &entry)
static QmlStaticData & instance()
#define mApp
#define QSL(x)
Definition: qzcommon.h:40
Definition: history.h:39
int count
Definition: history.h:41