NCSelectDestination.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. //
  2. // CCMove.m
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 04/09/14.
  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 "NCSelectDestination.h"
  24. #import "NCBridgeSwift.h"
  25. @interface NCSelectDestination ()
  26. {
  27. NSString *activeAccount;
  28. NSString *activeUrl;
  29. BOOL _loadingFolder;
  30. // Automatic Upload Folder
  31. NSString *_autoUploadFileName;
  32. NSString *_autoUploadDirectory;
  33. NSPredicate *predicateDataSource;
  34. }
  35. @end
  36. @implementation NCSelectDestination
  37. // MARK: - View
  38. - (void)viewDidLoad
  39. {
  40. [super viewDidLoad];
  41. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  42. if (tableAccount) {
  43. activeAccount = tableAccount.account;
  44. activeUrl = tableAccount.url;
  45. } else {
  46. UIAlertController * alert= [UIAlertController alertControllerWithTitle:nil message:NSLocalizedString(@"_no_active_account_", nil) preferredStyle:UIAlertControllerStyleAlert];
  47. UIAlertAction* ok = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
  48. [alert dismissViewControllerAnimated:YES completion:nil];
  49. }];
  50. [alert addAction:ok];
  51. [self presentViewController:alert animated:YES completion:nil];
  52. }
  53. [self.cancel setTitle:NSLocalizedString(@"_cancel_", nil)];
  54. [self.create setTitle:NSLocalizedString(@"_create_folder_", nil)];
  55. if (![_serverUrl length]) {
  56. _serverUrl = [CCUtility getHomeServerUrlActiveUrl:activeUrl];
  57. [self.navigationController.navigationBar.topItem setTitleView:[[UIImageView alloc] initWithImage: [UIImage imageNamed:@"themingLogo"]]];
  58. self.title = @"Home";
  59. } else {
  60. UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0,0, self.navigationItem.titleView.frame.size.width, 40)];
  61. label.text = self.passMetadata.fileNameView;
  62. label.textColor = NCBrandColor.sharedInstance.brandText;
  63. label.backgroundColor =[UIColor clearColor];
  64. label.textAlignment = NSTextAlignmentCenter;
  65. self.navigationItem.titleView=label;
  66. }
  67. // TableView : at the end of rows nothing
  68. self.tableView.tableFooterView = [UIView new];
  69. self.tableView.separatorColor = NCBrandColor.sharedInstance.separator;
  70. // get auto upload folder
  71. _autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  72. _autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:activeUrl];
  73. [self readFolder];
  74. }
  75. // Apparirà
  76. - (void)viewWillAppear:(BOOL)animated
  77. {
  78. [super viewWillAppear:animated];
  79. self.navigationController.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand;
  80. self.navigationController.navigationBar.tintColor = NCBrandColor.sharedInstance.brandText;
  81. self.navigationController.toolbar.barTintColor = NCBrandColor.sharedInstance.tabBar;
  82. self.navigationController.toolbar.tintColor = [UIColor grayColor];
  83. if (self.hideCreateFolder) {
  84. [self.create setEnabled:NO];
  85. [self.create setTintColor: [UIColor clearColor]];
  86. }
  87. if (self.hideMoveutton) {
  88. [self.move setEnabled:NO];
  89. [self.move setTintColor: [UIColor clearColor]];
  90. }
  91. self.view.backgroundColor = NCBrandColor.sharedInstance.backgroundView;
  92. self.tableView.backgroundColor = NCBrandColor.sharedInstance.backgroundView;
  93. }
  94. // MARK: - IBAction
  95. - (IBAction)cancel:(UIBarButtonItem *)sender
  96. {
  97. if ([self.delegate respondsToSelector:@selector(dismissMove)])
  98. [self.delegate dismissMove];
  99. [self dismissViewControllerAnimated:YES completion:nil];
  100. }
  101. - (IBAction)move:(UIBarButtonItem *)sender
  102. {
  103. if ([self.delegate respondsToSelector:@selector(dismissMove)])
  104. [self.delegate dismissMove];
  105. [self.delegate moveServerUrlTo:_serverUrl title:self.passMetadata.fileNameView type:self.type];
  106. [self dismissViewControllerAnimated:YES completion:nil];
  107. }
  108. - (IBAction)create:(UIBarButtonItem *)sender
  109. {
  110. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_create_folder_",nil) message:@"" preferredStyle:UIAlertControllerStyleAlert];
  111. [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
  112. //textField.placeholder = NSLocalizedString(@"LoginPlaceholder", @"Login");
  113. textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
  114. }];
  115. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_save_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  116. [self createFolder:alertController.textFields.firstObject.text];
  117. }]];
  118. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  119. }]];
  120. [self presentViewController:alertController animated:YES completion:nil];
  121. }
  122. // MARK: - BKPasscodeViewController
  123. - (void)passcodeViewController:(CCBKPasscode *)aViewController didFinishWithPasscode:(NSString *)aPasscode
  124. {
  125. [aViewController dismissViewControllerAnimated:YES completion:nil];
  126. [self performSegueDirectoryWithControlPasscode:false];
  127. }
  128. - (void)passcodeViewController:(BKPasscodeViewController *)aViewController authenticatePasscode:(NSString *)aPasscode resultHandler:(void (^)(BOOL))aResultHandler
  129. {
  130. if ([aPasscode isEqualToString:[CCUtility getBlockCode]]) {
  131. self.lockUntilDate = nil;
  132. self.failedAttempts = 0;
  133. aResultHandler(YES);
  134. } else {
  135. aResultHandler(NO);
  136. }
  137. }
  138. - (void)passcodeViewControllerDidFailAttempt:(BKPasscodeViewController *)aViewController
  139. {
  140. self.failedAttempts++;
  141. if (self.failedAttempts > 5) {
  142. NSTimeInterval timeInterval = 60;
  143. if (self.failedAttempts > 6) {
  144. NSUInteger multiplier = self.failedAttempts - 6;
  145. timeInterval = (5 * 60) * multiplier;
  146. if (timeInterval > 3600 * 24) {
  147. timeInterval = 3600 * 24;
  148. }
  149. }
  150. self.lockUntilDate = [NSDate dateWithTimeIntervalSinceNow:timeInterval];
  151. }
  152. }
  153. - (NSUInteger)passcodeViewControllerNumberOfFailedAttempts:(BKPasscodeViewController *)aViewController
  154. {
  155. return self.failedAttempts;
  156. }
  157. - (NSDate *)passcodeViewControllerLockUntilDate:(BKPasscodeViewController *)aViewController
  158. {
  159. return self.lockUntilDate;
  160. }
  161. - (void)passcodeViewCloseButtonPressed:(id)sender
  162. {
  163. [self dismissViewControllerAnimated:YES completion:nil];
  164. }
  165. // MARK: - Read Folder
  166. - (void)readFolder
  167. {
  168. [[NCNetworking sharedInstance] readFolderWithServerUrl:_serverUrl account:activeAccount completion:^(NSString *account, tableMetadata *metadataFolder, NSArray *metadatas, NSInteger errorCode, NSString *errorDescription) {
  169. if (errorCode == 0) {
  170. self.move.enabled = true;
  171. } else {
  172. self.move.enabled = false;
  173. }
  174. _loadingFolder = NO;
  175. [self.tableView reloadData];
  176. }];
  177. _loadingFolder = YES;
  178. [self.tableView reloadData];
  179. }
  180. // MARK: - Create Folder
  181. - (void)createFolder:(NSString *)fileNameFolder
  182. {
  183. NSString *serverUrlFileName = [NSString stringWithFormat:@"%@/%@", _serverUrl, fileNameFolder];
  184. [[NCCommunication sharedInstance] createFolder:serverUrlFileName customUserAgent:nil addCustomHeaders:nil account:activeAccount completionHandler:^(NSString *account, NSString *ocID, NSDate *date, NSInteger errorCode, NSString *errorDecription) {
  185. if (errorCode == 0) {
  186. [self readFolder];
  187. } else {
  188. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_",nil) message:errorDecription preferredStyle:UIAlertControllerStyleAlert];
  189. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { }]];
  190. [self presentViewController:alertController animated:YES completion:nil];
  191. }
  192. }];
  193. }
  194. // MARK: - Table
  195. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  196. {
  197. return 1;
  198. }
  199. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  200. {
  201. if (self.includeDirectoryE2EEncryption) {
  202. if (self.includeImages) {
  203. predicateDataSource = [NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND (directory == true OR typeFile == 'image')", activeAccount, _serverUrl];
  204. } else {
  205. predicateDataSource = [NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND directory == true", activeAccount, _serverUrl];
  206. }
  207. } else {
  208. if (self.includeImages) {
  209. predicateDataSource = [NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND e2eEncrypted == false AND (directory == true OR typeFile == 'image')", activeAccount, _serverUrl];
  210. } else {
  211. predicateDataSource = [NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND e2eEncrypted == false AND directory == true", activeAccount, _serverUrl];
  212. }
  213. }
  214. NSArray *result = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:predicateDataSource sorted:nil ascending:NO];
  215. if (result)
  216. return [result count];
  217. else
  218. return 0;
  219. }
  220. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  221. {
  222. static NSString *CellIdentifier = @"MyCustomCell";
  223. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  224. if (cell == nil) {
  225. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  226. }
  227. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataAtIndexWithPredicate:predicateDataSource sorted:@"fileName" ascending:YES index:indexPath.row];
  228. // Create Directory Provider Storage ocId
  229. [CCUtility getDirectoryProviderStorageOcId:metadata.ocId];
  230. // colors
  231. cell.textLabel.textColor = NCBrandColor.sharedInstance.textView;
  232. cell.detailTextLabel.text = @"";
  233. if (metadata.directory) {
  234. if (metadata.e2eEncrypted)
  235. cell.imageView.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folderEncrypted"] multiplier:2 color:NCBrandColor.sharedInstance.brandElement];
  236. else if ([metadata.fileName isEqualToString:_autoUploadFileName] && [self.serverUrl isEqualToString:_autoUploadDirectory])
  237. cell.imageView.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folderAutomaticUpload"] multiplier:2 color:NCBrandColor.sharedInstance.brandElement];
  238. else
  239. cell.imageView.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder"] multiplier:2 color:NCBrandColor.sharedInstance.brandElement];
  240. } else {
  241. UIImage *thumb = [UIImage imageWithContentsOfFile:[CCUtility getDirectoryProviderStorageIconOcId:metadata.ocId fileNameView:metadata.fileNameView]];
  242. if (thumb) {
  243. cell.imageView.image = thumb;
  244. } else {
  245. if (metadata.iconName.length > 0) {
  246. cell.imageView.image = [UIImage imageNamed:metadata.iconName];
  247. } else {
  248. cell.imageView.image = [UIImage imageNamed:@"file"];
  249. }
  250. }
  251. }
  252. cell.textLabel.text = metadata.fileNameView;
  253. cell.accessoryType = UITableViewCellAccessoryNone;
  254. return cell;
  255. }
  256. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  257. {
  258. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataAtIndexWithPredicate:predicateDataSource sorted:@"fileName" ascending:YES index:indexPath.row];
  259. if (metadata.directory) {
  260. [self performSegueDirectoryWithControlPasscode:YES];
  261. } else {
  262. if (self.selectFile) {
  263. if ([self.delegate respondsToSelector:@selector(dismissMove)])
  264. [self.delegate dismissMove];
  265. if ([self.delegate respondsToSelector:@selector(selectMetadata:serverUrl:)])
  266. [self.delegate selectMetadata:metadata serverUrl:_serverUrl];
  267. [self dismissViewControllerAnimated:YES completion:nil];
  268. }
  269. }
  270. }
  271. // MARK: - Navigation
  272. - (void)performSegueDirectoryWithControlPasscode:(BOOL)controlPasscode
  273. {
  274. NSString *nomeDir;
  275. NSIndexPath *index = [self.tableView indexPathForSelectedRow];
  276. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataAtIndexWithPredicate:predicateDataSource sorted:@"fileName" ascending:YES index:index.row];
  277. // lockServerUrl
  278. NSString *lockServerUrl = [CCUtility stringAppendServerUrl:_serverUrl addFileName:metadata.fileName];
  279. // Se siamo in presenza di una directory bloccata E è attivo il block E la sessione PASSWORD Lock è senza data ALLORA chiediamo la password per procedere
  280. tableDirectory *directory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", activeAccount, lockServerUrl]];
  281. if (directory.lock && [[CCUtility getBlockCode] length] && controlPasscode) {
  282. CCBKPasscode *viewController = [[CCBKPasscode alloc] initWithNibName:nil bundle:nil];
  283. viewController.delegate = self;
  284. //viewController.fromType = CCBKPasscodeFromLockDirectory;
  285. viewController.type = BKPasscodeViewControllerCheckPasscodeType;
  286. viewController.inputViewTitlePassword = YES;
  287. if ([CCUtility getSimplyBlockCode]) {
  288. viewController.passcodeStyle = BKPasscodeInputViewNumericPasscodeStyle;
  289. viewController.passcodeInputView.maximumLength = 6;
  290. } else {
  291. viewController.passcodeStyle = BKPasscodeInputViewNormalPasscodeStyle;
  292. viewController.passcodeInputView.maximumLength = 64;
  293. }
  294. BKTouchIDManager *touchIDManager = [[BKTouchIDManager alloc] initWithKeychainServiceName:k_serviceShareKeyChain];
  295. touchIDManager.promptText = NSLocalizedString(@"_scan_fingerprint_", nil);
  296. viewController.touchIDManager = touchIDManager;
  297. viewController.title = NSLocalizedString(@"_folder_blocked_", nil);
  298. viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(passcodeViewCloseButtonPressed:)];
  299. viewController.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
  300. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  301. navigationController.modalPresentationStyle = UIModalPresentationFullScreen;
  302. [self presentViewController:navigationController animated:YES completion:nil];
  303. return;
  304. }
  305. nomeDir = metadata.fileName;
  306. UINavigationController* navigationController = [[UIStoryboard storyboardWithName:@"NCSelectDestination" bundle:nil] instantiateInitialViewController];
  307. NCSelectDestination *viewController = (NCSelectDestination *)navigationController.topViewController;
  308. viewController.delegate = self.delegate;
  309. viewController.includeDirectoryE2EEncryption = self.includeDirectoryE2EEncryption;
  310. viewController.includeImages = self.includeImages;
  311. viewController.move.title = self.move.title;
  312. viewController.hideCreateFolder = self.hideCreateFolder;
  313. viewController.hideMoveutton = self.hideMoveutton;
  314. viewController.selectFile = self.selectFile;
  315. viewController.type = self.type;
  316. viewController.passMetadata = metadata;
  317. viewController.serverUrl = [CCUtility stringAppendServerUrl:_serverUrl addFileName:nomeDir];
  318. [self.navigationController pushViewController:viewController animated:YES];
  319. }
  320. @end