2009-10-18 00:13:14 +02:00
|
|
|
/*
|
|
|
|
* This file is a part of TTCalc - a mathematical calculator
|
|
|
|
* and is distributed under the (new) BSD licence.
|
2009-10-18 20:59:21 +02:00
|
|
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
2009-10-18 00:13:14 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2010-02-17 18:05:00 +01:00
|
|
|
* Copyright (c) 2006-2010, Tomasz Sowa
|
2009-10-18 00:13:14 +02:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* * Neither the name Tomasz Sowa nor the names of contributors to this
|
|
|
|
* project may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
|
|
|
* THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2009-10-18 20:59:21 +02:00
|
|
|
#include "compileconfig.h"
|
2009-11-04 18:59:42 +01:00
|
|
|
|
2009-10-18 00:13:14 +02:00
|
|
|
#include <cstdlib>
|
2009-10-18 20:59:21 +02:00
|
|
|
#include <shlobj.h>
|
2009-11-04 18:59:42 +01:00
|
|
|
#include <process.h>
|
|
|
|
|
2009-10-18 00:13:14 +02:00
|
|
|
#include "update.h"
|
|
|
|
#include "programresources.h"
|
|
|
|
#include "messages.h"
|
|
|
|
#include "resource.h"
|
2009-11-03 22:00:05 +01:00
|
|
|
#include "languages.h"
|
2009-10-18 00:13:14 +02:00
|
|
|
#include "winmain.h"
|
2010-02-17 18:05:00 +01:00
|
|
|
#include "misc.h"
|
|
|
|
|
2009-10-18 00:13:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
namespace Update
|
|
|
|
{
|
2009-11-04 18:59:42 +01:00
|
|
|
bool continue_down;
|
2009-10-18 00:13:14 +02:00
|
|
|
int level;
|
|
|
|
std::string url;
|
2009-10-18 20:59:21 +02:00
|
|
|
std::string remote_file_name;
|
2009-10-18 23:00:54 +02:00
|
|
|
std::string local_file_name;
|
2009-10-18 00:13:14 +02:00
|
|
|
int major;
|
|
|
|
int minor;
|
|
|
|
int revision;
|
2009-11-09 13:46:13 +01:00
|
|
|
int prerelease;
|
2009-11-04 18:59:42 +01:00
|
|
|
|
|
|
|
// this is set by another thread at the beginning
|
|
|
|
namespace Background
|
|
|
|
{
|
|
|
|
std::string url_;
|
|
|
|
std::string remote_file_name_;
|
|
|
|
int major_;
|
|
|
|
int minor_;
|
|
|
|
int revision_;
|
2009-11-09 13:46:13 +01:00
|
|
|
int prerelease_;
|
2009-11-04 18:59:42 +01:00
|
|
|
}
|
2009-10-18 00:13:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
DownloadIni::DownloadIni()
|
|
|
|
{
|
|
|
|
file = 0;
|
|
|
|
file_name = new char[MAX_PATH];
|
|
|
|
file_name[0] = 0;
|
2009-11-04 18:59:42 +01:00
|
|
|
continue_download = 0;
|
2009-10-18 00:13:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DownloadIni::~DownloadIni()
|
|
|
|
{
|
|
|
|
if( file )
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
DeleteTmpFile();
|
|
|
|
|
|
|
|
delete [] file_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DownloadIni::DeleteTmpFile()
|
|
|
|
{
|
|
|
|
if( file_name[0] != 0 )
|
|
|
|
DeleteFile(file_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char * DownloadIni::GetFileName()
|
|
|
|
{
|
|
|
|
return file_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FILE * DownloadIni::CreateTmpFile()
|
|
|
|
{
|
|
|
|
char buf[MAX_PATH];
|
|
|
|
|
|
|
|
file = 0;
|
|
|
|
file_name[0] = 0;
|
|
|
|
|
|
|
|
if( !GetTempPath(MAX_PATH, buf) )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
|
|
if( !GetTempFileName(buf, "ttcalc_", 0, file_name) )
|
|
|
|
{
|
|
|
|
file_name[0] = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
file = fopen(file_name, "wb");
|
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadIni::GetLen(HINTERNET conn)
|
|
|
|
{
|
|
|
|
char buf[100];
|
|
|
|
DWORD buf_len = sizeof(buf);
|
|
|
|
DWORD index = 0;
|
|
|
|
|
|
|
|
if( HttpQueryInfo(conn, HTTP_QUERY_CONTENT_LENGTH, buf, &buf_len, &index) )
|
|
|
|
all_len = atoi(buf);
|
|
|
|
else
|
|
|
|
all_len = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool DownloadIni::Init()
|
|
|
|
{
|
|
|
|
if( !CreateTmpFile() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool DownloadIni::Read(char * buffer, size_t size)
|
|
|
|
{
|
|
|
|
if( !file )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
fwrite(buffer, size, 1, file);
|
|
|
|
CheckMessages();
|
|
|
|
|
2009-11-04 18:59:42 +01:00
|
|
|
if( continue_download && !(*continue_download) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2009-10-18 00:13:14 +02:00
|
|
|
}
|
|
|
|
|
2009-11-04 18:59:42 +01:00
|
|
|
bool DownloadIni::DownloadUrl(const char * url, bool * c)
|
|
|
|
{
|
|
|
|
continue_download = c;
|
|
|
|
|
|
|
|
return Download::DownloadUrl(url);
|
|
|
|
}
|
2009-10-18 00:13:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
void DownloadIni::Close()
|
|
|
|
{
|
|
|
|
if( file )
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
file = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2009-11-04 18:59:42 +01:00
|
|
|
DownloadProgram::DownloadProgram()
|
|
|
|
{
|
|
|
|
continue_download = 0;
|
|
|
|
}
|
|
|
|
|
2009-10-18 00:13:14 +02:00
|
|
|
|
|
|
|
void DownloadProgram::GetLen(HINTERNET conn)
|
|
|
|
{
|
|
|
|
char buf[100];
|
|
|
|
DWORD buf_len = sizeof(buf);
|
|
|
|
DWORD index = 0;
|
|
|
|
|
|
|
|
if( HttpQueryInfo(conn, HTTP_QUERY_CONTENT_LENGTH, buf, &buf_len, &index) )
|
|
|
|
all_len = atoi(buf);
|
|
|
|
else
|
|
|
|
all_len = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool DownloadProgram::Init()
|
|
|
|
{
|
2009-10-18 23:00:54 +02:00
|
|
|
file.open(Update::local_file_name.c_str(), std::ios_base::out | std::ios_base::binary);
|
2009-10-18 00:13:14 +02:00
|
|
|
|
|
|
|
if( !file )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool DownloadProgram::Read(char * buffer, size_t size)
|
|
|
|
{
|
|
|
|
file.write(buffer, size);
|
|
|
|
CheckMessages();
|
|
|
|
|
2009-11-04 18:59:42 +01:00
|
|
|
if( continue_download && !(*continue_download) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2009-10-18 00:13:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-04 18:59:42 +01:00
|
|
|
bool DownloadProgram::DownloadUrl(const char * url, bool * c)
|
|
|
|
{
|
|
|
|
continue_download = c;
|
|
|
|
|
|
|
|
return Download::DownloadUrl(url);
|
|
|
|
}
|
|
|
|
|
2009-10-18 00:13:14 +02:00
|
|
|
|
|
|
|
void DownloadProgram::Close()
|
|
|
|
{
|
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-02-17 18:05:00 +01:00
|
|
|
/*
|
|
|
|
we check whether the download url is correct
|
|
|
|
this is in the case of someone hacked the server with *.ini file
|
|
|
|
*/
|
|
|
|
bool IsServerCorrect(const std::string & url)
|
|
|
|
{
|
|
|
|
if( IsSubStringNoCase("http://downloads.sourceforge.net/", url.c_str()) ||
|
|
|
|
IsSubStringNoCase("ftp://ttmath.org/", url.c_str()) )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-18 00:13:14 +02:00
|
|
|
|
2009-11-04 18:59:42 +01:00
|
|
|
/*
|
|
|
|
this method is called from the other thread too
|
|
|
|
make sure you don't write directly to any global variables (from Update namespace etc)
|
2009-10-18 00:13:14 +02:00
|
|
|
*/
|
2009-11-04 18:59:42 +01:00
|
|
|
bool CheckUpdateFromIni(HWND hwnd, std::string & url_, std::string & remote_file_name_,
|
2009-11-09 13:46:13 +01:00
|
|
|
int & major_, int & minor_, int & revision_, int & prerelease_,
|
|
|
|
bool * continue_download_)
|
2009-10-18 00:13:14 +02:00
|
|
|
{
|
|
|
|
DownloadIni down_ini;
|
|
|
|
|
2009-11-04 18:59:42 +01:00
|
|
|
if( hwnd )
|
|
|
|
down_ini.AttachProgress(GetDlgItem(hwnd, IDC_PROGRESS));
|
|
|
|
|
2009-11-22 22:46:11 +01:00
|
|
|
if( down_ini.DownloadUrl("http://update.ttmath.org/ttcalc_update.ini", continue_download_) )
|
2009-10-18 00:13:14 +02:00
|
|
|
{
|
|
|
|
IniParser iparser;
|
2009-11-09 13:46:13 +01:00
|
|
|
std::string ini_value[4];
|
2009-10-18 00:13:14 +02:00
|
|
|
|
|
|
|
iparser.SectionCaseSensitive(false);
|
2009-10-18 20:59:21 +02:00
|
|
|
|
|
|
|
#ifndef TTCALC_PORTABLE
|
2009-11-04 18:59:42 +01:00
|
|
|
iparser.Associate("normal|url", &url_ );
|
|
|
|
iparser.Associate("normal|filename", &remote_file_name_ );
|
2009-10-18 20:59:21 +02:00
|
|
|
iparser.Associate("normal|version.major", &ini_value[0] );
|
|
|
|
iparser.Associate("normal|version.minor", &ini_value[1] );
|
|
|
|
iparser.Associate("normal|version.revision", &ini_value[2] );
|
2009-11-09 13:46:13 +01:00
|
|
|
iparser.Associate("normal|version.prerelease", &ini_value[3] );
|
|
|
|
|
2009-10-18 20:59:21 +02:00
|
|
|
#else
|
2009-11-04 18:59:42 +01:00
|
|
|
iparser.Associate("portable|url", &url_ );
|
|
|
|
iparser.Associate("portable|filename", &remote_file_name_ );
|
2009-10-18 20:59:21 +02:00
|
|
|
iparser.Associate("portable|version.major", &ini_value[0] );
|
|
|
|
iparser.Associate("portable|version.minor", &ini_value[1] );
|
|
|
|
iparser.Associate("portable|version.revision", &ini_value[2] );
|
2009-11-09 13:46:13 +01:00
|
|
|
iparser.Associate("portable|version.prerelease",&ini_value[3] );
|
2009-10-18 20:59:21 +02:00
|
|
|
#endif
|
2009-10-18 00:13:14 +02:00
|
|
|
|
|
|
|
IniParser::Error err = iparser.ReadFromFile( down_ini.GetFileName() );
|
|
|
|
|
2010-02-17 18:05:00 +01:00
|
|
|
if( err == IniParser::err_ok && IsServerCorrect(url_) )
|
2009-10-18 00:13:14 +02:00
|
|
|
{
|
2009-11-04 18:59:42 +01:00
|
|
|
major_ = atoi(ini_value[0].c_str());
|
|
|
|
minor_ = atoi(ini_value[1].c_str());
|
|
|
|
revision_ = atoi(ini_value[2].c_str());
|
2009-11-09 13:46:13 +01:00
|
|
|
prerelease_ = atoi(ini_value[3].c_str());
|
2009-10-18 00:13:14 +02:00
|
|
|
|
2009-11-04 18:59:42 +01:00
|
|
|
if( major_ > TTCALC_MAJOR_VER )
|
2009-10-18 00:13:14 +02:00
|
|
|
return true;
|
|
|
|
else
|
2009-11-04 18:59:42 +01:00
|
|
|
if( major_ == TTCALC_MAJOR_VER && minor_ > TTCALC_MINOR_VER )
|
2009-10-18 00:13:14 +02:00
|
|
|
return true;
|
|
|
|
else
|
2009-11-04 18:59:42 +01:00
|
|
|
if( major_ == TTCALC_MAJOR_VER && minor_ == TTCALC_MINOR_VER && revision_ > TTCALC_REVISION_VER )
|
2009-10-18 00:13:14 +02:00
|
|
|
return true;
|
2009-11-09 13:46:13 +01:00
|
|
|
else
|
|
|
|
if( major_ == TTCALC_MAJOR_VER && minor_ == TTCALC_MINOR_VER && revision_ == TTCALC_REVISION_VER &&
|
|
|
|
prerelease_ < TTCALC_PRERELEASE_VER )
|
|
|
|
return true;
|
2009-10-18 00:13:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-11-04 18:59:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace Update
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-10-18 00:13:14 +02:00
|
|
|
void CheckUpdate(HWND hwnd)
|
|
|
|
{
|
|
|
|
char buffer[200];
|
|
|
|
|
2009-10-18 20:59:21 +02:00
|
|
|
level = -1; // nothing to do when Next is pressed
|
2009-11-04 18:59:42 +01:00
|
|
|
continue_down = true;
|
2009-11-03 22:00:05 +01:00
|
|
|
Languages * pl = GetPrgRes()->GetLanguages();
|
2009-10-18 20:59:21 +02:00
|
|
|
EnableWindow(GetDlgItem(hwnd, IDC_BUTTON_NEXT), false);
|
2009-10-18 00:13:14 +02:00
|
|
|
EnableWindow(GetDlgItem(hwnd, IDC_UPDATE_INFO1), false);
|
2009-10-18 20:59:21 +02:00
|
|
|
ShowWindow(GetDlgItem(hwnd, IDC_PROGRESS), SW_SHOW);
|
|
|
|
CheckMessages();
|
2009-10-18 00:13:14 +02:00
|
|
|
|
2009-11-09 13:46:13 +01:00
|
|
|
if( CheckUpdateFromIni(hwnd, url, remote_file_name, major, minor, revision, prerelease, &continue_down) )
|
2009-10-18 00:13:14 +02:00
|
|
|
{
|
|
|
|
level = 1;
|
2009-11-03 22:00:05 +01:00
|
|
|
sprintf(buffer, pl->GuiMessage(Languages::update_is_new_version), major, minor, revision);
|
2009-10-18 00:13:14 +02:00
|
|
|
SetDlgItemText(hwnd, IDC_UPDATE_INFO1, buffer);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
level = 100;
|
2009-12-05 19:30:33 +01:00
|
|
|
SetDlgItemText(hwnd, IDC_UPDATE_INFO1, pl->GuiMessage(Languages::update_no_new_version1));
|
|
|
|
SetDlgItemText(hwnd, IDC_UPDATE_INFO2, pl->GuiMessage(Languages::update_no_new_version2));
|
2009-11-03 22:00:05 +01:00
|
|
|
SetDlgItemText(hwnd, IDC_BUTTON_NEXT, pl->GuiMessage(Languages::update_button_finish));
|
2009-10-18 00:13:14 +02:00
|
|
|
ShowWindow(GetDlgItem(hwnd, IDC_PROGRESS), SW_HIDE);
|
2009-10-18 20:59:21 +02:00
|
|
|
EnableWindow(GetDlgItem(hwnd, IDC_BUTTON_CANCEL), false);
|
2009-10-18 00:13:14 +02:00
|
|
|
}
|
|
|
|
|
2009-10-18 20:59:21 +02:00
|
|
|
EnableWindow(GetDlgItem(hwnd, IDC_BUTTON_NEXT), true);
|
2009-10-18 00:13:14 +02:00
|
|
|
EnableWindow(GetDlgItem(hwnd, IDC_UPDATE_INFO1), true);
|
2009-10-18 20:59:21 +02:00
|
|
|
ShowWindow(GetDlgItem(hwnd, IDC_PROGRESS), SW_HIDE);
|
|
|
|
SetFocus(GetDlgItem(hwnd, IDC_BUTTON_NEXT));
|
2009-10-18 00:13:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SaveDialog(HWND hwnd)
|
|
|
|
{
|
|
|
|
OPENFILENAME o;
|
2009-10-18 20:59:21 +02:00
|
|
|
char buf[MAX_PATH], desktop[MAX_PATH];
|
|
|
|
|
|
|
|
if( !remote_file_name.empty() )
|
|
|
|
sprintf(buf, remote_file_name.c_str());
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifndef TTCALC_PORTABLE
|
|
|
|
sprintf(buf, "ttcalc-%d.%d.%d-setup.exe", major, minor, revision);
|
|
|
|
#else
|
|
|
|
sprintf(buf, "ttcalc-portable-%d.%d.%d-bin.tar.gz", major, minor, revision);
|
|
|
|
#endif
|
|
|
|
}
|
2009-10-18 00:13:14 +02:00
|
|
|
|
2009-10-18 20:59:21 +02:00
|
|
|
if( !SHGetSpecialFolderPath(hwnd, desktop, CSIDL_DESKTOPDIRECTORY, 0) )
|
|
|
|
desktop[0] = 0;
|
2009-10-18 00:13:14 +02:00
|
|
|
|
|
|
|
o.lStructSize = sizeof(o);
|
|
|
|
o.hwndOwner = hwnd;
|
|
|
|
o.hInstance = GetPrgRes()->GetInstance();
|
|
|
|
o.lpstrFilter = "*.*\0*.*\0";
|
|
|
|
o.lpstrCustomFilter = 0;
|
|
|
|
o.nMaxCustFilter = 0;
|
|
|
|
o.nFilterIndex = 1;
|
|
|
|
o.lpstrFile = buf;
|
|
|
|
o.nMaxFile = MAX_PATH;
|
|
|
|
o.lpstrFileTitle = 0;
|
|
|
|
o.nMaxFileTitle = 0;
|
2009-10-18 20:59:21 +02:00
|
|
|
o.lpstrInitialDir = (desktop[0]) ? desktop : 0;
|
2009-10-18 00:13:14 +02:00
|
|
|
o.lpstrTitle = 0;
|
|
|
|
o.Flags = OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST;
|
|
|
|
o.nFileOffset = 0;
|
|
|
|
o.nFileExtension = 0;
|
|
|
|
o.lpstrDefExt = 0;
|
|
|
|
o.lCustData = 0;
|
|
|
|
o.lpfnHook = 0;
|
|
|
|
o.lpTemplateName = 0;
|
|
|
|
|
|
|
|
|
|
|
|
if( GetSaveFileName(&o) )
|
|
|
|
{
|
2009-10-18 23:00:54 +02:00
|
|
|
local_file_name = buf;
|
2009-10-18 00:13:14 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool DownloadUpdate(HWND hwnd)
|
|
|
|
{
|
|
|
|
DownloadProgram down;
|
|
|
|
|
2009-10-18 20:59:21 +02:00
|
|
|
level = -1; // nothing to do when Next is pressed
|
2009-11-04 18:59:42 +01:00
|
|
|
continue_down = true;
|
2009-10-18 20:59:21 +02:00
|
|
|
EnableWindow(GetDlgItem(hwnd, IDC_BUTTON_NEXT), false);
|
2009-11-03 22:00:05 +01:00
|
|
|
Languages * pl = GetPrgRes()->GetLanguages();
|
2009-10-18 00:13:14 +02:00
|
|
|
|
|
|
|
if( SaveDialog(hwnd) )
|
|
|
|
{
|
2009-11-03 22:00:05 +01:00
|
|
|
SetDlgItemText(hwnd, IDC_UPDATE_INFO1, pl->GuiMessage(Languages::update_download_from));
|
2009-10-18 20:59:21 +02:00
|
|
|
SetDlgItemText(hwnd, IDC_UPDATE_INFO2, url.c_str());
|
|
|
|
ShowWindow(GetDlgItem(hwnd, IDC_PROGRESS), SW_SHOW);
|
2009-10-18 00:13:14 +02:00
|
|
|
|
|
|
|
down.AttachProgress(GetDlgItem(hwnd, IDC_PROGRESS));
|
2009-10-18 20:59:21 +02:00
|
|
|
CheckMessages();
|
2009-10-18 00:13:14 +02:00
|
|
|
|
2009-11-04 18:59:42 +01:00
|
|
|
if( down.DownloadUrl(url.c_str(), &continue_down) )
|
2009-10-18 00:13:14 +02:00
|
|
|
{
|
2009-11-03 22:00:05 +01:00
|
|
|
SetDlgItemText(hwnd, IDC_UPDATE_INFO1, pl->GuiMessage(Languages::update_downloaded_info1));
|
|
|
|
SetDlgItemText(hwnd, IDC_UPDATE_INFO2, pl->GuiMessage(Languages::update_downloaded_info2));
|
|
|
|
|
|
|
|
#ifndef TTCALC_PORTABLE
|
2009-10-18 20:59:21 +02:00
|
|
|
level = 2;
|
2009-11-03 22:00:05 +01:00
|
|
|
#else
|
2009-10-18 20:59:21 +02:00
|
|
|
level = 100;
|
|
|
|
EnableWindow(GetDlgItem(hwnd, IDC_BUTTON_CANCEL), false);
|
2009-11-03 22:00:05 +01:00
|
|
|
#endif
|
2009-10-18 00:13:14 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-10-18 20:59:21 +02:00
|
|
|
level = 100;
|
2009-10-18 23:00:54 +02:00
|
|
|
DeleteFile(local_file_name.c_str());
|
2009-11-03 22:00:05 +01:00
|
|
|
SetDlgItemText(hwnd, IDC_UPDATE_INFO1, pl->GuiMessage(Languages::update_download_error));
|
2009-10-18 20:59:21 +02:00
|
|
|
SetDlgItemText(hwnd, IDC_UPDATE_INFO2, "");
|
2009-10-18 00:13:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ShowWindow(GetDlgItem(hwnd, IDC_PROGRESS), SW_HIDE);
|
2009-11-03 22:00:05 +01:00
|
|
|
SetDlgItemText(hwnd, IDC_BUTTON_NEXT, pl->GuiMessage(Languages::update_button_finish));
|
2009-10-18 20:59:21 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-10-18 00:13:14 +02:00
|
|
|
level = 100;
|
2009-10-18 20:59:21 +02:00
|
|
|
EndDialog(hwnd, 0);
|
2009-10-18 00:13:14 +02:00
|
|
|
}
|
|
|
|
|
2009-10-18 20:59:21 +02:00
|
|
|
EnableWindow(GetDlgItem(hwnd, IDC_BUTTON_NEXT), true);
|
|
|
|
SetFocus(GetDlgItem(hwnd, IDC_BUTTON_NEXT));
|
2009-10-18 00:13:14 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BOOL UpdateInit(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
|
|
|
|
{
|
2009-11-03 22:00:05 +01:00
|
|
|
Languages * pl = GetPrgRes()->GetLanguages();
|
|
|
|
|
2009-11-04 18:59:42 +01:00
|
|
|
if( level == 0 )
|
|
|
|
{
|
|
|
|
SetDlgItemText(hwnd, IDC_UPDATE_INFO1, pl->GuiMessage(Languages::update_check_for_info));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if( level == 1 )
|
|
|
|
{
|
|
|
|
char buffer[200];
|
|
|
|
|
|
|
|
sprintf(buffer, pl->GuiMessage(Languages::update_is_new_version), major, minor, revision);
|
|
|
|
SetDlgItemText(hwnd, IDC_UPDATE_INFO1, buffer);
|
|
|
|
}
|
|
|
|
|
2009-10-18 20:59:21 +02:00
|
|
|
SetDlgItemText(hwnd, IDC_UPDATE_INFO2, "");
|
2009-11-03 22:00:05 +01:00
|
|
|
SetDlgItemText(hwnd, IDC_BUTTON_NEXT, pl->GuiMessage(Languages::update_button_next));
|
|
|
|
SetDlgItemText(hwnd, IDC_BUTTON_CANCEL, pl->GuiMessage(Languages::update_button_cancel));
|
|
|
|
SetWindowText(hwnd, pl->GuiMessage(Languages::update_title));
|
|
|
|
SetDlgItemText(hwnd, IDC_CHECK_UPDATE_STURTUP, pl->GuiMessage(Languages::update_check_at_startup));
|
2009-11-04 18:59:42 +01:00
|
|
|
|
|
|
|
if( GetPrgRes()->GetCheckUpdateStartup() )
|
|
|
|
CheckDlgButton(hwnd, IDC_CHECK_UPDATE_STURTUP, BST_CHECKED);
|
|
|
|
|
2009-10-18 20:59:21 +02:00
|
|
|
ShowWindow(GetDlgItem(hwnd, IDC_PROGRESS), SW_HIDE);
|
2009-10-18 00:13:14 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BOOL UpdateCommand(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
|
|
|
|
{
|
2009-10-18 20:59:21 +02:00
|
|
|
if( LOWORD(wParam)==IDCANCEL || LOWORD(wParam)==IDC_BUTTON_CANCEL )
|
2009-10-18 00:13:14 +02:00
|
|
|
{
|
2009-11-04 18:59:42 +01:00
|
|
|
continue_down = false;
|
2009-10-18 00:13:14 +02:00
|
|
|
EndDialog(hwnd, 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-11-04 18:59:42 +01:00
|
|
|
if( LOWORD(wParam) == IDC_CHECK_UPDATE_STURTUP )
|
|
|
|
{
|
|
|
|
GetPrgRes()->SetCheckUpdateStartup( IsDlgButtonChecked(hwnd, IDC_CHECK_UPDATE_STURTUP)==BST_CHECKED );
|
|
|
|
}
|
|
|
|
else
|
2009-10-18 20:59:21 +02:00
|
|
|
if( LOWORD(wParam) == IDC_BUTTON_NEXT )
|
2009-10-18 00:13:14 +02:00
|
|
|
{
|
|
|
|
switch(level)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
CheckUpdate(hwnd);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
DownloadUpdate(hwnd);
|
|
|
|
break;
|
|
|
|
|
2009-10-18 20:59:21 +02:00
|
|
|
case 2:
|
2009-10-18 23:00:54 +02:00
|
|
|
ShellExecute(0, "open", local_file_name.c_str(), "", "", SW_SHOWNORMAL);
|
2009-10-18 20:59:21 +02:00
|
|
|
EndDialog(hwnd, 0);
|
|
|
|
PostQuitMessage(0);
|
|
|
|
break;
|
|
|
|
|
2009-10-18 00:13:14 +02:00
|
|
|
case 100:
|
|
|
|
EndDialog(hwnd, 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CreateUpdateMessagesTable(Messages<BOOL> & messages)
|
|
|
|
{
|
|
|
|
messages.Associate(WM_INITDIALOG, UpdateInit);
|
|
|
|
messages.Associate(WM_COMMAND, UpdateCommand);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BOOL CALLBACK UpdateProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
|
|
|
|
{
|
|
|
|
static Messages<BOOL> messages;
|
|
|
|
|
|
|
|
if( messages.Empty() )
|
|
|
|
// initiation
|
|
|
|
CreateUpdateMessagesTable(messages);
|
|
|
|
|
|
|
|
return messages.Call(msg, hwnd, msg, wParam, lParam);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-04 18:59:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
unsigned __stdcall UpdateBackgroundProc(void *)
|
|
|
|
{
|
|
|
|
using namespace Background;
|
|
|
|
|
2010-09-24 00:52:28 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
if( CheckUpdateFromIni(0, url_, remote_file_name_, major_, minor_, revision_, prerelease_, 0) )
|
|
|
|
{
|
|
|
|
HWND main = GetPrgRes()->GetMainWindow();
|
|
|
|
SendMessage(main, WM_UPDATE_EXISTS, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(...)
|
2009-11-04 18:59:42 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
_endthreadex(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-18 00:13:14 +02:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-11-04 18:59:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-10-18 00:13:14 +02:00
|
|
|
void ShowUpdateDialog(HWND hwnd)
|
|
|
|
{
|
|
|
|
using namespace Update;
|
|
|
|
|
|
|
|
level = 0;
|
2009-10-18 23:00:54 +02:00
|
|
|
local_file_name.clear();
|
2009-10-18 20:59:21 +02:00
|
|
|
remote_file_name.clear();
|
|
|
|
url.clear();
|
2009-10-18 00:13:14 +02:00
|
|
|
|
2009-10-18 20:59:21 +02:00
|
|
|
DialogBox(GetPrgRes()->GetInstance(), MAKEINTRESOURCE(IDD_UPDATE_DIALOG), hwnd, UpdateProc);
|
2009-10-18 00:13:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-04 18:59:42 +01:00
|
|
|
void ShowUpdateDialogAfterCheck(HWND hwnd)
|
|
|
|
{
|
|
|
|
using namespace Update;
|
|
|
|
|
|
|
|
level = 1;
|
|
|
|
local_file_name.clear();
|
|
|
|
|
|
|
|
// make sure that there is a memory barrier before reading next objects (those from Background::)
|
|
|
|
// objects from Background:: are set by an another thread at the beginning
|
|
|
|
// when we are here then the thread is not writing (it exited)
|
|
|
|
remote_file_name = Background::remote_file_name_;
|
|
|
|
url = Background::url_;
|
|
|
|
major = Background::major_;
|
|
|
|
minor = Background::minor_;
|
|
|
|
revision = Background::revision_;
|
|
|
|
|
|
|
|
DialogBox(GetPrgRes()->GetInstance(), MAKEINTRESOURCE(IDD_UPDATE_DIALOG), hwnd, UpdateProc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CheckUpdateInBackground()
|
|
|
|
{
|
2009-11-22 22:46:11 +01:00
|
|
|
time_t last = GetPrgRes()->GetLastUpdate();
|
|
|
|
time_t now = time(0);
|
|
|
|
|
|
|
|
// testing only once a day (86400 = 60*60*24)
|
|
|
|
if( GetPrgRes()->GetCheckUpdateStartup() && last+86400 < now )
|
2009-11-04 18:59:42 +01:00
|
|
|
{
|
2009-11-22 22:46:11 +01:00
|
|
|
// this setting can be added also when a user selects manually the test for an update
|
|
|
|
GetPrgRes()->SetLastUpdate(now);
|
|
|
|
|
2009-11-04 18:59:42 +01:00
|
|
|
unsigned int thread_id;
|
|
|
|
_beginthreadex(0, 0, Update::UpdateBackgroundProc, 0, 0, &thread_id);
|
|
|
|
}
|
|
|
|
}
|
2009-10-18 00:13:14 +02:00
|
|
|
|
|
|
|
|