CCShareUserOC.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. //
  2. // CCShareUserOC.m
  3. // Nextcloud iOS
  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 && [account isEqualToString:appDelegate.activeAccount]) {
  95. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:@"ShareReloadDatasource" object:nil userInfo:nil];
  96. [self dismissViewControllerAnimated:YES completion:nil];
  97. } else if (errorCode != 0) {
  98. [appDelegate messageNotification:@"_share_" description:message visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
  99. }
  100. }];
  101. }
  102. - (void)getUserAndGroup:(NSString *)find
  103. {
  104. [[OCNetworking sharedManager] getUserGroupWithAccount:appDelegate.activeAccount searchString:find completion:^(NSString *account, NSArray *item, NSString *message, NSInteger errorCode) {
  105. if (errorCode == 0 && [account isEqualToString:appDelegate.activeAccount]) {
  106. [self reloadUserAndGroup:item];
  107. } else if (errorCode != 0) {
  108. [appDelegate messageNotification:@"_error_" description:message visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
  109. }
  110. }];
  111. }
  112. #pragma --------------------------------------------------------------------------------------------
  113. #pragma mark ===== Button =====
  114. #pragma --------------------------------------------------------------------------------------------
  115. - (IBAction)endButtonAction:(id)sender
  116. {
  117. NSInteger permission;
  118. if (self.isDirectory) permission = k_max_folder_share_permission;
  119. else permission = k_max_file_share_permission;
  120. // start share of select users
  121. for (NSString *num in self.selectedItems) {
  122. // fix #166 Crashlytics
  123. if (self.users.count > 0 && [num integerValue] < self.users.count) {
  124. OCShareUser *item = [self.users objectAtIndex:[num integerValue]];
  125. [self shareUserAndGroup:item.name shareeType:item.shareeType permission:permission metadata:self.metadata serverUrl:self.serverUrl];
  126. }
  127. }
  128. // start share with a user if not be i
  129. if ([self.directUser isEqual:[NSNull null]] == NO) {
  130. if ([self.directUser length] > 0 && [self.directUser isEqualToString:appDelegate.activeUser] == NO) {
  131. // User/Group/Federate
  132. [self shareUserAndGroup:self.directUser shareeType:self.shareType permission:permission metadata:self.metadata serverUrl:self.serverUrl];
  133. }
  134. }
  135. }
  136. #pragma --------------------------------------------------------------------------------------------
  137. #pragma mark ===== Change Value =====
  138. #pragma --------------------------------------------------------------------------------------------
  139. - (void)formRowDescriptorValueHasChanged:(XLFormRowDescriptor *)rowDescriptor oldValue:(id)oldValue newValue:(id)newValue
  140. {
  141. [super formRowDescriptorValueHasChanged:rowDescriptor oldValue:oldValue newValue:newValue];
  142. if ([rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeBooleanCheck]) {
  143. if ([newValue boolValue] == YES)
  144. [self.selectedItems addObject:rowDescriptor.tag];
  145. if ([newValue boolValue] == NO)
  146. [self.selectedItems removeObject:rowDescriptor.tag];
  147. }
  148. if ([rowDescriptor.tag isEqualToString:@"directUser"]) {
  149. self.directUser = newValue;
  150. }
  151. if ([rowDescriptor.tag isEqualToString:@"shareType"]){
  152. if ([newValue isEqualToString:NSLocalizedString(@"_share_type_user_", nil)])
  153. self.shareType = shareTypeUser;
  154. if ([newValue isEqualToString:NSLocalizedString(@"_share_type_group_", nil)])
  155. self.shareType = shareTypeGroup;
  156. if ([newValue isEqualToString:NSLocalizedString(@"_share_type_remote_", nil)])
  157. self.shareType = shareTypeRemote;
  158. }
  159. }
  160. - (void)endEditing:(XLFormRowDescriptor *)rowDescriptor
  161. {
  162. [super endEditing:rowDescriptor];
  163. if ([rowDescriptor.tag isEqualToString:@"findUser"]) {
  164. rowDescriptor.value = [rowDescriptor.value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
  165. if ([rowDescriptor.value length] > 1)
  166. [self getUserAndGroup:rowDescriptor.value];
  167. }
  168. }
  169. #pragma --------------------------------------------------------------------------------------------
  170. #pragma mark ===== Delegate =====
  171. #pragma --------------------------------------------------------------------------------------------
  172. - (void)reloadUserAndGroup:(NSArray *)items
  173. {
  174. self.users = [[NSMutableArray alloc] initWithArray:items];
  175. self.form.delegate = nil;
  176. // remove the select users and i
  177. for (OCShareUser *user in items) {
  178. for (OCSharedDto *item in self.itemsShareWith)
  179. if ([item.shareWith isEqualToString:user.name] && ((item.shareType == shareTypeGroup && user.shareeType == 1) || (item.shareType != shareTypeGroup && user.shareeType == 0)))
  180. [self.users removeObject:user];
  181. if ([self.itemsShareWith containsObject:user.name] || [user.name isEqualToString:appDelegate.activeUser])
  182. [self.users removeObject:user];
  183. }
  184. XLFormSectionDescriptor *section = [self.form formSectionAtIndex:1];
  185. [section.formRows removeAllObjects];
  186. for (OCShareUser *item in self.users) {
  187. NSInteger num = [self.users indexOfObject:item];
  188. NSString *title;
  189. if (item.shareeType == 1) {
  190. if (item.displayName)
  191. title = [item.displayName stringByAppendingString:NSLocalizedString(@"_user_is_group_", nil)];
  192. else
  193. title = [item.name stringByAppendingString:NSLocalizedString(@"_user_is_group_", nil)];
  194. } else {
  195. if (item.displayName)
  196. title = item.displayName;
  197. else
  198. title = item.name;
  199. }
  200. XLFormRowDescriptor *row = [XLFormRowDescriptor formRowDescriptorWithTag:[@(num) stringValue] rowType:XLFormRowDescriptorTypeBooleanCheck title:title];
  201. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  202. [row.cellConfig setObject:[NCBrandColor sharedInstance].brandElement forKey:@"self.tintColor"];
  203. [section addFormRow:row];
  204. }
  205. [self.tableView reloadData];
  206. self.form.delegate = self;
  207. }
  208. @end