icloud_helper.cpp 8.47 KB
Newer Older
1
/*
2
 * Copyright (C) 2014-2019 CZ.NIC
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
 *
 * 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 "src/auxiliaries/icloud_helper.h"
#include "src/io/filesystem.h"
#include "src/models/cloudmodel.h"
#include "src/datovka_shared/log/log.h"

#ifdef Q_OS_IOS
#include "ios/src/icloud_io.h"
#define ICLOUD_DATOVKA_CONTAINER_NAME "Datovka"
36
#define ICLOUD_FILE_CONTAINER_ID "iCloud~cz~nic~mobile-datovka/Documents"
37
#define SEND_FILE_PATH_PREFIX "Documents/Datovka"
38 39 40 41 42 43 44 45 46
#endif /* Q_OS_IOS */

#ifndef Q_OS_IOS
QStringList iCloudFileList;
bool isSearchRunning;
#endif /* Q_OS_IOS */

ICloudHelper::ICloudHelper(QObject *parent)
    : QObject(parent),
47
    m_icloudTimer(new QTimer(this))
48 49 50 51 52
{
}

void ICloudHelper::createCloudConteiner(void)
{
53
	debugFuncCall();
54 55 56 57 58 59 60 61 62 63

#ifdef Q_OS_IOS

	ICloudIo::createCloudConteiner();

#endif /* Q_OS_IOS */
}

void ICloudHelper::getCloudHierarchyAsync(void)
{
64
	debugFuncCall();
65 66 67 68 69 70 71 72 73 74 75

#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;
	}

	iCloudFileList.clear();
76 77 78 79 80
	isSearchRunning = ICloudIo::getCloudHierarchyAsync();
	if (isSearchRunning) {
		connect(m_icloudTimer, SIGNAL(timeout()), this, SLOT(receivedCloudHierarchy()));
		m_icloudTimer->start(200);
	}
81 82 83
#endif /* Q_OS_IOS */
}

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
void ICloudHelper::stopCloudHierarchyAsync(void)
{
	debugFuncCall();

#ifdef Q_OS_IOS

	m_icloudTimer->stop();
	isSearchRunning = false;
	disconnect(m_icloudTimer, SIGNAL(timeout()),
	    this, SLOT(receivedCloudHierarchy()));
	iCloudFileList.clear();

#endif /* Q_OS_IOS */
}

99 100 101
void ICloudHelper::storeFilesToCloud(const QStringList &srcFilePaths,
    const QString &targetPath)
{
102
	debugFuncCall();
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

#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 */
}

184
bool ICloudHelper::downloadFileFromCloud(const QString &cloudFilePath)
185
{
186
	debugFuncCall();
187 188 189

#ifdef Q_OS_IOS

190 191 192 193 194 195 196 197 198 199 200 201 202 203
	return ICloudIo::isDownloadedFromCloud(cloudFilePath);

#else
	Q_UNUSED(cloudFilePath);
	return false;
#endif
}

QString ICloudHelper::copyFileFromCloud(const QString &cloudFilePath,
    const QString &localFilePath)
{
	debugFuncCall();

#ifdef Q_OS_IOS
204

205 206 207 208 209 210 211 212 213
	QString lfPath = ICloudIo::copyFileFromCloud(cloudFilePath,
	    appSendDirPath() + localFilePath);
	if (!lfPath.isEmpty()) {
		lfPath = QUrl::fromPercentEncoding(lfPath.toUtf8());
		lfPath = lfPath.replace("file://", "");
		return lfPath;
	} else {
		return QString();
	}
214 215 216

#else
	Q_UNUSED(cloudFilePath);
217 218 219 220 221 222 223 224
	Q_UNUSED(localFilePath);
	return QString();
#endif
}

/*
 * Retrun short iCloud path.
 */
225
static
226 227 228 229 230 231 232
QString getShortPathFromCloudPath(const QString &fileCloudPath)
{
#ifdef Q_OS_IOS

	// Get short file path from iCloud path
	QString pattern(ICLOUD_FILE_CONTAINER_ID);
	int pos = fileCloudPath.indexOf(pattern) + pattern.length();
233
	return QUrl::fromPercentEncoding(fileCloudPath.mid(pos).toUtf8());
234 235 236

#else
	Q_UNUSED(fileCloudPath);
237 238 239 240
	return QString();
#endif
}

241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
/*
 * Retrun short local send file path.
 */
QString ICloudHelper::getShortSendFilePath(const QString &sandBoxFilePath)
{
#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
}

259 260 261
void ICloudHelper::setCloudFileModel(const QVariant &cloudFileModelVariant,
    const QStringList &fileList)
{
262
	debugFuncCall();
263

264
	/* Obtain pointer to icloud file model from variant. */
265 266 267 268 269 270 271
	CloudFileListModel *cloudModel =
	    CloudFileListModel::fromVariant(cloudFileModelVariant);
	if (cloudModel == Q_NULLPTR) {
		Q_ASSERT(0);
		return;
	}

272 273 274
	foreach (const QString &fileCloudPath, fileList) {

		QFileInfo fi(fileCloudPath);
275
		if (fi.fileName().isEmpty()) {
276
			// Skip directories
277 278
			continue;
		}
279 280 281 282 283

		// Append file to model, false means file is unselected
		cloudModel->appendFileFromCloud(fileCloudPath,
		    QUrl::fromPercentEncoding(fi.fileName().toUtf8()),
		    getShortPathFromCloudPath(fi.path()), false);
284 285 286 287 288 289 290 291 292
	}
}

void ICloudHelper::clearSendDir(void)
{
	debugFuncCall();

	QDir dir(appSendDirPath());
	dir.removeRecursively();
293 294 295 296 297
/*
	// TODO - used for experiments and testing only
	QDir dir2(dfltAttachSavingLoc());
	dir2.removeRecursively();
*/
298 299 300 301 302 303 304
}

void ICloudHelper::receivedCloudHierarchy(void)
{
#ifdef Q_OS_IOS

	if (!isSearchRunning) {
305 306
		m_icloudTimer->stop();
		disconnect(m_icloudTimer, SIGNAL(timeout()),
307
		    this, SLOT(receivedCloudHierarchy()));
308
		emit cloudContentSig(iCloudFileList);
309 310 311 312 313 314
	} else {
		emit cloudActivitySig(tr("Searching for iCloud files..."));
	}

#endif
}
315 316 317 318 319 320 321

void ICloudHelper::openDocumentPickerController(void)
{
	debugFuncCall();

#ifdef Q_OS_IOS

322
	ICloudIo::openDocumentPickerController(QStringList());
323 324 325 326

#endif /* Q_OS_IOS */
}

327
void ICloudHelper::importFilesToAppInbox(QList<QUrl> selectedFileUrls)
328
{
329 330
	foreach (const QUrl &fileUrl, selectedFileUrls) {
		moveFileToSendDir(fileUrl);
331 332 333
	}
}

334
void ICloudHelper::moveFileToSendDir(const QUrl &sourceFileUrl)
335 336 337
{
#ifdef Q_OS_IOS

338
	if (Q_UNLIKELY(!sourceFileUrl.isValid())) {
339 340
		return;
	}
341 342 343 344 345

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

348 349 350 351
#else /* !Q_OS_IOS */

	Q_UNUSED(sourceFileUrl);

352 353
#endif
}
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372

void ICloudHelper::storeFilesToDeviceStorage(const QStringList &srcFilePaths)
{
	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 */
}