NCShares.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. //
  2. // NCShares.m
  3. // Nextcloud iOS
  4. //
  5. // Created by Marino Faggiana on 05/06/17.
  6. // Copyright (c) 2017 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  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. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(triggerProgressTask:) name:@"NotificationProgressTask" object:nil];
  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. appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  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. //NSDictionary *dict = notification.userInfo;
  83. //float progress = [[dict valueForKey:@"progress"] floatValue];
  84. }
  85. #pragma --------------------------------------------------------------------------------------------
  86. #pragma mark ==== DZNEmptyDataSetSource ====
  87. #pragma --------------------------------------------------------------------------------------------
  88. - (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
  89. {
  90. return [UIColor whiteColor];
  91. }
  92. - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
  93. {
  94. return [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"sharesNoFiles"] color:[NCBrandColor sharedInstance].brand];
  95. }
  96. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
  97. {
  98. NSString *text = [NSString stringWithFormat:@"%@", NSLocalizedString(@"_list_shares_no_files_", nil)];
  99. NSDictionary *attributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20.0f], NSForegroundColorAttributeName:[UIColor lightGrayColor]};
  100. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  101. }
  102. - (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
  103. {
  104. NSString *text = [NSString stringWithFormat:@"\n%@", NSLocalizedString(@"_tutorial_list_shares_view_", nil)];
  105. NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
  106. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  107. paragraph.alignment = NSTextAlignmentCenter;
  108. NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0], NSForegroundColorAttributeName: [UIColor lightGrayColor], NSParagraphStyleAttributeName: paragraph};
  109. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  110. }
  111. #pragma --------------------------------------------------------------------------------------------
  112. #pragma mark ==== Download Thumbnail <Delegate> ====
  113. #pragma --------------------------------------------------------------------------------------------
  114. - (void)downloadThumbnailSuccess:(CCMetadataNet *)metadataNet
  115. {
  116. [self reloadDatasource];
  117. }
  118. #pragma --------------------------------------------------------------------------------------------
  119. #pragma mark ==== Read File <Delegate> ====
  120. #pragma --------------------------------------------------------------------------------------------
  121. - (void)readFileFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  122. {
  123. NSLog(@"[LOG] Read file failure error %lu, %@", (long)errorCode, message);
  124. }
  125. - (void)readFileSuccess:(CCMetadataNet *)metadataNet metadata:(tableMetadata *)metadata
  126. {
  127. (void)[[NCManageDatabase sharedInstance] addMetadata:metadata];
  128. [self reloadDatasource];
  129. }
  130. #pragma --------------------------------------------------------------------------------------------
  131. #pragma mark ==== unShare <Delegate> ====
  132. #pragma --------------------------------------------------------------------------------------------
  133. - (void)unShareSuccess:(CCMetadataNet *)metadataNet
  134. {
  135. NSArray *result = [[NCManageDatabase sharedInstance] unShare:metadataNet.share fileName:metadataNet.fileName serverUrl:metadataNet.serverUrl sharesLink:appDelegate.sharesLink sharesUserAndGroup:appDelegate.sharesUserAndGroup];
  136. appDelegate.sharesLink = result[0];
  137. appDelegate.sharesUserAndGroup = result[1];
  138. [self reloadDatasource];
  139. }
  140. - (void)removeShares:(tableMetadata *)metadata tableShare:(tableShare *)tableShare
  141. {
  142. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:appDelegate.activeAccount];
  143. metadataNet.action = actionUnShare;
  144. metadataNet.fileID = metadata.fileID;
  145. metadataNet.fileName = metadata.fileName;
  146. metadataNet.selector = selectorUnshare;
  147. metadataNet.serverUrl = tableShare.serverUrl;
  148. // Unshare Link
  149. if (tableShare.shareLink.length > 0) {
  150. metadataNet.share = tableShare.shareLink;
  151. [appDelegate addNetworkingOperationQueue:appDelegate.netQueue delegate:self metadataNet:metadataNet];
  152. }
  153. // Unshare User&Group
  154. NSArray *shareUserAndGroup = [tableShare.shareUserAndGroup componentsSeparatedByString:@","];
  155. for (NSString *share in shareUserAndGroup) {
  156. metadataNet.share = [share stringByReplacingOccurrencesOfString:@" " withString:@""];
  157. [appDelegate addNetworkingOperationQueue:appDelegate.netQueue delegate:self metadataNet:metadataNet];
  158. }
  159. }
  160. #pragma mark -
  161. #pragma --------------------------------------------------------------------------------------------
  162. #pragma mark ===== Swipe Tablet -> menu =====
  163. #pragma --------------------------------------------------------------------------------------------
  164. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  165. {
  166. return UITableViewCellEditingStyleDelete;
  167. }
  168. - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
  169. {
  170. return NSLocalizedString(@"_delete_", nil);
  171. }
  172. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  173. {
  174. tableMetadata *metadata;
  175. if (indexPath.row+1 <= _dataSource.count) {
  176. tableShare *table = [_dataSource objectAtIndex:indexPath.row];
  177. NSString *directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:table.serverUrl];
  178. if (directoryID)
  179. metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND directoryID = %@ AND fileName = %@", appDelegate.activeAccount, directoryID, table.fileName]];
  180. }
  181. if (metadata) return YES;
  182. else return NO;
  183. }
  184. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  185. {
  186. if (editingStyle == UITableViewCellEditingStyleDelete) {
  187. tableShare *table = [_dataSource objectAtIndex:indexPath.row];
  188. NSString *directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:table.serverUrl];
  189. if (directoryID) {
  190. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND directoryID = %@ AND fileName = %@", appDelegate.activeAccount, directoryID, table.fileName]];
  191. [self removeShares:metadata tableShare:table];
  192. }
  193. }
  194. }
  195. #pragma --------------------------------------------------------------------------------------------
  196. #pragma mark ==== Table ====
  197. #pragma --------------------------------------------------------------------------------------------
  198. - (void)readFolder:(NSString *)serverUrl
  199. {
  200. [self reloadDatasource];
  201. }
  202. - (void)reloadDatasource
  203. {
  204. _dataSource = [[NCManageDatabase sharedInstance] getTableShares];
  205. [self.tableView reloadData];
  206. }
  207. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  208. {
  209. return 60;
  210. }
  211. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  212. {
  213. return 1;
  214. }
  215. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  216. {
  217. return [_dataSource count];
  218. }
  219. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  220. {
  221. NCSharesCell *cell = (NCSharesCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
  222. tableMetadata *metadata;
  223. // separator
  224. cell.separatorInset = UIEdgeInsetsMake(0.f, 60.f, 0.f, 0.f);
  225. // Initialize
  226. cell.statusImageView.image = nil;
  227. cell.offlineImageView.image = nil;
  228. // change color selection
  229. UIView *selectionColor = [[UIView alloc] init];
  230. selectionColor.backgroundColor = [[NCBrandColor sharedInstance] getColorSelectBackgrond];
  231. cell.selectedBackgroundView = selectionColor;
  232. tableShare *table = [_dataSource objectAtIndex:indexPath.row];
  233. NSString *directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:table.serverUrl];
  234. if (!directoryID)
  235. return cell;
  236. if (directoryID.length > 0)
  237. metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND directoryID = %@ AND fileName = %@", appDelegate.activeAccount, directoryID, table.fileName]];
  238. if (metadata) {
  239. if (metadata.directory) {
  240. cell.fileImageView.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder"] color:[NCBrandColor sharedInstance].brand];
  241. } else {
  242. cell.fileImageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", appDelegate.directoryUser, metadata.fileID]];
  243. if (cell.fileImageView.image == nil) {
  244. if (metadata.thumbnailExists)
  245. [[CCActions sharedInstance] downloadTumbnail:metadata delegate:self];
  246. else
  247. cell.fileImageView.image = [UIImage imageNamed:metadata.iconName];
  248. }
  249. }
  250. } else {
  251. cell.fileImageView.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"file"] color:[NCBrandColor sharedInstance].brand];
  252. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:appDelegate.activeAccount];
  253. metadataNet.action = actionReadFile;
  254. metadataNet.fileName = table.fileName;
  255. metadataNet.serverUrl = table.serverUrl;
  256. [appDelegate addNetworkingOperationQueue:appDelegate.netQueue delegate:self metadataNet:metadataNet];
  257. }
  258. cell.labelTitle.text = table.fileName;
  259. if ([table.serverUrl isEqualToString:[CCUtility getHomeServerUrlActiveUrl:appDelegate.activeUrl]])
  260. cell.labelInfoFile.text = @"/";
  261. else
  262. cell.labelInfoFile.text = [table.serverUrl stringByReplacingOccurrencesOfString:[CCUtility getHomeServerUrlActiveUrl:appDelegate.activeUrl] withString:@""];
  263. return cell;
  264. }
  265. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  266. {
  267. // deselect row
  268. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  269. tableMetadata *metadata;
  270. tableShare *table = [_dataSource objectAtIndex:indexPath.row];
  271. if (table.serverUrl) {
  272. NSString *directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:table.serverUrl];
  273. if (directoryID)
  274. metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND directoryID = %@ AND fileName = %@", appDelegate.activeAccount, directoryID, table.fileName]];
  275. if (metadata) {
  276. [appDelegate.activeMain openWindowShare:metadata];
  277. }
  278. }
  279. }
  280. @end