Implement AddDesktopItem() and DeleteDesktopItem().
[reactos.git] / reactos / lib / userenv / desktop.c
1 /* $Id: desktop.c,v 1.1 2004/04/29 14:41:26 ekohl Exp $
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS system libraries
5 * FILE: lib/userenv/desktop.c
6 * PURPOSE: Desktop and start menu support functions.
7 * PROGRAMMER: Eric Kohl
8 */
9
10 #include <ntos.h>
11 #include <windows.h>
12 #include <userenv.h>
13 #include <tchar.h>
14 #include <shlobj.h>
15
16 #include "internal.h"
17
18
19 /* FUNCTIONS ***************************************************************/
20
21 static BOOL
22 GetDesktopPath (BOOL bCommonPath,
23 LPWSTR lpDesktopPath)
24 {
25 WCHAR szPath[MAX_PATH];
26 DWORD dwLength;
27 DWORD dwType;
28 HKEY hKey;
29
30 DPRINT ("GetDesktopPath() called\n");
31
32 if (RegOpenKeyExW (HKEY_CURRENT_USER,
33 L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders",
34 0,
35 KEY_ALL_ACCESS,
36 &hKey))
37 {
38 DPRINT1 ("RegOpenKeyExW() failed\n");
39 return FALSE;
40 }
41
42 dwLength = MAX_PATH * sizeof(WCHAR);
43 if (RegQueryValueExW (hKey,
44 bCommonPath ? L"Common Desktop" : L"Desktop",
45 0,
46 &dwType,
47 (LPBYTE)szPath,
48 &dwLength))
49 {
50 DPRINT1 ("RegQueryValueExW() failed\n");
51 RegCloseKey (hKey);
52 return FALSE;
53 }
54
55 RegCloseKey (hKey);
56
57 if (dwType == REG_EXPAND_SZ)
58 {
59 ExpandEnvironmentStringsW (szPath,
60 lpDesktopPath,
61 MAX_PATH);
62 }
63 else
64 {
65 wcscpy (lpDesktopPath, szPath);
66 }
67
68 DPRINT ("GetDesktopPath() done\n");
69
70 return TRUE;
71 }
72
73
74 BOOL WINAPI
75 AddDesktopItemA (BOOL bCommonItem,
76 LPCSTR lpItemName,
77 LPCSTR lpArguments,
78 LPCSTR lpIconLocation,
79 INT iIcon,
80 LPCSTR lpWorkingDirectory,
81 WORD wHotKey,
82 INT iShowCmd)
83 {
84 DPRINT1 ("AddDesktopItemA() not implemented!\n");
85 return FALSE;
86 }
87
88
89 BOOL WINAPI
90 AddDesktopItemW (BOOL bCommonItem,
91 LPCWSTR lpItemName,
92 LPCWSTR lpArguments,
93 LPCWSTR lpIconLocation,
94 INT iIcon,
95 LPCWSTR lpWorkingDirectory,
96 WORD wHotKey,
97 INT iShowCmd)
98 {
99 WCHAR szLinkPath[MAX_PATH];
100 WCHAR szArguments[MAX_PATH];
101 WCHAR szCommand[MAX_PATH];
102 LPWSTR Ptr;
103 DWORD dwLength;
104 IShellLinkW* psl;
105 IPersistFile* ppf;
106 HRESULT hr;
107 BOOL bResult;
108
109 DPRINT ("AddDesktopItemW() called\n");
110
111 bResult = FALSE;
112
113 if (!GetDesktopPath (bCommonItem, szLinkPath))
114 {
115 DPRINT1 ("GetDesktopPath() failed\n");
116 return FALSE;
117 }
118
119 DPRINT ("Desktop path: '%S'\n", szLinkPath);
120
121 /* FIXME: Make sure the path exists */
122
123 wcscat (szLinkPath, L"\\");
124 wcscat (szLinkPath, lpItemName);
125 wcscat (szLinkPath, L".lnk");
126 DPRINT ("Link path: '%S'\n", szLinkPath);
127
128 /* Split 'lpArguments' string into command and arguments */
129 Ptr = wcschr (lpArguments, L' ');
130 DPRINT ("Ptr %p lpArguments %p\n", Ptr, lpArguments);
131 if (Ptr != NULL)
132 {
133 dwLength = (DWORD)(Ptr - lpArguments);
134 DPRINT ("dwLength %lu\n", dwLength);
135 memcpy (szCommand, lpArguments, dwLength * sizeof(WCHAR));
136 szCommand[dwLength] = 0;
137 Ptr++;
138 wcscpy (szArguments, Ptr);
139 }
140 else
141 {
142 wcscpy (szCommand, lpArguments);
143 szArguments[0] = 0;
144 }
145 DPRINT ("szCommand: '%S'\n", szCommand);
146 DPRINT ("szArguments: '%S'\n", szArguments);
147
148 CoInitialize(NULL);
149
150 hr = CoCreateInstance(&CLSID_ShellLink,
151 NULL,
152 CLSCTX_INPROC_SERVER,
153 &IID_IShellLinkW,
154 (LPVOID*)&psl);
155 if (!SUCCEEDED(hr))
156 {
157 CoUninitialize();
158 return FALSE;
159 }
160
161 hr = psl->lpVtbl->QueryInterface(psl,
162 &IID_IPersistFile,
163 (LPVOID*)&ppf);
164 if (SUCCEEDED(hr))
165 {
166 psl->lpVtbl->SetDescription(psl,
167 lpItemName);
168
169 psl->lpVtbl->SetPath(psl,
170 szCommand);
171
172 psl->lpVtbl->SetArguments(psl,
173 szArguments);
174
175 psl->lpVtbl->SetIconLocation(psl,
176 lpIconLocation,
177 iIcon);
178
179 if (lpWorkingDirectory != NULL)
180 {
181 psl->lpVtbl->SetWorkingDirectory(psl,
182 lpWorkingDirectory);
183 }
184 else
185 {
186 psl->lpVtbl->SetWorkingDirectory(psl,
187 L"%HOMEDRIVE%%HOMEPATH%");
188 }
189
190 psl->lpVtbl->SetHotkey(psl,
191 wHotKey);
192
193 psl->lpVtbl->SetShowCmd(psl,
194 iShowCmd);
195
196 hr = ppf->lpVtbl->Save(ppf,
197 szLinkPath,
198 TRUE);
199 if (SUCCEEDED(hr))
200 bResult = TRUE;
201
202 ppf->lpVtbl->Release(ppf);
203 }
204
205 psl->lpVtbl->Release(psl);
206
207 CoUninitialize();
208
209 DPRINT ("AddDesktopItemW() done\n");
210
211 return bResult;
212 }
213
214
215 BOOL WINAPI
216 DeleteDesktopItemA (BOOL bCommonItem,
217 LPCSTR lpItemName)
218 {
219 DPRINT1 ("DeleteDesktopItemA() not implemented!\n");
220 return FALSE;
221 }
222
223
224 BOOL WINAPI
225 DeleteDesktopItemW (BOOL bCommonItem,
226 LPCWSTR lpItemName)
227 {
228 WCHAR szLinkPath[MAX_PATH];
229
230 DPRINT ("DeleteDesktopItemW() called\n");
231
232 if (!GetDesktopPath (bCommonItem, szLinkPath))
233 {
234 DPRINT1 ("GetDesktopPath() failed\n");
235 return FALSE;
236 }
237
238 wcscat (szLinkPath, L"\\");
239 wcscat (szLinkPath, lpItemName);
240 wcscat (szLinkPath, L".lnk");
241 DPRINT ("Link path: '%S'\n", szLinkPath);
242
243 return DeleteFile (szLinkPath);
244 }
245
246 /* EOF */