123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504 |
- //
- // CCShareOC.m
- // Crypto Cloud Technology Nextcloud
- //
- // Created by Marino Faggiana on 13/11/15.
- // Copyright (c) 2017 TWS. All rights reserved.
- //
- // Author Marino Faggiana <m.faggiana@twsweb.it>
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program. If not, see <http://www.gnu.org/licenses/>.
- //
- #import "CCShareOC.h"
- #import "AppDelegate.h"
- #import "NCBridgeSwift.h"
- @interface CCShareOC ()
- @end
- @implementation CCShareOC
- - (instancetype)initWithCoder:(NSCoder *)coder
- {
- self = [super initWithCoder:coder];
- if (self) {
-
- self.itemsShareWith = [[NSMutableArray alloc] init];
-
- [self initializeForm];
- }
- return self;
- }
- - (void)initializeForm
- {
- XLFormDescriptor *form ;
- XLFormSectionDescriptor *section;
- XLFormRowDescriptor *row;
-
- form = [XLFormDescriptor formDescriptor];
- form.rowNavigationOptions = XLFormRowNavigationOptionNone;
-
- // Share Link
-
- section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_share_link_", nil)];
- [form addFormSection:section];
- row = [XLFormRowDescriptor formRowDescriptorWithTag:@"password" rowType:XLFormRowDescriptorTypePassword title:NSLocalizedString(@"_password_", nil)];
- [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
- [section addFormRow:row];
-
- row = [XLFormRowDescriptor formRowDescriptorWithTag:@"shareLinkSwitch" rowType:XLFormRowDescriptorTypeBooleanSwitch title:NSLocalizedString(@"_share_link_", nil)];
- [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
- [section addFormRow:row];
-
- // Expiration date
-
- section = [XLFormSectionDescriptor formSection];
- [form addFormSection:section];
-
- row = [XLFormRowDescriptor formRowDescriptorWithTag:@"expirationDate" rowType:XLFormRowDescriptorTypeDate title:NSLocalizedString(@"_date_", nil)];
- [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
- row.value = [self tomorrow];
- [row.cellConfigAtConfigure setObject:[self tomorrow] forKey:@"minimumDate"];
- [section addFormRow:row];
-
- row = [XLFormRowDescriptor formRowDescriptorWithTag:@"expirationDateSwitch" rowType:XLFormRowDescriptorTypeBooleanSwitch title:NSLocalizedString(@"_share_expirationdate_", nil)];
- [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
- [section addFormRow:row];
-
- // Send Link To
-
- section = [XLFormSectionDescriptor formSection];
- [form addFormSection:section];
- row = [XLFormRowDescriptor formRowDescriptorWithTag:@"sendLinkTo" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_share_link_button_", nil)];
- [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
- [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
- row.action.formSelector = @selector(sendLinkTo:);
- [section addFormRow:row];
- // Sharee
-
- section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_share_title_", nil)];
- [form addFormSection:section];
- section.footerTitle = NSLocalizedString(@"_add_sharee_footer_", nil);
-
- row = [XLFormRowDescriptor formRowDescriptorWithTag:@"findUser" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_add_sharee_", nil)];
- [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
- [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
- row.action.formSelector = @selector(shareUserButton:);
- [section addFormRow:row];
-
- section = [XLFormSectionDescriptor formSectionWithTitle:@"" sectionOptions:XLFormSectionOptionCanDelete];
- [form addFormSection:section];
-
- self.form = form;
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
-
- self.view.backgroundColor = [NCBrandColor sharedInstance].tableBackground;
-
- [self.endButton setTitle:NSLocalizedString(@"_done_", nil) forState:UIControlStateNormal];
- self.endButton.tintColor = [NCBrandColor sharedInstance].brand;
-
- [self reloadData];
-
- if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, self.metadata.fileID]]) {
-
- self.fileImageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, self.metadata.fileID]];
-
- } else {
-
- if (self.metadata.directory)
- self.fileImageView.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:self.metadata.iconName] color:[NCBrandColor sharedInstance].brand];
- else
- self.fileImageView.image = [UIImage imageNamed:self.metadata.iconName];
- }
-
- self.labelTitle.text = self.metadata.fileNamePrint;
- self.labelTitle.textColor = [UIColor blackColor];
-
- self.tableView.tableHeaderView = ({UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 0.1 / UIScreen.mainScreen.scale)];
- line.backgroundColor = self.tableView.separatorColor;
- line;
- });
-
- self.tableView.backgroundColor = [NCBrandColor sharedInstance].tableBackground;
- }
- #pragma --------------------------------------------------------------------------------------------
- #pragma mark ===== Reload Data =====
- #pragma --------------------------------------------------------------------------------------------
- - (void)reloadData
- {
- self.shareLink = [app.sharesLink objectForKey:[self.serverUrl stringByAppendingString:self.metadata.fileName]];
- self.shareUserAndGroup = [app.sharesUserAndGroup objectForKey:[self.serverUrl stringByAppendingString:self.metadata.fileName]];
- self.itemShareLink = [app.sharesID objectForKey:self.shareLink];
- if ([self.shareUserAndGroup length] > 0) self.itemsUserAndGroupLink = [self.shareUserAndGroup componentsSeparatedByString:@","];
- else self.itemsUserAndGroupLink = nil;
- self.form.delegate = nil;
- XLFormRowDescriptor *rowPassword = [self.form formRowWithTag:@"password"];
- XLFormRowDescriptor *rowShareLinkSwitch = [self.form formRowWithTag:@"shareLinkSwitch"];
-
- XLFormRowDescriptor *rowExpirationDate = [self.form formRowWithTag:@"expirationDate"];
- XLFormRowDescriptor *rowExpirationDateSwitch = [self.form formRowWithTag:@"expirationDateSwitch"];
-
- XLFormRowDescriptor *rowSendLinkTo = [self.form formRowWithTag:@"sendLinkTo"];
- // Passoword
- if ([[self.itemShareLink shareWith] length] > 0 && self.itemShareLink.shareType == shareTypeLink)
- rowPassword.value = [self.itemShareLink shareWith];
- else
- rowPassword.value = @"";
- // Share Link
- if ([self.shareLink length] > 0) {
-
- [rowShareLinkSwitch setValue:@1];
-
- rowExpirationDate.disabled = @NO;
- rowExpirationDateSwitch.disabled = @NO;
- rowSendLinkTo.disabled = @NO;
-
- } else {
-
- [rowShareLinkSwitch setValue:@0];
-
- rowExpirationDate.disabled = @YES;
- rowExpirationDateSwitch.disabled = @YES;
-
- rowSendLinkTo.disabled = @YES;
- }
-
- // Expiration Date
- if (self.itemShareLink.expirationDate) {
-
- rowExpirationDateSwitch.value = @1;
- NSDate *expireDate;
-
- if (self.itemShareLink.expirationDate) expireDate = [NSDate dateWithTimeIntervalSince1970: self.itemShareLink.expirationDate];
- else expireDate = [self tomorrow];
-
- rowExpirationDate.value = expireDate;
-
- } else {
-
- rowExpirationDateSwitch.value = @0;
- rowExpirationDate.value = [self tomorrow];
- }
-
- // User & Group
- XLFormSectionDescriptor *section = [self.form formSectionAtIndex:4];
- [section.formRows removeAllObjects];
- [self.itemsShareWith removeAllObjects];
-
- if ([self.itemsUserAndGroupLink count] > 0) {
-
- for (NSString *idRemoteShared in self.itemsUserAndGroupLink) {
-
- OCSharedDto *item = [app.sharesID objectForKey:idRemoteShared];
-
- XLFormRowDescriptor *row = [XLFormRowDescriptor formRowDescriptorWithTag:idRemoteShared rowType:XLFormRowDescriptorTypeButton];
- [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
- //[row.cellConfig setObject:@(UITableViewCellAccessoryDisclosureIndicator) forKey:@"accessoryType"];
- [row.cellConfig setObject:[NCBrandColor sharedInstance].brand forKey:@"textLabel.textColor"];
- row.action.formSelector = @selector(sharePermissionButton:);
-
- if (item.shareType == shareTypeGroup) row.title = [item.shareWithDisplayName stringByAppendingString:NSLocalizedString(@"_user_is_group_", nil)];
- else row.title = item.shareWithDisplayName;
-
- [section addFormRow:row];
-
- // add users
- [self.itemsShareWith addObject:item];
- }
-
- section.footerTitle = NSLocalizedString(@"_user_sharee_footer_", nil);
- } else {
-
- section.footerTitle = @"";
- }
-
- self.form.disabled = NO;
-
- [self.tableView reloadData];
-
- self.form.delegate = self;
- }
- #pragma --------------------------------------------------------------------------------------------
- #pragma mark ===== Change Value & Button =====
- #pragma --------------------------------------------------------------------------------------------
- - (void)sendLinkTo:(XLFormRowDescriptor *)sender
- {
- [self deselectFormRow:sender];
-
- NSString *sharedLink = self.itemShareLink.token;
- NSString *url;
-
- if ([sharedLink hasPrefix:@"http://"] || [sharedLink hasPrefix:@"https://"]) {
-
- url = sharedLink;
-
- } else {
- url = [NSString stringWithFormat:@"%@/%@%@", app.activeUrl, k_share_link_middle_part_url_after_version_8, sharedLink];
- }
- NSArray *activityItems = @[[NSString stringWithFormat:@""], [NSURL URLWithString:url]];
- NSArray *applicationActivities = nil;
-
- UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:applicationActivities];
-
- if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) [self presentViewController:activityController animated:YES completion:nil];
- if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
-
- UIPopoverController *popup;
-
- popup = [[UIPopoverController alloc] initWithContentViewController:activityController];
- [popup presentPopoverFromRect:CGRectMake(120, 100, 200, 400) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
- }
- }
- - (void)shareUserButton:(XLFormRowDescriptor *)rowDescriptor
- {
- [self deselectFormRow:rowDescriptor];
-
- self.shareUserOC = [[UIStoryboard storyboardWithName:@"CCShare" bundle:nil] instantiateViewControllerWithIdentifier:@"CCShareUserOC"];
- self.shareUserOC.delegate = self;
- self.shareUserOC.itemsShareWith = self.itemsShareWith;
- self.shareUserOC.isDirectory = self.metadata.directory;
-
- [self.shareUserOC setModalPresentationStyle:UIModalPresentationFormSheet];
- [self presentViewController:self.shareUserOC animated:YES completion:NULL];
- }
- - (void)sharePermissionButton:(XLFormRowDescriptor *)rowDescriptor
- {
- [self deselectFormRow:rowDescriptor];
-
- self.sharePermissionOC = [[UIStoryboard storyboardWithName:@"CCShare" bundle:nil] instantiateViewControllerWithIdentifier:@"CCSharePermissionOC"];
- self.sharePermissionOC.delegate = self;
- self.sharePermissionOC.idRemoteShared = rowDescriptor.tag;
- self.sharePermissionOC.metadata = self.metadata;
- self.sharePermissionOC.serverUrl = self.serverUrl;
-
-
- [self.sharePermissionOC setModalPresentationStyle:UIModalPresentationFormSheet];
- [self presentViewController:self.sharePermissionOC animated:YES completion:NULL];
- }
- - (void)formRowDescriptorValueHasChanged:(XLFormRowDescriptor *)rowDescriptor oldValue:(id)oldValue newValue:(id)newValue
- {
- [super formRowDescriptorValueHasChanged:rowDescriptor oldValue:oldValue newValue:newValue];
-
- OCSharedDto *shareDto = [app.sharesID objectForKey:self.shareLink];
-
- if ([rowDescriptor.tag isEqualToString:@"shareLinkSwitch"]) {
-
- if ([[rowDescriptor.value valueData] boolValue] == YES) {
-
- // share
- XLFormRowDescriptor *rowPassword = [self.form formRowWithTag:@"password"];
-
- [self.delegate share:self.metadata serverUrl:self.serverUrl password:rowPassword.value];
- [self disableForm];
-
- } else {
-
- // unshare
- [self.delegate unShare:self.shareLink metadata:self.metadata serverUrl:self.serverUrl];
- [self disableForm];
- }
- }
-
- if ([rowDescriptor.tag isEqualToString:@"expirationDateSwitch"]) {
-
- // remove expiration date
- if ([[rowDescriptor.value valueData] boolValue] == NO) {
-
- [self.delegate updateShare:self.shareLink metadata:self.metadata serverUrl:self.serverUrl password:nil expirationTime:@"" permission:shareDto.permissions];
- [self disableForm];
-
- } else {
-
- // new date
- XLFormRowDescriptor *rowExpirationDate = [self.form formRowWithTag:@"expirationDate"];
- NSString *expirationDate = [self convertDateInServerFormat:rowExpirationDate.value];
-
- [self.delegate updateShare:self.shareLink metadata:self.metadata serverUrl:self.serverUrl password:nil expirationTime:expirationDate permission:shareDto.permissions];
- [self disableForm];
- }
- }
- }
- - (void)formRowHasBeenRemoved:(XLFormRowDescriptor *)formRow atIndexPath:(NSIndexPath *)indexPath
- {
- long long idRemoteShared = [formRow.tag longLongValue];
-
- if ([formRow.rowType isEqualToString:@"button"] && idRemoteShared > 0) {
-
- [self.delegate unShare:formRow.tag metadata:self.metadata serverUrl:self.serverUrl];
- [self disableForm];
- }
- }
- - (void)beginEditing:(XLFormRowDescriptor *)rowDescriptor
- {
- [super beginEditing:rowDescriptor];
-
- if ([rowDescriptor.tag isEqualToString:@"expirationDate"]) {
-
- self.endButton.enabled = NO;
- }
- }
- - (void)endEditing:(XLFormRowDescriptor *)rowDescriptor
- {
- [super endEditing:rowDescriptor];
-
- OCSharedDto *shareDto = [app.sharesID objectForKey:self.shareLink];
-
- if ([rowDescriptor.tag isEqualToString:@"expirationDate"]) {
-
- NSDate *old = [NSDate dateWithTimeIntervalSince1970: self.itemShareLink.expirationDate];
- NSDate *new = rowDescriptor.value;
-
- if ([old compare:new] != NSOrderedSame) {
-
- NSString *expirationDate = [self convertDateInServerFormat:rowDescriptor.value];
-
- [self.delegate updateShare:self.shareLink metadata:self.metadata serverUrl:self.serverUrl password:nil expirationTime:expirationDate permission:shareDto.permissions];
- [self disableForm];
- }
-
- self.endButton.enabled = YES;
- }
-
- if ([rowDescriptor.tag isEqualToString:@"password"]) {
-
- NSString *password = rowDescriptor.value;
-
- // if the password is not changed or is 0 lenght
- if ([[self.itemShareLink shareWith] isEqualToString:password]) {
-
- [self reloadData];
-
- } else {
-
- /*
- if (password == nil) {
-
- [self reloadData];
- return;
- }
- */
-
- if (password == nil)
- password = @"";
-
- if (self.shareLink) {
-
- [self.delegate updateShare:self.shareLink metadata:self.metadata serverUrl:self.serverUrl password:password expirationTime:nil permission:shareDto.permissions];
- [self disableForm];
- }
- }
- }
- }
- #pragma --------------------------------------------------------------------------------------------
- #pragma mark ===== Button =====
- #pragma --------------------------------------------------------------------------------------------
- - (IBAction)endButtonAction:(id)sender
- {
- [self.tableView endEditing:YES];
-
- // reload delegate
- [self.delegate reloadDatasource:[CCCoreData getServerUrlFromDirectoryID:self.metadata.directoryID activeAccount:self.metadata.account] fileID:nil selector:nil];
-
- [self dismissViewControllerAnimated:YES completion:nil];
- }
- #pragma --------------------------------------------------------------------------------------------
- #pragma mark ===== User & Group =====
- #pragma --------------------------------------------------------------------------------------------
- - (void)getUserAndGroup:(NSString *)find
- {
- [self.delegate getUserAndGroup:find];
- }
- - (void)reloadUserAndGroup:(NSArray *)items
- {
- if (self.shareUserOC)
- [self.shareUserOC reloadUserAndGroup:items];
- }
- - (void)shareUserAndGroup:(NSString *)user shareeType:(NSInteger)shareeType permission:(NSInteger)permission
- {
- [self.delegate shareUserAndGroup:user shareeType:shareeType permission:permission metadata:self.metadata directoryID:self.metadata.directoryID serverUrl:self.serverUrl];
- }
- - (void)updateShare:(NSString *)share metadata:(CCMetadata *)metadata serverUrl:(NSString *)serverUrl password:(NSString *)password expirationTime:(NSString *)expirationTime permission:(NSInteger)permission
- {
- [self.delegate updateShare:share metadata:metadata serverUrl:serverUrl password:password expirationTime:expirationTime permission:permission];
- }
- #pragma --------------------------------------------------------------------------------------------
- #pragma mark ===== Utility =====
- #pragma --------------------------------------------------------------------------------------------
- -(void)disableForm
- {
- self.form.disabled = YES;
- [self.tableView endEditing:YES];
- [self.tableView reloadData];
- }
- - (NSString *)convertDateInServerFormat:(NSDate *)date {
-
- NSDateFormatter *dateFormatter = [NSDateFormatter new];
-
- [dateFormatter setDateFormat:@"YYYY-MM-dd"];
-
- return [dateFormatter stringFromDate:date];
- }
- -(NSDate *)tomorrow
- {
- NSDate *now = [NSDate date];
- int daysToAdd = 1;
- return [now dateByAddingTimeInterval:60*60*24*daysToAdd];
- }
- @end
|