CCShareOC.m 19 KB

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