CCMove.m 20 KB

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