NCSelectDestination.m 17 KB

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