ErrorNotificationHandler.m 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 "ErrorNotificationHandler.h"
  21. #import "Contact.h"
  22. #import "AppDelegate.h"
  23. @implementation ErrorNotificationHandler
  24. static ErrorNotificationHandler *singleton;
  25. + (void)setup {
  26. static dispatch_once_t onceToken;
  27. dispatch_once(&onceToken, ^{
  28. singleton = [[ErrorNotificationHandler alloc] init];
  29. });
  30. }
  31. - (instancetype)init
  32. {
  33. self = [super init];
  34. if (self) {
  35. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleServerMessage:) name:kNotificationServerMessage object:nil];
  36. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleConnectionFailed:) name:kNotificationErrorConnectionFailed object:nil];
  37. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleUnknownGroup:) name:kNotificationErrorUnknownGroup object:nil];
  38. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handlePublicKeyMismatch:) name:kNotificationErrorPublicKeyMismatch object:nil];
  39. }
  40. return self;
  41. }
  42. - (void)dealloc {
  43. [[NSNotificationCenter defaultCenter] removeObserver:self];
  44. }
  45. - (void)handleConnectionFailed:(NSNotification*)notification {
  46. NSString *title = @"Connection error";
  47. NSString *message = [notification.userInfo objectForKey:kKeyMessage];
  48. [self showAlertWithTitle:title message:message];
  49. }
  50. - (void)handleUnknownGroup:(NSNotification*)notification {
  51. Contact *contact = [notification.userInfo objectForKey:kKeyContact];
  52. NSString *title = NSLocalizedString(@"msg_unknown_group_request_sync_x_title", nil);
  53. NSString *message = [NSString stringWithFormat:NSLocalizedString(@"msg_unknown_group_request_sync_x_message", nil), contact.displayName];
  54. [self showAlertWithTitle:title message:message];
  55. }
  56. - (void)handlePublicKeyMismatch:(NSNotification*)notification {
  57. NSString *title = NSLocalizedString(@"public_key_mismatch_title", nil);
  58. NSString *message = NSLocalizedString(@"public_key_mismatch_message", nil);
  59. [self showAlertWithTitle:title message:message];
  60. }
  61. - (void)handleServerMessage:(NSNotification*)notification {
  62. NSString *title = NSLocalizedString(@"server_message_title", nil);
  63. NSString *message = [notification.userInfo objectForKey:kKeyMessage];
  64. [self showAlertWithTitle:title message:message];
  65. }
  66. - (void)showAlertWithTitle:(NSString *)title message:(NSString *)message {
  67. dispatch_async(dispatch_get_main_queue(), ^{
  68. UIViewController *vc = [[[AppDelegate sharedAppDelegate] window] rootViewController];
  69. [UIAlertTemplate showAlertWithOwner:vc title:title message:message actionOk:nil];
  70. });
  71. }
  72. @end