527227c90f4c5277c8f829c2ae0d58a8d4a566b5
[reactos.git] / reactos / base / applications / rapps / settingsdlg.cpp
1 /*
2 * PROJECT: ReactOS Applications Manager
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/applications/rapps/settingsdlg.cpp
5 * PURPOSE: Settings Dialog
6 * PROGRAMMERS: Dmitry Chapyshev (dmitry@reactos.org)
7 */
8
9 #include "rapps.h"
10
11 SETTINGS_INFO NewSettingsInfo;
12
13 #define IS_CHECKED(a, b) \
14 a = (SendDlgItemMessage(hDlg, b, BM_GETCHECK, 0, 0) == BST_CHECKED) ? TRUE : FALSE
15
16 BOOL
17 ChooseFolder(HWND hwnd)
18 {
19 BOOL bRet = FALSE;
20 BROWSEINFO bi;
21 WCHAR szPath[MAX_PATH], szBuf[MAX_STR_LEN];
22
23 LoadStringW(hInst, IDS_CHOOSE_FOLDER_TEXT, szBuf, _countof(szBuf));
24
25 ZeroMemory(&bi, sizeof(bi));
26 bi.hwndOwner = hwnd;
27 bi.pidlRoot = NULL;
28 bi.lpszTitle = szBuf;
29 bi.ulFlags = BIF_USENEWUI | BIF_DONTGOBELOWDOMAIN | BIF_RETURNONLYFSDIRS | /* BIF_BROWSEFILEJUNCTIONS | */ BIF_VALIDATE;
30
31 if (SUCCEEDED(CoInitializeEx(NULL, COINIT_APARTMENTTHREADED)))
32 {
33 LPITEMIDLIST lpItemList = SHBrowseForFolder(&bi);
34 if (lpItemList && SHGetPathFromIDList(lpItemList, szPath))
35 {
36 if (szPath[0] != 0)
37 {
38 SetDlgItemTextW(hwnd, IDC_DOWNLOAD_DIR_EDIT, szPath);
39 bRet = TRUE;
40 }
41 }
42
43 CoTaskMemFree(lpItemList);
44 CoUninitialize();
45 }
46
47 return bRet;
48 }
49
50 static VOID
51 InitSettingsControls(HWND hDlg, PSETTINGS_INFO Info)
52 {
53 SendDlgItemMessage(hDlg, IDC_SAVE_WINDOW_POS, BM_SETCHECK, Info->bSaveWndPos, 0);
54 SendDlgItemMessage(hDlg, IDC_UPDATE_AVLIST, BM_SETCHECK, Info->bUpdateAtStart, 0);
55 SendDlgItemMessage(hDlg, IDC_LOG_ENABLED, BM_SETCHECK, Info->bLogEnabled, 0);
56 SendDlgItemMessage(hDlg, IDC_DEL_AFTER_INSTALL, BM_SETCHECK, Info->bDelInstaller, 0);
57
58 SetWindowTextW(GetDlgItem(hDlg, IDC_DOWNLOAD_DIR_EDIT),
59 Info->szDownloadDir);
60
61 CheckRadioButton(hDlg, IDC_PROXY_DEFAULT, IDC_USE_PROXY, IDC_PROXY_DEFAULT+Info->Proxy);
62
63 if(IDC_PROXY_DEFAULT + Info->Proxy == IDC_USE_PROXY)
64 {
65 EnableWindow(GetDlgItem(hDlg, IDC_PROXY_SERVER), TRUE);
66 EnableWindow(GetDlgItem(hDlg, IDC_NO_PROXY_FOR), TRUE);
67 }
68
69 SetWindowTextW(GetDlgItem(hDlg, IDC_PROXY_SERVER), Info->szProxyServer);
70 SetWindowTextW(GetDlgItem(hDlg, IDC_NO_PROXY_FOR), Info->szNoProxyFor);
71 }
72
73 static
74 INT_PTR CALLBACK
75 SettingsDlgProc(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
76 {
77 switch (Msg)
78 {
79 case WM_INITDIALOG:
80 {
81 NewSettingsInfo = SettingsInfo;
82 InitSettingsControls(hDlg, &SettingsInfo);
83 }
84 break;
85
86 case WM_COMMAND:
87 {
88 switch (LOWORD(wParam))
89 {
90 case IDC_CHOOSE:
91 ChooseFolder(hDlg);
92 break;
93
94 case IDC_SAVE_WINDOW_POS:
95 IS_CHECKED(NewSettingsInfo.bSaveWndPos, IDC_SAVE_WINDOW_POS);
96 break;
97
98 case IDC_UPDATE_AVLIST:
99 IS_CHECKED(NewSettingsInfo.bUpdateAtStart, IDC_UPDATE_AVLIST);
100 break;
101
102 case IDC_LOG_ENABLED:
103 IS_CHECKED(NewSettingsInfo.bLogEnabled, IDC_LOG_ENABLED);
104 break;
105
106 case IDC_DEL_AFTER_INSTALL:
107 IS_CHECKED(NewSettingsInfo.bDelInstaller, IDC_DEL_AFTER_INSTALL);
108 break;
109
110 case IDC_PROXY_DEFAULT:
111 NewSettingsInfo.Proxy = 0;
112 EnableWindow(GetDlgItem(hDlg, IDC_PROXY_SERVER), FALSE);
113 EnableWindow(GetDlgItem(hDlg, IDC_NO_PROXY_FOR), FALSE);
114 break;
115
116 case IDC_NO_PROXY:
117 NewSettingsInfo.Proxy = 1;
118 EnableWindow(GetDlgItem(hDlg, IDC_PROXY_SERVER), FALSE);
119 EnableWindow(GetDlgItem(hDlg, IDC_NO_PROXY_FOR), FALSE);
120 break;
121
122 case IDC_USE_PROXY:
123 NewSettingsInfo.Proxy = 2;
124 EnableWindow(GetDlgItem(hDlg, IDC_PROXY_SERVER), TRUE);
125 EnableWindow(GetDlgItem(hDlg, IDC_NO_PROXY_FOR), TRUE);
126 break;
127
128 case IDC_DEFAULT_SETTINGS:
129 FillDefaultSettings(&NewSettingsInfo);
130 InitSettingsControls(hDlg, &NewSettingsInfo);
131 break;
132
133 case IDOK:
134 {
135 WCHAR szDir[MAX_PATH];
136 WCHAR szProxy[MAX_PATH];
137 WCHAR szNoProxy[MAX_PATH];
138 DWORD dwAttr;
139
140 GetWindowTextW(GetDlgItem(hDlg, IDC_DOWNLOAD_DIR_EDIT),
141 szDir, MAX_PATH);
142
143 GetWindowTextW(GetDlgItem(hDlg, IDC_PROXY_SERVER),
144 szProxy, MAX_PATH);
145 StringCbCopyW(NewSettingsInfo.szProxyServer, sizeof(NewSettingsInfo.szProxyServer), szProxy);
146
147 GetWindowTextW(GetDlgItem(hDlg, IDC_NO_PROXY_FOR),
148 szNoProxy, MAX_PATH);
149 StringCbCopyW(NewSettingsInfo.szNoProxyFor, sizeof(NewSettingsInfo.szNoProxyFor), szNoProxy);
150
151 dwAttr = GetFileAttributesW(szDir);
152 if (dwAttr != INVALID_FILE_ATTRIBUTES &&
153 (dwAttr & FILE_ATTRIBUTE_DIRECTORY))
154 {
155 StringCbCopyW(NewSettingsInfo.szDownloadDir,
156 sizeof(NewSettingsInfo.szDownloadDir),
157 szDir);
158 }
159 else
160 {
161 WCHAR szMsgText[MAX_STR_LEN];
162
163 LoadStringW(hInst,
164 IDS_CHOOSE_FOLDER_ERROR,
165 szMsgText, _countof(szMsgText));
166
167 if (MessageBoxW(hDlg, szMsgText, NULL, MB_YESNO) == IDYES)
168 {
169 if (CreateDirectoryW(szDir, NULL))
170 {
171 EndDialog(hDlg, LOWORD(wParam));
172 }
173 }
174 else
175 {
176 SetFocus(GetDlgItem(hDlg, IDC_DOWNLOAD_DIR_EDIT));
177 break;
178 }
179 }
180
181 SettingsInfo = NewSettingsInfo;
182 SaveSettings(GetParent(hDlg));
183 EndDialog(hDlg, LOWORD(wParam));
184 }
185 break;
186
187 case IDCANCEL:
188 EndDialog(hDlg, LOWORD(wParam));
189 break;
190 }
191 }
192 break;
193 }
194
195 return FALSE;
196 }
197
198 VOID
199 CreateSettingsDlg(HWND hwnd)
200 {
201 DialogBoxW(hInst,
202 MAKEINTRESOURCEW(IDD_SETTINGS_DIALOG),
203 hwnd,
204 SettingsDlgProc);
205 }