[RAPPS]
[reactos.git] / reactos / base / applications / rapps / winmain.cpp
index a5d8d8d..20e16d5 100644 (file)
@@ -5,20 +5,20 @@
  * PURPOSE:         Main program
  * PROGRAMMERS:     Dmitry Chapyshev           (dmitry@reactos.org)
  *                  Ismael Ferreras Morezuelas (swyterzone+ros@gmail.com)
+ *                  Alexander Shaposhnikov     (chaez.san@gmail.com)
  */
-
+#include "defines.h"
 #include "rapps.h"
+#include "unattended.h"
 
-#include <atlbase.h>
 #include <atlcom.h>
-#include <shellapi.h>
 
 HWND hMainWnd;
 HINSTANCE hInst;
 INT SelectedEnumType = ENUM_ALL_COMPONENTS;
 SETTINGS_INFO SettingsInfo;
 
-WCHAR szSearchPattern[MAX_STR_LEN] = L"";
+ATL::CStringW szSearchPattern;
 
 class CRAppsModule : public CComModule
 {
@@ -28,13 +28,8 @@ public:
 BEGIN_OBJECT_MAP(ObjectMap)
 END_OBJECT_MAP()
 
-CRAppsModule                             gModule;
-CAtlWinModule                               gWinModule;
-
-void *operator new (size_t, void *buf)
-{
-    return buf;
-}
+CRAppsModule gModule;
+CAtlWinModule gWinModule;
 
 static VOID InitializeAtlModule(HINSTANCE hInstance, BOOL bInitialize)
 {
@@ -48,61 +43,62 @@ static VOID InitializeAtlModule(HINSTANCE hInstance, BOOL bInitialize)
     }
 }
 
-VOID
-FillDefaultSettings(PSETTINGS_INFO pSettingsInfo)
+VOID FillDefaultSettings(PSETTINGS_INFO pSettingsInfo)
 {
+    ATL::CStringW szDownloadDir;
+    ZeroMemory(pSettingsInfo, sizeof(SETTINGS_INFO));
+
     pSettingsInfo->bSaveWndPos = TRUE;
     pSettingsInfo->bUpdateAtStart = FALSE;
     pSettingsInfo->bLogEnabled = TRUE;
-    if (SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, pSettingsInfo->szDownloadDir)))
+
+    if (FAILED(SHGetFolderPathW(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, szDownloadDir.GetBuffer(MAX_PATH))))
     {
-        StringCbCatW(pSettingsInfo->szDownloadDir, sizeof(pSettingsInfo->szDownloadDir), L"\\RAPPS Downloads");
-    } 
+        szDownloadDir.ReleaseBuffer();
+        if (!szDownloadDir.GetEnvironmentVariableW(L"SystemDrive"))
+        {
+            szDownloadDir = L"C:";
+        }
+    }
     else
     {
-        ExpandEnvironmentStringsW(L"%SystemDrive%\\RAPPS Downloads", 
-            pSettingsInfo->szDownloadDir, sizeof(pSettingsInfo->szDownloadDir));
+        szDownloadDir.ReleaseBuffer();
     }
 
-    pSettingsInfo->bDelInstaller = FALSE;
+    szDownloadDir += L"\\RAPPS Downloads";
+    ATL::CStringW::CopyChars(pSettingsInfo->szDownloadDir,
+                             _countof(pSettingsInfo->szDownloadDir),
+                             szDownloadDir.GetString(),
+                             szDownloadDir.GetLength() + 1);
 
+    pSettingsInfo->bDelInstaller = FALSE;
     pSettingsInfo->Maximized = FALSE;
     pSettingsInfo->Left = CW_USEDEFAULT;
     pSettingsInfo->Top = CW_USEDEFAULT;
     pSettingsInfo->Width = 680;
     pSettingsInfo->Height = 450;
-
-    pSettingsInfo->Proxy = 0;
-    StringCbCopyW(pSettingsInfo->szProxyServer, sizeof(pSettingsInfo->szProxyServer), L"");
-    StringCbCopyW(pSettingsInfo->szNoProxyFor,  sizeof(pSettingsInfo->szNoProxyFor),  L"");
 }
 
-static BOOL
-LoadSettings(VOID)
+static BOOL LoadSettings()
 {
-    HKEY hKey;
+    ATL::CRegKey RegKey;
     DWORD dwSize;
-
-    if (RegOpenKeyExW(HKEY_CURRENT_USER, L"Software\\ReactOS\\rapps", 0, KEY_READ, &hKey) == ERROR_SUCCESS)
+    BOOL bResult = FALSE;
+    if (RegKey.Open(HKEY_CURRENT_USER, L"Software\\ReactOS\\rapps", KEY_READ) == ERROR_SUCCESS)
     {
         dwSize = sizeof(SettingsInfo);
-        if (RegQueryValueExW(hKey, L"Settings", NULL, NULL, (LPBYTE)&SettingsInfo, &dwSize) == ERROR_SUCCESS)
-        {
-            RegCloseKey(hKey);
-            return TRUE;
-        }
+        bResult = (RegKey.QueryBinaryValue(L"Settings", (PVOID) &SettingsInfo, &dwSize) == ERROR_SUCCESS);
 
-        RegCloseKey(hKey);
+        RegKey.Close();
     }
 
-    return FALSE;
+    return bResult;
 }
 
-VOID
-SaveSettings(HWND hwnd)
+VOID SaveSettings(HWND hwnd)
 {
     WINDOWPLACEMENT wp;
-    HKEY hKey;
+    ATL::CRegKey RegKey;
 
     if (SettingsInfo.bSaveWndPos)
     {
@@ -110,38 +106,34 @@ SaveSettings(HWND hwnd)
         GetWindowPlacement(hwnd, &wp);
 
         SettingsInfo.Left = wp.rcNormalPosition.left;
-        SettingsInfo.Top  = wp.rcNormalPosition.top;
-        SettingsInfo.Width  = wp.rcNormalPosition.right - wp.rcNormalPosition.left;
+        SettingsInfo.Top = wp.rcNormalPosition.top;
+        SettingsInfo.Width = wp.rcNormalPosition.right - wp.rcNormalPosition.left;
         SettingsInfo.Height = wp.rcNormalPosition.bottom - wp.rcNormalPosition.top;
-        SettingsInfo.Maximized = (wp.showCmd == SW_MAXIMIZE || (wp.showCmd == SW_SHOWMINIMIZED && (wp.flags & WPF_RESTORETOMAXIMIZED)));
+        SettingsInfo.Maximized = (wp.showCmd == SW_MAXIMIZE
+                                  || (wp.showCmd == SW_SHOWMINIMIZED
+                                      && (wp.flags & WPF_RESTORETOMAXIMIZED)));
     }
 
-    if (RegCreateKeyExW(HKEY_CURRENT_USER, L"Software\\ReactOS\\rapps", 0, NULL,
-        REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS)
+    if (RegKey.Create(HKEY_CURRENT_USER, L"Software\\ReactOS\\rapps", NULL,
+                      REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, NULL) == ERROR_SUCCESS)
     {
-        RegSetValueExW(hKey, L"Settings", 0, REG_BINARY, (LPBYTE)&SettingsInfo, sizeof(SettingsInfo));
-        RegCloseKey(hKey);
+        RegKey.SetBinaryValue(L"Settings", (const PVOID) &SettingsInfo, sizeof(SettingsInfo));
+        RegKey.Close();
     }
 }
 
-int WINAPI
-wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nShowCmd)
+INT WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, INT nShowCmd)
 {
-    WCHAR szWindowClass[] = L"ROSAPPMGR";
-    HANDLE hMutex = NULL;
+    LPCWSTR szWindowClass = L"ROSAPPMGR";
+    HANDLE hMutex;
     HACCEL KeyBrd;
     MSG Msg;
 
     InitializeAtlModule(hInstance, TRUE);
 
-    switch (GetUserDefaultUILanguage())
+    if (GetUserDefaultUILanguage() == MAKELANGID(LANG_HEBREW, SUBLANG_DEFAULT))
     {
-        case MAKELANGID(LANG_HEBREW, SUBLANG_DEFAULT):
-            SetProcessDefaultLayout(LAYOUT_RTL);
-            break;
-
-        default:
-            break;
+        SetProcessDefaultLayout(LAYOUT_RTL);
     }
 
     hInst = hInstance;
@@ -164,33 +156,37 @@ wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nSh
     }
 
     InitLogs();
-
     InitCommonControls();
 
-    hMainWnd = CreateMainWindow();
-    if (!hMainWnd) goto Exit;
-
-    /* Maximize it if we must */
-    ShowWindow(hMainWnd, (SettingsInfo.bSaveWndPos && SettingsInfo.Maximized ? SW_MAXIMIZE : nShowCmd));
-    UpdateWindow(hMainWnd);
-
-    if (SettingsInfo.bUpdateAtStart)
-        UpdateAppsDB();
-
-    /* Load the menu hotkeys */
-    KeyBrd = LoadAccelerators(NULL, MAKEINTRESOURCE(HOTKEYS));
-
-    /* Message Loop */
-    while (GetMessage(&Msg, NULL, 0, 0))
+    //skip window creation if there were some keys
+    if (!CmdParser(lpCmdLine))
     {
-        if (!TranslateAccelerator(hMainWnd, KeyBrd, &Msg))
+        hMainWnd = CreateMainWindow();
+        if (hMainWnd)
         {
-            TranslateMessage(&Msg);
-            DispatchMessage(&Msg);
+            /* Maximize it if we must */
+            ShowWindow(hMainWnd, (SettingsInfo.bSaveWndPos && SettingsInfo.Maximized ? SW_MAXIMIZE : nShowCmd));
+            UpdateWindow(hMainWnd);
+
+            //TODO: get around the ugliness
+            if (SettingsInfo.bUpdateAtStart)
+                GetAvailableApps()->UpdateAppsDB();
+
+            /* Load the menu hotkeys */
+            KeyBrd = LoadAcceleratorsW(NULL, MAKEINTRESOURCEW(HOTKEYS));
+
+            /* Message Loop */
+            while (GetMessageW(&Msg, NULL, 0, 0))
+            {
+                if (!TranslateAcceleratorW(hMainWnd, KeyBrd, &Msg))
+                {
+                    TranslateMessage(&Msg);
+                    DispatchMessageW(&Msg);
+                }
+            }
         }
     }
 
-Exit:
     if (hMutex)
         CloseHandle(hMutex);