CCMove.m 20 KB

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