CCMove.m 20 KB

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