CCShareOC.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. //
  2. // CCShareOC.m
  3. // Nextcloud iOS
  4. //
  5. // Created by Marino Faggiana on 13/11/15.
  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 "CCShareOC.h"
  24. #import "AppDelegate.h"
  25. #import "NCBridgeSwift.h"
  26. @interface CCShareOC ()
  27. {
  28. AppDelegate *appDelegate;
  29. }
  30. @end
  31. @implementation CCShareOC
  32. - (instancetype)initWithCoder:(NSCoder *)coder
  33. {
  34. self = [super initWithCoder:coder];
  35. if (self) {
  36. appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  37. self.itemsShareWith = [[NSMutableArray alloc] init];
  38. [self initializeForm];
  39. }
  40. return self;
  41. }
  42. - (void)initializeForm
  43. {
  44. XLFormDescriptor *form ;
  45. XLFormSectionDescriptor *section;
  46. XLFormRowDescriptor *row;
  47. form = [XLFormDescriptor formDescriptor];
  48. form.rowNavigationOptions = XLFormRowNavigationOptionNone;
  49. // Share Link
  50. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_share_link_", nil)];
  51. [form addFormSection:section];
  52. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"password" rowType:XLFormRowDescriptorTypePassword title:NSLocalizedString(@"_password_", nil)];
  53. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  54. [section addFormRow:row];
  55. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"shareLinkSwitch" rowType:XLFormRowDescriptorTypeBooleanSwitch title:NSLocalizedString(@"_share_link_", nil)];
  56. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  57. [section addFormRow:row];
  58. // Expiration date
  59. section = [XLFormSectionDescriptor formSection];
  60. [form addFormSection:section];
  61. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"expirationDate" rowType:XLFormRowDescriptorTypeDate title:NSLocalizedString(@"_date_", nil)];
  62. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  63. row.value = [self tomorrow];
  64. [row.cellConfigAtConfigure setObject:[self tomorrow] forKey:@"minimumDate"];
  65. [section addFormRow:row];
  66. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"expirationDateSwitch" rowType:XLFormRowDescriptorTypeBooleanSwitch title:NSLocalizedString(@"_share_expirationdate_", nil)];
  67. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  68. [section addFormRow:row];
  69. // Send Link To
  70. section = [XLFormSectionDescriptor formSection];
  71. [form addFormSection:section];
  72. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"sendLinkTo" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_share_link_button_", nil)];
  73. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  74. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  75. row.action.formSelector = @selector(sendLinkTo:);
  76. [section addFormRow:row];
  77. // Sharee
  78. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_share_title_", nil)];
  79. [form addFormSection:section];
  80. section.footerTitle = NSLocalizedString(@"_add_sharee_footer_", nil);
  81. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"findUser" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_add_sharee_", nil)];
  82. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  83. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  84. row.action.formSelector = @selector(shareUserButton:);
  85. [section addFormRow:row];
  86. section = [XLFormSectionDescriptor formSectionWithTitle:@"" sectionOptions:XLFormSectionOptionCanDelete];
  87. [form addFormSection:section];
  88. self.form = form;
  89. }
  90. - (void)viewDidLoad
  91. {
  92. [super viewDidLoad];
  93. self.view.backgroundColor = [NCBrandColor sharedInstance].backgroundView;
  94. [self.endButton setTitle:NSLocalizedString(@"_done_", nil) forState:UIControlStateNormal];
  95. self.endButton.tintColor = [UIColor blackColor];
  96. [self reloadData];
  97. if ([[NSFileManager defaultManager] fileExistsAtPath:[CCUtility getDirectoryProviderStorageIconFileID:self.metadata.fileID fileNameView:self.metadata.fileNameView]]) {
  98. self.fileImageView.image = [UIImage imageWithContentsOfFile:[CCUtility getDirectoryProviderStorageIconFileID:self.metadata.fileID fileNameView:self.metadata.fileNameView]];
  99. } else {
  100. if (self.metadata.directory)
  101. self.fileImageView.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder"] multiplier:2 color:[NCBrandColor sharedInstance].brandElement];
  102. else
  103. self.fileImageView.image = [UIImage imageNamed:self.metadata.iconName];
  104. }
  105. self.labelTitle.text = self.metadata.fileNameView;
  106. self.labelTitle.textColor = [UIColor blackColor];
  107. self.tableView.tableHeaderView = ({UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 0.1 / UIScreen.mainScreen.scale)];
  108. line.backgroundColor = self.tableView.separatorColor;
  109. line;
  110. });
  111. self.tableView.backgroundColor = [NCBrandColor sharedInstance].backgroundView;
  112. }
  113. #pragma --------------------------------------------------------------------------------------------
  114. #pragma mark ===== Reload Data =====
  115. #pragma --------------------------------------------------------------------------------------------
  116. - (void)reloadData
  117. {
  118. // bugfix
  119. if (!self.serverUrl || !self.metadata) {
  120. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  121. [self dismissViewControllerAnimated:YES completion:nil];
  122. });
  123. return;
  124. }
  125. self.shareLink = [appDelegate.sharesLink objectForKey:[self.serverUrl stringByAppendingString:self.metadata.fileName]];
  126. self.shareUserAndGroup = [appDelegate.sharesUserAndGroup objectForKey:[self.serverUrl stringByAppendingString:self.metadata.fileName]];
  127. self.itemShareLink = [appDelegate.sharesID objectForKey:self.shareLink];
  128. if ([self.shareUserAndGroup length] > 0) self.itemsUserAndGroupLink = [self.shareUserAndGroup componentsSeparatedByString:@","];
  129. else self.itemsUserAndGroupLink = nil;
  130. self.form.delegate = nil;
  131. XLFormRowDescriptor *rowPassword = [self.form formRowWithTag:@"password"];
  132. XLFormRowDescriptor *rowShareLinkSwitch = [self.form formRowWithTag:@"shareLinkSwitch"];
  133. XLFormRowDescriptor *rowExpirationDate = [self.form formRowWithTag:@"expirationDate"];
  134. XLFormRowDescriptor *rowExpirationDateSwitch = [self.form formRowWithTag:@"expirationDateSwitch"];
  135. XLFormRowDescriptor *rowSendLinkTo = [self.form formRowWithTag:@"sendLinkTo"];
  136. // Passoword
  137. if ([[self.itemShareLink shareWith] length] > 0 && self.itemShareLink.shareType == shareTypeLink)
  138. rowPassword.value = [self.itemShareLink shareWith];
  139. else
  140. rowPassword.value = @"";
  141. // Share Link
  142. if ([self.shareLink length] > 0) {
  143. [rowShareLinkSwitch setValue:@1];
  144. rowExpirationDate.disabled = @NO;
  145. rowExpirationDateSwitch.disabled = @NO;
  146. rowSendLinkTo.disabled = @NO;
  147. } else {
  148. [rowShareLinkSwitch setValue:@0];
  149. rowExpirationDate.disabled = @YES;
  150. rowExpirationDateSwitch.disabled = @YES;
  151. rowSendLinkTo.disabled = @YES;
  152. }
  153. // Expiration Date
  154. if (self.itemShareLink.expirationDate) {
  155. rowExpirationDateSwitch.value = @1;
  156. NSDate *expireDate;
  157. if (self.itemShareLink.expirationDate) expireDate = [NSDate dateWithTimeIntervalSince1970: self.itemShareLink.expirationDate];
  158. else expireDate = [self tomorrow];
  159. rowExpirationDate.value = expireDate;
  160. } else {
  161. rowExpirationDateSwitch.value = @0;
  162. rowExpirationDate.value = [self tomorrow];
  163. }
  164. // User & Group
  165. XLFormSectionDescriptor *section = [self.form formSectionAtIndex:4];
  166. [section.formRows removeAllObjects];
  167. [self.itemsShareWith removeAllObjects];
  168. if ([self.itemsUserAndGroupLink count] > 0) {
  169. for (NSString *idRemoteShared in self.itemsUserAndGroupLink) {
  170. OCSharedDto *item = [appDelegate.sharesID objectForKey:idRemoteShared];
  171. XLFormRowDescriptor *row = [XLFormRowDescriptor formRowDescriptorWithTag:idRemoteShared rowType:XLFormRowDescriptorTypeButton];
  172. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  173. //[row.cellConfig setObject:@(UITableViewCellAccessoryDisclosureIndicator) forKey:@"accessoryType"];
  174. [row.cellConfig setObject:[NCBrandColor sharedInstance].brandElement forKey:@"textLabel.textColor"];
  175. row.action.formSelector = @selector(sharePermissionButton:);
  176. if (item.shareType == shareTypeGroup) row.title = [item.shareWithDisplayName stringByAppendingString:NSLocalizedString(@"_user_is_group_", nil)];
  177. else row.title = item.shareWithDisplayName;
  178. [section addFormRow:row];
  179. // add users
  180. [self.itemsShareWith addObject:item];
  181. }
  182. section.footerTitle = NSLocalizedString(@"_user_sharee_footer_", nil);
  183. } else {
  184. section.footerTitle = @"";
  185. }
  186. self.form.disabled = NO;
  187. [self.tableView reloadData];
  188. self.form.delegate = self;
  189. }
  190. #pragma --------------------------------------------------------------------------------------------
  191. #pragma mark ===== Change Value & Button =====
  192. #pragma --------------------------------------------------------------------------------------------
  193. - (void)sendLinkTo:(XLFormRowDescriptor *)sender
  194. {
  195. [self deselectFormRow:sender];
  196. NSString *sharedLink = self.itemShareLink.token;
  197. NSString *url;
  198. if ([sharedLink hasPrefix:@"http://"] || [sharedLink hasPrefix:@"https://"]) {
  199. url = sharedLink;
  200. } else {
  201. url = [NSString stringWithFormat:@"%@/%@%@", appDelegate.activeUrl, k_share_link_middle_part_url_after_version_8, sharedLink];
  202. }
  203. NSArray *activityItems = @[[NSString stringWithFormat:@""], [NSURL URLWithString:url]];
  204. NSArray *applicationActivities = nil;
  205. UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:applicationActivities];
  206. activityController.popoverPresentationController.sourceView = self.view;
  207. NSIndexPath *indexPath = [self.form indexPathOfFormRow:sender];
  208. CGRect cellRect = [self.tableView rectForRowAtIndexPath:indexPath];
  209. activityController.popoverPresentationController.sourceRect = CGRectOffset(cellRect, -self.tableView.contentOffset.x, -self.tableView.contentOffset.y);
  210. [self presentViewController:activityController animated:YES completion:nil];
  211. }
  212. - (void)shareUserButton:(XLFormRowDescriptor *)rowDescriptor
  213. {
  214. [self deselectFormRow:rowDescriptor];
  215. self.shareUserOC = [[UIStoryboard storyboardWithName:@"CCShare" bundle:nil] instantiateViewControllerWithIdentifier:@"CCShareUserOC"];
  216. self.shareUserOC.delegate = self;
  217. self.shareUserOC.itemsShareWith = self.itemsShareWith;
  218. self.shareUserOC.isDirectory = self.metadata.directory;
  219. [self.shareUserOC setModalPresentationStyle:UIModalPresentationFormSheet];
  220. [self presentViewController:self.shareUserOC animated:YES completion:NULL];
  221. }
  222. - (void)sharePermissionButton:(XLFormRowDescriptor *)rowDescriptor
  223. {
  224. [self deselectFormRow:rowDescriptor];
  225. self.sharePermissionOC = [[UIStoryboard storyboardWithName:@"CCShare" bundle:nil] instantiateViewControllerWithIdentifier:@"CCSharePermissionOC"];
  226. self.sharePermissionOC.delegate = self;
  227. self.sharePermissionOC.idRemoteShared = rowDescriptor.tag;
  228. self.sharePermissionOC.metadata = self.metadata;
  229. self.sharePermissionOC.serverUrl = self.serverUrl;
  230. [self.sharePermissionOC setModalPresentationStyle:UIModalPresentationFormSheet];
  231. [self presentViewController:self.sharePermissionOC animated:YES completion:NULL];
  232. }
  233. - (void)formRowDescriptorValueHasChanged:(XLFormRowDescriptor *)rowDescriptor oldValue:(id)oldValue newValue:(id)newValue
  234. {
  235. [super formRowDescriptorValueHasChanged:rowDescriptor oldValue:oldValue newValue:newValue];
  236. OCSharedDto *shareDto = [appDelegate.sharesID objectForKey:self.shareLink];
  237. if ([rowDescriptor.tag isEqualToString:@"shareLinkSwitch"]) {
  238. if ([[rowDescriptor.value valueData] boolValue] == YES) {
  239. // share
  240. XLFormRowDescriptor *rowPassword = [self.form formRowWithTag:@"password"];
  241. [self.delegate share:self.metadata serverUrl:self.serverUrl password:rowPassword.value];
  242. [self disableForm];
  243. } else {
  244. // unshare
  245. [self.delegate unShare:self.shareLink metadata:self.metadata serverUrl:self.serverUrl];
  246. [self disableForm];
  247. }
  248. }
  249. if ([rowDescriptor.tag isEqualToString:@"expirationDateSwitch"]) {
  250. // remove expiration date
  251. if ([[rowDescriptor.value valueData] boolValue] == NO) {
  252. [self.delegate updateShare:self.shareLink metadata:self.metadata serverUrl:self.serverUrl password:nil expirationTime:@"" permission:shareDto.permissions];
  253. [self disableForm];
  254. } else {
  255. // new date
  256. XLFormRowDescriptor *rowExpirationDate = [self.form formRowWithTag:@"expirationDate"];
  257. NSString *expirationDate = [self convertDateInServerFormat:rowExpirationDate.value];
  258. [self.delegate updateShare:self.shareLink metadata:self.metadata serverUrl:self.serverUrl password:nil expirationTime:expirationDate permission:shareDto.permissions];
  259. [self disableForm];
  260. }
  261. }
  262. }
  263. - (void)formRowHasBeenRemoved:(XLFormRowDescriptor *)formRow atIndexPath:(NSIndexPath *)indexPath
  264. {
  265. long long idRemoteShared = [formRow.tag longLongValue];
  266. if ([formRow.rowType isEqualToString:@"button"] && idRemoteShared > 0) {
  267. [self.delegate unShare:formRow.tag metadata:self.metadata serverUrl:self.serverUrl];
  268. [self disableForm];
  269. }
  270. }
  271. - (void)beginEditing:(XLFormRowDescriptor *)rowDescriptor
  272. {
  273. [super beginEditing:rowDescriptor];
  274. if ([rowDescriptor.tag isEqualToString:@"expirationDate"]) {
  275. self.endButton.enabled = NO;
  276. }
  277. }
  278. - (void)endEditing:(XLFormRowDescriptor *)rowDescriptor
  279. {
  280. [super endEditing:rowDescriptor];
  281. OCSharedDto *shareDto = [appDelegate.sharesID objectForKey:self.shareLink];
  282. if ([rowDescriptor.tag isEqualToString:@"expirationDate"]) {
  283. NSDate *old = [NSDate dateWithTimeIntervalSince1970: self.itemShareLink.expirationDate];
  284. NSDate *new = rowDescriptor.value;
  285. if ([old compare:new] != NSOrderedSame) {
  286. NSString *expirationDate = [self convertDateInServerFormat:rowDescriptor.value];
  287. [self.delegate updateShare:self.shareLink metadata:self.metadata serverUrl:self.serverUrl password:nil expirationTime:expirationDate permission:shareDto.permissions];
  288. [self disableForm];
  289. }
  290. self.endButton.enabled = YES;
  291. }
  292. if ([rowDescriptor.tag isEqualToString:@"password"]) {
  293. NSString *password = rowDescriptor.value;
  294. // if the password is not changed or is 0 lenght
  295. if ([[self.itemShareLink shareWith] isEqualToString:password]) {
  296. [self reloadData];
  297. } else {
  298. /*
  299. if (password == nil) {
  300. [self reloadData];
  301. return;
  302. }
  303. */
  304. if (password == nil)
  305. password = @"";
  306. if (self.shareLink) {
  307. [self.delegate updateShare:self.shareLink metadata:self.metadata serverUrl:self.serverUrl password:password expirationTime:nil permission:shareDto.permissions];
  308. [self disableForm];
  309. }
  310. }
  311. }
  312. }
  313. #pragma --------------------------------------------------------------------------------------------
  314. #pragma mark ===== Button =====
  315. #pragma --------------------------------------------------------------------------------------------
  316. - (IBAction)endButtonAction:(id)sender
  317. {
  318. [self.tableView endEditing:YES];
  319. // reload delegate
  320. [[NCMainCommon sharedInstance] reloadDatasourceWithServerUrl:[[NCManageDatabase sharedInstance] getServerUrl:self.metadata.directoryID] fileID:self.metadata.fileID action:k_action_MOD];
  321. [self dismissViewControllerAnimated:YES completion:nil];
  322. }
  323. #pragma --------------------------------------------------------------------------------------------
  324. #pragma mark ===== User & Group =====
  325. #pragma --------------------------------------------------------------------------------------------
  326. - (void)getUserAndGroup:(NSString *)find
  327. {
  328. [self.delegate getUserAndGroup:find];
  329. }
  330. - (void)reloadUserAndGroup:(NSArray *)items
  331. {
  332. if (self.shareUserOC)
  333. [self.shareUserOC reloadUserAndGroup:items];
  334. }
  335. - (void)shareUserAndGroup:(NSString *)user shareeType:(NSInteger)shareeType permission:(NSInteger)permission
  336. {
  337. [self.delegate shareUserAndGroup:user shareeType:shareeType permission:permission metadata:self.metadata directoryID:self.metadata.directoryID serverUrl:self.serverUrl];
  338. }
  339. - (void)updateShare:(NSString *)share metadata:(tableMetadata *)metadata serverUrl:(NSString *)serverUrl password:(NSString *)password expirationTime:(NSString *)expirationTime permission:(NSInteger)permission
  340. {
  341. [self.delegate updateShare:share metadata:metadata serverUrl:serverUrl password:password expirationTime:expirationTime permission:permission];
  342. }
  343. #pragma --------------------------------------------------------------------------------------------
  344. #pragma mark ===== Utility =====
  345. #pragma --------------------------------------------------------------------------------------------
  346. -(void)disableForm
  347. {
  348. self.form.disabled = YES;
  349. [self.tableView endEditing:YES];
  350. [self.tableView reloadData];
  351. }
  352. - (NSString *)convertDateInServerFormat:(NSDate *)date {
  353. NSDateFormatter *dateFormatter = [NSDateFormatter new];
  354. [dateFormatter setDateFormat:@"YYYY-MM-dd"];
  355. return [dateFormatter stringFromDate:date];
  356. }
  357. -(NSDate *)tomorrow
  358. {
  359. NSDate *now = [NSDate date];
  360. int daysToAdd = 1;
  361. return [now dateByAddingTimeInterval:60*60*24*daysToAdd];
  362. }
  363. @end