CCShareOC.m 19 KB

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