CCShareOC.m 19 KB

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