CCMove.m 20 KB

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