[REGEDIT]
[reactos.git] / base / applications / regedit / framewnd.c
index a6b4144..d11c8a0 100644 (file)
@@ -95,7 +95,7 @@ static void OnInitMenu(HWND hWnd)
     dwIndex = 0;
     do
     {
-        cbValueName = sizeof(szValueName) / sizeof(szValueName[0]);
+        cbValueName = COUNT_OF(szValueName);
         cbValueData = sizeof(abValueData);
         lResult = RegEnumValue(hKey, dwIndex, szValueName, &cbValueName, NULL, &dwType, abValueData, &cbValueData);
         if ((lResult == ERROR_SUCCESS) && (dwType == REG_SZ))
@@ -278,6 +278,108 @@ static BOOL InitOpenFileName(HWND hWnd, OPENFILENAME* pofn)
     pofn->lpstrFileTitle = FileTitleBuffer;
     pofn->nMaxFileTitle = _MAX_PATH;
     pofn->Flags = OFN_HIDEREADONLY;
+    pofn->lpstrDefExt = TEXT("reg");
+    return TRUE;
+}
+
+static INT_PTR CALLBACK LoadHive_KeyNameInHookProc(HWND hWndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
+{
+    static LPTSTR sKey = NULL;
+    static INT sLength = 0;
+    switch(uMsg)
+    {
+     case WM_INITDIALOG:
+        sKey = (LPTSTR)lParam;
+        sLength = 128; /* FIXME: Ugly hack! */
+     case WM_COMMAND:
+        switch(LOWORD(wParam))
+        {
+         case IDOK:
+            if(GetDlgItemText(hWndDlg, IDC_EDIT_KEY, sKey, sLength))
+                return EndDialog(hWndDlg, -1);
+            else
+                return EndDialog(hWndDlg, 0);
+         case IDCANCEL:
+            return EndDialog(hWndDlg, 0);
+        }
+        break;
+    }
+    return FALSE;
+}
+
+static BOOL LoadHive(HWND hWnd)
+{
+    OPENFILENAME ofn;
+    TCHAR Caption[128];
+    LPCTSTR pszKeyPath;
+    TCHAR xPath[128];
+    HKEY hRootKey;
+    TCHAR Filter[1024];
+    FILTERPAIR filter;
+    /* get the item key to load the hive in */
+    pszKeyPath = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
+    /* initialize the "open file" dialog */
+    InitOpenFileName(hWnd, &ofn);
+    /* build the "All Files" filter up */
+    filter.DisplayID = IDS_FLT_ALLFILES;
+    filter.FilterID = IDS_FLT_ALLFILES_FLT;
+    BuildFilterStrings(Filter, &filter, sizeof(filter));
+    ofn.lpstrFilter = Filter;
+    /* load and set the caption and flags for dialog */
+    LoadString(hInst, IDS_LOAD_HIVE, Caption, COUNT_OF(Caption));
+    ofn.lpstrTitle = Caption;
+    ofn.Flags |= OFN_ENABLESIZING;
+    /*    ofn.lCustData = ;*/
+    /* now load the hive */
+    if (GetOpenFileName(&ofn))
+    {
+        if(DialogBoxParam(hInst, MAKEINTRESOURCE(IDD_LOADHIVE), hWnd, &LoadHive_KeyNameInHookProc, (LPARAM)xPath))
+        {
+            LONG regLoadResult = RegLoadKey(hRootKey, xPath, ofn.lpstrFile);
+            if(regLoadResult == ERROR_SUCCESS)
+            {
+                /* refresh tree and list views */
+                RefreshTreeView(g_pChildWnd->hTreeWnd);
+                pszKeyPath = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
+                RefreshListView(g_pChildWnd->hListWnd, hRootKey, pszKeyPath);
+            }
+            else
+            {
+                ErrorMessageBox(hWnd, Caption, regLoadResult);
+                return FALSE;
+            }
+        }
+    } else {
+        CheckCommDlgError(hWnd);
+    }
+    return TRUE;
+}
+
+static BOOL UnloadHive(HWND hWnd)
+{
+    TCHAR Caption[128];
+    LPCTSTR pszKeyPath;
+    HKEY hRootKey;
+    LONG regUnloadResult;
+
+    /* get the item key to unload */
+    pszKeyPath = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
+    /* load and set the caption and flags for dialog */
+    LoadString(hInst, IDS_UNLOAD_HIVE, Caption, COUNT_OF(Caption));
+    /* now unload the hive */
+    regUnloadResult = RegUnLoadKey(hRootKey, pszKeyPath);
+    if(regUnloadResult == ERROR_SUCCESS)
+    {
+        /* refresh tree and list views */
+        RefreshTreeView(g_pChildWnd->hTreeWnd);
+        pszKeyPath = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
+        RefreshListView(g_pChildWnd->hListWnd, hRootKey, pszKeyPath);
+    }
+    else
+    {
+        ErrorMessageBox(hWnd, Caption, regUnloadResult);
+        return FALSE;
+    }
     return TRUE;
 }
 
@@ -285,42 +387,36 @@ static BOOL ImportRegistryFile(HWND hWnd)
 {
     OPENFILENAME ofn;
     TCHAR Caption[128];
+    LPCTSTR pszKeyPath;
+    HKEY hRootKey;
 
     InitOpenFileName(hWnd, &ofn);
-    LoadString(hInst, IDS_IMPORT_REG_FILE, Caption, sizeof(Caption)/sizeof(TCHAR));
+    LoadString(hInst, IDS_IMPORT_REG_FILE, Caption, COUNT_OF(Caption));
     ofn.lpstrTitle = Caption;
+    ofn.Flags |= OFN_ENABLESIZING;
     /*    ofn.lCustData = ;*/
     if (GetOpenFileName(&ofn)) {
-        /* FIXME - convert to ascii */
-       if (!import_registry_file(ofn.lpstrFile)) {
-            /*printf("Can't open file \"%s\"\n", ofn.lpstrFile);*/
+        FILE *fp = _wfopen(ofn.lpstrFile, L"r");
+        if (fp == NULL || !import_registry_file(fp)) {
+            LPSTR p = GetMultiByteString(ofn.lpstrFile);
+            fprintf(stderr, "Can't open file \"%s\"\n", p);
+            HeapFree(GetProcessHeap(), 0, p);
+            if (fp != NULL)
+                fclose(fp);
             return FALSE;
         }
-#if 0
-        get_file_name(&s, filename, MAX_PATH);
-        if (!filename[0]) {
-            printf("No file name is specified\n%s", usage);
-            return FALSE;
-            /*exit(1);*/
-        }
-        while (filename[0]) {
-            if (!import_registry_file(filename)) {
-                perror("");
-                printf("Can't open file \"%s\"\n", filename);
-                return FALSE;
-                /*exit(1);*/
-            }
-            get_file_name(&s, filename, MAX_PATH);
-        }
-#endif
-
+        fclose(fp);
     } else {
         CheckCommDlgError(hWnd);
     }
+
+    RefreshTreeView(g_pChildWnd->hTreeWnd);
+    pszKeyPath = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
+    RefreshListView(g_pChildWnd->hListWnd, hRootKey, pszKeyPath);
+
     return TRUE;
 }
 
-
 static UINT_PTR CALLBACK ExportRegistryFile_OFNHookProc(HWND hdlg, UINT uiMsg, WPARAM wParam, LPARAM lParam)
 {
     HWND hwndExportAll;
@@ -384,8 +480,7 @@ BOOL ExportRegistryFile(HWND hWnd)
 
     /* Figure out which key path we are exporting */
     pszKeyPath = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hKeyRoot);
-    RegKeyGetName(ExportKeyPath, sizeof(ExportKeyPath) / sizeof(ExportKeyPath[0]),
-        hKeyRoot, pszKeyPath);
+    GetKeyName(ExportKeyPath, COUNT_OF(ExportKeyPath), hKeyRoot, pszKeyPath);
 
     InitOpenFileName(hWnd, &ofn);
     LoadString(hInst, IDS_EXPORT_REG_FILE, Caption, sizeof(Caption)/sizeof(TCHAR));
@@ -396,44 +491,24 @@ BOOL ExportRegistryFile(HWND hWnd)
     {
         ofn.lCustData = (LPARAM) ExportKeyPath;
     }
-    ofn.Flags = OFN_ENABLETEMPLATE | OFN_EXPLORER | OFN_ENABLEHOOK;
+    ofn.Flags = OFN_ENABLETEMPLATE | OFN_EXPLORER | OFN_ENABLEHOOK | OFN_OVERWRITEPROMPT;
     ofn.lpfnHook = ExportRegistryFile_OFNHookProc;
     ofn.lpTemplateName = MAKEINTRESOURCE(IDD_EXPORTRANGE);
     if (GetSaveFileName(&ofn)) {
         BOOL result;
-        LPSTR pszExportKeyPath;
-#ifdef UNICODE
-        CHAR buffer[_MAX_PATH];
-
-        WideCharToMultiByte(CP_ACP, 0, ExportKeyPath, -1, buffer, sizeof(buffer), NULL, NULL);
-        pszExportKeyPath = buffer;
-#else
-        pszExportKeyPath = ExportKeyPath;
-#endif
+        DWORD format;
 
-        result = export_registry_key(ofn.lpstrFile, pszExportKeyPath);
+        if (ofn.nFilterIndex == 1)
+            format = REG_FORMAT_5;
+        else
+            format = REG_FORMAT_4;
+        result = export_registry_key(ofn.lpstrFile, ExportKeyPath, format);
         if (!result) {
-            /*printf("Can't open file \"%s\"\n", ofn.lpstrFile);*/
+            LPSTR p = GetMultiByteString(ofn.lpstrFile);
+            fprintf(stderr, "Can't open file \"%s\"\n", p);
+            HeapFree(GetProcessHeap(), 0, p);
             return FALSE;
         }
-#if 0
-        TCHAR filename[MAX_PATH];
-        filename[0] = '\0';
-        get_file_name(&s, filename, MAX_PATH);
-        if (!filename[0]) {
-            printf("No file name is specified\n%s", usage);
-            return FALSE;
-            /*exit(1);*/
-        }
-        if (s[0]) {
-            TCHAR reg_key_name[KEY_MAX_LEN];
-            get_file_name(&s, reg_key_name, KEY_MAX_LEN);
-            export_registry_key((CHAR)filename, reg_key_name);
-        } else {
-            export_registry_key(filename, NULL);
-        }
-#endif
-
     } else {
         CheckCommDlgError(hWnd);
     }
@@ -543,10 +618,10 @@ BOOL CopyKeyName(HWND hWnd, HKEY hRootKey, LPCTSTR keyName)
     if (!EmptyClipboard())
         goto done;
 
-    if (!RegKeyGetName(szBuffer, sizeof(szBuffer) / sizeof(szBuffer[0]), hRootKey, keyName))
+    if (!GetKeyName(szBuffer, COUNT_OF(szBuffer), hRootKey, keyName))
         goto done;
 
-    hGlobal = GlobalAlloc(GMEM_MOVEABLE, (_tcslen(szBuffer) + 1) * sizeof(TCHAR));
+    hGlobal = GlobalAlloc(GMEM_MOVEABLE, (lstrlen(szBuffer) + 1) * sizeof(TCHAR));
     if (!hGlobal)
         goto done;
 
@@ -578,21 +653,18 @@ static BOOL CreateNewValue(HKEY hRootKey, LPCTSTR pszKeyPath, DWORD dwType)
     HKEY hKey;
     LVFINDINFO lvfi;
 
-    if (RegOpenKey(hRootKey, pszKeyPath, &hKey) != ERROR_SUCCESS)
+    if (RegOpenKeyEx(hRootKey, pszKeyPath, 0, KEY_QUERY_VALUE | KEY_SET_VALUE,
+                     &hKey) != ERROR_SUCCESS)
         return FALSE;
 
-    LoadString(hInst, IDS_NEW_VALUE, szNewValueFormat, sizeof(szNewValueFormat)
-        / sizeof(szNewValueFormat[0]));
+    LoadString(hInst, IDS_NEW_VALUE, szNewValueFormat, COUNT_OF(szNewValueFormat));
 
     do
     {
-        _sntprintf(szNewValue, sizeof(szNewValue) / sizeof(szNewValue[0]),
-            szNewValueFormat, iIndex++);
-
+        wsprintf(szNewValue, szNewValueFormat, iIndex++);
         cbData = sizeof(data);
         lResult = RegQueryValueEx(hKey, szNewValue, NULL, &dwExistingType, data, &cbData);
-    }
-    while(lResult == ERROR_SUCCESS);
+    } while(lResult == ERROR_SUCCESS);
 
     switch(dwType) {
     case REG_DWORD:
@@ -614,8 +686,11 @@ static BOOL CreateNewValue(HKEY hRootKey, LPCTSTR pszKeyPath, DWORD dwType)
     }
     memset(data, 0, cbData);
     lResult = RegSetValueEx(hKey, szNewValue, 0, dwType, data, cbData);
+    RegCloseKey(hKey);
     if (lResult != ERROR_SUCCESS)
+    {
         return FALSE;
+    }
 
     RefreshListView(g_pChildWnd->hListWnd, hRootKey, pszKeyPath);
 
@@ -783,6 +858,12 @@ static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
     UNREFERENCED_PARAMETER(message);
 
     switch (LOWORD(wParam)) {
+    case ID_REGISTRY_LOADHIVE:
+        LoadHive(hWnd);
+        return TRUE;
+    case ID_REGISTRY_UNLOADHIVE:
+        UnloadHive(hWnd);
+        return TRUE;
     case ID_REGISTRY_IMPORTREGISTRYFILE:
         ImportRegistryFile(hWnd);
         return TRUE;
@@ -887,7 +968,7 @@ static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
                 }
             }
         }
-        if (GetFocus() == g_pChildWnd->hTreeWnd)
+        else if (GetFocus() == g_pChildWnd->hTreeWnd)
         {
             /* Get focused entry of treeview (if any) */
             HTREEITEM hItem = TreeView_GetSelection(g_pChildWnd->hTreeWnd);
@@ -930,8 +1011,8 @@ static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
               }
             }
           }
-        } else
-        if (GetFocus() == g_pChildWnd->hTreeWnd)
+        }
+        else if (GetFocus() == g_pChildWnd->hTreeWnd)
         {
           if (keyPath == 0 || *keyPath == 0)
           {
@@ -943,7 +1024,8 @@ static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
             RefreshTreeView(g_pChildWnd->hTreeWnd);
           }
         }
-       break;
+        break;
+    }
     case ID_EDIT_NEW_STRINGVALUE:
         CreateNewValue(hKeyRoot, keyPath, REG_SZ);
         break;
@@ -953,14 +1035,12 @@ static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
     case ID_EDIT_NEW_DWORDVALUE:
         CreateNewValue(hKeyRoot, keyPath, REG_DWORD);
         break;
-       case ID_EDIT_NEW_MULTISTRINGVALUE:
+    case ID_EDIT_NEW_MULTISTRINGVALUE:
         CreateNewValue(hKeyRoot, keyPath, REG_MULTI_SZ);
         break;
-       case ID_EDIT_NEW_EXPANDABLESTRINGVALUE:
+    case ID_EDIT_NEW_EXPANDABLESTRINGVALUE:
         CreateNewValue(hKeyRoot, keyPath, REG_EXPAND_SZ);
         break;
-
-    }
     case ID_EDIT_FIND:
         FindDialog(hWnd);
         break;