CCShareOC.m 29 KB

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