ios_helper.cpp 5.06 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 24 25 26 27
 *
 * 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>

Martin Straka's avatar
Martin Straka committed
28
#include "src/auxiliaries/ios_helper.h"
29 30 31 32 33 34
#include "src/io/filesystem.h"
#include "src/datovka_shared/log/log.h"

#ifdef Q_OS_IOS
#include "ios/src/icloud_io.h"
#define ICLOUD_DATOVKA_CONTAINER_NAME "Datovka"
35
#define SEND_FILE_PATH_PREFIX "Documents/Datovka"
36 37
#endif /* Q_OS_IOS */

Martin Straka's avatar
Martin Straka committed
38
IosHelper::IosHelper(QObject *parent)
39
    : QObject(parent)
40 41 42
{
}

Martin Straka's avatar
Martin Straka committed
43
void IosHelper::storeFilesToCloud(const QStringList &srcFilePaths,
44 45
    const QString &targetPath)
{
46
	debugFuncCall();
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

#ifdef Q_OS_IOS

	if (!ICloudIo::isCloudOn()) {
		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) {

		ICloudIo::ICloudResult retCode = ICloudIo::moveFileToCloud(filePath, targetPath);

		QFileInfo fi(filePath);

		switch (retCode) {
		case ICloudIo::ICLOUD_NOT_ON:
			success &= false;
			errorUploads.append(tr("Unable to access iCloud!"));
			goto finish;
			break;
		case ICloudIo::ICLOUD_TARGET_SAVE_DIR_ERROR:
			success &= false;
			errorUploads.append(tr("Cannot create subdir '%1' in iCloud.").arg(targetPath));
			goto finish;
			break;
		case ICloudIo::ICLOUD_FILE_EXISTS:
			success &= false;
			errorUploads.append(tr("File '%1' already exists in iCloud.").arg(fi.fileName()));
			break;
		case ICloudIo::ICLOUD_FILE_UPLOAD_ERROR:
			success &= false;
			errorUploads.append(tr("File '%1' upload failed.").arg(fi.fileName()));
			break;
		case ICloudIo::ICLOUD_FILE_UPLOAD_SUCCESS:
			errorUploads.append(tr("File '%1' has been moved 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) {
		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 {
		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 */
}

Martin Straka's avatar
Martin Straka committed
126
QString IosHelper::getShortSendFilePath(const QString &sandBoxFilePath)
127 128 129 130 131 132 133 134 135 136 137 138 139 140
{
#ifdef Q_OS_IOS

	// Get short local send file path
	QString pattern(SEND_FILE_PATH_PREFIX);
	int pos = sandBoxFilePath.indexOf(pattern) + pattern.length();
	return QUrl::fromPercentEncoding(sandBoxFilePath.mid(pos).toUtf8());

#else
	Q_UNUSED(sandBoxFilePath);
	return QString();
#endif
}

Martin Straka's avatar
Martin Straka committed
141
void IosHelper::clearSendDir(void)
142 143 144 145 146
{
	debugFuncCall();

	QDir dir(appSendDirPath());
	dir.removeRecursively();
147 148 149 150 151
/*
	// TODO - used for experiments and testing only
	QDir dir2(dfltAttachSavingLoc());
	dir2.removeRecursively();
*/
152 153
}

Martin Straka's avatar
Martin Straka committed
154
void IosHelper::openDocumentPickerController(void)
155 156 157 158 159
{
	debugFuncCall();

#ifdef Q_OS_IOS

160
	ICloudIo::openDocumentPickerController(QStringList());
161 162 163 164

#endif /* Q_OS_IOS */
}

Martin Straka's avatar
Martin Straka committed
165
void IosHelper::importFilesToAppInbox(QList<QUrl> selectedFileUrls)
166
{
167 168
	foreach (const QUrl &fileUrl, selectedFileUrls) {
		moveFileToSendDir(fileUrl);
169 170 171
	}
}

Martin Straka's avatar
Martin Straka committed
172
void IosHelper::moveFileToSendDir(const QUrl &sourceFileUrl)
173
{
174

175 176
#ifdef Q_OS_IOS

177
	if (Q_UNLIKELY(!sourceFileUrl.isValid())) {
178 179
		return;
	}
180 181 182 183 184

	QUrl targetFileUrl = ICloudIo::moveFile(sourceFileUrl, appSendDirPath());
	if (targetFileUrl.isValid()) {
		targetFileUrl.setScheme(QString());
		emit fileSelectedSig(targetFileUrl.toString());
185 186
	}

187 188
#else /* !Q_OS_IOS */
	Q_UNUSED(sourceFileUrl);
189 190
#endif
}
191

Martin Straka's avatar
Martin Straka committed
192
void IosHelper::storeFilesToDeviceStorage(const QStringList &srcFilePaths)
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
{
	debugFuncCall();

#ifdef Q_OS_IOS

	if (Q_UNLIKELY(srcFilePaths.isEmpty())) {
		return;
	}

	ICloudIo::openDocumentPickerController(srcFilePaths);

#else /* !Q_OS_IOS */
	Q_UNUSED(srcFilePaths);
#endif /* Q_OS_IOS */
}