Mobile: Ask for password if the document needs one

This commit is contained in:
Albert Astals Cid 2022-07-20 19:21:35 +02:00
parent c9b338de10
commit 63d7839129
3 changed files with 55 additions and 1 deletions

View File

@ -5,6 +5,7 @@
*/
import QtQuick 2.15
import QtQuick.Controls 2.15 as QQC2
import QtQuick.Dialogs 1.3 as QQD
import org.kde.okular 2.0 as Okular
import org.kde.kirigami 2.17 as Kirigami
@ -64,6 +65,12 @@ Kirigami.ApplicationWindow {
Okular.DocumentItem {
id: documentItem
onUrlChanged: { currentPage = 0 }
onNeedsPasswordChanged: {
if (needsPassword) {
passwordDialog.open();
}
}
}
pageStack.initialPage: MainView {
@ -89,4 +96,20 @@ Kirigami.ApplicationWindow {
}
}
}
QQC2.Dialog {
id: passwordDialog
focus: true
anchors.centerIn: parent
title: i18n("Password Needed")
contentItem: Kirigami.PasswordField {
id: pwdField
onAccepted: passwordDialog.accept();
focus: true
}
standardButtons: QQC2.Dialog.Ok | QQC2.Dialog.Cancel
onAccepted: documentItem.setPassword(pwdField.text);
onRejected: documentItem.url = "";
}
}

View File

@ -50,6 +50,11 @@ DocumentItem::~DocumentItem()
}
void DocumentItem::setUrl(const QUrl &url)
{
openUrl(url, {});
}
void DocumentItem::openUrl(const QUrl &url, const QString &password)
{
m_document->closeDocument();
// TODO: password
@ -63,7 +68,7 @@ void DocumentItem::setUrl(const QUrl &url)
const QString path = realUrl.isLocalFile() ? realUrl.toLocalFile() : QStringLiteral("-");
m_document->openDocument(path, realUrl, db.mimeTypeForUrl(realUrl));
const Okular::Document::OpenResult res = m_document->openDocument(path, realUrl, db.mimeTypeForUrl(realUrl), password);
m_tocModel->clear();
m_tocModel->fill(m_document->documentSynopsis());
@ -73,10 +78,12 @@ void DocumentItem::setUrl(const QUrl &url)
for (uint i = 0; i < m_document->pages(); ++i) {
m_matchingPages << (int)i;
}
m_needsPassword = res == Okular::Document::OpenNeedsPassword;
Q_EMIT matchingPagesChanged();
Q_EMIT urlChanged();
Q_EMIT pageCountChanged();
Q_EMIT openedChanged();
Q_EMIT needsPasswordChanged();
Q_EMIT supportsSearchingChanged();
Q_EMIT windowTitleForDocumentChanged();
Q_EMIT bookmarkedPagesChanged();
@ -224,6 +231,11 @@ void DocumentItem::resetSearch()
Q_EMIT matchingPagesChanged();
}
void DocumentItem::setPassword(const QString &password)
{
openUrl(m_document->currentDocument(), password);
}
Okular::Document *DocumentItem::document()
{
return m_document;

View File

@ -47,6 +47,11 @@ class DocumentItem : public QObject
*/
Q_PROPERTY(bool opened READ isOpened NOTIFY openedChanged)
/**
* True if this DocumentItem instance needs a password to open the document
*/
Q_PROPERTY(bool needsPassword READ needsPassword NOTIFY needsPasswordChanged)
/**
* How many pages there are in the document
*/
@ -101,6 +106,11 @@ public:
bool isOpened() const;
bool needsPassword() const
{
return m_needsPassword;
}
int pageCount() const;
bool supportsSearching() const;
@ -130,6 +140,11 @@ public:
*/
Q_INVOKABLE void resetSearch();
/**
* Tries to reopen the document with the given password.
*/
Q_INVOKABLE void setPassword(const QString &password);
// Internal, not binded to qml
Okular::Document *document();
Observer *pageviewObserver();
@ -139,6 +154,7 @@ Q_SIGNALS:
void urlChanged();
void pageCountChanged();
void openedChanged();
void needsPasswordChanged();
void searchInProgressChanged();
void matchingPagesChanged();
void currentPageChanged();
@ -175,6 +191,8 @@ private Q_SLOTS:
void searchFinished(int id, Okular::Document::SearchStatus endStatus);
private:
void openUrl(const QUrl &url, const QString &password);
Okular::Document *m_document;
TOCModel *m_tocModel;
SignatureModel *m_signaturesModel;
@ -182,6 +200,7 @@ private:
Observer *m_pageviewObserver;
QVariantList m_matchingPages;
bool m_searchInProgress;
bool m_needsPassword = false;
};
class Observer : public QObject, public Okular::DocumentObserver