Skip to content

Commit

Permalink
Support floating point number of days to wait for next "check updates"
Browse files Browse the repository at this point in the history
  • Loading branch information
dacap committed Feb 28, 2015
1 parent bf27db1 commit 6c826a6
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 12 deletions.
8 changes: 4 additions & 4 deletions src/app/check_update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ CheckUpdateThreadLauncher::CheckUpdateThreadLauncher(CheckUpdateDelegate* delega
, m_timer(kMonitoringPeriod, NULL)
{
// Get how many days we have to wait for the next "check for update"
int waitDays = get_config_int("Updater", "WaitDays", 0);
if (waitDays > 0) {
double waitDays = get_config_double("Updater", "WaitDays", 0.0);
if (waitDays > 0.0) {
// Get the date of the last "check for updates"
time_t lastCheck = (time_t)get_config_int("Updater", "LastCheck", 0);
time_t now = std::time(NULL);

// Verify if we are in the "WaitDays" period...
if (now < lastCheck+60*60*24*waitDays &&
if (now < lastCheck+int(double(60*60*24*waitDays)) &&
now > lastCheck) { // <- Avoid broken clocks
// So we do not check for updates.
m_doCheck = false;
Expand Down Expand Up @@ -181,7 +181,7 @@ void CheckUpdateThreadLauncher::onMonitoringTick()

// Set the date of the last "check for updates" and the "WaitDays" parameter.
set_config_int("Updater", "LastCheck", (int)std::time(NULL));
set_config_int("Updater", "WaitDays", m_response.getWaitDays());
set_config_double("Updater", "WaitDays", m_response.getWaitDays());

// Save the config file right now
flush_config_file();
Expand Down
14 changes: 13 additions & 1 deletion src/base/convert_to.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Aseprite Base Library
// Copyright (c) 2001-2013 David Capello
// Copyright (c) 2001-2015 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
Expand Down Expand Up @@ -28,6 +28,18 @@ template<> std::string convert_to(const int& from)
return buf;
}

template<> double convert_to(const std::string& from)
{
return std::strtod(from.c_str(), NULL);
}

template<> std::string convert_to(const double& from)
{
char buf[32];
std::sprintf(buf, "%g", from);
return buf;
}

template<> Sha1 convert_to(const std::string& from)
{
std::vector<uint8_t> digest(Sha1::HashSize);
Expand Down
5 changes: 4 additions & 1 deletion src/base/convert_to.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Aseprite Base Library
// Copyright (c) 2001-2013 David Capello
// Copyright (c) 2001-2015 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
Expand All @@ -24,6 +24,9 @@ namespace base {
template<> int convert_to(const std::string& from);
template<> std::string convert_to(const int& from);

template<> double convert_to(const std::string& from);
template<> std::string convert_to(const double& from);

template<> Sha1 convert_to(const std::string& from);
template<> std::string convert_to(const Sha1& from);

Expand Down
8 changes: 4 additions & 4 deletions src/updater/check_update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,21 @@ namespace updater {

CheckUpdateResponse::CheckUpdateResponse()
: m_type(Unknown)
, m_waitDays(0)
, m_waitDays(0.0)
{
}

CheckUpdateResponse::CheckUpdateResponse(const CheckUpdateResponse& other)
: m_type(other.m_type)
, m_version(other.m_version)
, m_url(other.m_url)
, m_waitDays(0)
, m_waitDays(0.0)
{
}

CheckUpdateResponse::CheckUpdateResponse(const std::string& responseBody)
: m_type(Unknown)
, m_waitDays(0)
, m_waitDays(0.0)
{
TiXmlDocument doc;
doc.Parse(responseBody.c_str());
Expand Down Expand Up @@ -79,7 +79,7 @@ CheckUpdateResponse::CheckUpdateResponse(const std::string& responseBody)
m_uuid = uuid_attr;

if (waitdays_attr)
m_waitDays = base::convert_to<int>(std::string(waitdays_attr));
m_waitDays = base::convert_to<double>(std::string(waitdays_attr));
}

class CheckUpdate::CheckUpdateImpl
Expand Down
4 changes: 2 additions & 2 deletions src/updater/check_update.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ namespace updater {

// Returns the number of days that this client should wait for the
// next "check for updates".
int getWaitDays() const { return m_waitDays; }
double getWaitDays() const { return m_waitDays; }

private:
Type m_type;
base::Version m_version;
std::string m_url;
Uuid m_uuid;
int m_waitDays;
double m_waitDays;
};

// Delegate called by CheckUpdate when the request to the server is
Expand Down

0 comments on commit 6c826a6

Please sign in to comment.