| 1 | /* |
| 2 | * Copyright (C) 2012-2013 Canonical, Ltd. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; version 3. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | */ |
| 16 | |
| 17 | #include "sharefile.h" |
| 18 | |
| 19 | #include <QtCore/QDebug> |
| 20 | #include <QtCore/QDir> |
| 21 | #include <QtCore/QFile> |
| 22 | #include <QtCore/QFileInfo> |
| 23 | #include <QtCore/QTemporaryFile> |
| 24 | #include <QtCore/QTextStream> |
| 25 | #include <QtQml/QQmlContext> |
| 26 | #include <QtQml/QQmlEngine> |
| 27 | #include <QtQuick/QQuickImageProvider> |
| 28 | |
| 29 | ShareFile::ShareFile(QObject* parent) |
| 30 | : QObject(parent) |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | QString ShareFile::saveImageFromProvider(const QString& imageUri) |
| 35 | { |
| 36 | // Get image provider |
| 37 | QQmlContext* ctx = QQmlEngine::contextForObject(this); |
| 38 | if (ctx == 0) { |
| 39 | qWarning() << "Share object does not have a QML context."; |
| 40 | return QString(); |
| 41 | } |
| 42 | |
| 43 | QQmlEngine* eng = ctx->engine(); |
| 44 | if (eng == 0) { |
| 45 | qWarning() << "Share object does not have a QML engine."; |
| 46 | return QString(); |
| 47 | } |
| 48 | |
| 49 | // parse uri (image://<provider>/<file-id>) |
| 50 | QString tempUri(imageUri.mid(8)); |
| 51 | QStringList uriParts = tempUri.split("/"); |
| 52 | if (uriParts.count() < 2) { |
| 53 | qWarning() << "Invalid image uri."; |
| 54 | return QString(); |
| 55 | } |
| 56 | |
| 57 | QString providerName = uriParts.takeFirst(); |
| 58 | QQmlImageProviderBase* provider = eng->imageProvider(providerName); |
| 59 | if (provider == 0) { |
| 60 | qWarning() << "Image provider not found: " << provider; |
| 61 | return QString(); |
| 62 | } |
| 63 | |
| 64 | QQuickImageProvider* qprovider = (QQuickImageProvider*)provider; |
| 65 | QSize size; |
| 66 | QImage img = qprovider->requestImage(uriParts.join("/"), &size, QSize()); |
| 67 | |
| 68 | QTemporaryFile tempFile; |
| 69 | tempFile.setAutoRemove(false); |
| 70 | if (img.save(&tempFile, "png")) { |
| 71 | return tempFile.fileName(); |
| 72 | } else { |
| 73 | qWarning() << "Fail to save image from provider."; |
| 74 | return QString(); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | void ShareFile::writeShareFile(const QString& path) |
| 79 | { |
| 80 | QString newPath = path; |
| 81 | if (path.startsWith("image://")) { |
| 82 | newPath = saveImageFromProvider(path); |
| 83 | } |
| 84 | |
| 85 | QFileInfo imageFilePath(QDir::tempPath() + QDir::separator() + "sharelocation"); |
| 86 | QFile imageFile(imageFilePath.absoluteFilePath()); |
| 87 | if (imageFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) { |
| 88 | QTextStream stream(&imageFile); |
| 89 | stream << newPath; |
| 90 | imageFile.close(); |
| 91 | } else { |
| 92 | qWarning() << "Failed to open share file for writing"; |
| 93 | } |
| 94 | } |