Attribute VB_Name = "modSettings"
' ----------------------------------------------------------------------------
'  Copyright (C) 2009 Noda (Yohan Lasorsa) - http://nodadev.wordpress.com
' ----------------------------------------------------------------------------
'  This software Is provided 'as-is', without any express or implied
'  warranty. In no event will the author be held liable for any damages
'  arising from the use of this software.
'
'  Permission is granted to anyone to use this software for any purpose,
'  including commercial applications, and to alter it and redistribute it
'  freely, subject to the following restrictions:
'
'  1. The origin of this software must not be misrepresented; you must not
'     claim that you wrote the original software. If you use this software
'     in a product, an acknowledgment in the product documentation would be
'     appreciated but is not required.
'  2. Altered source versions must be plainly marked as such, and must not be
'     misrepresented as being the original software.
'  3. This notice may not be removed or altered from any source distribution.
' ----------------------------------------------------------------------------

Option Explicit

Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" _
    (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, _
    ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" _
    (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, _
    ByVal lpFileName As String) As Long

Private Const CFG_EXTENSION As String = ".ini"
Private Const BIOS_REG_PATH As String = "HARDWARE\Description\System"
Private Const OS_VERSION_REG_PATH As String = "SOFTWARE\Microsoft\Windows NT\CurrentVersion"

Public Enum BiosVersion
    v3109 = 3109
    v3114 = 3114
    v3304 = 3304
    v3305 = 3305
End Enum

Public Enum fanControl
    Off = 0
    Slow = 1
    Auto = 2
    Custom = 3
End Enum

Public Type CFGDATA
    vistaOS As Boolean
    autostart As Boolean
    fullSizeTempIcon As Boolean
    singleIconMode As Boolean
    downscaling As Boolean
    timerInterval As Long
    tempTextColor As Long
    temp2TextColor As Long
    batteryTextColor As Long
    noHDClicks As Boolean
    showTime As Boolean
    bios As Integer
    fanControl As Integer
    fanStop As Integer
    fanSlow As Integer
    fanAuto As Integer
    procModeAC As Integer
    procModeDC As Integer
    customFanVal As Boolean
    fanValOff As Byte
    fanValSlow As Byte
    fanValAuto As Byte
    fanSlowThreshold As Integer
    resolutionWidth As Long
    resolutionHeight As Long
    useAltDriver As Boolean
End Type

Public settings As CFGDATA

Public Function CurrentPath() As String
    Dim path As String
    path = App.path
    If Right(path, 1) <> "\" Then path = path & "\"
    CurrentPath = path
End Function

Public Function ConfigFileExists() As Boolean
    ConfigFileExists = (Dir(CurrentPath & App.Title & CFG_EXTENSION) <> "")
End Function

Private Sub ReadBiosVersion()
    Dim res As String
    If (RegDoesKeyExist(HKEY_LOCAL_MACHINE, BIOS_REG_PATH)) Then
        res = RegQueryKey(HKEY_LOCAL_MACHINE, BIOS_REG_PATH, "SystemBiosVersion")
        If ((Right(res, 6) = "3114") Or (Right(res, 6) = "3109")) Then
            settings.bios = BiosVersion.v3114
'        ElseIf (Right(res, 6) = "3304") Then
        Else
            settings.bios = BiosVersion.v3304
        End If
    End If
End Sub

Private Sub CheckForVistaOS()
    Dim res As String
    If (RegDoesKeyExist(HKEY_LOCAL_MACHINE, OS_VERSION_REG_PATH)) Then
        res = RegQueryKey(HKEY_LOCAL_MACHINE, OS_VERSION_REG_PATH, "CurrentVersion")
        If (val(res) >= 6) Then
            settings.vistaOS = True
        Else
            settings.vistaOS = False
        End If
    End If
End Sub

Sub WriteSettings()
    Dim file As String
    file = CurrentPath & App.Title & CFG_EXTENSION

    On Error GoTo err_write_settings
    
    WriteCfgEntry "Advanced", "PreventHDClicks", CStr(settings.noHDClicks), file
    WriteCfgEntry "Advanced", "ShowRemainingBattIcon", CStr(settings.showTime), file
    WriteCfgEntry "Advanced", "TimerInterval", CStr(settings.timerInterval), file
    WriteCfgEntry "Advanced", "EnableDownscaling", CStr(settings.downscaling), file
    WriteCfgEntry "Advanced", "ShowFullSizeTempIcon", CStr(settings.fullSizeTempIcon), file
    WriteCfgEntry "Advanced", "SingleIconMode", CStr(settings.singleIconMode), file
    WriteCfgEntry "Fan", "Control", CStr(settings.fanControl), file
    WriteCfgEntry "Fan", "Stop", CStr(settings.fanStop), file
    WriteCfgEntry "Fan", "Slow", CStr(settings.fanSlow), file
    WriteCfgEntry "Fan", "Auto", CStr(settings.fanAuto), file
    WriteCfgEntry "Processor", "AC", CStr(settings.procModeAC), file
    WriteCfgEntry "Processor", "DC", CStr(settings.procModeDC), file
    WriteCfgEntry "Colors", "TemperatureTextColor", CStr("&H" & Hex$(settings.tempTextColor)), file
    WriteCfgEntry "Colors", "TemperatureTextColor2", CStr("&H" & Hex$(settings.temp2TextColor)), file
    WriteCfgEntry "Colors", "BatteryTextColor", CStr("&H" & Hex$(settings.batteryTextColor)), file
    WriteCfgEntry "Special", "UseCustomFanValue", CStr(settings.customFanVal), file
    WriteCfgEntry "Special", "FanValueOff", CStr("&H" & Hex$(settings.fanValOff)), file
    WriteCfgEntry "Special", "FanValueSlow", CStr("&H" & Hex$(settings.fanValSlow)), file
    WriteCfgEntry "Special", "FanValueAuto", CStr("&H" & Hex$(settings.fanValAuto)), file
    WriteCfgEntry "Special", "FanSlowModeThreshold", CStr(settings.fanSlowThreshold), file
    WriteCfgEntry "Special", "CustomResolutionWidth", CStr(settings.resolutionWidth), file
    WriteCfgEntry "Special", "CustomResolutionHeight", CStr(settings.resolutionHeight), file
'    WriteCfgEntry "Special", "UseAlternateDriver", CStr(settings.useAltDriver), file
    
    Exit Sub
    
err_write_settings:
    MsgBox "Error, can't save settings: " & Err.Description
End Sub

Sub ReadSettings()
    Dim file As String
    file = CurrentPath & App.Title & CFG_EXTENSION
    
    ReadBiosVersion
    CheckForVistaOS
    
    On Error GoTo err_read_settings
    
    With settings
        .noHDClicks = ReadCfgEntry("Advanced", "PreventHDClicks", file, False)
        .showTime = ReadCfgEntry("Advanced", "ShowRemainingBattIcon", file, True)
        .timerInterval = ReadCfgEntry("Advanced", "TimerInterval", file, 3000)
        .downscaling = ReadCfgEntry("Advanced", "EnableDownscaling", file, False)
        .fullSizeTempIcon = ReadCfgEntry("Advanced", "ShowFullSizeTempIcon", file, False)
        .singleIconMode = ReadCfgEntry("Advanced", "SingleIconMode", file, False)
        .tempTextColor = ReadCfgEntry("Colors", "TemperatureTextColor", file, ColorConstants.vbWhite)
        .temp2TextColor = ReadCfgEntry("Colors", "TemperatureTextColor2", file, RGB(96, 255, 64))
        .batteryTextColor = ReadCfgEntry("Colors", "BatteryTextColor", file, ColorConstants.vbBlack)
        .fanControl = ReadCfgEntry("Fan", "Control", file, 3)
        .fanStop = ReadCfgEntry("Fan", "Stop", file, 50)
        .fanSlow = ReadCfgEntry("Fan", "Slow", file, 60)
        .fanAuto = ReadCfgEntry("Fan", "Auto", file, 65)
        .procModeAC = ReadCfgEntry("Processor", "AC", file, 1)
        .procModeDC = ReadCfgEntry("Processor", "DC", file, 0)
        .customFanVal = ReadCfgEntry("Special", "UseCustomFanValue", file, False)
        .fanValOff = ReadCfgEntry("Special", "FanValueOff", file, &H20)
        .fanValSlow = ReadCfgEntry("Special", "FanValueSlow", file, &H10)
        .fanValAuto = ReadCfgEntry("Special", "FanValueAuto", file, &H0)
        .fanSlowThreshold = ReadCfgEntry("Special", "FanSlowModeThreshold", file, 40)
        .resolutionWidth = ReadCfgEntry("Special", "CustomResolutionWidth", file, 1024)
        .resolutionHeight = ReadCfgEntry("Special", "CustomResolutionHeight", file, 768)
        .useAltDriver = ReadCfgEntry("Special", "UseAlternateDriver", file, False)
    End With
    Exit Sub
    
err_read_settings:
'    MsgBox "Error, can't load settings: " & Err.Description
End Sub

Private Function WriteCfgEntry(section As String, key As String, value As String, _
    FileName As String) As Long
    
    WriteCfgEntry = WritePrivateProfileString(section, key, value, FileName)
End Function

Private Function ReadCfgEntry(section As String, key As String, FileName As String, _
    Optional defaultValue As String = "") As String

    Dim value As String
    value = String(255, 0)
    GetPrivateProfileString section, key, defaultValue, value, Len(value), FileName
    ReadCfgEntry = Left(value, InStr(value, Chr(0)) - 1)
End Function

