SystemMessage.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2013-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 "SystemMessage.h"
  21. #import "Contact.h"
  22. #import "LicenseStore.h"
  23. @implementation SystemMessage
  24. @dynamic type;
  25. @dynamic arg;
  26. - (NSString*)format {
  27. NSDictionary *dict;
  28. switch (self.type.intValue) {
  29. case kSystemMessageContactOtherAppInfo:
  30. if ([LicenseStore requiresLicenseKey] == true) {
  31. return NSLocalizedString(@"contact_threema_conversation_info", nil);
  32. } else {
  33. return NSLocalizedString(@"contact_threema_work_conversation_info", nil);
  34. }
  35. case kSystemMessageRenameGroup:
  36. return [NSString stringWithFormat:NSLocalizedString(@"group_renamed_to_x", nil), [self argAsUTF8String]];
  37. case kSystemMessageGroupMemberLeave:
  38. return [NSString stringWithFormat:NSLocalizedString(@"group_member_x_left", nil), [self argAsUTF8String]];
  39. case kSystemMessageGroupMemberAdd:
  40. return [NSString stringWithFormat:NSLocalizedString(@"group_member_x_added", nil), [self argAsUTF8String]];
  41. case kSystemMessageGroupMemberForcedLeave:
  42. return [NSString stringWithFormat:NSLocalizedString(@"group_member_x_forced_leave", nil), [self argAsUTF8String]];
  43. case kSystemMessageGroupSelfAdded:
  44. return NSLocalizedString(@"group_member_self_added", nil);
  45. case kSystemMessageGroupSelfRemoved:
  46. return NSLocalizedString(@"group_member_self_removed", nil);
  47. case kSystemMessageGroupSelfLeft:
  48. return NSLocalizedString(@"group_member_self_left", nil);
  49. case kSystemMessageCallMissed:
  50. return NSLocalizedString(@"call_missed", nil);
  51. case kSystemMessageCallRejected:
  52. return NSLocalizedString(@"call_rejected", nil);
  53. case kSystemMessageCallRejectedBusy:
  54. return NSLocalizedString(@"call_rejected_busy", nil);
  55. case kSystemMessageCallRejectedTimeout:
  56. dict = [self argAsDictionary];
  57. if ([dict[@"CallInitiator"] boolValue]) {
  58. return NSLocalizedString(@"call_rejected_timeout", nil);
  59. } else {
  60. return NSLocalizedString(@"call_missed", nil);
  61. }
  62. case kSystemMessageCallRejectedDisabled:
  63. return NSLocalizedString(@"call_rejected_disabled", nil);
  64. case kSystemMessageCallRejectedOffHours:
  65. dict = [self argAsDictionary];
  66. if ([dict[@"CallInitiator"] boolValue]) {
  67. return NSLocalizedString(@"call_rejected_unknown", nil);
  68. } else {
  69. return NSLocalizedString(@"call_missed", nil);
  70. }
  71. case kSystemMessageCallRejectedUnknown:
  72. dict = [self argAsDictionary];
  73. if ([dict[@"CallInitiator"] boolValue]) {
  74. return NSLocalizedString(@"call_rejected_unknown", nil);
  75. } else {
  76. return NSLocalizedString(@"call_missed", nil);
  77. }
  78. case kSystemMessageCallEnded:
  79. dict = [self argAsDictionary];
  80. NSString *callTime = dict[@"CallTime"];
  81. if (callTime && callTime.length > 0) {
  82. if ([dict[@"CallInitiator"] boolValue]) {
  83. return NSLocalizedString(@"call_outgoing_ended", nil);
  84. } else {
  85. return NSLocalizedString(@"call_incoming_ended", nil);
  86. }
  87. } else {
  88. if ([dict[@"CallInitiator"] boolValue]) {
  89. return NSLocalizedString(@"call_canceled", nil);
  90. } else {
  91. return NSLocalizedString(@"call_missed", nil);
  92. }
  93. }
  94. }
  95. return nil;
  96. }
  97. - (NSString *)callDetail {
  98. NSDictionary *dict;
  99. switch (self.type.intValue) {
  100. case kSystemMessageCallMissed:
  101. case kSystemMessageCallRejected:
  102. case kSystemMessageCallRejectedBusy:
  103. case kSystemMessageCallRejectedTimeout:
  104. case kSystemMessageCallRejectedDisabled:
  105. case kSystemMessageCallRejectedOffHours:
  106. case kSystemMessageCallRejectedUnknown:
  107. return nil;
  108. case kSystemMessageCallEnded:
  109. dict = [self argAsDictionary];
  110. NSString *callTime = dict[@"CallTime"];
  111. if (callTime && callTime.length > 0) {
  112. return [NSString stringWithFormat:NSLocalizedString(@"call_duration", nil), dict[@"CallTime"]];
  113. }
  114. return nil;
  115. }
  116. return nil;
  117. }
  118. - (BOOL)isCallType {
  119. switch (self.type.integerValue) {
  120. case kSystemMessageCallMissed:
  121. case kSystemMessageCallRejected:
  122. case kSystemMessageCallRejectedBusy:
  123. case kSystemMessageCallRejectedTimeout:
  124. case kSystemMessageCallRejectedDisabled:
  125. case kSystemMessageCallRejectedOffHours:
  126. case kSystemMessageCallRejectedUnknown:
  127. case kSystemMessageCallEnded:
  128. return YES;
  129. default:
  130. return NO;
  131. }
  132. }
  133. - (BOOL)haveCallTime {
  134. NSDictionary *dict = [self argAsDictionary];
  135. NSString *callTime = dict[@"CallTime"];
  136. if (callTime && callTime.length > 0) {
  137. return YES;
  138. }
  139. return NO;
  140. }
  141. - (NSString *)callTime {
  142. NSDictionary *dict = [self argAsDictionary];
  143. NSString *callTime = dict[@"CallTime"];
  144. if (callTime && callTime.length > 0) {
  145. return callTime;
  146. }
  147. return nil;
  148. }
  149. - (NSString*)logText {
  150. return [self format];
  151. }
  152. - (NSString*)previewText {
  153. return [self format];
  154. }
  155. - (NSString *)argAsUTF8String {
  156. return [[NSString alloc] initWithData:self.arg encoding:NSUTF8StringEncoding];
  157. }
  158. - (NSDictionary *)argAsDictionary {
  159. NSError *error;
  160. if (!self.arg || self.arg.length == 0) {
  161. return nil;
  162. }
  163. NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:self.arg options:NSJSONReadingAllowFragments error:&error];
  164. if (error) {
  165. return nil;
  166. }
  167. return dict;
  168. }
  169. @end