[RAPPS]
[reactos.git] / reactos / base / applications / rapps / settingsdlg.c
1 /*
2 * PROJECT: ReactOS Applications Manager
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/applications/rapps/settingsdlg.c
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, sizeof(szBuf) / sizeof(TCHAR));
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, SETTINGS_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
62 static
63 INT_PTR CALLBACK
64 SettingsDlgProc(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
65 {
66 switch (Msg)
67 {
68 case WM_INITDIALOG:
69 {
70 NewSettingsInfo = SettingsInfo;
71 InitSettingsControls(hDlg, SettingsInfo);
72 }
73 break;
74
75 case WM_COMMAND:
76 {
77 switch (LOWORD(wParam))
78 {
79 case IDC_CHOOSE:
80 ChooseFolder(hDlg);
81 break;
82
83 case IDC_SAVE_WINDOW_POS:
84 IS_CHECKED(NewSettingsInfo.bSaveWndPos, IDC_SAVE_WINDOW_POS);
85 break;
86
87 case IDC_UPDATE_AVLIST:
88 IS_CHECKED(NewSettingsInfo.bUpdateAtStart, IDC_UPDATE_AVLIST);
89 break;
90
91 case IDC_LOG_ENABLED:
92 IS_CHECKED(NewSettingsInfo.bLogEnabled, IDC_LOG_ENABLED);
93 break;
94
95 case IDC_DEL_AFTER_INSTALL:
96 IS_CHECKED(NewSettingsInfo.bDelInstaller, IDC_DEL_AFTER_INSTALL);
97 break;
98
99 case IDC_DEFAULT_SETTINGS:
100 FillDefaultSettings(&NewSettingsInfo);
101 InitSettingsControls(hDlg, NewSettingsInfo);
102 break;
103
104 case IDOK:
105 {
106 WCHAR szDir[MAX_PATH];
107 DWORD dwAttr;
108
109 GetWindowTextW(GetDlgItem(hDlg, IDC_DOWNLOAD_DIR_EDIT),
110 szDir, MAX_PATH);
111
112 dwAttr = GetFileAttributesW(szDir);
113 if (dwAttr != INVALID_FILE_ATTRIBUTES &&
114 (dwAttr & FILE_ATTRIBUTE_DIRECTORY))
115 {
116 StringCbCopyW(NewSettingsInfo.szDownloadDir,
117 sizeof(NewSettingsInfo.szDownloadDir),
118 szDir);
119 }
120 else
121 {
122 WCHAR szMsgText[MAX_STR_LEN];
123
124 LoadStringW(hInst,
125 IDS_CHOOSE_FOLDER_ERROR,
126 szMsgText, sizeof(szMsgText) / sizeof(WCHAR));
127
128 if (MessageBoxW(hDlg, szMsgText, NULL, MB_YESNO) == IDYES)
129 {
130 if (CreateDirectoryW(szDir, NULL))
131 {
132 EndDialog(hDlg, LOWORD(wParam));
133 }
134 }
135
136 SetFocus(GetDlgItem(hDlg, IDC_DOWNLOAD_DIR_EDIT));
137 break;
138 }
139
140 SettingsInfo = NewSettingsInfo;
141 SaveSettings(GetParent(hDlg));
142 EndDialog(hDlg, LOWORD(wParam));
143 }
144 break;
145
146 case IDCANCEL:
147 EndDialog(hDlg, LOWORD(wParam));
148 break;
149 }
150 }
151 break;
152 }
153
154 return FALSE;
155 }
156
157 VOID
158 CreateSettingsDlg(HWND hwnd)
159 {
160 DialogBoxW(hInst,
161 MAKEINTRESOURCEW(IDD_SETTINGS_DIALOG),
162 hwnd,
163 SettingsDlgProc);
164 }