NCShares.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. }
  61. // Apparirà
  62. - (void)viewWillAppear:(BOOL)animated
  63. {
  64. [super viewWillAppear:animated];
  65. // Color
  66. [appDelegate aspectNavigationControllerBar:self.navigationController.navigationBar online:[appDelegate.reachability isReachable] hidden:NO];
  67. [appDelegate aspectTabBar:self.tabBarController.tabBar hidden:NO];
  68. // Plus Button
  69. [appDelegate plusButtonVisibile:true];
  70. [self reloadDatasource];
  71. }
  72. - (void)changeTheming
  73. {
  74. if (self.isViewLoaded && self.view.window)
  75. [appDelegate changeTheming:self];
  76. // Reload Table View
  77. [self.tableView reloadData];
  78. }
  79. #pragma --------------------------------------------------------------------------------------------
  80. #pragma mark ==== DZNEmptyDataSetSource ====
  81. #pragma --------------------------------------------------------------------------------------------
  82. - (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
  83. {
  84. return [UIColor whiteColor];
  85. }
  86. - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
  87. {
  88. return [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"sharesNoFiles"] width:300 height:300 scale:2 color:[NCBrandColor sharedInstance].graySoft];
  89. }
  90. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
  91. {
  92. NSString *text = [NSString stringWithFormat:@"%@", NSLocalizedString(@"_list_shares_no_files_", nil)];
  93. NSDictionary *attributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20.0f], NSForegroundColorAttributeName:[UIColor lightGrayColor]};
  94. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  95. }
  96. - (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
  97. {
  98. NSString *text = [NSString stringWithFormat:@"\n%@", NSLocalizedString(@"_tutorial_list_shares_view_", nil)];
  99. NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
  100. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  101. paragraph.alignment = NSTextAlignmentCenter;
  102. NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0], NSForegroundColorAttributeName: [UIColor lightGrayColor], NSParagraphStyleAttributeName: paragraph};
  103. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  104. }
  105. #pragma --------------------------------------------------------------------------------------------
  106. #pragma mark ==== unShare <Delegate> ====
  107. #pragma --------------------------------------------------------------------------------------------
  108. - (void)removeShares:(tableMetadata *)metadata tableShare:(tableShare *)tableShare
  109. {
  110. NSString *shareString;
  111. // Unshare Link
  112. if (tableShare.shareLink.length > 0) {
  113. shareString = tableShare.shareLink;
  114. }
  115. // Unshare User&Group
  116. NSArray *shareUserAndGroup = [tableShare.shareUserAndGroup componentsSeparatedByString:@","];
  117. for (NSString *share in shareUserAndGroup) {
  118. shareString = [share stringByReplacingOccurrencesOfString:@" " withString:@""];
  119. }
  120. [[OCNetworking sharedManager] unshareAccount:appDelegate.activeAccount shareID:[shareString integerValue] completion:^(NSString *account, NSString *message, NSInteger errorCode) {
  121. if (errorCode == 0 && [account isEqualToString:appDelegate.activeAccount]) {
  122. NSArray *result = [[NCManageDatabase sharedInstance] unShare:shareString fileName:metadata.fileName serverUrl:metadata.serverUrl sharesLink:appDelegate.sharesLink sharesUserAndGroup:appDelegate.sharesUserAndGroup account:account];
  123. appDelegate.sharesLink = result[0];
  124. appDelegate.sharesUserAndGroup = result[1];
  125. [self reloadDatasource];
  126. } if (errorCode == kOCErrorServerUnauthorized) {
  127. [appDelegate openLoginView:self delegate:appDelegate.activeMain loginType:k_login_Modify_Password selector:k_intro_login];
  128. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  129. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:message viewController:self delegate:self];
  130. } else if (errorCode != 0) {
  131. [appDelegate messageNotification:@"_share_" description:message visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
  132. } else {
  133. NSLog(@"[LOG] It has been changed user during networking process, error.");
  134. }
  135. }];
  136. }
  137. #pragma mark -
  138. #pragma --------------------------------------------------------------------------------------------
  139. #pragma mark ===== Swipe Tablet -> menu =====
  140. #pragma --------------------------------------------------------------------------------------------
  141. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  142. {
  143. return UITableViewCellEditingStyleDelete;
  144. }
  145. - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
  146. {
  147. return NSLocalizedString(@"_delete_", nil);
  148. }
  149. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  150. {
  151. tableMetadata *metadata;
  152. if (indexPath.row+1 <= _dataSource.count) {
  153. tableShare *table = [_dataSource objectAtIndex:indexPath.row];
  154. metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND fileName = %@", appDelegate.activeAccount, table.serverUrl, table.fileName]];
  155. }
  156. if (metadata) return YES;
  157. else return NO;
  158. }
  159. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  160. {
  161. if (editingStyle == UITableViewCellEditingStyleDelete) {
  162. tableShare *table = [_dataSource objectAtIndex:indexPath.row];
  163. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND fileName == %@", appDelegate.activeAccount, table.serverUrl, table.fileName]];
  164. [self removeShares:metadata tableShare:table];
  165. }
  166. }
  167. #pragma --------------------------------------------------------------------------------------------
  168. #pragma mark ==== Table ====
  169. #pragma --------------------------------------------------------------------------------------------
  170. - (void)readFolder:(NSString *)serverUrl
  171. {
  172. [self reloadDatasource];
  173. }
  174. - (void)reloadDatasource
  175. {
  176. _dataSource = [[NCManageDatabase sharedInstance] getTableSharesWithAccount:appDelegate.activeAccount];
  177. [self.tableView reloadData];
  178. }
  179. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  180. {
  181. return 60;
  182. }
  183. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  184. {
  185. return 1;
  186. }
  187. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  188. {
  189. return [_dataSource count];
  190. }
  191. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  192. {
  193. NCSharesCell *cell = (NCSharesCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
  194. tableMetadata *metadata;
  195. // separator
  196. cell.separatorInset = UIEdgeInsetsMake(0.f, 60.f, 0.f, 0.f);
  197. // Initialize
  198. cell.statusImageView.image = nil;
  199. cell.offlineImageView.image = nil;
  200. // change color selection
  201. UIView *selectionColor = [[UIView alloc] init];
  202. selectionColor.backgroundColor = [[NCBrandColor sharedInstance] getColorSelectBackgrond];
  203. cell.selectedBackgroundView = selectionColor;
  204. tableShare *table = [_dataSource objectAtIndex:indexPath.row];
  205. metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND fileName == %@", appDelegate.activeAccount, table.serverUrl, table.fileName]];
  206. if (metadata) {
  207. if (metadata.directory) {
  208. cell.fileImageView.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder"] multiplier:2 color:[NCBrandColor sharedInstance].brandElement];
  209. } else {
  210. cell.fileImageView.image = [UIImage imageWithContentsOfFile:[CCUtility getDirectoryProviderStorageIconFileID:metadata.fileID fileNameView:metadata.fileNameView]];
  211. if (cell.fileImageView.image == nil) {
  212. cell.fileImageView.image = [UIImage imageNamed:metadata.iconName];
  213. [[NCNetworkingMain sharedInstance] downloadThumbnailWith:metadata view:tableView indexPath:indexPath];
  214. }
  215. }
  216. } else {
  217. cell.fileImageView.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"file"] multiplier:2 color:[NCBrandColor sharedInstance].brandElement];
  218. [[OCNetworking sharedManager] readFileWithAccount:appDelegate.activeAccount serverUrl:table.serverUrl fileName:table.fileName completion:^(NSString *account, tableMetadata *metadata, NSString *message, NSInteger errorCode) {
  219. if (errorCode == 0 && [account isEqualToString:appDelegate.activeAccount]) {
  220. (void)[[NCManageDatabase sharedInstance] addMetadata:metadata];
  221. [self reloadDatasource];
  222. }
  223. }];
  224. }
  225. cell.labelTitle.text = table.fileName;
  226. if ([table.serverUrl isEqualToString:[CCUtility getHomeServerUrlActiveUrl:appDelegate.activeUrl]])
  227. cell.labelInfoFile.text = @"/";
  228. else
  229. cell.labelInfoFile.text = [table.serverUrl stringByReplacingOccurrencesOfString:[CCUtility getHomeServerUrlActiveUrl:appDelegate.activeUrl] withString:@""];
  230. return cell;
  231. }
  232. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  233. {
  234. // deselect row
  235. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  236. tableMetadata *metadata;
  237. tableShare *table = [_dataSource objectAtIndex:indexPath.row];
  238. if (table.serverUrl) {
  239. metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND fileName == %@", appDelegate.activeAccount, table.serverUrl, table.fileName]];
  240. if (metadata) {
  241. [appDelegate.activeMain readShareWithAccount:appDelegate.activeAccount openWindow:YES metadata:metadata];
  242. }
  243. }
  244. }
  245. @end