From f144fa0a1b021713cc34c4840986129f4cb63fda Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Tue, 30 Aug 2022 11:53:02 -0700 Subject: [PATCH] mobile: allow sharing files via email This will allow the user of the mobile app to export dive and dive site data from their mobile device without using the Subsurface cloud. Signed-off-by: Dirk Hohndel --- mobile-widgets/qmlmanager.cpp | 57 +++++++++++++++++++++++++++++++++++ mobile-widgets/qmlmanager.h | 2 +- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/mobile-widgets/qmlmanager.cpp b/mobile-widgets/qmlmanager.cpp index 109160680..c47bec59e 100644 --- a/mobile-widgets/qmlmanager.cpp +++ b/mobile-widgets/qmlmanager.cpp @@ -2234,6 +2234,63 @@ void QMLManager::exportToWEB(export_types type, QString userId, QString password } } +void QMLManager::shareViaEmail(export_types type, bool anonymize) +{ +#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS) + QString fileName = appLogFileName; +#else + QString fileName = system_default_directory(); +#endif + QString body; + switch (type) { + case EX_DIVES_XML: + fileName.replace("subsurface.log", "subsurface.ssrf"); + if (save_dives_logic(qPrintable(fileName), false, anonymize) == 0) { + // ok, we have a file, let's send it + body = "Subsurface dive log data"; + } else { + appendTextToLog("failure to save dive log, aborting attempt to send via email"); + } + break; + case EX_DIVE_SITES_XML: + fileName.replace("subsurface.log", "subsurface_divesites.xml"); + { // need a block so the compiler doesn't complain about creating the sites variable here + std::vector sites = getDiveSitesToExport(false); + if (save_dive_sites_logic(qPrintable(fileName), sites.data(), (int)sites.size(), anonymize) == 0) { + // ok, we have a file, let's send it + body = "Subsurface dive site data"; + } else { + appendTextToLog("failure to save dive site data, aborting attempt to send via email"); + } + } + break; + default: + qDebug() << "cannot export type " << type << " via email"; + return; + } +#if defined(Q_OS_ANDROID) + // let's use our nifty Java shareViaEmail function + QAndroidJniObject activity = QtAndroid::androidActivity(); + if (activity.isValid()) { + QAndroidJniObject attachmentPath = QAndroidJniObject::fromString(fileName); + QAndroidJniObject subject = QAndroidJniObject::fromString("Subsurface export"); + QAndroidJniObject bodyString = QAndroidJniObject::fromString(body); + QAndroidJniObject emptyString = QAndroidJniObject::fromString(""); + bool success = activity.callMethod("shareViaEmail", + "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Z", // five string arguments, return bool + subject.object(), emptyString.object(), bodyString.object(), + attachmentPath.object(), emptyString.object()); + qDebug() << __FUNCTION__ << "shareViaEmail" << (success ? "succeeded" : "failed"); + } +#elif defined(Q_OS_IOS) + // call into objC++ code to share on iOS + QString subject("Subsurface export"); + QString emptyString(""); + iosshare.shareViaEmail(subject, emptyString, body, fileName, emptyString); +#else + appendTextToLog("on a mobile platform this would send" + fileName + "via email with body" + body); +#endif +} void QMLManager::uploadFinishSlot(bool success, const QString &text, const QByteArray &html) { emit uploadFinish(success, text); diff --git a/mobile-widgets/qmlmanager.h b/mobile-widgets/qmlmanager.h index 3776af5b6..c22267121 100644 --- a/mobile-widgets/qmlmanager.h +++ b/mobile-widgets/qmlmanager.h @@ -74,7 +74,7 @@ public: Q_INVOKABLE void exportToFile(export_types type, QString directory, bool anonymize); #endif Q_INVOKABLE void exportToWEB(export_types type, QString userId, QString password, bool anonymize); - + Q_INVOKABLE void shareViaEmail(export_types type, bool anonymize); QString DC_vendor() const; void DC_setVendor(const QString& vendor);