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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
|
//
// ShareViewController.m
// Nextcloud iOS
//
// Created by Marino Faggiana on 26/01/16.
// Copyright (c) 2017 Marino Faggiana. All rights reserved.
//
// Author Marino Faggiana <marino.faggiana@nextcloud.com>
//
// 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/>.
//
#import "ShareViewController.h"
#import "NCBridgeSwift.h"
@import MobileCoreServices;
@interface ShareViewController () <NCSelectDestinationDelegate>
{
NSURL *dirGroup;
NSUInteger totalSize;
NSExtensionItem *inputItem;
UIColor *barTintColor;
UIColor *tintColor;
NSString *fileNameOriginal;
UIBarButtonItem *rightButtonUpload, *leftButtonCancel;
}
@end
@implementation ShareViewController
#pragma --------------------------------------------------------------------------------------------
#pragma mark ===== View =====
#pragma --------------------------------------------------------------------------------------------
-(void)viewDidLoad
{
tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountActive];
if (tableAccount == nil) {
// close now
[self performSelector:@selector(closeShareViewController) withObject:nil afterDelay:0.1];
return;
} else {
NSInteger serverVersionMajor = [[NCManageDatabase sharedInstance] getCapabilitiesServerIntWithAccount:tableAccount.account elements:NCElementsJSON.shared.capabilitiesVersionMajor];
NSString *webDavRoot = [[NCManageDatabase sharedInstance] getCapabilitiesServerStringWithAccount:tableAccount.account elements:NCElementsJSON.shared.capabilitiesWebDavRoot];
// Networking
[[NCCommunicationCommon shared] setupWithAccount:tableAccount.account user:tableAccount.user userId:tableAccount.userID password:[CCUtility getPassword:tableAccount.account] url:tableAccount.url userAgent:[CCUtility getUserAgent] capabilitiesGroup:[NCBrandOptions sharedInstance].capabilitiesGroups webDavRoot:webDavRoot davRoot:nil nextcloudVersion:serverVersionMajor delegate:[NCNetworking shared]];
_activeAccount = tableAccount.account;
if ([_activeAccount isEqualToString:[CCUtility getActiveAccountExt]]) {
// load
_serverUrl = [CCUtility getServerUrlExt];
_destinyFolderButton.title = [NSString stringWithFormat:NSLocalizedString(@"_destiny_folder_", nil), [CCUtility getTitleServerUrlExt]];
} else {
// Default settings
[CCUtility setActiveAccountExt:self.activeAccount];
_serverUrl = [CCUtility getHomeServerUrlActiveUrl:tableAccount.url];
[CCUtility setServerUrlExt:_serverUrl];
_destinyFolderButton.title = [NSString stringWithFormat:NSLocalizedString(@"_destiny_folder_", nil), NSLocalizedString(@"_home_", nil)];
[CCUtility setTitleServerUrlExt:NSLocalizedString(@"_home_", nil)];
}
}
self.filesName = [[NSMutableArray alloc] init];
self.hud = [[CCHud alloc] initWithView:self.navigationController.view];
[self.shareTable registerNib:[UINib nibWithNibName:@"CCCellShareExt" bundle:nil] forCellReuseIdentifier:@"ShareExtCell"];
[self navigationBarToolBar];
[self loadDataSwift];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.view.backgroundColor = NCBrandColor.sharedInstance.backgroundView;
self.shareTable.backgroundColor = NCBrandColor.sharedInstance.backgroundView;
}
- (void)closeShareViewController
{
[self.extensionContext completeRequestReturningItems:self.extensionContext.inputItems completionHandler:nil];
}
- (void)applicationWillTerminate:(UIApplication *)application
{
NSLog(@"[LOG] bye bye, Nextcloud Share Extension!");
}
#pragma --------------------------------------------------------------------------------------------
#pragma mark == TextField Delegate ==
#pragma --------------------------------------------------------------------------------------------
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
rightButtonUpload.enabled = false;
fileNameOriginal = textField.text;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
rightButtonUpload.enabled = true;
NSInteger index = [self.filesName indexOfObject:fileNameOriginal];
if (index != NSNotFound) {
self.filesName[index] = textField.text;
}
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSError *error;
if ([string isEqualToString:@"\n"]) {
//NSString *fileName = [textField.text stringByDeletingPathExtension];
NSString *ext = [textField.text pathExtension];
if (![ext isEqualToString:@""]) {
NSString *fileNameAtPath = [NSTemporaryDirectory() stringByAppendingString:fileNameOriginal];
NSString *fileNameToPath = [NSTemporaryDirectory() stringByAppendingString:textField.text];
[[NSFileManager defaultManager] moveItemAtPath:fileNameAtPath toPath:fileNameToPath error:&error];
if (error != nil) {
textField.text = fileNameOriginal;
}
} else {
textField.text = fileNameOriginal;
}
[textField endEditing:true];
return false;
}
return true;
}
#pragma --------------------------------------------------------------------------------------------
#pragma mark == Action ==
#pragma --------------------------------------------------------------------------------------------
- (void)navigationBarToolBar
{
// Theming
if ([NCBrandOptions sharedInstance].use_themingColor) {
NSString *themingColor = [[NCManageDatabase sharedInstance] getCapabilitiesServerStringWithAccount:self.activeAccount elements:NCElementsJSON.shared.capabilitiesThemingColor];
NSString *themingColorElement = [[NCManageDatabase sharedInstance] getCapabilitiesServerStringWithAccount:self.activeAccount elements:NCElementsJSON.shared.capabilitiesThemingColorElement];
NSString *themingColorText = [[NCManageDatabase sharedInstance] getCapabilitiesServerStringWithAccount:self.activeAccount elements:NCElementsJSON.shared.capabilitiesThemingColorText];
[CCGraphics settingThemingColor:themingColor themingColorElement:themingColorElement themingColorText:themingColorText];
}
self.navigationController.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand;
self.navigationController.navigationBar.tintColor = NCBrandColor.sharedInstance.brandText;
self.toolBar.barTintColor = NCBrandColor.sharedInstance.tabBar;
self.toolBar.tintColor = [UIColor grayColor];
// Upload
rightButtonUpload = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"_save_", nil) style:UIBarButtonItemStylePlain target:self action:@selector(selectPost)];
// Cancel
leftButtonCancel = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIBarButtonItemStylePlain target:self action:@selector(cancelPost)];
// Title
[self.navigationController.navigationBar setTitleTextAttributes: @{NSForegroundColorAttributeName:self.navigationController.navigationBar.tintColor}];
self.navigationItem.title = [NCBrandOptions sharedInstance].brand;
self.navigationItem.leftBarButtonItem = leftButtonCancel;
self.navigationItem.rightBarButtonItems = [[NSArray alloc] initWithObjects:rightButtonUpload, nil];
self.navigationItem.hidesBackButton = YES;
}
- (void)moveServerUrlTo:(NSString *)serverUrlTo title:(NSString *)title type:(NSString *)type
{
// DENIED e2e
if ([CCUtility isFolderEncrypted:serverUrlTo e2eEncrypted:false account:self.activeAccount]) {
return;
}
if (serverUrlTo)
_serverUrl = serverUrlTo;
if (title) {
self.destinyFolderButton.title = [NSString stringWithFormat:NSLocalizedString(@"_destiny_folder_", nil), title];
[CCUtility setTitleServerUrlExt:title];
} else {
self.destinyFolderButton.title = [NSString stringWithFormat:NSLocalizedString(@"_destiny_folder_", nil), NSLocalizedString(@"_home_", nil)];
[CCUtility setTitleServerUrlExt:NSLocalizedString(@"_home_", nil)];
}
[CCUtility setActiveAccountExt:self.activeAccount];
[CCUtility setServerUrlExt:_serverUrl];
}
- (IBAction)destinyFolderButtonTapped:(UIBarButtonItem *)sender
{
UINavigationController* navigationController = [[UIStoryboard storyboardWithName:@"NCSelectDestination" bundle:nil] instantiateInitialViewController];
NCSelectDestination *viewController = (NCSelectDestination *)navigationController.topViewController;
viewController.delegate = self;
viewController.move.title = NSLocalizedString(@"_select_", nil);
viewController.tintColor = tintColor;
viewController.barTintColor = barTintColor;
viewController.tintColorTitle = tintColor;
// E2EE
viewController.includeDirectoryE2EEncryption = NO;
[navigationController setModalPresentationStyle:UIModalPresentationFormSheet];
[self presentViewController:navigationController animated:YES completion:nil];
}
- (void)selectPost
{
if ([self.filesName count] > 0) {
[self.hud visibleHudTitle:NSLocalizedString(@"_uploading_", nil) mode:MBProgressHUDModeDeterminate color:NCBrandColor.sharedInstance.brandElement];
NSString *fileName = [self.filesName objectAtIndex:0];
NSString *fileNameLocal = [NSTemporaryDirectory() stringByAppendingString:fileName];
// CONVERSION -->
if ([fileName.pathExtension.uppercaseString isEqualToString:@"HEIC"] && [CCUtility getFormatCompatibility]) {
UIImage *image = [UIImage imageWithContentsOfFile:fileNameLocal];
if (image != nil) {
fileName = fileName.stringByDeletingPathExtension;
fileName = [NSString stringWithFormat:@"%@.jpg", fileName];
[self.filesName replaceObjectAtIndex:0 withObject:fileName];
fileNameLocal = [NSTemporaryDirectory() stringByAppendingString:fileName];
[UIImageJPEGRepresentation(image, 1.0) writeToFile:fileNameLocal atomically:YES];
[self.shareTable reloadData];
}
}
// <--
NSString *fileNameForUpload = [[NCUtility sharedInstance] createFileName:fileName serverUrl:self.serverUrl account:self.activeAccount];
NSString *fileNameServer = [NSString stringWithFormat:@"%@/%@", self.serverUrl, fileNameForUpload];
[[NCCommunication shared] uploadWithServerUrlFileName:fileNameServer fileNameLocalPath:fileNameLocal dateCreationFile:nil dateModificationFile:nil customUserAgent:nil addCustomHeaders:nil taskHandler:^(NSURLSessionTask *task) {
} progressHandler:^(NSProgress * progress) {
[self.hud progress:progress.fractionCompleted];
} completionHandler:^(NSString *account, NSString *ocId, NSString *etag, NSDate *date, int64_t size, NSInteger errorCode, NSString *errorDescription) {
[self.hud hideHud];
[self.filesName removeObject:fileName];
if (errorCode == 0) {
[CCUtility copyFileAtPath:fileNameLocal toPath:[CCUtility getDirectoryProviderStorageOcId:ocId fileNameView:fileNameForUpload]];
tableMetadata *metadata = [[NCManageDatabase sharedInstance] createMetadataWithAccount:self.activeAccount fileName:fileNameForUpload ocId:ocId serverUrl:self.serverUrl url:@"" contentType:@""];
metadata.date = date;
metadata.etag = etag;
metadata.serverUrl = self.serverUrl;
metadata.size = size;
metadata = [[NCManageDatabase sharedInstance] addMetadata:metadata];
(void)[[NCManageDatabase sharedInstance] addLocalFileWithMetadata:metadata];
[self.shareTable performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
[self performSelector:@selector(selectPost) withObject:nil];
} else {
UIAlertController * alert= [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_", nil) message: errorDescription preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[alert dismissViewControllerAnimated:YES completion:nil];
[self closeShareViewController];
}];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];
}
}];
} else {
[self closeShareViewController];
}
}
- (void)cancelPost
{
// rimuoviamo i file+ico
for (NSString *fileName in self.filesName) {
[[NSFileManager defaultManager] removeItemAtPath:[NSTemporaryDirectory() stringByAppendingString:fileName] error:nil];
}
[self closeShareViewController];
}
#pragma --------------------------------------------------------------------------------------------
#pragma mark ===== Swipe Table DELETE -> menu =====
#pragma--------------------------------------------------------------------------------------------
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[self setEditing:NO animated:YES];
NSString *fileName = [self.filesName objectAtIndex:indexPath.row];
[[NSFileManager defaultManager] removeItemAtPath:[NSTemporaryDirectory() stringByAppendingString:fileName] error:nil];
[self.filesName removeObjectAtIndex:indexPath.row];
if ([self.filesName count] == 0) [self closeShareViewController];
else [self.shareTable performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
}
#pragma --------------------------------------------------------------------------------------------
#pragma mark == Table ==
#pragma --------------------------------------------------------------------------------------------
- (void)loadDataSwift
{
CCloadItemData *loadItem = [[CCloadItemData alloc] init];
[loadItem loadFiles:NSTemporaryDirectory() extensionContext:self.extensionContext vc:self];
}
- (void)reloadData:(NSArray *)files
{
totalSize = 0;
for (NSString *file in files) {
NSUInteger fileSize = (NSInteger)[[[NSFileManager defaultManager] attributesOfItemAtPath:[NSTemporaryDirectory() stringByAppendingString:file] error:nil] fileSize];
totalSize += fileSize;
}
if (totalSize > 0) {
self.filesName = [[NSMutableArray alloc] initWithArray:files];
[self.shareTable reloadData];
} else {
[self closeShareViewController];
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.filesName count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *fileName = [self.filesName objectAtIndex:indexPath.row];
UIImage *image = nil;
CCCellShareExt *cell = (CCCellShareExt *)[tableView dequeueReusableCellWithIdentifier:@"ShareExtCell" forIndexPath:indexPath];
CFStringRef fileExtension = (__bridge CFStringRef)[fileName pathExtension];
CFStringRef fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);
if (UTTypeConformsTo(fileUTI, kUTTypeZipArchive) && [(__bridge NSString *)fileUTI containsString:@"org.openxmlformats"] == NO) image = [UIImage imageNamed:@"file_compress"];
else if (UTTypeConformsTo(fileUTI, kUTTypeAudio)) image = [UIImage imageNamed:@"file_audio"];
else if (UTTypeConformsTo(fileUTI, kUTTypeMovie)) image = [UIImage imageNamed:@"file_movie"];
else if (UTTypeConformsTo(fileUTI, kUTTypeImage)) {
image = [UIImage imageWithContentsOfFile:[NSTemporaryDirectory() stringByAppendingString:fileName]];
if (image) {
image = [NCUtility.sharedInstance resizeImageWithImage:image newWidth:cell.frame.size.width];
} else {
image = [UIImage imageNamed:@"file_photo"];
}
}
else if (UTTypeConformsTo(fileUTI, kUTTypeContent)) {
image = [UIImage imageNamed:@"document"];
NSString *typeFile = (__bridge NSString *)fileUTI;
if ([typeFile isEqualToString:@"com.adobe.pdf"]) image = [UIImage imageNamed:@"file_pdf"];
if ([typeFile isEqualToString:@"org.openxmlformats.spreadsheetml.sheet"]) image = [UIImage imageNamed:@"file_xls"];
if ([typeFile isEqualToString:@"public.plain-text"]) image = [UIImage imageNamed:@"file_txt"];
}
else image = [UIImage imageNamed:@"file"];
NSUInteger fileSize = (NSInteger)[[[NSFileManager defaultManager] attributesOfItemAtPath:[NSTemporaryDirectory() stringByAppendingString:fileName] error:nil] fileSize];
cell.fileImageView.image = image;
cell.fileName.text = fileName;
cell.fileName.textColor = NCBrandColor.sharedInstance.textView;
cell.fileName.delegate = self;
cell.info.text = [CCUtility transformedSize:fileSize];
cell.info.textColor = NCBrandColor.sharedInstance.textView;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
@end
|