NCSelectDestination.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. //
  2. // CCMove.m
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 04/09/14.
  6. // Copyright (c) 2017 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  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 "NCSelectDestination.h"
  24. #import "NCBridgeSwift.h"
  25. @interface NCSelectDestination ()
  26. {
  27. NSString *activeAccount;
  28. NSString *activeUrl;
  29. BOOL _loadingFolder;
  30. // Automatic Upload Folder
  31. NSString *_autoUploadFileName;
  32. NSString *_autoUploadDirectory;
  33. NSPredicate *predicateDataSource;
  34. }
  35. @end
  36. @implementation NCSelectDestination
  37. // MARK: - View
  38. - (void)viewDidLoad
  39. {
  40. [super viewDidLoad];
  41. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  42. if (tableAccount) {
  43. activeAccount = tableAccount.account;
  44. activeUrl = tableAccount.url;
  45. } else {
  46. UIAlertController * alert= [UIAlertController alertControllerWithTitle:nil message:NSLocalizedString(@"_no_active_account_", nil) preferredStyle:UIAlertControllerStyleAlert];
  47. UIAlertAction* ok = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
  48. [alert dismissViewControllerAnimated:YES completion:nil];
  49. }];
  50. [alert addAction:ok];
  51. [self presentViewController:alert animated:YES completion:nil];
  52. }
  53. [self.cancel setTitle:NSLocalizedString(@"_cancel_", nil)];
  54. [self.create setTitle:NSLocalizedString(@"_create_folder_", nil)];
  55. if (![_serverUrl length]) {
  56. _serverUrl = [CCUtility getHomeServerUrlActiveUrl:activeUrl];
  57. [self.navigationController.navigationBar.topItem setTitleView:[[UIImageView alloc] initWithImage: [UIImage imageNamed:@"themingLogo"]]];
  58. self.title = @"Home";
  59. } else {
  60. UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0,0, self.navigationItem.titleView.frame.size.width, 40)];
  61. label.text = self.passMetadata.fileNameView;
  62. label.textColor = NCBrandColor.sharedInstance.brandText;
  63. label.backgroundColor =[UIColor clearColor];
  64. label.textAlignment = NSTextAlignmentCenter;
  65. self.navigationItem.titleView=label;
  66. }
  67. // TableView : at the end of rows nothing
  68. self.tableView.tableFooterView = [UIView new];
  69. self.tableView.separatorColor = NCBrandColor.sharedInstance.separator;
  70. self.tableView.emptyDataSetDelegate = self;
  71. self.tableView.emptyDataSetSource = self;
  72. // get auto upload folder
  73. _autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  74. _autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:activeUrl];
  75. // read file->folder
  76. [self readFolder];
  77. }
  78. // Apparirà
  79. - (void)viewWillAppear:(BOOL)animated
  80. {
  81. [super viewWillAppear:animated];
  82. self.navigationController.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand;
  83. self.navigationController.navigationBar.tintColor = NCBrandColor.sharedInstance.brandText;
  84. self.navigationController.toolbar.barTintColor = NCBrandColor.sharedInstance.tabBar;
  85. self.navigationController.toolbar.tintColor = NCBrandColor.sharedInstance.brandElement;
  86. if (self.hideCreateFolder) {
  87. [self.create setEnabled:NO];
  88. [self.create setTintColor: [UIColor clearColor]];
  89. }
  90. if (self.hideMoveutton) {
  91. [self.move setEnabled:NO];
  92. [self.move setTintColor: [UIColor clearColor]];
  93. }
  94. self.view.backgroundColor = NCBrandColor.sharedInstance.backgroundView;
  95. self.tableView.backgroundColor = NCBrandColor.sharedInstance.backgroundView;
  96. }
  97. #pragma --------------------------------------------------------------------------------------------
  98. #pragma mark ==== DZNEmptyDataSetSource ====
  99. #pragma --------------------------------------------------------------------------------------------
  100. - (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView
  101. {
  102. if (_loadingFolder)
  103. return YES;
  104. else
  105. return NO;
  106. }
  107. - (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView
  108. {
  109. return NO;
  110. }
  111. - (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
  112. {
  113. return NCBrandColor.sharedInstance.backgroundView;
  114. }
  115. - (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView
  116. {
  117. if (_loadingFolder) {
  118. UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
  119. activityView.transform = CGAffineTransformMakeScale(1.5f, 1.5f);
  120. activityView.color = NCBrandColor.sharedInstance.brandElement;
  121. [activityView startAnimating];
  122. return activityView;
  123. }
  124. return nil;
  125. }
  126. // MARK: - IBAction
  127. - (IBAction)cancel:(UIBarButtonItem *)sender
  128. {
  129. if ([self.delegate respondsToSelector:@selector(dismissMove)])
  130. [self.delegate dismissMove];
  131. [self dismissViewControllerAnimated:YES completion:nil];
  132. }
  133. - (IBAction)move:(UIBarButtonItem *)sender
  134. {
  135. if ([self.delegate respondsToSelector:@selector(dismissMove)])
  136. [self.delegate dismissMove];
  137. [self.delegate moveServerUrlTo:_serverUrl title:self.passMetadata.fileNameView type:self.type];
  138. [self dismissViewControllerAnimated:YES completion:nil];
  139. }
  140. - (IBAction)create:(UIBarButtonItem *)sender
  141. {
  142. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_create_folder_",nil) message:@"" preferredStyle:UIAlertControllerStyleAlert];
  143. [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
  144. //textField.placeholder = NSLocalizedString(@"LoginPlaceholder", @"Login");
  145. textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
  146. }];
  147. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_save_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  148. [self createFolder:alertController.textFields.firstObject.text];
  149. }]];
  150. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  151. }]];
  152. [self presentViewController:alertController animated:YES completion:nil];
  153. }
  154. // MARK: - BKPasscodeViewController
  155. - (void)passcodeViewController:(CCBKPasscode *)aViewController didFinishWithPasscode:(NSString *)aPasscode
  156. {
  157. [aViewController dismissViewControllerAnimated:YES completion:nil];
  158. [self performSegueDirectoryWithControlPasscode:false];
  159. }
  160. - (void)passcodeViewController:(BKPasscodeViewController *)aViewController authenticatePasscode:(NSString *)aPasscode resultHandler:(void (^)(BOOL))aResultHandler
  161. {
  162. if ([aPasscode isEqualToString:[CCUtility getBlockCode]]) {
  163. self.lockUntilDate = nil;
  164. self.failedAttempts = 0;
  165. aResultHandler(YES);
  166. } else {
  167. aResultHandler(NO);
  168. }
  169. }
  170. - (void)passcodeViewControllerDidFailAttempt:(BKPasscodeViewController *)aViewController
  171. {
  172. self.failedAttempts++;
  173. if (self.failedAttempts > 5) {
  174. NSTimeInterval timeInterval = 60;
  175. if (self.failedAttempts > 6) {
  176. NSUInteger multiplier = self.failedAttempts - 6;
  177. timeInterval = (5 * 60) * multiplier;
  178. if (timeInterval > 3600 * 24) {
  179. timeInterval = 3600 * 24;
  180. }
  181. }
  182. self.lockUntilDate = [NSDate dateWithTimeIntervalSinceNow:timeInterval];
  183. }
  184. }
  185. - (NSUInteger)passcodeViewControllerNumberOfFailedAttempts:(BKPasscodeViewController *)aViewController
  186. {
  187. return self.failedAttempts;
  188. }
  189. - (NSDate *)passcodeViewControllerLockUntilDate:(BKPasscodeViewController *)aViewController
  190. {
  191. return self.lockUntilDate;
  192. }
  193. - (void)passcodeViewCloseButtonPressed:(id)sender
  194. {
  195. [self dismissViewControllerAnimated:YES completion:nil];
  196. }
  197. // MARK: - Read Folder
  198. - (void)readFolder
  199. {
  200. [[NCCommunication sharedInstance] readFileOrFolderWithServerUrlFileName:_serverUrl depth:@"1" account:activeAccount completionHandler:^(NSString *account, NSArray<NCFile *> *files, NSInteger errorCode, NSString *errorDecription) {
  201. if (errorCode == 0 && files.count >= 1) {
  202. NCFile *fileDirectory = files[0];
  203. // Update directory etag
  204. [[NCManageDatabase sharedInstance] setDirectoryWithServerUrl:_serverUrl serverUrlTo:nil etag:fileDirectory.etag ocId:fileDirectory.ocId encrypted:fileDirectory.e2eEncrypted account:account];
  205. // Delete metadata
  206. [[NCManageDatabase sharedInstance] deleteMetadataWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND (status == %d OR status == %d)", account, _serverUrl, k_metadataStatusNormal, k_metadataStatusHide]];
  207. // In download
  208. NSArray *metadatasInDownload = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND (status == %d OR status == %d OR status == %d OR status == %d)", account, _serverUrl, k_metadataStatusWaitDownload, k_metadataStatusInDownload, k_metadataStatusDownloading, k_metadataStatusDownloadError] sorted:nil ascending:NO];
  209. // Insert in Database
  210. [[NCManageDatabase sharedInstance] addMetadatasWithFiles:files account:account serverUrl:_serverUrl removeFirst:true];
  211. // reinsert metadatas in Download
  212. if (metadatasInDownload) {
  213. (void)[[NCManageDatabase sharedInstance] addMetadatas:metadatasInDownload];
  214. }
  215. } else {
  216. self.move.enabled = NO;
  217. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_",nil) message:errorDecription preferredStyle:UIAlertControllerStyleAlert];
  218. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  219. }]];
  220. [self presentViewController:alertController animated:YES completion:nil];
  221. }
  222. _loadingFolder = NO;
  223. [self.tableView reloadData];
  224. }];
  225. _loadingFolder = YES;
  226. [self.tableView reloadData];
  227. }
  228. // MARK: - Create Folder
  229. - (void)createFolder:(NSString *)fileNameFolder
  230. {
  231. NSString *serverUrlFileName = [NSString stringWithFormat:@"%@/%@", _serverUrl, fileNameFolder];
  232. [[NCCommunication sharedInstance] createFolder:serverUrlFileName account:activeAccount completionHandler:^(NSString *account, NSString *ocID, NSDate *date, NSInteger errorCode, NSString *errorDecription) {
  233. if (errorCode == 0) {
  234. [self readFolder];
  235. } else {
  236. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_",nil) message:errorDecription preferredStyle:UIAlertControllerStyleAlert];
  237. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { }]];
  238. [self presentViewController:alertController animated:YES completion:nil];
  239. }
  240. }];
  241. }
  242. // MARK: - Table
  243. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  244. {
  245. return 1;
  246. }
  247. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  248. {
  249. if (self.includeDirectoryE2EEncryption) {
  250. if (self.includeImages) {
  251. predicateDataSource = [NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND (directory == true OR typeFile == 'image')", activeAccount, _serverUrl];
  252. } else {
  253. predicateDataSource = [NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND directory == true", activeAccount, _serverUrl];
  254. }
  255. } else {
  256. if (self.includeImages) {
  257. predicateDataSource = [NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND e2eEncrypted == false AND (directory == true OR typeFile == 'image')", activeAccount, _serverUrl];
  258. } else {
  259. predicateDataSource = [NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND e2eEncrypted == false AND directory == true", activeAccount, _serverUrl];
  260. }
  261. }
  262. NSArray *result = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:predicateDataSource sorted:nil ascending:NO];
  263. if (result)
  264. return [result count];
  265. else
  266. return 0;
  267. }
  268. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  269. {
  270. static NSString *CellIdentifier = @"MyCustomCell";
  271. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  272. if (cell == nil) {
  273. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  274. }
  275. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataAtIndexWithPredicate:predicateDataSource sorted:@"fileName" ascending:YES index:indexPath.row];
  276. // Create Directory Provider Storage ocId
  277. [CCUtility getDirectoryProviderStorageOcId:metadata.ocId];
  278. // colors
  279. cell.textLabel.textColor = NCBrandColor.sharedInstance.textView;
  280. cell.detailTextLabel.text = @"";
  281. if (metadata.directory) {
  282. if (metadata.e2eEncrypted)
  283. cell.imageView.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folderEncrypted"] multiplier:2 color:NCBrandColor.sharedInstance.brandElement];
  284. else if ([metadata.fileName isEqualToString:_autoUploadFileName] && [self.serverUrl isEqualToString:_autoUploadDirectory])
  285. cell.imageView.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folderAutomaticUpload"] multiplier:2 color:NCBrandColor.sharedInstance.brandElement];
  286. else
  287. cell.imageView.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder"] multiplier:2 color:NCBrandColor.sharedInstance.brandElement];
  288. } else {
  289. UIImage *thumb = [UIImage imageWithContentsOfFile:[CCUtility getDirectoryProviderStorageIconOcId:metadata.ocId fileNameView:metadata.fileNameView]];
  290. if (thumb) {
  291. cell.imageView.image = thumb;
  292. } else {
  293. if (metadata.iconName.length > 0) {
  294. cell.imageView.image = [UIImage imageNamed:metadata.iconName];
  295. } else {
  296. cell.imageView.image = [UIImage imageNamed:@"file"];
  297. }
  298. }
  299. }
  300. cell.textLabel.text = metadata.fileNameView;
  301. cell.accessoryType = UITableViewCellAccessoryNone;
  302. return cell;
  303. }
  304. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  305. {
  306. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataAtIndexWithPredicate:predicateDataSource sorted:@"fileName" ascending:YES index:indexPath.row];
  307. if (metadata.directory) {
  308. [self performSegueDirectoryWithControlPasscode:YES];
  309. } else {
  310. if (self.selectFile) {
  311. if ([self.delegate respondsToSelector:@selector(dismissMove)])
  312. [self.delegate dismissMove];
  313. if ([self.delegate respondsToSelector:@selector(selectMetadata:serverUrl:)])
  314. [self.delegate selectMetadata:metadata serverUrl:_serverUrl];
  315. [self dismissViewControllerAnimated:YES completion:nil];
  316. }
  317. }
  318. }
  319. // MARK: - Navigation
  320. - (void)performSegueDirectoryWithControlPasscode:(BOOL)controlPasscode
  321. {
  322. NSString *nomeDir;
  323. NSIndexPath *index = [self.tableView indexPathForSelectedRow];
  324. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataAtIndexWithPredicate:predicateDataSource sorted:@"fileName" ascending:YES index:index.row];
  325. // lockServerUrl
  326. NSString *lockServerUrl = [CCUtility stringAppendServerUrl:_serverUrl addFileName:metadata.fileName];
  327. // 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
  328. tableDirectory *directory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", activeAccount, lockServerUrl]];
  329. if (directory.lock && [[CCUtility getBlockCode] length] && controlPasscode) {
  330. CCBKPasscode *viewController = [[CCBKPasscode alloc] initWithNibName:nil bundle:nil];
  331. viewController.delegate = self;
  332. //viewController.fromType = CCBKPasscodeFromLockDirectory;
  333. viewController.type = BKPasscodeViewControllerCheckPasscodeType;
  334. viewController.inputViewTitlePassword = YES;
  335. if ([CCUtility getSimplyBlockCode]) {
  336. viewController.passcodeStyle = BKPasscodeInputViewNumericPasscodeStyle;
  337. viewController.passcodeInputView.maximumLength = 6;
  338. } else {
  339. viewController.passcodeStyle = BKPasscodeInputViewNormalPasscodeStyle;
  340. viewController.passcodeInputView.maximumLength = 64;
  341. }
  342. BKTouchIDManager *touchIDManager = [[BKTouchIDManager alloc] initWithKeychainServiceName:k_serviceShareKeyChain];
  343. touchIDManager.promptText = NSLocalizedString(@"_scan_fingerprint_", nil);
  344. viewController.touchIDManager = touchIDManager;
  345. viewController.title = NSLocalizedString(@"_folder_blocked_", nil);
  346. viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(passcodeViewCloseButtonPressed:)];
  347. viewController.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
  348. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  349. navigationController.modalPresentationStyle = UIModalPresentationFullScreen;
  350. [self presentViewController:navigationController animated:YES completion:nil];
  351. return;
  352. }
  353. nomeDir = metadata.fileName;
  354. UINavigationController* navigationController = [[UIStoryboard storyboardWithName:@"NCSelectDestination" bundle:nil] instantiateInitialViewController];
  355. NCSelectDestination *viewController = (NCSelectDestination *)navigationController.topViewController;
  356. viewController.delegate = self.delegate;
  357. viewController.includeDirectoryE2EEncryption = self.includeDirectoryE2EEncryption;
  358. viewController.includeImages = self.includeImages;
  359. viewController.move.title = self.move.title;
  360. viewController.hideCreateFolder = self.hideCreateFolder;
  361. viewController.hideMoveutton = self.hideMoveutton;
  362. viewController.selectFile = self.selectFile;
  363. viewController.type = self.type;
  364. viewController.passMetadata = metadata;
  365. viewController.serverUrl = [CCUtility stringAppendServerUrl:_serverUrl addFileName:nomeDir];
  366. [self.navigationController pushViewController:viewController animated:YES];
  367. }
  368. @end