sparkle-updater.mm 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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:(nonnull SPUUpdater *)updater
  14. {
  15. return [NSSet setWithObject:branch];
  16. }
  17. - (void)observeCanCheckForUpdatesWithAction:(QAction *)action
  18. {
  19. [_updaterController.updater addObserver:self forKeyPath:NSStringFromSelector(@selector(canCheckForUpdates))
  20. options:(NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew)
  21. context:(void *) action];
  22. }
  23. - (void)observeValueForKeyPath:(NSString *)keyPath
  24. ofObject:(id)object
  25. change:(NSDictionary<NSKeyValueChangeKey, id> *)change
  26. context:(void *)context
  27. {
  28. if ([keyPath isEqualToString:NSStringFromSelector(@selector(canCheckForUpdates))]) {
  29. QAction *menuAction = (QAction *) context;
  30. menuAction->setEnabled(_updaterController.updater.canCheckForUpdates);
  31. } else {
  32. [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
  33. }
  34. }
  35. - (void)dealloc
  36. {
  37. @autoreleasepool {
  38. [_updaterController.updater removeObserver:self forKeyPath:NSStringFromSelector(@selector(canCheckForUpdates))];
  39. }
  40. }
  41. @end
  42. OBSSparkle::OBSSparkle(const char *branch, QAction *checkForUpdatesAction)
  43. {
  44. @autoreleasepool {
  45. updaterDelegate = [[OBSUpdateDelegate alloc] init];
  46. updaterDelegate.branch = [NSString stringWithUTF8String:branch];
  47. updaterDelegate.updaterController =
  48. [[SPUStandardUpdaterController alloc] initWithStartingUpdater:YES updaterDelegate:updaterDelegate
  49. userDriverDelegate:nil];
  50. [updaterDelegate observeCanCheckForUpdatesWithAction:checkForUpdatesAction];
  51. }
  52. }
  53. void OBSSparkle::setBranch(const char *branch)
  54. {
  55. updaterDelegate.branch = [NSString stringWithUTF8String:branch];
  56. }
  57. void OBSSparkle::checkForUpdates(bool manualCheck)
  58. {
  59. @autoreleasepool {
  60. if (manualCheck) {
  61. [updaterDelegate.updaterController checkForUpdates:nil];
  62. } else {
  63. [updaterDelegate.updaterController.updater checkForUpdatesInBackground];
  64. }
  65. }
  66. }