CCShareUserOC.m 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. //
  2. // CCShareUserOC.m
  3. // Nextcloud iOS
  4. //
  5. // Created by Marino Faggiana on 30/11/15.
  6. // Copyright (c) 2017 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. #import "CCShareUserOC.h"
  24. #import "AppDelegate.h"
  25. #import "NCBridgeSwift.h"
  26. @interface CCShareUserOC ()
  27. {
  28. AppDelegate *appDelegate;
  29. }
  30. @end
  31. @implementation CCShareUserOC
  32. - (instancetype)initWithCoder:(NSCoder *)coder
  33. {
  34. self = [super initWithCoder:coder];
  35. if (self) {
  36. self.directUser = @"";
  37. [self initializeForm];
  38. }
  39. return self;
  40. }
  41. - (void)initializeForm
  42. {
  43. XLFormDescriptor *form ;
  44. XLFormSectionDescriptor *section;
  45. XLFormRowDescriptor *row;
  46. form = [XLFormDescriptor formDescriptor];
  47. form.rowNavigationOptions = XLFormRowNavigationOptionNone;
  48. form.assignFirstResponderOnShow = NO;
  49. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_find_sharee_title_", nil)];
  50. [form addFormSection:section];
  51. section.footerTitle = NSLocalizedString(@"_find_sharee_footer_", nil);
  52. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"findUser" rowType:XLFormRowDescriptorTypeAccount];
  53. [row.cellConfigAtConfigure setObject:NSLocalizedString(@"_find_sharee_", nil) forKey:@"textField.placeholder"];
  54. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textField.font"];
  55. [section addFormRow:row];
  56. section = [XLFormSectionDescriptor formSection];
  57. [form addFormSection:section];
  58. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_direct_sharee_title_", nil)];
  59. [form addFormSection:section];
  60. section.footerTitle = NSLocalizedString(@"_direct_sharee_footer_", nil);
  61. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"directUser" rowType:XLFormRowDescriptorTypeAccount];
  62. [row.cellConfigAtConfigure setObject:NSLocalizedString(@"_direct_sharee_", nil) forKey:@"textField.placeholder"];
  63. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textField.font"];
  64. [section addFormRow:row];
  65. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_share_type_title_", nil)];
  66. [form addFormSection:section];
  67. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"shareType" rowType:XLFormRowDescriptorTypePicker];
  68. row.selectorOptions = @[NSLocalizedString(@"_share_type_user_", nil), NSLocalizedString(@"_share_type_group_", nil), NSLocalizedString(@"_share_type_remote_", nil)];
  69. row.value = NSLocalizedString(@"_share_type_user_", nil);
  70. self.shareType = shareTypeUser;
  71. row.height = 100;
  72. [section addFormRow:row];
  73. section = [XLFormSectionDescriptor formSection];
  74. [form addFormSection:section];
  75. self.form = form;
  76. }
  77. - (void)viewDidLoad
  78. {
  79. [super viewDidLoad];
  80. appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  81. self.selectedItems = [[NSMutableArray alloc] init];
  82. self.view.backgroundColor = [NCBrandColor sharedInstance].backgroundView;
  83. [self.endButton setTitle:NSLocalizedString(@"_done_", nil) forState:UIControlStateNormal];
  84. self.endButton.tintColor = [NCBrandColor sharedInstance].brand;
  85. self.tableView.backgroundColor = [NCBrandColor sharedInstance].backgroundView;
  86. }
  87. #pragma --------------------------------------------------------------------------------------------
  88. #pragma mark ===== Button =====
  89. #pragma --------------------------------------------------------------------------------------------
  90. - (IBAction)endButtonAction:(id)sender
  91. {
  92. NSInteger permission;
  93. if (self.isDirectory) permission = k_max_folder_share_permission;
  94. else permission = k_max_file_share_permission;
  95. // start share of select users
  96. for (NSString *num in self.selectedItems) {
  97. // fix #166 Crashlytics
  98. if (self.users.count > 0 && [num integerValue] < self.users.count) {
  99. OCShareUser *item = [self.users objectAtIndex:[num integerValue]];
  100. [self.delegate shareUserAndGroup:item.name shareeType:item.shareeType permission:permission];
  101. }
  102. }
  103. // start share with a user if not be i
  104. if ([self.directUser isEqual:[NSNull null]] == NO) {
  105. if ([self.directUser length] > 0 && [self.directUser isEqualToString:appDelegate.activeUser] == NO) {
  106. // User/Group/Federate
  107. [self.delegate shareUserAndGroup:self.directUser shareeType:self.shareType permission:permission];
  108. }
  109. }
  110. [self dismissViewControllerAnimated:YES completion:nil];
  111. }
  112. #pragma --------------------------------------------------------------------------------------------
  113. #pragma mark ===== Change Value =====
  114. #pragma --------------------------------------------------------------------------------------------
  115. - (void)formRowDescriptorValueHasChanged:(XLFormRowDescriptor *)rowDescriptor oldValue:(id)oldValue newValue:(id)newValue
  116. {
  117. [super formRowDescriptorValueHasChanged:rowDescriptor oldValue:oldValue newValue:newValue];
  118. if ([rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeBooleanCheck]) {
  119. if ([newValue boolValue] == YES)
  120. [self.selectedItems addObject:rowDescriptor.tag];
  121. if ([newValue boolValue] == NO)
  122. [self.selectedItems removeObject:rowDescriptor.tag];
  123. }
  124. if ([rowDescriptor.tag isEqualToString:@"directUser"]) {
  125. self.directUser = newValue;
  126. }
  127. if ([rowDescriptor.tag isEqualToString:@"shareType"]){
  128. if ([newValue isEqualToString:NSLocalizedString(@"_share_type_user_", nil)])
  129. self.shareType = shareTypeUser;
  130. if ([newValue isEqualToString:NSLocalizedString(@"_share_type_group_", nil)])
  131. self.shareType = shareTypeGroup;
  132. if ([newValue isEqualToString:NSLocalizedString(@"_share_type_remote_", nil)])
  133. self.shareType = shareTypeRemote;
  134. }
  135. }
  136. - (void)endEditing:(XLFormRowDescriptor *)rowDescriptor
  137. {
  138. [super endEditing:rowDescriptor];
  139. if ([rowDescriptor.tag isEqualToString:@"findUser"]) {
  140. rowDescriptor.value = [rowDescriptor.value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
  141. if ([rowDescriptor.value length] > 1)
  142. [self.delegate getUserAndGroup:rowDescriptor.value];
  143. }
  144. }
  145. #pragma --------------------------------------------------------------------------------------------
  146. #pragma mark ===== Delegate =====
  147. #pragma --------------------------------------------------------------------------------------------
  148. - (void)reloadUserAndGroup:(NSArray *)items
  149. {
  150. self.users = [[NSMutableArray alloc] initWithArray:items];
  151. self.form.delegate = nil;
  152. // remove the select users and i
  153. for (OCShareUser *user in items) {
  154. for (OCSharedDto *item in self.itemsShareWith)
  155. if ([item.shareWith isEqualToString:user.name] && ((item.shareType == shareTypeGroup && user.shareeType == 1) || (item.shareType != shareTypeGroup && user.shareeType == 0)))
  156. [self.users removeObject:user];
  157. if ([self.itemsShareWith containsObject:user.name] || [user.name isEqualToString:appDelegate.activeUser])
  158. [self.users removeObject:user];
  159. }
  160. XLFormSectionDescriptor *section = [self.form formSectionAtIndex:1];
  161. [section.formRows removeAllObjects];
  162. for (OCShareUser *item in self.users) {
  163. NSInteger num = [self.users indexOfObject:item];
  164. NSString *title;
  165. if (item.shareeType == 1) title = [item.name stringByAppendingString:NSLocalizedString(@"_user_is_group_", nil)];
  166. else title = item.name;
  167. XLFormRowDescriptor *row = [XLFormRowDescriptor formRowDescriptorWithTag:[@(num) stringValue] rowType:XLFormRowDescriptorTypeBooleanCheck title:title];
  168. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  169. [row.cellConfig setObject:[NCBrandColor sharedInstance].brand forKey:@"self.tintColor"];
  170. [section addFormRow:row];
  171. }
  172. [self.tableView reloadData];
  173. self.form.delegate = self;
  174. }
  175. @end