CCMove.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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. // get auto upload folder
  86. _autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  87. _autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:activeUrl];
  88. // read file->folder
  89. [self readFileReloadFolder];
  90. }
  91. // Apparirà
  92. - (void)viewWillAppear:(BOOL)animated
  93. {
  94. [super viewWillAppear:animated];
  95. self.navigationController.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand;
  96. self.navigationController.navigationBar.tintColor = NCBrandColor.sharedInstance.brandText;
  97. self.navigationController.toolbar.barTintColor = NCBrandColor.sharedInstance.tabBar;
  98. self.navigationController.toolbar.tintColor = NCBrandColor.sharedInstance.brandElement;
  99. if (self.hideCreateFolder) {
  100. [self.create setEnabled:NO];
  101. [self.create setTintColor: [UIColor clearColor]];
  102. }
  103. }
  104. #pragma --------------------------------------------------------------------------------------------
  105. #pragma mark ==== DZNEmptyDataSetSource ====
  106. #pragma --------------------------------------------------------------------------------------------
  107. - (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView
  108. {
  109. if (_loadingFolder)
  110. return YES;
  111. else
  112. return NO;
  113. }
  114. - (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView
  115. {
  116. return NO;
  117. }
  118. - (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
  119. {
  120. return [UIColor whiteColor];
  121. }
  122. - (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView
  123. {
  124. if (_loadingFolder) {
  125. UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
  126. activityView.transform = CGAffineTransformMakeScale(1.5f, 1.5f);
  127. activityView.color = [NCBrandColor sharedInstance].brandElement;
  128. [activityView startAnimating];
  129. return activityView;
  130. }
  131. return nil;
  132. }
  133. // MARK: - IBAction
  134. - (IBAction)cancel:(UIBarButtonItem *)sender
  135. {
  136. if ([self.delegate respondsToSelector:@selector(dismissMove)])
  137. [self.delegate dismissMove];
  138. [self dismissViewControllerAnimated:YES completion:nil];
  139. }
  140. - (IBAction)move:(UIBarButtonItem *)sender
  141. {
  142. [_networkingOperationQueue cancelAllOperations];
  143. if ([self.delegate respondsToSelector:@selector(dismissMove)])
  144. [self.delegate dismissMove];
  145. [self.delegate moveServerUrlTo:_serverUrl title:self.passMetadata.fileNameView];
  146. [self dismissViewControllerAnimated:YES completion:nil];
  147. }
  148. - (IBAction)create:(UIBarButtonItem *)sender
  149. {
  150. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_create_folder_",nil) message:@"" preferredStyle:UIAlertControllerStyleAlert];
  151. [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
  152. //textField.placeholder = NSLocalizedString(@"LoginPlaceholder", @"Login");
  153. textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
  154. }];
  155. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_save_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  156. [self createFolder:alertController.textFields.firstObject.text];
  157. }]];
  158. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  159. }]];
  160. [self presentViewController:alertController animated:YES completion:nil];
  161. }
  162. // MARK: - BKPasscodeViewController
  163. - (void)passcodeViewController:(CCBKPasscode *)aViewController didFinishWithPasscode:(NSString *)aPasscode
  164. {
  165. [aViewController dismissViewControllerAnimated:YES completion:nil];
  166. [self performSegueDirectoryWithControlPasscode:false];
  167. }
  168. - (void)passcodeViewController:(BKPasscodeViewController *)aViewController authenticatePasscode:(NSString *)aPasscode resultHandler:(void (^)(BOOL))aResultHandler
  169. {
  170. if ([aPasscode isEqualToString:[CCUtility getBlockCode]]) {
  171. self.lockUntilDate = nil;
  172. self.failedAttempts = 0;
  173. aResultHandler(YES);
  174. } else {
  175. aResultHandler(NO);
  176. }
  177. }
  178. - (void)passcodeViewControllerDidFailAttempt:(BKPasscodeViewController *)aViewController
  179. {
  180. self.failedAttempts++;
  181. if (self.failedAttempts > 5) {
  182. NSTimeInterval timeInterval = 60;
  183. if (self.failedAttempts > 6) {
  184. NSUInteger multiplier = self.failedAttempts - 6;
  185. timeInterval = (5 * 60) * multiplier;
  186. if (timeInterval > 3600 * 24) {
  187. timeInterval = 3600 * 24;
  188. }
  189. }
  190. self.lockUntilDate = [NSDate dateWithTimeIntervalSinceNow:timeInterval];
  191. }
  192. }
  193. - (NSUInteger)passcodeViewControllerNumberOfFailedAttempts:(BKPasscodeViewController *)aViewController
  194. {
  195. return self.failedAttempts;
  196. }
  197. - (NSDate *)passcodeViewControllerLockUntilDate:(BKPasscodeViewController *)aViewController
  198. {
  199. return self.lockUntilDate;
  200. }
  201. - (void)passcodeViewCloseButtonPressed:(id)sender
  202. {
  203. [self dismissViewControllerAnimated:YES completion:nil];
  204. }
  205. // MARK: - NetWorking
  206. - (void)addNetworkingQueue:(CCMetadataNet *)metadataNet
  207. {
  208. OCnetworking *operation = [[OCnetworking alloc] initWithDelegate:self metadataNet:metadataNet withUser:activeUser withUserID:activeUserID withPassword:activePassword withUrl:activeUrl];
  209. _networkingOperationQueue.maxConcurrentOperationCount = k_maxConcurrentOperation;
  210. [_networkingOperationQueue addOperation:operation];
  211. }
  212. // MARK: - Read Folder
  213. - (void)readFileSuccessFailure:(CCMetadataNet *)metadataNet metadata:(tableMetadata *)metadata message:(NSString *)message errorCode:(NSInteger)errorCode
  214. {
  215. if (errorCode == 0) {
  216. if ([metadataNet.selector isEqualToString:selectorReadFileReloadFolder]) {
  217. tableDirectory *directory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", metadataNet.account, metadataNet.serverUrl]];
  218. if ([metadata.etag isEqualToString:directory.etag] == NO) {
  219. [self readFolder];
  220. }
  221. }
  222. } else {
  223. [self readFolder];
  224. }
  225. }
  226. - (void)readFileReloadFolder
  227. {
  228. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:activeAccount];
  229. metadataNet.action = actionReadFile;
  230. metadataNet.priority = NSOperationQueuePriorityHigh;
  231. metadataNet.selector = selectorReadFileReloadFolder;
  232. metadataNet.serverUrl = _serverUrl;
  233. [self addNetworkingQueue:metadataNet];
  234. }
  235. // MARK: - Read Folder
  236. - (void)readFolderSuccessFailure:(CCMetadataNet *)metadataNet metadataFolder:(tableMetadata *)metadataFolder metadatas:(NSArray *)metadatas message:(NSString *)message errorCode:(NSInteger)errorCode
  237. {
  238. if (errorCode == 0) {
  239. NSMutableArray *metadatasToInsertInDB = [NSMutableArray new];
  240. // Update directory etag
  241. [[NCManageDatabase sharedInstance] setDirectoryWithServerUrl:metadataNet.serverUrl serverUrlTo:nil etag:metadataFolder.etag fileID:metadataFolder.fileID encrypted:metadataFolder.e2eEncrypted];
  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. } else {
  251. _loadingFolder = NO;
  252. self.move.enabled = NO;
  253. [self.tableView reloadData];
  254. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_",nil) message:message preferredStyle:UIAlertControllerStyleAlert];
  255. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  256. }]];
  257. [self presentViewController:alertController animated:YES completion:nil];
  258. }
  259. }
  260. - (void)readFolder
  261. {
  262. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:activeAccount];
  263. metadataNet.action = actionReadFolder;
  264. metadataNet.date = nil;
  265. metadataNet.depth = @"1";
  266. metadataNet.selector = selectorReadFolder;
  267. metadataNet.serverUrl = _serverUrl;
  268. [self addNetworkingQueue:metadataNet];
  269. //
  270. _loadingFolder = YES;
  271. [self.tableView reloadData];
  272. }
  273. // MARK: - Create Folder
  274. - (void)createFolderSuccessFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  275. {
  276. if (errorCode == 0) {
  277. [self readFolder];
  278. } else {
  279. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_",nil) message:message preferredStyle:UIAlertControllerStyleAlert];
  280. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  281. }]];
  282. [self presentViewController:alertController animated:YES completion:nil];
  283. }
  284. }
  285. - (void)createFolder:(NSString *)fileNameFolder
  286. {
  287. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:activeAccount];
  288. fileNameFolder = [CCUtility removeForbiddenCharactersServer:fileNameFolder];
  289. if (![fileNameFolder length]) return;
  290. metadataNet.action = actionCreateFolder;
  291. metadataNet.fileName = fileNameFolder;
  292. metadataNet.selector = selectorCreateFolder;
  293. metadataNet.selectorPost = selectorReadFolderForced;
  294. metadataNet.serverUrl = _serverUrl;
  295. [self addNetworkingQueue:metadataNet];
  296. }
  297. // MARK: - Table
  298. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  299. {
  300. return 1;
  301. }
  302. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  303. {
  304. NSString *directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:_serverUrl];
  305. if (!directoryID) return 0;
  306. NSPredicate *predicate;
  307. if (self.includeDirectoryE2EEncryption) predicate = [NSPredicate predicateWithFormat:@"account == %@ AND directoryID == %@ AND directory == true", activeAccount, directoryID];
  308. else predicate = [NSPredicate predicateWithFormat:@"account == %@ AND directoryID == %@ AND directory == true AND e2eEncrypted == false", activeAccount, directoryID];
  309. NSArray *result = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:predicate sorted:nil ascending:NO];
  310. if (result)
  311. return [result count];
  312. else
  313. return 0;
  314. }
  315. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  316. {
  317. NSPredicate *predicate;
  318. static NSString *CellIdentifier = @"MyCustomCell";
  319. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  320. if (cell == nil) {
  321. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  322. }
  323. NSString *directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:_serverUrl];
  324. if (!directoryID)
  325. return cell;
  326. if (self.includeDirectoryE2EEncryption) predicate = [NSPredicate predicateWithFormat:@"account == %@ AND directoryID == %@ AND directory == true", activeAccount, directoryID];
  327. else predicate = [NSPredicate predicateWithFormat:@"account == %@ AND directoryID == %@ AND directory == true AND e2eEncrypted == false", activeAccount, directoryID];
  328. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataAtIndexWithPredicate:predicate sorted:@"fileName" ascending:YES index:indexPath.row];
  329. // colors
  330. cell.textLabel.textColor = [UIColor blackColor];
  331. cell.detailTextLabel.text = @"";
  332. if (metadata.e2eEncrypted)
  333. cell.imageView.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folderEncrypted"] color:[NCBrandColor sharedInstance].brandElement];
  334. else if ([metadata.fileName isEqualToString:_autoUploadFileName] && [self.serverUrl isEqualToString:_autoUploadDirectory])
  335. cell.imageView.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folderPhotos"] color:[NCBrandColor sharedInstance].brandElement];
  336. else
  337. cell.imageView.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder"] color:[NCBrandColor sharedInstance].brandElement];
  338. cell.textLabel.text = metadata.fileNameView;
  339. return cell;
  340. }
  341. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  342. {
  343. [self performSegueDirectoryWithControlPasscode:YES];
  344. }
  345. // MARK: - Navigation
  346. - (void)performSegueDirectoryWithControlPasscode:(BOOL)controlPasscode
  347. {
  348. NSString *nomeDir;
  349. NSPredicate *predicate;
  350. NSIndexPath *index = [self.tableView indexPathForSelectedRow];
  351. NSString *directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:_serverUrl];
  352. if (!directoryID) return;
  353. if (self.includeDirectoryE2EEncryption) predicate = [NSPredicate predicateWithFormat:@"account == %@ AND directoryID == %@ AND directory == true", activeAccount, directoryID];
  354. else predicate = [NSPredicate predicateWithFormat:@"account == %@ AND directoryID == %@ AND directory == true AND e2eEncrypted == false", activeAccount, directoryID];
  355. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataAtIndexWithPredicate:predicate sorted:@"fileName" ascending:YES index:index.row];
  356. // lockServerUrl
  357. NSString *lockServerUrl = [CCUtility stringAppendServerUrl:_serverUrl addFileName:metadata.fileName];
  358. // 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
  359. tableDirectory *directory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", activeAccount, lockServerUrl]];
  360. if (directory.lock && [[CCUtility getBlockCode] length] && controlPasscode) {
  361. CCBKPasscode *viewController = [[CCBKPasscode alloc] initWithNibName:nil bundle:nil];
  362. viewController.delegate = self;
  363. //viewController.fromType = CCBKPasscodeFromLockDirectory;
  364. viewController.type = BKPasscodeViewControllerCheckPasscodeType;
  365. viewController.inputViewTitlePassword = YES;
  366. if ([CCUtility getSimplyBlockCode]) {
  367. viewController.passcodeStyle = BKPasscodeInputViewNumericPasscodeStyle;
  368. viewController.passcodeInputView.maximumLength = 6;
  369. } else {
  370. viewController.passcodeStyle = BKPasscodeInputViewNormalPasscodeStyle;
  371. viewController.passcodeInputView.maximumLength = 64;
  372. }
  373. BKTouchIDManager *touchIDManager = [[BKTouchIDManager alloc] initWithKeychainServiceName:k_serviceShareKeyChain];
  374. touchIDManager.promptText = NSLocalizedString(@"_scan_fingerprint_", nil);
  375. viewController.touchIDManager = touchIDManager;
  376. viewController.title = NSLocalizedString(@"_folder_blocked_", nil);
  377. viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(passcodeViewCloseButtonPressed:)];
  378. viewController.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
  379. UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
  380. [self presentViewController:navController animated:YES completion:nil];
  381. return;
  382. }
  383. nomeDir = metadata.fileName;
  384. CCMove *viewController = [[UIStoryboard storyboardWithName:@"CCMove" bundle:nil] instantiateViewControllerWithIdentifier:@"CCMoveVC"];
  385. viewController.delegate = self.delegate;
  386. viewController.includeDirectoryE2EEncryption = self.includeDirectoryE2EEncryption;
  387. viewController.move.title = self.move.title;
  388. viewController.hideCreateFolder = self.hideCreateFolder;
  389. viewController.networkingOperationQueue = _networkingOperationQueue;
  390. viewController.passMetadata = metadata;
  391. viewController.serverUrl = [CCUtility stringAppendServerUrl:_serverUrl addFileName:nomeDir];
  392. [self.navigationController pushViewController:viewController animated:YES];
  393. }
  394. @end