CCMove.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. //
  2. // CCMove.m
  3. // Nextcloud iOS
  4. //
  5. // Created by Marino Faggiana on 04/09/14.
  6. // Copyright (c) 2017 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  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 "CCMove.h"
  24. #import "NCBridgeSwift.h"
  25. @interface CCMove ()
  26. {
  27. NSString *activeAccount;
  28. NSString *activePassword;
  29. NSString *activeUrl;
  30. NSString *activeUser;
  31. NSString *directoryUser;
  32. BOOL _loadingFolder;
  33. }
  34. @end
  35. @implementation CCMove
  36. // MARK: - View
  37. - (void)viewDidLoad
  38. {
  39. [super viewDidLoad];
  40. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  41. if (recordAccount) {
  42. activeAccount = recordAccount.account;
  43. activePassword = recordAccount.password;
  44. activeUrl = recordAccount.url;
  45. activeUser = recordAccount.user;
  46. directoryUser = [CCUtility getDirectoryActiveUser:activeUser activeUrl:activeUrl];
  47. } else {
  48. UIAlertController * alert= [UIAlertController alertControllerWithTitle:nil message:NSLocalizedString(@"_no_active_account_", nil) preferredStyle:UIAlertControllerStyleAlert];
  49. UIAlertAction* ok = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
  50. [alert dismissViewControllerAnimated:YES completion:nil];
  51. }];
  52. [alert addAction:ok];
  53. [self presentViewController:alert animated:YES completion:nil];
  54. }
  55. [self.cancel setTitle:NSLocalizedString(@"_cancel_", nil)];
  56. [self.create setTitle:NSLocalizedString(@"_create_folder_", nil)];
  57. if (![_serverUrl length]) {
  58. _serverUrl = [CCUtility getHomeServerUrlActiveUrl:activeUrl];
  59. UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed: @"navigationLogo"]];
  60. [self.navigationController.navigationBar.topItem setTitleView:image];
  61. self.title = @"Home";
  62. } else {
  63. UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0,0, self.navigationItem.titleView.frame.size.width, 40)];
  64. label.text = self.passMetadata.fileName;
  65. label.textColor = NCBrandColor.sharedInstance.navigationBarText;
  66. label.backgroundColor =[UIColor clearColor];
  67. label.textAlignment = NSTextAlignmentCenter;
  68. self.navigationItem.titleView=label;
  69. }
  70. // TableView : at the end of rows nothing
  71. self.tableView.tableFooterView = [UIView new];
  72. self.tableView.separatorColor = NCBrandColor.sharedInstance.seperator;
  73. self.tableView.emptyDataSetDelegate = self;
  74. self.tableView.emptyDataSetSource = self;
  75. // read file->folder
  76. [self readFileReloadFolder];
  77. }
  78. // Apparirà
  79. - (void)viewWillAppear:(BOOL)animated
  80. {
  81. [super viewWillAppear:animated];
  82. self.navigationController.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand;
  83. self.navigationController.navigationBar.tintColor = NCBrandColor.sharedInstance.navigationBarText;
  84. self.navigationController.toolbar.barTintColor = NCBrandColor.sharedInstance.tabBar;
  85. self.navigationController.toolbar.tintColor = NCBrandColor.sharedInstance.brand;
  86. }
  87. #pragma --------------------------------------------------------------------------------------------
  88. #pragma mark ==== DZNEmptyDataSetSource ====
  89. #pragma --------------------------------------------------------------------------------------------
  90. - (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView
  91. {
  92. if (_loadingFolder)
  93. return YES;
  94. else
  95. return NO;
  96. }
  97. - (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView
  98. {
  99. return NO;
  100. }
  101. - (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
  102. {
  103. return [UIColor whiteColor];
  104. }
  105. - (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView
  106. {
  107. if (_loadingFolder) {
  108. UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
  109. activityView.transform = CGAffineTransformMakeScale(1.5f, 1.5f);
  110. activityView.color = [NCBrandColor sharedInstance].brand;
  111. [activityView startAnimating];
  112. return activityView;
  113. }
  114. return nil;
  115. }
  116. // MARK: - IBAction
  117. - (IBAction)cancel:(UIBarButtonItem *)sender
  118. {
  119. if ([self.delegate respondsToSelector:@selector(dismissMove)])
  120. [self.delegate dismissMove];
  121. [self dismissViewControllerAnimated:YES completion:nil];
  122. }
  123. - (IBAction)move:(UIBarButtonItem *)sender
  124. {
  125. [_networkingOperationQueue cancelAllOperations];
  126. if ([self.delegate respondsToSelector:@selector(dismissMove)])
  127. [self.delegate dismissMove];
  128. [self.delegate moveServerUrlTo:_serverUrl title:self.passMetadata.fileName];
  129. [self dismissViewControllerAnimated:YES completion:nil];
  130. }
  131. - (IBAction)create:(UIBarButtonItem *)sender
  132. {
  133. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_create_folder_",nil) message:@"" preferredStyle:UIAlertControllerStyleAlert];
  134. [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
  135. //textField.placeholder = NSLocalizedString(@"LoginPlaceholder", @"Login");
  136. textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
  137. }];
  138. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_save_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  139. [self createFolder:alertController.textFields.firstObject.text];
  140. }]];
  141. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  142. }]];
  143. [self presentViewController:alertController animated:YES completion:nil];
  144. }
  145. // MARK: - BKPasscodeViewController
  146. - (void)passcodeViewController:(CCBKPasscode *)aViewController didFinishWithPasscode:(NSString *)aPasscode
  147. {
  148. [aViewController dismissViewControllerAnimated:YES completion:nil];
  149. [self performSegueDirectoryWithControlPasscode:false];
  150. }
  151. - (void)passcodeViewController:(BKPasscodeViewController *)aViewController authenticatePasscode:(NSString *)aPasscode resultHandler:(void (^)(BOOL))aResultHandler
  152. {
  153. if ([aPasscode isEqualToString:[CCUtility getBlockCode]]) {
  154. self.lockUntilDate = nil;
  155. self.failedAttempts = 0;
  156. aResultHandler(YES);
  157. } else {
  158. aResultHandler(NO);
  159. }
  160. }
  161. - (void)passcodeViewControllerDidFailAttempt:(BKPasscodeViewController *)aViewController
  162. {
  163. self.failedAttempts++;
  164. if (self.failedAttempts > 5) {
  165. NSTimeInterval timeInterval = 60;
  166. if (self.failedAttempts > 6) {
  167. NSUInteger multiplier = self.failedAttempts - 6;
  168. timeInterval = (5 * 60) * multiplier;
  169. if (timeInterval > 3600 * 24) {
  170. timeInterval = 3600 * 24;
  171. }
  172. }
  173. self.lockUntilDate = [NSDate dateWithTimeIntervalSinceNow:timeInterval];
  174. }
  175. }
  176. - (NSUInteger)passcodeViewControllerNumberOfFailedAttempts:(BKPasscodeViewController *)aViewController
  177. {
  178. return self.failedAttempts;
  179. }
  180. - (NSDate *)passcodeViewControllerLockUntilDate:(BKPasscodeViewController *)aViewController
  181. {
  182. return self.lockUntilDate;
  183. }
  184. - (void)passcodeViewCloseButtonPressed:(id)sender
  185. {
  186. [self dismissViewControllerAnimated:YES completion:nil];
  187. }
  188. // MARK: - NetWorking
  189. - (void)addNetworkingQueue:(CCMetadataNet *)metadataNet
  190. {
  191. OCnetworking *operation = [[OCnetworking alloc] initWithDelegate:self metadataNet:metadataNet withUser:activeUser withPassword:activePassword withUrl:activeUrl];
  192. _networkingOperationQueue.maxConcurrentOperationCount = k_maxConcurrentOperation;
  193. [_networkingOperationQueue addOperation:operation];
  194. }
  195. // MARK: - Download File
  196. - (void)downloadFileSuccess:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector selectorPost:(NSString *)selectorPost
  197. {
  198. }
  199. - (void)downloadFileFailure:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector message:(NSString *)message errorCode:(NSInteger)errorCode
  200. {
  201. self.move.enabled = NO;
  202. }
  203. // MARK: - Read Folder
  204. - (void)readFileFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  205. {
  206. [self readFolder];
  207. }
  208. - (void)readFileSuccess:(CCMetadataNet *)metadataNet metadata:(tableMetadata *)metadata
  209. {
  210. if ([metadataNet.selector isEqualToString:selectorReadFileReloadFolder]) {
  211. tableDirectory *directory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND serverUrl = %@", metadataNet.account, metadataNet.serverUrl]];
  212. if ([metadata.etag isEqualToString:directory.etag] == NO) {
  213. [self readFolder];
  214. }
  215. }
  216. }
  217. - (void)readFileReloadFolder
  218. {
  219. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:activeAccount];
  220. metadataNet.action = actionReadFile;
  221. metadataNet.priority = NSOperationQueuePriorityHigh;
  222. metadataNet.selector = selectorReadFileReloadFolder;
  223. metadataNet.serverUrl = _serverUrl;
  224. [self addNetworkingQueue:metadataNet];
  225. }
  226. // MARK: - Read Folder
  227. - (void)readFolderFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  228. {
  229. _loadingFolder = NO;
  230. self.move.enabled = NO;
  231. [self.tableView reloadData];
  232. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_",nil) message:message preferredStyle:UIAlertControllerStyleAlert];
  233. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  234. }]];
  235. [self presentViewController:alertController animated:YES completion:nil];
  236. }
  237. - (void)readFolderSuccess:(CCMetadataNet *)metadataNet metadataFolder:(tableMetadata *)metadataFolder metadatas:(NSArray *)metadatas
  238. {
  239. NSMutableArray *metadatasToInsertInDB = [NSMutableArray new];
  240. // Update directory etag
  241. [[NCManageDatabase sharedInstance] setDirectoryWithServerUrl:metadataNet.serverUrl serverUrlTo:nil etag:metadataFolder.etag];
  242. for (tableMetadata *metadata in metadatas) {
  243. // Insert in Array
  244. [metadatasToInsertInDB addObject:metadata];
  245. }
  246. // insert in Database
  247. metadatas = [[NCManageDatabase sharedInstance] addMetadatas:metadatasToInsertInDB serverUrl:metadataNet.serverUrl];
  248. _loadingFolder = NO;
  249. [self.tableView reloadData];
  250. }
  251. - (void)readFolder
  252. {
  253. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:activeAccount];
  254. metadataNet.action = actionReadFolder;
  255. metadataNet.date = nil;
  256. metadataNet.depth = @"1";
  257. metadataNet.selector = selectorReadFolder;
  258. metadataNet.serverUrl = _serverUrl;
  259. [self addNetworkingQueue:metadataNet];
  260. //
  261. _loadingFolder = YES;
  262. [self.tableView reloadData];
  263. }
  264. // MARK: - Create Folder
  265. - (void)createFolderFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  266. {
  267. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_",nil) message:message preferredStyle:UIAlertControllerStyleAlert];
  268. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  269. }]];
  270. [self presentViewController:alertController animated:YES completion:nil];
  271. }
  272. - (void)createFolderSuccess:(CCMetadataNet *)metadataNet
  273. {
  274. (void)[[NCManageDatabase sharedInstance] addDirectoryWithServerUrl:[NSString stringWithFormat:@"%@/%@", metadataNet.serverUrl, metadataNet.fileName] permissions:nil];
  275. // Load Folder or the Datasource
  276. [self readFolder];
  277. }
  278. - (void)createFolder:(NSString *)fileNameFolder
  279. {
  280. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:activeAccount];
  281. fileNameFolder = [CCUtility removeForbiddenCharactersServer:fileNameFolder];
  282. if (![fileNameFolder length]) return;
  283. metadataNet.action = actionCreateFolder;
  284. metadataNet.fileName = fileNameFolder;
  285. metadataNet.selector = selectorCreateFolder;
  286. metadataNet.selectorPost = selectorReadFolderForced;
  287. metadataNet.serverUrl = _serverUrl;
  288. [self addNetworkingQueue:metadataNet];
  289. }
  290. // MARK: - Table
  291. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  292. {
  293. return 1;
  294. }
  295. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  296. {
  297. NSString *directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:_serverUrl];
  298. if (!directoryID) return 0;
  299. NSPredicate *predicate;
  300. if (self.onlyClearDirectory) predicate = [NSPredicate predicateWithFormat:@"account = %@ AND directoryID = %@ AND directory = true", activeAccount, directoryID];
  301. else predicate = [NSPredicate predicateWithFormat:@"account == %@ AND directoryID = %@ AND directory = true", activeAccount, directoryID];
  302. NSArray *result = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:predicate sorted:nil ascending:NO];
  303. if (result)
  304. return [result count];
  305. else
  306. return 0;
  307. }
  308. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  309. {
  310. NSPredicate *predicate;
  311. static NSString *CellIdentifier = @"MyCustomCell";
  312. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  313. if (cell == nil) {
  314. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  315. }
  316. NSString *directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:_serverUrl];
  317. if (!directoryID)
  318. return cell;
  319. if (self.onlyClearDirectory) predicate = [NSPredicate predicateWithFormat:@"account = %@ AND directoryID = %@ AND directory = true", activeAccount, directoryID];
  320. else predicate = [NSPredicate predicateWithFormat:@"account = %@ AND directoryID = %@ AND directory = true", activeAccount, directoryID];
  321. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataAtIndexWithPredicate:predicate sorted:@"fileName" ascending:YES index:indexPath.row];
  322. // colors
  323. cell.textLabel.textColor = [UIColor blackColor];
  324. cell.detailTextLabel.text = @"";
  325. cell.imageView.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:metadata.iconName] color:[NCBrandColor sharedInstance].brand];
  326. cell.textLabel.text = metadata.fileName;
  327. return cell;
  328. }
  329. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  330. {
  331. [self performSegueDirectoryWithControlPasscode:YES];
  332. }
  333. // MARK: - Navigation
  334. - (void)performSegueDirectoryWithControlPasscode:(BOOL)controlPasscode
  335. {
  336. NSString *nomeDir;
  337. NSPredicate *predicate;
  338. NSIndexPath *index = [self.tableView indexPathForSelectedRow];
  339. NSString *directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:_serverUrl];
  340. if (!directoryID) return;
  341. if (self.onlyClearDirectory) predicate = [NSPredicate predicateWithFormat:@"account = %@ AND directoryID = %@ AND directory = true", activeAccount, directoryID];
  342. else predicate = [NSPredicate predicateWithFormat:@"account = %@ AND directoryID == %@ AND directory = true", activeAccount, directoryID];
  343. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataAtIndexWithPredicate:predicate sorted:@"fileName" ascending:YES index:index.row];
  344. // lockServerUrl
  345. NSString *lockServerUrl = [CCUtility stringAppendServerUrl:_serverUrl addFileName:metadata.fileName];
  346. // 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
  347. tableDirectory *directory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND serverUrl = %@", activeAccount, lockServerUrl]];
  348. if (directory.lock && [[CCUtility getBlockCode] length] && controlPasscode) {
  349. CCBKPasscode *viewController = [[CCBKPasscode alloc] initWithNibName:nil bundle:nil];
  350. viewController.delegate = self;
  351. //viewController.fromType = CCBKPasscodeFromLockDirectory;
  352. viewController.type = BKPasscodeViewControllerCheckPasscodeType;
  353. viewController.inputViewTitlePassword = YES;
  354. if ([CCUtility getSimplyBlockCode]) {
  355. viewController.passcodeStyle = BKPasscodeInputViewNumericPasscodeStyle;
  356. viewController.passcodeInputView.maximumLength = 6;
  357. } else {
  358. viewController.passcodeStyle = BKPasscodeInputViewNormalPasscodeStyle;
  359. viewController.passcodeInputView.maximumLength = 64;
  360. }
  361. BKTouchIDManager *touchIDManager = [[BKTouchIDManager alloc] initWithKeychainServiceName:k_serviceShareKeyChain];
  362. touchIDManager.promptText = NSLocalizedString(@"_scan_fingerprint_", nil);
  363. viewController.touchIDManager = touchIDManager;
  364. viewController.title = NSLocalizedString(@"_folder_blocked_", nil);
  365. viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(passcodeViewCloseButtonPressed:)];
  366. viewController.navigationItem.leftBarButtonItem.tintColor = NCBrandColor.sharedInstance.cryptocloud;
  367. UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
  368. [self presentViewController:navController animated:YES completion:nil];
  369. return;
  370. }
  371. nomeDir = metadata.fileName;
  372. CCMove *viewController = [[UIStoryboard storyboardWithName:@"CCMove" bundle:nil] instantiateViewControllerWithIdentifier:@"CCMoveVC"];
  373. viewController.delegate = self.delegate;
  374. viewController.onlyClearDirectory = self.onlyClearDirectory;
  375. viewController.move.title = self.move.title;
  376. viewController.networkingOperationQueue = _networkingOperationQueue;
  377. viewController.passMetadata = metadata;
  378. viewController.serverUrl = [CCUtility stringAppendServerUrl:_serverUrl addFileName:nomeDir];
  379. [self.navigationController pushViewController:viewController animated:YES];
  380. }
  381. @end