CCShareOC.m 19 KB

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