CCMove.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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. _serverUrl = [CCUtility getHomeServerUrlActiveUrl:activeUrl];
  64. UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed: @"navigationLogo"]];
  65. [self.navigationController.navigationBar.topItem setTitleView:image];
  66. self.title = @"Home";
  67. } else {
  68. UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0,0, self.navigationItem.titleView.frame.size.width, 40)];
  69. label.text = self.passMetadata.fileNameView;
  70. label.textColor = NCBrandColor.sharedInstance.brandText;
  71. label.backgroundColor =[UIColor clearColor];
  72. label.textAlignment = NSTextAlignmentCenter;
  73. self.navigationItem.titleView=label;
  74. }
  75. // TableView : at the end of rows nothing
  76. self.tableView.tableFooterView = [UIView new];
  77. self.tableView.separatorColor = NCBrandColor.sharedInstance.seperator;
  78. self.tableView.emptyDataSetDelegate = self;
  79. self.tableView.emptyDataSetSource = self;
  80. // read file->folder
  81. [self readFileReloadFolder];
  82. }
  83. // Apparirà
  84. - (void)viewWillAppear:(BOOL)animated
  85. {
  86. [super viewWillAppear:animated];
  87. self.navigationController.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand;
  88. self.navigationController.navigationBar.tintColor = NCBrandColor.sharedInstance.brandText;
  89. self.navigationController.toolbar.barTintColor = NCBrandColor.sharedInstance.tabBar;
  90. self.navigationController.toolbar.tintColor = NCBrandColor.sharedInstance.brand;
  91. }
  92. #pragma --------------------------------------------------------------------------------------------
  93. #pragma mark ==== DZNEmptyDataSetSource ====
  94. #pragma --------------------------------------------------------------------------------------------
  95. - (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView
  96. {
  97. if (_loadingFolder)
  98. return YES;
  99. else
  100. return NO;
  101. }
  102. - (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView
  103. {
  104. return NO;
  105. }
  106. - (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
  107. {
  108. return [UIColor whiteColor];
  109. }
  110. - (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView
  111. {
  112. if (_loadingFolder) {
  113. UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
  114. activityView.transform = CGAffineTransformMakeScale(1.5f, 1.5f);
  115. activityView.color = [NCBrandColor sharedInstance].brand;
  116. [activityView startAnimating];
  117. return activityView;
  118. }
  119. return nil;
  120. }
  121. // MARK: - IBAction
  122. - (IBAction)cancel:(UIBarButtonItem *)sender
  123. {
  124. if ([self.delegate respondsToSelector:@selector(dismissMove)])
  125. [self.delegate dismissMove];
  126. [self dismissViewControllerAnimated:YES completion:nil];
  127. }
  128. - (IBAction)move:(UIBarButtonItem *)sender
  129. {
  130. [_networkingOperationQueue cancelAllOperations];
  131. if ([self.delegate respondsToSelector:@selector(dismissMove)])
  132. [self.delegate dismissMove];
  133. [self.delegate moveServerUrlTo:_serverUrl title:self.passMetadata.fileNameView];
  134. [self dismissViewControllerAnimated:YES completion:nil];
  135. }
  136. - (IBAction)create:(UIBarButtonItem *)sender
  137. {
  138. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_create_folder_",nil) message:@"" preferredStyle:UIAlertControllerStyleAlert];
  139. [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
  140. //textField.placeholder = NSLocalizedString(@"LoginPlaceholder", @"Login");
  141. textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
  142. }];
  143. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_save_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  144. [self createFolder:alertController.textFields.firstObject.text];
  145. }]];
  146. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  147. }]];
  148. [self presentViewController:alertController animated:YES completion:nil];
  149. }
  150. // MARK: - BKPasscodeViewController
  151. - (void)passcodeViewController:(CCBKPasscode *)aViewController didFinishWithPasscode:(NSString *)aPasscode
  152. {
  153. [aViewController dismissViewControllerAnimated:YES completion:nil];
  154. [self performSegueDirectoryWithControlPasscode:false];
  155. }
  156. - (void)passcodeViewController:(BKPasscodeViewController *)aViewController authenticatePasscode:(NSString *)aPasscode resultHandler:(void (^)(BOOL))aResultHandler
  157. {
  158. if ([aPasscode isEqualToString:[CCUtility getBlockCode]]) {
  159. self.lockUntilDate = nil;
  160. self.failedAttempts = 0;
  161. aResultHandler(YES);
  162. } else {
  163. aResultHandler(NO);
  164. }
  165. }
  166. - (void)passcodeViewControllerDidFailAttempt:(BKPasscodeViewController *)aViewController
  167. {
  168. self.failedAttempts++;
  169. if (self.failedAttempts > 5) {
  170. NSTimeInterval timeInterval = 60;
  171. if (self.failedAttempts > 6) {
  172. NSUInteger multiplier = self.failedAttempts - 6;
  173. timeInterval = (5 * 60) * multiplier;
  174. if (timeInterval > 3600 * 24) {
  175. timeInterval = 3600 * 24;
  176. }
  177. }
  178. self.lockUntilDate = [NSDate dateWithTimeIntervalSinceNow:timeInterval];
  179. }
  180. }
  181. - (NSUInteger)passcodeViewControllerNumberOfFailedAttempts:(BKPasscodeViewController *)aViewController
  182. {
  183. return self.failedAttempts;
  184. }
  185. - (NSDate *)passcodeViewControllerLockUntilDate:(BKPasscodeViewController *)aViewController
  186. {
  187. return self.lockUntilDate;
  188. }
  189. - (void)passcodeViewCloseButtonPressed:(id)sender
  190. {
  191. [self dismissViewControllerAnimated:YES completion:nil];
  192. }
  193. // MARK: - NetWorking
  194. - (void)addNetworkingQueue:(CCMetadataNet *)metadataNet
  195. {
  196. OCnetworking *operation = [[OCnetworking alloc] initWithDelegate:self metadataNet:metadataNet withUser:activeUser withUserID:activeUserID withPassword:activePassword withUrl:activeUrl];
  197. _networkingOperationQueue.maxConcurrentOperationCount = k_maxConcurrentOperation;
  198. [_networkingOperationQueue addOperation:operation];
  199. }
  200. // MARK: - Read Folder
  201. - (void)readFileFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  202. {
  203. [self readFolder];
  204. }
  205. - (void)readFileSuccess:(CCMetadataNet *)metadataNet metadata:(tableMetadata *)metadata
  206. {
  207. if ([metadataNet.selector isEqualToString:selectorReadFileReloadFolder]) {
  208. tableDirectory *directory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND serverUrl = %@", metadataNet.account, metadataNet.serverUrl]];
  209. if ([metadata.etag isEqualToString:directory.etag] == NO) {
  210. [self readFolder];
  211. }
  212. }
  213. }
  214. - (void)readFileReloadFolder
  215. {
  216. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:activeAccount];
  217. metadataNet.action = actionReadFile;
  218. metadataNet.priority = NSOperationQueuePriorityHigh;
  219. metadataNet.selector = selectorReadFileReloadFolder;
  220. metadataNet.serverUrl = _serverUrl;
  221. [self addNetworkingQueue:metadataNet];
  222. }
  223. // MARK: - Read Folder
  224. - (void)readFolderFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  225. {
  226. _loadingFolder = NO;
  227. self.move.enabled = NO;
  228. [self.tableView reloadData];
  229. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_",nil) message:message preferredStyle:UIAlertControllerStyleAlert];
  230. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  231. }]];
  232. [self presentViewController:alertController animated:YES completion:nil];
  233. }
  234. - (void)readFolderSuccess:(CCMetadataNet *)metadataNet metadataFolder:(tableMetadata *)metadataFolder metadatas:(NSArray *)metadatas
  235. {
  236. NSMutableArray *metadatasToInsertInDB = [NSMutableArray new];
  237. // Update directory etag
  238. [[NCManageDatabase sharedInstance] setDirectoryWithServerUrl:metadataNet.serverUrl serverUrlTo:nil etag:metadataFolder.etag fileID:metadataFolder.fileID encrypted:metadataFolder.e2eEncrypted];
  239. for (tableMetadata *metadata in metadatas) {
  240. // Insert in Array
  241. [metadatasToInsertInDB addObject:metadata];
  242. }
  243. // insert in Database
  244. metadatas = [[NCManageDatabase sharedInstance] addMetadatas:metadatasToInsertInDB serverUrl:metadataNet.serverUrl];
  245. // get auto upload folder
  246. _autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  247. _autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:activeUrl];
  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 encrypted:false];
  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. if (metadata.e2eEncrypted)
  326. cell.imageView.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folderEncrypted"] color:[NCBrandColor sharedInstance].brand];
  327. else if ([metadata.fileName isEqualToString:_autoUploadFileName] && [self.serverUrl isEqualToString:_autoUploadDirectory])
  328. cell.imageView.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folderphotocamera"] color:[NCBrandColor sharedInstance].brand];
  329. else
  330. cell.imageView.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder"] color:[NCBrandColor sharedInstance].brand];
  331. cell.textLabel.text = metadata.fileNameView;
  332. return cell;
  333. }
  334. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  335. {
  336. [self performSegueDirectoryWithControlPasscode:YES];
  337. }
  338. // MARK: - Navigation
  339. - (void)performSegueDirectoryWithControlPasscode:(BOOL)controlPasscode
  340. {
  341. NSString *nomeDir;
  342. NSPredicate *predicate;
  343. NSIndexPath *index = [self.tableView indexPathForSelectedRow];
  344. NSString *directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:_serverUrl];
  345. if (!directoryID) return;
  346. if (self.onlyClearDirectory) predicate = [NSPredicate predicateWithFormat:@"account = %@ AND directoryID = %@ AND directory = true", activeAccount, directoryID];
  347. else predicate = [NSPredicate predicateWithFormat:@"account = %@ AND directoryID == %@ AND directory = true", activeAccount, directoryID];
  348. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataAtIndexWithPredicate:predicate sorted:@"fileName" ascending:YES index:index.row];
  349. // lockServerUrl
  350. NSString *lockServerUrl = [CCUtility stringAppendServerUrl:_serverUrl addFileName:metadata.fileName];
  351. // 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
  352. tableDirectory *directory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND serverUrl = %@", activeAccount, lockServerUrl]];
  353. if (directory.lock && [[CCUtility getBlockCode] length] && controlPasscode) {
  354. CCBKPasscode *viewController = [[CCBKPasscode alloc] initWithNibName:nil bundle:nil];
  355. viewController.delegate = self;
  356. //viewController.fromType = CCBKPasscodeFromLockDirectory;
  357. viewController.type = BKPasscodeViewControllerCheckPasscodeType;
  358. viewController.inputViewTitlePassword = YES;
  359. if ([CCUtility getSimplyBlockCode]) {
  360. viewController.passcodeStyle = BKPasscodeInputViewNumericPasscodeStyle;
  361. viewController.passcodeInputView.maximumLength = 6;
  362. } else {
  363. viewController.passcodeStyle = BKPasscodeInputViewNormalPasscodeStyle;
  364. viewController.passcodeInputView.maximumLength = 64;
  365. }
  366. BKTouchIDManager *touchIDManager = [[BKTouchIDManager alloc] initWithKeychainServiceName:k_serviceShareKeyChain];
  367. touchIDManager.promptText = NSLocalizedString(@"_scan_fingerprint_", nil);
  368. viewController.touchIDManager = touchIDManager;
  369. viewController.title = NSLocalizedString(@"_folder_blocked_", nil);
  370. viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(passcodeViewCloseButtonPressed:)];
  371. viewController.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
  372. UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
  373. [self presentViewController:navController animated:YES completion:nil];
  374. return;
  375. }
  376. nomeDir = metadata.fileName;
  377. CCMove *viewController = [[UIStoryboard storyboardWithName:@"CCMove" bundle:nil] instantiateViewControllerWithIdentifier:@"CCMoveVC"];
  378. viewController.delegate = self.delegate;
  379. viewController.onlyClearDirectory = self.onlyClearDirectory;
  380. viewController.move.title = self.move.title;
  381. viewController.networkingOperationQueue = _networkingOperationQueue;
  382. viewController.passMetadata = metadata;
  383. viewController.serverUrl = [CCUtility stringAppendServerUrl:_serverUrl addFileName:nomeDir];
  384. [self.navigationController pushViewController:viewController animated:YES];
  385. }
  386. @end