From 58069828e5e2dba0de605af03b0260144572d788 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Wed, 4 Nov 2009 17:59:42 +0000 Subject: [PATCH] added: the program can check for an update at startup git-svn-id: svn://ttmath.org/publicrep/ttcalc/trunk@236 e52654a7-88a9-db11-a3e9-0013d4bc506e --- CHANGELOG | 1 + src/Makefile.dep | 7 +- src/bigtypes.h | 2 +- src/calculation.cpp | 2 +- src/compileconfig.h | 2 +- src/convert.cpp | 2 +- src/convert.h | 2 +- src/download.cpp | 6 +- src/download.h | 2 +- src/functions.cpp | 2 +- src/iniparser.cpp | 2 +- src/iniparser.h | 2 +- src/languages.cpp | 8 +- src/languages.h | 2 +- src/mainwindow.cpp | 49 +++++++++-- src/messages.h | 2 +- src/pad.cpp | 2 +- src/pad.h | 2 +- src/parsermanager.cpp | 2 +- src/parsermanager.h | 2 +- src/programresources.cpp | 43 +++++++++- src/programresources.h | 32 ++++++- src/resource.h | 5 +- src/stopcalculating.h | 2 +- src/tabs.cpp | 2 +- src/tabs.h | 8 +- src/threadcontroller.h | 111 +++++-------------------- src/ttcalc_update.ini | 2 +- src/update.cpp | 175 ++++++++++++++++++++++++++++++++------- src/update.h | 14 ++++ src/variables.cpp | 2 +- src/winmain.cpp | 6 +- src/winmain.h | 3 +- 33 files changed, 337 insertions(+), 169 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index f8a3c26..3d76a09 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -2,6 +2,7 @@ Version 0.9.0 prerelease (2009.11.03): * fixed: when 'C' button was pressed, the cursor was not put in the edit window * added: Pad window - a multiline edit window * added: Checking for update dialog box + the program can check automatically for an update at startup * added: there are some new buttons on the standard tab - (...) - the whole expression is surrounded by brackets (ctrl+0 short cut) - 1/(...) diff --git a/src/Makefile.dep b/src/Makefile.dep index 0debe8e..e93dc30 100644 --- a/src/Makefile.dep +++ b/src/Makefile.dep @@ -1,5 +1,5 @@ -o = resource.o calculation.o convert.o download.o functions.o iniparser.o languages.o mainwindow.o pad.o parsermanager.o programresources.o tabs.o update.o variables.o winmain.o - +o = resource.o calculation.o convert.o download.o functions.o iniparser.o languages.o mainwindow.o pad.o parsermanager.o programresources.o tabs.o threadcontroller.o update.o variables.o winmain.o + calculation.o: calculation.cpp compileconfig.h parsermanager.h resource.h programresources.h iniparser.h languages.h bigtypes.h threadcontroller.h stopcalculating.h convert.h tabs.h messages.h convert.o: convert.cpp convert.h compileconfig.h bigtypes.h download.o: download.cpp compileconfig.h download.h @@ -11,9 +11,10 @@ pad.o: pad.cpp programresources.h compileconfig.h iniparser.h languages.h big parsermanager.o: parsermanager.cpp compileconfig.h parsermanager.h resource.h programresources.h iniparser.h languages.h bigtypes.h threadcontroller.h stopcalculating.h convert.h tabs.h messages.h programresources.o: programresources.cpp compileconfig.h programresources.h iniparser.h languages.h bigtypes.h threadcontroller.h stopcalculating.h convert.h tabs.o: tabs.cpp compileconfig.h tabs.h resource.h messages.h programresources.h iniparser.h languages.h bigtypes.h threadcontroller.h stopcalculating.h convert.h +threadcontroller.o: threadcontroller.cpp threadcontroller.h stopcalculating.h compileconfig.h update.o: update.cpp compileconfig.h update.h download.h programresources.h iniparser.h languages.h bigtypes.h threadcontroller.h stopcalculating.h convert.h messages.h resource.h winmain.h tabs.h pad.h variables.o: variables.cpp compileconfig.h tabs.h resource.h messages.h programresources.h iniparser.h languages.h bigtypes.h threadcontroller.h stopcalculating.h convert.h -winmain.o: winmain.cpp compileconfig.h winmain.h programresources.h iniparser.h languages.h bigtypes.h threadcontroller.h stopcalculating.h convert.h resource.h messages.h tabs.h pad.h +winmain.o: winmain.cpp compileconfig.h winmain.h programresources.h iniparser.h languages.h bigtypes.h threadcontroller.h stopcalculating.h convert.h resource.h messages.h tabs.h pad.h update.h download.h .SUFFIXES: .cpp .o diff --git a/src/bigtypes.h b/src/bigtypes.h index 3c9b4bc..0a1054d 100644 --- a/src/bigtypes.h +++ b/src/bigtypes.h @@ -1,7 +1,7 @@ /* * This file is a part of TTCalc - a mathematical calculator * and is distributed under the (new) BSD licence. - * Author: Tomasz Sowa + * Author: Tomasz Sowa */ /* diff --git a/src/calculation.cpp b/src/calculation.cpp index 3416788..e73bc43 100644 --- a/src/calculation.cpp +++ b/src/calculation.cpp @@ -1,7 +1,7 @@ /* * This file is a part of TTCalc - a mathematical calculator * and is distributed under the (new) BSD licence. - * Author: Tomasz Sowa + * Author: Tomasz Sowa */ /* diff --git a/src/compileconfig.h b/src/compileconfig.h index f846e75..189e0c5 100644 --- a/src/compileconfig.h +++ b/src/compileconfig.h @@ -1,7 +1,7 @@ /* * This file is a part of TTCalc - a mathematical calculator * and is distributed under the (new) BSD licence. - * Author: Tomasz Sowa + * Author: Tomasz Sowa */ /* diff --git a/src/convert.cpp b/src/convert.cpp index 30e05db..43ff7aa 100644 --- a/src/convert.cpp +++ b/src/convert.cpp @@ -1,7 +1,7 @@ /* * This file is a part of TTCalc - a mathematical calculator * and is distributed under the (new) BSD licence. - * Author: Tomasz Sowa + * Author: Tomasz Sowa */ /* diff --git a/src/convert.h b/src/convert.h index acee501..d2bb6e4 100644 --- a/src/convert.h +++ b/src/convert.h @@ -1,7 +1,7 @@ /* * This file is a part of TTCalc - a mathematical calculator * and is distributed under the (new) BSD licence. - * Author: Tomasz Sowa + * Author: Tomasz Sowa */ /* diff --git a/src/download.cpp b/src/download.cpp index f70c0d7..1e18ef4 100644 --- a/src/download.cpp +++ b/src/download.cpp @@ -2,7 +2,7 @@ /* * This file is a part of TTCalc - a mathematical calculator * and is distributed under the (new) BSD licence. - * Author: Tomasz Sowa + * Author: Tomasz Sowa */ /* @@ -42,7 +42,7 @@ Download::Download() { - buffer_len = 512; + buffer_len = 128; progress = 0; buffer = new char[buffer_len]; @@ -122,7 +122,7 @@ bool res = true; down_size = 0; - HINTERNET hint = InternetOpen("TTCalcDownload",INTERNET_OPEN_TYPE_DIRECT, 0, 0, 0); + HINTERNET hint = InternetOpen("TTCalcAutoUpdate",INTERNET_OPEN_TYPE_DIRECT, 0, 0, 0); if( !hint ) return false; diff --git a/src/download.h b/src/download.h index 789f87a..5114a0a 100644 --- a/src/download.h +++ b/src/download.h @@ -1,7 +1,7 @@ /* * This file is a part of TTCalc - a mathematical calculator * and is distributed under the (new) BSD licence. - * Author: Tomasz Sowa + * Author: Tomasz Sowa */ /* diff --git a/src/functions.cpp b/src/functions.cpp index f6f83fe..a108011 100644 --- a/src/functions.cpp +++ b/src/functions.cpp @@ -1,7 +1,7 @@ /* * This file is a part of TTCalc - a mathematical calculator * and is distributed under the (new) BSD licence. - * Author: Tomasz Sowa + * Author: Tomasz Sowa */ /* diff --git a/src/iniparser.cpp b/src/iniparser.cpp index 2c0ff1f..e7b0dc7 100644 --- a/src/iniparser.cpp +++ b/src/iniparser.cpp @@ -1,7 +1,7 @@ /* * This file is a part of TTCalc - a mathematical calculator * and is distributed under the (new) BSD licence. - * Author: Tomasz Sowa + * Author: Tomasz Sowa */ /* diff --git a/src/iniparser.h b/src/iniparser.h index 579ffed..a807194 100644 --- a/src/iniparser.h +++ b/src/iniparser.h @@ -1,7 +1,7 @@ /* * This file is a part of TTCalc - a mathematical calculator * and is distributed under the (new) BSD licence. - * Author: Tomasz Sowa + * Author: Tomasz Sowa */ /* diff --git a/src/languages.cpp b/src/languages.cpp index bf7580b..172669d 100644 --- a/src/languages.cpp +++ b/src/languages.cpp @@ -1,7 +1,7 @@ /* * This file is a part of TTCalc - a mathematical calculator * and is distributed under the (new) BSD licence. - * Author: Tomasz Sowa + * Author: Tomasz Sowa */ /* @@ -656,7 +656,7 @@ void Languages::InitGuiMessagesTab() InsertGuiPair(menu_view_lang_spanish, "&Hiszpański"); InsertGuiPair(menu_view_lang_danish, "&Duński"); InsertGuiPair(menu_view_lang_chinese, "&Chiński"); - InsertGuiPair(menu_view_lang_russian, "&Russian"); + InsertGuiPair(menu_view_lang_russian, "&Rosyjski"); InsertGuiPair(menu_view_lang_swedish, "&Szwedzki"); InsertGuiPair(menu_view_close_program, "&Zamknij"); InsertGuiPair(menu_edit_undo, "&Cofnij"); @@ -734,9 +734,9 @@ void Languages::InitGuiMessagesTab() InsertGuiPair(update_downloaded_info1, "Nowa wersja programu TTCalc została ściągnięta"); InsertGuiPair(update_downloaded_info2, ""); #endif - InsertGuiPair(update_download_error, "Wystąpiły problemy ze ściąganiem, proszę spróbować ponownie później"); + InsertGuiPair(update_download_error, "Wystąpiły problemy ze ściągnięciem, proszę spróbować ponownie później."); - InsertGuiPair(pad_title, "Padzzzzz"); + InsertGuiPair(pad_title, "Pad"); InsertGuiPair(pad_menu_file, "&Plik"); InsertGuiPair(pad_menu_edit, "&Edycja"); InsertGuiPair(pad_menu_file_new, "&Nowy"); // temporarily not used diff --git a/src/languages.h b/src/languages.h index 4e81220..e4bfb74 100644 --- a/src/languages.h +++ b/src/languages.h @@ -1,7 +1,7 @@ /* * This file is a part of TTCalc - a mathematical calculator * and is distributed under the (new) BSD licence. - * Author: Tomasz Sowa + * Author: Tomasz Sowa */ /* diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index a185edb..c546229 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -1,7 +1,7 @@ /* * This file is a part of TTCalc - a mathematical calculator * and is distributed under the (new) BSD licence. - * Author: Tomasz Sowa + * Author: Tomasz Sowa */ /* @@ -385,6 +385,8 @@ HMENU menu = GetMenu(hWnd); if( !menu ) return; + Languages * pl = GetPrgRes()->GetLanguages(); + MENUITEMINFO mi; mi.cbSize = sizeof(mi); mi.fMask = MIIM_TYPE; @@ -392,18 +394,26 @@ HMENU menu = GetMenu(hWnd); // on popup menus we're using indexes because they don't want to work correctly // with the identifiers - mi.dwTypeData = const_cast( GetPrgRes()->GetLanguages()->GuiMessage(Languages::menu_view) ); + mi.dwTypeData = const_cast( pl->GuiMessage(Languages::menu_view) ); SetMenuItemInfo(menu, IDM_VIEW_INDEX, true, &mi); - mi.dwTypeData = const_cast( GetPrgRes()->GetLanguages()->GuiMessage(Languages::menu_edit) ); + mi.dwTypeData = const_cast( pl->GuiMessage(Languages::menu_edit) ); SetMenuItemInfo(menu, IDM_EDIT_INDEX, true, &mi); - mi.dwTypeData = const_cast( GetPrgRes()->GetLanguages()->GuiMessage(Languages::menu_help) ); + mi.dwTypeData = const_cast( pl->GuiMessage(Languages::menu_help) ); SetMenuItemInfo(menu, IDM_HELP_INDEX, true, &mi); - mi.dwTypeData = const_cast( GetPrgRes()->GetLanguages()->GuiMessage(Languages::menu_language) ); + if( GetPrgRes()->GetUpdateExists() ) + { + mi.dwTypeData = const_cast( pl->GuiMessage(Languages::menu_update_available) ); + SetMenuItemInfo(menu, IDM_UPDATE_AVAILABLE_INDEX, true, &mi); + } + + mi.dwTypeData = const_cast( pl->GuiMessage(Languages::menu_language) ); SetMenuItemInfo(GetSubMenu(menu,IDM_VIEW_INDEX), IDM_LANGUAGE_INDEX, true, &mi); + + SetMenuLanguageItem(menu, IDM_NEW_WINDOW, Languages::menu_view_new_window); SetMenuLanguageItem(menu, IDM_NORMAL_VIEW, Languages::menu_view_normal_view); SetMenuLanguageItem(menu, IDM_COMPACT_VIEW, Languages::menu_view_compact_view); @@ -1001,6 +1011,12 @@ BOOL WmCommand_HelpCheckUpdate(HWND hWnd, UINT message, WPARAM wParam, LPARAM lP return true; } +BOOL WmCommand_UpdateAvailable(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) +{ + ShowUpdateDialogAfterCheck(hWnd); + +return true; +} @@ -1359,6 +1375,27 @@ return true; } +BOOL WmUpdateExists(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) +{ +MENUITEMINFO mi; + + mi.cbSize = sizeof(mi); + mi.fMask = MIIM_TYPE | MIIM_ID; + mi.fType = MFT_STRING; + mi.dwTypeData = const_cast(GetPrgRes()->GetLanguages()->GuiMessage(Languages::menu_update_available)); + mi.cch = 0; + mi.wID = IDM_UPDATE_AVAILABLE; + + HMENU menu = GetMenu(hWnd); + InsertMenuItem(menu, IDM_UPDATE_AVAILABLE_INDEX, true, &mi); + + GetPrgRes()->SetUpdateExists(true); // used for the dynamic menu (for changing languages) + DrawMenuBar(hWnd); + +return true; +} + + void CreateCommandMessagesTable(Messages & cmessages) { cmessages.Associate(IDM_ALWAYS_ON_TOP, WmCommand_AlwaysOnTop); @@ -1367,6 +1404,7 @@ void CreateCommandMessagesTable(Messages & cmessages) cmessages.Associate(IDM_PAD, WmCommand_Pad); cmessages.Associate(IDM_HELP_ABOUT, WmCommand_HelpAbout); cmessages.Associate(IDM_HELP_CHECK_UPDATE, WmCommand_HelpCheckUpdate); + cmessages.Associate(IDM_UPDATE_AVAILABLE, WmCommand_UpdateAvailable); cmessages.Associate(IDM_NEW_WINDOW, WmCommand_NewWindow); cmessages.Associate(IDM_CLOSE_PROGRAM, WmCommand_CloseProgram); cmessages.Associate(IDC_INPUT_EDIT, WmCommand_InputEditNotify); @@ -1422,6 +1460,7 @@ void CreateMainMessagesTable(Messages & messages) messages.Associate(WM_SIZING, WmSizing); messages.Associate(WM_SET_LAST_ERROR, WmSetLastError); messages.Associate(WM_HELP, WmHelp); + messages.Associate(WM_UPDATE_EXISTS, WmUpdateExists); } diff --git a/src/messages.h b/src/messages.h index f68e80d..6a5768c 100644 --- a/src/messages.h +++ b/src/messages.h @@ -1,7 +1,7 @@ /* * This file is a part of TTCalc - a mathematical calculator * and is distributed under the (new) BSD licence. - * Author: Tomasz Sowa + * Author: Tomasz Sowa */ /* diff --git a/src/pad.cpp b/src/pad.cpp index e4a2860..70f889e 100644 --- a/src/pad.cpp +++ b/src/pad.cpp @@ -1,7 +1,7 @@ /* * This file is a part of TTCalc - a mathematical calculator * and is distributed under the (new) BSD licence. - * Author: Tomasz Sowa + * Author: Tomasz Sowa */ /* diff --git a/src/pad.h b/src/pad.h index 3381fa6..d3afabf 100644 --- a/src/pad.h +++ b/src/pad.h @@ -1,7 +1,7 @@ /* * This file is a part of TTCalc - a mathematical calculator * and is distributed under the (new) BSD licence. - * Author: Tomasz Sowa + * Author: Tomasz Sowa */ /* diff --git a/src/parsermanager.cpp b/src/parsermanager.cpp index 4fb3cd9..a7f6cb7 100644 --- a/src/parsermanager.cpp +++ b/src/parsermanager.cpp @@ -1,7 +1,7 @@ /* * This file is a part of TTCalc - a mathematical calculator * and is distributed under the (new) BSD licence. - * Author: Tomasz Sowa + * Author: Tomasz Sowa */ /* diff --git a/src/parsermanager.h b/src/parsermanager.h index 7bc0c23..27f127a 100644 --- a/src/parsermanager.h +++ b/src/parsermanager.h @@ -1,7 +1,7 @@ /* * This file is a part of TTCalc - a mathematical calculator * and is distributed under the (new) BSD licence. - * Author: Tomasz Sowa + * Author: Tomasz Sowa */ /* diff --git a/src/programresources.cpp b/src/programresources.cpp index 9d6df3b..25a0a82 100644 --- a/src/programresources.cpp +++ b/src/programresources.cpp @@ -1,7 +1,7 @@ /* * This file is a part of TTCalc - a mathematical calculator * and is distributed under the (new) BSD licence. - * Author: Tomasz Sowa + * Author: Tomasz Sowa */ /* @@ -574,6 +574,31 @@ int ProgramResources::GetParamSep() } +void ProgramResources::SetCheckUpdateStartup(bool s) +{ + check_update_startup = s; +} + + +bool ProgramResources::GetCheckUpdateStartup() +{ + return check_update_startup; +} + + +void ProgramResources::SetUpdateExists(bool e) +{ + update_exists = e; +} + + +bool ProgramResources::GetUpdateExists() +{ + return update_exists; +} + + + ProgramResources::ProgramResources() { // if you want to change the input buffer you have also to change @@ -641,6 +666,8 @@ ProgramResources::ProgramResources() functions_id = 0; show_pad = false; + check_update_startup = true; + update_exists = false; // for dynamic menu } @@ -957,6 +984,7 @@ return err; + IniParser::Error ProgramResources::ReadFromFile() { IniParser iparser; @@ -996,6 +1024,7 @@ std::string ini_value[30]; iparser.Associate( "global|disp.grouping", &ini_value[23] ); iparser.Associate( "global|disp.input_comma", &ini_value[24] ); iparser.Associate( "global|disp.param_sep", &ini_value[25] ); + iparser.Associate( "global|update_start", &ini_value[26] ); iparser.Associate( "variables", &temp_variables ); iparser.Associate( "functions", &temp_functions ); @@ -1026,7 +1055,11 @@ std::string ini_value[30]; SetDisplayAlwaysScientific( bool(atoi(ini_value[9].c_str())) ); SetDisplayWhenScientific( atoi(ini_value[10].c_str()) ); SetDisplayRounding( atoi(ini_value[11].c_str()) ); - SetRemovingZeroes( bool(atoi(ini_value[15].c_str())) ); + + // remove zeroes - in there is not in the config then will be true by default + // (from the constructor) + if( !ini_value[15].empty() ) + SetRemovingZeroes( bool(atoi(ini_value[15].c_str())) ); if( ini_value[13] == "6" ) languages.SetCurrentLanguage(Languages::swe); @@ -1066,6 +1099,11 @@ std::string ini_value[30]; if( err != IniParser::err_ok ) bad_line = iparser.GetBadLine(); + // check update at startup - true by default (if there is not in the config) + // (default from the constructor) + if( !ini_value[26].empty() ) + check_update_startup = bool( atoi(ini_value[26].c_str()) ); + return err; } @@ -1090,6 +1128,7 @@ std::ofstream file( configuration_file.c_str() ); file << "size.x = " << x_size << std::endl; file << "size.y = " << y_size << std::endl; file << "maximized = " << (int)maximized << std::endl; + file << "update_start = " << (int)check_update_startup << std::endl; file << "pad = " << (int)show_pad << std::endl; file << "pad.x = " << pad_x_pos << std::endl; file << "pad.y = " << pad_y_pos << std::endl; diff --git a/src/programresources.h b/src/programresources.h index d9bc47c..08d538c 100644 --- a/src/programresources.h +++ b/src/programresources.h @@ -1,7 +1,7 @@ /* * This file is a part of TTCalc - a mathematical calculator * and is distributed under the (new) BSD licence. - * Author: Tomasz Sowa + * Author: Tomasz Sowa */ /* @@ -56,6 +56,18 @@ #include +/*! + some messages +*/ +#define WM_INIT_TAB_VARIABLES WM_APP +#define WM_INIT_TAB_FUNCTIONS WM_APP+1 +#define WM_INIT_TAB_PRECISION WM_APP+2 +#define WM_INIT_TAB_DISPLAY WM_APP+3 +#define WM_SET_LAST_ERROR WM_APP+4 +#define WM_INIT_TAB_CONVERT WM_APP+5 +#define WM_UPDATE_EXISTS WM_APP+6 + + /*! you don't have to create an object of this class but instead of it you can get the pointer to the object by using 'GetPrgRes()' @@ -421,6 +433,22 @@ public: int GetParamSep(); + /*! + setting/getting the test which is made when the program starts + (testing for an update) + */ + void SetCheckUpdateStartup(bool s); + bool GetCheckUpdateStartup(); + + + /*! + true if there is an update available + (used only with the dynamic menu) + */ + void SetUpdateExists(bool e); + bool GetUpdateExists(); + + private: bool IsWhiteCharacter(int c); @@ -491,6 +519,8 @@ private: int bad_line; bool show_pad; + bool check_update_startup; + bool update_exists; }; diff --git a/src/resource.h b/src/resource.h index 81dc501..c5aee1e 100644 --- a/src/resource.h +++ b/src/resource.h @@ -1,7 +1,7 @@ /* * This file is a part of TTCalc - a mathematical calculator * and is distributed under the (new) BSD licence. - * Author: Tomasz Sowa + * Author: Tomasz Sowa */ /* @@ -206,6 +206,8 @@ #define IDM_VIEW_INDEX 0 #define IDM_EDIT_INDEX 1 #define IDM_HELP_INDEX 2 +#define IDM_UPDATE_AVAILABLE_INDEX 3 + #define IDM_LANGUAGE_INDEX 8 #define IDM_NEW_WINDOW 40001 @@ -238,6 +240,7 @@ #define IDM_HELP_CHECK_UPDATE 40052 #define IDM_HELP_ABOUT 40053 +#define IDM_UPDATE_AVAILABLE 40060 // pad menu #define MENUPAD_FILE_INDEX 0 diff --git a/src/stopcalculating.h b/src/stopcalculating.h index 20f0291..928df6b 100644 --- a/src/stopcalculating.h +++ b/src/stopcalculating.h @@ -1,7 +1,7 @@ /* * This file is a part of TTCalc - a mathematical calculator * and is distributed under the (new) BSD licence. - * Author: Tomasz Sowa + * Author: Tomasz Sowa */ /* diff --git a/src/tabs.cpp b/src/tabs.cpp index 8930f05..4752d1f 100644 --- a/src/tabs.cpp +++ b/src/tabs.cpp @@ -1,7 +1,7 @@ /* * This file is a part of TTCalc - a mathematical calculator * and is distributed under the (new) BSD licence. - * Author: Tomasz Sowa + * Author: Tomasz Sowa */ /* diff --git a/src/tabs.h b/src/tabs.h index 4d53491..09e8831 100644 --- a/src/tabs.h +++ b/src/tabs.h @@ -1,7 +1,7 @@ /* * This file is a part of TTCalc - a mathematical calculator * and is distributed under the (new) BSD licence. - * Author: Tomasz Sowa + * Author: Tomasz Sowa */ /* @@ -49,12 +49,6 @@ #include "programresources.h" -#define WM_INIT_TAB_VARIABLES WM_APP -#define WM_INIT_TAB_FUNCTIONS WM_APP+1 -#define WM_INIT_TAB_PRECISION WM_APP+2 -#define WM_INIT_TAB_DISPLAY WM_APP+3 -#define WM_SET_LAST_ERROR WM_APP+4 -#define WM_INIT_TAB_CONVERT WM_APP+5 namespace TabWindowFunctions { diff --git a/src/threadcontroller.h b/src/threadcontroller.h index 2025a20..941f9ff 100644 --- a/src/threadcontroller.h +++ b/src/threadcontroller.h @@ -1,7 +1,7 @@ /* * This file is a part of TTCalc - a mathematical calculator * and is distributed under the (new) BSD licence. - * Author: Tomasz Sowa + * Author: Tomasz Sowa */ /* @@ -80,22 +80,13 @@ public: the default constructor (notice that there'll be only one object of this class) */ - ThreadController() - { - calculations = 0; - ready_for_stop = 0; - exit_thread = false; - } + ThreadController(); /*! the destructor */ - ~ThreadController() - { - if(calculations) CloseHandle(calculations); - if(ready_for_stop) CloseHandle(ready_for_stop); - } + ~ThreadController(); /*! @@ -103,38 +94,7 @@ public: we create two system event and initialize the 'stop_calculating' object */ - bool Init() volatile - { - char * buffer = new char[300]; - - // with 'GetTickCount()' we're generating an unique identifier of our event - // (there can be another window of ttcalc) - sprintf((char*)buffer,"TTCalcEventForManagingThreads9928%u", - (unsigned int)GetTickCount()); - - // 'calculations' will be for auto-reseting and initialized as non-signaled - if( (calculations = CreateEvent(0,false,false,(char*)buffer))==NULL) - { - delete [] buffer; - return false; - } - - sprintf((char*)buffer,"TTCalcEventReadyForStop5567%u", - (unsigned int)GetTickCount()); - - // 'ready_for_stop' will be for manual-reseting and initialized as signaled - // 'manual-reset' means that we must call ResetEvent() function (from WinAPI) - // to manually reset the state to nonsignaled - if( (ready_for_stop = CreateEvent(0,true,true,(char*)buffer))==NULL) - { - delete [] buffer; - return false; - } - - delete [] buffer; - - return stop_calculating.Init(); - } + bool Init() volatile; /*! @@ -143,10 +103,7 @@ public: string, user-defined variables, functions etc.) and when the second thread will have finished that then it call ReadyForStop() method */ - void ReadyForStop() volatile - { - SetEvent( ready_for_stop ); - } + void ReadyForStop() volatile; /*! @@ -154,14 +111,7 @@ public: close button we call StopCalculatingAndExitThread() from the first main thread (gui), it means that the second thread (calculations) will finish itself */ - void StopCalculatingAndExitThread() volatile - { - WaitForSingleObject(ready_for_stop,INFINITE); - - stop_calculating.Stop(); - exit_thread = true; - SetEvent(calculations); - } + void StopCalculatingAndExitThread() volatile; /*! @@ -172,12 +122,7 @@ public: of copying variables) then sets the 'stop object' for signaled and returns to the caller */ - void StopCalculating() volatile - { - WaitForSingleObject(ready_for_stop, INFINITE); - - stop_calculating.Stop(); - } + void StopCalculating() volatile; /*! @@ -185,67 +130,49 @@ public: in other words it means that the calculations will start (maybe now, maybe at once if the second thread is still working) */ - void StartCalculating() volatile - { - SetEvent(calculations); - } + void StartCalculating() volatile; /*! this is the main method which is used by the second thread, if there's nothing to do this method (and the second thread as well) waits */ - volatile bool WaitForCalculatingAndBlockForStop() volatile - { - WaitForSingleObject(calculations,INFINITE); - ResetEvent(ready_for_stop); - - stop_calculating.Start(); - - return !exit_thread; - } + volatile bool WaitForCalculatingAndBlockForStop() volatile; /*! this method returns the pointer to the 'stop object' it's used by the second thread during calculating */ - const volatile ttmath::StopCalculating * GetStopObject() volatile const - { - return &stop_calculating; - } + const volatile ttmath::StopCalculating * GetStopObject() volatile const; /*! it returns 'true' if there was a stop signal during calculating the stop signal can be caused by the first thread (gui thread) */ - bool WasStopSignal() volatile const - { - return stop_calculating.WasStopSignal(); - } + bool WasStopSignal() volatile const; private: -// auto-reset, initialized as non-signaled -HANDLE calculations; + // auto-reset, initialized as non-signaled + HANDLE calculations; -// manual-reset, initialized as signaled -HANDLE ready_for_stop; + // manual-reset, initialized as signaled + HANDLE ready_for_stop; -bool exit_thread; + bool exit_thread; -NewStopCalculating stop_calculating; + NewStopCalculating stop_calculating; /*! we make the copy-constructor private so that nobody will be able to make a copy of the one object of this class */ - ThreadController(const ThreadController &) - { - } + ThreadController(const ThreadController &); + }; diff --git a/src/ttcalc_update.ini b/src/ttcalc_update.ini index d75a60d..70197db 100644 --- a/src/ttcalc_update.ini +++ b/src/ttcalc_update.ini @@ -9,7 +9,7 @@ version.revision = 1 [portable] url = http://downloads.sourceforge.net/project/ttcalc/ttcalc/ttcalc-0.8.7/ttcalc-portable-0.8.7-bin.tar.gz?use_mirror=dfn -fillename = ttcalc-portable-0.8.7-bin.tar +filename = ttcalc-portable-0.8.7-bin.tar version.major = 0 version.minor = 9 version.revision = 1 diff --git a/src/update.cpp b/src/update.cpp index 27d0df1..fc9f593 100644 --- a/src/update.cpp +++ b/src/update.cpp @@ -37,8 +37,11 @@ #include "compileconfig.h" + #include #include +#include + #include "update.h" #include "programresources.h" #include "messages.h" @@ -49,7 +52,7 @@ namespace Update { - bool continue_download; + bool continue_down; int level; std::string url; std::string remote_file_name; @@ -57,6 +60,17 @@ namespace Update int major; int minor; int revision; + + + // 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_; + } } @@ -70,6 +84,7 @@ DownloadIni::DownloadIni() file = 0; file_name = new char[MAX_PATH]; file_name[0] = 0; + continue_download = 0; } @@ -137,8 +152,6 @@ bool DownloadIni::Init() if( !CreateTmpFile() ) return false; - Update::continue_download = true; - return true; } @@ -152,9 +165,18 @@ bool DownloadIni::Read(char * buffer, size_t size) fwrite(buffer, size, 1, file); CheckMessages(); -return Update::continue_download; + if( continue_download && !(*continue_download) ) + return false; + +return true; } +bool DownloadIni::DownloadUrl(const char * url, bool * c) +{ + continue_download = c; + +return Download::DownloadUrl(url); +} void DownloadIni::Close() @@ -176,6 +198,11 @@ void DownloadIni::Close() */ +DownloadProgram::DownloadProgram() +{ + continue_download = 0; +} + void DownloadProgram::GetLen(HINTERNET conn) { @@ -193,7 +220,6 @@ DWORD index = 0; bool DownloadProgram::Init() { file.open(Update::local_file_name.c_str(), std::ios_base::out | std::ios_base::binary); - Update::continue_download = true; if( !file ) return false; @@ -208,10 +234,20 @@ bool DownloadProgram::Read(char * buffer, size_t size) file.write(buffer, size); CheckMessages(); -return Update::continue_download; + if( continue_download && !(*continue_download) ) + return false; + +return true; } +bool DownloadProgram::DownloadUrl(const char * url, bool * c) +{ + continue_download = c; + +return Download::DownloadUrl(url); +} + void DownloadProgram::Close() { @@ -221,20 +257,20 @@ void DownloadProgram::Close() -namespace Update -{ + /* - - - + this method is called from the other thread too + make sure you don't write directly to any global variables (from Update namespace etc) */ - -bool CheckUpdateFromIni(HWND hwnd) +bool CheckUpdateFromIni(HWND hwnd, std::string & url_, std::string & remote_file_name_, + int & major_, int & minor_, int & revision_, bool * continue_download_) { DownloadIni down_ini; - down_ini.AttachProgress(GetDlgItem(hwnd, IDC_PROGRESS)); - if( down_ini.DownloadUrl("http://ttcalc.sourceforge.net/ttcalc_update.ini") ) + if( hwnd ) + down_ini.AttachProgress(GetDlgItem(hwnd, IDC_PROGRESS)); + + if( down_ini.DownloadUrl("http://ttcalc.sourceforge.net/ttcalc_update.ini", continue_download_) ) { IniParser iparser; std::string ini_value[3]; @@ -242,14 +278,14 @@ DownloadIni down_ini; iparser.SectionCaseSensitive(false); #ifndef TTCALC_PORTABLE - iparser.Associate("normal|url", &url ); - iparser.Associate("normal|filename", &remote_file_name ); + iparser.Associate("normal|url", &url_ ); + iparser.Associate("normal|filename", &remote_file_name_ ); iparser.Associate("normal|version.major", &ini_value[0] ); iparser.Associate("normal|version.minor", &ini_value[1] ); iparser.Associate("normal|version.revision", &ini_value[2] ); #else - iparser.Associate("portable|url", &url ); - iparser.Associate("portable|filename", &remote_file_name ); + iparser.Associate("portable|url", &url_ ); + iparser.Associate("portable|filename", &remote_file_name_ ); iparser.Associate("portable|version.major", &ini_value[0] ); iparser.Associate("portable|version.minor", &ini_value[1] ); iparser.Associate("portable|version.revision", &ini_value[2] ); @@ -259,17 +295,17 @@ DownloadIni down_ini; if( err == IniParser::err_ok ) { - major = atoi(ini_value[0].c_str()); - minor = atoi(ini_value[1].c_str()); - revision = atoi(ini_value[2].c_str()); + major_ = atoi(ini_value[0].c_str()); + minor_ = atoi(ini_value[1].c_str()); + revision_ = atoi(ini_value[2].c_str()); - if( major > TTCALC_MAJOR_VER ) + if( major_ > TTCALC_MAJOR_VER ) return true; else - if( major == TTCALC_MAJOR_VER && minor > TTCALC_MINOR_VER ) + if( major_ == TTCALC_MAJOR_VER && minor_ > TTCALC_MINOR_VER ) return true; else - if( major == TTCALC_MAJOR_VER && minor == TTCALC_MINOR_VER && revision > TTCALC_REVISION_VER ) + if( major_ == TTCALC_MAJOR_VER && minor_ == TTCALC_MINOR_VER && revision_ > TTCALC_REVISION_VER ) return true; } } @@ -277,18 +313,27 @@ DownloadIni down_ini; return false; } + + + +namespace Update +{ + + + void CheckUpdate(HWND hwnd) { char buffer[200]; level = -1; // nothing to do when Next is pressed + continue_down = true; Languages * pl = GetPrgRes()->GetLanguages(); EnableWindow(GetDlgItem(hwnd, IDC_BUTTON_NEXT), false); EnableWindow(GetDlgItem(hwnd, IDC_UPDATE_INFO1), false); ShowWindow(GetDlgItem(hwnd, IDC_PROGRESS), SW_SHOW); CheckMessages(); - if( CheckUpdateFromIni(hwnd) ) + if( CheckUpdateFromIni(hwnd, url, remote_file_name, major, minor, revision, &continue_down) ) { level = 1; sprintf(buffer, pl->GuiMessage(Languages::update_is_new_version), major, minor, revision); @@ -367,6 +412,7 @@ bool DownloadUpdate(HWND hwnd) DownloadProgram down; level = -1; // nothing to do when Next is pressed + continue_down = true; EnableWindow(GetDlgItem(hwnd, IDC_BUTTON_NEXT), false); Languages * pl = GetPrgRes()->GetLanguages(); @@ -379,7 +425,7 @@ DownloadProgram down; down.AttachProgress(GetDlgItem(hwnd, IDC_PROGRESS)); CheckMessages(); - if( down.DownloadUrl( url.c_str()) ) + if( down.DownloadUrl(url.c_str(), &continue_down) ) { SetDlgItemText(hwnd, IDC_UPDATE_INFO1, pl->GuiMessage(Languages::update_downloaded_info1)); SetDlgItemText(hwnd, IDC_UPDATE_INFO2, pl->GuiMessage(Languages::update_downloaded_info2)); @@ -425,12 +471,28 @@ BOOL UpdateInit(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { Languages * pl = GetPrgRes()->GetLanguages(); - SetDlgItemText(hwnd, IDC_UPDATE_INFO1, pl->GuiMessage(Languages::update_check_for_info)); + 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); + } + SetDlgItemText(hwnd, IDC_UPDATE_INFO2, ""); 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)); + + if( GetPrgRes()->GetCheckUpdateStartup() ) + CheckDlgButton(hwnd, IDC_CHECK_UPDATE_STURTUP, BST_CHECKED); + ShowWindow(GetDlgItem(hwnd, IDC_PROGRESS), SW_HIDE); return true; @@ -441,11 +503,16 @@ BOOL UpdateCommand(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { if( LOWORD(wParam)==IDCANCEL || LOWORD(wParam)==IDC_BUTTON_CANCEL ) { - continue_download = false; + continue_down = false; EndDialog(hwnd, 0); return 0; } + if( LOWORD(wParam) == IDC_CHECK_UPDATE_STURTUP ) + { + GetPrgRes()->SetCheckUpdateStartup( IsDlgButtonChecked(hwnd, IDC_CHECK_UPDATE_STURTUP)==BST_CHECKED ); + } + else if( LOWORD(wParam) == IDC_BUTTON_NEXT ) { switch(level) @@ -494,10 +561,32 @@ return messages.Call(msg, hwnd, msg, wParam, lParam); } + + +unsigned __stdcall UpdateBackgroundProc(void *) +{ +using namespace Background; + + if( CheckUpdateFromIni(0, url_, remote_file_name_, major_, minor_, revision_, 0) ) + { + HWND main = GetPrgRes()->GetMainWindow(); + SendMessage(main, WM_UPDATE_EXISTS, 0, 0); + } + +_endthreadex(0); +return 0; +} + + } // namespace + + + + + void ShowUpdateDialog(HWND hwnd) { using namespace Update; @@ -511,5 +600,33 @@ using namespace Update; } +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() +{ + if( GetPrgRes()->GetCheckUpdateStartup() ) + { + unsigned int thread_id; + _beginthreadex(0, 0, Update::UpdateBackgroundProc, 0, 0, &thread_id); + } +} diff --git a/src/update.h b/src/update.h index 0f3f718..e04d63c 100644 --- a/src/update.h +++ b/src/update.h @@ -45,6 +45,8 @@ void ShowUpdateDialog(HWND hwnd); +void ShowUpdateDialogAfterCheck(HWND hwnd); +void CheckUpdateInBackground(); @@ -55,6 +57,7 @@ public: ~DownloadIni(); void DeleteTmpFile(); const char * GetFileName(); + bool DownloadUrl(const char * url, bool * continue_download = 0); private: @@ -66,16 +69,27 @@ private: char * file_name; FILE * file; + bool * continue_download; }; + + + + class DownloadProgram : public Download { +public: + DownloadProgram(); + bool DownloadUrl(const char * url, bool * continue_download = 0); + +private: virtual void GetLen(HINTERNET conn); virtual bool Init(); virtual bool Read(char * buffer, size_t size); virtual void Close(); std::ofstream file; + bool * continue_download; }; diff --git a/src/variables.cpp b/src/variables.cpp index d87a490..194780a 100644 --- a/src/variables.cpp +++ b/src/variables.cpp @@ -1,7 +1,7 @@ /* * This file is a part of TTCalc - a mathematical calculator * and is distributed under the (new) BSD licence. - * Author: Tomasz Sowa + * Author: Tomasz Sowa */ /* diff --git a/src/winmain.cpp b/src/winmain.cpp index f8cf7c2..324376a 100644 --- a/src/winmain.cpp +++ b/src/winmain.cpp @@ -1,7 +1,7 @@ /* * This file is a part of TTCalc - a mathematical calculator * and is distributed under the (new) BSD licence. - * Author: Tomasz Sowa + * Author: Tomasz Sowa */ /* @@ -37,6 +37,7 @@ #include "compileconfig.h" #include "winmain.h" +#include "update.h" /*! \file mainwin.cpp @@ -97,6 +98,9 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int) if( !CreatePadWindow() ) return ShowError( Languages::cant_create_pad ); + // checking for an update (in an another thread) + CheckUpdateInBackground(); + // the main loop of messages MainMessagesLoop(); diff --git a/src/winmain.h b/src/winmain.h index 8d2637b..3760631 100644 --- a/src/winmain.h +++ b/src/winmain.h @@ -1,7 +1,7 @@ /* * This file is a part of TTCalc - a mathematical calculator * and is distributed under the (new) BSD licence. - * Author: Tomasz Sowa + * Author: Tomasz Sowa */ /* @@ -52,7 +52,6 @@ #include #include #include - #include