CCShareOC.m 30 KB

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