MCPcopy Create free account
hub / github.com/Tablecruncher/tablecruncher / checkUpdate

Method checkUpdate

src/csvapplication.cpp:3268–3327  ·  view source on GitHub ↗

checkUpdate(bool ignore = false) Check for updates. If 'ignore' is true, ignores if update checks are not allowed and also ignores 'daysSinceLastCheck' */

Source from the content-addressed store, hash-verified

3266 also ignores 'daysSinceLastCheck'
3267*/
3268void CsvApplication::checkUpdate(bool ignore) {
3269 if( !ignore && !isUpdateCheckAllowed() ) {
3270 #ifdef DEBUG
3271 printf("No update checks allowed\n");
3272 #endif
3273 return;
3274 }
3275 std::string updateUrl("https://tablecruncher.com/?utm_source=appUpdateWindow&utm_medium=");
3276 updateUrl += std::string(MY_VERSION);
3277 bool newerVersion = false;
3278
3279 std::string prefLastCheck = getPreference(&preferences, TCRUNCHER_PREF_LAST_UPDATE_CHECK, "0");
3280 long lastCheckTs = 0;
3281 try {
3282 lastCheckTs = std::stol(prefLastCheck);
3283 } catch(...) {};
3284 long nowTs = static_cast<long>(std::time(nullptr));
3285 std::string nowString = std::to_string(nowTs);
3286 float daysSinceLastCheck = (nowTs - lastCheckTs) / (3600*24);
3287 #ifdef DEBUG
3288 printf("Now: %ld, lastCheck: %ld, days: %.2f\n", nowTs, lastCheckTs, daysSinceLastCheck);
3289 ignore = true;
3290 #endif
3291
3292 if( ignore || daysSinceLastCheck > TCRUNCHER_PREF_UPDATE_CHECK_DAYS ) {
3293 auto timeout = std::chrono::milliseconds(TCRUNCHER_VERSION_CHECK_TIMEOUT);
3294 httplib::Client cli(TCRUNCHER_VERSION_CHECK_HOST, 80);
3295 cli.set_connection_timeout(timeout);
3296 cli.set_read_timeout(timeout);
3297 cli.set_write_timeout(timeout);
3298 auto versionResponse = cli.Get(TCRUNCHER_VERSION_CHECK_PATH);
3299 if( versionResponse ) {
3300 #ifdef DEBUG
3301 std::cerr << "MY_VERSION: " << std::string(MY_VERSION) << std::endl;
3302 #endif
3303 if( versionResponse->status == 200 ) {
3304 #ifdef DEBUG
3305 std::cerr << "SERVER_VERSION: " << versionResponse->body << std::endl;
3306 #endif
3307 if( Helper::isUpdateAvailable(std::string(MY_VERSION), versionResponse->body) ) {
3308 newerVersion = true;
3309 CsvApplication::myFlChoice("Update Notice", "A newer version is available. Please visit <a href=\""+updateUrl+"\">tablecruncher.com</a> to download it.", {"Okay"});
3310 #ifdef DEBUG
3311 std::cerr << "A new version " + versionResponse->body + " is available." << std::endl;
3312 #endif
3313 }
3314 preferences.set(TCRUNCHER_PREF_LAST_UPDATE_CHECK, nowString.c_str());
3315 } else {
3316 #ifdef DEBUG
3317 std::cerr << "ERROR: " << versionResponse->status << std::endl;
3318 #endif
3319 }
3320 }
3321 if( !newerVersion && ignore ) {
3322 CsvApplication::myFlChoice("Update Notice", "No newer version available.", {"Okay"});
3323 }
3324
3325 }

Callers 2

mainFunction · 0.80
checkUpdateCBMethod · 0.80

Calls 6

to_stringFunction · 0.85
set_read_timeoutMethod · 0.80
set_write_timeoutMethod · 0.80
GetMethod · 0.80
setMethod · 0.80

Tested by

no test coverage detected