ShareViewController.m 19 KB

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