123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515 |
- //
- // CCMove.m
- // Crypto Cloud Technology Nextcloud
- //
- // Created by Marino Faggiana on 04/09/14.
- // Copyright (c) 2014 TWS. All rights reserved.
- //
- // Author Marino Faggiana <m.faggiana@twsweb.it>
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program. If not, see <http://www.gnu.org/licenses/>.
- //
- #import "CCMove.h"
- #ifndef EXTENSION
- #import "AppDelegate.h"
- #endif
- @interface CCMove ()
- {
- NSString *activeAccount;
- NSString *activePassword;
- NSString *activeUrl;
- NSString *activeUser;
- NSString *directoryUser;
- NSString *typeCloud;
- NSString *activeUID;
- NSString *activeAccessToken;
-
- CCHud *_hud;
- }
- @end
- @implementation CCMove
- // MARK: - View
- - (void)viewDidLoad
- {
- [super viewDidLoad];
-
- TableAccount *recordAccount = [CCCoreData getActiveAccount];
-
- if (recordAccount) {
-
- activeAccount = recordAccount.account;
- activePassword = recordAccount.password;
- activeUrl = recordAccount.url;
- activeUser = recordAccount.user;
- directoryUser = [CCUtility getDirectoryActiveUser:activeUser activeUrl:activeUrl];
- typeCloud = recordAccount.typeCloud;
- activeUID = recordAccount.uid;
- activeAccessToken = recordAccount.token;
-
- } else {
-
- UIAlertController * alert= [UIAlertController alertControllerWithTitle:nil message:NSLocalizedString(@"_no_active_account_", nil) preferredStyle:UIAlertControllerStyleAlert];
- UIAlertAction* ok = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
- [alert dismissViewControllerAnimated:YES completion:nil];
- }];
- [alert addAction:ok];
- [self presentViewController:alert animated:YES completion:nil];
- }
-
- _hud = [[CCHud alloc] initWithView:self.view];
- // TableView : at the end of rows nothing
- self.tableView.tableFooterView = [UIView new];
- [self.cancel setTitle:NSLocalizedString(@"_cancel_", nil)];
- [self.create setTitle:NSLocalizedString(@"_create_folder_", nil)];
- if (![_serverUrl length]) {
-
- _serverUrl = [CCUtility getHomeServerUrlActiveUrl:activeUrl typeCloud:typeCloud];
- UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:image_brandNavigationController]];
- [self.navigationController.navigationBar.topItem setTitleView:image];
- self.title = @"Home";
-
- } else {
-
- UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0,0, self.navigationItem.titleView.frame.size.width, 40)];
- label.text = self.passMetadata.fileNamePrint;
-
- if (self.passMetadata.cryptated) label.textColor = COLOR_ENCRYPTED;
- else label.textColor = self.tintColorTitle;
-
- label.backgroundColor =[UIColor clearColor];
- label.textAlignment = NSTextAlignmentCenter;
- self.navigationItem.titleView=label;
- }
-
- // Toolbar Color
- self.navigationController.navigationBar.barTintColor = self.barTintColor;
- self.navigationController.navigationBar.tintColor = self.tintColor;
-
- self.navigationController.toolbar.barTintColor = self.barTintColor;
- self.navigationController.toolbar.tintColor = self.tintColor;
-
- // read folder
- [self readFolder];
- }
- // MARK: - alertView
- - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
- {
- if (buttonIndex == 1) {
- NSString *nome = [alertView textFieldAtIndex:0].text;
- if ([nome length]) {
- nome = [NSString stringWithFormat:@"%@/%@", _serverUrl, [CCUtility removeForbiddenCharacters:nome hasServerForbiddenCharactersSupport:YES]];
- }
- }
- }
- // MARK: - IBAction
- - (IBAction)cancel:(UIBarButtonItem *)sender
- {
- [self dismissViewControllerAnimated:YES completion:nil];
- }
- - (IBAction)move:(UIBarButtonItem *)sender
- {
- [_networkingOperationQueue cancelAllOperations];
-
- [self.delegate moveServerUrlTo:_serverUrl title:self.passMetadata.fileNamePrint selectedMetadatas:self.selectedMetadatas];
-
- [self dismissViewControllerAnimated:YES completion:nil];
- }
- - (IBAction)create:(UIBarButtonItem *)sender
- {
- UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_create_folder_",nil) message:@"" preferredStyle:UIAlertControllerStyleAlert];
-
- [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
- //textField.placeholder = NSLocalizedString(@"LoginPlaceholder", @"Login");
- }];
-
- [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_save_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
- [self createFolder:alertController.textFields.firstObject.text];
- }]];
-
- [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
- }]];
-
- [self presentViewController:alertController animated:YES completion:nil];
- }
- // MARK: - BKPasscodeViewController
- - (void)passcodeViewController:(CCBKPasscode *)aViewController didFinishWithPasscode:(NSString *)aPasscode
- {
- [aViewController dismissViewControllerAnimated:YES completion:nil];
-
- [self performSegueDirectoryWithControlPasscode:false];
- }
- - (void)passcodeViewController:(BKPasscodeViewController *)aViewController authenticatePasscode:(NSString *)aPasscode resultHandler:(void (^)(BOOL))aResultHandler
- {
- if ([aPasscode isEqualToString:[CCUtility getBlockCode]]) {
-
- self.lockUntilDate = nil;
- self.failedAttempts = 0;
- aResultHandler(YES);
-
- } else {
-
- aResultHandler(NO);
- }
- }
- - (void)passcodeViewControllerDidFailAttempt:(BKPasscodeViewController *)aViewController
- {
- self.failedAttempts++;
-
- if (self.failedAttempts > 5) {
-
- NSTimeInterval timeInterval = 60;
-
- if (self.failedAttempts > 6) {
-
- NSUInteger multiplier = self.failedAttempts - 6;
-
- timeInterval = (5 * 60) * multiplier;
-
- if (timeInterval > 3600 * 24) {
- timeInterval = 3600 * 24;
- }
- }
-
- self.lockUntilDate = [NSDate dateWithTimeIntervalSinceNow:timeInterval];
- }
- }
- - (NSUInteger)passcodeViewControllerNumberOfFailedAttempts:(BKPasscodeViewController *)aViewController
- {
- return self.failedAttempts;
- }
- - (NSDate *)passcodeViewControllerLockUntilDate:(BKPasscodeViewController *)aViewController
- {
- return self.lockUntilDate;
- }
- - (void)passcodeViewCloseButtonPressed:(id)sender
- {
- [self dismissViewControllerAnimated:YES completion:nil];
- }
- // MARK: - NetWorking
- - (void)addNetworkingQueue:(CCMetadataNet *)metadataNet
- {
- if ([typeCloud isEqualToString:typeCloudOwnCloud] || [typeCloud isEqualToString:typeCloudNextcloud]) {
-
- OCnetworking *operation = [[OCnetworking alloc] initWithDelegate:self metadataNet:metadataNet withUser:activeUser withPassword:activePassword withUrl:activeUrl withTypeCloud:typeCloud activityIndicator:NO];
-
- _networkingOperationQueue.maxConcurrentOperationCount = k_maxConcurrentOperation;
- [_networkingOperationQueue addOperation:operation];
- }
- }
- // MARK: - Download File
- - (void)downloadFileSuccess:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector selectorPost:(NSString *)selectorPost
- {
- if ([selector isEqualToString:selectorLoadPlist]) {
- CCMetadata *metadata = [CCCoreData getMetadataWithPreficate:[NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", fileID, activeAccount] context:nil];
- [CCCoreData downloadFilePlist:metadata activeAccount:activeAccount activeUrl:activeUrl typeCloud:typeCloud directoryUser:directoryUser];
-
- [self.tableView reloadData];
- }
- }
- - (void)downloadFileFailure:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector message:(NSString *)message errorCode:(NSInteger)errorCode
- {
- self.move.enabled = NO;
- }
- // MARK: - Read Folder
- - (void)readFolderFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
- {
- [_hud hideHud];
- self.move.enabled = NO;
-
- UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_",nil) message:message preferredStyle:UIAlertControllerStyleAlert];
-
- [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
- }]];
-
- [self presentViewController:alertController animated:YES completion:nil];
- }
- - (void)readFolderSuccess:(CCMetadataNet *)metadataNet permissions:(NSString *)permissions rev:(NSString *)rev metadatas:(NSArray *)metadatas
- {
- // remove all record
- [CCCoreData deleteMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@) AND ((session == NULL) OR (session == ''))", activeAccount, metadataNet.directoryID]];
-
- for (CCMetadata *metadata in metadatas) {
-
- // do not insert crypto file
- if ([CCUtility isCryptoString:metadata.fileName]) continue;
-
- // plist + crypto = completed ?
- if ([CCUtility isCryptoPlistString:metadata.fileName] && metadata.directory == NO) {
-
- BOOL isCryptoComplete = NO;
-
- for (CCMetadata *completeMetadata in metadatas) {
- if ([completeMetadata.fileName isEqualToString:[CCUtility trasformedFileNamePlistInCrypto:metadata.fileName]]) isCryptoComplete = YES;
- }
- if (isCryptoComplete == NO) continue;
- }
-
- [CCCoreData addMetadata:metadata activeAccount:activeAccount activeUrl:activeUrl typeCloud:typeCloud context:nil];
-
- // if plist do not exists, download it !
- if ([CCUtility isCryptoPlistString:metadata.fileName] && [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", directoryUser, metadata.fileName]] == NO) {
-
- // download only the directories
- for (CCMetadata *metadataDirectory in metadatas) {
-
- if (metadataDirectory.directory == YES && [metadataDirectory.fileName isEqualToString:metadata.fileNameData]) {
-
- CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:activeAccount];
-
- metadataNet.action = actionDownloadFile;
- metadataNet.metadata = metadata;
- metadataNet.downloadData = NO;
- metadataNet.downloadPlist = YES;
- metadataNet.selector = selectorLoadPlist;
- metadataNet.serverUrl = _serverUrl;
- metadataNet.session = download_session_foreground;
- metadataNet.taskStatus = k_taskStatusResume;
-
- [self addNetworkingQueue:metadataNet];
- }
- }
- }
- }
-
- [self.tableView reloadData];
-
- [_hud hideHud];
- }
- - (void)readFolder
- {
- // read folder
- [_hud visibleIndeterminateHud];
-
- CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:activeAccount];
-
- metadataNet.action = actionReadFolder;
- metadataNet.serverUrl = _serverUrl;
- metadataNet.selector = selectorReadFolder;
- metadataNet.date = nil;
-
- [self addNetworkingQueue:metadataNet];
- }
- // MARK: - Create Folder
- - (void)createFolderFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
- {
- [_hud hideHud];
-
- UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_",nil) message:message preferredStyle:UIAlertControllerStyleAlert];
-
- [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
- }]];
-
- [self presentViewController:alertController animated:YES completion:nil];
- }
- - (void)createFolderSuccess:(CCMetadataNet *)metadataNet
- {
- [_hud hideHud];
-
- [CCCoreData addDirectory:[NSString stringWithFormat:@"%@/%@", metadataNet.serverUrl, metadataNet.fileName] date:[NSDate date] permissions:nil activeAccount:activeAccount];
-
- // Load Folder or the Datasource
- [self readFolder];
- }
- - (void)createFolder:(NSString *)fileNameFolder
- {
- CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:activeAccount];
-
- fileNameFolder = [CCUtility removeForbiddenCharacters:fileNameFolder hasServerForbiddenCharactersSupport:YES];
- if (![fileNameFolder length]) return;
-
- metadataNet.action = actionCreateFolder;
- metadataNet.fileName = fileNameFolder;
- metadataNet.selector = selectorCreateFolder;
- metadataNet.selectorPost = selectorReadFolderForced;
- metadataNet.serverUrl = _serverUrl;
-
- [self addNetworkingQueue:metadataNet];
-
- [_hud visibleIndeterminateHud];
- }
- // MARK: - Table
- - (void)reloadTable
- {
- [self.tableView reloadData];
- }
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- return 1;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- NSString *directoryID = [CCCoreData getDirectoryIDFromServerUrl:_serverUrl activeAccount:activeAccount];
- NSPredicate *predicate;
-
- if (self.onlyClearDirectory) predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@) AND (directory == 1) AND (cryptated == 0)", activeAccount, directoryID];
- else predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@) AND (directory == 1)", activeAccount, directoryID];
-
- return [[CCCoreData getTableMetadataWithPredicate:predicate context:nil] count];
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- NSPredicate *predicate;
-
- static NSString *CellIdentifier = @"MyCustomCell";
-
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if (cell == nil) {
- cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
- }
-
- NSString *directoryID = [CCCoreData getDirectoryIDFromServerUrl:_serverUrl activeAccount:activeAccount];
-
- if (self.onlyClearDirectory) predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@) AND (directory == 1) AND (cryptated == 0)", activeAccount, directoryID];
- else predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@) AND (directory == 1)", activeAccount, directoryID];
- CCMetadata *metadata = [CCCoreData getMetadataAtIndex:predicate fieldOrder:@"fileName" ascending:YES objectAtIndex:indexPath.row];
-
- // colors
- if (metadata.cryptated) {
- cell.textLabel.textColor = COLOR_ENCRYPTED;
- } else {
- cell.textLabel.textColor = COLOR_CLEAR;
- }
-
- cell.detailTextLabel.text = @"";
- cell.imageView.image = [UIImage imageNamed:metadata.iconName];
- cell.textLabel.text = metadata.fileNamePrint;
-
- return cell;
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- [self performSegueDirectoryWithControlPasscode:YES];
- }
- // MARK: - Navigation
- - (void)performSegueDirectoryWithControlPasscode:(BOOL)controlPasscode
- {
- NSString *nomeDir;
- NSPredicate *predicate;
- NSIndexPath *index = [self.tableView indexPathForSelectedRow];
- NSString *directoryID = [CCCoreData getDirectoryIDFromServerUrl:_serverUrl activeAccount:activeAccount];
-
- if (self.onlyClearDirectory) predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@) AND (directory == 1) AND (cryptated == 0)", activeAccount, directoryID];
- else predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@) AND (directory == 1)", activeAccount, directoryID];
-
- CCMetadata *metadata = [CCCoreData getMetadataAtIndex:predicate fieldOrder:@"fileName" ascending:YES objectAtIndex:index.row];
-
- if (metadata.errorPasscode == NO) {
-
- // lockServerUrl
- NSString *lockServerUrl = [CCUtility stringAppendServerUrl:_serverUrl addServerUrl:metadata.fileNameData];
-
- // 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
- if ([CCCoreData isDirectoryLock:lockServerUrl activeAccount:activeAccount] && [[CCUtility getBlockCode] length] && controlPasscode) {
-
- CCBKPasscode *viewController = [[CCBKPasscode alloc] initWithNibName:nil bundle:nil];
- viewController.delegate = self;
- //viewController.fromType = CCBKPasscodeFromLockDirectory;
- viewController.type = BKPasscodeViewControllerCheckPasscodeType;
- viewController.inputViewTitlePassword = YES;
-
- if ([CCUtility getSimplyBlockCode]) {
-
- viewController.passcodeStyle = BKPasscodeInputViewNumericPasscodeStyle;
- viewController.passcodeInputView.maximumLength = 6;
-
- } else {
-
- viewController.passcodeStyle = BKPasscodeInputViewNormalPasscodeStyle;
- viewController.passcodeInputView.maximumLength = 64;
- }
-
- BKTouchIDManager *touchIDManager = [[BKTouchIDManager alloc] initWithKeychainServiceName:BKPasscodeKeychainServiceName];
- touchIDManager.promptText = NSLocalizedString(@"_scan_fingerprint_", nil);
- viewController.touchIDManager = touchIDManager;
-
- viewController.title = NSLocalizedString(@"_folder_blocked_", nil);
- viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(passcodeViewCloseButtonPressed:)];
- viewController.navigationItem.leftBarButtonItem.tintColor = COLOR_ENCRYPTED;
-
- UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
- [self presentViewController:navController animated:YES completion:nil];
-
- return;
- }
-
- if (metadata.cryptated) nomeDir = [metadata.fileName substringToIndex:[metadata.fileName length]-6];
- else nomeDir = metadata.fileName;
-
- CCMove *viewController = [[UIStoryboard storyboardWithName:@"CCMove" bundle:nil] instantiateViewControllerWithIdentifier:@"CCMoveVC"];
-
- viewController.delegate = self.delegate;
- viewController.onlyClearDirectory = self.onlyClearDirectory;
- viewController.selectedMetadatas = self.selectedMetadatas;
- viewController.move.title = self.move.title;
- viewController.barTintColor = self.barTintColor;
- viewController.tintColor = self.tintColor;
- viewController.tintColorTitle = self.tintColorTitle;
- viewController.networkingOperationQueue = _networkingOperationQueue;
- viewController.passMetadata = metadata;
- viewController.serverUrl = [CCUtility stringAppendServerUrl:_serverUrl addServerUrl:nomeDir];
-
- [self.navigationController pushViewController:viewController animated:YES];
- }
- }
- @end
|