NCSelectDestination.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. [[NCCommunication sharedInstance] readFileOrFolderWithServerUrlFileName:_serverUrl depth:@"1" showHiddenFiles:[CCUtility getShowHiddenFiles] account:activeAccount completionHandler:^(NSString *account, NSArray<NCFile *> *files, NSInteger errorCode, NSString *errorDecription) {
  169. if (errorCode == 0 && files.count >= 1) {
  170. NCFile *fileDirectory = files[0];
  171. // Update directory etag
  172. [[NCManageDatabase sharedInstance] setDirectoryWithServerUrl:_serverUrl serverUrlTo:nil etag:fileDirectory.etag ocId:fileDirectory.ocId encrypted:fileDirectory.e2eEncrypted richWorkspace:nil account:account];
  173. // Delete metadata
  174. [[NCManageDatabase sharedInstance] deleteMetadataWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND status == %d", account, _serverUrl, k_metadataStatusNormal]];
  175. // In download
  176. NSArray *metadatasInDownload = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND (status == %d OR status == %d OR status == %d OR status == %d)", account, _serverUrl, k_metadataStatusWaitDownload, k_metadataStatusInDownload, k_metadataStatusDownloading, k_metadataStatusDownloadError] sorted:nil ascending:NO];
  177. // Insert in Database
  178. [[NCManageDatabase sharedInstance] addMetadatasWithFiles:files account:account serverUrl:_serverUrl removeFirst:true];
  179. // reinsert metadatas in Download
  180. if (metadatasInDownload) {
  181. [[NCManageDatabase sharedInstance] addMetadatas:metadatasInDownload];
  182. }
  183. } else {
  184. self.move.enabled = NO;
  185. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_",nil) message:errorDecription preferredStyle:UIAlertControllerStyleAlert];
  186. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  187. }]];
  188. [self presentViewController:alertController animated:YES completion:nil];
  189. }
  190. _loadingFolder = NO;
  191. [self.tableView reloadData];
  192. }];
  193. _loadingFolder = YES;
  194. [self.tableView reloadData];
  195. }
  196. // MARK: - Create Folder
  197. - (void)createFolder:(NSString *)fileNameFolder
  198. {
  199. NSString *serverUrlFileName = [NSString stringWithFormat:@"%@/%@", _serverUrl, fileNameFolder];
  200. [[NCCommunication sharedInstance] createFolder:serverUrlFileName account:activeAccount completionHandler:^(NSString *account, NSString *ocID, NSDate *date, NSInteger errorCode, NSString *errorDecription) {
  201. if (errorCode == 0) {
  202. [self readFolder];
  203. } else {
  204. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_",nil) message:errorDecription preferredStyle:UIAlertControllerStyleAlert];
  205. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { }]];
  206. [self presentViewController:alertController animated:YES completion:nil];
  207. }
  208. }];
  209. }
  210. // MARK: - Table
  211. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  212. {
  213. return 1;
  214. }
  215. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  216. {
  217. if (self.includeDirectoryE2EEncryption) {
  218. if (self.includeImages) {
  219. predicateDataSource = [NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND (directory == true OR typeFile == 'image')", activeAccount, _serverUrl];
  220. } else {
  221. predicateDataSource = [NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND directory == true", activeAccount, _serverUrl];
  222. }
  223. } else {
  224. if (self.includeImages) {
  225. predicateDataSource = [NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND e2eEncrypted == false AND (directory == true OR typeFile == 'image')", activeAccount, _serverUrl];
  226. } else {
  227. predicateDataSource = [NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND e2eEncrypted == false AND directory == true", activeAccount, _serverUrl];
  228. }
  229. }
  230. NSArray *result = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:predicateDataSource sorted:nil ascending:NO];
  231. if (result)
  232. return [result count];
  233. else
  234. return 0;
  235. }
  236. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  237. {
  238. static NSString *CellIdentifier = @"MyCustomCell";
  239. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  240. if (cell == nil) {
  241. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  242. }
  243. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataAtIndexWithPredicate:predicateDataSource sorted:@"fileName" ascending:YES index:indexPath.row];
  244. // Create Directory Provider Storage ocId
  245. [CCUtility getDirectoryProviderStorageOcId:metadata.ocId];
  246. // colors
  247. cell.textLabel.textColor = NCBrandColor.sharedInstance.textView;
  248. cell.detailTextLabel.text = @"";
  249. if (metadata.directory) {
  250. if (metadata.e2eEncrypted)
  251. cell.imageView.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folderEncrypted"] multiplier:2 color:NCBrandColor.sharedInstance.brandElement];
  252. else if ([metadata.fileName isEqualToString:_autoUploadFileName] && [self.serverUrl isEqualToString:_autoUploadDirectory])
  253. cell.imageView.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folderAutomaticUpload"] multiplier:2 color:NCBrandColor.sharedInstance.brandElement];
  254. else
  255. cell.imageView.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder"] multiplier:2 color:NCBrandColor.sharedInstance.brandElement];
  256. } else {
  257. UIImage *thumb = [UIImage imageWithContentsOfFile:[CCUtility getDirectoryProviderStorageIconOcId:metadata.ocId fileNameView:metadata.fileNameView]];
  258. if (thumb) {
  259. cell.imageView.image = thumb;
  260. } else {
  261. if (metadata.iconName.length > 0) {
  262. cell.imageView.image = [UIImage imageNamed:metadata.iconName];
  263. } else {
  264. cell.imageView.image = [UIImage imageNamed:@"file"];
  265. }
  266. }
  267. }
  268. cell.textLabel.text = metadata.fileNameView;
  269. cell.accessoryType = UITableViewCellAccessoryNone;
  270. return cell;
  271. }
  272. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  273. {
  274. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataAtIndexWithPredicate:predicateDataSource sorted:@"fileName" ascending:YES index:indexPath.row];
  275. if (metadata.directory) {
  276. [self performSegueDirectoryWithControlPasscode:YES];
  277. } else {
  278. if (self.selectFile) {
  279. if ([self.delegate respondsToSelector:@selector(dismissMove)])
  280. [self.delegate dismissMove];
  281. if ([self.delegate respondsToSelector:@selector(selectMetadata:serverUrl:)])
  282. [self.delegate selectMetadata:metadata serverUrl:_serverUrl];
  283. [self dismissViewControllerAnimated:YES completion:nil];
  284. }
  285. }
  286. }
  287. // MARK: - Navigation
  288. - (void)performSegueDirectoryWithControlPasscode:(BOOL)controlPasscode
  289. {
  290. NSString *nomeDir;
  291. NSIndexPath *index = [self.tableView indexPathForSelectedRow];
  292. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataAtIndexWithPredicate:predicateDataSource sorted:@"fileName" ascending:YES index:index.row];
  293. // lockServerUrl
  294. NSString *lockServerUrl = [CCUtility stringAppendServerUrl:_serverUrl addFileName:metadata.fileName];
  295. // 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
  296. tableDirectory *directory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", activeAccount, lockServerUrl]];
  297. if (directory.lock && [[CCUtility getBlockCode] length] && controlPasscode) {
  298. CCBKPasscode *viewController = [[CCBKPasscode alloc] initWithNibName:nil bundle:nil];
  299. viewController.delegate = self;
  300. //viewController.fromType = CCBKPasscodeFromLockDirectory;
  301. viewController.type = BKPasscodeViewControllerCheckPasscodeType;
  302. viewController.inputViewTitlePassword = YES;
  303. if ([CCUtility getSimplyBlockCode]) {
  304. viewController.passcodeStyle = BKPasscodeInputViewNumericPasscodeStyle;
  305. viewController.passcodeInputView.maximumLength = 6;
  306. } else {
  307. viewController.passcodeStyle = BKPasscodeInputViewNormalPasscodeStyle;
  308. viewController.passcodeInputView.maximumLength = 64;
  309. }
  310. BKTouchIDManager *touchIDManager = [[BKTouchIDManager alloc] initWithKeychainServiceName:k_serviceShareKeyChain];
  311. touchIDManager.promptText = NSLocalizedString(@"_scan_fingerprint_", nil);
  312. viewController.touchIDManager = touchIDManager;
  313. viewController.title = NSLocalizedString(@"_folder_blocked_", nil);
  314. viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(passcodeViewCloseButtonPressed:)];
  315. viewController.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
  316. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  317. navigationController.modalPresentationStyle = UIModalPresentationFullScreen;
  318. [self presentViewController:navigationController animated:YES completion:nil];
  319. return;
  320. }
  321. nomeDir = metadata.fileName;
  322. UINavigationController* navigationController = [[UIStoryboard storyboardWithName:@"NCSelectDestination" bundle:nil] instantiateInitialViewController];
  323. NCSelectDestination *viewController = (NCSelectDestination *)navigationController.topViewController;
  324. viewController.delegate = self.delegate;
  325. viewController.includeDirectoryE2EEncryption = self.includeDirectoryE2EEncryption;
  326. viewController.includeImages = self.includeImages;
  327. viewController.move.title = self.move.title;
  328. viewController.hideCreateFolder = self.hideCreateFolder;
  329. viewController.hideMoveutton = self.hideMoveutton;
  330. viewController.selectFile = self.selectFile;
  331. viewController.type = self.type;
  332. viewController.passMetadata = metadata;
  333. viewController.serverUrl = [CCUtility stringAppendServerUrl:_serverUrl addFileName:nomeDir];
  334. [self.navigationController pushViewController:viewController animated:YES];
  335. }
  336. @end