ShareViewController.m 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. //
  2. // ShareViewController.m
  3. // Nextcloud iOS
  4. //
  5. // Created by Marino Faggiana on 26/01/16.
  6. // Copyright (c) 2017 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. #import "ShareViewController.h"
  24. #import "NCBridgeSwift.h"
  25. @import MobileCoreServices;
  26. @interface ShareViewController () <NCSelectDestinationDelegate>
  27. {
  28. NSURL *dirGroup;
  29. NSUInteger totalSize;
  30. NSExtensionItem *inputItem;
  31. UIColor *barTintColor;
  32. UIColor *tintColor;
  33. NSString *fileNameOriginal;
  34. UIBarButtonItem *rightButtonUpload, *leftButtonCancel;
  35. }
  36. @end
  37. @implementation ShareViewController
  38. #pragma --------------------------------------------------------------------------------------------
  39. #pragma mark ===== View =====
  40. #pragma --------------------------------------------------------------------------------------------
  41. -(void)viewDidLoad
  42. {
  43. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  44. if (tableAccount == nil) {
  45. // close now
  46. [self performSelector:@selector(closeShareViewController) withObject:nil afterDelay:0.1];
  47. return;
  48. } else {
  49. tableCapabilities *capabilities = [[NCManageDatabase sharedInstance] getCapabilitesWithAccount:tableAccount.account];
  50. // Networking
  51. [[NCCommunicationCommon sharedInstance] setupWithUsername:tableAccount.user userID:tableAccount.userID password:[CCUtility getPassword:tableAccount.account] userAgent:[CCUtility getUserAgent] capabilitiesGroup:[NCBrandOptions sharedInstance].capabilitiesGroups nextcloudVersion:capabilities.versionMajor delegate:[NCNetworking sharedInstance]];
  52. _activeAccount = tableAccount.account;
  53. if ([_activeAccount isEqualToString:[CCUtility getActiveAccountExt]]) {
  54. // load
  55. _serverUrl = [CCUtility getServerUrlExt];
  56. _destinyFolderButton.title = [NSString stringWithFormat:NSLocalizedString(@"_destiny_folder_", nil), [CCUtility getTitleServerUrlExt]];
  57. } else {
  58. // Default settings
  59. [CCUtility setActiveAccountExt:self.activeAccount];
  60. _serverUrl = [CCUtility getHomeServerUrlActiveUrl:tableAccount.url];
  61. [CCUtility setServerUrlExt:_serverUrl];
  62. _destinyFolderButton.title = [NSString stringWithFormat:NSLocalizedString(@"_destiny_folder_", nil), NSLocalizedString(@"_home_", nil)];
  63. [CCUtility setTitleServerUrlExt:NSLocalizedString(@"_home_", nil)];
  64. }
  65. }
  66. self.filesName = [[NSMutableArray alloc] init];
  67. self.hud = [[CCHud alloc] initWithView:self.navigationController.view];
  68. [self.shareTable registerNib:[UINib nibWithNibName:@"CCCellShareExt" bundle:nil] forCellReuseIdentifier:@"ShareExtCell"];
  69. [self navigationBarToolBar];
  70. [self loadDataSwift];
  71. }
  72. - (void)viewWillAppear:(BOOL)animated
  73. {
  74. [super viewWillAppear:animated];
  75. if ([[CCUtility getBlockCode] length] > 0 && [CCUtility getOnlyLockDir] == NO)
  76. [self openBKPasscode];
  77. self.view.backgroundColor = NCBrandColor.sharedInstance.backgroundView;
  78. self.shareTable.backgroundColor = NCBrandColor.sharedInstance.backgroundView;
  79. }
  80. - (void)closeShareViewController
  81. {
  82. [self.extensionContext completeRequestReturningItems:self.extensionContext.inputItems completionHandler:nil];
  83. }
  84. - (void)applicationWillTerminate:(UIApplication *)application
  85. {
  86. NSLog(@"[LOG] bye bye, Nextcloud Share Extension!");
  87. }
  88. #pragma --------------------------------------------------------------------------------------------
  89. #pragma mark == TextField Delegate ==
  90. #pragma --------------------------------------------------------------------------------------------
  91. - (void)textFieldDidBeginEditing:(UITextField *)textField
  92. {
  93. rightButtonUpload.enabled = false;
  94. fileNameOriginal = textField.text;
  95. }
  96. - (void)textFieldDidEndEditing:(UITextField *)textField
  97. {
  98. rightButtonUpload.enabled = true;
  99. NSInteger index = [self.filesName indexOfObject:fileNameOriginal];
  100. if (index != NSNotFound) {
  101. self.filesName[index] = textField.text;
  102. }
  103. }
  104. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
  105. {
  106. NSError *error;
  107. if ([string isEqualToString:@"\n"]) {
  108. //NSString *fileName = [textField.text stringByDeletingPathExtension];
  109. NSString *ext = [textField.text pathExtension];
  110. if (![ext isEqualToString:@""]) {
  111. NSString *fileNameAtPath = [NSTemporaryDirectory() stringByAppendingString:fileNameOriginal];
  112. NSString *fileNameToPath = [NSTemporaryDirectory() stringByAppendingString:textField.text];
  113. [[NSFileManager defaultManager] moveItemAtPath:fileNameAtPath toPath:fileNameToPath error:&error];
  114. if (error != nil) {
  115. textField.text = fileNameOriginal;
  116. }
  117. } else {
  118. textField.text = fileNameOriginal;
  119. }
  120. [textField endEditing:true];
  121. return false;
  122. }
  123. return true;
  124. }
  125. #pragma --------------------------------------------------------------------------------------------
  126. #pragma mark == Action ==
  127. #pragma --------------------------------------------------------------------------------------------
  128. - (void)navigationBarToolBar
  129. {
  130. // Theming
  131. if ([NCBrandOptions sharedInstance].use_themingColor) {
  132. tableCapabilities *capabilities = [[NCManageDatabase sharedInstance] getCapabilitesWithAccount:self.activeAccount];
  133. [CCGraphics settingThemingColor:capabilities.themingColor themingColorElement:capabilities.themingColorElement themingColorText:capabilities.themingColorText];
  134. }
  135. self.navigationController.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand;
  136. self.navigationController.navigationBar.tintColor = NCBrandColor.sharedInstance.brandText;
  137. self.toolBar.barTintColor = NCBrandColor.sharedInstance.tabBar;
  138. self.toolBar.tintColor = [UIColor grayColor];
  139. // Upload
  140. rightButtonUpload = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"_save_", nil) style:UIBarButtonItemStylePlain target:self action:@selector(selectPost)];
  141. // Cancel
  142. leftButtonCancel = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIBarButtonItemStylePlain target:self action:@selector(cancelPost)];
  143. // Title
  144. [self.navigationController.navigationBar setTitleTextAttributes: @{NSForegroundColorAttributeName:self.navigationController.navigationBar.tintColor}];
  145. self.navigationItem.title = [NCBrandOptions sharedInstance].brand;
  146. self.navigationItem.leftBarButtonItem = leftButtonCancel;
  147. self.navigationItem.rightBarButtonItems = [[NSArray alloc] initWithObjects:rightButtonUpload, nil];
  148. self.navigationItem.hidesBackButton = YES;
  149. }
  150. - (void)moveServerUrlTo:(NSString *)serverUrlTo title:(NSString *)title type:(NSString *)type
  151. {
  152. // DENIED e2e
  153. if ([CCUtility isFolderEncrypted:serverUrlTo account:self.activeAccount]) {
  154. return;
  155. }
  156. if (serverUrlTo)
  157. _serverUrl = serverUrlTo;
  158. if (title) {
  159. self.destinyFolderButton.title = [NSString stringWithFormat:NSLocalizedString(@"_destiny_folder_", nil), title];
  160. [CCUtility setTitleServerUrlExt:title];
  161. } else {
  162. self.destinyFolderButton.title = [NSString stringWithFormat:NSLocalizedString(@"_destiny_folder_", nil), NSLocalizedString(@"_home_", nil)];
  163. [CCUtility setTitleServerUrlExt:NSLocalizedString(@"_home_", nil)];
  164. }
  165. [CCUtility setActiveAccountExt:self.activeAccount];
  166. [CCUtility setServerUrlExt:_serverUrl];
  167. }
  168. - (IBAction)destinyFolderButtonTapped:(UIBarButtonItem *)sender
  169. {
  170. UINavigationController* navigationController = [[UIStoryboard storyboardWithName:@"NCSelectDestination" bundle:nil] instantiateInitialViewController];
  171. NCSelectDestination *viewController = (NCSelectDestination *)navigationController.topViewController;
  172. viewController.delegate = self;
  173. viewController.move.title = NSLocalizedString(@"_select_", nil);
  174. viewController.tintColor = tintColor;
  175. viewController.barTintColor = barTintColor;
  176. viewController.tintColorTitle = tintColor;
  177. // E2EE
  178. viewController.includeDirectoryE2EEncryption = NO;
  179. [navigationController setModalPresentationStyle:UIModalPresentationFormSheet];
  180. [self presentViewController:navigationController animated:YES completion:nil];
  181. }
  182. - (void)selectPost
  183. {
  184. if ([self.filesName count] > 0) {
  185. [self.hud visibleHudTitle:NSLocalizedString(@"_uploading_", nil) mode:MBProgressHUDModeDeterminate color:NCBrandColor.sharedInstance.brandElement];
  186. NSString *fileName = [self.filesName objectAtIndex:0];
  187. NSString *fileNameLocal = [NSTemporaryDirectory() stringByAppendingString:fileName];
  188. // CONVERSION -->
  189. if ([fileName.pathExtension.uppercaseString isEqualToString:@"HEIC"] && [CCUtility getFormatCompatibility]) {
  190. UIImage *image = [UIImage imageWithContentsOfFile:fileNameLocal];
  191. if (image != nil) {
  192. fileName = fileName.stringByDeletingPathExtension;
  193. fileName = [NSString stringWithFormat:@"%@.jpg", fileName];
  194. [self.filesName replaceObjectAtIndex:0 withObject:fileName];
  195. fileNameLocal = [NSTemporaryDirectory() stringByAppendingString:fileName];
  196. [UIImageJPEGRepresentation(image, 1.0) writeToFile:fileNameLocal atomically:YES];
  197. [self.shareTable reloadData];
  198. }
  199. }
  200. // <--
  201. NSString *fileNameForUpload = [[NCUtility sharedInstance] createFileName:fileName serverUrl:self.serverUrl account:self.activeAccount];
  202. NSString *fileNameServer = [NSString stringWithFormat:@"%@/%@", self.serverUrl, fileNameForUpload];
  203. (void)[[NCCommunication sharedInstance] uploadWithServerUrlFileName:fileNameServer fileNameLocalPath:fileNameLocal dateCreationFile:nil dateModificationFile:nil account:self.activeAccount progressHandler:^(NSProgress * progress) {
  204. [self.hud progress:progress.fractionCompleted];
  205. } completionHandler:^(NSString *account, NSString *ocId, NSString *etag, NSDate *date, int64_t size, NSInteger errorCode, NSString *errorDescription) {
  206. [self.hud hideHud];
  207. [self.filesName removeObject:fileName];
  208. if (errorCode == 0) {
  209. [CCUtility copyFileAtPath:fileNameLocal toPath:[CCUtility getDirectoryProviderStorageOcId:ocId fileNameView:fileNameForUpload]];
  210. tableMetadata *metadata = [tableMetadata new];
  211. metadata.account = self.activeAccount;
  212. metadata.date = date;
  213. metadata.etag = etag;
  214. metadata.ocId = ocId;
  215. metadata.fileName = fileNameForUpload;
  216. metadata.fileNameView = fileNameForUpload;
  217. metadata.serverUrl = self.serverUrl;
  218. metadata.size = size;
  219. (void)[CCUtility insertTypeFileIconName:fileNameForUpload metadata:metadata];
  220. metadata = [[NCManageDatabase sharedInstance] addMetadata:metadata];
  221. (void)[[NCManageDatabase sharedInstance] addLocalFileWithMetadata:metadata];
  222. [self.shareTable performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
  223. [self performSelector:@selector(selectPost) withObject:nil];
  224. } else {
  225. UIAlertController * alert= [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_", nil) message: errorDescription preferredStyle:UIAlertControllerStyleAlert];
  226. UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
  227. handler:^(UIAlertAction * action) {
  228. [alert dismissViewControllerAnimated:YES completion:nil];
  229. [self closeShareViewController];
  230. }];
  231. [alert addAction:ok];
  232. [self presentViewController:alert animated:YES completion:nil];
  233. }
  234. }];
  235. } else {
  236. [self closeShareViewController];
  237. }
  238. }
  239. - (void)cancelPost
  240. {
  241. // rimuoviamo i file+ico
  242. for (NSString *fileName in self.filesName) {
  243. [[NSFileManager defaultManager] removeItemAtPath:[NSTemporaryDirectory() stringByAppendingString:fileName] error:nil];
  244. }
  245. [self closeShareViewController];
  246. }
  247. #pragma --------------------------------------------------------------------------------------------
  248. #pragma mark ===== Lock Password =====
  249. #pragma --------------------------------------------------------------------------------------------
  250. - (void)openBKPasscode
  251. {
  252. CCBKPasscode *viewController = [[CCBKPasscode alloc] initWithNibName:nil bundle:nil];
  253. viewController.delegate = self;
  254. viewController.type = BKPasscodeViewControllerCheckPasscodeType;
  255. viewController.inputViewTitlePassword = YES;
  256. if ([CCUtility getSimplyBlockCode]) {
  257. viewController.passcodeStyle = BKPasscodeInputViewNumericPasscodeStyle;
  258. viewController.passcodeInputView.maximumLength = 6;
  259. } else {
  260. viewController.passcodeStyle = BKPasscodeInputViewNormalPasscodeStyle;
  261. viewController.passcodeInputView.maximumLength = 64;
  262. }
  263. BKTouchIDManager *touchIDManager = [[BKTouchIDManager alloc] initWithKeychainServiceName:k_serviceShareKeyChain];
  264. touchIDManager.promptText = NSLocalizedString(@"_scan_fingerprint_", nil);
  265. viewController.touchIDManager = touchIDManager;
  266. viewController.title = [NCBrandOptions sharedInstance].brand;
  267. viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(passcodeViewCloseButtonPressed:)];
  268. viewController.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
  269. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  270. navigationController.modalPresentationStyle = UIModalPresentationFullScreen;
  271. [self presentViewController:navigationController animated:YES completion:nil];
  272. }
  273. - (NSUInteger)passcodeViewControllerNumberOfFailedAttempts:(CCBKPasscode *)aViewController
  274. {
  275. return self.failedAttempts;
  276. }
  277. - (NSDate *)passcodeViewControllerLockUntilDate:(CCBKPasscode *)aViewController
  278. {
  279. return self.lockUntilDate;
  280. }
  281. - (void)passcodeViewCloseButtonPressed:(id)sender
  282. {
  283. [self dismissViewControllerAnimated:YES completion:^{
  284. [self performSelector:@selector(closeShareViewController) withObject:nil];
  285. }];
  286. }
  287. - (void)passcodeViewController:(CCBKPasscode *)aViewController authenticatePasscode:(NSString *)aPasscode resultHandler:(void (^)(BOOL))aResultHandler
  288. {
  289. if ([aPasscode isEqualToString:[CCUtility getBlockCode]]) {
  290. self.lockUntilDate = nil;
  291. self.failedAttempts = 0;
  292. aResultHandler(YES);
  293. } else aResultHandler(NO);
  294. }
  295. - (void)passcodeViewController:(CCBKPasscode *)aViewController didFinishWithPasscode:(NSString *)aPasscode
  296. {
  297. [aViewController dismissViewControllerAnimated:YES completion:nil];
  298. }
  299. #pragma --------------------------------------------------------------------------------------------
  300. #pragma mark ===== Swipe Table DELETE -> menu =====
  301. #pragma--------------------------------------------------------------------------------------------
  302. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  303. {
  304. [self setEditing:NO animated:YES];
  305. NSString *fileName = [self.filesName objectAtIndex:indexPath.row];
  306. [[NSFileManager defaultManager] removeItemAtPath:[NSTemporaryDirectory() stringByAppendingString:fileName] error:nil];
  307. [self.filesName removeObjectAtIndex:indexPath.row];
  308. if ([self.filesName count] == 0) [self closeShareViewController];
  309. else [self.shareTable performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
  310. }
  311. #pragma --------------------------------------------------------------------------------------------
  312. #pragma mark == Table ==
  313. #pragma --------------------------------------------------------------------------------------------
  314. - (void)loadDataSwift
  315. {
  316. CCloadItemData *loadItem = [[CCloadItemData alloc] init];
  317. [loadItem loadFiles:NSTemporaryDirectory() extensionContext:self.extensionContext vc:self];
  318. }
  319. - (void)reloadData:(NSArray *)files
  320. {
  321. totalSize = 0;
  322. for (NSString *file in files) {
  323. NSUInteger fileSize = (NSInteger)[[[NSFileManager defaultManager] attributesOfItemAtPath:[NSTemporaryDirectory() stringByAppendingString:file] error:nil] fileSize];
  324. totalSize += fileSize;
  325. }
  326. if (totalSize > 0) {
  327. self.filesName = [[NSMutableArray alloc] initWithArray:files];
  328. [self.shareTable reloadData];
  329. } else {
  330. [self closeShareViewController];
  331. }
  332. }
  333. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  334. {
  335. return 80;
  336. }
  337. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  338. {
  339. return 1;
  340. }
  341. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  342. {
  343. return [self.filesName count];
  344. }
  345. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  346. {
  347. NSString *fileName = [self.filesName objectAtIndex:indexPath.row];
  348. UIImage *image = nil;
  349. CCCellShareExt *cell = (CCCellShareExt *)[tableView dequeueReusableCellWithIdentifier:@"ShareExtCell" forIndexPath:indexPath];
  350. CFStringRef fileExtension = (__bridge CFStringRef)[fileName pathExtension];
  351. CFStringRef fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);
  352. if (UTTypeConformsTo(fileUTI, kUTTypeZipArchive) && [(__bridge NSString *)fileUTI containsString:@"org.openxmlformats"] == NO) image = [UIImage imageNamed:@"file_compress"];
  353. else if (UTTypeConformsTo(fileUTI, kUTTypeAudio)) image = [UIImage imageNamed:@"file_audio"];
  354. else if (UTTypeConformsTo(fileUTI, kUTTypeMovie)) image = [UIImage imageNamed:@"file_movie"];
  355. else if (UTTypeConformsTo(fileUTI, kUTTypeImage)) {
  356. image = [UIImage imageWithContentsOfFile:[NSTemporaryDirectory() stringByAppendingString:fileName]];
  357. if (image) {
  358. image = [NCUtility.sharedInstance resizeImageWithImage:image newWidth:cell.frame.size.width];
  359. } else {
  360. image = [UIImage imageNamed:@"file_photo"];
  361. }
  362. }
  363. else if (UTTypeConformsTo(fileUTI, kUTTypeContent)) {
  364. image = [UIImage imageNamed:@"document"];
  365. NSString *typeFile = (__bridge NSString *)fileUTI;
  366. if ([typeFile isEqualToString:@"com.adobe.pdf"]) image = [UIImage imageNamed:@"file_pdf"];
  367. if ([typeFile isEqualToString:@"org.openxmlformats.spreadsheetml.sheet"]) image = [UIImage imageNamed:@"file_xls"];
  368. if ([typeFile isEqualToString:@"public.plain-text"]) image = [UIImage imageNamed:@"file_txt"];
  369. }
  370. else image = [UIImage imageNamed:@"file"];
  371. NSUInteger fileSize = (NSInteger)[[[NSFileManager defaultManager] attributesOfItemAtPath:[NSTemporaryDirectory() stringByAppendingString:fileName] error:nil] fileSize];
  372. cell.fileImageView.image = image;
  373. cell.fileName.text = fileName;
  374. cell.fileName.textColor = NCBrandColor.sharedInstance.textView;
  375. cell.fileName.delegate = self;
  376. cell.info.text = [CCUtility transformedSize:fileSize];
  377. cell.info.textColor = NCBrandColor.sharedInstance.textView;
  378. return cell;
  379. }
  380. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  381. {
  382. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  383. }
  384. @end