File indexing completed on 2024-12-22 04:41:14

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #include "qmlhistory.h"
0019 #include "mainapplication.h"
0020 #include "history.h"
0021 #include "sqldatabase.h"
0022 #include "qml/qmlstaticdata.h"
0023 #include <QQmlEngine>
0024 
0025 QmlHistory::QmlHistory(QObject *parent)
0026     : QObject(parent)
0027 {
0028     connect(mApp->history(), &History::historyEntryAdded, this, [this](const HistoryEntry &entry){
0029         QmlHistoryItem *historyItem = QmlStaticData::instance().getHistoryItem(entry);
0030         Q_EMIT visited(historyItem);
0031     });
0032 
0033     connect(mApp->history(), &History::historyEntryDeleted, this, [this](const HistoryEntry &entry){
0034         QmlHistoryItem *historyItem = QmlStaticData::instance().getHistoryItem(entry);
0035         Q_EMIT visitRemoved(historyItem);
0036     });
0037 }
0038 
0039 QList<QObject*> QmlHistory::search(const QString &text)
0040 {
0041     QList<QObject*> list;
0042     const QList<HistoryEntry> result = mApp->history()->searchHistoryEntry(text);
0043     list.reserve(result.size());
0044     for (const HistoryEntry &entry : result) {
0045         auto item = QmlStaticData::instance().getHistoryItem(entry);
0046         list.append(item);
0047     }
0048     return list;
0049 }
0050 
0051 int QmlHistory::getVisits(const QString &url)
0052 {
0053     HistoryEntry entry = mApp->history()->getHistoryEntry(url);
0054     return entry.count;
0055 }
0056 
0057 void QmlHistory::addUrl(const QVariantMap &map)
0058 {
0059     if (!map.contains(QSL("title")) || !map.contains(QSL("url"))) {
0060         qWarning() << "Error:" << "wrong arguments passed to" << __FUNCTION__;
0061         return;
0062     }
0063     QString title = map.value(QSL("title")).toString();
0064     const QString url = map.value(QSL("url")).toString();
0065 
0066     title = title.isEmpty() ? url : title;
0067 
0068     mApp->history()->addHistoryEntry(QUrl::fromEncoded(url.toUtf8()), title);
0069 }
0070 
0071 void QmlHistory::deleteUrl(const QString &url)
0072 {
0073     mApp->history()->deleteHistoryEntry(url);
0074 }
0075 
0076 void QmlHistory::deleteRange(const QVariantMap &map)
0077 {
0078     if (!map.contains(QSL("startTime")) || !map.contains(QSL("endTime"))) {
0079         qWarning() << "Error:" << "wrong arguments passed to" << __FUNCTION__;
0080         return;
0081     }
0082     const qlonglong startTime = map.value(QSL("startTime")).toLongLong();
0083     const qlonglong endTime = map.value(QSL("endTime")).toLongLong();
0084 
0085     const QList<int> entries = mApp->history()->indexesFromTimeRange(startTime, endTime);
0086     for (int index : entries) {
0087         mApp->history()->deleteHistoryEntry(index);
0088     }
0089 }
0090 
0091 void QmlHistory::deleteAll()
0092 {
0093     mApp->history()->clearHistory();
0094 }