-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetting.cpp
More file actions
111 lines (95 loc) · 2.71 KB
/
Copy pathsetting.cpp
File metadata and controls
111 lines (95 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "stdafx.h"
#include "setting.h"
#include <shlobj.h>
namespace
{
const wchar_t kSectionGeneral[] = L"general";
const wchar_t kSectionScreen[] = L"screen";
const wchar_t kKeyStartup[] = L"startup";
const wchar_t kKeyBrightness[] = L"brightness";
const wchar_t kWorkFolderName[] = L"CareUEyes Lite";
const wchar_t kConfigFileName[] = L"setting.dat";
int ClampBrightness(int nBrightness)
{
if (nBrightness < 10)
return 10;
if (nBrightness > 100)
return 100;
return nBrightness;
}
BOOL GetCommonSettingFileName(WCHAR szPath[MAX_PATH])
{
ZeroMemory(szPath, sizeof(WCHAR) * MAX_PATH);
if (!SHGetSpecialFolderPathW(NULL, szPath, CSIDL_APPDATA, TRUE))
return FALSE;
PathAppendW(szPath, kWorkFolderName);
switch (SHCreateDirectoryExW(NULL, szPath, NULL))
{
case ERROR_SUCCESS:
case ERROR_FILE_EXISTS:
case ERROR_ALREADY_EXISTS:
break;
default:
return FALSE;
}
PathAppendW(szPath, kConfigFileName);
return TRUE;
}
}
CSetting* CSetting::GetInstance()
{
static CSetting setting;
return &setting;
}
CSetting::CSetting()
{
m_nBrightness = 80;
m_bStartupEnabled = TRUE;
ZeroMemory(m_szProfilePath, sizeof(m_szProfilePath));
ReadSettings();
}
void CSetting::ReadSettings()
{
WCHAR szPath[MAX_PATH] = {0};
if (!GetCommonSettingFileName(szPath))
return;
wcsncpy_s(m_szProfilePath, _countof(m_szProfilePath), szPath, _TRUNCATE);
m_bStartupEnabled = GetPrivateProfileIntW(
kSectionGeneral,
kKeyStartup,
m_bStartupEnabled ? 1 : 0,
m_szProfilePath) != 0;
m_nBrightness = ClampBrightness(GetPrivateProfileIntW(
kSectionScreen,
kKeyBrightness,
m_nBrightness,
m_szProfilePath));
}
void CSetting::SaveSetting()
{
WCHAR szValue[16] = {0};
_snwprintf_s(szValue, _countof(szValue), _TRUNCATE, L"%d", m_bStartupEnabled ? 1 : 0);
WritePrivateProfileStringW(kSectionGeneral, kKeyStartup, szValue, m_szProfilePath);
_snwprintf_s(szValue, _countof(szValue), _TRUNCATE, L"%d", m_nBrightness);
WritePrivateProfileStringW(kSectionScreen, kKeyBrightness, szValue, m_szProfilePath);
}
BOOL CSetting::SetBrightness(int nBrightness)
{
m_nBrightness = ClampBrightness(nBrightness);
return TRUE;
}
BOOL CSetting::GetBrightness(int& nBrightness)
{
nBrightness = m_nBrightness;
return TRUE;
}
BOOL CSetting::SetStartupEnabled(BOOL bEnabled)
{
m_bStartupEnabled = bEnabled ? TRUE : FALSE;
return TRUE;
}
BOOL CSetting::GetStartupEnabled(BOOL& bEnabled)
{
bEnabled = m_bStartupEnabled;
return TRUE;
}