ContactUtil.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2015-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "ContactUtil.h"
  21. #import "UserSettings.h"
  22. #import <ContactsUI/ContactsUI.h>
  23. #import "MDMSetup.h"
  24. #ifdef DEBUG
  25. static const DDLogLevel ddLogLevel = DDLogLevelVerbose;
  26. #else
  27. static const DDLogLevel ddLogLevel = DDLogLevelWarning;
  28. #endif
  29. @implementation ContactUtil
  30. + (ContactUtil *) contactUtil
  31. {
  32. ContactUtil *addressUtil = [[ContactUtil alloc] init];
  33. return addressUtil;
  34. }
  35. + (NSMutableString *)nameFromFirstname:(NSString *)firstName lastname:(NSString *)lastName {
  36. NSMutableString *name = [[NSMutableString alloc] init];
  37. if ([UserSettings sharedUserSettings].displayOrderFirstName) {
  38. if (firstName != nil && firstName.length > 0)
  39. [name appendString:firstName];
  40. if (lastName != nil && lastName.length > 0) {
  41. if (firstName != nil && firstName.length > 0) {
  42. [name appendString:@" "];
  43. }
  44. [name appendString:lastName];
  45. }
  46. } else {
  47. if (lastName != nil && lastName.length > 0)
  48. [name appendString:lastName];
  49. if (firstName != nil && firstName.length > 0) {
  50. if (lastName != nil && lastName.length > 0) {
  51. [name appendString:@" "];
  52. }
  53. [name appendString:firstName];
  54. }
  55. }
  56. return name;
  57. }
  58. + (UIViewController *)getContactViewControllerForVCardData:(NSData *)data
  59. {
  60. CNContact *contact = [self cnContactForVCardData:data];
  61. if (contact) {
  62. MDMSetup *mdmSetup = [[MDMSetup alloc] initWithSetup:false];
  63. CNContactViewController *controller = [CNContactViewController viewControllerForUnknownContact:contact];
  64. controller.allowsActions = ![mdmSetup disableShareMedia];
  65. controller.allowsEditing = NO;
  66. return controller;
  67. } else {
  68. DDLogInfo(@"cannot display person details");
  69. return nil;
  70. }
  71. }
  72. + (NSString *)getNameFromVCardData:(NSData *)data {
  73. CNContact *contact = [self cnContactForVCardData:data];
  74. if (contact) {
  75. NSString *firstName = contact.givenName;
  76. NSString *lastName = contact.familyName;
  77. return [ContactUtil nameFromFirstname:firstName lastname:lastName];
  78. }
  79. return nil;
  80. }
  81. + (CNContact *)cnContactForVCardData:(NSData *)data {
  82. NSError *error;
  83. CNContact *contact = [CNContactVCardSerialization contactsWithData:data error:&error].firstObject;
  84. if (!error)
  85. return contact;
  86. return nil;
  87. }
  88. + (NSData *)vCardDataForCnContact:(CNContact *)contact {
  89. NSError *error;
  90. NSData *data = [CNContactVCardSerialization dataWithContacts:@[contact] error:&error];
  91. if (!error)
  92. return data;
  93. return nil;
  94. }
  95. @end