CCShareUserOC.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. //
  2. // CCShareUserOC.m
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 30/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 "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. appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  37. self.directUser = @"";
  38. [self initializeForm];
  39. }
  40. return self;
  41. }
  42. - (void)initializeForm
  43. {
  44. XLFormDescriptor *form ;
  45. XLFormSectionDescriptor *section;
  46. XLFormRowDescriptor *row;
  47. form = [XLFormDescriptor formDescriptor];
  48. form.rowNavigationOptions = XLFormRowNavigationOptionNone;
  49. form.assignFirstResponderOnShow = NO;
  50. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_find_sharee_title_", nil)];
  51. [form addFormSection:section];
  52. section.footerTitle = NSLocalizedString(@"_find_sharee_footer_", nil);
  53. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"findUser" rowType:XLFormRowDescriptorTypeAccount];
  54. [row.cellConfigAtConfigure setObject:NSLocalizedString(@"_find_sharee_", nil) forKey:@"textField.placeholder"];
  55. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textField.font"];
  56. [section addFormRow:row];
  57. section = [XLFormSectionDescriptor formSection];
  58. [form addFormSection:section];
  59. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_direct_sharee_title_", nil)];
  60. [form addFormSection:section];
  61. section.footerTitle = NSLocalizedString(@"_direct_sharee_footer_", nil);
  62. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"directUser" rowType:XLFormRowDescriptorTypeAccount];
  63. [row.cellConfigAtConfigure setObject:NSLocalizedString(@"_direct_sharee_", nil) forKey:@"textField.placeholder"];
  64. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textField.font"];
  65. [section addFormRow:row];
  66. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_share_type_title_", nil)];
  67. [form addFormSection:section];
  68. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"shareType" rowType:XLFormRowDescriptorTypePicker];
  69. row.selectorOptions = @[NSLocalizedString(@"_share_type_user_", nil), NSLocalizedString(@"_share_type_group_", nil), NSLocalizedString(@"_share_type_remote_", nil)];
  70. row.value = NSLocalizedString(@"_share_type_user_", nil);
  71. self.shareType = shareTypeUser;
  72. row.height = 100;
  73. [section addFormRow:row];
  74. section = [XLFormSectionDescriptor formSection];
  75. [form addFormSection:section];
  76. self.form = form;
  77. }
  78. - (void)viewDidLoad
  79. {
  80. [super viewDidLoad];
  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 = [UIColor blackColor];
  85. self.tableView.backgroundColor = [NCBrandColor sharedInstance].backgroundView;
  86. }
  87. #pragma --------------------------------------------------------------------------------------------
  88. #pragma mark ===== Networking =====
  89. #pragma --------------------------------------------------------------------------------------------
  90. - (void)shareUserAndGroup:(NSString *)user shareeType:(NSInteger)shareeType permission:(NSInteger)permission metadata:(tableMetadata *)metadata serverUrl:(NSString *)serverUrl
  91. {
  92. NSString *fileName = [CCUtility returnFileNamePathFromFileName:metadata.fileName serverUrl:serverUrl activeUrl:appDelegate.activeUrl];
  93. [[OCNetworking sharedManager] shareUserGroupWithAccount:appDelegate.activeAccount userOrGroup:user fileName:fileName permission:permission shareeType:shareeType completion:^(NSString *account, NSString *message, NSInteger errorCode) {
  94. if (errorCode != 0) {
  95. [appDelegate messageNotification:@"_share_" description:message visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
  96. }
  97. }];
  98. }
  99. - (void)getUserAndGroup:(NSString *)find
  100. {
  101. [[OCNetworking sharedManager] getUserGroupWithAccount:appDelegate.activeAccount searchString:find completion:^(NSString *account, NSArray *item, NSString *message, NSInteger errorCode) {
  102. if (errorCode == 0) {
  103. [self reloadUserAndGroup:item];
  104. } else {
  105. [appDelegate messageNotification:@"_error_" description:message visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
  106. }
  107. }];
  108. }
  109. #pragma --------------------------------------------------------------------------------------------
  110. #pragma mark ===== Button =====
  111. #pragma --------------------------------------------------------------------------------------------
  112. - (IBAction)endButtonAction:(id)sender
  113. {
  114. NSInteger permission;
  115. if (self.isDirectory) permission = k_max_folder_share_permission;
  116. else permission = k_max_file_share_permission;
  117. // start share of select users
  118. for (NSString *num in self.selectedItems) {
  119. // fix #166 Crashlytics
  120. if (self.users.count > 0 && [num integerValue] < self.users.count) {
  121. OCShareUser *item = [self.users objectAtIndex:[num integerValue]];
  122. [self shareUserAndGroup:item.name shareeType:item.shareeType permission:permission metadata:self.metadata serverUrl:self.serverUrl];
  123. }
  124. }
  125. // add manual user
  126. if ([self.directUser isEqual:[NSNull null]] == NO && [self.directUser length] > 0 && [self.directUser isEqualToString:appDelegate.activeUser] == NO) {
  127. [self shareUserAndGroup:self.directUser shareeType:self.shareType permission:permission metadata:self.metadata serverUrl:self.serverUrl];
  128. }
  129. [self dismissViewControllerAnimated:YES completion:nil];
  130. }
  131. #pragma --------------------------------------------------------------------------------------------
  132. #pragma mark ===== Change Value =====
  133. #pragma --------------------------------------------------------------------------------------------
  134. - (void)formRowDescriptorValueHasChanged:(XLFormRowDescriptor *)rowDescriptor oldValue:(id)oldValue newValue:(id)newValue
  135. {
  136. [super formRowDescriptorValueHasChanged:rowDescriptor oldValue:oldValue newValue:newValue];
  137. if ([rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeBooleanCheck]) {
  138. if ([newValue boolValue] == YES)
  139. [self.selectedItems addObject:rowDescriptor.tag];
  140. if ([newValue boolValue] == NO)
  141. [self.selectedItems removeObject:rowDescriptor.tag];
  142. }
  143. if ([rowDescriptor.tag isEqualToString:@"directUser"]) {
  144. self.directUser = newValue;
  145. }
  146. if ([rowDescriptor.tag isEqualToString:@"shareType"]){
  147. if ([newValue isEqualToString:NSLocalizedString(@"_share_type_user_", nil)])
  148. self.shareType = shareTypeUser;
  149. if ([newValue isEqualToString:NSLocalizedString(@"_share_type_group_", nil)])
  150. self.shareType = shareTypeGroup;
  151. if ([newValue isEqualToString:NSLocalizedString(@"_share_type_remote_", nil)])
  152. self.shareType = shareTypeRemote;
  153. }
  154. }
  155. - (void)endEditing:(XLFormRowDescriptor *)rowDescriptor
  156. {
  157. [super endEditing:rowDescriptor];
  158. if ([rowDescriptor.tag isEqualToString:@"findUser"]) {
  159. rowDescriptor.value = [rowDescriptor.value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
  160. if ([rowDescriptor.value length] > 1)
  161. [self getUserAndGroup:rowDescriptor.value];
  162. }
  163. }
  164. #pragma --------------------------------------------------------------------------------------------
  165. #pragma mark ===== Delegate =====
  166. #pragma --------------------------------------------------------------------------------------------
  167. - (void)reloadUserAndGroup:(NSArray *)items
  168. {
  169. self.users = [[NSMutableArray alloc] initWithArray:items];
  170. self.form.delegate = nil;
  171. // remove the select users and i
  172. for (OCShareUser *user in items) {
  173. for (OCSharedDto *item in self.itemsShareWith)
  174. if ([item.shareWith isEqualToString:user.name] && ((item.shareType == shareTypeGroup && user.shareeType == 1) || (item.shareType != shareTypeGroup && user.shareeType == 0)))
  175. [self.users removeObject:user];
  176. if ([self.itemsShareWith containsObject:user.name] || [user.name isEqualToString:appDelegate.activeUser])
  177. [self.users removeObject:user];
  178. }
  179. XLFormSectionDescriptor *section = [self.form formSectionAtIndex:1];
  180. [section.formRows removeAllObjects];
  181. for (OCShareUser *item in self.users) {
  182. NSInteger num = [self.users indexOfObject:item];
  183. NSString *title;
  184. if (item.shareeType == 1) {
  185. if (item.displayName)
  186. title = [item.displayName stringByAppendingString:NSLocalizedString(@"_user_is_group_", nil)];
  187. else
  188. title = [item.name stringByAppendingString:NSLocalizedString(@"_user_is_group_", nil)];
  189. } else {
  190. if (item.displayName)
  191. title = item.displayName;
  192. else
  193. title = item.name;
  194. }
  195. XLFormRowDescriptor *row = [XLFormRowDescriptor formRowDescriptorWithTag:[@(num) stringValue] rowType:XLFormRowDescriptorTypeBooleanCheck title:title];
  196. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  197. [row.cellConfig setObject:[NCBrandColor sharedInstance].brandElement forKey:@"self.tintColor"];
  198. [section addFormRow:row];
  199. }
  200. [self.tableView reloadData];
  201. self.form.delegate = self;
  202. }
  203. @end