CCMove.m 20 KB

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