Add support for "waitdays" attribute to control the "check for updates" frequency from the server.

This commit is contained in:
David Capello 2011-07-27 22:08:53 -03:00
parent d22d46fd2b
commit f2b92d8107
4 changed files with 43 additions and 2 deletions

View File

@ -29,6 +29,7 @@
#include "widgets/statebar.h"
#include <sstream>
#include <ctime>
namespace app {
@ -82,11 +83,27 @@ private:
};
CheckUpdateThreadLauncher::CheckUpdateThreadLauncher()
: m_received(false)
: m_doCheck(true)
, m_received(false)
, m_guiMonitor(NULL)
, m_inits(get_config_int("Updater", "Inits", 0))
, m_exits(get_config_int("Updater", "Exits", 0))
{
// 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) {
// 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 &&
now > lastCheck) { // <- Avoid broken clocks
// So we do not check for updates.
m_doCheck = false;
}
}
// Minimal stats: number of initializations
set_config_int("Updater", "Inits", get_config_int("Updater", "Inits", 0)+1);
flush_config_file();
@ -113,6 +130,11 @@ CheckUpdateThreadLauncher::~CheckUpdateThreadLauncher()
void CheckUpdateThreadLauncher::launch()
{
// In this case we are in the "wait days" period, so we don't check
// for updates.
if (!m_doCheck)
return;
if (m_uuid.empty())
m_uuid = get_config_string("Updater", "Uuid", "");
@ -153,9 +175,15 @@ void CheckUpdateThreadLauncher::monitorActivity()
if (!m_response.getUuid().empty()) {
m_uuid = m_response.getUuid();
set_config_string("Updater", "Uuid", m_uuid.c_str());
flush_config_file();
}
// 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());
// Save the config file right now
flush_config_file();
// Remove the monitor
remove_gui_monitor(m_guiMonitor);
m_guiMonitor = NULL;

View File

@ -55,6 +55,7 @@ namespace app {
updater::Uuid m_uuid;
UniquePtr<base::thread> m_thread;
UniquePtr<CheckUpdateBackgroundJob> m_bgJob;
bool m_doCheck;
bool m_received;
updater::CheckUpdateResponse m_response;
Monitor* m_guiMonitor;

View File

@ -37,6 +37,7 @@ namespace updater {
CheckUpdateResponse::CheckUpdateResponse()
: m_type(Unknown)
, m_waitDays(0)
{
}
@ -44,11 +45,13 @@ CheckUpdateResponse::CheckUpdateResponse(const CheckUpdateResponse& other)
: m_type(other.m_type)
, m_version(other.m_version)
, m_url(other.m_url)
, m_waitDays(0)
{
}
CheckUpdateResponse::CheckUpdateResponse(const std::string& responseBody)
: m_type(Unknown)
, m_waitDays(0)
{
TiXmlDocument doc;
doc.Parse(responseBody.c_str());
@ -65,6 +68,7 @@ CheckUpdateResponse::CheckUpdateResponse(const std::string& responseBody)
const char* type_attr = xmlUpdate->Attribute("type");
const char* url_attr = xmlUpdate->Attribute("url");
const char* uuid_attr = xmlUpdate->Attribute("uuid");
const char* waitdays_attr = xmlUpdate->Attribute("waitdays");
if (latest_attr && strcmp(latest_attr, "1") == 0)
m_type = NoUpdate;
@ -84,6 +88,9 @@ CheckUpdateResponse::CheckUpdateResponse(const std::string& responseBody)
if (uuid_attr)
m_uuid = uuid_attr;
if (waitdays_attr)
m_waitDays = base::convert_to<int>(std::string(waitdays_attr));
}
class CheckUpdate::CheckUpdateImpl

View File

@ -55,11 +55,16 @@ namespace updater {
// UUID.
Uuid getUuid() const { return m_uuid; }
// Returns the number of days that this client should wait for the
// next "check for updates".
int getWaitDays() const { return m_waitDays; }
private:
Type m_type;
base::Version m_version;
std::string m_url;
Uuid m_uuid;
int m_waitDays;
};
// Delegate called by CheckUpdate when the request to the server is