CCMove.m 19 KB

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