ShareViewController.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. // Networking
  50. [[NCCommunicationCommon sharedInstance] setupWithUsername:tableAccount.user userID:tableAccount.userID password:[CCUtility getPassword:tableAccount.account] userAgent:[CCUtility getUserAgent] capabilitiesGroup:[NCBrandOptions sharedInstance].capabilitiesGroups delegate:[NCNetworking sharedInstance]];
  51. _activeAccount = tableAccount.account;
  52. if ([_activeAccount isEqualToString:[CCUtility getActiveAccountExt]]) {
  53. // load
  54. _serverUrl = [CCUtility getServerUrlExt];
  55. _destinyFolderButton.title = [NSString stringWithFormat:NSLocalizedString(@"_destiny_folder_", nil), [CCUtility getTitleServerUrlExt]];
  56. } else {
  57. // Default settings
  58. [CCUtility setActiveAccountExt:self.activeAccount];
  59. _serverUrl = [CCUtility getHomeServerUrlActiveUrl:tableAccount.url];
  60. [CCUtility setServerUrlExt:_serverUrl];
  61. _destinyFolderButton.title = [NSString stringWithFormat:NSLocalizedString(@"_destiny_folder_", nil), NSLocalizedString(@"_home_", nil)];
  62. [CCUtility setTitleServerUrlExt:NSLocalizedString(@"_home_", nil)];
  63. }
  64. }
  65. self.filesName = [[NSMutableArray alloc] init];
  66. self.hud = [[CCHud alloc] initWithView:self.navigationController.view];
  67. [self.shareTable registerNib:[UINib nibWithNibName:@"CCCellShareExt" bundle:nil] forCellReuseIdentifier:@"ShareExtCell"];
  68. [self navigationBarToolBar];
  69. [self loadDataSwift];
  70. }
  71. - (void)viewWillAppear:(BOOL)animated
  72. {
  73. [super viewWillAppear:animated];
  74. if ([[CCUtility getBlockCode] length] > 0 && [CCUtility getOnlyLockDir] == NO)
  75. [self openBKPasscode];
  76. self.view.backgroundColor = NCBrandColor.sharedInstance.backgroundView;
  77. self.shareTable.backgroundColor = NCBrandColor.sharedInstance.backgroundView;
  78. }
  79. - (void)closeShareViewController
  80. {
  81. [self.extensionContext completeRequestReturningItems:self.extensionContext.inputItems completionHandler:nil];
  82. }
  83. - (void)applicationWillTerminate:(UIApplication *)application
  84. {
  85. NSLog(@"[LOG] bye bye, Nextcloud Share Extension!");
  86. }
  87. #pragma --------------------------------------------------------------------------------------------
  88. #pragma mark == TextField Delegate ==
  89. #pragma --------------------------------------------------------------------------------------------
  90. - (void)textFieldDidBeginEditing:(UITextField *)textField
  91. {
  92. rightButtonUpload.enabled = false;
  93. fileNameOriginal = textField.text;
  94. }
  95. - (void)textFieldDidEndEditing:(UITextField *)textField
  96. {
  97. rightButtonUpload.enabled = true;
  98. NSInteger index = [self.filesName indexOfObject:fileNameOriginal];
  99. if (index != NSNotFound) {
  100. self.filesName[index] = textField.text;
  101. }
  102. }
  103. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
  104. {
  105. NSError *error;
  106. if ([string isEqualToString:@"\n"]) {
  107. //NSString *fileName = [textField.text stringByDeletingPathExtension];
  108. NSString *ext = [textField.text pathExtension];
  109. if (![ext isEqualToString:@""]) {
  110. NSString *fileNameAtPath = [NSTemporaryDirectory() stringByAppendingString:fileNameOriginal];
  111. NSString *fileNameToPath = [NSTemporaryDirectory() stringByAppendingString:textField.text];
  112. [[NSFileManager defaultManager] moveItemAtPath:fileNameAtPath toPath:fileNameToPath error:&error];
  113. if (error != nil) {
  114. textField.text = fileNameOriginal;
  115. }
  116. } else {
  117. textField.text = fileNameOriginal;
  118. }
  119. [textField endEditing:true];
  120. return false;
  121. }
  122. return true;
  123. }
  124. #pragma --------------------------------------------------------------------------------------------
  125. #pragma mark == Action ==
  126. #pragma --------------------------------------------------------------------------------------------
  127. - (void)navigationBarToolBar
  128. {
  129. // Theming
  130. if ([NCBrandOptions sharedInstance].use_themingColor) {
  131. tableCapabilities *capabilities = [[NCManageDatabase sharedInstance] getCapabilitesWithAccount:self.activeAccount];
  132. [CCGraphics settingThemingColor:capabilities.themingColor themingColorElement:capabilities.themingColorElement themingColorText:capabilities.themingColorText];
  133. }
  134. self.navigationController.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand;
  135. self.navigationController.navigationBar.tintColor = NCBrandColor.sharedInstance.brandText;
  136. self.toolBar.barTintColor = NCBrandColor.sharedInstance.tabBar;
  137. self.toolBar.tintColor = [UIColor grayColor];
  138. // Upload
  139. rightButtonUpload = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"_save_", nil) style:UIBarButtonItemStylePlain target:self action:@selector(selectPost)];
  140. // Cancel
  141. leftButtonCancel = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIBarButtonItemStylePlain target:self action:@selector(cancelPost)];
  142. // Title
  143. [self.navigationController.navigationBar setTitleTextAttributes: @{NSForegroundColorAttributeName:self.navigationController.navigationBar.tintColor}];
  144. self.navigationItem.title = [NCBrandOptions sharedInstance].brand;
  145. self.navigationItem.leftBarButtonItem = leftButtonCancel;
  146. self.navigationItem.rightBarButtonItems = [[NSArray alloc] initWithObjects:rightButtonUpload, nil];
  147. self.navigationItem.hidesBackButton = YES;
  148. }
  149. - (void)moveServerUrlTo:(NSString *)serverUrlTo title:(NSString *)title type:(NSString *)type
  150. {
  151. // DENIED e2e
  152. if ([CCUtility isFolderEncrypted:serverUrlTo account:self.activeAccount]) {
  153. return;
  154. }
  155. if (serverUrlTo)
  156. _serverUrl = serverUrlTo;
  157. if (title) {
  158. self.destinyFolderButton.title = [NSString stringWithFormat:NSLocalizedString(@"_destiny_folder_", nil), title];
  159. [CCUtility setTitleServerUrlExt:title];
  160. } else {
  161. self.destinyFolderButton.title = [NSString stringWithFormat:NSLocalizedString(@"_destiny_folder_", nil), NSLocalizedString(@"_home_", nil)];
  162. [CCUtility setTitleServerUrlExt:NSLocalizedString(@"_home_", nil)];
  163. }
  164. [CCUtility setActiveAccountExt:self.activeAccount];
  165. [CCUtility setServerUrlExt:_serverUrl];
  166. }
  167. - (IBAction)destinyFolderButtonTapped:(UIBarButtonItem *)sender
  168. {
  169. UINavigationController* navigationController = [[UIStoryboard storyboardWithName:@"NCSelectDestination" bundle:nil] instantiateInitialViewController];
  170. NCSelectDestination *viewController = (NCSelectDestination *)navigationController.topViewController;
  171. viewController.delegate = self;
  172. viewController.move.title = NSLocalizedString(@"_select_", nil);
  173. viewController.tintColor = tintColor;
  174. viewController.barTintColor = barTintColor;
  175. viewController.tintColorTitle = tintColor;
  176. // E2EE
  177. viewController.includeDirectoryE2EEncryption = NO;
  178. [navigationController setModalPresentationStyle:UIModalPresentationFormSheet];
  179. [self presentViewController:navigationController animated:YES completion:nil];
  180. }
  181. - (void)selectPost
  182. {
  183. if ([self.filesName count] > 0) {
  184. [self.hud visibleHudTitle:NSLocalizedString(@"_uploading_", nil) mode:MBProgressHUDModeDeterminate color:NCBrandColor.sharedInstance.brandElement];
  185. NSString *fileName = [self.filesName objectAtIndex:0];
  186. NSString *fileNameForUpload = [[NCUtility sharedInstance] createFileName:fileName serverUrl:self.serverUrl account:self.activeAccount];
  187. NSString *fileNameServer = [NSString stringWithFormat:@"%@/%@", self.serverUrl, fileNameForUpload];
  188. NSString *fileNameLocal = [NSTemporaryDirectory() stringByAppendingString:fileName];
  189. (void)[[NCCommunication sharedInstance] uploadWithServerUrlFileName:fileNameServer fileNameLocalPath:fileNameLocal dateCreationFile:nil dateModificationFile:nil account:self.activeAccount progressHandler:^(NSProgress * progress) {
  190. [self.hud progress:progress.fractionCompleted];
  191. } completionHandler:^(NSString *account, NSString *ocId, NSString *etag, NSDate *date, int64_t size, NSInteger errorCode, NSString *errorDescription) {
  192. [self.hud hideHud];
  193. [self.filesName removeObject:fileName];
  194. if (errorCode == 0) {
  195. [CCUtility copyFileAtPath:fileNameLocal toPath:[CCUtility getDirectoryProviderStorageOcId:ocId fileNameView:fileNameForUpload]];
  196. tableMetadata *metadata = [tableMetadata new];
  197. metadata.account = self.activeAccount;
  198. metadata.date = date;
  199. metadata.etag = etag;
  200. metadata.ocId = ocId;
  201. metadata.fileName = fileNameForUpload;
  202. metadata.fileNameView = fileNameForUpload;
  203. metadata.serverUrl = self.serverUrl;
  204. metadata.size = size;
  205. (void)[CCUtility insertTypeFileIconName:fileNameForUpload metadata:metadata];
  206. metadata = [[NCManageDatabase sharedInstance] addMetadata:metadata];
  207. [[NCManageDatabase sharedInstance] addLocalFileWithMetadata:metadata];
  208. [self.shareTable performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
  209. [self performSelector:@selector(selectPost) withObject:nil];
  210. } else {
  211. UIAlertController * alert= [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_", nil) message: errorDescription preferredStyle:UIAlertControllerStyleAlert];
  212. UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
  213. handler:^(UIAlertAction * action) {
  214. [alert dismissViewControllerAnimated:YES completion:nil];
  215. [self closeShareViewController];
  216. }];
  217. [alert addAction:ok];
  218. [self presentViewController:alert animated:YES completion:nil];
  219. }
  220. }];
  221. } else {
  222. [self closeShareViewController];
  223. }
  224. }
  225. - (void)cancelPost
  226. {
  227. // rimuoviamo i file+ico
  228. for (NSString *fileName in self.filesName) {
  229. [[NSFileManager defaultManager] removeItemAtPath:[NSTemporaryDirectory() stringByAppendingString:fileName] error:nil];
  230. }
  231. [self closeShareViewController];
  232. }
  233. #pragma --------------------------------------------------------------------------------------------
  234. #pragma mark ===== Lock Password =====
  235. #pragma --------------------------------------------------------------------------------------------
  236. - (void)openBKPasscode
  237. {
  238. CCBKPasscode *viewController = [[CCBKPasscode alloc] initWithNibName:nil bundle:nil];
  239. viewController.delegate = self;
  240. viewController.type = BKPasscodeViewControllerCheckPasscodeType;
  241. viewController.inputViewTitlePassword = YES;
  242. if ([CCUtility getSimplyBlockCode]) {
  243. viewController.passcodeStyle = BKPasscodeInputViewNumericPasscodeStyle;
  244. viewController.passcodeInputView.maximumLength = 6;
  245. } else {
  246. viewController.passcodeStyle = BKPasscodeInputViewNormalPasscodeStyle;
  247. viewController.passcodeInputView.maximumLength = 64;
  248. }
  249. BKTouchIDManager *touchIDManager = [[BKTouchIDManager alloc] initWithKeychainServiceName:k_serviceShareKeyChain];
  250. touchIDManager.promptText = NSLocalizedString(@"_scan_fingerprint_", nil);
  251. viewController.touchIDManager = touchIDManager;
  252. viewController.title = [NCBrandOptions sharedInstance].brand;
  253. viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(passcodeViewCloseButtonPressed:)];
  254. viewController.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
  255. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  256. navigationController.modalPresentationStyle = UIModalPresentationFullScreen;
  257. [self presentViewController:navigationController animated:YES completion:nil];
  258. }
  259. - (NSUInteger)passcodeViewControllerNumberOfFailedAttempts:(CCBKPasscode *)aViewController
  260. {
  261. return self.failedAttempts;
  262. }
  263. - (NSDate *)passcodeViewControllerLockUntilDate:(CCBKPasscode *)aViewController
  264. {
  265. return self.lockUntilDate;
  266. }
  267. - (void)passcodeViewCloseButtonPressed:(id)sender
  268. {
  269. [self dismissViewControllerAnimated:YES completion:^{
  270. [self performSelector:@selector(closeShareViewController) withObject:nil];
  271. }];
  272. }
  273. - (void)passcodeViewController:(CCBKPasscode *)aViewController authenticatePasscode:(NSString *)aPasscode resultHandler:(void (^)(BOOL))aResultHandler
  274. {
  275. if ([aPasscode isEqualToString:[CCUtility getBlockCode]]) {
  276. self.lockUntilDate = nil;
  277. self.failedAttempts = 0;
  278. aResultHandler(YES);
  279. } else aResultHandler(NO);
  280. }
  281. - (void)passcodeViewController:(CCBKPasscode *)aViewController didFinishWithPasscode:(NSString *)aPasscode
  282. {
  283. [aViewController dismissViewControllerAnimated:YES completion:nil];
  284. }
  285. #pragma --------------------------------------------------------------------------------------------
  286. #pragma mark ===== Swipe Table DELETE -> menu =====
  287. #pragma--------------------------------------------------------------------------------------------
  288. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  289. {
  290. [self setEditing:NO animated:YES];
  291. NSString *fileName = [self.filesName objectAtIndex:indexPath.row];
  292. [[NSFileManager defaultManager] removeItemAtPath:[NSTemporaryDirectory() stringByAppendingString:fileName] error:nil];
  293. [self.filesName removeObjectAtIndex:indexPath.row];
  294. if ([self.filesName count] == 0) [self closeShareViewController];
  295. else [self.shareTable performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
  296. }
  297. #pragma --------------------------------------------------------------------------------------------
  298. #pragma mark == Table ==
  299. #pragma --------------------------------------------------------------------------------------------
  300. - (void)loadDataSwift
  301. {
  302. CCloadItemData *loadItem = [[CCloadItemData alloc] init];
  303. [loadItem loadFiles:NSTemporaryDirectory() extensionContext:self.extensionContext vc:self];
  304. }
  305. - (void)reloadData:(NSArray *)files
  306. {
  307. totalSize = 0;
  308. for (NSString *file in files) {
  309. NSUInteger fileSize = (NSInteger)[[[NSFileManager defaultManager] attributesOfItemAtPath:[NSTemporaryDirectory() stringByAppendingString:file] error:nil] fileSize];
  310. totalSize += fileSize;
  311. }
  312. if (totalSize > 0) {
  313. self.filesName = [[NSMutableArray alloc] initWithArray:files];
  314. [self.shareTable reloadData];
  315. } else {
  316. [self closeShareViewController];
  317. }
  318. }
  319. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  320. {
  321. return 80;
  322. }
  323. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  324. {
  325. return 1;
  326. }
  327. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  328. {
  329. return [self.filesName count];
  330. }
  331. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  332. {
  333. NSString *fileName = [self.filesName objectAtIndex:indexPath.row];
  334. UIImage *image = nil;
  335. CCCellShareExt *cell = (CCCellShareExt *)[tableView dequeueReusableCellWithIdentifier:@"ShareExtCell" forIndexPath:indexPath];
  336. CFStringRef fileExtension = (__bridge CFStringRef)[fileName pathExtension];
  337. CFStringRef fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);
  338. if (UTTypeConformsTo(fileUTI, kUTTypeZipArchive) && [(__bridge NSString *)fileUTI containsString:@"org.openxmlformats"] == NO) image = [UIImage imageNamed:@"file_compress"];
  339. else if (UTTypeConformsTo(fileUTI, kUTTypeAudio)) image = [UIImage imageNamed:@"file_audio"];
  340. else if (UTTypeConformsTo(fileUTI, kUTTypeMovie)) image = [UIImage imageNamed:@"file_movie"];
  341. else if (UTTypeConformsTo(fileUTI, kUTTypeImage)) {
  342. image = [UIImage imageWithContentsOfFile:[NSTemporaryDirectory() stringByAppendingString:fileName]];
  343. if (image) {
  344. image = [NCUtility.sharedInstance resizeImageWithImage:image newWidth:cell.frame.size.width];
  345. } else {
  346. image = [UIImage imageNamed:@"file_photo"];
  347. }
  348. }
  349. else if (UTTypeConformsTo(fileUTI, kUTTypeContent)) {
  350. image = [UIImage imageNamed:@"document"];
  351. NSString *typeFile = (__bridge NSString *)fileUTI;
  352. if ([typeFile isEqualToString:@"com.adobe.pdf"]) image = [UIImage imageNamed:@"file_pdf"];
  353. if ([typeFile isEqualToString:@"org.openxmlformats.spreadsheetml.sheet"]) image = [UIImage imageNamed:@"file_xls"];
  354. if ([typeFile isEqualToString:@"public.plain-text"]) image = [UIImage imageNamed:@"file_txt"];
  355. }
  356. else image = [UIImage imageNamed:@"file"];
  357. NSUInteger fileSize = (NSInteger)[[[NSFileManager defaultManager] attributesOfItemAtPath:[NSTemporaryDirectory() stringByAppendingString:fileName] error:nil] fileSize];
  358. cell.fileImageView.image = image;
  359. cell.fileName.text = fileName;
  360. cell.fileName.textColor = NCBrandColor.sharedInstance.textView;
  361. cell.fileName.delegate = self;
  362. cell.info.text = [CCUtility transformedSize:fileSize];
  363. cell.info.textColor = NCBrandColor.sharedInstance.textView;
  364. return cell;
  365. }
  366. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  367. {
  368. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  369. }
  370. @end