From a273ca9c439b515358607acd05fd90346b49a448 Mon Sep 17 00:00:00 2001 From: Richard Moe Gustavsen Date: Wed, 18 Feb 2015 13:43:02 +0100 Subject: iOS: return file urls rather than asset urls from file dialog MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We need to pass the asset url around as a file url in the application, so that QUrl::fromLocalFile()/toLocalFile() works. Note that QUrl::fromLocalFile() will remove double slashes. We therefore need to check for this, and restore missing slashes, when loading files in the file engine. Change-Id: I2de6b91d7a112354590cf2981f7b403eacf92a59 Reviewed-by: Tor Arne Vestbø --- src/plugins/platforms/ios/qiosfiledialog.mm | 3 ++- .../platforms/ios/qiosfileengineassetslibrary.mm | 24 +++++++++++++++------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/plugins/platforms/ios/qiosfiledialog.mm b/src/plugins/platforms/ios/qiosfiledialog.mm index 1d4298a158..28b9c0d883 100644 --- a/src/plugins/platforms/ios/qiosfiledialog.mm +++ b/src/plugins/platforms/ios/qiosfiledialog.mm @@ -60,7 +60,8 @@ { Q_UNUSED(picker); NSURL *url = [info objectForKey:UIImagePickerControllerReferenceURL]; - m_fileDialog->selectedFilesChanged(QList() << QUrl::fromNSURL(url)); + QUrl fileUrl = QUrl::fromLocalFile(QString::fromNSString([url description])); + m_fileDialog->selectedFilesChanged(QList() << fileUrl); emit m_fileDialog->accept(); } diff --git a/src/plugins/platforms/ios/qiosfileengineassetslibrary.mm b/src/plugins/platforms/ios/qiosfileengineassetslibrary.mm index c4be7cf4dc..73bfc2a87f 100644 --- a/src/plugins/platforms/ios/qiosfileengineassetslibrary.mm +++ b/src/plugins/platforms/ios/qiosfileengineassetslibrary.mm @@ -42,9 +42,9 @@ class QIOSAssetData : public QObject { public: - QIOSAssetData(const QString &fileName, QIOSFileEngineAssetsLibrary *engine) + QIOSAssetData(const QString &assetUrl, QIOSFileEngineAssetsLibrary *engine) : m_asset(0) - , m_fileName(fileName) + , m_assetUrl(assetUrl) , m_assetLibrary(0) { switch ([ALAssetsLibrary authorizationStatus]) { @@ -71,7 +71,7 @@ public: // reuse its data. Since QFile is (mostly) reentrant, we need to protect m_currentAssetData // from being modified by several threads at the same time. QMutexLocker lock(&g_mutex); - if (g_currentAssetData && g_currentAssetData->m_fileName == fileName) { + if (g_currentAssetData && g_currentAssetData->m_assetUrl == assetUrl) { m_assetLibrary = [g_currentAssetData->m_assetLibrary retain]; m_asset = [g_currentAssetData->m_asset retain]; return; @@ -87,7 +87,7 @@ public: dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - NSURL *url = [NSURL URLWithString:m_fileName.toNSString()]; + NSURL *url = [NSURL URLWithString:assetUrl.toNSString()]; m_assetLibrary = [[ALAssetsLibrary alloc] init]; [m_assetLibrary assetForURL:url resultBlock:^(ALAsset *asset) { m_asset = [asset retain]; @@ -117,7 +117,7 @@ public: ALAsset *m_asset; private: - QString m_fileName; + QString m_assetUrl; ALAssetsLibrary *m_assetLibrary; static QBasicMutex g_mutex; @@ -143,8 +143,18 @@ QIOSFileEngineAssetsLibrary::~QIOSFileEngineAssetsLibrary() ALAsset *QIOSFileEngineAssetsLibrary::loadAsset() const { - if (!m_data) - m_data = new QIOSAssetData(m_fileName, const_cast(this)); + if (!m_data) { + // QUrl::fromLocalFile() will remove double slashes. Since the asset url is passed around as a file + // name in the app (and converted to/from a file url, e.g in QFileDialog), we need to check if we still + // have two leading slashes after the scheme, and restore the second slash if not. + QString assetUrl = m_fileName; + const int index = 16; // "assets-library://" + if (assetUrl[index] != QLatin1Char('/')) + assetUrl.insert(index, '/'); + + m_data = new QIOSAssetData(assetUrl, const_cast(this)); + } + return m_data->m_asset; } -- cgit v1.2.3