NCShares.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. //
  2. // NCShares.m
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 05/06/17.
  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 "NCShares.h"
  24. #import "NCSharesCell.h"
  25. #import "AppDelegate.h"
  26. #import "NCBridgeSwift.h"
  27. @interface NCShares ()
  28. {
  29. AppDelegate *appDelegate;
  30. NSArray *_dataSource;
  31. }
  32. @end
  33. @implementation NCShares
  34. #pragma --------------------------------------------------------------------------------------------
  35. #pragma mark ===== Init =====
  36. #pragma --------------------------------------------------------------------------------------------
  37. - (void)viewDidLoad
  38. {
  39. [super viewDidLoad];
  40. appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  41. // Custom Cell
  42. [self.tableView registerNib:[UINib nibWithNibName:@"NCSharesCell" bundle:nil] forCellReuseIdentifier:@"Cell"];
  43. // dataSource
  44. _dataSource = [NSMutableArray new];
  45. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 1)];
  46. self.tableView.delegate = self;
  47. // Title
  48. self.title = NSLocalizedString(@"_list_shares_", nil);
  49. // Register for 3D Touch Previewing if available
  50. if ([self.traitCollection respondsToSelector:@selector(forceTouchCapability)] && (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable)) {
  51. [self registerForPreviewingWithDelegate:self sourceView:self.view];
  52. }
  53. // Notification
  54. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheming) name:k_notificationCenter_changeTheming object:nil];
  55. [self changeTheming];
  56. }
  57. // Apparirà
  58. - (void)viewWillAppear:(BOOL)animated
  59. {
  60. [super viewWillAppear:animated];
  61. [self reloadDatasource];
  62. }
  63. - (void)changeTheming
  64. {
  65. [appDelegate changeTheming:self tableView:self.tableView collectionView:nil form:false];
  66. }
  67. #pragma mark -
  68. #pragma --------------------------------------------------------------------------------------------
  69. #pragma mark ===== Peek & Pop =====
  70. #pragma --------------------------------------------------------------------------------------------
  71. - (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
  72. {
  73. CGPoint convertedLocation = [self.view convertPoint:location toView:self.tableView];
  74. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:convertedLocation];
  75. tableShare *table = [_dataSource objectAtIndex:indexPath.row];
  76. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND fileName == %@", appDelegate.account, table.serverUrl, table.fileName]];
  77. NCSharesCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
  78. if (cell) {
  79. previewingContext.sourceRect = cell.frame;
  80. CCPeekPop *viewController = [[UIStoryboard storyboardWithName:@"CCPeekPop" bundle:nil] instantiateViewControllerWithIdentifier:@"PeekPopImagePreview"];
  81. viewController.metadata = metadata;
  82. viewController.imageFile = cell.fileImageView.image;
  83. viewController.showOpenIn = false;
  84. viewController.showOpenQuickLook = false;
  85. viewController.showShare = false;
  86. return viewController;
  87. }
  88. return nil;
  89. }
  90. - (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
  91. {
  92. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:previewingContext.sourceRect.origin];
  93. [self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
  94. }
  95. #pragma --------------------------------------------------------------------------------------------
  96. #pragma mark ==== unShare <Delegate> ====
  97. #pragma --------------------------------------------------------------------------------------------
  98. - (void)removeShares:(tableMetadata *)metadata tableShare:(tableShare *)tableShare
  99. {
  100. [[NCCommunication shared] deleteShareWithIdShare:tableShare.idShare customUserAgent:nil addCustomHeaders:nil completionHandler:^(NSString *account, NSInteger errorCode, NSString *errorDescription) {
  101. if (errorCode == 0 && [account isEqualToString:appDelegate.account]) {
  102. [[NCManageDatabase sharedInstance] deleteTableShareWithAccount:account idShare:tableShare.idShare];
  103. [self reloadDatasource];
  104. } else if (errorCode != 0) {
  105. [[NCContentPresenter shared] messageNotification:@"_share_" description:errorDescription delay:k_dismissAfterSecond type:messageTypeError errorCode:errorCode forced:true];
  106. } else {
  107. NSLog(@"[LOG] It has been changed user during networking process, error.");
  108. }
  109. }];
  110. }
  111. #pragma mark -
  112. #pragma --------------------------------------------------------------------------------------------
  113. #pragma mark ===== Swipe Tablet -> menu =====
  114. #pragma --------------------------------------------------------------------------------------------
  115. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  116. {
  117. return UITableViewCellEditingStyleDelete;
  118. }
  119. - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
  120. {
  121. return NSLocalizedString(@"_delete_", nil);
  122. }
  123. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  124. {
  125. tableMetadata *metadata;
  126. if (indexPath.row+1 <= _dataSource.count) {
  127. tableShare *table = [_dataSource objectAtIndex:indexPath.row];
  128. metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND fileName = %@", appDelegate.account, table.serverUrl, table.fileName]];
  129. }
  130. if (metadata) return YES;
  131. else return NO;
  132. }
  133. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  134. {
  135. if (editingStyle == UITableViewCellEditingStyleDelete) {
  136. tableShare *table = [_dataSource objectAtIndex:indexPath.row];
  137. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND fileName == %@", appDelegate.account, table.serverUrl, table.fileName]];
  138. [self removeShares:metadata tableShare:table];
  139. }
  140. }
  141. #pragma --------------------------------------------------------------------------------------------
  142. #pragma mark ==== Table ====
  143. #pragma --------------------------------------------------------------------------------------------
  144. - (void)readFolder:(NSString *)serverUrl
  145. {
  146. [self reloadDatasource];
  147. }
  148. - (void)reloadDatasource
  149. {
  150. _dataSource = [[NCManageDatabase sharedInstance] getTableSharesWithAccount:appDelegate.account];
  151. [self.tableView reloadData];
  152. }
  153. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  154. {
  155. return 60;
  156. }
  157. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  158. {
  159. return 1;
  160. }
  161. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  162. {
  163. return [_dataSource count];
  164. }
  165. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  166. {
  167. NCSharesCell *cell = (NCSharesCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
  168. tableMetadata *metadata;
  169. // separator
  170. cell.separatorInset = UIEdgeInsetsMake(0.f, 60.f, 0.f, 0.f);
  171. // Initialize
  172. cell.statusImageView.image = nil;
  173. cell.offlineImageView.image = nil;
  174. // change color selection
  175. UIView *selectionColor = [[UIView alloc] init];
  176. selectionColor.backgroundColor = NCBrandColor.sharedInstance.select;
  177. cell.selectedBackgroundView = selectionColor;
  178. cell.backgroundColor = NCBrandColor.sharedInstance.backgroundView;
  179. cell.labelTitle.textColor = NCBrandColor.sharedInstance.textView;
  180. tableShare *table = [_dataSource objectAtIndex:indexPath.row];
  181. metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND fileName == %@", appDelegate.account, table.serverUrl, table.fileName]];
  182. if (metadata) {
  183. if (metadata.directory) {
  184. cell.fileImageView.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder"] multiplier:2 color:NCBrandColor.sharedInstance.brandElement];
  185. } else {
  186. cell.fileImageView.image = [UIImage imageWithContentsOfFile:[CCUtility getDirectoryProviderStorageIconOcId:metadata.ocId etag:metadata.etag]];
  187. if (cell.fileImageView.image == nil) {
  188. cell.fileImageView.image = [UIImage imageNamed:metadata.iconName];
  189. [[NCOperationQueue shared] downloadThumbnailWithMetadata:metadata urlBase:appDelegate.urlBase view:tableView indexPath:indexPath];
  190. }
  191. }
  192. } else {
  193. cell.fileImageView.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"file"] multiplier:2 color:NCBrandColor.sharedInstance.brandElement];
  194. NSString *serverUrlFileName = [NSString stringWithFormat:@"%@/%@", table.serverUrl, table.fileName];
  195. [[NCNetworking shared] readFileWithServerUrlFileName:serverUrlFileName account:appDelegate.account completion:^(NSString *account, tableMetadata *metadata, NSInteger errorCode, NSString *errorDescription) {
  196. if (errorCode == 0 && [account isEqualToString:appDelegate.account]) {
  197. [[NCManageDatabase sharedInstance] addMetadata:metadata];
  198. [self reloadDatasource];
  199. }
  200. }];
  201. }
  202. cell.labelTitle.text = table.fileName;
  203. if ([table.serverUrl isEqualToString:[[NCUtility shared] getHomeServerWithUrlBase:appDelegate.urlBase account:appDelegate.account]])
  204. cell.labelInfoFile.text = @"/";
  205. else
  206. cell.labelInfoFile.text = [table.serverUrl stringByReplacingOccurrencesOfString:[[NCUtility shared] getHomeServerWithUrlBase:appDelegate.urlBase account:appDelegate.account] withString:@""];
  207. return cell;
  208. }
  209. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  210. {
  211. // deselect row
  212. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  213. tableMetadata *metadata;
  214. tableShare *table = [_dataSource objectAtIndex:indexPath.row];
  215. if (table.serverUrl) {
  216. metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND fileName == %@", appDelegate.account, table.serverUrl, table.fileName]];
  217. if (metadata) {
  218. [[NCNetworkingNotificationCenter shared] openShareWithViewController:self metadata:metadata indexPage:2];
  219. }
  220. }
  221. }
  222. @end