CCShareOC.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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 = [NCBrandColor sharedInstance].brand;
  96. [self reloadData];
  97. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@.ico", appDelegate.directoryUser, self.metadata.fileID]]) {
  98. self.fileImageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", appDelegate.directoryUser, self.metadata.fileID]];
  99. } else {
  100. if (self.metadata.directory)
  101. self.fileImageView.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder"] color:[NCBrandColor sharedInstance].brand];
  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. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  119. // bugfix
  120. if (!self.serverUrl || !self.metadata) {
  121. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  122. [self dismissViewControllerAnimated:YES completion:nil];
  123. });
  124. return;
  125. }
  126. self.shareLink = [appDelegate.sharesLink objectForKey:[self.serverUrl stringByAppendingString:self.metadata.fileName]];
  127. self.shareUserAndGroup = [appDelegate.sharesUserAndGroup objectForKey:[self.serverUrl stringByAppendingString:self.metadata.fileName]];
  128. self.itemShareLink = [appDelegate.sharesID objectForKey:self.shareLink];
  129. if ([self.shareUserAndGroup length] > 0) self.itemsUserAndGroupLink = [self.shareUserAndGroup componentsSeparatedByString:@","];
  130. else self.itemsUserAndGroupLink = nil;
  131. self.form.delegate = nil;
  132. XLFormRowDescriptor *rowPassword = [self.form formRowWithTag:@"password"];
  133. XLFormRowDescriptor *rowShareLinkSwitch = [self.form formRowWithTag:@"shareLinkSwitch"];
  134. XLFormRowDescriptor *rowExpirationDate = [self.form formRowWithTag:@"expirationDate"];
  135. XLFormRowDescriptor *rowExpirationDateSwitch = [self.form formRowWithTag:@"expirationDateSwitch"];
  136. XLFormRowDescriptor *rowSendLinkTo = [self.form formRowWithTag:@"sendLinkTo"];
  137. // Passoword
  138. if ([[self.itemShareLink shareWith] length] > 0 && self.itemShareLink.shareType == shareTypeLink)
  139. rowPassword.value = [self.itemShareLink shareWith];
  140. else
  141. rowPassword.value = @"";
  142. // Share Link
  143. if ([self.shareLink length] > 0) {
  144. [rowShareLinkSwitch setValue:@1];
  145. rowExpirationDate.disabled = @NO;
  146. rowExpirationDateSwitch.disabled = @NO;
  147. rowSendLinkTo.disabled = @NO;
  148. } else {
  149. [rowShareLinkSwitch setValue:@0];
  150. rowExpirationDate.disabled = @YES;
  151. rowExpirationDateSwitch.disabled = @YES;
  152. rowSendLinkTo.disabled = @YES;
  153. }
  154. // Expiration Date
  155. if (self.itemShareLink.expirationDate) {
  156. rowExpirationDateSwitch.value = @1;
  157. NSDate *expireDate;
  158. if (self.itemShareLink.expirationDate) expireDate = [NSDate dateWithTimeIntervalSince1970: self.itemShareLink.expirationDate];
  159. else expireDate = [self tomorrow];
  160. rowExpirationDate.value = expireDate;
  161. } else {
  162. rowExpirationDateSwitch.value = @0;
  163. rowExpirationDate.value = [self tomorrow];
  164. }
  165. // User & Group
  166. XLFormSectionDescriptor *section = [self.form formSectionAtIndex:4];
  167. [section.formRows removeAllObjects];
  168. [self.itemsShareWith removeAllObjects];
  169. if ([self.itemsUserAndGroupLink count] > 0) {
  170. for (NSString *idRemoteShared in self.itemsUserAndGroupLink) {
  171. OCSharedDto *item = [appDelegate.sharesID objectForKey:idRemoteShared];
  172. XLFormRowDescriptor *row = [XLFormRowDescriptor formRowDescriptorWithTag:idRemoteShared rowType:XLFormRowDescriptorTypeButton];
  173. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  174. //[row.cellConfig setObject:@(UITableViewCellAccessoryDisclosureIndicator) forKey:@"accessoryType"];
  175. [row.cellConfig setObject:[NCBrandColor sharedInstance].brand forKey:@"textLabel.textColor"];
  176. row.action.formSelector = @selector(sharePermissionButton:);
  177. if (item.shareType == shareTypeGroup) row.title = [item.shareWithDisplayName stringByAppendingString:NSLocalizedString(@"_user_is_group_", nil)];
  178. else row.title = item.shareWithDisplayName;
  179. [section addFormRow:row];
  180. // add users
  181. [self.itemsShareWith addObject:item];
  182. }
  183. section.footerTitle = NSLocalizedString(@"_user_sharee_footer_", nil);
  184. } else {
  185. section.footerTitle = @"";
  186. }
  187. self.form.disabled = NO;
  188. [self.tableView reloadData];
  189. self.form.delegate = self;
  190. }
  191. #pragma --------------------------------------------------------------------------------------------
  192. #pragma mark ===== Change Value & Button =====
  193. #pragma --------------------------------------------------------------------------------------------
  194. - (void)sendLinkTo:(XLFormRowDescriptor *)sender
  195. {
  196. [self deselectFormRow:sender];
  197. NSString *sharedLink = self.itemShareLink.token;
  198. NSString *url;
  199. if ([sharedLink hasPrefix:@"http://"] || [sharedLink hasPrefix:@"https://"]) {
  200. url = sharedLink;
  201. } else {
  202. url = [NSString stringWithFormat:@"%@/%@%@", appDelegate.activeUrl, k_share_link_middle_part_url_after_version_8, sharedLink];
  203. }
  204. NSArray *activityItems = @[[NSString stringWithFormat:@""], [NSURL URLWithString:url]];
  205. NSArray *applicationActivities = nil;
  206. UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:applicationActivities];
  207. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  208. [self presentViewController:activityController animated:YES completion:nil];
  209. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  210. UIPopoverController *popup;
  211. popup = [[UIPopoverController alloc] initWithContentViewController:activityController];
  212. [popup presentPopoverFromRect:CGRectMake(120, 100, 200, 400) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
  213. }
  214. }
  215. - (void)shareUserButton:(XLFormRowDescriptor *)rowDescriptor
  216. {
  217. [self deselectFormRow:rowDescriptor];
  218. self.shareUserOC = [[UIStoryboard storyboardWithName:@"CCShare" bundle:nil] instantiateViewControllerWithIdentifier:@"CCShareUserOC"];
  219. self.shareUserOC.delegate = self;
  220. self.shareUserOC.itemsShareWith = self.itemsShareWith;
  221. self.shareUserOC.isDirectory = self.metadata.directory;
  222. [self.shareUserOC setModalPresentationStyle:UIModalPresentationFormSheet];
  223. [self presentViewController:self.shareUserOC animated:YES completion:NULL];
  224. }
  225. - (void)sharePermissionButton:(XLFormRowDescriptor *)rowDescriptor
  226. {
  227. [self deselectFormRow:rowDescriptor];
  228. self.sharePermissionOC = [[UIStoryboard storyboardWithName:@"CCShare" bundle:nil] instantiateViewControllerWithIdentifier:@"CCSharePermissionOC"];
  229. self.sharePermissionOC.delegate = self;
  230. self.sharePermissionOC.idRemoteShared = rowDescriptor.tag;
  231. self.sharePermissionOC.metadata = self.metadata;
  232. self.sharePermissionOC.serverUrl = self.serverUrl;
  233. [self.sharePermissionOC setModalPresentationStyle:UIModalPresentationFormSheet];
  234. [self presentViewController:self.sharePermissionOC animated:YES completion:NULL];
  235. }
  236. - (void)formRowDescriptorValueHasChanged:(XLFormRowDescriptor *)rowDescriptor oldValue:(id)oldValue newValue:(id)newValue
  237. {
  238. [super formRowDescriptorValueHasChanged:rowDescriptor oldValue:oldValue newValue:newValue];
  239. OCSharedDto *shareDto = [appDelegate.sharesID objectForKey:self.shareLink];
  240. if ([rowDescriptor.tag isEqualToString:@"shareLinkSwitch"]) {
  241. if ([[rowDescriptor.value valueData] boolValue] == YES) {
  242. // share
  243. XLFormRowDescriptor *rowPassword = [self.form formRowWithTag:@"password"];
  244. [self.delegate share:self.metadata serverUrl:self.serverUrl password:rowPassword.value];
  245. [self disableForm];
  246. } else {
  247. // unshare
  248. [self.delegate unShare:self.shareLink metadata:self.metadata serverUrl:self.serverUrl];
  249. [self disableForm];
  250. }
  251. }
  252. if ([rowDescriptor.tag isEqualToString:@"expirationDateSwitch"]) {
  253. // remove expiration date
  254. if ([[rowDescriptor.value valueData] boolValue] == NO) {
  255. [self.delegate updateShare:self.shareLink metadata:self.metadata serverUrl:self.serverUrl password:nil expirationTime:@"" permission:shareDto.permissions];
  256. [self disableForm];
  257. } else {
  258. // new date
  259. XLFormRowDescriptor *rowExpirationDate = [self.form formRowWithTag:@"expirationDate"];
  260. NSString *expirationDate = [self convertDateInServerFormat:rowExpirationDate.value];
  261. [self.delegate updateShare:self.shareLink metadata:self.metadata serverUrl:self.serverUrl password:nil expirationTime:expirationDate permission:shareDto.permissions];
  262. [self disableForm];
  263. }
  264. }
  265. }
  266. - (void)formRowHasBeenRemoved:(XLFormRowDescriptor *)formRow atIndexPath:(NSIndexPath *)indexPath
  267. {
  268. long long idRemoteShared = [formRow.tag longLongValue];
  269. if ([formRow.rowType isEqualToString:@"button"] && idRemoteShared > 0) {
  270. [self.delegate unShare:formRow.tag metadata:self.metadata serverUrl:self.serverUrl];
  271. [self disableForm];
  272. }
  273. }
  274. - (void)beginEditing:(XLFormRowDescriptor *)rowDescriptor
  275. {
  276. [super beginEditing:rowDescriptor];
  277. if ([rowDescriptor.tag isEqualToString:@"expirationDate"]) {
  278. self.endButton.enabled = NO;
  279. }
  280. }
  281. - (void)endEditing:(XLFormRowDescriptor *)rowDescriptor
  282. {
  283. [super endEditing:rowDescriptor];
  284. OCSharedDto *shareDto = [appDelegate.sharesID objectForKey:self.shareLink];
  285. if ([rowDescriptor.tag isEqualToString:@"expirationDate"]) {
  286. NSDate *old = [NSDate dateWithTimeIntervalSince1970: self.itemShareLink.expirationDate];
  287. NSDate *new = rowDescriptor.value;
  288. if ([old compare:new] != NSOrderedSame) {
  289. NSString *expirationDate = [self convertDateInServerFormat:rowDescriptor.value];
  290. [self.delegate updateShare:self.shareLink metadata:self.metadata serverUrl:self.serverUrl password:nil expirationTime:expirationDate permission:shareDto.permissions];
  291. [self disableForm];
  292. }
  293. self.endButton.enabled = YES;
  294. }
  295. if ([rowDescriptor.tag isEqualToString:@"password"]) {
  296. NSString *password = rowDescriptor.value;
  297. // if the password is not changed or is 0 lenght
  298. if ([[self.itemShareLink shareWith] isEqualToString:password]) {
  299. [self reloadData];
  300. } else {
  301. /*
  302. if (password == nil) {
  303. [self reloadData];
  304. return;
  305. }
  306. */
  307. if (password == nil)
  308. password = @"";
  309. if (self.shareLink) {
  310. [self.delegate updateShare:self.shareLink metadata:self.metadata serverUrl:self.serverUrl password:password expirationTime:nil permission:shareDto.permissions];
  311. [self disableForm];
  312. }
  313. }
  314. }
  315. }
  316. #pragma --------------------------------------------------------------------------------------------
  317. #pragma mark ===== Button =====
  318. #pragma --------------------------------------------------------------------------------------------
  319. - (IBAction)endButtonAction:(id)sender
  320. {
  321. [self.tableView endEditing:YES];
  322. // reload delegate
  323. [self.delegate reloadDatasource:[[NCManageDatabase sharedInstance] getServerUrl:_metadata.directoryID]];
  324. [self dismissViewControllerAnimated:YES completion:nil];
  325. }
  326. #pragma --------------------------------------------------------------------------------------------
  327. #pragma mark ===== User & Group =====
  328. #pragma --------------------------------------------------------------------------------------------
  329. - (void)getUserAndGroup:(NSString *)find
  330. {
  331. [self.delegate getUserAndGroup:find];
  332. }
  333. - (void)reloadUserAndGroup:(NSArray *)items
  334. {
  335. if (self.shareUserOC)
  336. [self.shareUserOC reloadUserAndGroup:items];
  337. }
  338. - (void)shareUserAndGroup:(NSString *)user shareeType:(NSInteger)shareeType permission:(NSInteger)permission
  339. {
  340. [self.delegate shareUserAndGroup:user shareeType:shareeType permission:permission metadata:self.metadata directoryID:self.metadata.directoryID serverUrl:self.serverUrl];
  341. }
  342. - (void)updateShare:(NSString *)share metadata:(tableMetadata *)metadata serverUrl:(NSString *)serverUrl password:(NSString *)password expirationTime:(NSString *)expirationTime permission:(NSInteger)permission
  343. {
  344. [self.delegate updateShare:share metadata:metadata serverUrl:serverUrl password:password expirationTime:expirationTime permission:permission];
  345. }
  346. #pragma --------------------------------------------------------------------------------------------
  347. #pragma mark ===== Utility =====
  348. #pragma --------------------------------------------------------------------------------------------
  349. -(void)disableForm
  350. {
  351. self.form.disabled = YES;
  352. [self.tableView endEditing:YES];
  353. [self.tableView reloadData];
  354. }
  355. - (NSString *)convertDateInServerFormat:(NSDate *)date {
  356. NSDateFormatter *dateFormatter = [NSDateFormatter new];
  357. [dateFormatter setDateFormat:@"YYYY-MM-dd"];
  358. return [dateFormatter stringFromDate:date];
  359. }
  360. -(NSDate *)tomorrow
  361. {
  362. NSDate *now = [NSDate date];
  363. int daysToAdd = 1;
  364. return [now dateByAddingTimeInterval:60*60*24*daysToAdd];
  365. }
  366. @end