NCShares.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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(triggerProgressTask:) name:@"NotificationProgressTask" object:nil];
  42. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheming) name:@"changeTheming" object:nil];
  43. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadDatasource) name:@"SharesReloadDatasource" object:nil];
  44. }
  45. return self;
  46. }
  47. - (void)viewDidLoad
  48. {
  49. [super viewDidLoad];
  50. // Custom Cell
  51. [self.tableView registerNib:[UINib nibWithNibName:@"NCSharesCell" bundle:nil] forCellReuseIdentifier:@"Cell"];
  52. // dataSource
  53. _dataSource = [NSMutableArray new];
  54. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 1)];
  55. self.tableView.separatorColor = [NCBrandColor sharedInstance].seperator;
  56. self.tableView.emptyDataSetDelegate = self;
  57. self.tableView.emptyDataSetSource = self;
  58. self.tableView.delegate = self;
  59. // Title
  60. self.title = NSLocalizedString(@"_list_shares_", nil);
  61. }
  62. // Apparirà
  63. - (void)viewWillAppear:(BOOL)animated
  64. {
  65. [super viewWillAppear:animated];
  66. // Color
  67. [appDelegate aspectNavigationControllerBar:self.navigationController.navigationBar online:[appDelegate.reachability isReachable] hidden:NO];
  68. [appDelegate aspectTabBar:self.tabBarController.tabBar hidden:NO];
  69. // Plus Button
  70. [appDelegate plusButtonVisibile:true];
  71. [self reloadDatasource];
  72. }
  73. - (void)changeTheming
  74. {
  75. if (self.isViewLoaded && self.view.window)
  76. [appDelegate changeTheming:self];
  77. // Reload Table View
  78. [self.tableView reloadData];
  79. }
  80. - (void)triggerProgressTask:(NSNotification *)notification
  81. {
  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:@"sharesNoFiles"] multiplier:2 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 --------------------------------------------------------------------------------------------
  110. #pragma mark ==== Read File <Delegate> ====
  111. #pragma --------------------------------------------------------------------------------------------
  112. - (void)readFileSuccessFailure:(CCMetadataNet *)metadataNet metadata:(tableMetadata *)metadata message:(NSString *)message errorCode:(NSInteger)errorCode
  113. {
  114. if (errorCode == 0) {
  115. (void)[[NCManageDatabase sharedInstance] addMetadata:metadata];
  116. [self reloadDatasource];
  117. } else {
  118. NSLog(@"[LOG] Read file failure error %d, %@", (int)errorCode, message);
  119. }
  120. }
  121. #pragma --------------------------------------------------------------------------------------------
  122. #pragma mark ==== unShare <Delegate> ====
  123. #pragma --------------------------------------------------------------------------------------------
  124. - (void)unShareSuccess:(CCMetadataNet *)metadataNet
  125. {
  126. NSArray *result = [[NCManageDatabase sharedInstance] unShare:metadataNet.share fileName:metadataNet.fileName serverUrl:metadataNet.serverUrl sharesLink:appDelegate.sharesLink sharesUserAndGroup:appDelegate.sharesUserAndGroup account:metadataNet.account];
  127. appDelegate.sharesLink = result[0];
  128. appDelegate.sharesUserAndGroup = result[1];
  129. [self reloadDatasource];
  130. }
  131. - (void)removeShares:(tableMetadata *)metadata tableShare:(tableShare *)tableShare
  132. {
  133. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:appDelegate.activeAccount];
  134. metadataNet.action = actionUnShare;
  135. metadataNet.fileID = metadata.fileID;
  136. metadataNet.fileName = metadata.fileName;
  137. metadataNet.fileNameView = metadata.fileNameView;
  138. metadataNet.selector = selectorUnshare;
  139. metadataNet.serverUrl = tableShare.serverUrl;
  140. // Unshare Link
  141. if (tableShare.shareLink.length > 0) {
  142. metadataNet.share = tableShare.shareLink;
  143. [appDelegate addNetworkingOperationQueue:appDelegate.netQueue delegate:self metadataNet:metadataNet];
  144. }
  145. // Unshare User&Group
  146. NSArray *shareUserAndGroup = [tableShare.shareUserAndGroup componentsSeparatedByString:@","];
  147. for (NSString *share in shareUserAndGroup) {
  148. metadataNet.share = [share stringByReplacingOccurrencesOfString:@" " withString:@""];
  149. [appDelegate addNetworkingOperationQueue:appDelegate.netQueue delegate:self metadataNet:metadataNet];
  150. }
  151. }
  152. #pragma mark -
  153. #pragma --------------------------------------------------------------------------------------------
  154. #pragma mark ===== Swipe Tablet -> menu =====
  155. #pragma --------------------------------------------------------------------------------------------
  156. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  157. {
  158. return UITableViewCellEditingStyleDelete;
  159. }
  160. - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
  161. {
  162. return NSLocalizedString(@"_delete_", nil);
  163. }
  164. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  165. {
  166. tableMetadata *metadata;
  167. if (indexPath.row+1 <= _dataSource.count) {
  168. tableShare *table = [_dataSource objectAtIndex:indexPath.row];
  169. metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND fileName = %@", appDelegate.activeAccount, table.serverUrl, table.fileName]];
  170. }
  171. if (metadata) return YES;
  172. else return NO;
  173. }
  174. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  175. {
  176. if (editingStyle == UITableViewCellEditingStyleDelete) {
  177. tableShare *table = [_dataSource objectAtIndex:indexPath.row];
  178. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND fileName == %@", appDelegate.activeAccount, table.serverUrl, table.fileName]];
  179. [self removeShares:metadata tableShare:table];
  180. }
  181. }
  182. #pragma --------------------------------------------------------------------------------------------
  183. #pragma mark ==== Table ====
  184. #pragma --------------------------------------------------------------------------------------------
  185. - (void)readFolder:(NSString *)serverUrl
  186. {
  187. [self reloadDatasource];
  188. }
  189. - (void)reloadDatasource
  190. {
  191. _dataSource = [[NCManageDatabase sharedInstance] getTableSharesWithAccount:appDelegate.activeAccount];
  192. [self.tableView reloadData];
  193. }
  194. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  195. {
  196. return 60;
  197. }
  198. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  199. {
  200. return 1;
  201. }
  202. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  203. {
  204. return [_dataSource count];
  205. }
  206. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  207. {
  208. NCSharesCell *cell = (NCSharesCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
  209. tableMetadata *metadata;
  210. // separator
  211. cell.separatorInset = UIEdgeInsetsMake(0.f, 60.f, 0.f, 0.f);
  212. // Initialize
  213. cell.statusImageView.image = nil;
  214. cell.offlineImageView.image = nil;
  215. // change color selection
  216. UIView *selectionColor = [[UIView alloc] init];
  217. selectionColor.backgroundColor = [[NCBrandColor sharedInstance] getColorSelectBackgrond];
  218. cell.selectedBackgroundView = selectionColor;
  219. tableShare *table = [_dataSource objectAtIndex:indexPath.row];
  220. metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND fileName == %@", appDelegate.activeAccount, table.serverUrl, table.fileName]];
  221. if (metadata) {
  222. if (metadata.directory) {
  223. cell.fileImageView.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder"] multiplier:2 color:[NCBrandColor sharedInstance].brandElement];
  224. } else {
  225. cell.fileImageView.image = [UIImage imageWithContentsOfFile:[CCUtility getDirectoryProviderStorageIconFileID:metadata.fileID fileNameView:metadata.fileNameView]];
  226. if (cell.fileImageView.image == nil) {
  227. cell.fileImageView.image = [UIImage imageNamed:metadata.iconName];
  228. [[NCNetworkingMain sharedInstance] downloadThumbnailWith:metadata serverUrl:table.serverUrl view:tableView indexPath:indexPath forceDownload:false];
  229. }
  230. }
  231. } else {
  232. cell.fileImageView.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"file"] multiplier:2 color:[NCBrandColor sharedInstance].brandElement];
  233. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:appDelegate.activeAccount];
  234. metadataNet.action = actionReadFile;
  235. metadataNet.fileName = table.fileName;
  236. metadataNet.serverUrl = table.serverUrl;
  237. [appDelegate addNetworkingOperationQueue:appDelegate.netQueue delegate:self metadataNet:metadataNet];
  238. }
  239. cell.labelTitle.text = table.fileName;
  240. if ([table.serverUrl isEqualToString:[CCUtility getHomeServerUrlActiveUrl:appDelegate.activeUrl]])
  241. cell.labelInfoFile.text = @"/";
  242. else
  243. cell.labelInfoFile.text = [table.serverUrl stringByReplacingOccurrencesOfString:[CCUtility getHomeServerUrlActiveUrl:appDelegate.activeUrl] withString:@""];
  244. return cell;
  245. }
  246. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  247. {
  248. // deselect row
  249. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  250. tableMetadata *metadata;
  251. tableShare *table = [_dataSource objectAtIndex:indexPath.row];
  252. if (table.serverUrl) {
  253. metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND fileName == %@", appDelegate.activeAccount, table.serverUrl, table.fileName]];
  254. if (metadata) {
  255. [appDelegate.activeMain openWindowShare:metadata];
  256. }
  257. }
  258. }
  259. @end