ShareViewController.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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 ()
  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. _activeAccount = tableAccount.account;
  48. _activePassword = [CCUtility getPassword:tableAccount.account];
  49. _activeUrl = tableAccount.url;
  50. _activeUser = tableAccount.user;
  51. _activeUserID = tableAccount.userID;
  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:self.activeUrl];
  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. // Apparirà
  72. - (void)viewWillAppear:(BOOL)animated
  73. {
  74. [super viewWillAppear:animated];
  75. if ([[CCUtility getBlockCode] length] > 0 && [CCUtility getOnlyLockDir] == NO)
  76. [self openBKPasscode];
  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. #pragma --------------------------------------------------------------------------------------------
  95. #pragma mark == Action ==
  96. #pragma --------------------------------------------------------------------------------------------
  97. - (void)navigationBarToolBar
  98. {
  99. UIBarButtonItem *rightButtonUpload, *leftButtonCancel;
  100. // Theming
  101. if ([NCBrandOptions sharedInstance].use_themingColor) {
  102. tableCapabilities *capabilities = [[NCManageDatabase sharedInstance] getCapabilitesWithAccount:self.activeAccount];
  103. [CCGraphics settingThemingColor:capabilities.themingColor themingColorElement:capabilities.themingColorElement themingColorText:capabilities.themingColorText];
  104. }
  105. self.navigationController.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand;
  106. self.navigationController.navigationBar.tintColor = NCBrandColor.sharedInstance.brandText;
  107. self.toolBar.barTintColor = NCBrandColor.sharedInstance.tabBar;
  108. self.toolBar.tintColor = NCBrandColor.sharedInstance.brandElement;
  109. // Upload
  110. rightButtonUpload = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"_save_", nil) style:UIBarButtonItemStylePlain target:self action:@selector(selectPost)];
  111. // Cancel
  112. leftButtonCancel = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIBarButtonItemStylePlain target:self action:@selector(cancelPost)];
  113. // Title
  114. [self.navigationController.navigationBar setTitleTextAttributes: @{NSForegroundColorAttributeName:self.navigationController.navigationBar.tintColor}];
  115. self.navigationItem.title = [NCBrandOptions sharedInstance].brand;
  116. self.navigationItem.leftBarButtonItem = leftButtonCancel;
  117. self.navigationItem.rightBarButtonItems = [[NSArray alloc] initWithObjects:rightButtonUpload, nil];
  118. self.navigationItem.hidesBackButton = YES;
  119. }
  120. - (void)moveServerUrlTo:(NSString *)serverUrlTo title:(NSString *)title type:(NSString *)type
  121. {
  122. // DENIED e2e
  123. if ([CCUtility isFolderEncrypted:serverUrlTo account:self.activeAccount]) {
  124. return;
  125. }
  126. if (serverUrlTo)
  127. _serverUrl = serverUrlTo;
  128. if (title) {
  129. self.destinyFolderButton.title = [NSString stringWithFormat:NSLocalizedString(@"_destiny_folder_", nil), title];
  130. [CCUtility setTitleServerUrlExt:title];
  131. } else {
  132. self.destinyFolderButton.title = [NSString stringWithFormat:NSLocalizedString(@"_destiny_folder_", nil), NSLocalizedString(@"_home_", nil)];
  133. [CCUtility setTitleServerUrlExt:NSLocalizedString(@"_home_", nil)];
  134. }
  135. [CCUtility setActiveAccountExt:self.activeAccount];
  136. [CCUtility setServerUrlExt:_serverUrl];
  137. }
  138. - (IBAction)destinyFolderButtonTapped:(UIBarButtonItem *)sender
  139. {
  140. UINavigationController* navigationController = [[UIStoryboard storyboardWithName:@"CCMove" bundle:nil] instantiateViewControllerWithIdentifier:@"CCMove"];
  141. CCMove *viewController = (CCMove *)navigationController.topViewController;
  142. viewController.delegate = self;
  143. viewController.move.title = NSLocalizedString(@"_select_", nil);
  144. viewController.tintColor = tintColor;
  145. viewController.barTintColor = barTintColor;
  146. viewController.tintColorTitle = tintColor;
  147. // E2EE
  148. viewController.includeDirectoryE2EEncryption = NO;
  149. [navigationController setModalPresentationStyle:UIModalPresentationFormSheet];
  150. [self presentViewController:navigationController animated:YES completion:nil];
  151. }
  152. - (void)selectPost
  153. {
  154. if ([self.filesName count] > 0) {
  155. [self.hud visibleHudTitle:NSLocalizedString(@"_uploading_", nil) mode:MBProgressHUDModeDeterminate color:NCBrandColor.sharedInstance.brandElement];
  156. NSString *fileName = [self.filesName objectAtIndex:0];
  157. NSString *fileNameForUpload = [[NCUtility sharedInstance] createFileName:fileName serverUrl:self.serverUrl account:self.activeAccount];
  158. NSString *fileNameServer = [NSString stringWithFormat:@"%@/%@", self.serverUrl, fileNameForUpload];
  159. NSString *fileNameLocal = [NSTemporaryDirectory() stringByAppendingString:fileName];
  160. [[OCNetworking sharedManager] uploadWithAccount:self.activeAccount fileNameServerUrl:fileNameServer fileNameLocalPath:fileNameLocal encode:true communication:[[OCNetworking sharedManager] sharedOCCommunication] progress:^(NSProgress *progress) {
  161. dispatch_async(dispatch_get_main_queue(), ^{
  162. [self.hud progress:progress.fractionCompleted];
  163. });
  164. } completion:^(NSString *account, NSString *ocId, NSString *etag, NSDate *date, NSString *message, NSInteger errorCode) {
  165. [self.hud hideHud];
  166. [self.filesName removeObject:fileName];
  167. if (errorCode == 0) {
  168. [CCUtility copyFileAtPath:fileNameLocal toPath:[CCUtility getDirectoryProviderStorageOcId:ocId fileNameView:fileNameForUpload]];
  169. tableMetadata *metadata = [tableMetadata new];
  170. metadata.account = self.activeAccount;
  171. metadata.date = date;
  172. metadata.etag = etag;
  173. metadata.ocId = ocId;
  174. metadata.fileName = fileNameForUpload;
  175. metadata.fileNameView = fileNameForUpload;
  176. metadata.serverUrl = self.serverUrl;
  177. (void)[CCUtility insertTypeFileIconName:fileNameForUpload metadata:metadata];
  178. metadata = [[NCManageDatabase sharedInstance] addMetadata:metadata];
  179. [[NCManageDatabase sharedInstance] addLocalFileWithMetadata:metadata];
  180. [self.shareTable performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
  181. [self performSelector:@selector(selectPost) withObject:nil];
  182. } else {
  183. UIAlertController * alert= [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_", nil) message:message preferredStyle:UIAlertControllerStyleAlert];
  184. UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
  185. handler:^(UIAlertAction * action) {
  186. [alert dismissViewControllerAnimated:YES completion:nil];
  187. [self closeShareViewController];
  188. }];
  189. [alert addAction:ok];
  190. [self presentViewController:alert animated:YES completion:nil];
  191. }
  192. }];
  193. } else {
  194. [self closeShareViewController];
  195. }
  196. }
  197. - (void)cancelPost
  198. {
  199. // rimuoviamo i file+ico
  200. for (NSString *fileName in self.filesName) {
  201. [[NSFileManager defaultManager] removeItemAtPath:[NSTemporaryDirectory() stringByAppendingString:fileName] error:nil];
  202. }
  203. [self closeShareViewController];
  204. }
  205. #pragma --------------------------------------------------------------------------------------------
  206. #pragma mark ===== Lock Password =====
  207. #pragma --------------------------------------------------------------------------------------------
  208. - (void)openBKPasscode
  209. {
  210. CCBKPasscode *viewController = [[CCBKPasscode alloc] initWithNibName:nil bundle:nil];
  211. viewController.delegate = self;
  212. viewController.type = BKPasscodeViewControllerCheckPasscodeType;
  213. viewController.inputViewTitlePassword = YES;
  214. if ([CCUtility getSimplyBlockCode]) {
  215. viewController.passcodeStyle = BKPasscodeInputViewNumericPasscodeStyle;
  216. viewController.passcodeInputView.maximumLength = 6;
  217. } else {
  218. viewController.passcodeStyle = BKPasscodeInputViewNormalPasscodeStyle;
  219. viewController.passcodeInputView.maximumLength = 64;
  220. }
  221. BKTouchIDManager *touchIDManager = [[BKTouchIDManager alloc] initWithKeychainServiceName:k_serviceShareKeyChain];
  222. touchIDManager.promptText = NSLocalizedString(@"_scan_fingerprint_", nil);
  223. viewController.touchIDManager = touchIDManager;
  224. viewController.title = [NCBrandOptions sharedInstance].brand;
  225. viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(passcodeViewCloseButtonPressed:)];
  226. viewController.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
  227. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  228. navigationController.modalPresentationStyle = UIModalPresentationFullScreen;
  229. [self presentViewController:navigationController animated:YES completion:nil];
  230. }
  231. - (NSUInteger)passcodeViewControllerNumberOfFailedAttempts:(CCBKPasscode *)aViewController
  232. {
  233. return self.failedAttempts;
  234. }
  235. - (NSDate *)passcodeViewControllerLockUntilDate:(CCBKPasscode *)aViewController
  236. {
  237. return self.lockUntilDate;
  238. }
  239. - (void)passcodeViewCloseButtonPressed:(id)sender
  240. {
  241. [self dismissViewControllerAnimated:YES completion:^{
  242. [self performSelector:@selector(closeShareViewController) withObject:nil];
  243. }];
  244. }
  245. - (void)passcodeViewController:(CCBKPasscode *)aViewController authenticatePasscode:(NSString *)aPasscode resultHandler:(void (^)(BOOL))aResultHandler
  246. {
  247. if ([aPasscode isEqualToString:[CCUtility getBlockCode]]) {
  248. self.lockUntilDate = nil;
  249. self.failedAttempts = 0;
  250. aResultHandler(YES);
  251. } else aResultHandler(NO);
  252. }
  253. - (void)passcodeViewController:(CCBKPasscode *)aViewController didFinishWithPasscode:(NSString *)aPasscode
  254. {
  255. [aViewController dismissViewControllerAnimated:YES completion:nil];
  256. }
  257. #pragma --------------------------------------------------------------------------------------------
  258. #pragma mark ===== Swipe Table DELETE -> menu =====
  259. #pragma--------------------------------------------------------------------------------------------
  260. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  261. {
  262. [self setEditing:NO animated:YES];
  263. NSString *fileName = [self.filesName objectAtIndex:indexPath.row];
  264. [[NSFileManager defaultManager] removeItemAtPath:[NSTemporaryDirectory() stringByAppendingString:fileName] error:nil];
  265. [self.filesName removeObjectAtIndex:indexPath.row];
  266. if ([self.filesName count] == 0) [self closeShareViewController];
  267. else [self.shareTable performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
  268. }
  269. #pragma --------------------------------------------------------------------------------------------
  270. #pragma mark == Table ==
  271. #pragma --------------------------------------------------------------------------------------------
  272. - (void)loadDataSwift
  273. {
  274. CCloadItemData *loadItem = [[CCloadItemData alloc] init];
  275. [loadItem loadFiles:NSTemporaryDirectory() extensionContext:self.extensionContext vc:self];
  276. }
  277. - (void)reloadData:(NSArray *)files
  278. {
  279. totalSize = 0;
  280. for (NSString *file in files) {
  281. NSUInteger fileSize = (NSInteger)[[[NSFileManager defaultManager] attributesOfItemAtPath:[NSTemporaryDirectory() stringByAppendingString:file] error:nil] fileSize];
  282. totalSize += fileSize;
  283. }
  284. if (totalSize > 0) {
  285. self.filesName = [[NSMutableArray alloc] initWithArray:files];
  286. [self.shareTable reloadData];
  287. } else {
  288. [self closeShareViewController];
  289. }
  290. }
  291. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  292. {
  293. return 80;
  294. }
  295. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  296. {
  297. return 1;
  298. }
  299. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  300. {
  301. return [self.filesName count];
  302. }
  303. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  304. {
  305. NSString *fileName = [self.filesName objectAtIndex:indexPath.row];
  306. UIImage *image = nil;
  307. CCCellShareExt *cell = (CCCellShareExt *)[tableView dequeueReusableCellWithIdentifier:@"ShareExtCell" forIndexPath:indexPath];
  308. CFStringRef fileExtension = (__bridge CFStringRef)[fileName pathExtension];
  309. CFStringRef fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);
  310. if (UTTypeConformsTo(fileUTI, kUTTypeZipArchive) && [(__bridge NSString *)fileUTI containsString:@"org.openxmlformats"] == NO) image = [UIImage imageNamed:@"file_compress"];
  311. else if (UTTypeConformsTo(fileUTI, kUTTypeAudio)) image = [UIImage imageNamed:@"file_audio"];
  312. else if (UTTypeConformsTo(fileUTI, kUTTypeMovie)) image = [UIImage imageNamed:@"file_movie"];
  313. else if (UTTypeConformsTo(fileUTI, kUTTypeImage)) {
  314. image = [UIImage imageWithContentsOfFile:[NSTemporaryDirectory() stringByAppendingString:fileName]];
  315. if (image) {
  316. image = [NCUtility.sharedInstance resizeImageWithImage:image newWidth:cell.frame.size.width];
  317. } else {
  318. image = [UIImage imageNamed:@"file_photo"];
  319. }
  320. }
  321. else if (UTTypeConformsTo(fileUTI, kUTTypeContent)) {
  322. image = [UIImage imageNamed:@"document"];
  323. NSString *typeFile = (__bridge NSString *)fileUTI;
  324. if ([typeFile isEqualToString:@"com.adobe.pdf"]) image = [UIImage imageNamed:@"file_pdf"];
  325. if ([typeFile isEqualToString:@"org.openxmlformats.spreadsheetml.sheet"]) image = [UIImage imageNamed:@"file_xls"];
  326. if ([typeFile isEqualToString:@"public.plain-text"]) image = [UIImage imageNamed:@"file_txt"];
  327. }
  328. else image = [UIImage imageNamed:@"file"];
  329. NSUInteger fileSize = (NSInteger)[[[NSFileManager defaultManager] attributesOfItemAtPath:[NSTemporaryDirectory() stringByAppendingString:fileName] error:nil] fileSize];
  330. cell.labelInformazioni.text = [NSString stringWithFormat:@"%@\r\r%@", fileName, [CCUtility transformedSize:fileSize]];
  331. cell.labelInformazioni.textColor = [UIColor blackColor];
  332. cell.fileImageView.image = image;
  333. return cell;
  334. }
  335. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  336. {
  337. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  338. }
  339. @end