/******************************************************************************
*
*
* Notepad2
*
* Appreg.c
*   Registry helper functions
*
* See Readme.txt for more information about this source code.
* Please send me your comments to this work.
*
* Distributed under the terms of the GNU General Public License,
* see License.txt for details.
*
*                                              (c) Florian Balmer 1996-2004
*                                                       textview@bluewin.ch
*                                               http://www.flos-freeware.ch
*
*
******************************************************************************/
#include <windows.h>
#include "helpers.h"
#include "appreg.h"
#include "resource.h"


HKEY RegGetAppKey(LPCSTR lpSubKey)
{

  HKEY  hKey;
  char  szKey[256];
  DWORD dwDisp;
  LONG  nRet;

  lstrcpy(szKey,"Software\\Notepad2");
  if (lpSubKey)
  {
    lstrcat(szKey,"\\");
    lstrcat(szKey,lpSubKey);
  }

  nRet = RegCreateKeyEx(
           HKEY_CURRENT_USER,
           szKey,
           0,
           NULL,
           0,
           KEY_ALL_ACCESS,
           NULL,
           &hKey,
           &dwDisp);

  if (nRet == ERROR_SUCCESS)
    return(hKey);

  else
    return(NULL);

}


HKEY RegGetSubKey(HKEY hKeyRoot,LPCSTR lpSubKey)
{

  HKEY  hKey;
  DWORD dwDisp;
  LONG  nRet;

  nRet = RegCreateKeyEx(
           hKeyRoot,
           lpSubKey,
           0,
           NULL,
           0,
           KEY_ALL_ACCESS,
           NULL,
           &hKey,
           &dwDisp);

  if (nRet == ERROR_SUCCESS)
    return(hKey);

  else
    return(NULL);

}


HKEY RegGetKey(LPCSTR lpKey)
{

  HKEY  hKey;
  DWORD dwDisp;
  LONG  nRet;

  nRet = RegCreateKeyEx(
           HKEY_CURRENT_USER,
           lpKey,
           0,
           NULL,
           0,
           KEY_ALL_ACCESS,
           NULL,
           &hKey,
           &dwDisp);

  if (nRet == ERROR_SUCCESS)
    return(hKey);

  else
    return(NULL);

}


void RegDeleteKeyEx(HKEY hKey,LPCSTR pszSubKey,BOOL *pbError)
{

  HKEY  hSubKey;
  char  szSubSubKey[64];
  DWORD dwSubSubKey = 64;

  if (ERROR_SUCCESS == RegOpenKeyEx(hKey,
                                    pszSubKey,
                                    0,
                                    KEY_ALL_ACCESS,
                                    &hSubKey))
  {

    while (ERROR_SUCCESS == RegEnumKeyEx(hSubKey,
                                         0,
                                         szSubSubKey,
                                         &dwSubSubKey,
                                         NULL,
                                         NULL,
                                         0,
                                         NULL))
    {

      RegDeleteKeyEx(hSubKey,szSubSubKey,pbError);
      dwSubSubKey = 64;

    }

    RegCloseKey(hSubKey);

    if (ERROR_SUCCESS != RegDeleteKey(hKey,pszSubKey))
      *pbError = TRUE;

  }

  else
    *pbError = TRUE;

}


int RegGetInt(HKEY hKey,LPCSTR lpName,int nDefault)
{

  LONG  nRet;
  DWORD dwValue;
  DWORD cbData = sizeof(DWORD);
  DWORD dwType = REG_DWORD;

  nRet = RegQueryValueEx(
           hKey,
           lpName,
           NULL,
           &dwType,
           (LPBYTE)&dwValue,
           &cbData);

  if (nRet == ERROR_SUCCESS)
    return(int)dwValue;

  else
    return(nDefault);

}


BOOL RegGetString(HKEY hKey,LPCSTR lpName,LPCSTR lpDefault,LPSTR lpBuf,int nBuf)
{

  LONG  nRet;
  DWORD cbData = nBuf;
  DWORD dwType = REG_SZ;

  nRet = RegQueryValueEx(
           hKey,
           lpName,
           NULL,
           &dwType,
           (LPBYTE)lpBuf,
           &cbData);

  if (nRet == ERROR_SUCCESS)
    return (lstrlen(lpBuf));

  else
  {
    lstrcpy(lpBuf,lpDefault);
    return FALSE;
  }

}


void RegSetInt(HKEY hKey,LPCSTR lpName,int nValue)
{

  RegSetValueEx(hKey,
                lpName,
                0,
                REG_DWORD,
                (LPBYTE)&nValue,
                sizeof(int));

}


void RegSetString(HKEY hKey,LPCSTR lpName,LPCSTR lpValue)
{

  if (lpValue && *lpValue) {

    RegSetValueEx(hKey,
                  lpName,
                  0,
                  REG_SZ,
                  (LPBYTE)lpValue,
                  lstrlen(lpValue)+1); }

  else {

    RegSetValueEx(hKey,
                  lpName,
                  0,
                  REG_SZ,
                  (LPBYTE)"\0",
                  1); }

}


int RegGetAppIntEx(LPCSTR lpKey, LPCSTR lpName, int nDefault)
{
  HKEY hKey = RegGetAppKey(lpKey);
  int  nValue = RegGetInt(hKey,lpName,nDefault);
  RegCloseKey(hKey);
  return(nValue);
}


void RegSetAppIntEx(LPCSTR lpKey, LPCSTR lpName, int nValue)
{
  HKEY hKey = RegGetAppKey(lpKey);
  RegSetInt(hKey,lpName,nValue);
  RegCloseKey(hKey);
}



/******************************************************************************
*
*
* UnregisterApp()
*
* Removes the Textview registry entry
*
*
******************************************************************************/
void UnregisterApp(HINSTANCE hInstance)
{

  MSGBOXPARAMS mbp;

  char szTitle[32];
  char szText[256];

  GetString(IDS_APPTITLE,szTitle,COUNTOF(szTitle));
  GetString(IDS_UNREG1,szText,COUNTOF(szText));

  mbp.cbSize = sizeof(MSGBOXPARAMS);
  mbp.hwndOwner = NULL;
  mbp.hInstance = hInstance;
  mbp.lpszText = szText;
  mbp.lpszCaption = szTitle;
  mbp.dwStyle = MB_YESNO | MB_USERICON | MB_SETFOREGROUND;
  mbp.lpszIcon = MAKEINTRESOURCE(IDR_MAINWND);
  mbp.dwContextHelpId = 0;
  mbp.lpfnMsgBoxCallback = NULL;
  mbp.dwLanguageId = MAKELANGID(LANG_NEUTRAL,SUBLANG_NEUTRAL);

  if (MessageBoxIndirect(&mbp) == IDYES)
  {
    BOOL bError;
    RegDeleteKeyEx(HKEY_CURRENT_USER,"Software\\Notepad2",&bError);

    GetString(IDS_UNREG2,szText,COUNTOF(szText));
    mbp.dwStyle = MB_OK | MB_USERICON | MB_SETFOREGROUND;

    MessageBoxIndirect(&mbp);
  }

}


// End of Appreg.h
