CCShareOC.m 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. //
  2. // CCShareOC.m
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 13/11/15.
  6. // Copyright (c) 2017 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  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. {
  28. AppDelegate *appDelegate;
  29. tableCapabilities *capabilities;
  30. }
  31. @end
  32. @implementation CCShareOC
  33. - (instancetype)initWithCoder:(NSCoder *)coder
  34. {
  35. self = [super initWithCoder:coder];
  36. if (self) {
  37. appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  38. self.itemsShareWith = [NSMutableArray new];
  39. [self initializeForm];
  40. }
  41. return self;
  42. }
  43. - (void)initializeForm
  44. {
  45. XLFormDescriptor *form ;
  46. XLFormSectionDescriptor *section;
  47. XLFormRowDescriptor *row;
  48. form = [XLFormDescriptor formDescriptor];
  49. form.rowNavigationOptions = XLFormRowNavigationOptionNone;
  50. // Share Link
  51. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_share_link_", nil)];
  52. [form addFormSection:section];
  53. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"shareLinkSwitch" rowType:XLFormRowDescriptorTypeBooleanSwitch title:NSLocalizedString(@"_share_link_", nil)];
  54. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  55. [section addFormRow:row];
  56. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"shareLinkPermission" rowType:XLFormRowDescriptorTypePicker];
  57. row.height = 70;
  58. [section addFormRow:row];
  59. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"password" rowType:XLFormRowDescriptorTypePassword title:NSLocalizedString(@"_password_", nil)];
  60. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  61. [section addFormRow:row];
  62. capabilities = [[NCManageDatabase sharedInstance] getCapabilitesWithAccount:appDelegate.activeAccount];
  63. if (capabilities != nil && capabilities.versionMajor >= k_nextcloud_version_15_0) {
  64. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"hideDownload" rowType:XLFormRowDescriptorTypeBooleanSwitch title:NSLocalizedString(@"_share_link_hide_download_", nil)];
  65. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  66. [section addFormRow:row];
  67. }
  68. // Expiration date
  69. section = [XLFormSectionDescriptor formSection];
  70. [form addFormSection:section];
  71. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"expirationDate" rowType:XLFormRowDescriptorTypeDate title:NSLocalizedString(@"_date_", nil)];
  72. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  73. row.value = [self tomorrow];
  74. [row.cellConfigAtConfigure setObject:[self tomorrow] forKey:@"minimumDate"];
  75. [section addFormRow:row];
  76. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"expirationDateSwitch" rowType:XLFormRowDescriptorTypeBooleanSwitch title:NSLocalizedString(@"_share_expirationdate_", nil)];
  77. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  78. [section addFormRow:row];
  79. // Send Link To
  80. section = [XLFormSectionDescriptor formSection];
  81. [form addFormSection:section];
  82. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"sendLinkTo" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_share_link_button_", nil)];
  83. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  84. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  85. row.action.formSelector = @selector(sendLinkTo:);
  86. [section addFormRow:row];
  87. // Sharee
  88. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_share_title_", nil)];
  89. [form addFormSection:section];
  90. section.footerTitle = NSLocalizedString(@"_add_sharee_footer_", nil);
  91. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"findUser" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_add_sharee_", nil)];
  92. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  93. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  94. row.action.formSelector = @selector(shareUserButton:);
  95. [section addFormRow:row];
  96. section = [XLFormSectionDescriptor formSectionWithTitle:@"" sectionOptions:XLFormSectionOptionCanDelete];
  97. [form addFormSection:section];
  98. self.form = form;
  99. }
  100. - (void)viewDidLoad
  101. {
  102. [super viewDidLoad];
  103. self.view.backgroundColor = [NCBrandColor sharedInstance].backgroundView;
  104. XLFormRowDescriptor *rowShareLinkPermission = [self.form formRowWithTag:@"shareLinkPermission"];
  105. if (self.metadata.directory) {
  106. rowShareLinkPermission.selectorOptions = @[NSLocalizedString(@"_share_link_readonly_", nil), NSLocalizedString(@"_share_link_upload_modify_", nil), NSLocalizedString(@"_share_link_upload_", nil)];
  107. } else {
  108. rowShareLinkPermission.selectorOptions = @[NSLocalizedString(@"_share_link_readonly_", nil), NSLocalizedString(@"_share_link_modify_", nil)];
  109. }
  110. [self.endButton setTitle:NSLocalizedString(@"_done_", nil) forState:UIControlStateNormal];
  111. self.endButton.tintColor = [UIColor blackColor];
  112. if ([[NSFileManager defaultManager] fileExistsAtPath:[CCUtility getDirectoryProviderStorageIconFileID:self.metadata.fileID fileNameView:self.metadata.fileNameView]]) {
  113. self.fileImageView.image = [UIImage imageWithContentsOfFile:[CCUtility getDirectoryProviderStorageIconFileID:self.metadata.fileID fileNameView:self.metadata.fileNameView]];
  114. } else {
  115. if (self.metadata.directory)
  116. self.fileImageView.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder"] multiplier:2 color:[NCBrandColor sharedInstance].brandElement];
  117. else
  118. self.fileImageView.image = [UIImage imageNamed:self.metadata.iconName];
  119. }
  120. self.labelTitle.text = self.metadata.fileNameView;
  121. self.labelTitle.textColor = [UIColor blackColor];
  122. self.tableView.tableHeaderView = ({UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 0.1 / UIScreen.mainScreen.scale)];
  123. line.backgroundColor = self.tableView.separatorColor;
  124. line;
  125. });
  126. self.tableView.backgroundColor = [NCBrandColor sharedInstance].backgroundView;
  127. }
  128. - (void)viewWillAppear:(BOOL)animated
  129. {
  130. [super viewWillAppear:animated];
  131. }
  132. - (void)viewDidAppear:(BOOL)animated
  133. {
  134. [super viewDidAppear:animated];
  135. [self reloadDatasource];
  136. }
  137. #pragma --------------------------------------------------------------------------------------------
  138. #pragma mark ===== Networking =====
  139. #pragma --------------------------------------------------------------------------------------------
  140. - (void)share:(tableMetadata *)metadata serverUrl:(NSString *)serverUrl password:(NSString *)password permission:(NSInteger)permission hideDownload:(BOOL)hideDownload
  141. {
  142. NSString *fileName = [CCUtility returnFileNamePathFromFileName:metadata.fileName serverUrl:serverUrl activeUrl:appDelegate.activeUrl];
  143. [[OCNetworking sharedManager] shareWithAccount:appDelegate.activeAccount fileName:fileName password:password permission:permission hideDownload:hideDownload completion:^(NSString *account, NSString *message, NSInteger errorCode) {
  144. if (errorCode != 0) {
  145. [appDelegate messageNotification:@"_share_" description:message visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
  146. }
  147. [self reloadDatasource];
  148. }];
  149. }
  150. - (void)unShare:(NSString *)share metadata:(tableMetadata *)metadata serverUrl:(NSString *)serverUrl
  151. {
  152. [[OCNetworking sharedManager] unshareAccount:appDelegate.activeAccount shareID:[share integerValue] completion:^(NSString *account, NSString *message, NSInteger errorCode) {
  153. if (errorCode != 0) {
  154. [appDelegate messageNotification:@"_share_" description:message visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
  155. }
  156. [self reloadDatasource];
  157. }];
  158. }
  159. - (void)updateShare:(NSString *)share metadata:(tableMetadata *)metadata serverUrl:(NSString *)serverUrl password:(NSString *)password expirationTime:(NSString *)expirationTime permission:(NSInteger)permission hideDownload:(BOOL)hideDownload
  160. {
  161. [[OCNetworking sharedManager] shareUpdateAccount:appDelegate.activeAccount shareID:[share integerValue] password:password permission:permission expirationTime:expirationTime hideDownload:hideDownload completion:^(NSString *account, NSString *message, NSInteger errorCode) {
  162. if (errorCode != 0) {
  163. [appDelegate messageNotification:@"_share_" description:message visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
  164. }
  165. [self reloadDatasource];
  166. }];
  167. }
  168. #pragma --------------------------------------------------------------------------------------------
  169. #pragma mark ===== Reload Data =====
  170. #pragma --------------------------------------------------------------------------------------------
  171. - (void)reloadDatasource
  172. {
  173. // bugfix
  174. if (!self.serverUrl || !self.metadata) {
  175. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  176. [self.delegate readShareServer];
  177. [self dismissViewControllerAnimated:YES completion:nil];
  178. });
  179. return;
  180. }
  181. XLFormRowDescriptor *rowShareLinkSwitch = [self.form formRowWithTag:@"shareLinkSwitch"];
  182. XLFormRowDescriptor *rowShareLinkPermission = [self.form formRowWithTag:@"shareLinkPermission"];
  183. XLFormRowDescriptor *rowPassword = [self.form formRowWithTag:@"password"];
  184. XLFormRowDescriptor *rowHideDownload = [self.form formRowWithTag:@"hideDownload"];
  185. XLFormRowDescriptor *rowExpirationDate = [self.form formRowWithTag:@"expirationDate"];
  186. XLFormRowDescriptor *rowExpirationDateSwitch = [self.form formRowWithTag:@"expirationDateSwitch"];
  187. XLFormRowDescriptor *rowSendLinkTo = [self.form formRowWithTag:@"sendLinkTo"];
  188. XLFormRowDescriptor *rowFindUser = [self.form formRowWithTag:@"findUser"];
  189. NSString *path = [CCUtility returnFileNamePathFromFileName:self.metadata.fileName serverUrl:self.serverUrl activeUrl:appDelegate.activeUrl];
  190. [[OCNetworking sharedManager] readShareWithAccount:appDelegate.activeAccount path:path completion:^(NSString *account, NSArray *items, NSString *message, NSInteger errorCode) {
  191. if (errorCode == 0) {
  192. [appDelegate.sharesID removeAllObjects];
  193. for (OCSharedDto *item in items) {
  194. [appDelegate.sharesID setObject:item forKey:[@(item.idRemoteShared) stringValue]];
  195. }
  196. NSArray *result = [[NCManageDatabase sharedInstance] updateShare:appDelegate.sharesID activeUrl:appDelegate.activeUrl account:appDelegate.activeAccount];
  197. if (result) {
  198. appDelegate.sharesLink = result[0];
  199. appDelegate.sharesUserAndGroup = result[1];
  200. }
  201. self.shareLink = [appDelegate.sharesLink objectForKey:[self.serverUrl stringByAppendingString:self.metadata.fileName]];
  202. self.shareUserAndGroup = [appDelegate.sharesUserAndGroup objectForKey:[self.serverUrl stringByAppendingString:self.metadata.fileName]];
  203. self.itemShareLink = [appDelegate.sharesID objectForKey:self.shareLink];
  204. if ([self.shareUserAndGroup length] > 0) self.itemsUserAndGroupLink = [self.shareUserAndGroup componentsSeparatedByString:@","];
  205. else self.itemsUserAndGroupLink = nil;
  206. } else {
  207. [appDelegate messageNotification:@"_share_" description:message visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
  208. }
  209. self.form.delegate = nil;
  210. // Share Link
  211. if ([self.shareLink length] > 0) {
  212. [rowShareLinkSwitch setValue:@1];
  213. rowShareLinkPermission.disabled = @NO;
  214. rowPassword.disabled = @NO;
  215. rowHideDownload.disabled = @NO;
  216. rowExpirationDate.disabled = @NO;
  217. rowExpirationDateSwitch.disabled = @NO;
  218. rowSendLinkTo.disabled = @NO;
  219. } else {
  220. [rowShareLinkSwitch setValue:@0];
  221. rowShareLinkPermission.disabled = @YES;
  222. rowPassword.disabled = @YES;
  223. rowHideDownload.disabled = @YES;
  224. rowExpirationDate.disabled = @YES;
  225. rowExpirationDateSwitch.disabled = @YES;
  226. rowSendLinkTo.disabled = @YES;
  227. }
  228. // Permission
  229. if (self.itemShareLink.permissions > 0 && self.itemShareLink.shareType == shareTypeLink) {
  230. switch (self.itemShareLink.permissions) {
  231. case 1:
  232. rowShareLinkPermission.value = NSLocalizedString(@"_share_link_readonly_", nil);
  233. break;
  234. case 3:
  235. rowShareLinkPermission.value = NSLocalizedString(@"_share_link_modify_", nil);
  236. break;
  237. case 4:
  238. rowShareLinkPermission.value = NSLocalizedString(@"_share_link_upload_", nil);
  239. break;
  240. case 15:
  241. rowShareLinkPermission.value = NSLocalizedString(@"_share_link_upload_modify_", nil);
  242. break;
  243. default:
  244. break;
  245. }
  246. } else {
  247. rowShareLinkPermission.value = NSLocalizedString(@"_share_link_readonly_", nil);
  248. }
  249. // Password
  250. if ([[self.itemShareLink shareWith] length] > 0 && self.itemShareLink.shareType == shareTypeLink)
  251. rowPassword.value = [self.itemShareLink shareWith];
  252. else
  253. rowPassword.value = @"";
  254. // Hide Download
  255. if (self.itemShareLink.hideDownload) rowHideDownload.value = @1;
  256. else rowHideDownload.value = @0;
  257. // Expiration Date
  258. if (self.itemShareLink.expirationDate) {
  259. rowExpirationDateSwitch.value = @1;
  260. NSDate *expireDate;
  261. if (self.itemShareLink.expirationDate) expireDate = [NSDate dateWithTimeIntervalSince1970: self.itemShareLink.expirationDate];
  262. else expireDate = [self tomorrow];
  263. rowExpirationDate.value = expireDate;
  264. } else {
  265. rowExpirationDateSwitch.value = @0;
  266. rowExpirationDate.value = [self tomorrow];
  267. }
  268. // User & Group
  269. XLFormSectionDescriptor *section = [self.form formSectionAtIndex:4];
  270. [section.formRows removeAllObjects];
  271. [self.itemsShareWith removeAllObjects];
  272. if ([self.itemsUserAndGroupLink count] > 0) {
  273. for (NSString *idRemoteShared in self.itemsUserAndGroupLink) {
  274. OCSharedDto *item = [appDelegate.sharesID objectForKey:idRemoteShared];
  275. XLFormRowDescriptor *row = [XLFormRowDescriptor formRowDescriptorWithTag:idRemoteShared rowType:XLFormRowDescriptorTypeButton];
  276. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  277. //[row.cellConfig setObject:@(UITableViewCellAccessoryDisclosureIndicator) forKey:@"accessoryType"];
  278. [row.cellConfig setObject:[NCBrandColor sharedInstance].brandElement forKey:@"textLabel.textColor"];
  279. row.action.formSelector = @selector(sharePermissionButton:);
  280. if (item.shareType == shareTypeGroup) row.title = [item.shareWithDisplayName stringByAppendingString:NSLocalizedString(@"_user_is_group_", nil)];
  281. else row.title = item.shareWithDisplayName;
  282. //If the initiator or the recipient is not the current user, show the list of sharees without any options to edit it.
  283. if (![item.uidOwner isEqualToString:appDelegate.activeUserID] && ![item.uidFileOwner isEqualToString:appDelegate.activeUserID]) {
  284. row.disabled = @YES;
  285. }
  286. [section addFormRow:row];
  287. // add users
  288. [self.itemsShareWith addObject:item];
  289. // shared with you by
  290. if (![item.uidFileOwner isEqualToString:appDelegate.activeUserID]) {
  291. self.labelSharedWithYouBy.text = [NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"_shared_with_you_by_", nil), item.displayNameFileOwner];
  292. }
  293. }
  294. section.footerTitle = NSLocalizedString(@"_user_sharee_footer_", nil);
  295. } else {
  296. section.footerTitle = @"";
  297. }
  298. // canShare
  299. BOOL canShare = [self.metadata.permissions containsString:k_permission_can_share];
  300. if (! canShare) {
  301. rowShareLinkSwitch.disabled = @YES;
  302. rowShareLinkPermission.disabled = @YES;
  303. rowPassword.disabled = @YES;
  304. rowHideDownload.disabled = @YES;
  305. rowExpirationDate.disabled = @YES;
  306. rowExpirationDateSwitch.disabled = @YES;
  307. rowSendLinkTo.disabled = @YES;
  308. rowFindUser.disabled = @YES;
  309. XLFormSectionDescriptor *section = [self.form formSectionAtIndex:4];
  310. [section.formRows removeAllObjects];
  311. }
  312. self.form.disabled = NO;
  313. [self.tableView reloadData];
  314. self.form.delegate = self;
  315. }];
  316. }
  317. #pragma --------------------------------------------------------------------------------------------
  318. #pragma mark ===== Change Value & Button =====
  319. #pragma --------------------------------------------------------------------------------------------
  320. - (void)sendLinkTo:(XLFormRowDescriptor *)sender
  321. {
  322. [self deselectFormRow:sender];
  323. NSString *sharedLink = self.itemShareLink.token;
  324. NSString *url;
  325. if ([sharedLink hasPrefix:@"http://"] || [sharedLink hasPrefix:@"https://"]) {
  326. url = sharedLink;
  327. } else if (self.itemShareLink.url) {
  328. url = self.itemShareLink.url;
  329. } else {
  330. url = [NSString stringWithFormat:@"%@/%@%@", appDelegate.activeUrl, k_share_link_middle_part_url_after_version_8, sharedLink];
  331. }
  332. NSArray *activityItems = @[[NSString stringWithFormat:@""], [NSURL URLWithString:url]];
  333. NSArray *applicationActivities = nil;
  334. UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:applicationActivities];
  335. activityController.popoverPresentationController.sourceView = self.view;
  336. NSIndexPath *indexPath = [self.form indexPathOfFormRow:sender];
  337. CGRect cellRect = [self.tableView rectForRowAtIndexPath:indexPath];
  338. activityController.popoverPresentationController.sourceRect = CGRectOffset(cellRect, -self.tableView.contentOffset.x, -self.tableView.contentOffset.y);
  339. [self presentViewController:activityController animated:YES completion:nil];
  340. }
  341. - (void)shareUserButton:(XLFormRowDescriptor *)rowDescriptor
  342. {
  343. [self deselectFormRow:rowDescriptor];
  344. self.shareUserOC = [[UIStoryboard storyboardWithName:@"CCShare" bundle:nil] instantiateViewControllerWithIdentifier:@"CCShareUserOC"];
  345. self.shareUserOC.metadata = self.metadata;
  346. self.shareUserOC.serverUrl = self.serverUrl;
  347. self.shareUserOC.itemsShareWith = self.itemsShareWith;
  348. self.shareUserOC.isDirectory = self.metadata.directory;
  349. [self.shareUserOC setModalPresentationStyle:UIModalPresentationFormSheet];
  350. [self presentViewController:self.shareUserOC animated:YES completion:NULL];
  351. }
  352. - (void)sharePermissionButton:(XLFormRowDescriptor *)rowDescriptor
  353. {
  354. [self deselectFormRow:rowDescriptor];
  355. self.sharePermissionOC = [[UIStoryboard storyboardWithName:@"CCShare" bundle:nil] instantiateViewControllerWithIdentifier:@"CCSharePermissionOC"];
  356. self.sharePermissionOC.idRemoteShared = rowDescriptor.tag;
  357. self.sharePermissionOC.metadata = self.metadata;
  358. self.sharePermissionOC.serverUrl = self.serverUrl;
  359. [self.sharePermissionOC setModalPresentationStyle:UIModalPresentationFormSheet];
  360. [self presentViewController:self.sharePermissionOC animated:YES completion:NULL];
  361. }
  362. - (void)formRowDescriptorValueHasChanged:(XLFormRowDescriptor *)rowDescriptor oldValue:(id)oldValue newValue:(id)newValue
  363. {
  364. [super formRowDescriptorValueHasChanged:rowDescriptor oldValue:oldValue newValue:newValue];
  365. //OCSharedDto *shareDto = [appDelegate.sharesID objectForKey:self.shareLink];
  366. if ([rowDescriptor.tag isEqualToString:@"shareLinkSwitch"]) {
  367. if ([[rowDescriptor.value valueData] boolValue] == YES) {
  368. if (capabilities.isFilesSharingPublicPasswordEnforced == YES) {
  369. __weak __typeof(UIAlertController) *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_enforce_password_protection_",nil) message:nil preferredStyle:UIAlertControllerStyleAlert];
  370. [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
  371. textField.secureTextEntry = true;
  372. [textField addTarget:self action:@selector(minCharTextFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
  373. }];
  374. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_",nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  375. [self reloadDatasource];
  376. }];
  377. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  378. NSString *password = alertController.textFields.firstObject.text;
  379. XLFormRowDescriptor *rowPassword = [self.form formRowWithTag:@"password"];
  380. rowPassword.value = password;
  381. [self share:self.metadata serverUrl:self.serverUrl password:password permission:1 hideDownload:false];
  382. [self disableForm];
  383. }];
  384. okAction.enabled = NO;
  385. [alertController addAction:cancelAction];
  386. [alertController addAction:okAction];
  387. [self presentViewController:alertController animated:YES completion:nil];
  388. } else {
  389. [self share:self.metadata serverUrl:self.serverUrl password:@"" permission:1 hideDownload:false];
  390. [self disableForm];
  391. }
  392. } else {
  393. // unshare
  394. [self unShare:self.shareLink metadata:self.metadata serverUrl:self.serverUrl];
  395. [self disableForm];
  396. }
  397. }
  398. if ([rowDescriptor.tag isEqualToString:@"shareLinkPermission"]) {
  399. [self updateShare:self.shareLink metadata:self.metadata serverUrl:self.serverUrl password:nil expirationTime:nil permission:[self getShareLinkPermission:newValue] hideDownload:false];
  400. [self disableForm];
  401. }
  402. if ([rowDescriptor.tag isEqualToString:@"hideDownload"]) {
  403. BOOL hideDownload = [newValue boolValue];
  404. [self updateShare:self.shareLink metadata:self.metadata serverUrl:self.serverUrl password:nil expirationTime:nil permission:0 hideDownload:hideDownload];
  405. [self disableForm];
  406. }
  407. if ([rowDescriptor.tag isEqualToString:@"expirationDateSwitch"]) {
  408. // remove expiration date
  409. if ([[rowDescriptor.value valueData] boolValue] == NO) {
  410. [self updateShare:self.shareLink metadata:self.metadata serverUrl:self.serverUrl password:nil expirationTime:@"" permission:0 hideDownload:false];
  411. [self disableForm];
  412. } else {
  413. // new date
  414. XLFormRowDescriptor *rowExpirationDate = [self.form formRowWithTag:@"expirationDate"];
  415. NSString *expirationDate = [self convertDateInServerFormat:rowExpirationDate.value];
  416. [self updateShare:self.shareLink metadata:self.metadata serverUrl:self.serverUrl password:nil expirationTime:expirationDate permission:0 hideDownload:false];
  417. [self disableForm];
  418. }
  419. }
  420. }
  421. - (void)formRowHasBeenRemoved:(XLFormRowDescriptor *)formRow atIndexPath:(NSIndexPath *)indexPath
  422. {
  423. long long idRemoteShared = [formRow.tag longLongValue];
  424. if ([formRow.rowType isEqualToString:@"button"] && idRemoteShared > 0) {
  425. [self unShare:formRow.tag metadata:self.metadata serverUrl:self.serverUrl];
  426. [self disableForm];
  427. }
  428. }
  429. - (void)beginEditing:(XLFormRowDescriptor *)rowDescriptor
  430. {
  431. [super beginEditing:rowDescriptor];
  432. if ([rowDescriptor.tag isEqualToString:@"expirationDate"]) {
  433. self.endButton.enabled = NO;
  434. }
  435. }
  436. - (void)endEditing:(XLFormRowDescriptor *)rowDescriptor
  437. {
  438. [super endEditing:rowDescriptor];
  439. //OCSharedDto *shareDto = [appDelegate.sharesID objectForKey:self.shareLink];
  440. if ([rowDescriptor.tag isEqualToString:@"expirationDate"]) {
  441. NSDate *old = [NSDate dateWithTimeIntervalSince1970: self.itemShareLink.expirationDate];
  442. NSDate *new = rowDescriptor.value;
  443. if ([old compare:new] != NSOrderedSame) {
  444. NSString *expirationDate = [self convertDateInServerFormat:rowDescriptor.value];
  445. [self updateShare:self.shareLink metadata:self.metadata serverUrl:self.serverUrl password:nil expirationTime:expirationDate permission:0 hideDownload:false];
  446. [self disableForm];
  447. }
  448. self.endButton.enabled = YES;
  449. }
  450. if ([rowDescriptor.tag isEqualToString:@"password"]) {
  451. NSString *password = rowDescriptor.value;
  452. // Public Password Enforced Test
  453. if (capabilities.isFilesSharingPublicPasswordEnforced == YES && password == nil) {
  454. [appDelegate messageNotification:@"_share_link_" description:@"_password_obligatory_" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:k_CCErrorInternalError];
  455. [self reloadDatasource];
  456. } else {
  457. // if the password is not changed or is 0 lenght
  458. if ([[self.itemShareLink shareWith] isEqualToString:password]) {
  459. [self reloadDatasource];
  460. } else {
  461. if (password == nil)
  462. password = @"";
  463. if (self.shareLink) {
  464. [self updateShare:self.shareLink metadata:self.metadata serverUrl:self.serverUrl password:password expirationTime:nil permission:0 hideDownload:false];
  465. [self disableForm];
  466. }
  467. }
  468. }
  469. }
  470. }
  471. #pragma --------------------------------------------------------------------------------------------
  472. #pragma mark ===== Button =====
  473. #pragma --------------------------------------------------------------------------------------------
  474. - (IBAction)endButtonAction:(id)sender
  475. {
  476. [self.tableView endEditing:YES];
  477. [[NCMainCommon sharedInstance] reloadDatasourceWithServerUrl:self.metadata.serverUrl fileID:self.metadata.fileID action:k_action_MOD];
  478. [self.delegate readShareServer];
  479. [self dismissViewControllerAnimated:YES completion:nil];
  480. }
  481. #pragma --------------------------------------------------------------------------------------------
  482. #pragma mark ===== Utility =====
  483. #pragma --------------------------------------------------------------------------------------------
  484. - (void)minCharTextFieldDidChange:(UITextField *)sender
  485. {
  486. UIAlertController *alertController = (UIAlertController *)self.presentedViewController;
  487. if (alertController) {
  488. UITextField *password = alertController.textFields.firstObject;
  489. UIAlertAction *okAction = alertController.actions.lastObject;
  490. okAction.enabled = password.text.length >= 8;
  491. }
  492. }
  493. -(void)disableForm
  494. {
  495. self.form.disabled = YES;
  496. [self.tableView endEditing:YES];
  497. [self.tableView reloadData];
  498. }
  499. - (NSString *)convertDateInServerFormat:(NSDate *)date {
  500. NSDateFormatter *dateFormatter = [NSDateFormatter new];
  501. [dateFormatter setDateFormat:@"YYYY-MM-dd"];
  502. return [dateFormatter stringFromDate:date];
  503. }
  504. -(NSDate *)tomorrow
  505. {
  506. NSDate *now = [NSDate date];
  507. int daysToAdd = 1;
  508. return [now dateByAddingTimeInterval:60*60*24*daysToAdd];
  509. }
  510. - (NSInteger)getShareLinkPermission:(NSString *)value
  511. {
  512. if ([value isEqualToString:NSLocalizedString(@"_share_link_readonly_", nil)]) {
  513. return 1;
  514. } else if ([value isEqualToString:NSLocalizedString(@"_share_link_modify_", nil)]) {
  515. return 3;
  516. } else if ([value isEqualToString:NSLocalizedString(@"_share_link_upload_", nil)]) {
  517. return 4;
  518. } else if ([value isEqualToString:NSLocalizedString(@"_share_link_upload_modify_", nil)]) {
  519. return 15;
  520. } else {
  521. return 1;
  522. }
  523. }
  524. @end