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