CCShareOC.m 19 KB

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