icloud_helper.mm 5.51 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
/*
 * 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 */
}