CCShareOC.m 18 KB

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