AudioMessage.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2014-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 "AudioMessage.h"
  21. #import "AudioData.h"
  22. #import "NSString+Hex.h"
  23. #import "UTIConverter.h"
  24. #import "Utils.h"
  25. #import "ThreemaFramework/ThreemaFramework-swift.h"
  26. @implementation AudioMessage
  27. @dynamic duration;
  28. @dynamic encryptionKey;
  29. @dynamic progress;
  30. @dynamic audioBlobId;
  31. @dynamic audioSize;
  32. @dynamic audio;
  33. - (NSString*)logText {
  34. int seconds = self.duration.intValue;
  35. int minutes = seconds / 60;
  36. seconds -= minutes * 60;
  37. return [NSString stringWithFormat:@"%@ (%02d:%02d, %@)", NSLocalizedString(@"audio", nil), minutes, seconds, [self blobGetFilename]];
  38. }
  39. - (NSString*)previewText {
  40. return [NSString stringWithFormat:@"%@ (%@)", NSLocalizedString(@"audio", nil), [Utils timeStringForSeconds:self.duration.integerValue]];
  41. }
  42. - (NSData *)blobGetData {
  43. if (self.audio) {
  44. return self.audio.data;
  45. }
  46. return nil;
  47. }
  48. - (NSData *)blobGetId {
  49. return self.audioBlobId;
  50. }
  51. - (NSData *)blobGetEncryptionKey {
  52. return self.encryptionKey;
  53. }
  54. - (NSNumber *)blobGetSize {
  55. return self.audioSize;
  56. }
  57. - (void)blobSetData:(NSData *)data {
  58. AudioData *dbData = [NSEntityDescription
  59. insertNewObjectForEntityForName:@"AudioData"
  60. inManagedObjectContext:self.managedObjectContext];
  61. dbData.data = data;
  62. self.audio = dbData;
  63. }
  64. - (NSData *)blobGetThumbnail {
  65. return nil;
  66. }
  67. - (NSString *)blobGetUTI {
  68. return UTTYPE_AUDIO;
  69. }
  70. - (NSString *)blobGetFilename {
  71. return [NSString stringWithFormat: @"%@.%@", [NSString stringWithHexData:self.id], MEDIA_EXTENSION_AUDIO];
  72. }
  73. - (NSString *)blobGetWebFilename {
  74. return [NSString stringWithFormat: @"threema-%@-audio.%@", [DateFormatter getDateForWeb:self.date], MEDIA_EXTENSION_AUDIO];
  75. }
  76. - (void)blobUpdateProgress:(NSNumber *)progress {
  77. self.progress = progress;
  78. }
  79. - (NSNumber *)blobGetProgress {
  80. return self.progress;
  81. }
  82. #ifdef DEBUG
  83. #else
  84. - (NSString *)debugDescription
  85. {
  86. return [NSString stringWithFormat:@"<%@: %p> %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@", [self class], self, @"duration = ", self.duration.description, @"encryptionKey = ", @"***", @"progress = ", self.progress.description, @"audioBlobId = ", @"***", @"audioSize = ", self.audioSize.description, @"audio = ", self.audio.description];
  87. }
  88. #endif
  89. #pragma mark ExternalStorageInfo
  90. - (NSString *)getFilename {
  91. if (self.audio != nil) {
  92. if (self.audio.data != nil && [self.audio.data respondsToSelector:NSSelectorFromString(@"filename")]) {
  93. return [self.audio.data performSelector:NSSelectorFromString(@"filename")];
  94. }
  95. }
  96. return nil;
  97. }
  98. @end