123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544 |
- //
- // ShareViewController.m
- // Crypto Cloud Technology Nextcloud
- //
- // Created by Marino Faggiana on 26/01/16.
- // 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 "ShareViewController.h"
- #ifdef CUSTOM_BUILD
- #import "CustomSwiftShare.h"
- #else
- #import "Share-Swift.h"
- #endif
- @import MobileCoreServices;
- @interface ShareViewController ()
- {
- NSURL *dirGroup;
- NSUInteger totalSize;
-
- NSExtensionItem *inputItem;
- CCMetadata *saveMetadataPlist;
-
- UIColor *barTintColor;
- UIColor *tintColor;
-
- NSMutableArray *_filesSendCryptated;
- BOOL _isCryptoCloudMode;
- }
- @end
- @implementation ShareViewController
- #pragma --------------------------------------------------------------------------------------------
- #pragma mark ===== View =====
- #pragma --------------------------------------------------------------------------------------------
- -(void)viewDidLoad
- {
- dirGroup = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:k_capabilitiesGroups];
-
- [MagicalRecord setupCoreDataStackWithAutoMigratingSqliteStoreNamed:(id)[dirGroup URLByAppendingPathComponent:[appDatabase stringByAppendingPathComponent:@"cryptocloud"]]];
- [MagicalRecord setLoggingLevel:MagicalRecordLoggingLevelOff];
- TableAccount *recordAccount = [CCCoreData getActiveAccount];
- if (recordAccount == nil) {
-
- // close now
- [self performSelector:@selector(closeShareViewController) withObject:nil afterDelay:0.1];
-
- return;
-
- } else {
-
- _activeAccount = recordAccount.account;
- _activePassword = recordAccount.password;
- _activeUrl = recordAccount.url;
- _activeUser = recordAccount.user;
- _directoryUser = [CCUtility getDirectoryActiveUser:self.activeUser activeUrl:self.activeUrl];
-
- if ([[CCUtility getKeyChainPasscodeForUUID:[CCUtility getUUID]] length] == 0) {
-
- _isCryptoCloudMode = NO;
-
- } else {
-
- _isCryptoCloudMode = YES;
- }
-
- if ([_activeAccount isEqualToString:[CCUtility getActiveAccountShareExt]]) {
-
- // load
-
- _serverUrl = [CCUtility getServerUrlShareExt];
-
- _destinyFolderButton.title = [NSString stringWithFormat:NSLocalizedString(@"_destiny_folder_", nil), [CCUtility getTitleServerUrlShareExt]];
-
- if (_isCryptoCloudMode)
- _localCryptated = [CCUtility getCryptatedShareExt];
-
- } else {
-
- // Default settings
-
- [CCUtility setActiveAccountShareExt:self.activeAccount];
- _serverUrl = [CCUtility getHomeServerUrlActiveUrl:self.activeUrl];
- [CCUtility setServerUrlShareExt:_serverUrl];
- _destinyFolderButton.title = [NSString stringWithFormat:NSLocalizedString(@"_destiny_folder_", nil), NSLocalizedString(@"_home_", nil)];
- [CCUtility setTitleServerUrlShareExt:NSLocalizedString(@"_home_", nil)];
- _localCryptated = NO;
- [CCUtility setCryptatedShareExt:NO];
- }
- }
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(triggerProgressTask:) name:@"NotificationProgressTask" object:nil];
-
- _filesName = [[NSMutableArray alloc] init];
- _filesSendCryptated = [[NSMutableArray alloc] init];
- _hud = [[CCHud alloc] initWithView:self.navigationController.view];
-
- _networkingOperationQueue = [NSOperationQueue new];
- _networkingOperationQueue.name = k_queue;
- _networkingOperationQueue.maxConcurrentOperationCount = 1;
-
- [[CCNetworking sharedNetworking] settingDelegate:self];
-
- [self.shareTable registerNib:[UINib nibWithNibName:@"CCCellShareExt" bundle:nil] forCellReuseIdentifier:@"ShareExtCell"];
-
- [self navigationBarToolBar];
-
- [self loadDataSwift];
- }
- // Apparirà
- - (void)viewWillAppear:(BOOL)animated
- {
- [super viewWillAppear:animated];
-
- if ([[CCUtility getBlockCode] length] > 0 && [CCUtility getOnlyLockDir] == NO)
- [self openBKPasscode];
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- - (void)closeShareViewController
- {
- [self.extensionContext completeRequestReturningItems:@[] completionHandler:nil];
- }
- //
- // L'applicazione terminerà
- //
- - (void)applicationWillTerminate:(UIApplication *)application
- {
- NSLog(@"[LOG] bye bye, Crypto Cloud Share Extension!");
- }
- #pragma --------------------------------------------------------------------------------------------
- #pragma mark == Action ==
- #pragma --------------------------------------------------------------------------------------------
- - (void)navigationBarToolBar
- {
- UIBarButtonItem *rightButtonUpload, *rightButtonEncrypt, *leftButtonCancel;
-
- self.navigationController.navigationBar.barTintColor = COLOR_NAVIGATIONBAR;
- self.navigationController.navigationBar.tintColor = COLOR_NAVIGATIONBAR_TEXT;
-
- self.toolBar.barTintColor = COLOR_TABBAR;
- self.toolBar.tintColor = COLOR_TABBAR_TEXT;
-
- // Upload
- if (self.localCryptated && _isCryptoCloudMode) {
-
- rightButtonUpload = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"_save_encrypted_", nil) style:UIBarButtonItemStylePlain target:self action:@selector(selectPost)];
- [rightButtonUpload setTintColor:COLOR_CRYPTOCLOUD];
-
- } else {
-
- rightButtonUpload = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"_save_", nil) style:UIBarButtonItemStylePlain target:self action:@selector(selectPost)];
- }
-
- // Encrypt ICON
- if (_isCryptoCloudMode) {
- UIImage *icon = [[UIImage imageNamed:image_shareExtEncrypt] imageWithRenderingMode:UIImageRenderingModeAutomatic];
- rightButtonEncrypt = [[UIBarButtonItem alloc] initWithImage:icon style:UIBarButtonItemStylePlain target:self action:@selector(changeEncrypt)];
- if (self.localCryptated) [rightButtonEncrypt setTintColor:COLOR_CRYPTOCLOUD];
- }
-
- // Cancel
- leftButtonCancel = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIBarButtonItemStylePlain target:self action:@selector(cancelPost)];
-
- // Title
- [self.navigationController.navigationBar setTitleTextAttributes: @{NSForegroundColorAttributeName:self.navigationController.navigationBar.tintColor}];
-
- self.navigationItem.title = k_brand;
- self.navigationItem.leftBarButtonItem = leftButtonCancel;
- self.navigationItem.rightBarButtonItems = [[NSArray alloc] initWithObjects:rightButtonUpload, rightButtonEncrypt, nil];
- self.navigationItem.hidesBackButton = YES;
- }
- - (void)moveServerUrlTo:(NSString *)serverUrlTo title:(NSString *)title selectedMetadatas:(NSArray *)selectedMetadatas
- {
- if (serverUrlTo)
- _serverUrl = serverUrlTo;
-
- if (title) {
- self.destinyFolderButton.title = [NSString stringWithFormat:NSLocalizedString(@"_destiny_folder_", nil), title];
- [CCUtility setTitleServerUrlShareExt:title];
- } else {
- self.destinyFolderButton.title = [NSString stringWithFormat:NSLocalizedString(@"_destiny_folder_", nil), NSLocalizedString(@"_home_", nil)];
- [CCUtility setTitleServerUrlShareExt:NSLocalizedString(@"_home_", nil)];
- }
-
- [CCUtility setActiveAccountShareExt:self.activeAccount];
- [CCUtility setServerUrlShareExt:_serverUrl];
- }
- - (IBAction)destinyFolderButtonTapped:(UIBarButtonItem *)sender
- {
- UINavigationController* navigationController = [[UIStoryboard storyboardWithName:@"CCMove" bundle:nil] instantiateViewControllerWithIdentifier:@"CCMove"];
-
- CCMove *viewController = (CCMove *)navigationController.topViewController;
-
- viewController.delegate = self;
- viewController.move.title = NSLocalizedString(@"_select_", nil);
- viewController.tintColor = tintColor;
- viewController.barTintColor = barTintColor;
- viewController.tintColorTitle = tintColor;
- viewController.networkingOperationQueue = _networkingOperationQueue;
- [navigationController setModalPresentationStyle:UIModalPresentationFormSheet];
- [self presentViewController:navigationController animated:YES completion:nil];
- }
- - (void)selectPost
- {
- if ([self.filesName count] > 0) {
-
- NSString *fileName = [self.filesName objectAtIndex:0];
-
- CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:_activeAccount];
-
- metadataNet.action = actionUploadFile;
- metadataNet.cryptated = _localCryptated;
- metadataNet.fileName = fileName;
- metadataNet.fileNamePrint = fileName;
- metadataNet.serverUrl = _serverUrl;
- metadataNet.session = k_upload_session_foreground;
- metadataNet.taskStatus = k_taskStatusResume;
-
- [self addNetworkingQueue:metadataNet];
-
- [self.hud visibleHudTitle:NSLocalizedString(@"_uploading_", nil) mode:MBProgressHUDModeDeterminateHorizontalBar color:self.view.tintColor];
-
- [self.hud AddButtonCancelWithTarget:self selector:@"cancelTransfer"];
- }
- else
- [self closeShareViewController];
- }
- - (void)cancelPost
- {
- // rimuoviamo i file+ico
- for (NSString *fileName in self.filesName) {
-
- [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", self.directoryUser, fileName] error:nil];
- [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.ico", self.directoryUser, fileName] error:nil];
- }
-
- [self closeShareViewController];
- }
- - (void)changeEncrypt
- {
- if (self.localCryptated) self.localCryptated = NO;
- else self.localCryptated = YES;
-
- [CCUtility setCryptatedShareExt:self.localCryptated];
- [self navigationBarToolBar];
- }
- - (void)cancelTransfer
- {
- [_networkingOperationQueue cancelAllOperations];
- }
- #pragma --------------------------------------------------------------------------------------------
- #pragma mark ======================= NetWorking ==================================
- #pragma --------------------------------------------------------------------------------------------
- - (void)triggerProgressTask:(NSNotification *)notification
- {
- NSDictionary *dict = notification.userInfo;
- float progress = [[dict valueForKey:@"progress"] floatValue];
- [self.hud progress:progress];
- }
- - (void)uploadFileFailure:(CCMetadataNet *)metadataNet fileID:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector message:(NSString *)message errorCode:(NSInteger)errorCode
- {
- [self.hud hideHud];
-
- // remove file
- [CCCoreData deleteMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", fileID, _activeAccount]];
-
- [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", _directoryUser, fileID] error:nil];
- [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.ico", _directoryUser, fileID] error:nil];
- // message error
- if (errorCode != kCFURLErrorCancelled) {
-
- UIAlertController * alert= [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_", nil) message:message preferredStyle:UIAlertControllerStyleAlert];
- UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
- handler:^(UIAlertAction * action) {
- [alert dismissViewControllerAnimated:YES completion:nil];
- [self closeShareViewController];
- }];
- [alert addAction:ok];
- [self presentViewController:alert animated:YES completion:nil];
- }
- else
- [self closeShareViewController];
- }
- - (void)uploadFileSuccess:(CCMetadataNet *)metadataNet fileID:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector selectorPost:(NSString *)selectorPost
- {
- [self.hud hideHud];
-
- CCMetadata *metadata = [CCCoreData getMetadataWithPreficate:[NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", fileID, _activeAccount] context:nil];
-
- [self.filesName removeObject:metadata.fileNamePrint];
- [self.shareTable performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
-
- [self performSelector:@selector(selectPost) withObject:nil afterDelay:0.1];
- }
- - (void)addNetworkingQueue:(CCMetadataNet *)metadataNet
- {
- id operation;
-
- operation = [[OCnetworking alloc] initWithDelegate:self metadataNet:metadataNet withUser:_activeUser withPassword:_activePassword withUrl:_activeUrl isCryptoCloudMode:_isCryptoCloudMode];
-
- [operation setQueuePriority:metadataNet.priority];
-
- [_networkingOperationQueue addOperation:operation];
- }
- #pragma --------------------------------------------------------------------------------------------
- #pragma mark ===== Lock Password =====
- #pragma --------------------------------------------------------------------------------------------
- - (void)openBKPasscode
- {
- CCBKPasscode *viewController = [[CCBKPasscode alloc] initWithNibName:nil bundle:nil];
- viewController.delegate = self;
- 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:k_serviceShareKeyChain];
- touchIDManager.promptText = NSLocalizedString(@"_scan_fingerprint_", nil);
- viewController.touchIDManager = touchIDManager;
- viewController.title = k_brand;
- viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(passcodeViewCloseButtonPressed:)];
- viewController.navigationItem.leftBarButtonItem.tintColor = COLOR_CRYPTOCLOUD;
-
- UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
- [self presentViewController:navController animated:YES completion:nil];
- }
- - (NSUInteger)passcodeViewControllerNumberOfFailedAttempts:(CCBKPasscode *)aViewController
- {
- return self.failedAttempts;
- }
- - (NSDate *)passcodeViewControllerLockUntilDate:(CCBKPasscode *)aViewController
- {
- return self.lockUntilDate;
- }
- - (void)passcodeViewCloseButtonPressed:(id)sender
- {
- [self dismissViewControllerAnimated:YES completion:^{
- [self performSelector:@selector(closeShareViewController) withObject:nil];
- }];
- }
- - (void)passcodeViewController:(CCBKPasscode *)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)passcodeViewController:(CCBKPasscode *)aViewController didFinishWithPasscode:(NSString *)aPasscode
- {
- [aViewController dismissViewControllerAnimated:YES completion:nil];
- }
- #pragma --------------------------------------------------------------------------------------------
- #pragma mark ===== Swipe Table DELETE -> menu =====
- #pragma--------------------------------------------------------------------------------------------
- - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
- {
- [self setEditing:NO animated:YES];
-
- NSString *fileName = [self.filesName objectAtIndex:indexPath.row];
-
- [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", self.directoryUser, fileName] error:nil];
- [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.ico", self.directoryUser, fileName] error:nil];
- [self.filesName removeObjectAtIndex:indexPath.row];
-
- if ([self.filesName count] == 0) [self closeShareViewController];
- else [self.shareTable performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
- }
- #pragma --------------------------------------------------------------------------------------------
- #pragma mark == Table ==
- #pragma --------------------------------------------------------------------------------------------
- - (void)loadDataSwift
- {
- CCloadItemData *loadItem = [[CCloadItemData alloc] init];
-
- [loadItem loadFiles:self.directoryUser extensionContext:self.extensionContext vc:self];
- }
- - (void)reloadData:(NSArray *)files
- {
- totalSize = 0;
- for (NSString *file in files) {
-
- NSUInteger fileSize = (NSInteger)[[[NSFileManager defaultManager] attributesOfItemAtPath:[NSString stringWithFormat:@"%@/%@", self.directoryUser, file] error:nil] fileSize];
-
- totalSize += fileSize;
-
- // creiamo l'ICO
- CFStringRef fileExtension = (__bridge CFStringRef)[file pathExtension];
- CFStringRef fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);
-
- if (fileSize > 0 && ((UTTypeConformsTo(fileUTI, kUTTypeImage)) || (UTTypeConformsTo(fileUTI, kUTTypeMovie)))) {
-
- NSString *typeFile;
- if (UTTypeConformsTo(fileUTI, kUTTypeImage)) typeFile = k_metadataTypeFile_image;
- if (UTTypeConformsTo(fileUTI, kUTTypeMovie)) typeFile = k_metadataTypeFile_video;
-
- [CCGraphics createNewImageFrom:file directoryUser:self.directoryUser fileNameTo:file fileNamePrint:nil size:@"m" imageForUpload:NO typeFile:typeFile writePreview:YES optimizedFileName:NO];
- }
- }
-
- if (totalSize > 0) {
-
- self.filesName = [[NSMutableArray alloc] initWithArray:files];
- [self.shareTable reloadData];
-
- } else {
-
- [self closeShareViewController];
- }
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- return 80;
- }
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- return 1;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- return [self.filesName count];
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- NSString *fileName = [self.filesName objectAtIndex:indexPath.row];
- UIImage *image = nil;
-
- CFStringRef fileExtension = (__bridge CFStringRef)[fileName pathExtension];
- CFStringRef fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);
- if (UTTypeConformsTo(fileUTI, kUTTypeZipArchive) && [(__bridge NSString *)fileUTI containsString:@"org.openxmlformats"] == NO) image = [UIImage imageNamed:image_file_compress];
- else if (UTTypeConformsTo(fileUTI, kUTTypeAudio)) image = [UIImage imageNamed:image_file_audio];
- else if ((UTTypeConformsTo(fileUTI, kUTTypeImage)) || (UTTypeConformsTo(fileUTI, kUTTypeMovie))) {
-
- image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", self.directoryUser, fileName]];
-
- }
- else if (UTTypeConformsTo(fileUTI, kUTTypeContent)) {
-
- image = [UIImage imageNamed:image_document];
-
- NSString *typeFile = (__bridge NSString *)fileUTI;
-
- if ([typeFile isEqualToString:@"com.adobe.pdf"]) image = [UIImage imageNamed:image_file_pdf];
- if ([typeFile isEqualToString:@"org.openxmlformats.spreadsheetml.sheet"]) image = [UIImage imageNamed:image_file_xls];
- if ([typeFile isEqualToString:@"public.plain-text"]) image = [UIImage imageNamed:image_file_txt];
- }
- else image = [UIImage imageNamed:image_file];
-
- CCCellShareExt *cell = (CCCellShareExt *)[tableView dequeueReusableCellWithIdentifier:@"ShareExtCell" forIndexPath:indexPath];
-
- NSUInteger fileSize = (NSInteger)[[[NSFileManager defaultManager] attributesOfItemAtPath:[NSString stringWithFormat:@"%@/%@", self.directoryUser, fileName] error:nil] fileSize];
-
- cell.labelInformazioni.text = [NSString stringWithFormat:@"%@\r\r%@", fileName, [CCUtility transformedSize:fileSize]];
- cell.labelInformazioni.textColor = [UIColor blackColor];
- cell.fileImageView.image = image;
-
- return cell;
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- [tableView deselectRowAtIndexPath:indexPath animated:YES];
- }
- @end
|