sparkle-updater.mm 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include "mac-update.hpp"
  2. #include <qaction.h>
  3. #import <Cocoa/Cocoa.h>
  4. #import <Sparkle/Sparkle.h>
  5. @interface OBSUpdateDelegate : NSObject <SPUUpdaterDelegate> {
  6. }
  7. @property (copy) NSString *branch;
  8. @property (nonatomic) SPUStandardUpdaterController *updaterController;
  9. @end
  10. @implementation OBSUpdateDelegate {
  11. }
  12. @synthesize branch;
  13. - (nonnull NSSet<NSString *> *)allowedChannelsForUpdater:
  14. (nonnull SPUUpdater *)updater
  15. {
  16. return [NSSet setWithObject:branch];
  17. }
  18. - (void)observeCanCheckForUpdatesWithAction:(QAction *)action
  19. {
  20. [_updaterController.updater
  21. addObserver:self
  22. forKeyPath:NSStringFromSelector(@selector(canCheckForUpdates))
  23. options:(NSKeyValueObservingOptionInitial |
  24. NSKeyValueObservingOptionNew)
  25. context:(void *)action];
  26. }
  27. - (void)observeValueForKeyPath:(NSString *)keyPath
  28. ofObject:(id)object
  29. change:(NSDictionary<NSKeyValueChangeKey, id> *)change
  30. context:(void *)context
  31. {
  32. if ([keyPath isEqualToString:NSStringFromSelector(
  33. @selector(canCheckForUpdates))]) {
  34. QAction *menuAction = (QAction *)context;
  35. menuAction->setEnabled(
  36. _updaterController.updater.canCheckForUpdates);
  37. } else {
  38. [super observeValueForKeyPath:keyPath
  39. ofObject:object
  40. change:change
  41. context:context];
  42. }
  43. }
  44. - (void)dealloc
  45. {
  46. @autoreleasepool {
  47. [_updaterController.updater
  48. removeObserver:self
  49. forKeyPath:NSStringFromSelector(
  50. @selector(canCheckForUpdates))];
  51. }
  52. }
  53. @end
  54. OBSSparkle::OBSSparkle(const char *branch, QAction *checkForUpdatesAction)
  55. {
  56. @autoreleasepool {
  57. updaterDelegate = [[OBSUpdateDelegate alloc] init];
  58. updaterDelegate.branch = [NSString stringWithUTF8String:branch];
  59. updaterDelegate.updaterController =
  60. [[SPUStandardUpdaterController alloc]
  61. initWithStartingUpdater:YES
  62. updaterDelegate:updaterDelegate
  63. userDriverDelegate:nil];
  64. [updaterDelegate observeCanCheckForUpdatesWithAction:
  65. checkForUpdatesAction];
  66. }
  67. }
  68. void OBSSparkle::setBranch(const char *branch)
  69. {
  70. updaterDelegate.branch = [NSString stringWithUTF8String:branch];
  71. }
  72. void OBSSparkle::checkForUpdates(bool manualCheck)
  73. {
  74. @autoreleasepool {
  75. if (manualCheck) {
  76. [updaterDelegate.updaterController checkForUpdates:nil];
  77. } else {
  78. [updaterDelegate.updaterController
  79. .updater checkForUpdatesInBackground];
  80. }
  81. }
  82. }