CCMove.m 20 KB

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