NCShares.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. CCHud *_hudDeterminate;
  32. }
  33. @end
  34. @implementation NCShares
  35. #pragma --------------------------------------------------------------------------------------------
  36. #pragma mark ===== Init =====
  37. #pragma --------------------------------------------------------------------------------------------
  38. - (id)initWithCoder:(NSCoder *)aDecoder
  39. {
  40. if (self = [super initWithCoder:aDecoder]) {
  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. appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  51. // Custom Cell
  52. [self.tableView registerNib:[UINib nibWithNibName:@"NCSharesCell" bundle:nil] forCellReuseIdentifier:@"Cell"];
  53. // dataSource
  54. _dataSource = [NSMutableArray new];
  55. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 1)];
  56. self.tableView.separatorColor = [NCBrandColor sharedInstance].seperator;
  57. self.tableView.emptyDataSetDelegate = self;
  58. self.tableView.emptyDataSetSource = self;
  59. self.tableView.delegate = self;
  60. // Title
  61. self.title = NSLocalizedString(@"_list_shares_", nil);
  62. }
  63. // Apparirà
  64. - (void)viewWillAppear:(BOOL)animated
  65. {
  66. [super viewWillAppear:animated];
  67. // Color
  68. [app aspectNavigationControllerBar:self.navigationController.navigationBar online:[appDelegate.reachability isReachable] hidden:NO];
  69. [app aspectTabBar:self.tabBarController.tabBar hidden:NO];
  70. // Plus Button
  71. [app plusButtonVisibile:true];
  72. [self reloadDatasource];
  73. }
  74. - (void)changeTheming
  75. {
  76. if (self.isViewLoaded && self.view.window)
  77. [app changeTheming:self];
  78. // Reload Table View
  79. [self.tableView reloadData];
  80. }
  81. - (void)triggerProgressTask:(NSNotification *)notification
  82. {
  83. //NSDictionary *dict = notification.userInfo;
  84. //float progress = [[dict valueForKey:@"progress"] floatValue];
  85. }
  86. #pragma --------------------------------------------------------------------------------------------
  87. #pragma mark ==== DZNEmptyDataSetSource ====
  88. #pragma --------------------------------------------------------------------------------------------
  89. - (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
  90. {
  91. return [UIColor whiteColor];
  92. }
  93. - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
  94. {
  95. return [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"sharesNoFiles"] color:[NCBrandColor sharedInstance].brand];
  96. }
  97. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
  98. {
  99. NSString *text = [NSString stringWithFormat:@"%@", NSLocalizedString(@"_list_shares_no_files_", nil)];
  100. NSDictionary *attributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20.0f], NSForegroundColorAttributeName:[UIColor lightGrayColor]};
  101. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  102. }
  103. - (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
  104. {
  105. NSString *text = [NSString stringWithFormat:@"\n%@", NSLocalizedString(@"_tutorial_list_shares_view_", nil)];
  106. NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
  107. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  108. paragraph.alignment = NSTextAlignmentCenter;
  109. NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0], NSForegroundColorAttributeName: [UIColor lightGrayColor], NSParagraphStyleAttributeName: paragraph};
  110. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  111. }
  112. #pragma --------------------------------------------------------------------------------------------
  113. #pragma mark ==== Download Thumbnail <Delegate> ====
  114. #pragma --------------------------------------------------------------------------------------------
  115. - (void)downloadThumbnailSuccess:(CCMetadataNet *)metadataNet
  116. {
  117. [self reloadDatasource];
  118. }
  119. #pragma --------------------------------------------------------------------------------------------
  120. #pragma mark ==== Read File <Delegate> ====
  121. #pragma --------------------------------------------------------------------------------------------
  122. - (void)readFileFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  123. {
  124. NSLog(@"[LOG] Read file failure error %lu, %@", (long)errorCode, message);
  125. }
  126. - (void)readFileSuccess:(CCMetadataNet *)metadataNet metadata:(tableMetadata *)metadata
  127. {
  128. (void)[[NCManageDatabase sharedInstance] addMetadata:metadata];
  129. [self reloadDatasource];
  130. }
  131. #pragma --------------------------------------------------------------------------------------------
  132. #pragma mark ==== unShare <Delegate> ====
  133. #pragma --------------------------------------------------------------------------------------------
  134. - (void)unShareSuccess:(CCMetadataNet *)metadataNet
  135. {
  136. NSArray *result = [[NCManageDatabase sharedInstance] unShare:metadataNet.share fileName:metadataNet.fileName serverUrl:metadataNet.serverUrl sharesLink:appDelegate.sharesLink sharesUserAndGroup:appDelegate.sharesUserAndGroup];
  137. appDelegate.sharesLink = result[0];
  138. appDelegate.sharesUserAndGroup = result[1];
  139. [self reloadDatasource];
  140. }
  141. - (void)removeShares:(tableMetadata *)metadata tableShare:(tableShare *)tableShare
  142. {
  143. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:app.activeAccount];
  144. metadataNet.action = actionUnShare;
  145. metadataNet.fileID = metadata.fileID;
  146. metadataNet.fileName = metadata.fileName;
  147. metadataNet.selector = selectorUnshare;
  148. metadataNet.serverUrl = tableShare.serverUrl;
  149. // Unshare Link
  150. if (tableShare.shareLink.length > 0) {
  151. metadataNet.share = tableShare.shareLink;
  152. [app addNetworkingOperationQueue:app.netQueue delegate:self metadataNet:metadataNet];
  153. }
  154. // Unshare User&Group
  155. NSArray *shareUserAndGroup = [tableShare.shareUserAndGroup componentsSeparatedByString:@","];
  156. for (NSString *share in shareUserAndGroup) {
  157. metadataNet.share = [share stringByReplacingOccurrencesOfString:@" " withString:@""];
  158. [app addNetworkingOperationQueue:app.netQueue delegate:self metadataNet:metadataNet];
  159. }
  160. }
  161. #pragma mark -
  162. #pragma --------------------------------------------------------------------------------------------
  163. #pragma mark ===== Swipe Tablet -> menu =====
  164. #pragma --------------------------------------------------------------------------------------------
  165. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  166. {
  167. return UITableViewCellEditingStyleDelete;
  168. }
  169. - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
  170. {
  171. return NSLocalizedString(@"_delete_", nil);
  172. }
  173. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  174. {
  175. tableMetadata *metadata;
  176. if (indexPath.row+1 <= _dataSource.count) {
  177. tableShare *table = [_dataSource objectAtIndex:indexPath.row];
  178. NSString *directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:table.serverUrl];
  179. if (directoryID)
  180. metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND directoryID = %@ AND fileName = %@", appDelegate.activeAccount, directoryID, table.fileName]];
  181. }
  182. if (metadata) return YES;
  183. else return NO;
  184. }
  185. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  186. {
  187. if (editingStyle == UITableViewCellEditingStyleDelete) {
  188. tableShare *table = [_dataSource objectAtIndex:indexPath.row];
  189. NSString *directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:table.serverUrl];
  190. if (directoryID) {
  191. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND directoryID = %@ AND fileName = %@", appDelegate.activeAccount, directoryID, table.fileName]];
  192. [self removeShares:metadata tableShare:table];
  193. }
  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] getTableShares];
  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. NSString *directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:table.serverUrl];
  235. if (!directoryID)
  236. return cell;
  237. if (directoryID.length > 0)
  238. metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND directoryID = %@ AND fileName = %@", appDelegate.activeAccount, directoryID, table.fileName]];
  239. if (metadata) {
  240. if (metadata.directory) {
  241. cell.fileImageView.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:metadata.iconName] color:[NCBrandColor sharedInstance].brand];
  242. } else {
  243. cell.fileImageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", appDelegate.directoryUser, metadata.fileID]];
  244. if (cell.fileImageView.image == nil) {
  245. if (metadata.thumbnailExists)
  246. [[CCActions sharedInstance] downloadTumbnail:metadata delegate:self];
  247. else
  248. cell.fileImageView.image = [UIImage imageNamed:metadata.iconName];
  249. }
  250. }
  251. } else {
  252. cell.fileImageView.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"file"] color:[NCBrandColor sharedInstance].brand];
  253. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:appDelegate.activeAccount];
  254. metadataNet.action = actionReadFile;
  255. metadataNet.fileName = table.fileName;
  256. metadataNet.serverUrl = table.serverUrl;
  257. [appDelegate addNetworkingOperationQueue:appDelegate.netQueue delegate:self metadataNet:metadataNet];
  258. }
  259. cell.labelTitle.text = table.fileName;
  260. if ([table.serverUrl isEqualToString:[CCUtility getHomeServerUrlActiveUrl:appDelegate.activeUrl]])
  261. cell.labelInfoFile.text = @"/";
  262. else
  263. cell.labelInfoFile.text = [table.serverUrl stringByReplacingOccurrencesOfString:[CCUtility getHomeServerUrlActiveUrl:appDelegate.activeUrl] withString:@""];
  264. return cell;
  265. }
  266. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  267. {
  268. // deselect row
  269. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  270. tableMetadata *metadata;
  271. tableShare *table = [_dataSource objectAtIndex:indexPath.row];
  272. if (table.serverUrl) {
  273. NSString *directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:table.serverUrl];
  274. if (directoryID)
  275. metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND directoryID = %@ AND fileName = %@", appDelegate.activeAccount, directoryID, table.fileName]];
  276. if (metadata) {
  277. [appDelegate.activeMain openWindowShare:metadata];
  278. }
  279. }
  280. }
  281. @end