2012-05-14 18:37:22 +00:00
|
|
|
/*
|
2012-05-14 19:16:56 +00:00
|
|
|
* Copyright 2012 by Marco Martin <mart@kde.org>
|
2012-05-14 18:37:22 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
2012-10-03 09:32:42 +00:00
|
|
|
* it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2,
|
|
|
|
* or (at your option) any later version.
|
2012-05-14 18:37:22 +00:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2012-10-03 09:32:42 +00:00
|
|
|
* GNU General Public License for more details
|
2012-05-14 18:37:22 +00:00
|
|
|
*
|
2012-10-03 09:32:42 +00:00
|
|
|
* You should have received a copy of the GNU General Public
|
2012-05-14 18:37:22 +00:00
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "documentitem.h"
|
|
|
|
|
2018-09-01 08:25:57 +00:00
|
|
|
#include <QtQml> // krazy:exclude=includes
|
2014-12-02 19:41:16 +00:00
|
|
|
#include <QMimeDatabase>
|
2012-07-31 14:28:54 +00:00
|
|
|
|
2019-10-25 15:06:59 +00:00
|
|
|
#ifdef Q_OS_ANDROID
|
|
|
|
#include <QAndroidJniObject>
|
|
|
|
#include <QtAndroid>
|
|
|
|
#endif
|
|
|
|
|
2013-02-24 21:58:53 +00:00
|
|
|
#include <core/document_p.h>
|
2012-05-15 15:23:15 +00:00
|
|
|
#include <core/page.h>
|
2012-08-01 16:00:56 +00:00
|
|
|
#include <core/bookmarkmanager.h>
|
2012-05-14 18:37:22 +00:00
|
|
|
|
2012-10-03 12:37:54 +00:00
|
|
|
#include "ui/tocmodel.h"
|
2012-05-14 18:37:22 +00:00
|
|
|
|
|
|
|
DocumentItem::DocumentItem(QObject *parent)
|
2012-05-15 10:12:56 +00:00
|
|
|
: QObject(parent),
|
2017-09-05 21:27:18 +00:00
|
|
|
m_thumbnailObserver(nullptr),
|
|
|
|
m_pageviewObserver(nullptr),
|
2012-05-15 10:12:56 +00:00
|
|
|
m_searchInProgress(false)
|
2012-05-14 18:37:22 +00:00
|
|
|
{
|
2015-10-29 12:37:11 +00:00
|
|
|
qmlRegisterUncreatableType<TOCModel>("org.kde.okular", 1, 0, "TOCModel", QStringLiteral("Do not create objects of this type."));
|
|
|
|
Okular::Settings::instance(QStringLiteral("okularproviderrc"));
|
2017-09-05 21:27:18 +00:00
|
|
|
m_document = new Okular::Document(nullptr);
|
2012-07-31 14:28:54 +00:00
|
|
|
m_tocModel = new TOCModel(m_document, this);
|
|
|
|
|
2015-03-17 08:08:04 +00:00
|
|
|
connect(m_document, &Okular::Document::searchFinished,
|
|
|
|
this, &DocumentItem::searchFinished);
|
2015-10-29 12:37:11 +00:00
|
|
|
connect(m_document->bookmarkManager(), &Okular::BookmarkManager::bookmarksChanged,
|
|
|
|
this, &DocumentItem::bookmarkedPagesChanged);
|
|
|
|
connect(m_document->bookmarkManager(), &Okular::BookmarkManager::bookmarksChanged,
|
|
|
|
this, &DocumentItem::bookmarksChanged);
|
2012-05-14 18:37:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DocumentItem::~DocumentItem()
|
|
|
|
{
|
|
|
|
delete m_document;
|
|
|
|
}
|
|
|
|
|
2018-05-09 00:45:12 +00:00
|
|
|
void DocumentItem::setUrl(const QUrl & url)
|
2012-05-14 18:37:22 +00:00
|
|
|
{
|
2018-05-14 23:42:02 +00:00
|
|
|
m_document->closeDocument();
|
2014-05-09 17:56:16 +00:00
|
|
|
//TODO: password
|
2014-12-02 19:41:16 +00:00
|
|
|
QMimeDatabase db;
|
2018-05-09 00:45:12 +00:00
|
|
|
|
2019-10-25 15:06:59 +00:00
|
|
|
QUrl realUrl = url;
|
|
|
|
|
|
|
|
#ifdef Q_OS_ANDROID
|
|
|
|
realUrl = QUrl(QtAndroid::androidActivity().callObjectMethod("contentUrlToFd",
|
|
|
|
"(Ljava/lang/String;)Ljava/lang/String;",
|
|
|
|
QAndroidJniObject::fromString(url.toString()).object<jstring>()
|
|
|
|
).toString());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
const QString path = realUrl.isLocalFile() ? realUrl.toLocalFile() : QStringLiteral("-");
|
2018-05-09 00:45:12 +00:00
|
|
|
|
2019-10-25 15:06:59 +00:00
|
|
|
m_document->openDocument(path, realUrl, db.mimeTypeForUrl(realUrl));
|
2012-05-14 18:37:22 +00:00
|
|
|
|
2015-09-23 12:34:12 +00:00
|
|
|
m_tocModel->clear();
|
2012-07-31 14:28:54 +00:00
|
|
|
m_tocModel->fill(m_document->documentSynopsis());
|
2012-07-31 19:33:22 +00:00
|
|
|
m_tocModel->setCurrentViewport(m_document->viewport());
|
2015-09-23 14:14:49 +00:00
|
|
|
|
2012-05-15 10:12:56 +00:00
|
|
|
m_matchingPages.clear();
|
|
|
|
for (uint i = 0; i < m_document->pages(); ++i) {
|
|
|
|
m_matchingPages << (int)i;
|
|
|
|
}
|
|
|
|
emit matchingPagesChanged();
|
2018-05-09 00:45:12 +00:00
|
|
|
emit urlChanged();
|
2012-05-14 18:37:22 +00:00
|
|
|
emit pageCountChanged();
|
|
|
|
emit openedChanged();
|
2012-05-15 11:56:08 +00:00
|
|
|
emit supportsSearchingChanged();
|
2012-10-03 10:06:02 +00:00
|
|
|
emit windowTitleForDocumentChanged();
|
2015-09-18 16:22:28 +00:00
|
|
|
emit bookmarkedPagesChanged();
|
2012-10-03 10:06:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString DocumentItem::windowTitleForDocument() const
|
|
|
|
{
|
|
|
|
// If 'DocumentTitle' should be used, check if the document has one. If
|
|
|
|
// either case is false, use the file name.
|
2015-01-29 19:55:57 +00:00
|
|
|
QString title = Okular::Settings::displayDocumentNameOrPath() == Okular::Settings::EnumDisplayDocumentNameOrPath::Path ?
|
|
|
|
m_document->currentDocument().toDisplayString(QUrl::PreferLocalFile) : m_document->currentDocument().fileName();
|
2012-10-03 10:06:02 +00:00
|
|
|
|
|
|
|
if (Okular::Settings::displayDocumentTitle()) {
|
2015-10-29 12:37:11 +00:00
|
|
|
const QString docTitle = m_document->metaData( QStringLiteral("DocumentTitle") ).toString();
|
2012-10-03 10:06:02 +00:00
|
|
|
|
|
|
|
if (!docTitle.isEmpty() && !docTitle.trimmed().isEmpty()) {
|
|
|
|
title = docTitle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return title;
|
2012-05-14 18:37:22 +00:00
|
|
|
}
|
|
|
|
|
2018-05-09 00:45:12 +00:00
|
|
|
QUrl DocumentItem::url() const
|
2012-05-14 18:37:22 +00:00
|
|
|
{
|
2018-05-09 00:45:12 +00:00
|
|
|
return m_document->currentDocument();
|
2012-05-14 18:37:22 +00:00
|
|
|
}
|
|
|
|
|
2012-05-15 11:50:16 +00:00
|
|
|
void DocumentItem::setCurrentPage(int page)
|
|
|
|
{
|
|
|
|
m_document->setViewportPage(page);
|
2012-07-31 19:33:22 +00:00
|
|
|
m_tocModel->setCurrentViewport(m_document->viewport());
|
2012-05-15 11:50:16 +00:00
|
|
|
emit currentPageChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
int DocumentItem::currentPage() const
|
|
|
|
{
|
|
|
|
return m_document->currentPage();
|
|
|
|
}
|
|
|
|
|
2012-05-14 18:37:22 +00:00
|
|
|
bool DocumentItem::isOpened() const
|
|
|
|
{
|
|
|
|
return m_document->isOpened();
|
|
|
|
}
|
|
|
|
|
|
|
|
int DocumentItem::pageCount() const
|
|
|
|
{
|
|
|
|
return m_document->pages();
|
|
|
|
}
|
|
|
|
|
2014-12-17 18:16:25 +00:00
|
|
|
QVariantList DocumentItem::matchingPages() const
|
2012-05-15 10:12:56 +00:00
|
|
|
{
|
|
|
|
return m_matchingPages;
|
|
|
|
}
|
|
|
|
|
2012-07-31 14:28:54 +00:00
|
|
|
TOCModel *DocumentItem::tableOfContents() const
|
|
|
|
{
|
|
|
|
return m_tocModel;
|
|
|
|
}
|
|
|
|
|
2015-09-18 16:22:28 +00:00
|
|
|
QVariantList DocumentItem::bookmarkedPages() const
|
2012-08-01 16:00:56 +00:00
|
|
|
{
|
|
|
|
QList<int> list;
|
2012-10-05 13:59:08 +00:00
|
|
|
QSet<int> pages;
|
2012-10-02 11:25:34 +00:00
|
|
|
foreach (const KBookmark &bookmark, m_document->bookmarkManager()->bookmarks()) {
|
2014-12-02 19:41:16 +00:00
|
|
|
Okular::DocumentViewport viewport(bookmark.url().fragment());
|
2012-10-05 14:33:08 +00:00
|
|
|
pages << viewport.pageNumber;
|
2012-08-01 16:00:56 +00:00
|
|
|
}
|
2012-10-05 13:59:08 +00:00
|
|
|
list = pages.toList();
|
2019-03-12 12:13:53 +00:00
|
|
|
std::sort(list.begin(), list.end());
|
2015-09-18 16:22:28 +00:00
|
|
|
|
|
|
|
QVariantList variantList;
|
|
|
|
foreach (const int page, list) {
|
|
|
|
variantList << page;
|
|
|
|
}
|
|
|
|
return variantList;
|
2012-08-01 16:00:56 +00:00
|
|
|
}
|
|
|
|
|
2012-10-05 14:15:55 +00:00
|
|
|
QStringList DocumentItem::bookmarks() const
|
|
|
|
{
|
|
|
|
QStringList list;
|
|
|
|
foreach(const KBookmark &bookmark, m_document->bookmarkManager()->bookmarks()) {
|
2014-12-02 19:41:16 +00:00
|
|
|
list << bookmark.url().toString();
|
2012-10-05 14:15:55 +00:00
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2012-05-15 11:56:08 +00:00
|
|
|
bool DocumentItem::supportsSearching() const
|
|
|
|
{
|
|
|
|
return m_document->supportsSearching();
|
|
|
|
}
|
|
|
|
|
2012-05-15 10:12:56 +00:00
|
|
|
bool DocumentItem::isSearchInProgress() const
|
|
|
|
{
|
|
|
|
return m_searchInProgress;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentItem::searchText(const QString &text)
|
|
|
|
{
|
|
|
|
if (text.isEmpty()) {
|
|
|
|
resetSearch();
|
|
|
|
return;
|
|
|
|
}
|
2012-05-15 11:56:08 +00:00
|
|
|
m_document->cancelSearch();
|
2012-05-15 10:12:56 +00:00
|
|
|
m_document->resetSearch(PAGEVIEW_SEARCH_ID);
|
|
|
|
m_document->searchText(PAGEVIEW_SEARCH_ID, text, 1, Qt::CaseInsensitive,
|
2014-05-10 09:33:21 +00:00
|
|
|
Okular::Document::AllDocument, true, QColor(100,100,200,40));
|
2012-05-15 10:12:56 +00:00
|
|
|
|
|
|
|
if (!m_searchInProgress) {
|
|
|
|
m_searchInProgress = true;
|
|
|
|
emit searchInProgressChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentItem::resetSearch()
|
|
|
|
{
|
|
|
|
m_document->resetSearch(PAGEVIEW_SEARCH_ID);
|
|
|
|
m_matchingPages.clear();
|
|
|
|
for (uint i = 0; i < m_document->pages(); ++i) {
|
|
|
|
m_matchingPages << (int)i;
|
|
|
|
}
|
|
|
|
if (m_searchInProgress) {
|
|
|
|
m_searchInProgress = false;
|
|
|
|
emit searchInProgressChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
emit matchingPagesChanged();
|
|
|
|
}
|
|
|
|
|
2012-05-14 18:37:22 +00:00
|
|
|
Okular::Document *DocumentItem::document()
|
|
|
|
{
|
|
|
|
return m_document;
|
|
|
|
}
|
|
|
|
|
2013-02-24 21:58:53 +00:00
|
|
|
Observer *DocumentItem::thumbnailObserver()
|
2012-05-14 18:37:22 +00:00
|
|
|
{
|
2013-02-24 21:58:53 +00:00
|
|
|
if (!m_thumbnailObserver)
|
|
|
|
m_thumbnailObserver = new Observer(this);
|
|
|
|
|
|
|
|
return m_thumbnailObserver;
|
|
|
|
}
|
|
|
|
|
|
|
|
Observer *DocumentItem::pageviewObserver()
|
|
|
|
{
|
|
|
|
if (!m_pageviewObserver) {
|
|
|
|
m_pageviewObserver = new Observer(this);
|
2012-05-14 18:37:22 +00:00
|
|
|
}
|
|
|
|
|
2013-02-24 21:58:53 +00:00
|
|
|
return m_pageviewObserver;
|
2012-05-14 18:37:22 +00:00
|
|
|
}
|
|
|
|
|
2012-05-15 10:12:56 +00:00
|
|
|
void DocumentItem::searchFinished(int id, Okular::Document::SearchStatus endStatus)
|
|
|
|
{
|
|
|
|
Q_UNUSED(endStatus)
|
|
|
|
|
|
|
|
if (id != PAGEVIEW_SEARCH_ID) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_matchingPages.clear();
|
|
|
|
for (uint i = 0; i < m_document->pages(); ++i) {
|
|
|
|
if (m_document->page(i)->hasHighlights(id)) {
|
|
|
|
m_matchingPages << (int)i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_searchInProgress) {
|
|
|
|
m_searchInProgress = false;
|
|
|
|
emit searchInProgressChanged();
|
|
|
|
}
|
|
|
|
emit matchingPagesChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-14 18:37:22 +00:00
|
|
|
//Observer
|
|
|
|
|
2013-02-24 21:58:53 +00:00
|
|
|
Observer::Observer(DocumentItem *parent)
|
2012-05-14 18:37:22 +00:00
|
|
|
: QObject(parent),
|
|
|
|
m_document(parent)
|
|
|
|
{
|
|
|
|
parent->document()->addObserver(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
Observer::~Observer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Observer::notifyPageChanged(int page, int flags)
|
|
|
|
{
|
|
|
|
emit pageChanged(page, flags);
|
|
|
|
}
|
|
|
|
|