NCSelectDestination.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. // get auto upload folder
  71. _autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  72. _autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:activeUrl];
  73. [self readFolder];
  74. }
  75. // Apparirà
  76. - (void)viewWillAppear:(BOOL)animated
  77. {
  78. [super viewWillAppear:animated];
  79. self.navigationController.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand;
  80. self.navigationController.navigationBar.tintColor = NCBrandColor.sharedInstance.brandText;
  81. self.navigationController.toolbar.barTintColor = NCBrandColor.sharedInstance.tabBar;
  82. self.navigationController.toolbar.tintColor = [UIColor grayColor];
  83. if (self.hideCreateFolder) {
  84. [self.create setEnabled:NO];
  85. [self.create setTintColor: [UIColor clearColor]];
  86. }
  87. if (self.hideMoveutton) {
  88. [self.move setEnabled:NO];
  89. [self.move setTintColor: [UIColor clearColor]];
  90. }
  91. self.view.backgroundColor = NCBrandColor.sharedInstance.backgroundView;
  92. self.tableView.backgroundColor = NCBrandColor.sharedInstance.backgroundView;
  93. }
  94. // MARK: - IBAction
  95. - (IBAction)cancel:(UIBarButtonItem *)sender
  96. {
  97. if ([self.delegate respondsToSelector:@selector(dismissMove)])
  98. [self.delegate dismissMove];
  99. [self dismissViewControllerAnimated:YES completion:nil];
  100. }
  101. - (IBAction)move:(UIBarButtonItem *)sender
  102. {
  103. if ([self.delegate respondsToSelector:@selector(dismissMove)])
  104. [self.delegate dismissMove];
  105. [self.delegate moveServerUrlTo:_serverUrl title:self.passMetadata.fileNameView type:self.type];
  106. [self dismissViewControllerAnimated:YES completion:nil];
  107. }
  108. - (IBAction)create:(UIBarButtonItem *)sender
  109. {
  110. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_create_folder_",nil) message:@"" preferredStyle:UIAlertControllerStyleAlert];
  111. [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
  112. //textField.placeholder = NSLocalizedString(@"LoginPlaceholder", @"Login");
  113. textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
  114. }];
  115. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_save_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  116. [self createFolder:alertController.textFields.firstObject.text];
  117. }]];
  118. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  119. }]];
  120. [self presentViewController:alertController animated:YES completion:nil];
  121. }
  122. // MARK: - Read Folder
  123. - (void)readFolder
  124. {
  125. [[NCNetworking shared] readFolderWithServerUrl:_serverUrl account:activeAccount completion:^(NSString *account, tableMetadata *metadataFolder, NSArray *metadatas, NSInteger errorCode, NSString *errorDescription) {
  126. if (errorCode == 0) {
  127. self.move.enabled = true;
  128. } else {
  129. self.move.enabled = false;
  130. }
  131. _loadingFolder = NO;
  132. [self.tableView reloadData];
  133. }];
  134. _loadingFolder = YES;
  135. [self.tableView reloadData];
  136. }
  137. // MARK: - Create Folder
  138. - (void)createFolder:(NSString *)fileNameFolder
  139. {
  140. NSString *serverUrlFileName = [NSString stringWithFormat:@"%@/%@", _serverUrl, fileNameFolder];
  141. [[NCCommunication shared] createFolder:serverUrlFileName customUserAgent:nil addCustomHeaders:nil completionHandler:^(NSString *account, NSString *ocID, NSDate *date, NSInteger errorCode, NSString *errorDecription) {
  142. if (errorCode == 0) {
  143. [self readFolder];
  144. } else {
  145. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_",nil) message:errorDecription preferredStyle:UIAlertControllerStyleAlert];
  146. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { }]];
  147. [self presentViewController:alertController animated:YES completion:nil];
  148. }
  149. }];
  150. }
  151. // MARK: - Table
  152. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  153. {
  154. return 1;
  155. }
  156. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  157. {
  158. if (self.includeDirectoryE2EEncryption) {
  159. if (self.includeImages) {
  160. predicateDataSource = [NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND (directory == true OR typeFile == 'image')", activeAccount, _serverUrl];
  161. } else {
  162. predicateDataSource = [NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND directory == true", activeAccount, _serverUrl];
  163. }
  164. } else {
  165. if (self.includeImages) {
  166. predicateDataSource = [NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND e2eEncrypted == false AND (directory == true OR typeFile == 'image')", activeAccount, _serverUrl];
  167. } else {
  168. predicateDataSource = [NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND e2eEncrypted == false AND directory == true", activeAccount, _serverUrl];
  169. }
  170. }
  171. NSArray *result = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:predicateDataSource sorted:nil ascending:NO];
  172. if (result)
  173. return [result count];
  174. else
  175. return 0;
  176. }
  177. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  178. {
  179. static NSString *CellIdentifier = @"MyCustomCell";
  180. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  181. if (cell == nil) {
  182. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  183. }
  184. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataAtIndexWithPredicate:predicateDataSource sorted:@"fileName" ascending:YES index:indexPath.row];
  185. // Create Directory Provider Storage ocId
  186. [CCUtility getDirectoryProviderStorageOcId:metadata.ocId];
  187. // colors
  188. cell.textLabel.textColor = NCBrandColor.sharedInstance.textView;
  189. cell.detailTextLabel.text = @"";
  190. if (metadata.directory) {
  191. if (metadata.e2eEncrypted)
  192. cell.imageView.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folderEncrypted"] multiplier:2 color:NCBrandColor.sharedInstance.brandElement];
  193. else if ([metadata.fileName isEqualToString:_autoUploadFileName] && [self.serverUrl isEqualToString:_autoUploadDirectory])
  194. cell.imageView.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folderAutomaticUpload"] multiplier:2 color:NCBrandColor.sharedInstance.brandElement];
  195. else
  196. cell.imageView.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder"] multiplier:2 color:NCBrandColor.sharedInstance.brandElement];
  197. } else {
  198. UIImage *thumb = [UIImage imageWithContentsOfFile:[CCUtility getDirectoryProviderStorageIconOcId:metadata.ocId fileNameView:metadata.fileNameView]];
  199. if (thumb) {
  200. cell.imageView.image = thumb;
  201. } else {
  202. if (metadata.iconName.length > 0) {
  203. cell.imageView.image = [UIImage imageNamed:metadata.iconName];
  204. } else {
  205. cell.imageView.image = [UIImage imageNamed:@"file"];
  206. }
  207. }
  208. }
  209. cell.textLabel.text = metadata.fileNameView;
  210. cell.accessoryType = UITableViewCellAccessoryNone;
  211. return cell;
  212. }
  213. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  214. {
  215. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataAtIndexWithPredicate:predicateDataSource sorted:@"fileName" ascending:YES index:indexPath.row];
  216. if (metadata.directory) {
  217. UINavigationController* navigationController = [[UIStoryboard storyboardWithName:@"NCSelectDestination" bundle:nil] instantiateInitialViewController];
  218. NCSelectDestination *viewController = (NCSelectDestination *)navigationController.topViewController;
  219. viewController.delegate = self.delegate;
  220. viewController.includeDirectoryE2EEncryption = self.includeDirectoryE2EEncryption;
  221. viewController.includeImages = self.includeImages;
  222. viewController.move.title = self.move.title;
  223. viewController.hideCreateFolder = self.hideCreateFolder;
  224. viewController.hideMoveutton = self.hideMoveutton;
  225. viewController.selectFile = self.selectFile;
  226. viewController.type = self.type;
  227. viewController.passMetadata = metadata;
  228. viewController.serverUrl = [CCUtility stringAppendServerUrl:_serverUrl addFileName:metadata.fileName];
  229. [self.navigationController pushViewController:viewController animated:YES];
  230. } else {
  231. if (self.selectFile) {
  232. if ([self.delegate respondsToSelector:@selector(dismissMove)])
  233. [self.delegate dismissMove];
  234. if ([self.delegate respondsToSelector:@selector(selectMetadata:serverUrl:)])
  235. [self.delegate selectMetadata:metadata serverUrl:_serverUrl];
  236. [self dismissViewControllerAnimated:YES completion:nil];
  237. }
  238. }
  239. }
  240. @end