NCShares.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. //
  2. // NCShares.m
  3. // Nextcloud iOS
  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. - (id)initWithCoder:(NSCoder *)aDecoder
  38. {
  39. if (self = [super initWithCoder:aDecoder]) {
  40. appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  41. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheming) name:@"changeTheming" object:nil];
  42. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadDatasource) name:@"SharesReloadDatasource" object:nil];
  43. }
  44. return self;
  45. }
  46. - (void)viewDidLoad
  47. {
  48. [super viewDidLoad];
  49. // Custom Cell
  50. [self.tableView registerNib:[UINib nibWithNibName:@"NCSharesCell" bundle:nil] forCellReuseIdentifier:@"Cell"];
  51. // dataSource
  52. _dataSource = [NSMutableArray new];
  53. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 1)];
  54. self.tableView.separatorColor = [NCBrandColor sharedInstance].seperator;
  55. self.tableView.emptyDataSetDelegate = self;
  56. self.tableView.emptyDataSetSource = self;
  57. self.tableView.delegate = self;
  58. // Title
  59. self.title = NSLocalizedString(@"_list_shares_", nil);
  60. // Register for 3D Touch Previewing if available
  61. if ([self.traitCollection respondsToSelector:@selector(forceTouchCapability)] && (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable)) {
  62. [self registerForPreviewingWithDelegate:self sourceView:self.view];
  63. }
  64. }
  65. // Apparirà
  66. - (void)viewWillAppear:(BOOL)animated
  67. {
  68. [super viewWillAppear:animated];
  69. // Color
  70. [appDelegate aspectNavigationControllerBar:self.navigationController.navigationBar online:[appDelegate.reachability isReachable] hidden:NO];
  71. [appDelegate aspectTabBar:self.tabBarController.tabBar hidden:NO];
  72. // Plus Button
  73. [appDelegate plusButtonVisibile:true];
  74. [self reloadDatasource];
  75. }
  76. - (void)changeTheming
  77. {
  78. if (self.isViewLoaded && self.view.window)
  79. [appDelegate changeTheming:self];
  80. // Reload Table View
  81. [self.tableView reloadData];
  82. }
  83. #pragma --------------------------------------------------------------------------------------------
  84. #pragma mark ==== DZNEmptyDataSetSource ====
  85. #pragma --------------------------------------------------------------------------------------------
  86. - (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
  87. {
  88. return [UIColor whiteColor];
  89. }
  90. - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
  91. {
  92. return [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"share"] width:300 height:300 color:[NCBrandColor sharedInstance].graySoft];
  93. }
  94. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
  95. {
  96. NSString *text = [NSString stringWithFormat:@"%@", NSLocalizedString(@"_list_shares_no_files_", nil)];
  97. NSDictionary *attributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20.0f], NSForegroundColorAttributeName:[UIColor lightGrayColor]};
  98. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  99. }
  100. - (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
  101. {
  102. NSString *text = [NSString stringWithFormat:@"\n%@", NSLocalizedString(@"_tutorial_list_shares_view_", nil)];
  103. NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
  104. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  105. paragraph.alignment = NSTextAlignmentCenter;
  106. NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0], NSForegroundColorAttributeName: [UIColor lightGrayColor], NSParagraphStyleAttributeName: paragraph};
  107. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  108. }
  109. #pragma mark -
  110. #pragma --------------------------------------------------------------------------------------------
  111. #pragma mark ===== Peek & Pop =====
  112. #pragma --------------------------------------------------------------------------------------------
  113. - (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
  114. {
  115. CGPoint convertedLocation = [self.view convertPoint:location toView:self.tableView];
  116. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:convertedLocation];
  117. tableShare *table = [_dataSource objectAtIndex:indexPath.row];
  118. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND fileName == %@", appDelegate.activeAccount, table.serverUrl, table.fileName]];
  119. NCSharesCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
  120. if (cell) {
  121. previewingContext.sourceRect = cell.frame;
  122. CCPeekPop *viewController = [[UIStoryboard storyboardWithName:@"CCPeekPop" bundle:nil] instantiateViewControllerWithIdentifier:@"PeekPopImagePreview"];
  123. viewController.metadata = metadata;
  124. viewController.imageFile = cell.fileImageView.image;
  125. viewController.showOpenIn = false;
  126. viewController.showShare = false;
  127. return viewController;
  128. }
  129. return nil;
  130. }
  131. - (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
  132. {
  133. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:previewingContext.sourceRect.origin];
  134. [self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
  135. }
  136. #pragma --------------------------------------------------------------------------------------------
  137. #pragma mark ==== unShare <Delegate> ====
  138. #pragma --------------------------------------------------------------------------------------------
  139. - (void)removeShares:(tableMetadata *)metadata tableShare:(tableShare *)tableShare
  140. {
  141. NSString *shareString;
  142. // Unshare Link
  143. if (tableShare.shareLink.length > 0) {
  144. shareString = tableShare.shareLink;
  145. }
  146. // Unshare User&Group
  147. NSArray *shareUserAndGroup = [tableShare.shareUserAndGroup componentsSeparatedByString:@","];
  148. for (NSString *share in shareUserAndGroup) {
  149. shareString = [share stringByReplacingOccurrencesOfString:@" " withString:@""];
  150. }
  151. [[OCNetworking sharedManager] unshareAccount:appDelegate.activeAccount shareID:[shareString integerValue] completion:^(NSString *account, NSString *message, NSInteger errorCode) {
  152. if (errorCode == 0 && [account isEqualToString:appDelegate.activeAccount]) {
  153. NSArray *result = [[NCManageDatabase sharedInstance] unShare:shareString fileName:metadata.fileName serverUrl:metadata.serverUrl sharesLink:appDelegate.sharesLink sharesUserAndGroup:appDelegate.sharesUserAndGroup account:account];
  154. appDelegate.sharesLink = result[0];
  155. appDelegate.sharesUserAndGroup = result[1];
  156. [self reloadDatasource];
  157. } if (errorCode == NSURLErrorServerCertificateUntrusted) {
  158. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:message viewController:self delegate:self];
  159. } else if (errorCode != 0) {
  160. [appDelegate messageNotification:@"_share_" description:message visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
  161. } else {
  162. NSLog(@"[LOG] It has been changed user during networking process, error.");
  163. }
  164. }];
  165. }
  166. #pragma mark -
  167. #pragma --------------------------------------------------------------------------------------------
  168. #pragma mark ===== Swipe Tablet -> menu =====
  169. #pragma --------------------------------------------------------------------------------------------
  170. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  171. {
  172. return UITableViewCellEditingStyleDelete;
  173. }
  174. - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
  175. {
  176. return NSLocalizedString(@"_delete_", nil);
  177. }
  178. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  179. {
  180. tableMetadata *metadata;
  181. if (indexPath.row+1 <= _dataSource.count) {
  182. tableShare *table = [_dataSource objectAtIndex:indexPath.row];
  183. metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND fileName = %@", appDelegate.activeAccount, table.serverUrl, table.fileName]];
  184. }
  185. if (metadata) return YES;
  186. else return NO;
  187. }
  188. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  189. {
  190. if (editingStyle == UITableViewCellEditingStyleDelete) {
  191. tableShare *table = [_dataSource objectAtIndex:indexPath.row];
  192. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND fileName == %@", appDelegate.activeAccount, table.serverUrl, table.fileName]];
  193. [self removeShares:metadata tableShare:table];
  194. }
  195. }
  196. #pragma --------------------------------------------------------------------------------------------
  197. #pragma mark ==== Table ====
  198. #pragma --------------------------------------------------------------------------------------------
  199. - (void)readFolder:(NSString *)serverUrl
  200. {
  201. [self reloadDatasource];
  202. }
  203. - (void)reloadDatasource
  204. {
  205. _dataSource = [[NCManageDatabase sharedInstance] getTableSharesWithAccount:appDelegate.activeAccount];
  206. [self.tableView reloadData];
  207. }
  208. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  209. {
  210. return 60;
  211. }
  212. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  213. {
  214. return 1;
  215. }
  216. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  217. {
  218. return [_dataSource count];
  219. }
  220. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  221. {
  222. NCSharesCell *cell = (NCSharesCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
  223. tableMetadata *metadata;
  224. // separator
  225. cell.separatorInset = UIEdgeInsetsMake(0.f, 60.f, 0.f, 0.f);
  226. // Initialize
  227. cell.statusImageView.image = nil;
  228. cell.offlineImageView.image = nil;
  229. // change color selection
  230. UIView *selectionColor = [[UIView alloc] init];
  231. selectionColor.backgroundColor = [[NCBrandColor sharedInstance] getColorSelectBackgrond];
  232. cell.selectedBackgroundView = selectionColor;
  233. tableShare *table = [_dataSource objectAtIndex:indexPath.row];
  234. metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND fileName == %@", appDelegate.activeAccount, table.serverUrl, table.fileName]];
  235. if (metadata) {
  236. if (metadata.directory) {
  237. cell.fileImageView.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder"] multiplier:2 color:[NCBrandColor sharedInstance].brandElement];
  238. } else {
  239. cell.fileImageView.image = [UIImage imageWithContentsOfFile:[CCUtility getDirectoryProviderStorageIconFileID:metadata.fileID fileNameView:metadata.fileNameView]];
  240. if (cell.fileImageView.image == nil) {
  241. cell.fileImageView.image = [UIImage imageNamed:metadata.iconName];
  242. [[NCNetworkingMain sharedInstance] downloadThumbnailWith:metadata view:tableView indexPath:indexPath];
  243. }
  244. }
  245. } else {
  246. cell.fileImageView.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"file"] multiplier:2 color:[NCBrandColor sharedInstance].brandElement];
  247. [[OCNetworking sharedManager] readFileWithAccount:appDelegate.activeAccount serverUrl:table.serverUrl fileName:table.fileName completion:^(NSString *account, tableMetadata *metadata, NSString *message, NSInteger errorCode) {
  248. if (errorCode == 0 && [account isEqualToString:appDelegate.activeAccount]) {
  249. (void)[[NCManageDatabase sharedInstance] addMetadata:metadata];
  250. [self reloadDatasource];
  251. }
  252. }];
  253. }
  254. cell.labelTitle.text = table.fileName;
  255. if ([table.serverUrl isEqualToString:[CCUtility getHomeServerUrlActiveUrl:appDelegate.activeUrl]])
  256. cell.labelInfoFile.text = @"/";
  257. else
  258. cell.labelInfoFile.text = [table.serverUrl stringByReplacingOccurrencesOfString:[CCUtility getHomeServerUrlActiveUrl:appDelegate.activeUrl] withString:@""];
  259. return cell;
  260. }
  261. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  262. {
  263. // deselect row
  264. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  265. tableMetadata *metadata;
  266. tableShare *table = [_dataSource objectAtIndex:indexPath.row];
  267. if (table.serverUrl) {
  268. metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND fileName == %@", appDelegate.activeAccount, table.serverUrl, table.fileName]];
  269. if (metadata) {
  270. [appDelegate.activeMain readShareWithAccount:appDelegate.activeAccount openWindow:YES metadata:metadata];
  271. }
  272. }
  273. }
  274. @end