UIChatNotifiedEventCell.m 8.67 KB
Newer Older
DanmeiChen's avatar
DanmeiChen committed
1
/*
DanmeiChen's avatar
DanmeiChen committed
2
 * Copyright (c) 2010-2020 Belledonne Communications SARL.
DanmeiChen's avatar
DanmeiChen committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This file is part of linphone-iphone
 *
 * 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/>.
 */
19 20 21 22 23 24 25 26 27 28 29

#import <Foundation/Foundation.h>
#import "UIChatNotifiedEventCell.h"
#import "LinphoneManager.h"
#import "PhoneMainView.h"

#import <AssetsLibrary/ALAsset.h>
#import <AssetsLibrary/ALAssetRepresentation.h>

@implementation UIChatNotifiedEventCell

30 31 32 33 34 35 36
#pragma mark - Class methods
static const CGFloat NOTIFIED_CELL_HEIGHT = 44;

+ (CGFloat)height {
	return NOTIFIED_CELL_HEIGHT;
}

37 38 39 40 41 42 43 44 45 46 47 48 49 50
#pragma mark - Lifecycle Functions

- (id)initWithIdentifier:(NSString *)identifier {
	if ((self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]) != nil) {
		if ([identifier isEqualToString:NSStringFromClass(self.class)]) {
			NSArray *arrayOfViews =
			[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self.class) owner:self options:nil];
			// resize cell to match .nib size. It is needed when resized the cell to
			// correctly adapt its height too
			UIView *sub = ((UIView *)[arrayOfViews objectAtIndex:arrayOfViews.count - 1]);
			[self setFrame:CGRectMake(0, 0, sub.frame.size.width, sub.frame.size.height)];
			[self addSubview:sub];
		}
	}
51
	_event = NULL;
52 53 54 55 56 57 58
	return self;
}

- (void)dealloc {
	_event = NULL;
}

59 60 61 62 63 64 65 66 67 68
- (void)setEditing:(BOOL)editing {
	[self setEditing:editing animated:FALSE];
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
	_leftBar.hidden = _rightBar.hidden = editing;
	if (editing)
		[_contactDateLabel setFrame:CGRectMake(1, 1, _contactDateLabel.frame.size.width, NOTIFIED_CELL_HEIGHT)];
}

69 70 71
#pragma mark -

- (void)setEvent:(LinphoneEventLog *)event {
72 73
	_event = event;
	NSString *eventString;
DanmeiChen's avatar
DanmeiChen committed
74
    UIColor *eventColor = [UIColor grayColor];
75 76
	switch (linphone_event_log_get_type(event)) {
		case LinphoneEventLogTypeConferenceSubjectChanged: {
Benjamin REIS's avatar
Benjamin REIS committed
77
			NSString *subject = [NSString stringWithUTF8String:linphone_event_log_get_subject(event) ?: LINPHONE_DUMMY_SUBJECT];
Benjamin REIS's avatar
Benjamin REIS committed
78
			eventString = [NSString stringWithFormat:NSLocalizedString(@"New subject : %@", nil), subject];
79 80 81 82
			break;
		}
		case LinphoneEventLogTypeConferenceParticipantAdded: {
			NSString *participant = [FastAddressBook displayNameForAddress:linphone_event_log_get_participant_address(event)];
Benjamin REIS's avatar
Benjamin REIS committed
83
			eventString = [NSString stringWithFormat:NSLocalizedString(@"%@ has joined", nil), participant];
84 85 86 87
			break;
		}
		case LinphoneEventLogTypeConferenceParticipantRemoved: {
			NSString *participant = [FastAddressBook displayNameForAddress:linphone_event_log_get_participant_address(event)];
Benjamin REIS's avatar
Benjamin REIS committed
88
			eventString = [NSString stringWithFormat:NSLocalizedString(@"%@ has left", nil), participant];
89 90 91 92
			break;
		}
		case LinphoneEventLogTypeConferenceParticipantSetAdmin: {
			NSString *participant = [FastAddressBook displayNameForAddress:linphone_event_log_get_participant_address(event)];
Benjamin REIS's avatar
Benjamin REIS committed
93
			eventString = [NSString stringWithFormat:NSLocalizedString(@"%@ is now an admin", nil), participant];
94 95 96 97
			break;
		}
		case LinphoneEventLogTypeConferenceParticipantUnsetAdmin: {
			NSString *participant = [FastAddressBook displayNameForAddress:linphone_event_log_get_participant_address(event)];
Benjamin REIS's avatar
Benjamin REIS committed
98
			eventString = [NSString stringWithFormat:NSLocalizedString(@"%@ is no longer an admin", nil), participant];
Benjamin REIS's avatar
Benjamin REIS committed
99 100 101
			break;
		}
		case LinphoneEventLogTypeConferenceTerminated: {
Benjamin REIS's avatar
Benjamin REIS committed
102
			eventString = [NSString stringWithFormat:NSLocalizedString(@"You have left the group", nil)];
Benjamin REIS's avatar
Benjamin REIS committed
103 104 105
			break;
		}
		case LinphoneEventLogTypeConferenceCreated: {
Benjamin REIS's avatar
Benjamin REIS committed
106
			eventString = [NSString stringWithFormat:NSLocalizedString(@"You have joined the group", nil)];
107 108
			break;
		}
DanmeiChen's avatar
DanmeiChen committed
109 110 111 112 113 114
        case LinphoneEventLogTypeConferenceSecurityEvent: {
            LinphoneSecurityEventType type = linphone_event_log_get_security_event_type(event);
            NSString *participant = [FastAddressBook displayNameForAddress:linphone_event_log_get_security_event_faulty_device_address(event)];
            switch (type) {
                case LinphoneSecurityEventTypeSecurityLevelDowngraded:
                    if (!participant)
DanmeiChen's avatar
DanmeiChen committed
115
                        eventString = [NSString stringWithFormat:NSLocalizedString(@"Security level decreased", nil)];
DanmeiChen's avatar
DanmeiChen committed
116
                    else
DanmeiChen's avatar
DanmeiChen committed
117
                        eventString = [NSString stringWithFormat:NSLocalizedString(@"Security level decreased because of %@", nil),participant];
DanmeiChen's avatar
DanmeiChen committed
118 119 120 121
                    eventColor = [UIColor grayColor];
                    break;
                case LinphoneSecurityEventTypeParticipantMaxDeviceCountExceeded:
                    if (!participant)
DanmeiChen's avatar
DanmeiChen committed
122
                        eventString = [NSString stringWithFormat:NSLocalizedString(@"Max participant count exceeded", nil)];
DanmeiChen's avatar
DanmeiChen committed
123
                    else
DanmeiChen's avatar
DanmeiChen committed
124
                        eventString = [NSString stringWithFormat:NSLocalizedString(@"Max participant count exceeded by %@", nil),participant];
DanmeiChen's avatar
DanmeiChen committed
125 126
                    eventColor = [UIColor redColor];
                    break;
DanmeiChen's avatar
DanmeiChen committed
127
                case LinphoneSecurityEventTypeEncryptionIdentityKeyChanged:
DanmeiChen's avatar
DanmeiChen committed
128 129 130 131 132 133
                    if (!participant)
                        eventString = [NSString stringWithFormat:NSLocalizedString(@"LIME identity key changed", nil)];
                    else
                        eventString = [NSString stringWithFormat:NSLocalizedString(@"LIME identity key changed for %@", nil),participant];
                    eventColor = [UIColor redColor];
                    break;
DanmeiChen's avatar
DanmeiChen committed
134 135
                case LinphoneSecurityEventTypeManInTheMiddleDetected:
                    if (!participant)
DanmeiChen's avatar
DanmeiChen committed
136
                        eventString = [NSString stringWithFormat:NSLocalizedString(@"Man-in-the-middle attack detected", nil)];
DanmeiChen's avatar
DanmeiChen committed
137
                    else
DanmeiChen's avatar
DanmeiChen committed
138
                        eventString = [NSString stringWithFormat:NSLocalizedString(@"Man-in-the-middle attack detected for %@", nil),participant];
DanmeiChen's avatar
DanmeiChen committed
139 140 141 142 143 144 145 146 147 148
                    eventColor = [UIColor redColor];
                    break;
                    
                case LinphoneSecurityEventTypeNone:
                default:
                    break;
            }
            
            break;
        }
Christophe Deschamps's avatar
Christophe Deschamps committed
149 150 151 152 153 154
			
		case LinphoneEventLogTypeConferenceEphemeralMessageDisabled: {
			eventString = [NSString stringWithFormat:NSLocalizedString(@"You disabled ephemeral messages", nil)];
			break;
		}
		case LinphoneEventLogTypeConferenceEphemeralMessageEnabled: {
155
			eventString = [NSString stringWithFormat:NSLocalizedString(@"You enabled ephemeral messages: %@", nil),[self formatEphemeralExpiration:linphone_event_log_get_ephemeral_message_lifetime(event)]];
Christophe Deschamps's avatar
Christophe Deschamps committed
156 157 158 159 160 161 162
			break;
		}
		case LinphoneEventLogTypeConferenceEphemeralMessageLifetimeChanged: {
			eventString = [NSString stringWithFormat:NSLocalizedString(@"Ephemeral messages expiry date: %@",nil),[self formatEphemeralExpiration:linphone_event_log_get_ephemeral_message_lifetime(event)]];
			break;
		}
			
163 164 165
		default:
			return;
	}
166
	_contactDateLabel.text = eventString;
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183

	CGSize newSize = [_contactDateLabel.text boundingRectWithSize:CGSizeZero
														  options:(NSStringDrawingUsesLineFragmentOrigin |
																   NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesFontLeading)
													   attributes:@{NSFontAttributeName :_contactDateLabel.font}
														  context:nil].size;
	float delta = (_contactDateLabel.frame.size.width - newSize.width) / 2;

	[_contactDateLabel setFrame:CGRectMake((_eventView.frame.size.width - newSize.width) / 2, 1, newSize.width, NOTIFIED_CELL_HEIGHT)];
	[_leftBar setFrame:CGRectMake(0,
								  _leftBar.frame.origin.y,
								  _contactDateLabel.frame.origin.x - 5,
								  1)];
	[_rightBar setFrame:CGRectMake(_contactDateLabel.frame.origin.x + newSize.width + 5,
								   _rightBar.frame.origin.y,
								   _rightBar.frame.size.width + delta,
								   1)];
184 185 186 187 188 189
}

- (void)layoutSubviews {
	[super layoutSubviews];
}

Christophe Deschamps's avatar
Christophe Deschamps committed
190 191 192 193 194 195 196 197 198 199 200 201 202
- (NSString *) formatEphemeralExpiration:(long)duration {
	switch (duration) {
		case 0:return NSLocalizedString(@"Disabled",nil);break;
		case 60:return NSLocalizedString(@"1 minute",nil);break;
		case 3600:return NSLocalizedString(@"1 hour",nil);break;
		case 86400:return NSLocalizedString(@"1 day",nil);break;
		case 259200:return NSLocalizedString(@"3 days",nil);break;
		case 604800L:return NSLocalizedString(@"1 week",nil);break;
	}
	return NSLocalizedString(@"Unexpected duration",nil);
}

 
203
@end