Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to auto install after download when using win_sparkle_check_update_with_ui() #229

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/psdk/app_psdk.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void OnCheckForUpdates(HWND hwnd,
always shows visible UI. You only need to call it if you have
a "Check for updates" menu item, otherwise win_sparkle_init()
checks for updates automatically. */
win_sparkle_check_update_with_ui();
win_sparkle_check_update_with_ui(0);
}
}

Expand Down
5 changes: 4 additions & 1 deletion include/winsparkle.h
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,9 @@ WIN_SPARKLE_API void __cdecl win_sparkle_set_user_run_installer_callback(win_spa
the user is told so. If there is an update, the usual "update available"
window is shown.

Pass a non-zero value for @a auto_install to skip the "install"
confirmation after download and automatically install.

This function returns immediately.

@note Because this function is intended for manual, user-initiated checks
Expand All @@ -561,7 +564,7 @@ WIN_SPARKLE_API void __cdecl win_sparkle_set_user_run_installer_callback(win_spa

@see win_sparkle_check_update_without_ui()
*/
WIN_SPARKLE_API void __cdecl win_sparkle_check_update_with_ui();
WIN_SPARKLE_API void __cdecl win_sparkle_check_update_with_ui(int auto_install);

/**
Checks if an update is available, showing progress UI to the user and
Expand Down
4 changes: 2 additions & 2 deletions src/dll_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,15 +373,15 @@ WIN_SPARKLE_API void __cdecl win_sparkle_set_update_dismissed_callback(win_spark
Manual usage
*--------------------------------------------------------------------------*/

WIN_SPARKLE_API void __cdecl win_sparkle_check_update_with_ui()
WIN_SPARKLE_API void __cdecl win_sparkle_check_update_with_ui(int auto_install)
{
try
{
// Initialize UI thread and show progress indicator.
UI::ShowCheckingUpdates();

// Then run the actual check in the background.
UpdateChecker *check = new ManualUpdateChecker();
UpdateChecker *check = new ManualUpdateChecker(auto_install);
check->Start();
}
CATCH_ALL_EXCEPTIONS
Expand Down
28 changes: 17 additions & 11 deletions src/ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ struct EventPayload
Appcast appcast;
size_t sizeDownloaded, sizeTotal;
std::wstring updateFile;
bool downloadAutomatically;
bool installAutomatically;
ErrorCode error;
};
Expand Down Expand Up @@ -425,7 +426,7 @@ class UpdateDialog : public WinSparkleDialog
// change state into "update error"
void StateUpdateError(ErrorCode err);
// change state into "a new version is available"
void StateUpdateAvailable(const Appcast& info, bool installAutomatically);
void StateUpdateAvailable(const Appcast& info, bool downloadAutomatically, bool installAutomatically);
// change state into "downloading update"
void StateDownloading();
// update download progress
Expand Down Expand Up @@ -476,6 +477,8 @@ class UpdateDialog : public WinSparkleDialog
std::string m_installerArguments;
// downloader (only valid between OnInstall and OnUpdateDownloaded)
UpdateDownloader* m_downloader;
// whether the update should be downloaded without prompting the user
bool m_downloadAutomatically;
// whether the update should be installed without prompting the user
bool m_installAutomatically;
// whether an error occurred (used to properly call NotifyUpdateCancelled)
Expand All @@ -490,6 +493,7 @@ UpdateDialog::UpdateDialog()
: m_timer(this),
m_downloader(NULL)
{
m_downloadAutomatically = false;
m_installAutomatically = false;
m_errorOccurred = false;

Expand Down Expand Up @@ -757,11 +761,11 @@ void UpdateDialog::StateCheckingUpdates()
}


void UpdateDialog::StateNoUpdateFound(bool installAutomatically)
void UpdateDialog::StateNoUpdateFound(bool downloadAutomatically)
{
m_installAutomatically = installAutomatically;
m_downloadAutomatically = downloadAutomatically;

if ( m_installAutomatically )
if (m_downloadAutomatically)
{
Close();
return;
Expand Down Expand Up @@ -840,12 +844,13 @@ void UpdateDialog::StateUpdateError(ErrorCode err)



void UpdateDialog::StateUpdateAvailable(const Appcast& info, bool installAutomatically)
void UpdateDialog::StateUpdateAvailable(const Appcast& info, bool downloadAutomatically, bool installAutomatically)
{
m_appcast = info;
m_downloadAutomatically = downloadAutomatically;
m_installAutomatically = installAutomatically;

if ( installAutomatically )
if (downloadAutomatically)
{
wxCommandEvent nullEvent;
OnInstall(nullEvent);
Expand Down Expand Up @@ -1362,7 +1367,7 @@ void App::OnNoUpdateFound(wxThreadEvent& event)
if ( m_win )
{
EventPayload payload(event.GetPayload<EventPayload>());
m_win->StateNoUpdateFound(payload.installAutomatically);
m_win->StateNoUpdateFound(payload.downloadAutomatically);
}
}

Expand Down Expand Up @@ -1400,7 +1405,7 @@ void App::OnUpdateAvailable(wxThreadEvent& event)
InitWindow();

EventPayload payload(event.GetPayload<EventPayload>());
m_win->StateUpdateAvailable(payload.appcast, payload.installAutomatically);
m_win->StateUpdateAvailable(payload.appcast, payload.downloadAutomatically, payload.installAutomatically);

ShowWindow();
}
Expand Down Expand Up @@ -1532,7 +1537,7 @@ void UI::ShutDown()


/*static*/
void UI::NotifyNoUpdates(bool installAutomatically)
void UI::NotifyNoUpdates(bool downloadAutomatically)
{
ApplicationController::NotifyUpdateNotFound();

Expand All @@ -1542,19 +1547,20 @@ void UI::NotifyNoUpdates(bool installAutomatically)
return;

EventPayload payload;
payload.installAutomatically = installAutomatically;
payload.downloadAutomatically = downloadAutomatically;
uit.App().SendMsg(MSG_NO_UPDATE_FOUND, &payload);
}


/*static*/
void UI::NotifyUpdateAvailable(const Appcast& info, bool installAutomatically)
void UI::NotifyUpdateAvailable(const Appcast& info, bool downloadAutomatically, bool installAutomatically)
{
ApplicationController::NotifyUpdateFound();

UIThreadAccess uit;
EventPayload payload;
payload.appcast = info;
payload.downloadAutomatically = downloadAutomatically;
payload.installAutomatically = installAutomatically;
uit.App().SendMsg(MSG_UPDATE_AVAILABLE, &payload);
}
Expand Down
4 changes: 2 additions & 2 deletions src/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class UI : public Thread
(i.e. the user is manually checking for updates), "no updates found"
message is shown. Otherwise, does nothing.
*/
static void NotifyNoUpdates(bool installAutomatically);
static void NotifyNoUpdates(bool downloadAutomatically);

/**
Notifies the UI that there was an error retrieving updates.
Expand All @@ -82,7 +82,7 @@ class UI : public Thread

If the UI thread isn't running yet, it will be launched.
*/
static void NotifyUpdateAvailable(const Appcast& info, bool installAutomatically);
static void NotifyUpdateAvailable(const Appcast& info, bool downloadAutomatically, bool installAutomatically);

/**
Notifies the UI about download progress.
Expand Down
6 changes: 3 additions & 3 deletions src/updatechecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,18 +245,18 @@ void UpdateChecker::PerformUpdateCheck()
if ( !appcast.IsValid() || CompareVersions(currentVersion, appcast.Version) >= 0 )
{
// The same or newer version is already installed.
UI::NotifyNoUpdates(ShouldAutomaticallyInstall());
UI::NotifyNoUpdates(ShouldAutomaticallyDownload());
return;
}

// Check if the user opted to ignore this particular version.
if ( ShouldSkipUpdate(appcast) )
{
UI::NotifyNoUpdates(ShouldAutomaticallyInstall());
UI::NotifyNoUpdates(ShouldAutomaticallyDownload());
return;
}

UI::NotifyUpdateAvailable(appcast, ShouldAutomaticallyInstall());
UI::NotifyUpdateAvailable(appcast, ShouldAutomaticallyDownload(), ShouldAutomaticallyInstall());
}
catch ( ... )
{
Expand Down
14 changes: 11 additions & 3 deletions src/updatechecker.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ class UpdateChecker : public Thread
/// Should give version be ignored?
virtual bool ShouldSkipUpdate(const Appcast& appcast) const;

/// Should we install the update or prompt the user for options first?
/// Should we download the update or prompt the user for options first?
virtual bool ShouldAutomaticallyDownload() const { return false; }
/// Should we install the update after download or require action from the user first?
virtual bool ShouldAutomaticallyInstall() const { return false; }

protected:
Expand Down Expand Up @@ -95,15 +97,20 @@ class ManualUpdateChecker : public OneShotUpdateChecker
{
public:
/// Creates checker thread.
ManualUpdateChecker(bool autoInstall) : OneShotUpdateChecker(), m_autoInstall(autoInstall) {}
ManualUpdateChecker() : OneShotUpdateChecker() {}

protected:
virtual bool ShouldSkipUpdate(const Appcast& appcast) const;
virtual bool ShouldAutomaticallyInstall() const { return m_autoInstall; }

private:
bool m_autoInstall{false};
};


/**
Update checker used to automatically install updates when found.
Update checker used to automatically download and install updates when found.
*/
class ManualAutoInstallUpdateChecker : public ManualUpdateChecker
{
Expand All @@ -112,7 +119,8 @@ class ManualAutoInstallUpdateChecker : public ManualUpdateChecker
ManualAutoInstallUpdateChecker() : ManualUpdateChecker() {}

protected:
virtual bool ShouldAutomaticallyInstall() const { return true; };
virtual bool ShouldAutomaticallyDownload() const { return true; }
virtual bool ShouldAutomaticallyInstall() const { return true; }
};


Expand Down