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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
/*
 * 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 <Photos/PHAssetChangeRequest.h>
 
#import "ChatConversationView.h"
#import "PhoneMainView.h"
#import "Utils.h"
#import "FileTransferDelegate.h"
#import "UIChatBubbleTextCell.h"
#import "DevicesListView.h"
#import "SVProgressHUD.h"
#import "EphemeralSettingsView.h"
#import "Utils.h"
 
@implementation FileContext
 
- (void)addObject:(UIImage *)image withQuality:(float)quality {
    NSString *name = [NSString stringWithFormat:@"%li-%f.jpg", (long)image.hash, [NSDate timeIntervalSinceReferenceDate]];
    NSData *data = UIImageJPEGRepresentation(image, quality);
 
    [self addObject:data name:name type:@"image" image:image];
}
 
- (void)addObject:(NSData *)data name:(NSString *)name type:(NSString *)type image:(UIImage *)image {
    [_previewsArray addObject:image];
    [_uuidsArray addObject:[NSUUID UUID]];
    [self addObject:data name:name type:type];
}
 
- (void)addObject:(NSData *)data name:(NSString *)name type:(NSString *)type {
    [_namesArray addObject:name];
    [_typesArray addObject:type];
    [_datasArray addObject:data];
}
 
- (void)deleteContentWithUuid:(NSUUID *)uuid {
    NSUInteger key = [_uuidsArray indexOfObject:uuid];
    [_previewsArray removeObjectAtIndex:key];
    [_uuidsArray removeObjectAtIndex:key];
    [_namesArray removeObjectAtIndex:key];
    [_typesArray removeObjectAtIndex:key];
    [_datasArray removeObjectAtIndex:key];
}
 
- (void)clear {
    _previewsArray = [NSMutableArray array];
    _uuidsArray = [NSMutableArray array];
    _namesArray = [NSMutableArray array];
    _typesArray = [NSMutableArray array];
    _datasArray = [NSMutableArray array];
}
 
- (NSUInteger)count {
    return [_datasArray count];
}
 
@end
 
 
@implementation PreviewItem
- (instancetype)initPreviewURL:(NSURL *)docURL
                     WithTitle:(NSString *)title {
    self = [super init];
    if (self) {
        _previewItemURL = [docURL copy];
        _previewItemTitle = [title copy];
    }
    return self;
}
@end
 
@implementation FileDataSource
- (instancetype)initWithFiles:(NSMutableArray<NSURL*>*)files {
    self = [super init];
    if (self) {
        _files = files;
    }
    return self;
}
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {
    return _files.count;
}
- (id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {
    NSURL *url = [_files objectAtIndex:index];
    return [[PreviewItem alloc] initPreviewURL:url WithTitle:[url lastPathComponent]];
}
@end
 
@implementation ChatConversationView
 
#pragma mark - Lifecycle Functions
 
- (id)init {
    self = [super initWithNibName:NSStringFromClass(self.class) bundle:[NSBundle mainBundle]];
    if (self != nil) {
        scrollOnGrowingEnabled = TRUE;
        _chatRoom = NULL;
        _chatRoomCbs = NULL;
        securityDialog = NULL;
        isOneToOne = TRUE;
        imageQualities = [[OrderedDictionary alloc]
            initWithObjectsAndKeys:[NSNumber numberWithFloat:0.9], NSLocalizedString(@"Maximum", nil),
                                   [NSNumber numberWithFloat:0.5], NSLocalizedString(@"Average", nil),
                                   [NSNumber numberWithFloat:0.0], NSLocalizedString(@"Minimum", nil), nil];
        composingVisible = false;
    }
    return self;
}
 
- (void)dealloc {
    [NSNotificationCenter.defaultCenter removeObserver:self];
}
 
#pragma mark - UICompositeViewDelegate Functions
 
static UICompositeViewDescription *compositeDescription = nil;
 
+ (UICompositeViewDescription *)compositeViewDescription {
    if (compositeDescription == nil) {
        compositeDescription = [[UICompositeViewDescription alloc] init:self.class
                                                              statusBar:StatusBarView.class
                                                                 tabBar:TabBarView.class
                                                               sideMenu:SideMenuView.class
                                                             fullscreen:false
                                                         isLeftFragment:NO
                                                           fragmentWith:ChatsListView.class];
    }
    return compositeDescription;
}
 
- (UICompositeViewDescription *)compositeViewDescription {
    return self.class.compositeViewDescription;
}
 
 
+ (void)markAsRead:(LinphoneChatRoom *)chatRoom {
    if (!chatRoom)
        return;
 
    linphone_chat_room_mark_as_read(chatRoom);
    if (IPAD) {
        ChatsListView *listView = VIEW(ChatsListView);
        [listView.tableController markCellAsRead:chatRoom];
    }
    [PhoneMainView.instance updateApplicationBadgeNumber];
}
 
#pragma mark - ViewController Functions
 
- (void)viewDidLoad {
    [super viewDidLoad];
    _markAsRead = TRUE;
    // if we use fragments, remove back button
    if (IPAD) {
        _backButton.hidden = YES;
        _backButton.alpha = 0;
    }
    
    refreshControl = [[UIRefreshControl alloc]init];
    [refreshControl addTarget:self action:@selector(refreshData) forControlEvents:UIControlEventValueChanged];
    _tableController.refreshControl = refreshControl;
    
    _messageField.minNumberOfLines = 1;
    _messageField.maxNumberOfLines = IPAD ? 10 : 3;
    _messageField.delegate = self;
    _messageField.font = [UIFont systemFontOfSize:18.0f];
    _messageField.contentInset = UIEdgeInsetsMake(-15, 0, 0, 0);
    //    _messageField.internalTextView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 0, 10);
    [_tableController setChatRoomDelegate:self];
    [_imagesCollectionView registerClass:[UIImageViewDeletable class] forCellWithReuseIdentifier:NSStringFromClass([UIImageViewDeletable class])];
    [_imagesCollectionView setDataSource:self];
    [_toggleSelectionButton setImage:[UIImage imageNamed:@"select_all_default.png"] forState:UIControlStateSelected];
}
 
- (void)refreshData {
    [_tableController refreshData];
    [refreshControl endRefreshing];
    if (_tableController.totalNumberOfItems == 0)
        return;
    [_tableController loadData];
    [_tableController.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:_tableController.currentIndex inSection:0]
                          atScrollPosition:UITableViewScrollPositionTop
                                  animated:false];
}
 
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [NSNotificationCenter.defaultCenter addObserver:self
                                           selector:@selector(keyboardWillShow:)
                                               name:UIKeyboardWillShowNotification
                                             object:nil];
    [NSNotificationCenter.defaultCenter addObserver:self
                                           selector:@selector(keyboardWillHide:)
                                               name:UIKeyboardWillHideNotification
                                             object:nil];
    [NSNotificationCenter.defaultCenter addObserver:self
                                           selector:@selector(onMessageChange:)
                                               name:UITextViewTextDidChangeNotification
                                             object:nil];
    [NSNotificationCenter.defaultCenter addObserver:self
                                           selector:@selector(callUpdateEvent:)
                                               name:kLinphoneCallUpdate
                                             object:nil];
    [NSNotificationCenter.defaultCenter addObserver:self
                                           selector:@selector(onLinphoneCoreReady:)
                                               name:kLinphoneGlobalStateUpdate
                                             object:nil];
    if ([_fileContext count] > 0) {
        [UIView animateWithDuration:0
                              delay:0
                            options:UIViewAnimationOptionBeginFromCurrentState
                         animations:^{
                             // resizing imagesView
                             CGRect imagesFrame = [_imagesView frame];
                             imagesFrame.origin.y = [_messageView frame].origin.y - 100;
                             imagesFrame.size.height = 100;
                             [_imagesView setFrame:imagesFrame];
                             // resizing chatTable
                             CGRect tableViewFrame = [_tableController.tableView frame];
                             tableViewFrame.size.height -= 100;
                             [_tableController.tableView setFrame:tableViewFrame];
                         }
                         completion:nil];
    }
    [self configureForRoom:self.editing];
    
    // Resize the popup table depending on wether ephemeral messages are enabled or not.
    CGRect popupFrame = _popupMenu.frame;
    popupFrame.size.height = 44 * [_popupMenu numberOfRowsInSection:0];
    _popupMenu.frame = popupFrame;
 
}
 
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
 
    [self removeCallBacks];
 
    [_messageField resignFirstResponder];
 
    [self setComposingVisible:false withDelay:0]; // will hide the "user is composing.." message
 
    [NSNotificationCenter.defaultCenter removeObserver:self];
    PhoneMainView.instance.currentRoom = NULL;
}
 
- (void)removeCallBacks {
    if (_chatRoom && _chatRoomCbs) {
        linphone_chat_room_remove_callbacks(_chatRoom, _chatRoomCbs);
        _chatRoomCbs = NULL;
    }
}
 
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
    if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground) {
        return;
    }
    composingVisible = !composingVisible;
    [self setComposingVisible:!composingVisible withDelay:0];
 
    // force offset recomputing
    [_messageField refreshHeight];
    LinphoneAddress *peerAddr = linphone_core_create_address([LinphoneManager getLc], _peerAddress);
    if (peerAddr) {
        _chatRoom = linphone_core_get_chat_room([LinphoneManager getLc], peerAddr);
        isOneToOne = linphone_chat_room_get_capabilities(_chatRoom) & LinphoneChatRoomCapabilitiesOneToOne;
    }
    [self configureForRoom:true];
    _backButton.hidden = _tableController.isEditing;
    [_tableController scrollToBottom:true];
    [self refreshImageDrawer];
}
 
#pragma mark -
 
- (void)configureForRoom:(BOOL)editing {
    if (!_chatRoom) {
        _chatView.hidden = YES;
        return;
    }
 
    if (!_chatRoomCbs) {
        _chatRoomCbs = linphone_factory_create_chat_room_cbs(linphone_factory_get());
        linphone_chat_room_cbs_set_state_changed(_chatRoomCbs, on_chat_room_state_changed);
        linphone_chat_room_cbs_set_subject_changed(_chatRoomCbs, on_chat_room_subject_changed);
        linphone_chat_room_cbs_set_participant_added(_chatRoomCbs, on_chat_room_participant_added);
        linphone_chat_room_cbs_set_participant_removed(_chatRoomCbs, on_chat_room_participant_removed);
        linphone_chat_room_cbs_set_participant_admin_status_changed(_chatRoomCbs, on_chat_room_participant_admin_status_changed);
        linphone_chat_room_cbs_set_chat_message_received(_chatRoomCbs, on_chat_room_chat_message_received);
        linphone_chat_room_cbs_set_chat_message_sending(_chatRoomCbs, on_chat_room_chat_message_sending);
        linphone_chat_room_cbs_set_is_composing_received(_chatRoomCbs, on_chat_room_is_composing_received);
        linphone_chat_room_cbs_set_conference_joined(_chatRoomCbs, on_chat_room_conference_joined);
        linphone_chat_room_cbs_set_conference_left(_chatRoomCbs, on_chat_room_conference_left);
         linphone_chat_room_cbs_set_security_event(_chatRoomCbs, on_chat_room_conference_alert);
        linphone_chat_room_cbs_set_user_data(_chatRoomCbs, (__bridge void*)self);
        linphone_chat_room_add_callbacks(_chatRoom, _chatRoomCbs);
    }
 
    [self updateSuperposedButtons];
 
    if (_tableController.isEditing)
        [_tableController setEditing:editing];
 
    BOOL fileSharingEnabled = linphone_core_get_file_transfer_server(LC) != NULL;
    [_pictureButton setEnabled:fileSharingEnabled];
 
    [self callUpdateEvent:nil];
    PhoneMainView.instance.currentRoom = _chatRoom;
    if (isOneToOne) {
        bctbx_list_t *participants = linphone_chat_room_get_participants(_chatRoom);
        LinphoneParticipant *firstParticipant = participants ? (LinphoneParticipant *)participants->data : NULL;
        const LinphoneAddress *addr = firstParticipant ? linphone_participant_get_address(firstParticipant) : linphone_chat_room_get_peer_address(_chatRoom);
        [ContactDisplay setDisplayNameLabel:_addressLabel forAddress:addr];
    } else
        _addressLabel.text = [NSString stringWithUTF8String:linphone_chat_room_get_subject(_chatRoom) ?: LINPHONE_DUMMY_SUBJECT];
 
    [self updateParticipantLabel];
    [self configureMessageField];
    [_tableController setChatRoom:_chatRoom];
 
    _chatView.hidden = NO;
    UIImage *image = [FastAddressBook imageForSecurityLevel:linphone_chat_room_get_security_level(_chatRoom)];
    [_encryptedButton setImage:image forState:UIControlStateNormal];
    _encryptedButton.hidden = image ? FALSE : TRUE;
    [self update];
    [self shareFile];
    
    if (![self isBasicChatRoom]) {
        [self setupPopupMenu];
        _ephemeralndicator.hidden = !linphone_chat_room_ephemeral_enabled(_chatRoom);
    }
 
}
 
-(BOOL) isBasicChatRoom {
    if (!_chatRoom)
        return true;
    LinphoneChatRoomCapabilitiesMask capabilities = linphone_chat_room_get_capabilities(_chatRoom);
    return capabilities & LinphoneChatRoomCapabilitiesBasic;
}
 
 
- (void)configureMessageField {
    if (isOneToOne) {
        _messageField.editable = TRUE;
        _pictureButton.enabled = TRUE;
        _messageView.userInteractionEnabled = TRUE;
        if (linphone_chat_room_has_been_left(_chatRoom)) {
            linphone_chat_room_add_participant(_chatRoom, linphone_participant_get_address(linphone_chat_room_get_me(_chatRoom)));
        }
    } else {
        _messageField.editable = !linphone_chat_room_has_been_left(_chatRoom);
        _pictureButton.enabled = !linphone_chat_room_has_been_left(_chatRoom);
        _messageView.userInteractionEnabled = !linphone_chat_room_has_been_left(_chatRoom);
    }
}
 
-(NSData *) nsDataRead {
    NSString* groupName = [NSString stringWithFormat:@"group.%@.linphoneExtension",[[NSBundle mainBundle] bundleIdentifier]];
    NSString *path  =[[[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:groupName] path];
    NSString *fullCacheFilePathPath = [NSString stringWithFormat:@"%@/%@",path,@"nsData"];
    return[NSData dataWithContentsOfFile:fullCacheFilePathPath];
}
 
 
- (void)shareFile {
    NSString* groupName = [NSString stringWithFormat:@"group.%@.linphoneExtension",[[NSBundle mainBundle] bundleIdentifier]];
 
    NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:groupName];
    NSDictionary *dict = [defaults valueForKey:@"photoData"];
    NSDictionary *dictFile = [defaults valueForKey:@"icloudData"];
    NSDictionary *dictUrl = [defaults valueForKey:@"url"];
    if (dict) {
        //file shared from photo lib
        NSString *fileName = dict[@"url"];
        [_messageField setText:dict[@"message"]];
        [self confirmShare:[self nsDataRead] url:nil fileName:fileName];
        [defaults removeObjectForKey:@"photoData"];
    } else if (dictFile) {
        NSString *fileName = dictFile[@"url"];
        [_messageField setText:dictFile[@"message"]];
        [self confirmShare:[self nsDataRead] url:nil fileName:fileName];
        [defaults removeObjectForKey:@"icloudData"];
    } else if (dictUrl) {
        NSString *url = dictUrl[@"url"];
        [_messageField setText:dictUrl[@"message"]];
        [self confirmShare:nil url:url fileName:nil];
        [defaults removeObjectForKey:@"url"];
    }
}
 
// reload the chatroom after the core starts
- (void)onLinphoneCoreReady:(NSNotification *)notif {
    if ((LinphoneGlobalState)[[[notif userInfo] valueForKey:@"state"] integerValue] == LinphoneGlobalOn) {
        LinphoneAddress *peerAddr = linphone_core_create_address([LinphoneManager getLc], _peerAddress);
        if (peerAddr) {
            _chatRoom = linphone_core_get_chat_room([LinphoneManager getLc], peerAddr);
            isOneToOne = linphone_chat_room_get_capabilities(_chatRoom) & LinphoneChatRoomCapabilitiesOneToOne;
        }
        [self configureForRoom:self.editing];
        if (_chatRoom && _markAsRead) {
            if (IPAD) {
                [VIEW(ChatsListView).tableController loadData];
            }
 
            [ChatConversationView markAsRead:_chatRoom];
        }
        _markAsRead = TRUE;
    }
}
 
- (void)callUpdateEvent:(NSNotification *)notif {
    [self updateSuperposedButtons];
}
 
- (void)update {
    if (_chatRoom == NULL) {
        LOGW(@"Cannot update chat room header: null contact");
        return;
    }
 
    const LinphoneAddress *addr = linphone_chat_room_get_peer_address(_chatRoom);
    if (addr == NULL) {
        [PhoneMainView.instance popCurrentView];
        UIAlertController *errView = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Invalid SIP address", nil)
                                                                         message:NSLocalizedString(@"Either configure a SIP proxy server from settings prior to send a "
                                                                                                   @"message or use a valid SIP address (I.E sip:john@example.net)",
                                                                                                   nil)
                                                                  preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Continue", nil)
                                                                style:UIAlertActionStyleDefault
                                                              handler:^(UIAlertAction * action) {}];
        
        [errView addAction:defaultAction];
        [self presentViewController:errView animated:YES completion:nil];
        return;
    }
}
 
- (BOOL)sendMessage:(NSString *)message withExterlBodyUrl:(NSURL *)externalUrl {
    if (_chatRoom == NULL) {
        LOGW(@"Cannot send message: No chatroom");
        return FALSE;
    }
 
    LinphoneChatMessage *msg = linphone_chat_room_create_message(_chatRoom, [message UTF8String]);
    if (externalUrl) {
        linphone_chat_message_set_external_body_url(msg, [[externalUrl absoluteString] UTF8String]);
    }
 
    // we must ref & unref message because in case of error, it will be destroy otherwise
    linphone_chat_message_send(msg);
 
    return TRUE;
}
 
- (void)saveAndSend:(UIImage *)image assetId:(NSString *)phAssetId withQuality:(float)quality{
    [_fileContext addObject:image withQuality:quality];
    [_qualitySettingsArray addObject:@(quality)];
    [self refreshImageDrawer];
}
 
- (void)chooseImageQuality:(UIImage *)image assetId:(NSString *)phAssetId {
    [SVProgressHUD show];
    NSMutableDictionary *optionsBlock = [[NSMutableDictionary alloc] init];
    NSMutableDictionary *optionsText = [[NSMutableDictionary alloc] init];
    DTActionSheet *sheet = [[DTActionSheet alloc] initWithTitle:NSLocalizedString(@"Choose the image size", nil)];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
      for (NSString *key in [imageQualities allKeys]) {
          NSNumber *quality = [imageQualities objectForKey:key];
          NSData *data = UIImageJPEGRepresentation(image, [quality floatValue]);
          NSNumber *size = [NSNumber numberWithInteger:[data length]];
          NSString *text = [NSString stringWithFormat:@"%@ (%@)", key, [size toHumanReadableSize]];
          [optionsBlock setObject:^() {
              [self saveAndSend:image assetId:phAssetId withQuality:[quality floatValue]];
          } forKey:key];
          [optionsText setObject:text forKey:key];
      }
      dispatch_async(dispatch_get_main_queue(), ^{
          for (NSString *key in [imageQualities allKeys]) {
              [sheet addButtonWithTitle:[optionsText objectForKey:key] block:[optionsBlock objectForKey:key]];
          }
        [sheet addCancelButtonWithTitle:NSLocalizedString(@"Cancel", nil) block:nil];
        [SVProgressHUD dismiss];
        [sheet showInView:PhoneMainView.instance.view];
      });
    });
}
 
- (void)confirmShare:(NSData *)data url:(NSString *)url fileName:(NSString *)fileName {
    DTActionSheet *sheet = [[DTActionSheet alloc] initWithTitle:@""];
    dispatch_async(dispatch_get_main_queue(), ^{
        [sheet addButtonWithTitle:NSLocalizedString(@"Send to this friend", nil)
                            block:^() {
                                if (![[self.messageField text] isEqualToString:@""]) {
                                    [self sendMessageInMessageField];
                                }
                                if (url)
                                    [self sendMessage:url withExterlBodyUrl:nil];
                                else
                                    [self startFileUpload:data withName:fileName];
        }];
     
        [sheet addCancelButtonWithTitle:NSLocalizedString(@"Cancel", nil) block:nil];
        [sheet showInView:PhoneMainView.instance.view];
    });
}
 
- (void)setComposingVisible:(BOOL)visible withDelay:(CGFloat)delay {
    Boolean shouldAnimate = composingVisible != visible;
    CGRect keyboardFrame = [_messageView frame];
    CGRect newComposingFrame = [_composeIndicatorView frame];
    CGRect newTableFrame = [_tableController.tableView frame];
 
    if (visible) {
        // pull up the composing frame and shrink the table view
        newTableFrame.size.height -= newComposingFrame.size.height;
        newComposingFrame.origin.y = keyboardFrame.origin.y - newComposingFrame.size.height;
        const bctbx_list_t *addresses = linphone_chat_room_get_composing_addresses(_chatRoom);
        NSString *composingAddresses = @"";
        if (bctbx_list_size(addresses) == 1) {
            composingAddresses = [FastAddressBook displayNameForAddress:(LinphoneAddress *)addresses->data];
            _composeLabel.text = [NSString stringWithFormat:NSLocalizedString(@"%@ is writing...", nil), composingAddresses];
        } else {
            while (addresses) {
                if (![composingAddresses isEqualToString:@""])
                    composingAddresses = [composingAddresses stringByAppendingString:@", "];
                composingAddresses = [composingAddresses stringByAppendingString:[FastAddressBook displayNameForAddress:(LinphoneAddress *)addresses->data]];
                addresses = addresses->next;
            }
            _composeLabel.text = [NSString stringWithFormat:NSLocalizedString(@"%@ are writing...", nil), composingAddresses];
        }
    } else {
        // pull down the composing frame and widen the tableview
        newTableFrame.size.height += newComposingFrame.size.height;
        newComposingFrame.origin.y = keyboardFrame.origin.y;
    }
    composingVisible = visible;
    if (!shouldAnimate)
        return;
 
    [UIView animateWithDuration:delay
                     animations:^{
                         _tableController.tableView.frame = newTableFrame;
                         _composeIndicatorView.frame = newComposingFrame;
                     }
                     completion:^(BOOL finished) {
                         [_tableController scrollToBottom:TRUE];
                         _composeIndicatorView.hidden = !visible;
                     }];
}
 
- (void)updateSuperposedButtons {
    [_backToCallButton update];
    _infoButton.hidden = (isOneToOne|| !_backToCallButton.hidden || _tableController.tableView.isEditing);
    _callButton.hidden = !_backToCallButton.hidden || !_infoButton.hidden || _tableController.tableView.isEditing;
    _toggleMenuButton.hidden =  [self isBasicChatRoom] || _tableController.tableView.isEditing;
    _tableController.editButton.hidden = _tableController.editButton.hidden || ![self isBasicChatRoom];
}
 
- (void)updateParticipantLabel {
    CGRect frame = _addressLabel.frame;
    if (isOneToOne) {
        _particpantsLabel.hidden = TRUE;
        frame.origin.y = (_topBar.frame.size.height - _addressLabel.frame.size.height)/2;
    } else {
        _particpantsLabel.hidden = FALSE;
        bctbx_list_t *participants = linphone_chat_room_get_participants(_chatRoom);
        _particpantsLabel.text = @"";
        while (participants) {
            LinphoneParticipant *participant = (LinphoneParticipant *)participants->data;
            if (![_particpantsLabel.text isEqualToString:@""])
                _particpantsLabel.text = [_particpantsLabel.text stringByAppendingString:@", "];
 
            _particpantsLabel.text = [_particpantsLabel.text stringByAppendingString:
                                      [FastAddressBook displayNameForAddress:linphone_participant_get_address(participant)]];
            participants = participants->next;
        }
        frame.origin.y = 0;
    }
    _addressLabel.frame = frame;
}
 
- (void)sendMessageInMessageField {
    if ([self sendMessage:[_messageField text] withExterlBodyUrl:nil]) {
        scrollOnGrowingEnabled = FALSE;
        [_messageField setText:@""];
        scrollOnGrowingEnabled = TRUE;
        [self onMessageChange:nil];
    }
}
 
#pragma mark - UITextFieldDelegate Functions
 
- (BOOL)growingTextViewShouldBeginEditing:(HPGrowingTextView *)growingTextView {
    if (_tableController.isEditing) {
        [_tableController setEditing:NO];
    }
    [_listTapGestureRecognizer setEnabled:TRUE];
    return TRUE;
}
 
- (BOOL)growingTextViewShouldEndEditing:(HPGrowingTextView *)growingTextView {
    [_listTapGestureRecognizer setEnabled:FALSE];
    return TRUE;
}
 
- (void)growingTextChanged:(HPGrowingTextView *)growingTextView text:(NSString *)text {
    if ([text length] > 0 && _chatRoom)
        linphone_chat_room_compose(_chatRoom);
}
 
- (void)growingTextView:(HPGrowingTextView *)growingTextView willChangeHeight:(float)height {
    int diff = height - growingTextView.bounds.size.height;
 
    if (diff != 0) {
        CGRect messageRect = [_messageView frame];
        messageRect.origin.y -= diff;
        messageRect.size.height += diff;
        [_messageView setFrame:messageRect];
 
        if ([_fileContext count] > 0) {
            CGRect _imagesRect = [_imagesView frame];
            _imagesRect.origin.y -= diff;
            [_imagesView setFrame:_imagesRect];
        }
        
        // Always stay at bottom
        if (scrollOnGrowingEnabled) {
            CGRect tableFrame = [_tableController.view frame];
            CGPoint contentPt = [_tableController.tableView contentOffset];
            contentPt.y += diff;
            if (contentPt.y + tableFrame.size.height > _tableController.tableView.contentSize.height)
                contentPt.y += diff;
            [_tableController.tableView setContentOffset:contentPt animated:FALSE];
        }
 
        CGRect tableRect = [_tableController.view frame];
        tableRect.size.height -= diff;
        [_tableController.view setFrame:tableRect];
 
        // if we're showing the compose message, update it position
        if (![_composeLabel isHidden]) {
            CGRect frame = [_composeLabel frame];
            frame.origin.y -= diff;
            [_composeLabel setFrame:frame];
        }
    }
}
 
#pragma mark - Action Functions
 
- (IBAction)onBackClick:(id)event {
    [_tableController setChatRoom:NULL];
    [PhoneMainView.instance popToView:ChatsListView.compositeViewDescription];
}
 
- (IBAction)onEditClick:(id)event {
    [_tableController setEditing:![_tableController isEditing] animated:TRUE];
    [_messageField resignFirstResponder];
    [self updateSuperposedButtons];
}
 
- (IBAction)onSendClick:(id)event {
    if ([_fileContext count] > 0) {
        if (linphone_chat_room_get_capabilities(_chatRoom) & LinphoneChatRoomCapabilitiesConference) {
            [self startMultiFilesUpload];
        } else {
            int i = 0;
            for (i = 0; i < [_fileContext count]-1; ++i) {
                [self startUploadData:[_fileContext.datasArray objectAtIndex:i] withType:[_fileContext.typesArray objectAtIndex:i] withName:[_fileContext.namesArray objectAtIndex:i] andMessage:NULL];
            }
            if (isOneToOne) {
                [self startUploadData:[_fileContext.datasArray objectAtIndex:i] withType:[_fileContext.typesArray objectAtIndex:i] withName:[_fileContext.namesArray objectAtIndex:i] andMessage:NULL];
                if (![[self.messageField text] isEqualToString:@""]) {
                    [self sendMessage:[_messageField text] withExterlBodyUrl:nil];
                }
            } else {
                [self startUploadData:[_fileContext.datasArray objectAtIndex:i] withType:[_fileContext.typesArray objectAtIndex:i] withName:[_fileContext.namesArray objectAtIndex:i] andMessage:[self.messageField text]];
            }
        }
 
        [self clearMessageView];
        return;
    }
    [self sendMessageInMessageField];
}
 
- (IBAction)onListTap:(id)sender {
    [_messageField resignFirstResponder];
}
 
- (IBAction)onDeleteClick:(id)sender {
    LOGI(@"onDeleteClick");
    NSString *msg = [NSString stringWithFormat:NSLocalizedString(@"Do you want to delete the selected messages?", nil)];
    [UIConfirmationDialog ShowWithMessage:msg
        cancelMessage:nil
        confirmMessage:nil
        onCancelClick:^() {
          [self onEditionChangeClick:nil];
        }
        onConfirmationClick:^() {
          [_tableController removeSelectionUsing:nil];
          [_tableController loadData];
          [self onEditionChangeClick:nil];
        }];
}
 
- (IBAction)onEditionChangeClick:(id)sender {
    _backButton.hidden = _tableController.isEditing;
    [self updateSuperposedButtons];
}
 
- (IBAction)onEncryptedDevicesClick:(id)sender {
    NSString *message = NSLocalizedString(@"Instant messages are end-to-end encrypted in secured conversations. It is possible to upgrade the security level of a conversation by authenticating participants. To do so, call the contact and follow the authentification process.",nil);
    BOOL notAskAgain = [LinphoneManager.instance lpConfigBoolForKey:@"confirmation_dialog_before_sas_call_not_ask_again"];
 
    if (notAskAgain) {
        [self goToDeviceListView];
    } else {
        securityDialog = [UIConfirmationDialog ShowWithMessage:message cancelMessage:NSLocalizedString(@"CANCEL", nil) confirmMessage:NSLocalizedString(@"OK", nil) onCancelClick:^() {
        } onConfirmationClick:^() {
            [self goToDeviceListView];
        }];
        [_messageField resignFirstResponder];
        securityDialog.authView.hidden = FALSE;
        [securityDialog setSpecialColor];
    }
}
 
- (IBAction)onCallClick:(id)sender {
    bctbx_list_t *participants = linphone_chat_room_get_participants(_chatRoom);
    LinphoneParticipant *firstParticipant = participants ? (LinphoneParticipant *)participants->data : NULL;
    const LinphoneAddress *addr = firstParticipant ? linphone_participant_get_address(firstParticipant) : linphone_chat_room_get_peer_address(_chatRoom);
    [LinphoneManager.instance call:addr];
}
 
- (IBAction)onListSwipe:(id)sender {
    [self onBackClick:sender];
}
 
- (IBAction)onMessageChange:(id)sender {
    if ([[_messageField text] length] > 0) {
        [_sendButton setEnabled:TRUE];
    } else {
        [_sendButton setEnabled:FALSE];
    }
}
 
- (IBAction)onPictureClick:(id)event {
    [_messageField resignFirstResponder];
    [ImagePickerView SelectImageFromDevice:self atPosition:_pictureButton inView:self.view withDocumentMenuDelegate:self];
 
}
 
- (IBAction)onInfoClick:(id)sender {
    NSMutableArray *contactsArray = [[NSMutableArray alloc] init];
    NSMutableArray *admins = [[NSMutableArray alloc] init];
    bctbx_list_t *participants = linphone_chat_room_get_participants(_chatRoom);
    while (participants) {
        LinphoneParticipant *participant = (LinphoneParticipant *)participants->data;
        char *curi = linphone_address_as_string_uri_only(linphone_participant_get_address(participant));
        NSString *uri = [NSString stringWithUTF8String:curi];
        [contactsArray addObject:uri];
 
        if(linphone_participant_is_admin(participant))
           [admins addObject:uri];
        participants = participants->next;
        ms_free(curi);
    }
    ChatConversationInfoView *view = VIEW(ChatConversationInfoView);
    view.create = FALSE;
    view.contacts = [contactsArray mutableCopy];
    view.oldContacts = [contactsArray mutableCopy];
    view.admins = [admins mutableCopy];
    view.oldAdmins = [admins mutableCopy];
    view.oldSubject = [NSString stringWithUTF8String:linphone_chat_room_get_subject(_chatRoom) ?: LINPHONE_DUMMY_SUBJECT];
    view.room = _chatRoom;
    [PhoneMainView.instance changeCurrentView:view.compositeViewDescription];
}
 
#pragma mark ChatRoomDelegate
 
- (BOOL)startMultiFilesUpload {
    FileTransferDelegate *fileTransfer = [[FileTransferDelegate alloc] init];
    [fileTransfer setText:[self.messageField text]];
    [fileTransfer uploadFileContent:_fileContext forChatRoom:_chatRoom];
    [_tableController scrollToBottom:true];
    return TRUE;
}
 
- (BOOL)startUploadData:(NSData *)data withType:(NSString*)type withName:(NSString *)name andMessage:(NSString *)message {
    FileTransferDelegate *fileTransfer = [[FileTransferDelegate alloc] init];
    if (message)
        [fileTransfer setText:message];
    NSString *key = @"localfile";
    if ([type isEqualToString:@"video"]) {
        key = @"localvideo";
    } else if ([type isEqualToString:@"image"]) {
        key = @"localimage";
    }
    [fileTransfer uploadData:data forChatRoom:_chatRoom type:type subtype:type name:name key:key];
    [_tableController scrollToBottom:true];
    return TRUE;
}
 
- (BOOL)startFileUpload:(NSData *)data withName:(NSString *)name  {
    FileTransferDelegate *fileTransfer = [[FileTransferDelegate alloc] init];
    [fileTransfer uploadFile:data forChatRoom:_chatRoom withName:name];
    [_tableController scrollToBottom:true];
    return TRUE;
}
 
- (BOOL)resendMultiFiles:(FileContext *)newFileContext message:(NSString *)message {
    FileTransferDelegate *fileTransfer = [[FileTransferDelegate alloc] init];
    if (message)
        [fileTransfer setText:message];
    [fileTransfer uploadFileContent:newFileContext forChatRoom:_chatRoom];
    [_tableController scrollToBottom:true];
    return TRUE;
}
 
- (BOOL)resendFile: (NSData *)data withName:(NSString *)name type:(NSString *)type key:(NSString *)key message:(NSString *)message {
    FileTransferDelegate *fileTransfer = [[FileTransferDelegate alloc] init];
    if (message)
        [fileTransfer setText:message];
    [fileTransfer uploadData:data forChatRoom:_chatRoom type:type subtype:type name:name key:key];
    [_tableController scrollToBottom:true];
    return TRUE;
}
 
- (void)resendChat:(NSString *)message withExternalUrl:(NSString *)url {
    [self sendMessage:message withExterlBodyUrl:[NSURL URLWithString:url]];
}
 
#pragma mark ImagePickerDelegate
 
- (void)imagePickerDelegateImage:(UIImage *)image info:(NSString *)phAssetId {
    // When getting image from the camera, it may be 90° rotated due to orientation
    // (image.imageOrientation = UIImageOrientationRight). Just rotate it to be face up.
    if (image.imageOrientation != UIImageOrientationUp) {
        UIGraphicsBeginImageContextWithOptions(image.size, false, image.scale);
        [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
        image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
 
    // Dismiss popover on iPad
    if (IPAD) {
        [VIEW(ImagePickerView).popoverController dismissPopoverAnimated:TRUE];
    }
    [self chooseImageQuality:image assetId:phAssetId];
}
 
 
- (void)imagePickerDelegateVideo:(NSURL*)url info:(NSDictionary *)info {
    NSURL * mediaURL = [info objectForKey:UIImagePickerControllerMediaURL];
    [SVProgressHUD show];
    AVAsset *video = [AVAsset assetWithURL:mediaURL];
    AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:video presetName:AVAssetExportPresetMediumQuality];
    exportSession.shouldOptimizeForNetworkUse = YES;
    exportSession.outputFileType = AVFileTypeMPEG4;
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    
    NSString *localname = [[[mediaURL absoluteString] md5] stringByAppendingString:@".mp4"];
    NSURL *compressedVideoUrl=[[NSURL fileURLWithPath:documentsDirectory] URLByAppendingPathComponent:localname];
    exportSession.outputURL = compressedVideoUrl;
    [exportSession exportAsynchronouslyWithCompletionHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
            UIImage* image = [UIChatBubbleTextCell getImageFromVideoUrl:compressedVideoUrl];
            [_fileContext addObject:[NSData dataWithContentsOfURL:compressedVideoUrl] name:localname type:@"video" image:image];
            [self refreshImageDrawer];
        });
    }];
    
    if (![info valueForKey:UIImagePickerControllerReferenceURL]) {
            [self writeVideoToGallery:mediaURL];
        }
}
 
+ (void)writeMediaToGallery:(NSString *)name fileType:(NSString *)fileType {
    NSString *filePath = [LinphoneManager validFilePath:name];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:filePath]) {
        NSData* data = [NSData dataWithContentsOfFile:filePath];
 
        // define a block , not called immediately. To avoid crash when saving photo before PHAuthorizationStatusNotDetermined.
        void (^block)(void)= ^ {
            if ([fileType isEqualToString:@"image"] ) {
                // we're finished, save the image and update the message
                UIImage *image = [UIImage imageWithData:data];
                if (!image) {
                    ChatConversationView *view = VIEW(ChatConversationView);
                    [view showFileDownloadError];
                    return;
                }
                __block PHObjectPlaceholder *placeHolder;
                [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
                    PHAssetCreationRequest *request = [PHAssetCreationRequest creationRequestForAssetFromImage:image];
                    placeHolder = [request placeholderForCreatedAsset];
                } completionHandler:^(BOOL success, NSError *error) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        if (error) {
                            LOGE(@"Cannot save image data downloaded [%@]", [error localizedDescription]);
                            UIAlertController *errView = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Transfer error", nil)
                                message:NSLocalizedString(@"Cannot write image to photo library",nil)
                         preferredStyle:UIAlertControllerStyleAlert];
 
                            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK"
                                                                    style:UIAlertActionStyleDefault
                                                                  handler:^(UIAlertAction * action) {}];
 
                            [errView addAction:defaultAction];
                            [PhoneMainView.instance presentViewController:errView animated:YES completion:nil];
                        } else {
                            LOGI(@"Image saved to [%@]", [placeHolder localIdentifier]);
                        }
                    });
                }];
            } else if([fileType isEqualToString:@"video"]) {
                __block PHObjectPlaceholder *placeHolder;
                [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
                    PHAssetCreationRequest *request = [PHAssetCreationRequest creationRequestForAssetFromVideoAtFileURL:[NSURL fileURLWithPath:filePath]];
                    placeHolder = [request placeholderForCreatedAsset];
                } completionHandler:^(BOOL success, NSError * _Nullable error) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        if (error) {
                            LOGE(@"Cannot save video data downloaded [%@]", [error localizedDescription]);
                            UIAlertController *errView = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Transfer error", nil)
                                 message:NSLocalizedString(@"Cannot write video to photo library", nil)
                          preferredStyle:UIAlertControllerStyleAlert];
 
                            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK"
                                                                    style:UIAlertActionStyleDefault
                                                                  handler:^(UIAlertAction * action) {}];
                
                            [errView addAction:defaultAction];
                            [PhoneMainView.instance presentViewController:errView animated:YES completion:nil];
                        } else {
                            LOGI(@"video saved to [%@]", [placeHolder localIdentifier]);
                        }
                    });
                }];
            }
        };
 
        // When you save an image or video to a photo library, make sure that it is allowed. Otherwise, there will be a backup error.
        if ([PHPhotoLibrary authorizationStatus] == PHAuthorizationStatusAuthorized) {
            block();
        } else {
            [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    if ([PHPhotoLibrary authorizationStatus] == PHAuthorizationStatusAuthorized) {
                        block();
                    } else {
                        [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Photo's permission", nil) message:NSLocalizedString(@"Photo not authorized", nil) delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Continue", nil] show];
                    }
                });
            }];
        }
    }
}
 
-(void) writeVideoToGallery:(NSURL *)url {
    NSString *localIdentifier;
    PHFetchResult<PHAssetCollection *> *assetCollections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
    for (PHAssetCollection *assetCollection in assetCollections) {
        if([[assetCollection localizedTitle] isEqualToString:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"]]  ){
            localIdentifier = assetCollection.localIdentifier;
            break;
        }
    }
    if(localIdentifier ){
        PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[localIdentifier] options:nil];
        PHAssetCollection *assetCollection = fetchResult.firstObject;
        
        [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
            PHAssetChangeRequest *assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:url];
            
            PHAssetCollectionChangeRequest *assetCollectionChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:assetCollection];
            [assetCollectionChangeRequest addAssets:@[[assetChangeRequest placeholderForCreatedAsset]]];
        } completionHandler:^(BOOL success, NSError *error) {
            if (!success) {
                NSLog(@"Error creating asset: %@", error);
            }
        }];
    }else{
        __block PHObjectPlaceholder *albumPlaceholder;
        [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
            PHAssetCollectionChangeRequest *changeRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"]];
            albumPlaceholder = changeRequest.placeholderForCreatedAssetCollection;
        } completionHandler:^(BOOL success, NSError *error) {
            if (success) {
                PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[albumPlaceholder.localIdentifier] options:nil];
                PHAssetCollection *assetCollection = fetchResult.firstObject;
                
                [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
                    PHAssetChangeRequest *assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:url];
                    PHAssetCollectionChangeRequest *assetCollectionChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:assetCollection];
                    [assetCollectionChangeRequest addAssets:@[[assetChangeRequest placeholderForCreatedAsset]]];
                } completionHandler:^(BOOL success, NSError *error) {
                    if (!success) {
                        NSLog(@"Error creating asset: %@", error);
                    }
                }];
            } else {
                NSLog(@"Error creating album: %@", error);
            }
        }];
    }
}
 
- (void)tableViewIsScrolling {
    // if user is scrolling in table view, we should hide the keyboard
    if ([_messageField isFirstResponder]) {
        [_messageField resignFirstResponder];
    }
}
 
#pragma mark - Keyboard Event Functions
 
- (void)keyboardWillHide:(NSNotification *)notif {
    NSTimeInterval duration = [[[notif userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    int heightDiff = UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]) ? 55 : 105;
    
    [UIView animateWithDuration:duration
        delay:0
        options:UIViewAnimationOptionBeginFromCurrentState
        animations:^{
          CGFloat composeIndicatorCompensation = composingVisible ? _composeIndicatorView.frame.size.height : 0.0f;
 
          //                          Show TabBar and status bar and also top bar
 
          // somehow, it breaks rotation if we put that in the block above when rotating portrait -> landscape
          //                         if (!UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) {
          [PhoneMainView.instance hideTabBar:NO];
          //                         }
          [PhoneMainView.instance hideStatusBar:NO];
          [PhoneMainView.instance fullScreen:NO];
          _topBar.alpha = 1.0;
 
          // Resize chat view
          {
              CGRect chatFrame = [_chatView frame];
              chatFrame.origin.y = _topBar.frame.origin.y + _topBar.frame.size.height;
              chatFrame.size.height = [[self view] frame].size.height - chatFrame.origin.y;
              [_chatView setFrame:chatFrame];
          }
 
          // Resize & Move table view
          {
              CGRect tableFrame = [_tableController.view frame];
              tableFrame.size.height =
                  [_messageView frame].origin.y - tableFrame.origin.y - composeIndicatorCompensation;
              [_tableController.view setFrame:tableFrame];
 
              // Scroll to bottom
              NSInteger lastSection = [_tableController.tableView numberOfSections] - 1;
              if (lastSection >= 0) {
                  NSInteger lastRow = [_tableController.tableView numberOfRowsInSection:lastSection] - 1;
                  if (lastRow >= 0) {
                      [_tableController.tableView
                          scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:lastRow inSection:lastSection]
                                atScrollPosition:UITableViewScrollPositionBottom
                                        animated:FALSE];
                  }
              }
          }
            
            
            if ([_fileContext count] > 0){
                // resizing imagesView
                CGRect imagesFrame = [_imagesView frame];
                imagesFrame.origin.y = [_messageView frame].origin.y - heightDiff;
                imagesFrame.size.height = heightDiff;
                [_imagesView setFrame:imagesFrame];
                // resizing chatTable
                CGRect tableViewFrame = [_tableController.tableView frame];
                tableViewFrame.size.height = imagesFrame.origin.y - tableViewFrame.origin.y;
                [_tableController.tableView setFrame:tableViewFrame];
            }
        }
        completion:^(BOOL finished){
 
        }];
}
 
- (void)keyboardWillShow:(NSNotification *)notif {
    NSTimeInterval duration = [[[notif userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    int heightDiff = UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]) ? 55 : 105;
    
    [UIView animateWithDuration:duration
        delay:0
        options:UIViewAnimationOptionBeginFromCurrentState
        animations:^{
          CGFloat composeIndicatorCompensation = composingVisible ? _composeIndicatorView.frame.size.height : 0.0f;
 
          CGRect endFrame = [[[notif userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
 
          if (([[UIDevice currentDevice].systemVersion floatValue] < 8) &&
              UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
              int width = endFrame.size.height;
              endFrame.size.height = endFrame.size.width;
              endFrame.size.width = width;
          }
 
          // Hide TabBar and status bar and also top bar
          [PhoneMainView.instance hideTabBar:YES];
          [PhoneMainView.instance hideStatusBar:YES];
          [PhoneMainView.instance fullScreen:YES];
          _topBar.alpha = 0.0;
 
          // Resize chat view
          {
              CGRect viewFrame = [[self view] frame];
              CGRect rect = PhoneMainView.instance.view.bounds;
              CGPoint pos = {viewFrame.size.width, viewFrame.size.height};
              CGPoint gPos =
                  [self.view convertPoint:pos
                                   toView:[UIApplication sharedApplication]
                                              .keyWindow.rootViewController.view]; // Bypass IOS bug on landscape mode
              float diff = (rect.size.height - gPos.y - endFrame.size.height);
              if (diff > 0)
                  diff = 0;
              CGRect chatFrame = [_chatView frame];
              chatFrame.origin.y = 0;
              chatFrame.size.height = viewFrame.size.height - chatFrame.origin.y + diff;
              [_chatView setFrame:chatFrame];
          }
 
          // Resize & Move table view
          {
              CGRect tableFrame = _tableController.view.frame;
              tableFrame.size.height =
                  [_messageView frame].origin.y - tableFrame.origin.y - composeIndicatorCompensation;
              [_tableController.view setFrame:tableFrame];
          }
            
            if ([_fileContext count] > 0){
                // resizing imagesView
                CGRect imagesFrame = [_imagesView frame];
                imagesFrame.origin.y = [_messageView frame].origin.y - heightDiff;
                imagesFrame.size.height = heightDiff;
                [_imagesView setFrame:imagesFrame];
                // resizing chatTable
                CGRect tableViewFrame = [_tableController.tableView frame];
                tableViewFrame.size.height = imagesFrame.origin.y - tableViewFrame.origin.y;
                [_tableController.tableView setFrame:tableViewFrame];
            }
 
          // Scroll
          NSInteger lastSection = [_tableController.tableView numberOfSections] - 1;
          if (lastSection >= 0) {
              NSInteger lastRow = [_tableController.tableView numberOfRowsInSection:lastSection] - 1;
              if (lastRow >= 0) {
                  [_tableController.tableView
                      scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:lastRow inSection:lastSection]
                            atScrollPosition:UITableViewScrollPositionBottom
                                    animated:FALSE];
              }
          }
            
        }
        completion:^(BOOL finished){
            
        }];
}
 
#pragma mark - chat room callbacks
 
void on_chat_room_state_changed(LinphoneChatRoom *cr, LinphoneChatRoomState newState) {
    ChatConversationView *view = (__bridge ChatConversationView *)linphone_chat_room_cbs_get_user_data(linphone_chat_room_get_current_callbacks(cr));
    [view configureMessageField];
}
 
void on_chat_room_subject_changed(LinphoneChatRoom *cr, const LinphoneEventLog *event_log) {
    ChatConversationView *view = (__bridge ChatConversationView *)linphone_chat_room_cbs_get_user_data(linphone_chat_room_get_current_callbacks(cr));
    const char *subject = linphone_chat_room_get_subject(cr) ?: linphone_event_log_get_subject(event_log);
    if (subject) {
        view.addressLabel.text = [NSString stringWithUTF8String:subject];
        [view.tableController addEventEntry:(LinphoneEventLog *)event_log];
        [view.tableController scrollToBottom:true];
        if (IPAD) {
            [VIEW(ChatsListView).tableController loadData];
        }
    }
}
 
void on_chat_room_participant_added(LinphoneChatRoom *cr, const LinphoneEventLog *event_log) {
    ChatConversationView *view = (__bridge ChatConversationView *)linphone_chat_room_cbs_get_user_data(linphone_chat_room_get_current_callbacks(cr));
    [view.tableController addEventEntry:(LinphoneEventLog *)event_log];
    [view updateParticipantLabel];
    [view.tableController scrollToBottom:true];
}
 
void on_chat_room_participant_removed(LinphoneChatRoom *cr, const LinphoneEventLog *event_log) {
    ChatConversationView *view = (__bridge ChatConversationView *)linphone_chat_room_cbs_get_user_data(linphone_chat_room_get_current_callbacks(cr));
    [view.tableController addEventEntry:(LinphoneEventLog *)event_log];
    [view updateParticipantLabel];
    [view.tableController scrollToBottom:true];
    UIImage *image = [FastAddressBook imageForSecurityLevel:linphone_chat_room_get_security_level(cr)];
    [view.encryptedButton setImage:image forState:UIControlStateNormal];
}
 
void on_chat_room_participant_admin_status_changed(LinphoneChatRoom *cr, const LinphoneEventLog *event_log) {
    ChatConversationView *view = (__bridge ChatConversationView *)linphone_chat_room_cbs_get_user_data(linphone_chat_room_get_current_callbacks(cr));
    [view.tableController addEventEntry:(LinphoneEventLog *)event_log];
    [view.tableController scrollToBottom:true];
}
 
void on_chat_room_chat_message_received(LinphoneChatRoom *cr, const LinphoneEventLog *event_log) {
    ChatConversationView *view = (__bridge ChatConversationView *)linphone_chat_room_cbs_get_user_data(linphone_chat_room_get_current_callbacks(cr));
 
    LinphoneChatMessage *chat = linphone_event_log_get_chat_message(event_log);
    if (!chat)
        return;
 
    BOOL hasFile = FALSE;
    // if auto_download is available and file is downloaded
    if ((linphone_core_get_max_size_for_auto_download_incoming_files(LC) > -1) && linphone_chat_message_get_file_transfer_information(chat))
        hasFile = TRUE;
 
    if (!linphone_chat_message_is_file_transfer(chat) && !linphone_chat_message_is_text(chat) && !hasFile) /*probably an imdn*/
        return;
 
    const LinphoneAddress *from = linphone_chat_message_get_from_address(chat);
    if (!from)
        return;
 
    [view.tableController addEventEntry:(LinphoneEventLog *)event_log];
    [NSNotificationCenter.defaultCenter postNotificationName:kLinphoneMessageReceived object:view];
    [view.tableController scrollToLastUnread:TRUE];
}
 
void on_chat_room_chat_message_sending(LinphoneChatRoom *cr, const LinphoneEventLog *event_log) {
    ChatConversationView *view = (__bridge ChatConversationView *)linphone_chat_room_cbs_get_user_data(linphone_chat_room_get_current_callbacks(cr));
    [view.tableController addEventEntry:(LinphoneEventLog *)event_log];
    [view.tableController scrollToBottom:true];
 
    if (IPAD)
        [NSNotificationCenter.defaultCenter postNotificationName:kLinphoneMessageReceived object:view];
}
 
void on_chat_room_is_composing_received(LinphoneChatRoom *cr, const LinphoneAddress *remoteAddr, bool_t isComposing) {
    ChatConversationView *view = (__bridge ChatConversationView *)linphone_chat_room_cbs_get_user_data(linphone_chat_room_get_current_callbacks(cr));
    BOOL composing = linphone_chat_room_is_remote_composing(cr) || bctbx_list_size(linphone_chat_room_get_composing_addresses(cr)) > 0;
    [view setComposingVisible:composing withDelay:0.3];
}
 
void on_chat_room_conference_joined(LinphoneChatRoom *cr, const LinphoneEventLog *event_log) {
    ChatConversationView *view = (__bridge ChatConversationView *)linphone_chat_room_cbs_get_user_data(linphone_chat_room_get_current_callbacks(cr));
    [view configureForRoom:false];
    [view.tableController scrollToBottom:true];
    if (IPAD)
        [NSNotificationCenter.defaultCenter postNotificationName:kLinphoneMessageReceived object:nil];
}
 
void on_chat_room_conference_left(LinphoneChatRoom *cr, const LinphoneEventLog *event_log) {
    ChatConversationView *view = (__bridge ChatConversationView *)linphone_chat_room_cbs_get_user_data(linphone_chat_room_get_current_callbacks(cr));
    [view.tableController addEventEntry:(LinphoneEventLog *)event_log];
    [view.tableController scrollToBottom:true];
}
 
- (void)goToDeviceListView {
    DevicesListView *view = VIEW(DevicesListView);
    view.room = _chatRoom;
    [PhoneMainView.instance popToView:view.compositeViewDescription];
}
 
void on_chat_room_conference_alert(LinphoneChatRoom *cr, const LinphoneEventLog *event_log) {
    ChatConversationView *view = (__bridge ChatConversationView *)linphone_chat_room_cbs_get_user_data(linphone_chat_room_get_current_callbacks(cr));
    [view.tableController addEventEntry:(LinphoneEventLog *)event_log];
    [view.tableController scrollToBottom:true];
    UIImage *image = [FastAddressBook imageForSecurityLevel:linphone_chat_room_get_security_level(cr)];
    [view.encryptedButton setImage:image forState:UIControlStateNormal];
}
 
- (void)openFileWithURLs:(NSMutableArray<NSURL *>*)urls index:(NSInteger)currentIndex
{
    //create the Quicklook controller.
    QLPreviewController *qlController = [[QLPreviewController alloc] init];
    self.FileDataSource = [[FileDataSource alloc] initWithFiles:urls];
    
    qlController.dataSource = self.FileDataSource;
    qlController.currentPreviewItemIndex = currentIndex;
    qlController.delegate = self;
    
    //present the document.
    [self presentViewController:qlController animated:YES completion:nil];
}
 
- (void)openFileWithURL:(NSURL *)url
{
    [self openFileWithURLs:[NSMutableArray arrayWithObject:url] index:0];
}
 
- (void)previewControllerDidDismiss:(QLPreviewController *)controller
{
    // QuickLook: When done button is pushed
    [PhoneMainView.instance fullScreen:NO];
}
 
+ (NSData *)getFileData: (NSString *)name {
    NSString *filePath = [LinphoneManager validFilePath:name];
    return [NSData dataWithContentsOfFile:filePath];
}
 
+ (NSURL *)getFileUrl: (NSString *)name {
    NSString *filePath = [LinphoneManager validFilePath:name];
    return [NSURL fileURLWithPath:filePath];
}
 
+ (void)writeFileInImagesDirectory:(NSData *)data name:(NSString *)name {
    NSString *filePath = [[LinphoneManager imagesDirectory] stringByAppendingPathComponent:name];
    if (name || [name isEqualToString:@""]) {
        LOGW(@"try to write file in %@", filePath);
    }
    [[NSFileManager defaultManager] createFileAtPath:filePath
                                                contents:data
                                              attributes:nil];
}
 
- (NSURL *)getICloudFileUrl:(NSString *)name {
    if (@available(iOS 11.0, *)) {
        return [NSURL fileURLWithPath:[LinphoneManager documentFile:name]];
    }
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSURL *icloudPath = [[fileManager URLForUbiquityContainerIdentifier:nil] URLByAppendingPathComponent:@"Documents"];
    
    if (icloudPath) {
        if (![fileManager fileExistsAtPath:icloudPath.path isDirectory:nil]) {
            LOGI(@"Create directory");
            [fileManager createDirectoryAtURL:icloudPath withIntermediateDirectories:YES attributes:nil error:nil];
        }
        
        return [icloudPath URLByAppendingPathComponent:name];
    }
    
    return nil;
}
 
- (void)deleteFileWithUuid:(NSUUID *)uuid {
    [_fileContext deleteContentWithUuid:uuid];
    [self refreshImageDrawer];
}
 
- (void)clearMessageView {
    [_messageField setText:@""];
    if (!_fileContext) _fileContext = [[FileContext alloc] init];
    [_fileContext clear];
    
    [self refreshImageDrawer];
}
 
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [_fileContext count];
}
 
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UIImageViewDeletable *imgView = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([UIImageViewDeletable class]) forIndexPath:indexPath];
    CGRect imgFrame = imgView.frame;
    imgFrame.origin.y = 5;
    if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
        imgFrame.size.height = 50;
    } else {
        imgFrame.size.height = 100;
    }
 
    [imgView.image setImage:[UIImage resizeImage:[_fileContext.previewsArray objectAtIndex:[indexPath item]] withMaxWidth:imgFrame.size.width andMaxHeight:imgFrame.size.height]];
    [imgView setUuid:[_fileContext.uuidsArray objectAtIndex:[indexPath item]]];
    [imgView setDeleteDelegate:self];
    [imgView setFrame:imgFrame];
    [_sendButton setEnabled:TRUE];
    return imgView;
}
 
- (void)refreshImageDrawer {
    int heightDiff = UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]) ? 55 : 105;
    
    if ([_fileContext count] == 0) {
        [UIView animateWithDuration:0
                              delay:0
                            options:UIViewAnimationOptionBeginFromCurrentState
                         animations:^{
                             // resizing imagesView
                             CGRect imagesFrame = [_imagesView frame];
                             imagesFrame.origin.y = [_messageView frame].origin.y;
                             imagesFrame.size.height = 0;
                             [_imagesView setFrame:imagesFrame];
                             // resizing chatTable
                             CGRect tableViewFrame = [_tableController.tableView frame];
                             tableViewFrame.size.height = imagesFrame.origin.y - tableViewFrame.origin.y;
                             [_tableController.tableView setFrame:tableViewFrame];
                         }
                         completion:nil];
        if ([_messageField.text isEqualToString:@""])
            [_sendButton setEnabled:FALSE];
    } else {
        // resizing imagesView
        CGRect imagesFrame = [_imagesView frame];
        imagesFrame.origin.y = [_messageView frame].origin.y - heightDiff;
        imagesFrame.size.height = heightDiff;
        [_imagesView setFrame:imagesFrame];
        // resizing chatTable
        CGRect tableViewFrame = [_tableController.tableView frame];
        tableViewFrame.size.height = imagesFrame.origin.y - tableViewFrame.origin.y;
        [_tableController.tableView setFrame:tableViewFrame];
        [_imagesCollectionView reloadData];
    }
}
 
- (void)showFileDownloadError {
    UIAlertController *errView = [UIAlertController
                                  alertControllerWithTitle:NSLocalizedString(@"File download error", nil)
                                  message:NSLocalizedString(@"Error while downloading the file.\n"
                                                            @"The file is probably encrypted.\n"
                                                            @"Please retry to download this file after activating LIME.",
                                                            nil)
                                  preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"OK"
                                                            style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction *action){
                                                          }];
    
    [errView addAction:defaultAction];
    [PhoneMainView.instance presentViewController:errView animated:YES completion:nil];
}
 
+ (NSString *)getKeyFromFileType:(NSString *)fileType fileName:(NSString *)name{
    if ([fileType isEqualToString:@"video"]) {
        return @"localvideo";
    } else if ([fileType isEqualToString:@"image"] || [name hasSuffix:@"JPG"] || [name hasSuffix:@"PNG"] || [name hasSuffix:@"jpg"] || [name hasSuffix:@"png"]) {
        return @"localimage";
    }
    return @"localfile";
}
 
/* There are three cases: auto download in foreground, auto download in background, on click download*/
+ (void)autoDownload:(LinphoneChatMessage *)message {
    ChatConversationView *view = VIEW(ChatConversationView);
    LinphoneContent *content = linphone_chat_message_get_file_transfer_information(message);
    NSString *name = [NSString stringWithUTF8String:linphone_content_get_name(content)];
    NSString *fileType = [NSString stringWithUTF8String:linphone_content_get_type(content)];
    NSString *key = [ChatConversationView getKeyFromFileType:fileType fileName:name];
 
    [LinphoneManager setValueInMessageAppData:name forKey:key inMessage:message];
    dispatch_async(dispatch_get_main_queue(), ^{
        [NSNotificationCenter.defaultCenter postNotificationName:kLinphoneMessageReceived object:view];
        if (![VFSUtil vfsEnabledWithGroupName:kLinphoneMsgNotificationAppGroupId] && [ConfigManager.instance lpConfigBoolForKeyWithKey:@"auto_write_to_gallery_preference"]) {
            [ChatConversationView writeMediaToGallery:name fileType:fileType];
        }
    });
}
 
-(void) documentMenu:(UIDocumentMenuViewController *)documentMenu didPickDocumentPicker:(UIDocumentPickerViewController *)documentPicker {
    documentPicker.delegate = self;
    [PhoneMainView.instance presentViewController:documentPicker animated:YES completion:nil];
}
 
-(void) documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url {
    [url startAccessingSecurityScopedResource];
    NSFileCoordinator *co =[[NSFileCoordinator alloc] init];
    NSError *error = nil;
    [co coordinateReadingItemAtURL:url options:0 error:&error byAccessor:^(NSURL * _Nonnull newURL) {
        UIImage *image = [ChatConversationView drawText:[newURL lastPathComponent] image:[ChatConversationView getBasicImage] textSize:10];
        [_fileContext addObject:[NSData dataWithContentsOfURL:newURL] name:[newURL lastPathComponent] type:@"file" image:image];
        [self refreshImageDrawer];
    }];
    [url stopAccessingSecurityScopedResource];
}
 
+(UIImage *)getBasicImage {
    UIColor *color=[UIColor grayColor];
    CGRect frame = CGRectMake(0, 0, 200, 200);
    UIGraphicsBeginImageContext(frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, frame);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
 
+(UIImage*)drawText:(NSString*)text image:(UIImage *)image textSize:(CGFloat)textSize
{
    UIFont *font = [UIFont boldSystemFontOfSize:textSize];
    UIGraphicsBeginImageContext(image.size);
    [image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
    CGRect rect = CGRectMake(0, 30, image.size.width, image.size.height);
    [[UIColor whiteColor] set];
    [text drawInRect:CGRectIntegral(rect) withFont:font];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
 
    return newImage;
}
 
// Popup menu
 
 
 
- (void) setupPopupMenu {
    _popupMenu.dataSource = self;
    _popupMenu.delegate = self;
    _tableController.editButton.hidden = true;
    _popupMenu.layer.shadowColor = [UIColor lightGrayColor].CGColor;
    _popupMenu.layer.shadowOpacity = 0.5;
    _popupMenu.layer.shadowOffset = CGSizeZero;
    _popupMenu.layer.shadowRadius = 10;
    _popupMenu.layer.masksToBounds = false;
    _toggleMenuButton.hidden = false;
    _popupMenu.tableFooterView = [UIView new];
    [_popupMenu reloadData];
}
 
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self onToggleMenu:nil];
    if (indexPath.row == 0) {
        [self goToDeviceListView];
    }
    if (indexPath.row == 1) {
        [_tableController onEditClick:nil];
        [self onEditionChangeClick:nil];
    }
    if ([ConfigManager.instance lpConfigBoolForKeyWithKey:@"ephemeral_feature" defaultValue:false]) {
        if (indexPath.row == 2) {
            EphemeralSettingsView *view = VIEW(EphemeralSettingsView);
            view.room = _chatRoom;
            [PhoneMainView.instance popToView:view.compositeViewDescription];
        }
    }
}
 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [ConfigManager.instance lpConfigBoolForKeyWithKey:@"ephemeral_feature" defaultValue:false] ? 3 : 2;
}
 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    
    if (indexPath.row == 0) {
        cell.imageView.image = [LinphoneUtils resizeImage:[UIImage imageNamed:@"menu_security_default.png"] newSize:CGSizeMake(20, 25)];
        cell.textLabel.text = NSLocalizedString(@"Conversation's devices",nil);
    }
    if (indexPath.row == 1) {
        cell.imageView.image =  [LinphoneUtils resizeImage:[UIImage imageNamed:@"delete_default.png"] newSize:CGSizeMake(20, 25)];
        cell.textLabel.text = NSLocalizedString(@"Delete messages",nil);
    }
    if ([ConfigManager.instance lpConfigBoolForKeyWithKey:@"ephemeral_feature" defaultValue:false]) {
        if (indexPath.row == 2) {
            cell.imageView.image =  [LinphoneUtils resizeImage:[UIImage imageNamed:@"ephemeral_messages_default.png"] newSize:CGSizeMake(20, 25)];
            cell.textLabel.text = NSLocalizedString(@"Ephemeral messages",nil);
        }
    }
    cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
    return cell;
}
- (IBAction)onToggleMenu:(id)sender {
    _popupMenu.hidden = !_popupMenu.hidden;
    if (!_popupMenu.hidden)
        [_popupMenu selectRowAtIndexPath:nil animated:false scrollPosition:UITableViewScrollPositionNone];
}
 
 
@end