Commit b06da449 authored by Martin Straka's avatar Martin Straka

Added iCloud objectiveC helper object

parent 6c7bf6c1
...@@ -11,6 +11,7 @@ HEADERS += \ ...@@ -11,6 +11,7 @@ HEADERS += \
ios/src/app_delegate.h \ ios/src/app_delegate.h \
ios/src/doc_view_controller.h \ ios/src/doc_view_controller.h \
ios/src/qt_app_delegate.h \ ios/src/qt_app_delegate.h \
ios/src/icloud_helper.h \
ios/src/ios_file_opener.h \ ios/src/ios_file_opener.h \
ios/src/send_email_controller.h \ ios/src/send_email_controller.h \
ios/src/url_opener.h ios/src/url_opener.h
...@@ -18,6 +19,7 @@ HEADERS += \ ...@@ -18,6 +19,7 @@ HEADERS += \
OBJECTIVE_SOURCES += \ OBJECTIVE_SOURCES += \
ios/src/app_delegate.mm \ ios/src/app_delegate.mm \
ios/src/doc_view_controller.mm \ ios/src/doc_view_controller.mm \
ios/src/icloud_helper.mm \
ios/src/ios_file_opener.mm \ ios/src/ios_file_opener.mm \
ios/src/send_email_controller.mm \ ios/src/send_email_controller.mm \
ios/src/url_opener.mm ios/src/url_opener.mm
......
/*
* Copyright (C) 2014-2018 CZ.NIC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 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 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 <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations including
* the two.
*/
#pragma once
#include <QObject>
#include <QString>
#include <QStringList>
/*!
* @brief Used for iCloud interaction on iOS.
*/
class ICloudHelper : public QObject {
Q_OBJECT
public:
/*!
* @brief Constructor.
*
* @param[in] parent Parent object.
*/
ICloudHelper(QObject *parent = Q_NULLPTR);
/*!
* @brief Check if iCloud is on and active.
*
* @return Return true if success.
*/
bool isCloudOn(void);
/*!
* @brief Store files to iCloud.
*
* @param[in] srcFilePaths List of file paths to be saved into iCloud.
* @param[in] targetPath Target iCloud path where files will store.
*/
void storeFilesToCloud(const QStringList &srcFilePaths,
const QString &targetPath);
private:
/*!
* @brief Return state describing what happened.
*/
enum ICloudResult {
ICLOUD_NOT_ON = 0, /*!< iCloud is not avalilable/turn on. */
ICLOUD_FILE_UPLOAD_SUCCESS, /*!< File upload was successful. */
ICLOUD_FILE_EXISTS, /*!< File exists on iCloud. */
ICLOUD_FILE_UPLOAD_ERROR, /*!< File upload failed. */
ICLOUD_TARGET_SAVE_DIR_ERROR /*!< Target dir is missing or not created. */
};
/*!
* @brief Upload single file into iCloud.
*
* @param[in] srcFilePath Source file path.
* @param[in] destFilePath iCloud target path.
* @return Return operation error/success code.
*/
ICloudResult uploadFileToCloud(const QString &srcFilePath,
const QString &destFilePath);
};
/*
* Copyright (C) 2014-2018 CZ.NIC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 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 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 <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations including
* the two.
*/
#include <QDir>
#include <QFileInfo>
#include <QMessageBox>
#include <QtCore>
#ifdef Q_OS_IOS
#include "ios/src/icloud_helper.h"
#include "ios/src/doc_view_controller.h"
#define ICLOUD_DATOVKA_CONTAINER_NAME "Datovka"
#endif /* Q_OS_IOS */
ICloudHelper::ICloudHelper(QObject *parent)
: QObject(parent)
{
}
bool ICloudHelper::isCloudOn(void)
{
#ifdef Q_OS_IOS
return ([[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil]);
#endif /* Q_OS_IOS */
return false;
}
void ICloudHelper::storeFilesToCloud(const QStringList &srcFilePaths,
const QString &targetPath)
{
#ifdef Q_OS_IOS
/* Is iCloud configured and available? */
if (!isCloudOn()) {
qCritical() << "Unable to access iCloud!";
QMessageBox::critical(Q_NULLPTR, tr("iCloud error"),
tr("Unable to access iCloud Account. Open the Settings app and enter your Apple ID into iCloud settings."),
QMessageBox::Ok);
return;
}
/* Upload files to iCloud */
bool success = true;
QStringList errorUploads;
QString filePath;
foreach (filePath, srcFilePaths) {
ICloudResult retCode = uploadFileToCloud(filePath, targetPath);
QFileInfo fi(filePath);
switch (retCode) {
case ICloudHelper::ICLOUD_NOT_ON:
success &= false;
errorUploads.append(tr("Unable to access iCloud!"));
goto finish;
break;
case ICloudHelper::ICLOUD_TARGET_SAVE_DIR_ERROR:
success &= false;
errorUploads.append(tr("Cannot create subdir '%1' in iCloud.").arg(targetPath));
goto finish;
break;
case ICloudHelper::ICLOUD_FILE_EXISTS:
success &= false;
errorUploads.append(tr("File '%1' already exists in iCloud.").arg(fi.fileName()));
break;
case ICloudHelper::ICLOUD_FILE_UPLOAD_ERROR:
success &= false;
errorUploads.append(tr("File '%1' upload failed.").arg(fi.fileName()));
break;
case ICloudHelper::ICLOUD_FILE_UPLOAD_SUCCESS:
errorUploads.append(tr("File '%1' has been uploaded in iCloud.").arg(fi.fileName()));
break;
default:
break;
}
}
finish:
/* Delete files and source dir */
QFileInfo fi(filePath);
QDir dir(fi.absolutePath());
dir.removeRecursively();
/* Show final notification */
if (success) {
qCritical() << "Files have been saved to iCloud.";
QMessageBox::critical(Q_NULLPTR, tr("iCloud saving"),
tr("Files have been saved to iCloud.") + "\n\n" +
tr("Path: '%1'").arg(QString(ICLOUD_DATOVKA_CONTAINER_NAME)
+ "/" + targetPath),
QMessageBox::Ok);
} else {
qCritical() << "Files upload to iCloud failed!";
QString txt;
foreach (const QString &error, errorUploads) {
txt += "\n" + error;
}
QMessageBox::critical(Q_NULLPTR,
tr("iCloud saving problem"),
tr("Files have not been saved!") + "\n" + txt,
QMessageBox::Ok);
}
#else /* !Q_OS_IOS */
Q_UNUSED(srcFilePaths);
Q_UNUSED(targetPath);
#endif /* Q_OS_IOS */
}
ICloudHelper::ICloudResult ICloudHelper::uploadFileToCloud(
const QString &srcFilePath, const QString &destFilePath)
{
#ifdef Q_OS_IOS
NSURL *baseURL = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
if (!baseURL) {
NSLog(@"iCloud: Unable to access iCloud.");
return ICLOUD_NOT_ON;
}
// Create iCloud target path
NSURL *documentURL = [baseURL URLByAppendingPathComponent:@"Documents"];
NSURL *messageURL = [documentURL URLByAppendingPathComponent:destFilePath.toNSString()];
// Create subdirectorues on iCloud
NSError *error = nil;
if (![[NSFileManager defaultManager] createDirectoryAtURL:messageURL
withIntermediateDirectories:YES attributes:nil error:&error]) {
NSLog(@"iCloud: Create message subdirectories error: %@", error);
return ICLOUD_TARGET_SAVE_DIR_ERROR;
}
// Create target upload path for iCloud
NSURL *sourceFileUrl = [NSURL fileURLWithPath:srcFilePath.toNSString()];
NSString *fileName = [srcFilePath.toNSString() lastPathComponent];
NSURL *fileURL = [messageURL URLByAppendingPathComponent:fileName];
// Upload file to target directory on iCloud
if ([[NSFileManager defaultManager] setUbiquitous:YES
itemAtURL:sourceFileUrl destinationURL:fileURL error:&error]) {
NSLog(@"iCloud: File %@ has been uploaded.", fileName);
return ICLOUD_FILE_UPLOAD_SUCCESS;
} else {
// Code 516 = file exists in the iCloud. See NSFileManager error codes.
if (error.code == NSFileWriteFileExistsError) {
NSLog(@"iCloud: File with the same name already exists: %@", fileName);
return ICLOUD_FILE_EXISTS;
} else {
NSLog(@"iCloud: Error code: %zd", error.code);
NSLog(@"iCloud: %@", error);
return ICLOUD_FILE_UPLOAD_ERROR;
}
}
#else /* !Q_OS_IOS */
Q_UNUSED(srcFilePath);
Q_UNUSED(destFilePath);
#endif /* Q_OS_IOS */
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment