-
Notifications
You must be signed in to change notification settings - Fork 6
/
SharingUtils_ios.mm
executable file
·159 lines (131 loc) · 6.01 KB
/
SharingUtils_ios.mm
1
2
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
36
37
38
39
40
41
42
43
44
45
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
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
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
/*
* Copyright (c) 2017 Ekkehard Gentz (ekke)
* Copyright (c) 2020 Emeric Grange
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#import "SharingUtils_ios.h"
#import "docviewcontroller_ios.h"
#import <UIKit/UIKit.h>
#import <UIKit/UIDocumentInteractionController.h>
#import <QGuiApplication>
#import <QQuickWindow>
#import <QDesktopServices>
#import <QUrl>
#import <QFileInfo>
/* ************************************************************************** */
IosShareUtils::IosShareUtils(QObject *parent) : PlatformShareUtils(parent)
{
// Sharing Files from other iOS Apps I got the ideas and some code contribution from:
// Thomas K. Fischer (@taskfabric) - http://taskfabric.com - thx
QDesktopServices::setUrlHandler("file", this, "handleFileUrlReceived");
}
/* ************************************************************************** */
bool IosShareUtils::checkMimeTypeView(const QString &mimeType) {
#pragma unused (mimeType)
// MimeType not used yet
return true;
}
bool IosShareUtils::checkMimeTypeEdit(const QString &mimeType) {
#pragma unused (mimeType)
// MimeType not used yet
return true;
}
void IosShareUtils::share(const QString &text, const QUrl &url) {
NSMutableArray *sharingItems = [NSMutableArray new];
if (!text.isEmpty()) {
[sharingItems addObject:text.toNSString()];
}
if (url.isValid()) {
[sharingItems addObject:url.toNSURL()];
}
// get the main window rootViewController
UIViewController *qtUIViewController = [[UIApplication sharedApplication].keyWindow rootViewController];
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:sharingItems applicationActivities:nil];
if ( [activityController respondsToSelector:@selector(popoverPresentationController)] ) { // iOS8
activityController.popoverPresentationController.sourceView = qtUIViewController.view;
}
[qtUIViewController presentViewController:activityController animated:YES completion:nil];
}
void IosShareUtils::sendFile(const QString &filePath, const QString &title, const QString &mimeType, const int &requestId) {
#pragma unused (title, mimeType)
NSString *nsFilePath = filePath.toNSString();
NSURL *nsFileUrl = [NSURL fileURLWithPath:nsFilePath];
static DocViewController *docViewController = nil;
if (docViewController != nil) {
[docViewController removeFromParentViewController];
[docViewController release];
}
UIDocumentInteractionController *documentInteractionController = nil;
documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:nsFileUrl];
UIViewController *qtUIViewController = [[[[UIApplication sharedApplication]windows] firstObject]rootViewController];
if (qtUIViewController!=nil) {
docViewController = [[DocViewController alloc] init];
docViewController.requestId = requestId;
// we need this to be able to execute handleDocumentPreviewDone() method,
// when preview was finished
docViewController.mIosShareUtils = this;
[qtUIViewController addChildViewController:docViewController];
documentInteractionController.delegate = docViewController;
// [documentInteractionController presentPreviewAnimated:YES];
if (![documentInteractionController presentPreviewAnimated:YES]) {
emit shareError(0, QString("No App found to open: %1").arg(filePath));
}
}
}
void IosShareUtils::viewFile(const QString &filePath, const QString &title, const QString &mimeType, const int &requestId) {
#pragma unused (title, mimeType)
sendFile(filePath, title, mimeType, requestId);
}
void IosShareUtils::editFile(const QString &filePath, const QString &title, const QString &mimeType, const int &requestId) {
#pragma unused (title, mimeType)
sendFile(filePath, title, mimeType, requestId);
}
void IosShareUtils::handleDocumentPreviewDone(const int &requestId)
{
// documentInteractionControllerDidEndPreview
qDebug() << "handleShareDone: " << requestId;
emit shareFinished(requestId);
}
void IosShareUtils::handleFileUrlReceived(const QUrl &url)
{
QString incomingUrl = url.toString();
if (incomingUrl.isEmpty()) {
qWarning() << "setFileUrlReceived: we got an empty URL";
emit shareError(0, "Empty URL received");
return;
}
qDebug() << "IosShareUtils setFileUrlReceived: we got the File URL from iOS: " << incomingUrl;
QString myUrl;
if (incomingUrl.startsWith("file://")) {
myUrl = incomingUrl.right(incomingUrl.length()-7);
qDebug() << "QFile needs this URL: " << myUrl;
} else {
myUrl = incomingUrl;
}
// check if File exists
QFileInfo fileInfo = QFileInfo(myUrl);
if (fileInfo.exists()) {
emit fileUrlReceived(myUrl);
} else {
qDebug() << "setFileUrlReceived: FILE does NOT exist ";
emit shareError(0, QString("File does not exist: %1").arg(myUrl));
}
}
/* ************************************************************************** */