Changeset 538


Ignore:
Timestamp:
11/13/08 11:06:33 (6 years ago)
Author:
lowjoel
Message:

Reshuffle all the global functions into classes. It is way neater now.

Location:
branches/eraser6/Installer/Bootstrapper
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/eraser6/Installer/Bootstrapper/Bootstrapper.cpp

    r537 r538  
    8282            s->FileRead += readSize; 
    8383 
    84             SetProgress((float)((double)s->FileRead / s->FileSize)); 
     84            MainWindow& mainWin = Application::Get().GetTopWindow(); 
     85            mainWin.SetProgress((float)((double)s->FileRead / s->FileSize)); 
    8586        } 
    8687 
     
    250251    DWORD lastWait = 0; 
    251252    while ((lastWait = WaitForSingleObject(pInfo.hProcess, 50)) == WAIT_TIMEOUT) 
    252         Yield(); 
     253        Application::Get().Yield(); 
    253254    if (lastWait == WAIT_ABANDONED) 
    254255        throw std::wstring(L"The condition waiting on the termination of the .NET installer was abandoned."); 
     
    270271{ 
    271272    //Update the UI 
    272     SetProgressIndeterminate(); 
    273     SetMessage(L"Installing .NET Framework..."); 
     273    MainWindow& mainWin = Application::Get().GetTopWindow(); 
     274    mainWin.SetProgressIndeterminate(); 
     275    mainWin.SetMessage(L"Installing .NET Framework..."); 
    274276 
    275277    //Get the path to the installer 
     
    285287bool InstallEraser(std::wstring tempDir) 
    286288{ 
    287     SetProgressIndeterminate(); 
    288     SetMessage(L"Installing Eraser..."); 
     289    MainWindow& mainWin = Application::Get().GetTopWindow(); 
     290    mainWin.SetProgressIndeterminate(); 
     291    mainWin.SetMessage(L"Installing Eraser..."); 
    289292 
    290293    //Determine the system architecture. 
  • branches/eraser6/Installer/Bootstrapper/Bootstrapper.h

    r536 r538  
    2222#pragma once 
    2323 
     24#include <windows.h> 
    2425#include <string> 
    2526 
    2627#include "resource.h" 
     28#undef Yield 
     29 
     30class Application 
     31{ 
     32public: 
     33    /// Gets the Singleton instance of the Application object. 
     34    static Application& Get(); 
     35 
     36    /// Processes messages in the message queue. 
     37    void Yield(); 
     38 
     39    /// Retrieves the MainWindow object representing the Application's top window. 
     40    class MainWindow& GetTopWindow(); 
     41 
     42private: 
     43    Application(); 
     44}; 
     45 
     46class MainWindow 
     47{ 
     48public: 
     49    /// Constructor. 
     50    MainWindow() 
     51    { 
     52        hWnd = hWndStatusLbl = hWndProgressBar = hWndCancelBtn = NULL; 
     53    } 
     54 
     55    /// Creates the Window. 
     56    bool Create(); 
     57 
     58    /// Sets the progress of the current operation. 
     59    ///  
     60    /// \param[in] progress The percentage of the operation complete. This is a real 
     61    ///                     number from 0 to 1. 
     62    void SetProgress(float progress); 
     63 
     64    /// Sets the progress bar to an indeterminate state. 
     65    void SetProgressIndeterminate(); 
     66 
     67    /// Sets the dialog label to display a short message. 
     68    void SetMessage(std::wstring message); 
     69 
     70    /// Gets the raw window handle. 
     71    HWND GetHandle() 
     72    { 
     73        return hWnd; 
     74    } 
     75 
     76private: 
     77    HWND hWnd; 
     78    HWND hWndStatusLbl; 
     79    HWND hWndProgressBar; 
     80    HWND hWndCancelBtn; 
     81 
     82private: 
     83 
     84    /// Registers the main window class and creates it. 
     85    bool InitInstance(); 
     86}; 
    2787 
    2888/// Formats the system error code using FormatMessage, returning the message as 
    2989/// a std::wstring. 
    3090std::wstring GetErrorMessage(DWORD lastError); 
    31  
    32 /// Processes existing messages, to prevent the application from being "unresponsive" 
    33 #undef Yield 
    34 void Yield(); 
    35  
    36 /// Retrieves the application's top level window. 
    37 HWND GetTopWindow(); 
    38  
    39 void SetProgress(float progress); 
    40 void SetProgressIndeterminate(); 
    41 void SetMessage(std::wstring message); 
    4291 
    4392/// Retrieves the path to the executable file. 
  • branches/eraser6/Installer/Bootstrapper/Main.cpp

    r537 r538  
    3737    //Static variables 
    3838    HINSTANCE hInstance = NULL; 
    39     HWND hWndParent = NULL; 
    40     HWND hWndStatusLbl = NULL; 
    41     HWND hWndProgressBar = NULL; 
    42     HWND hWndCancelBtn = NULL; 
     39    MainWindow MainWin; 
    4340 
    4441    bool              InitInstance(HINSTANCE hInstance, HWND& hWnd); 
     
    8784    }; 
    8885 
    89     /// Registers the main window class and creates it. 
    90     bool InitInstance(HINSTANCE hInstance, HWND& hWnd) 
    91     { 
    92         WNDCLASSEX wcex; 
    93         ::ZeroMemory(&wcex, sizeof(wcex)); 
    94  
    95         wcex.cbSize         = sizeof(WNDCLASSEX); 
    96         wcex.style          = CS_HREDRAW | CS_VREDRAW; 
    97         wcex.lpfnWndProc    = WndProc; 
    98         wcex.cbClsExtra     = 0; 
    99         wcex.cbWndExtra     = 0; 
    100         wcex.hInstance      = hInstance; 
    101         wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(BOOTSTRAPPER_ICON)); 
    102         wcex.hCursor        = LoadCursor(NULL, IDC_ARROW); 
    103         wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW + 1); 
    104         wcex.lpszClassName  = szWindowClass; 
    105         wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(BOOTSTRAPPER_ICON)); 
    106         RegisterClassExW(&wcex); 
    107         InitCommonControls(); 
    108  
    109         //Create the window 
    110         hWnd = CreateWindowW(szWindowClass, L"Eraser Setup", WS_CAPTION | WS_SYSMENU, 
    111             CW_USEDEFAULT, 0, 300, 130, NULL, NULL, hInstance, NULL); 
    112  
    113         if (!hWnd) 
    114             return false; 
    115  
    116         //Set default settings (font) 
    117         SetWindowFont(hWnd); 
    118         return true; 
    119     } 
     86     
     87     
    12088 
    12189    /// Helper function to set the window font for created windows to the system default. 
     
    186154    //Create the parent window and the child controls 
    187155    ::hInstance = hInstance; 
    188     if (!InitInstance(hInstance, hWndParent)) 
     156    MainWin.Create(); 
     157 
     158    try 
     159    { 
     160        //OK, now we do the hard work. Create a folder to place our payload into 
     161        wchar_t tempPath[MAX_PATH]; 
     162        DWORD result = GetTempPathW(sizeof(tempPath) / sizeof(tempPath[0]), tempPath); 
     163        if (!result) 
     164            throw GetErrorMessage(GetLastError()); 
     165 
     166        std::wstring tempDir(tempPath, result); 
     167        if (std::wstring(L"\\/").find(tempDir[tempDir.length() - 1]) == std::wstring::npos) 
     168            tempDir += L"\\"; 
     169        tempDir += L"eraserInstallBootstrapper\\"; 
     170        TempDir dir(tempDir); 
     171        ExtractTempFiles(tempDir); 
     172 
     173        //Install the .NET framework 
     174        if (!HasNetFramework()) 
     175            InstallNetFramework(tempDir); 
     176 
     177        //Then install Eraser! 
     178        InstallEraser(tempDir); 
     179    } 
     180    catch (const std::wstring& e) 
     181    { 
     182        MessageBoxW(Application::Get().GetTopWindow().GetHandle(), e.c_str(), 
     183            L"Eraser Setup", MB_OK | MB_ICONERROR); 
     184    } 
     185 
     186    return 0; 
     187} 
     188 
     189bool MainWindow::Create() 
     190{ 
     191    if (!InitInstance()) 
    189192        return false; 
    190193 
    191194    HWND hWndPanel = CreateWindowExW(0, STATIC_CLASS, NULL, WS_CHILD | WS_VISIBLE, 
    192         0, 0, 294, 104, hWndParent, NULL, hInstance, NULL); 
     195        0, 0, 294, 104, hWnd, NULL, hInstance, NULL); 
    193196    hWndStatusLbl = CreateWindowExW(0, STATIC_CLASS, L"Extracting setup files...", 
    194197        WS_CHILD | WS_VISIBLE, 13, 38, 270, 19, hWndPanel, NULL, hInstance, NULL); 
     
    208211    SendMessage(hWndProgressBar, PBM_SETRANGE, 0, MAKELPARAM(0, 1000)); 
    209212 
    210     ShowWindow(hWndParent, nCmdShow); 
    211     UpdateWindow(hWndParent); 
    212  
    213     try 
    214     { 
    215         //OK, now we do the hard work. Create a folder to place our payload into 
    216         wchar_t tempPath[MAX_PATH]; 
    217         DWORD result = GetTempPathW(sizeof(tempPath) / sizeof(tempPath[0]), tempPath); 
    218         if (!result) 
    219             throw GetErrorMessage(GetLastError()); 
    220  
    221         std::wstring tempDir(tempPath, result); 
    222         if (std::wstring(L"\\/").find(tempDir[tempDir.length() - 1]) == std::wstring::npos) 
    223             tempDir += L"\\"; 
    224         tempDir += L"eraserInstallBootstrapper\\"; 
    225         TempDir dir(tempDir); 
    226         ExtractTempFiles(tempDir); 
    227  
    228         //Install the .NET framework 
    229         if (!HasNetFramework()) 
    230             InstallNetFramework(tempDir); 
    231  
    232         //Then install Eraser! 
    233         InstallEraser(tempDir); 
    234     } 
    235     catch (const std::wstring& e) 
    236     { 
    237         MessageBoxW(GetTopWindow(), e.c_str(), L"Eraser Setup", MB_OK | MB_ICONERROR); 
    238     } 
    239  
    240     return 0; 
    241 } 
    242  
    243 HWND GetTopWindow() 
    244 { 
    245     return hWndParent; 
    246 } 
    247  
    248 void SetProgress(float progress) 
     213    ShowWindow(hWnd, nCmdShow); 
     214    UpdateWindow(hWnd); 
     215} 
     216 
     217bool MainWindow::InitInstance() 
     218{ 
     219    WNDCLASSEX wcex; 
     220    ::ZeroMemory(&wcex, sizeof(wcex)); 
     221 
     222    wcex.cbSize         = sizeof(WNDCLASSEX); 
     223    wcex.style          = CS_HREDRAW | CS_VREDRAW; 
     224    wcex.lpfnWndProc    = WndProc; 
     225    wcex.cbClsExtra     = 0; 
     226    wcex.cbWndExtra     = 0; 
     227    wcex.hInstance      = hInstance; 
     228    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(BOOTSTRAPPER_ICON)); 
     229    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW); 
     230    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW + 1); 
     231    wcex.lpszClassName  = szWindowClass; 
     232    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(BOOTSTRAPPER_ICON)); 
     233    RegisterClassExW(&wcex); 
     234    InitCommonControls(); 
     235 
     236    //Create the window 
     237    hWnd = CreateWindowW(szWindowClass, L"Eraser Setup", WS_CAPTION | WS_SYSMENU, 
     238        CW_USEDEFAULT, 0, 300, 130, NULL, NULL, hInstance, NULL); 
     239 
     240    if (!hWnd) 
     241        return false; 
     242 
     243    //Set default settings (font) 
     244    SetWindowFont(hWnd); 
     245    return true; 
     246} 
     247 
     248void MainWindow::SetProgress(float progress) 
    249249{ 
    250250    SetWindowLong(hWndProgressBar, GWL_STYLE, 
     
    253253} 
    254254 
    255 void SetProgressIndeterminate() 
     255void MainWindow::SetProgressIndeterminate() 
    256256{ 
    257257    SetWindowLong(hWndProgressBar, GWL_STYLE, 
     
    260260} 
    261261 
    262 void SetMessage(std::wstring message) 
    263 { 
    264 } 
    265  
    266 void Yield() 
     262void MainWindow::SetMessage(std::wstring message) 
     263{ 
     264} 
     265 
     266void Application::Yield() 
    267267{ 
    268268    MSG msg; 
Note: See TracChangeset for help on using the changeset viewer.