diff --git a/src/app/DocumentsPage.qml b/src/app/DocumentsPage.qml index 20fd0e1..7a1827e 100644 --- a/src/app/DocumentsPage.qml +++ b/src/app/DocumentsPage.qml @@ -1,111 +1,111 @@ /* Copyright (C) 2019 Volker Krause This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. 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 GNU Library General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ import QtQuick 2.5 import QtQuick.Layouts 1.1 import QtQuick.Controls 2.1 as QQC2 import org.kde.kirigami 2.5 as Kirigami import org.kde.kitinerary 1.0 import org.kde.itinerary 1.0 import "." as App Kirigami.ScrollablePage { id: page title: i18n("Documents") property var controller: null DocumentsModel { id: docsModel reservationManager: _reservationManager batchId: controller.batchId documentManager: DocumentManager } Component { id: documentDelegate Kirigami.SwipeListItem { RowLayout { Kirigami.Icon { source: model.decoration width: height height: Kirigami.Units.iconSizes.small } QQC2.Label { text: model.display } } actions: [ Kirigami.Action { iconName: "document-open" text: i18n("Open Document") - onTriggered: Qt.openUrlExternally(model.filePath); + onTriggered: ApplicationController.openDocument(model.filePath); }, Kirigami.Action { iconName: "edit-delete" text: i18n("Delete Document") onTriggered: { deleteWarningSheet.docId = model.id; deleteWarningSheet.sheetOpen = true; } } ] } } Kirigami.OverlaySheet { id: deleteWarningSheet property string docId QQC2.Label { text: i18n("Do you really want to delete this document?") wrapMode: Text.WordWrap } footer: RowLayout { QQC2.Button { Layout.alignment: Qt.AlignHCenter text: i18n("Delete") icon.name: "edit-delete" onClicked: { ApplicationController.removeDocument(controller.batchId, deleteWarningSheet.docId); deleteWarningSheet.sheetOpen = false; } } } } actions { contextualActions: [ Kirigami.Action { iconName: "list-add" text: i18n("Add Document...") onTriggered: ApplicationController.addDocument(controller.batchId) } ] } ListView { delegate: documentDelegate model: docsModel QQC2.Label { text: i18n("No documents attached to this reservation.") anchors.centerIn: parent visible: docsModel.empty } } } diff --git a/src/app/applicationcontroller.cpp b/src/app/applicationcontroller.cpp index 37a8878..0d9b12a 100644 --- a/src/app/applicationcontroller.cpp +++ b/src/app/applicationcontroller.cpp @@ -1,490 +1,496 @@ /* Copyright (C) 2018 Volker Krause This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. 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 GNU Library General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include "applicationcontroller.h" #include "documentmanager.h" #include "logging.h" #include "pkpassmanager.h" #include "reservationmanager.h" #include #include #include #include #include #include #include #include +#include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef Q_OS_ANDROID #include #include #include #include #include #else #include #endif #include using namespace KItinerary; #ifdef Q_OS_ANDROID #define PERMISSION_CALENDAR QStringLiteral("android.permission.READ_CALENDAR") static void importReservation(JNIEnv *env, jobject that, jstring data) { Q_UNUSED(that); const char *str = env->GetStringUTFChars(data, nullptr); ApplicationController::instance()->importData(str); env->ReleaseStringUTFChars(data, str); } static void importDavDroidJson(JNIEnv *env, jobject that, jstring data) { Q_UNUSED(that); const char *str = env->GetStringUTFChars(data, nullptr); const auto doc = QJsonDocument::fromJson(str); env->ReleaseStringUTFChars(data, str); const auto array = doc.array(); if (array.size() < 2 || array.at(0).toString() != QLatin1String("X-KDE-KITINERARY-RESERVATION")) { return; } ApplicationController::instance()->importData(array.at(1).toString().toUtf8()); } static void importFromIntent(JNIEnv *env, jobject that, jobject data) { Q_UNUSED(that) Q_UNUSED(env) KAndroidExtras::Intent intent(data); ApplicationController::instance()->importFromUrl(intent.getData()); } static const JNINativeMethod methods[] = { {"importReservation", "(Ljava/lang/String;)V", (void*)importReservation}, {"importFromIntent", "(Landroid/content/Intent;)V", (void*)importFromIntent}, {"importDavDroidJson", "(Ljava/lang/String;)V", (void*)importDavDroidJson} }; Q_DECL_EXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void*) { static bool initialized = false; if (initialized) return JNI_VERSION_1_6; initialized = true; JNIEnv *env = nullptr; if (vm->GetEnv((void**)&env, JNI_VERSION_1_4) != JNI_OK) { qCWarning(Log) << "Failed to get JNI environment."; return -1; } jclass cls = env->FindClass("org/kde/itinerary/Activity"); if (env->RegisterNatives(cls, methods, sizeof(methods) / sizeof(JNINativeMethod)) < 0) { qCWarning(Log) << "Failed to register native functions."; return -1; } return JNI_VERSION_1_4; } #endif ApplicationController* ApplicationController::s_instance = nullptr; ApplicationController::ApplicationController(QObject* parent) : QObject(parent) { s_instance = this; connect(QGuiApplication::clipboard(), &QClipboard::dataChanged, this, &ApplicationController::clipboardContentChanged); } ApplicationController::~ApplicationController() { s_instance = nullptr; } ApplicationController* ApplicationController::instance() { return s_instance; } void ApplicationController::setReservationManager(ReservationManager* resMgr) { m_resMgr = resMgr; } void ApplicationController::setPkPassManager(PkPassManager* pkPassMgr) { m_pkPassMgr = pkPassMgr; } void ApplicationController::setDocumentManager(DocumentManager* docMgr) { m_docMgr = docMgr; } void ApplicationController::showImportFileDialog() { #ifdef Q_OS_ANDROID using namespace KAndroidExtras; Intent intent; intent.setAction(Intent::ACTION_OPEN_DOCUMENT()); intent.addCategory(Intent::CATEGORY_OPENABLE()); intent.setType(QStringLiteral("*/*")); QtAndroid::startActivity(intent, 0, [this](int, int, const QAndroidJniObject &intent) { importFromUrl(KAndroidExtras::Intent(intent).getData()); }); #else const auto url = QFileDialog::getOpenFileUrl(nullptr, i18n("Import Reservation"), QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)), i18n("All Files (*.*);;PkPass files (*.pkpass);;PDF files (*.pdf);;KDE Itinerary files (*.itinerary)")); if (url.isValid()) { importFromUrl(url); } #endif } void ApplicationController::importFromClipboard() { if (QGuiApplication::clipboard()->mimeData()->hasUrls()) { const auto urls = QGuiApplication::clipboard()->mimeData()->urls(); for (const auto &url : urls) importFromUrl(url); return; } if (QGuiApplication::clipboard()->mimeData()->hasText()) { const auto content = QGuiApplication::clipboard()->mimeData()->data(QLatin1String("text/plain")); importData(content); } } void ApplicationController::importFromUrl(const QUrl &url) { qCDebug(Log) << url; if (url.isLocalFile() || url.scheme() == QLatin1String("content")) { importLocalFile(url); return; } if (url.scheme().startsWith(QLatin1String("http"))) { if (!m_nam ) { m_nam = new QNetworkAccessManager(this); } auto reqUrl(url); reqUrl.setScheme(QLatin1String("https")); QNetworkRequest req(reqUrl); req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy); auto reply = m_nam->get(req); connect(reply, &QNetworkReply::finished, this, [this, reply]() { if (reply->error() != QNetworkReply::NoError) { qCDebug(Log) << reply->url() << reply->errorString(); return; } importData(reply->readAll()); }); return; } qCDebug(Log) << "Unhandled URL type:" << url; } void ApplicationController::importLocalFile(const QUrl &url) { qCDebug(Log) << url; if (url.isEmpty()) { return; } QFile f(url.isLocalFile() ? url.toLocalFile() : url.toString()); if (!f.open(QFile::ReadOnly)) { qCWarning(Log) << "Failed to open" << f.fileName() << f.errorString(); return; } if (f.size() > 4000000) { qCWarning(Log) << "File too large, ignoring" << f.fileName() << f.size(); return; } const auto head = f.peek(4); if (url.fileName().endsWith(QLatin1String(".pkpass"), Qt::CaseInsensitive)) { m_pkPassMgr->importPass(url); } else if (url.fileName().endsWith(QLatin1String(".itinerary"), Qt::CaseInsensitive)) { importBundle(url); } else if (strncmp(head.constData(), "PK\x03\x04", 4) == 0) { if (m_pkPassMgr->importPass(url).isEmpty()) { importBundle(url); } } else { const auto data = f.readAll(); const auto resIds = m_resMgr->importReservation(data, f.fileName()); if (resIds.empty()) { return; } // check if there is a document we want to attach here QMimeDatabase db; const auto mt = db.mimeTypeForFileNameAndData(f.fileName(), data); if (mt.name() != QLatin1String("application/pdf")) { // TODO support more file types (however we certainly want to exclude pkpass and json here) return; } DigitalDocument docInfo; docInfo.setName(f.fileName()); docInfo.setEncodingFormat(mt.name()); const auto docId = DocumentUtil::idForContent(data); m_docMgr->addDocument(docId, docInfo, data); for (const auto &resId : resIds) { auto res = m_resMgr->reservation(resId); if (DocumentUtil::addDocumentId(res, docId)) { m_resMgr->updateReservation(resId, res); } } } } void ApplicationController::importData(const QByteArray &data) { qCDebug(Log); if (data.size() < 4) { return; } if (strncmp(data.constData(), "PK\x03\x04", 4) == 0) { if (m_pkPassMgr->importPassFromData(data).isEmpty()) { importBundle(data); } } else { m_resMgr->importReservation(data); } } void ApplicationController::checkCalendar() { #ifdef Q_OS_ANDROID if (QtAndroid::checkPermission(PERMISSION_CALENDAR) == QtAndroid::PermissionResult::Granted) { const auto activity = QtAndroid::androidActivity(); if (activity.isValid()) { activity.callMethod("checkCalendar"); } } else { QtAndroid::requestPermissions({PERMISSION_CALENDAR}, [this] (const QtAndroid::PermissionResultMap &result){ if (result[PERMISSION_CALENDAR] == QtAndroid::PermissionResult::Granted) { checkCalendar(); } }); } #endif } bool ApplicationController::hasClipboardContent() const { return QGuiApplication::clipboard()->mimeData()->hasText() || QGuiApplication::clipboard()->mimeData()->hasUrls(); } void ApplicationController::exportData() { qCDebug(Log); #ifdef Q_OS_ANDROID using namespace KAndroidExtras; Intent intent; intent.setAction(Intent::ACTION_CREATE_DOCUMENT()); intent.addCategory(Intent::CATEGORY_OPENABLE()); intent.setType(QStringLiteral("*/*")); QtAndroid::startActivity(intent, 0, [this](int, int, const QAndroidJniObject &jniIntent) { Intent intent(jniIntent); exportToFile(intent.getData().toString()); }); #else const auto filePath = QFileDialog::getSaveFileName(nullptr, i18n("Export Itinerary Data"), QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation), i18n("KDE Itinerary files (*.itinerary)")); exportToFile(filePath); #endif } void ApplicationController::exportToFile(const QString &filePath) { qCDebug(Log) << filePath; if (filePath.isEmpty()) { return; } File f(filePath); if (!f.open(File::Write)) { qCWarning(Log) << f.errorString(); // TODO show error in UI return; } // export reservation data for (const auto &batchId : m_resMgr->batches()) { const auto resIds = m_resMgr->reservationsForBatch(batchId); for (const auto &resId : resIds) { f.addReservation(resId, m_resMgr->reservation(resId)); } } // export passes const auto passIds = m_pkPassMgr->passes(); for (const auto &passId : passIds) { f.addPass(passId, m_pkPassMgr->rawData(passId)); } // export documents const auto docIds = m_docMgr->documents(); for (const auto &docId : docIds) { const auto fileName = m_docMgr->documentFilePath(docId); QFile file(fileName); if (!file.open(QFile::ReadOnly)) { qCWarning(Log) << "failed to open" << fileName << "for exporting" << file.errorString(); continue; } f.addDocument(docId, m_docMgr->documentInfo(docId), file.readAll()); } // TODO export settings } void ApplicationController::importBundle(const QUrl &url) { KItinerary::File f(url.isLocalFile() ? url.toLocalFile() : url.toString()); if (!f.open(File::Read)) { // TODO show error in the ui qCWarning(Log) << "Failed to open bundle file:" << url << f.errorString(); return; } importBundle(&f); } void ApplicationController::importBundle(const QByteArray &data) { QBuffer buffer; buffer.setData(data); buffer.open(QBuffer::ReadOnly); KItinerary::File f(&buffer); if (!f.open(File::Read)) { // TODO show error in the ui qCWarning(Log) << "Failed to open bundle data:" << f.errorString(); return; } importBundle(&f); } void ApplicationController::importBundle(KItinerary::File *file) { const auto resIds = file->reservations(); for (const auto &resId : resIds) { m_resMgr->addReservation(file->reservation(resId)); } const auto passIds = file->passes(); for (const auto &passId : passIds) { m_pkPassMgr->importPassFromData(file->passData(passId)); } const auto docIds = file->documents(); for (const auto &docId : docIds) { m_docMgr->addDocument(docId, file->documentInfo(docId), file->documentData(docId)); } } void ApplicationController::addDocument(const QString &batchId) { // TODO Android support #ifndef Q_OS_ANDROID const auto url = QFileDialog::getOpenFileUrl(nullptr, i18n("Add Document"), QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)), i18n("All Files (*.*)")); addDocument(batchId, url); #else using namespace KAndroidExtras; Intent intent; intent.setAction(Intent::ACTION_OPEN_DOCUMENT()); intent.addCategory(Intent::CATEGORY_OPENABLE()); intent.setType(QStringLiteral("*/*")); QtAndroid::startActivity(intent, 0, [this, batchId](int, int, const QAndroidJniObject &jniIntent) { Intent intent(jniIntent); addDocument(batchId, intent.getData()); }); #endif } void ApplicationController::addDocument(const QString &batchId, const QUrl &url) { if (!url.isValid()) { return; } const auto docId = QUuid::createUuid().toString(); auto res = m_resMgr->reservation(batchId); DocumentUtil::addDocumentId(res, docId); DigitalDocument docInfo; #ifdef Q_OS_ANDROID docInfo.setEncodingFormat(KAndroidExtras::ContentResolver::mimeType(url)); docInfo.setName(KAndroidExtras::ContentResolver::fileName(url)); #else QMimeDatabase db; docInfo.setEncodingFormat(db.mimeTypeForFile(url.isLocalFile() ? url.toLocalFile() : url.toString()).name()); docInfo.setName(url.fileName()); #endif m_docMgr->addDocument(docId, docInfo, url.isLocalFile() ? url.toLocalFile() : url.toString()); const auto resIds = m_resMgr->reservationsForBatch(batchId); for (const auto &resId : resIds) { m_resMgr->updateReservation(resId, res); } } void ApplicationController::removeDocument(const QString &batchId, const QString &docId) { const auto resIds = m_resMgr->reservationsForBatch(batchId); for (const auto &resId : resIds) { auto res = m_resMgr->reservation(batchId); if (DocumentUtil::removeDocumentId(res, docId)) { m_resMgr->updateReservation(resId, res); } } m_docMgr->removeDocument(docId); } + +void ApplicationController::openDocument(const QUrl &url) +{ + QDesktopServices::openUrl(url); +} diff --git a/src/app/applicationcontroller.h b/src/app/applicationcontroller.h index 501b38b..d06b750 100644 --- a/src/app/applicationcontroller.h +++ b/src/app/applicationcontroller.h @@ -1,88 +1,89 @@ /* Copyright (C) 2018 Volker Krause This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. 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 GNU Library General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #ifndef APPLICATIONCONTROLLER_H #define APPLICATIONCONTROLLER_H #include #ifdef Q_OS_ANDROID #include #endif class DocumentManager; class PkPassManager; class ReservationManager; namespace KItinerary { class File; } class QNetworkAccessManager; class ApplicationController : public QObject { Q_OBJECT Q_PROPERTY(bool hasClipboardContent READ hasClipboardContent NOTIFY clipboardContentChanged) public: explicit ApplicationController(QObject *parent = nullptr); ~ApplicationController(); void setReservationManager(ReservationManager *resMgr); void setPkPassManager(PkPassManager *pkPassMgr); void setDocumentManager(DocumentManager *docMgr); // data import Q_INVOKABLE void showImportFileDialog(); Q_INVOKABLE void importFromClipboard(); Q_INVOKABLE void importFromUrl(const QUrl &url); void importData(const QByteArray &data); Q_INVOKABLE void checkCalendar(); static ApplicationController* instance(); bool hasClipboardContent() const; void importBundle(const QUrl &url); void importBundle(const QByteArray &data); // data export Q_INVOKABLE void exportData(); void exportToFile(const QString &filePath); // document attaching Q_INVOKABLE void addDocument(const QString &batchId); Q_INVOKABLE void removeDocument(const QString &batchId, const QString &docId); + Q_INVOKABLE void openDocument(const QUrl &url); Q_SIGNALS: void clipboardContentChanged(); private: void importLocalFile(const QUrl &url); void importBundle(KItinerary::File *file); void addDocument(const QString &batchId, const QUrl &url); static ApplicationController *s_instance; ReservationManager *m_resMgr = nullptr; PkPassManager *m_pkPassMgr = nullptr; DocumentManager *m_docMgr = nullptr; QNetworkAccessManager *m_nam = nullptr; }; #endif // APPLICATIONCONTROLLER_H