ShareViewController.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. //
  2. // ShareViewController.m
  3. // Nextcloud iOS
  4. //
  5. // Created by Marino Faggiana on 26/01/16.
  6. // Copyright (c) 2017 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 "ShareViewController.h"
  24. #import "NCBridgeSwift.h"
  25. @import MobileCoreServices;
  26. @interface ShareViewController ()
  27. {
  28. NSURL *dirGroup;
  29. NSUInteger totalSize;
  30. NSExtensionItem *inputItem;
  31. UIColor *barTintColor;
  32. UIColor *tintColor;
  33. }
  34. @end
  35. @implementation ShareViewController
  36. #pragma --------------------------------------------------------------------------------------------
  37. #pragma mark ===== View =====
  38. #pragma --------------------------------------------------------------------------------------------
  39. -(void)viewDidLoad
  40. {
  41. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  42. if (recordAccount == nil) {
  43. // close now
  44. [self performSelector:@selector(closeShareViewController) withObject:nil afterDelay:0.1];
  45. return;
  46. } else {
  47. _activeAccount = recordAccount.account;
  48. _activePassword = recordAccount.password;
  49. _activeUrl = recordAccount.url;
  50. _activeUser = recordAccount.user;
  51. _activeUserID = recordAccount.userID;
  52. _directoryUser = [CCUtility getDirectoryActiveUser:self.activeUser activeUrl:self.activeUrl];
  53. if ([_activeAccount isEqualToString:[CCUtility getActiveAccountExt]]) {
  54. // load
  55. _serverUrl = [CCUtility getServerUrlExt];
  56. _destinyFolderButton.title = [NSString stringWithFormat:NSLocalizedString(@"_destiny_folder_", nil), [CCUtility getTitleServerUrlExt]];
  57. } else {
  58. // Default settings
  59. [CCUtility setActiveAccountExt:self.activeAccount];
  60. _serverUrl = [CCUtility getHomeServerUrlActiveUrl:self.activeUrl];
  61. [CCUtility setServerUrlExt:_serverUrl];
  62. _destinyFolderButton.title = [NSString stringWithFormat:NSLocalizedString(@"_destiny_folder_", nil), NSLocalizedString(@"_home_", nil)];
  63. [CCUtility setTitleServerUrlExt:NSLocalizedString(@"_home_", nil)];
  64. }
  65. }
  66. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(triggerProgressTask:) name:@"NotificationProgressTask" object:nil];
  67. _filesName = [[NSMutableArray alloc] init];
  68. _hud = [[CCHud alloc] initWithView:self.navigationController.view];
  69. _networkingOperationQueue = [NSOperationQueue new];
  70. _networkingOperationQueue.name = k_queue;
  71. _networkingOperationQueue.maxConcurrentOperationCount = 1;
  72. [CCNetworking sharedNetworking].delegate = self;
  73. [self.shareTable registerNib:[UINib nibWithNibName:@"CCCellShareExt" bundle:nil] forCellReuseIdentifier:@"ShareExtCell"];
  74. [self navigationBarToolBar];
  75. [self loadDataSwift];
  76. }
  77. // Apparirà
  78. - (void)viewWillAppear:(BOOL)animated
  79. {
  80. [super viewWillAppear:animated];
  81. // BUGFIX 2.17 - Change user Nextcloud App
  82. [[CCNetworking sharedNetworking] settingAccount];
  83. if ([[CCUtility getBlockCode] length] > 0 && [CCUtility getOnlyLockDir] == NO)
  84. [self openBKPasscode];
  85. }
  86. - (void)didReceiveMemoryWarning
  87. {
  88. [super didReceiveMemoryWarning];
  89. // Dispose of any resources that can be recreated.
  90. }
  91. - (void)closeShareViewController
  92. {
  93. [self.extensionContext completeRequestReturningItems:@[] completionHandler:nil];
  94. }
  95. //
  96. // L'applicazione terminerà
  97. //
  98. - (void)applicationWillTerminate:(UIApplication *)application
  99. {
  100. NSLog(@"[LOG] bye bye, Nextcloud Share Extension!");
  101. }
  102. #pragma --------------------------------------------------------------------------------------------
  103. #pragma mark == Action ==
  104. #pragma --------------------------------------------------------------------------------------------
  105. - (void)navigationBarToolBar
  106. {
  107. UIBarButtonItem *rightButtonUpload, *leftButtonCancel;
  108. // Theming
  109. tableCapabilities *capabilities = [[NCManageDatabase sharedInstance] getCapabilites];
  110. if ([NCBrandOptions sharedInstance].use_themingColor && capabilities.themingColor.length > 0) {
  111. UIColor *newColor = [CCGraphics colorFromHexString:capabilities.themingColor];
  112. if (newColor)
  113. [NCBrandColor sharedInstance].brand = newColor;
  114. }
  115. self.navigationController.navigationBar.barTintColor = [NCBrandColor sharedInstance].brand;
  116. self.navigationController.navigationBar.tintColor = [NCBrandColor sharedInstance].navigationBarText;
  117. self.toolBar.barTintColor = [NCBrandColor sharedInstance].tabBar;
  118. self.toolBar.tintColor = [NCBrandColor sharedInstance].brand;
  119. // Upload
  120. rightButtonUpload = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"_save_", nil) style:UIBarButtonItemStylePlain target:self action:@selector(selectPost)];
  121. // Cancel
  122. leftButtonCancel = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIBarButtonItemStylePlain target:self action:@selector(cancelPost)];
  123. // Title
  124. [self.navigationController.navigationBar setTitleTextAttributes: @{NSForegroundColorAttributeName:self.navigationController.navigationBar.tintColor}];
  125. self.navigationItem.title = [NCBrandOptions sharedInstance].brand;
  126. self.navigationItem.leftBarButtonItem = leftButtonCancel;
  127. self.navigationItem.rightBarButtonItems = [[NSArray alloc] initWithObjects:rightButtonUpload, nil];
  128. self.navigationItem.hidesBackButton = YES;
  129. }
  130. - (void)moveServerUrlTo:(NSString *)serverUrlTo title:(NSString *)title
  131. {
  132. if (serverUrlTo)
  133. _serverUrl = serverUrlTo;
  134. if (title) {
  135. self.destinyFolderButton.title = [NSString stringWithFormat:NSLocalizedString(@"_destiny_folder_", nil), title];
  136. [CCUtility setTitleServerUrlExt:title];
  137. } else {
  138. self.destinyFolderButton.title = [NSString stringWithFormat:NSLocalizedString(@"_destiny_folder_", nil), NSLocalizedString(@"_home_", nil)];
  139. [CCUtility setTitleServerUrlExt:NSLocalizedString(@"_home_", nil)];
  140. }
  141. [CCUtility setActiveAccountExt:self.activeAccount];
  142. [CCUtility setServerUrlExt:_serverUrl];
  143. }
  144. - (IBAction)destinyFolderButtonTapped:(UIBarButtonItem *)sender
  145. {
  146. UINavigationController* navigationController = [[UIStoryboard storyboardWithName:@"CCMove" bundle:nil] instantiateViewControllerWithIdentifier:@"CCMove"];
  147. CCMove *viewController = (CCMove *)navigationController.topViewController;
  148. viewController.delegate = self;
  149. viewController.move.title = NSLocalizedString(@"_select_", nil);
  150. viewController.tintColor = tintColor;
  151. viewController.barTintColor = barTintColor;
  152. viewController.tintColorTitle = tintColor;
  153. viewController.networkingOperationQueue = _networkingOperationQueue;
  154. [navigationController setModalPresentationStyle:UIModalPresentationFormSheet];
  155. [self presentViewController:navigationController animated:YES completion:nil];
  156. }
  157. - (void)selectPost
  158. {
  159. if ([self.filesName count] > 0) {
  160. NSString *fileName = [self.filesName objectAtIndex:0];
  161. [[CCNetworking sharedNetworking] uploadFile:fileName serverUrl:_serverUrl session:k_upload_session_foreground taskStatus:k_taskStatusResume selector:@"" selectorPost:@"" errorCode:0 delegate:self];
  162. [self.hud visibleHudTitle:NSLocalizedString(@"_uploading_", nil) mode:MBProgressHUDModeDeterminate color:[NCBrandColor sharedInstance].brand];
  163. }
  164. else
  165. [self closeShareViewController];
  166. }
  167. - (void)cancelPost
  168. {
  169. // rimuoviamo i file+ico
  170. for (NSString *fileName in self.filesName) {
  171. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", self.directoryUser, fileName] error:nil];
  172. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.ico", self.directoryUser, fileName] error:nil];
  173. }
  174. [self closeShareViewController];
  175. }
  176. #pragma --------------------------------------------------------------------------------------------
  177. #pragma mark ======================= NetWorking ==================================
  178. #pragma --------------------------------------------------------------------------------------------
  179. - (void)triggerProgressTask:(NSNotification *)notification
  180. {
  181. NSDictionary *dict = notification.userInfo;
  182. float progress = [[dict valueForKey:@"progress"] floatValue];
  183. [self.hud progress:progress];
  184. }
  185. - (void)uploadFileFailure:(CCMetadataNet *)metadataNet fileID:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector message:(NSString *)message errorCode:(NSInteger)errorCode
  186. {
  187. [self.hud hideHud];
  188. // remove file
  189. [[NCManageDatabase sharedInstance] deleteMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID = %@", fileID] clearDateReadDirectoryID:nil];
  190. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", _directoryUser, fileID] error:nil];
  191. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.ico", _directoryUser, fileID] error:nil];
  192. // message error
  193. if (errorCode != kCFURLErrorCancelled) {
  194. UIAlertController * alert= [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_", nil) message:message preferredStyle:UIAlertControllerStyleAlert];
  195. UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
  196. handler:^(UIAlertAction * action) {
  197. [alert dismissViewControllerAnimated:YES completion:nil];
  198. [self closeShareViewController];
  199. }];
  200. [alert addAction:ok];
  201. [self presentViewController:alert animated:YES completion:nil];
  202. }
  203. else
  204. [self closeShareViewController];
  205. }
  206. - (void)uploadFileSuccess:(CCMetadataNet *)metadataNet fileID:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector selectorPost:(NSString *)selectorPost
  207. {
  208. [self.hud hideHud];
  209. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID = %@", fileID]];
  210. [self.filesName removeObject:metadata.fileName];
  211. [self.shareTable performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
  212. [self performSelector:@selector(selectPost) withObject:nil afterDelay:0.1];
  213. }
  214. - (void)addNetworkingQueue:(CCMetadataNet *)metadataNet
  215. {
  216. id operation;
  217. operation = [[OCnetworking alloc] initWithDelegate:self metadataNet:metadataNet withUser:_activeUser withUserID:_activeUserID withPassword:_activePassword withUrl:_activeUrl];
  218. [operation setQueuePriority:metadataNet.priority];
  219. [_networkingOperationQueue addOperation:operation];
  220. }
  221. #pragma --------------------------------------------------------------------------------------------
  222. #pragma mark ===== Lock Password =====
  223. #pragma --------------------------------------------------------------------------------------------
  224. - (void)openBKPasscode
  225. {
  226. CCBKPasscode *viewController = [[CCBKPasscode alloc] initWithNibName:nil bundle:nil];
  227. viewController.delegate = self;
  228. viewController.type = BKPasscodeViewControllerCheckPasscodeType;
  229. viewController.inputViewTitlePassword = YES;
  230. if ([CCUtility getSimplyBlockCode]) {
  231. viewController.passcodeStyle = BKPasscodeInputViewNumericPasscodeStyle;
  232. viewController.passcodeInputView.maximumLength = 6;
  233. } else {
  234. viewController.passcodeStyle = BKPasscodeInputViewNormalPasscodeStyle;
  235. viewController.passcodeInputView.maximumLength = 64;
  236. }
  237. BKTouchIDManager *touchIDManager = [[BKTouchIDManager alloc] initWithKeychainServiceName:k_serviceShareKeyChain];
  238. touchIDManager.promptText = NSLocalizedString(@"_scan_fingerprint_", nil);
  239. viewController.touchIDManager = touchIDManager;
  240. viewController.title = [NCBrandOptions sharedInstance].brand;
  241. viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(passcodeViewCloseButtonPressed:)];
  242. //viewController.navigationItem.leftBarButtonItem.tintColor = [NCBrandColor sharedInstance].cryptocloud;
  243. UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
  244. [self presentViewController:navController animated:YES completion:nil];
  245. }
  246. - (NSUInteger)passcodeViewControllerNumberOfFailedAttempts:(CCBKPasscode *)aViewController
  247. {
  248. return self.failedAttempts;
  249. }
  250. - (NSDate *)passcodeViewControllerLockUntilDate:(CCBKPasscode *)aViewController
  251. {
  252. return self.lockUntilDate;
  253. }
  254. - (void)passcodeViewCloseButtonPressed:(id)sender
  255. {
  256. [self dismissViewControllerAnimated:YES completion:^{
  257. [self performSelector:@selector(closeShareViewController) withObject:nil];
  258. }];
  259. }
  260. - (void)passcodeViewController:(CCBKPasscode *)aViewController authenticatePasscode:(NSString *)aPasscode resultHandler:(void (^)(BOOL))aResultHandler
  261. {
  262. if ([aPasscode isEqualToString:[CCUtility getBlockCode]]) {
  263. self.lockUntilDate = nil;
  264. self.failedAttempts = 0;
  265. aResultHandler(YES);
  266. } else aResultHandler(NO);
  267. }
  268. - (void)passcodeViewController:(CCBKPasscode *)aViewController didFinishWithPasscode:(NSString *)aPasscode
  269. {
  270. [aViewController dismissViewControllerAnimated:YES completion:nil];
  271. }
  272. #pragma --------------------------------------------------------------------------------------------
  273. #pragma mark ===== Swipe Table DELETE -> menu =====
  274. #pragma--------------------------------------------------------------------------------------------
  275. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  276. {
  277. [self setEditing:NO animated:YES];
  278. NSString *fileName = [self.filesName objectAtIndex:indexPath.row];
  279. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", self.directoryUser, fileName] error:nil];
  280. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.ico", self.directoryUser, fileName] error:nil];
  281. [self.filesName removeObjectAtIndex:indexPath.row];
  282. if ([self.filesName count] == 0) [self closeShareViewController];
  283. else [self.shareTable performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
  284. }
  285. #pragma --------------------------------------------------------------------------------------------
  286. #pragma mark == Table ==
  287. #pragma --------------------------------------------------------------------------------------------
  288. - (void)loadDataSwift
  289. {
  290. CCloadItemData *loadItem = [[CCloadItemData alloc] init];
  291. [loadItem loadFiles:self.directoryUser extensionContext:self.extensionContext vc:self];
  292. }
  293. - (void)reloadData:(NSArray *)files
  294. {
  295. totalSize = 0;
  296. for (NSString *file in files) {
  297. NSUInteger fileSize = (NSInteger)[[[NSFileManager defaultManager] attributesOfItemAtPath:[NSString stringWithFormat:@"%@/%@", self.directoryUser, file] error:nil] fileSize];
  298. totalSize += fileSize;
  299. // creiamo l'ICO
  300. CFStringRef fileExtension = (__bridge CFStringRef)[file pathExtension];
  301. CFStringRef fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);
  302. if (fileSize > 0 && ((UTTypeConformsTo(fileUTI, kUTTypeImage)) || (UTTypeConformsTo(fileUTI, kUTTypeMovie)))) {
  303. NSString *typeFile;
  304. if (UTTypeConformsTo(fileUTI, kUTTypeImage)) typeFile = k_metadataTypeFile_image;
  305. if (UTTypeConformsTo(fileUTI, kUTTypeMovie)) typeFile = k_metadataTypeFile_video;
  306. [CCGraphics createNewImageFrom:file directoryUser:self.directoryUser fileNameTo:file extension:nil size:@"m" imageForUpload:NO typeFile:typeFile writePreview:YES optimizedFileName:NO];
  307. }
  308. }
  309. if (totalSize > 0) {
  310. self.filesName = [[NSMutableArray alloc] initWithArray:files];
  311. [self.shareTable reloadData];
  312. } else {
  313. [self closeShareViewController];
  314. }
  315. }
  316. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  317. {
  318. return 80;
  319. }
  320. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  321. {
  322. return 1;
  323. }
  324. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  325. {
  326. return [self.filesName count];
  327. }
  328. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  329. {
  330. NSString *fileName = [self.filesName objectAtIndex:indexPath.row];
  331. UIImage *image = nil;
  332. CFStringRef fileExtension = (__bridge CFStringRef)[fileName pathExtension];
  333. CFStringRef fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);
  334. if (UTTypeConformsTo(fileUTI, kUTTypeZipArchive) && [(__bridge NSString *)fileUTI containsString:@"org.openxmlformats"] == NO) image = [UIImage imageNamed:@"file_compress"];
  335. else if (UTTypeConformsTo(fileUTI, kUTTypeAudio)) image = [UIImage imageNamed:@"file_audio"];
  336. else if ((UTTypeConformsTo(fileUTI, kUTTypeImage)) || (UTTypeConformsTo(fileUTI, kUTTypeMovie))) {
  337. image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", self.directoryUser, fileName]];
  338. }
  339. else if (UTTypeConformsTo(fileUTI, kUTTypeContent)) {
  340. image = [UIImage imageNamed:@"document"];
  341. NSString *typeFile = (__bridge NSString *)fileUTI;
  342. if ([typeFile isEqualToString:@"com.adobe.pdf"]) image = [UIImage imageNamed:@"file_pdf"];
  343. if ([typeFile isEqualToString:@"org.openxmlformats.spreadsheetml.sheet"]) image = [UIImage imageNamed:@"file_xls"];
  344. if ([typeFile isEqualToString:@"public.plain-text"]) image = [UIImage imageNamed:@"file_txt"];
  345. }
  346. else image = [UIImage imageNamed:@"file"];
  347. CCCellShareExt *cell = (CCCellShareExt *)[tableView dequeueReusableCellWithIdentifier:@"ShareExtCell" forIndexPath:indexPath];
  348. NSUInteger fileSize = (NSInteger)[[[NSFileManager defaultManager] attributesOfItemAtPath:[NSString stringWithFormat:@"%@/%@", self.directoryUser, fileName] error:nil] fileSize];
  349. cell.labelInformazioni.text = [NSString stringWithFormat:@"%@\r\r%@", fileName, [CCUtility transformedSize:fileSize]];
  350. cell.labelInformazioni.textColor = [UIColor blackColor];
  351. cell.fileImageView.image = image;
  352. return cell;
  353. }
  354. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  355. {
  356. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  357. }
  358. @end