icloud_io.mm 5.17 KB
Newer Older
1
/*
2
 * Copyright (C) 2014-2019 CZ.NIC
3 4 5 6 7 8 9 10
 *
 * 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
Martin Straka's avatar
Martin Straka committed
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 13 14
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
Martin Straka's avatar
Martin Straka committed
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 17 18 19 20 21 22 23
 *
 * 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.
 */

24
#include "ios/src/doc_picker_controller.h"
25 26 27 28 29 30 31
#include "ios/src/icloud_io.h"

ICloudIo::ICloudIo(QObject *parent)
    : QObject(parent)
{
}

Karel Slaný's avatar
Karel Slaný committed
32 33 34
static
NSURL *getCloudBaseUrl(void)
{
35 36 37
	return [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
}

Karel Slaný's avatar
Karel Slaný committed
38 39 40
static
NSURL *getCloudDocumentsUrl(NSURL *baseURL)
{
41 42 43
	return [baseURL URLByAppendingPathComponent:@"Documents"];
}

Karel Slaný's avatar
Karel Slaný committed
44 45
bool ICloudIo::isCloudOn(void)
{
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
	return [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
}

ICloudIo::ICloudResult ICloudIo::moveFileToCloud(
    const QString &srcFilePath, const QString &destFilePath)
{
	NSURL *baseURL = getCloudBaseUrl();
	if (!baseURL) {
		return ICLOUD_NOT_ON;
	}

	// Create iCloud target path
	NSURL *messageURL = [getCloudDocumentsUrl(baseURL) URLByAppendingPathComponent:destFilePath.toNSString()];

	// Create subdirectorues on iCloud
	NSError *error = nil;
	if (![[NSFileManager defaultManager] createDirectoryAtURL:messageURL
	    withIntermediateDirectories:YES attributes:nil 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]) {
		return ICLOUD_FILE_UPLOAD_SUCCESS;
	} else {
		// Code 516 = file exists in the iCloud. See NSFileManager error codes.
		if (error.code == NSFileWriteFileExistsError) {
			return ICLOUD_FILE_EXISTS;
		} else {
			NSLog(@"iCloud: Error code: %zd", error.code);
			NSLog(@"iCloud: %@", error);
			return ICLOUD_FILE_UPLOAD_ERROR;
		}
	}
}

88
bool ICloudIo::openDocumentPickerController(const QStringList &exportFilesPath)
89
{
Karel Slaný's avatar
Karel Slaný committed
90
	static DocumentPickerController *dpc = nil;
91 92 93 94 95 96 97 98 99 100
	if (dpc != nil) {
		[dpc removeFromParentViewController];
		[dpc release];
	}

	UIViewController *rootv = [[[[UIApplication sharedApplication]windows] firstObject]rootViewController];

	if (rootv != nil) {
		dpc = [[DocumentPickerController alloc] init];
		[rootv addChildViewController:dpc];
101 102 103 104 105 106 107 108
		if (exportFilesPath.isEmpty()) {
			// exportFilesPath is empty so import Document Picker will open
			[dpc openImportDocumentPicker];
		} else {
			// exportFilesPath is not empty so export Document Picker will open
			NSMutableArray<NSURL*> *exportUrls = [NSMutableArray array];
			// covert export file paths to array of nsurl
			for (int i = 0; i < exportFilesPath.count(); ++i) {
Karel Slaný's avatar
Karel Slaný committed
109
				QUrl url(QUrl::fromLocalFile(exportFilesPath.at(i)));
110 111 112 113 114 115 116 117 118 119 120 121
				if (url.isValid()) {
					NSURL *fileUrl = url.toNSURL();
					[exportUrls addObject:fileUrl];
					//NSLog(@"ADD FILE URL to list: %@", fileUrl);
				} else {
					NSLog(@"ERROR FILE URL: %@", url.toNSURL());
				}
			}
			if ([exportUrls count] > 0) {
				[dpc openExportDocumentPicker:exportUrls];
			}
		}
122 123 124 125 126 127
		return true;
	}

	return false;
}

128
QUrl ICloudIo::moveFile(const QUrl &sourceFileUrl,
129 130 131
    const QString &newFilePath)
{
	// Convert string path to URL
132
	NSURL *ofp = sourceFileUrl.toNSURL();
133 134 135 136 137 138 139 140
	NSURL *np = [NSURL fileURLWithPath:newFilePath.toNSString()];
	NSString *fileName = [ofp lastPathComponent];

	// Create subdirectorues in the sandbox local storage
	NSError *error = nil;
	if (![[NSFileManager defaultManager] createDirectoryAtURL:np
	    withIntermediateDirectories:YES attributes:nil error:&error]) {
		NSLog(@"Local storage: Create message subdirectories error: %@", error);
141
		return QUrl();
142 143 144 145 146 147 148 149 150 151 152 153
	}

	NSURL *nfp = [np URLByAppendingPathComponent:fileName];
	//NSLog(@"TMP url: %@", ofp);
	//NSLog(@"NP url: %@", np);
	//NSLog(@"SEND url: %@", nfp);

	// Remove file from sandbox local storage if exists
	[[NSFileManager defaultManager] removeItemAtURL:nfp error:&error];

	if ([[NSFileManager defaultManager] moveItemAtURL:ofp toURL:nfp error:&error]) {
		NSLog(@"Local storage: File has moved to target path.");
154
		return QUrl::fromNSURL(nfp);
155 156 157 158 159 160
	} else {
		if (error.code == NSFileWriteFileExistsError) {
			NSLog(@"Local storage: File with the same name already exists in the target path.");
		} else {
			NSLog(@"Local storage: Error code: %zd %@", error.code, error);
		}
161
		return QUrl();
162 163
	}
}