JLChen
2021-08-02 38f4fb064df09f344fc3237409c76a9fba2a8a9e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
 * Copyright (c) 2010-2020 Belledonne Communications SARL.
 *
 * 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/>.
 */
 
#import <Foundation/Foundation.h>
#import "UIChatNotifiedEventCell.h"
#import "LinphoneManager.h"
#import "PhoneMainView.h"
 
#import <AssetsLibrary/ALAsset.h>
#import <AssetsLibrary/ALAssetRepresentation.h>
 
@implementation UIChatNotifiedEventCell
 
#pragma mark - Class methods
static const CGFloat NOTIFIED_CELL_HEIGHT = 44;
 
+ (CGFloat)height {
    return NOTIFIED_CELL_HEIGHT;
}
 
#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];
        }
    }
    _event = NULL;
    return self;
}
 
- (void)dealloc {
    _event = NULL;
}
 
- (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)];
}
 
#pragma mark -
 
- (void)setEvent:(LinphoneEventLog *)event {
    _event = event;
    NSString *eventString;
    UIColor *eventColor = [UIColor grayColor];
    switch (linphone_event_log_get_type(event)) {
        case LinphoneEventLogTypeConferenceSubjectChanged: {
            NSString *subject = [NSString stringWithUTF8String:linphone_event_log_get_subject(event) ?: LINPHONE_DUMMY_SUBJECT];
            eventString = [NSString stringWithFormat:NSLocalizedString(@"New subject : %@", nil), subject];
            break;
        }
        case LinphoneEventLogTypeConferenceParticipantAdded: {
            NSString *participant = [FastAddressBook displayNameForAddress:linphone_event_log_get_participant_address(event)];
            eventString = [NSString stringWithFormat:NSLocalizedString(@"%@ has joined", nil), participant];
            break;
        }
        case LinphoneEventLogTypeConferenceParticipantRemoved: {
            NSString *participant = [FastAddressBook displayNameForAddress:linphone_event_log_get_participant_address(event)];
            eventString = [NSString stringWithFormat:NSLocalizedString(@"%@ has left", nil), participant];
            break;
        }
        case LinphoneEventLogTypeConferenceParticipantSetAdmin: {
            NSString *participant = [FastAddressBook displayNameForAddress:linphone_event_log_get_participant_address(event)];
            eventString = [NSString stringWithFormat:NSLocalizedString(@"%@ is now an admin", nil), participant];
            break;
        }
        case LinphoneEventLogTypeConferenceParticipantUnsetAdmin: {
            NSString *participant = [FastAddressBook displayNameForAddress:linphone_event_log_get_participant_address(event)];
            eventString = [NSString stringWithFormat:NSLocalizedString(@"%@ is no longer an admin", nil), participant];
            break;
        }
        case LinphoneEventLogTypeConferenceTerminated: {
            eventString = [NSString stringWithFormat:NSLocalizedString(@"You have left the group", nil)];
            break;
        }
        case LinphoneEventLogTypeConferenceCreated: {
            eventString = [NSString stringWithFormat:NSLocalizedString(@"You have joined the group", nil)];
            break;
        }
        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)
                        eventString = [NSString stringWithFormat:NSLocalizedString(@"Security level decreased", nil)];
                    else
                        eventString = [NSString stringWithFormat:NSLocalizedString(@"Security level decreased because of %@", nil),participant];
                    eventColor = [UIColor grayColor];
                    break;
                case LinphoneSecurityEventTypeParticipantMaxDeviceCountExceeded:
                    if (!participant)
                        eventString = [NSString stringWithFormat:NSLocalizedString(@"Max participant count exceeded", nil)];
                    else
                        eventString = [NSString stringWithFormat:NSLocalizedString(@"Max participant count exceeded by %@", nil),participant];
                    eventColor = [UIColor redColor];
                    break;
                case LinphoneSecurityEventTypeEncryptionIdentityKeyChanged:
                    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;
                case LinphoneSecurityEventTypeManInTheMiddleDetected:
                    if (!participant)
                        eventString = [NSString stringWithFormat:NSLocalizedString(@"Man-in-the-middle attack detected", nil)];
                    else
                        eventString = [NSString stringWithFormat:NSLocalizedString(@"Man-in-the-middle attack detected for %@", nil),participant];
                    eventColor = [UIColor redColor];
                    break;
                    
                case LinphoneSecurityEventTypeNone:
                default:
                    break;
            }
            
            break;
        }
            
        case LinphoneEventLogTypeConferenceEphemeralMessageDisabled: {
            eventString = [NSString stringWithFormat:NSLocalizedString(@"You disabled ephemeral messages", nil)];
            break;
        }
        case LinphoneEventLogTypeConferenceEphemeralMessageEnabled: {
            eventString = [NSString stringWithFormat:NSLocalizedString(@"You enabled ephemeral messages: %@", nil),[self formatEphemeralExpiration:linphone_event_log_get_ephemeral_message_lifetime(event)]];
            break;
        }
        case LinphoneEventLogTypeConferenceEphemeralMessageLifetimeChanged: {
            eventString = [NSString stringWithFormat:NSLocalizedString(@"Ephemeral messages expiry date: %@",nil),[self formatEphemeralExpiration:linphone_event_log_get_ephemeral_message_lifetime(event)]];
            break;
        }
            
        default:
            return;
    }
    _contactDateLabel.text = eventString;
 
    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)];
}
 
- (void)layoutSubviews {
    [super layoutSubviews];
}
 
- (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);
}
 
 
@end