Skip to content

Commit

Permalink
Refactor Sparkle updates checking to its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
vslavik committed Feb 26, 2024
1 parent 4126c29 commit 0524913
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 134 deletions.
16 changes: 8 additions & 8 deletions Poedit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@
B28F1CF116F629D30018AF7E /* findframe.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B28F1CC216F629D30018AF7E /* findframe.cpp */; };
B28F1CF216F629D30018AF7E /* gexecute.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B28F1CC416F629D30018AF7E /* gexecute.cpp */; };
B28F1CF516F629D30018AF7E /* manager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B28F1CCA16F629D30018AF7E /* manager.cpp */; };
B28F1CF616F629D30018AF7E /* macos_helpers.mm in Sources */ = {isa = PBXBuildFile; fileRef = B28F1CCD16F629D30018AF7E /* macos_helpers.mm */; };
B28F1CF816F629D30018AF7E /* prefsdlg.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B28F1CD016F629D30018AF7E /* prefsdlg.cpp */; };
B28F1CF916F629D30018AF7E /* progressinfo.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B28F1CD216F629D30018AF7E /* progressinfo.cpp */; };
B28F1CFA16F629D30018AF7E /* propertiesdlg.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B28F1CD416F629D30018AF7E /* propertiesdlg.cpp */; };
Expand Down Expand Up @@ -202,6 +201,7 @@
B2EC60A41812D9D40059756A /* icudt57l.dat in Resources */ = {isa = PBXBuildFile; fileRef = B2EC60A31812D9D40059756A /* icudt57l.dat */; };
B2F25F0D199E327C00127FF9 /* spellchecking.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B2F25F0C199E327C00127FF9 /* spellchecking.cpp */; };
B2F287C0170DCC63004C1F41 /* CoreFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B248B2DF170D765100EBA58E /* CoreFoundation.framework */; };
B2F3C2912B891644008E0593 /* app_updates.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B2F3C28F2B891644008E0593 /* app_updates.cpp */; };
B2FE296B1AC85FFF005588E4 /* keytar_mac.cc in Sources */ = {isa = PBXBuildFile; fileRef = B2FE29691AC85FFF005588E4 /* keytar_mac.cc */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -470,8 +470,6 @@
B28F1CC516F629D30018AF7E /* gexecute.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = gexecute.h; sourceTree = "<group>"; };
B28F1CCA16F629D30018AF7E /* manager.cpp */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.objcpp; fileEncoding = 4; path = manager.cpp; sourceTree = "<group>"; };
B28F1CCB16F629D30018AF7E /* manager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = manager.h; sourceTree = "<group>"; };
B28F1CCC16F629D30018AF7E /* macos_helpers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = macos_helpers.h; sourceTree = "<group>"; };
B28F1CCD16F629D30018AF7E /* macos_helpers.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = macos_helpers.mm; sourceTree = "<group>"; };
B28F1CD016F629D30018AF7E /* prefsdlg.cpp */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.objcpp; fileEncoding = 4; path = prefsdlg.cpp; sourceTree = "<group>"; };
B28F1CD116F629D30018AF7E /* prefsdlg.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = prefsdlg.h; sourceTree = "<group>"; };
B28F1CD216F629D30018AF7E /* progressinfo.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = progressinfo.cpp; sourceTree = "<group>"; };
Expand Down Expand Up @@ -618,6 +616,8 @@
B2EC60A31812D9D40059756A /* icudt57l.dat */ = {isa = PBXFileReference; lastKnownFileType = file; name = icudt57l.dat; path = deps/icu4c/source/data/in/icudt57l.dat; sourceTree = "<group>"; };
B2F25F0B199E23B300127FF9 /* spellchecking.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = spellchecking.h; sourceTree = "<group>"; };
B2F25F0C199E327C00127FF9 /* spellchecking.cpp */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.objcpp; fileEncoding = 4; path = spellchecking.cpp; sourceTree = "<group>"; };
B2F3C28F2B891644008E0593 /* app_updates.cpp */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.objcpp; fileEncoding = 4; path = app_updates.cpp; sourceTree = "<group>"; };
B2F3C2902B891644008E0593 /* app_updates.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = app_updates.h; sourceTree = "<group>"; };
B2F672D6197A83F900DBFDB0 /* da */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.strings; name = da; path = da.lproj/MoveApplication.strings; sourceTree = "<group>"; };
B2F672D7197A83FE00DBFDB0 /* de */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.strings; name = de; path = de.lproj/MoveApplication.strings; sourceTree = "<group>"; };
B2F672D8197A840400DBFDB0 /* es */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.strings; name = es; path = es.lproj/MoveApplication.strings; sourceTree = "<group>"; };
Expand Down Expand Up @@ -758,6 +758,8 @@
isa = PBXGroup;
children = (
B25504A51A2201CC00EE2CEE /* Native */,
B2F3C28F2B891644008E0593 /* app_updates.cpp */,
B2F3C2902B891644008E0593 /* app_updates.h */,
B28F1CB116F629D30018AF7E /* attentionbar.cpp */,
B28F1CB216F629D30018AF7E /* attentionbar.h */,
B28F1CD616F629D30018AF7E /* cat_update.cpp */,
Expand Down Expand Up @@ -813,8 +815,6 @@
B28F1CCB16F629D30018AF7E /* manager.h */,
B26E2C8425A24541008D6DF1 /* menus.cpp */,
B26E2C8525A24541008D6DF1 /* menus.h */,
B28F1CCC16F629D30018AF7E /* macos_helpers.h */,
B28F1CCD16F629D30018AF7E /* macos_helpers.mm */,
B28F1CD016F629D30018AF7E /* prefsdlg.cpp */,
B28F1CD116F629D30018AF7E /* prefsdlg.h */,
B2A3637A1E4B9DC800E96253 /* pretranslate.cpp */,
Expand Down Expand Up @@ -1515,6 +1515,7 @@
B25504A81A2201E800EE2CEE /* main_toolbar.cpp in Sources */,
B2D52B8F1DEC785700E27B35 /* custom_buttons.cpp in Sources */,
B28F1CE616F629D30018AF7E /* catalog.cpp in Sources */,
B2F3C2912B891644008E0593 /* app_updates.cpp in Sources */,
B2C21633251906CC002B144A /* recent_files.cpp in Sources */,
B28602441DDB279400FCA617 /* colorscheme.cpp in Sources */,
B28F1CE716F629D30018AF7E /* edapp.cpp in Sources */,
Expand Down Expand Up @@ -1548,7 +1549,6 @@
B2A3637C1E4B9DC800E96253 /* pretranslate.cpp in Sources */,
B28F1CF516F629D30018AF7E /* manager.cpp in Sources */,
B212FEED20A7356300FAC68F /* pl_evaluate.cpp in Sources */,
B28F1CF616F629D30018AF7E /* macos_helpers.mm in Sources */,
B240FFC719C6F1A600777AFE /* suggestions.cpp in Sources */,
B2BC828B20A1F0DC007652D6 /* catalog_po.cpp in Sources */,
B2380F9A1A9B821200B7D8C9 /* crowdin_gui.cpp in Sources */,
Expand Down Expand Up @@ -1967,7 +1967,7 @@
"-licudata",
"-licui18n",
);
POEDIT_FEATURES = "USE_SPARKLE HAVE_CLD2 HAVE_HTTP_CLIENT";
POEDIT_FEATURES = "HAVE_CLD2 HAVE_HTTP_CLIENT";
PRODUCT_BUNDLE_IDENTIFIER = "net.poedit.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = "$(TARGET_NAME)";
WRAPPER_EXTENSION = app;
Expand All @@ -1991,7 +1991,7 @@
"-licudata",
"-licui18n",
);
POEDIT_FEATURES = "USE_SPARKLE HAVE_CLD2 HAVE_HTTP_CLIENT";
POEDIT_FEATURES = "HAVE_CLD2 HAVE_HTTP_CLIENT";
PRODUCT_BUNDLE_IDENTIFIER = "net.poedit.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = "$(TARGET_NAME)";
WRAPPER_EXTENSION = app;
Expand Down
146 changes: 85 additions & 61 deletions src/macos_helpers.mm → src/app_updates.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of Poedit (https://poedit.net)
*
* Copyright (C) 2007-2023 Vaclav Slavik
* Copyright (C) 2024 Vaclav Slavik
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
Expand All @@ -19,30 +19,20 @@
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
* DEALINGS IN THE SOFTWARE.
*
*/

#include "macos_helpers.h"
#include "app_updates.h"

#include "edapp.h"
#ifdef HAS_UPDATES_CHECK

#import <Foundation/NSString.h>
#import <Foundation/NSUserDefaults.h>
#import <Foundation/NSAutoreleasePool.h>
#ifdef __WXOSX__

#import <AppKit/NSApplication.h>
#import <AppKit/NSButton.h>
#import <AppKit/NSSpellChecker.h>
#include <wx/menu.h>

#import "PFMoveApplication.h"

#ifdef USE_SPARKLE
#import <Sparkle/Sparkle.h>

// --------------------------------------------------------------------------------
// Sparkle helpers
// --------------------------------------------------------------------------------

@interface PoeditSparkleDelegate : NSObject <SUUpdaterDelegate>
@end
Expand All @@ -52,7 +42,7 @@ @implementation PoeditSparkleDelegate
- (NSArray *)feedParametersForUpdater:(SUUpdater *)updater sendingSystemProfile:(BOOL)sendingProfile
{
#pragma unused(updater, sendingProfile)
if (wxGetApp().CheckForBetaUpdates())
if (Config::CheckForBetaUpdates())
{
return @[ @{
@"key": @"beta",
Expand All @@ -70,9 +60,19 @@ - (NSArray *)feedParametersForUpdater:(SUUpdater *)updater sendingSystemProfile:
@end


NSObject *Sparkle_Initialize()
class AppUpdates::impl
{
@autoreleasepool {
public:
impl() {}

~impl()
{
// Make sure that Sparkle's updates to plist preferences are saved:
[[NSUserDefaults standardUserDefaults] synchronize];
}

void Initialize()
{
// Poedit < 2.0 stored this in preferences, which was wrong - it overrode
// changes to Info.plist. Undo the damage:
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"SUFeedURL"];
Expand All @@ -81,79 +81,103 @@ - (NSArray *)feedParametersForUpdater:(SUUpdater *)updater sendingSystemProfile:
NSDictionary *sparkleDefaults = @{ @"SUEnableAutomaticChecks": @YES };
[[NSUserDefaults standardUserDefaults] registerDefaults:sparkleDefaults];

NSObject<SUUpdaterDelegate> *delegate = [PoeditSparkleDelegate new];
SUUpdater.sharedUpdater.delegate = delegate;
return delegate;
m_sparkleDelegate = [PoeditSparkleDelegate new];
SUUpdater.sharedUpdater.delegate = m_sparkleDelegate;
}
}

void EnableAutomaticChecks(bool enable)
{
SetBoolValue("SUEnableAutomaticChecks", enable);
}

void Sparkle_AddMenuItem(NSMenu *appmenu, const char *title)
{
@autoreleasepool {
NSString *nstitle = [NSString stringWithUTF8String: title];
bool AutomaticChecksEnabled() const
{
return GetBoolValue("SUEnableAutomaticChecks");
}

void AddMenuItem(wxMenu *appleMenu)
{
NSString *nstitle = [NSString stringWithUTF8String: _(L"Check for Updates…").utf8_str()];
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:nstitle
action:@selector(checkForUpdates:)
keyEquivalent:@""];
SUUpdater *updater = [SUUpdater sharedUpdater];
[item setEnabled:YES];
[item setTarget:updater];
[appmenu insertItem:item atIndex:1];
[appleMenu->GetHMenu() insertItem:item atIndex:1];
}
}

private:
bool GetBoolValue(const char *key) const
{
NSString *nskey = [NSString stringWithUTF8String: key];
return (bool)[[NSUserDefaults standardUserDefaults] boolForKey:nskey];
}

void Sparkle_Cleanup()
{
/* Make sure that Sparkle's updates to plist preferences are saved: */
@autoreleasepool {
void SetBoolValue(const char *key, int value)
{
NSString *nskey = [NSString stringWithUTF8String: key];

[[NSUserDefaults standardUserDefaults] setBool:value forKey:nskey];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
#endif // USE_SPARKLE

private:
NSObject<SUUpdaterDelegate> *m_sparkleDelegate = nullptr;
};

#endif // __WXOSX__

// --------------------------------------------------------------------------------
// Native preferences
// --------------------------------------------------------------------------------

int UserDefaults_GetBoolValue(const char *key)

// Boilerplate:

namespace
{
@autoreleasepool {
NSString *nskey = [NSString stringWithUTF8String: key];
return [[NSUserDefaults standardUserDefaults] boolForKey:nskey];
}
static std::once_flag initializationFlag;
AppUpdates* gs_instance = nullptr;
}


void UserDefaults_SetBoolValue(const char *key, int value)
AppUpdates& AppUpdates::Get()
{
@autoreleasepool {
NSString *nskey = [NSString stringWithUTF8String: key];
std::call_once(initializationFlag, []() {
gs_instance = new AppUpdates;
});
return *gs_instance;
}

[[NSUserDefaults standardUserDefaults] setBool:value forKey:nskey];
[[NSUserDefaults standardUserDefaults] synchronize];
void AppUpdates::CleanUp()
{
if (gs_instance)
{
delete gs_instance;
gs_instance = nullptr;
}
}

AppUpdates::AppUpdates() : m_impl(new impl) {}
AppUpdates::~AppUpdates() {}

void UserDefaults_RemoveValue(const char *key)
void AppUpdates::Initialize()
{
@autoreleasepool {
NSString *nskey = [NSString stringWithUTF8String: key];

[[NSUserDefaults standardUserDefaults] removeObjectForKey:nskey];
[[NSUserDefaults standardUserDefaults] synchronize];
}
m_impl->Initialize();
}

void AppUpdates::EnableAutomaticChecks(bool enable)
{
m_impl->EnableAutomaticChecks(enable);
}

// --------------------------------------------------------------------------------
// Misc UI helpers
// --------------------------------------------------------------------------------
bool AppUpdates::AutomaticChecksEnabled() const
{
return m_impl->AutomaticChecksEnabled();
}

void MoveToApplicationsFolderIfNecessary()
#ifdef __WXOSX__
void AppUpdates::AddMenuItem(wxMenu *appleMenu)
{
PFMoveToApplicationsFolderIfNecessary();
m_impl->AddMenuItem(appleMenu);
}
#endif

#endif // HAS_UPDATES_CHECK
60 changes: 41 additions & 19 deletions src/macos_helpers.h → src/app_updates.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of Poedit (https://poedit.net)
*
* Copyright (C) 2007-2023 Vaclav Slavik
* Copyright (C) 2024 Vaclav Slavik
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
Expand All @@ -19,29 +19,51 @@
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
* DEALINGS IN THE SOFTWARE.
*
*/

#ifndef Poedit_macos_helpers_h
#define Poedit_macos_helpers_h
#ifndef Poedit_app_updates_h
#define Poedit_app_updates_h

// FIXME: This is a hack to work around Automake's lack of support for ObjC++.
// Remove it after switching build system to Bakefile.
#if defined(__WXMSW__) || defined(__WXOSX__)
#define HAS_UPDATES_CHECK
#endif

#ifdef USE_SPARKLE
// Sparkle helpers
NSObject *Sparkle_Initialize();
void Sparkle_AddMenuItem(NSMenu *appmenu, const char *title);
void Sparkle_Cleanup();
#endif // USE_SPARKLE
class WXDLLIMPEXP_FWD_CORE wxMenu;

// Native preferences
void UserDefaults_SetBoolValue(const char *key, int value);
int UserDefaults_GetBoolValue(const char *key);
void UserDefaults_RemoveValue(const char *key);
#include <memory>

// Misc UI helpers
void MoveToApplicationsFolderIfNecessary();
#ifdef HAS_UPDATES_CHECK

#endif // Poedit_macos_helpers_h
/// Management of app updates.
class AppUpdates
{
public:
/// Return singleton instance of the manager.
static AppUpdates& Get();

// Initialize and start checking for updates (if allowed by the user).
void Initialize();

/// Destroys the singleton, must be called (only) on app shutdown.
static void CleanUp();

void EnableAutomaticChecks(bool enable);
bool AutomaticChecksEnabled() const;

#ifdef __WXOSX__
void AddMenuItem(wxMenu *appleMenu);
#endif

private:
AppUpdates();
~AppUpdates();

class impl;
std::unique_ptr<impl> m_impl;
};

#endif // HAS_UPDATES_CHECK

#endif // Poedit_app_updates_h
4 changes: 0 additions & 4 deletions src/attentionbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@
#include <wx/dcclient.h>
#include <wx/wupdlock.h>

#ifdef __WXOSX__
#include "macos_helpers.h"
#endif

#ifdef __WXOSX__
#define SMALL_BORDER PX(7)
#define BUTTONS_SPACE PX(10)
Expand Down
Loading

0 comments on commit 0524913

Please sign in to comment.