forked from coddec/Classic-Shell
- Notifications
You must be signed in to change notification settings - Fork 452
/
Copy pathStartMenuExt.cpp
367 lines (343 loc) · 10.8 KB
/
StartMenuExt.cpp
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// StartMenuExt.cpp : Implementation of CStartMenuExt
#include"stdafx.h"
#include"StartMenuExt.h"
#include"dllmain.h"
#include"StringUtils.h"
#include"Translations.h"
#include"Settings.h"
#include"ComHelper.h"
#include"FileHelper.h"
#include<propvarutil.h>
#include<propkey.h>
#include<vector>
#definePIN_VERB"pin_classic"
// CStartMenuExt
static CLIPFORMAT s_ShellFormat;
#defineGetPIDLFolder(pida) (LPCITEMIDLIST)(((LPBYTE)pida)+(pida)->aoffset[0])
#defineGetPIDLItem(pida, i) (LPCITEMIDLIST)(((LPBYTE)pida)+(pida)->aoffset[i+1])
// FOLDERID_AppsFolder is defined in the 8.0 SDK, but we don't want to require it
KNOWNFOLDERID FOLDERID_AppsFolder2={0x1E87508D,0x89C2,0x42F0,{0x8A,0x7E,0x64,0x5A,0x0F,0x50,0xCA,0x58}}; // similar to shell:::{4234d49b-0245-4df3-b780-3893943456e1}
static HRESULT CreateLink( constwchar_t *source, LPITEMIDLIST sourcePidl, constwchar_t *dir, constwchar_t *fname )
{
HRESULT hr;
{
CComPtr<IShellLink> pLink;
hr=pLink.CoCreateInstance(CLSID_ShellLink);
if (FAILED(hr)) return hr;
if (sourcePidl)
hr=pLink->SetIDList(sourcePidl);
else
hr=pLink->SetPath(source);
if (FAILED(hr)) return hr;
if (dir)
pLink->SetWorkingDirectory(dir);
CComQIPtr<IPersistFile> pFile(pLink);
if (!pFile) return E_FAIL;
hr=pFile->Save(fname,TRUE);
}
{
// reopen the link and set the "no new" property. without reopening the original properties are lost
CComPtr<IShellLink> pLink;
hr=pLink.CoCreateInstance(CLSID_ShellLink);
CComQIPtr<IPersistFile> pFile(pLink);
hr=pFile->Load(fname,STGM_READWRITE);
CComQIPtr<IPropertyStore> pStore(pLink);
if (pStore)
{
PROPVARIANT val;
InitPropVariantFromBoolean(TRUE,&val);
pStore->SetValue(PKEY_AppUserModel_ExcludeFromShowInNewInstall,val);
PropVariantClear(&val);
pStore->Commit();
}
hr=pFile->Save(fname,TRUE);
}
HANDLE h=CreateFile(fname,GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if (h!=INVALID_HANDLE_VALUE)
{
FILETIME curTime;
GetSystemTimeAsFileTime(&curTime);
SetFileTime(h,&curTime,&curTime,&curTime);
CloseHandle(h);
}
return S_OK;
}
HRESULT WINAPI CStartMenuExt::UpdateRegistry( BOOL bRegister )
{
return _AtlModule.UpdateRegistryFromResource(IDR_STARTMENUEXT,bRegister);
}
STDMETHODIMP CStartMenuExt::Initialize( PCIDLIST_ABSOLUTE pidlFolder, IDataObject *pData, HKEY hkey )
{
WaitDllInitThread();
LoadSettings();
s_ShellFormat=RegisterClipboardFormat(CFSTR_SHELLIDLIST);
if (GetSettingBool(L"DisablePinExt"))
return E_FAIL;
bool bUsePinned;
bool bDef;
int setting=GetSettingInt(L"PinnedPrograms",bDef);
if (bDef)
bUsePinned=(GetSettingInt(L"MenuStyle")==2); // pinned is default in Win7 style
else
bUsePinned=(setting==1);
if (bUsePinned)
{
Sprintf(m_PinFolder1,_countof(m_PinFolder1),L"%s\\",GetSettingString(L"PinnedItemsPath"));
DoEnvironmentSubst(m_PinFolder1,_countof(m_PinFolder1));
m_PinFolder2[0]=0;
}
else
{
CString str=GetSettingString(L"FolderStartMenu");
if (!str.IsEmpty())
{
Sprintf(m_PinFolder1,_countof(m_PinFolder1),L"%s\\",str);
DoEnvironmentSubst(m_PinFolder1,_countof(m_PinFolder1));
}
else
{
CComString pPath;
if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_StartMenu,0,NULL,&pPath)))
Sprintf(m_PinFolder1,_countof(m_PinFolder1),L"%s\\",(constwchar_t*)pPath);
}
str=GetSettingString(L"FolderCommonStartMenu");
if (!str.IsEmpty())
{
Sprintf(m_PinFolder2,_countof(m_PinFolder2),L"%s\\",str);
DoEnvironmentSubst(m_PinFolder2,_countof(m_PinFolder2));
}
else
{
CComString pPath;
if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_CommonStartMenu,0,NULL,&pPath)))
Sprintf(m_PinFolder2,_countof(m_PinFolder2),L"%s\\",(constwchar_t*)pPath);
}
}
FORMATETC fmt1={CF_HDROP,NULL,DVASPECT_CONTENT,-1,TYMED_HGLOBAL};
FORMATETC fmt2={s_ShellFormat,NULL,DVASPECT_CONTENT,-1,TYMED_HGLOBAL};
STGMEDIUM stg={TYMED_HGLOBAL};
if (pData && SUCCEEDED(pData->GetData(&fmt1,&stg)))
{
HDROP hDrop=(HDROP)GlobalLock(stg.hGlobal);
if (hDrop)
{
if (DragQueryFile(hDrop,0xFFFFFFFF,NULL,0)==1)
{
DragQueryFile(hDrop,0,m_FileName,_countof(m_FileName));
constwchar_t *fname=PathFindFileName(m_FileName);
m_bInPinFolder1=(fname-m_FileName==Strlen(m_PinFolder1) && _wcsnicmp(m_PinFolder1,m_FileName,(fname-m_FileName))==0);
m_bInPinFolder2=(m_PinFolder2[0] && fname-m_FileName==Strlen(m_PinFolder2) && _wcsnicmp(m_PinFolder2,m_FileName,(fname-m_FileName))==0);
GlobalUnlock(stg.hGlobal);
ReleaseStgMedium(&stg);
return S_OK;
}
GlobalUnlock(stg.hGlobal);
}
ReleaseStgMedium(&stg);
}
elseif (pData && SUCCEEDED(pData->GetData(&fmt2,&stg)))
{
CIDA *pIda=(CIDA*)GlobalLock(stg.hGlobal);
if (pIda)
{
if (pIda->cidl==1)
{
LPCITEMIDLIST folderPidl=GetPIDLFolder(pIda);
LPCITEMIDLIST itemPidl=GetPIDLItem(pIda,0);
m_bInPinFolder1=m_bInPinFolder2=false;
LPITEMIDLIST appsFolder;
if (SUCCEEDED(SHGetKnownFolderIDList(FOLDERID_AppsFolder2,0,NULL,&appsFolder)) && ILIsEqual(folderPidl,appsFolder))
m_FilePidl=ILCombine(folderPidl,itemPidl);
GlobalUnlock(stg.hGlobal);
ReleaseStgMedium(&stg);
return m_FilePidl?S_OK:E_INVALIDARG;
}
GlobalUnlock(stg.hGlobal);
}
ReleaseStgMedium(&stg);
}
return E_INVALIDARG;
}
STDMETHODIMP CStartMenuExt::QueryContextMenu( HMENU menu, UINT uMenuIndex, UINT uidFirstCmd, UINT uidLastCmd, UINT uFlags )
{
if (uFlags&(CMF_DEFAULTONLY|CMF_VERBSONLY))
returnMAKE_HRESULT(SEVERITY_SUCCESS,FACILITY_NULL,0);
if (m_bInPinFolder2)
returnMAKE_HRESULT(SEVERITY_SUCCESS,FACILITY_NULL,0); // can't unpin from common folder
bool bDirectory=(PathIsDirectory(m_FileName)!=FALSE);
bool bFakeDirectory=bDirectory && IsFakeFolder(m_FileName);
constwchar_t *ext=PathFindExtension(m_FileName);
if (bDirectory || m_FilePidl || _wcsicmp(ext,L".exe")==0 || _wcsicmp(ext,L".lnk")==0)
{
if (m_bInPinFolder1)
{
if (!bFakeDirectory && _wcsicmp(ext,L".lnk")!=0)
returnMAKE_HRESULT(SEVERITY_SUCCESS,FACILITY_NULL,0); // can only unpin fake folders and links
InsertMenu(menu,uMenuIndex,MF_BYPOSITION,uidFirstCmd,FindTranslation(L"Menu.UnpinStartCs",L"Unpin from Start menu (Open-Shell)"));
}
else
{
if (bFakeDirectory)
returnMAKE_HRESULT(SEVERITY_SUCCESS,FACILITY_NULL,0); // can't pin fake folders
InsertMenu(menu,uMenuIndex,MF_BYPOSITION,uidFirstCmd,FindTranslation(L"Menu.PinStartCs",L"Pin to Start menu (Open-Shell)"));
}
returnMAKE_HRESULT(SEVERITY_SUCCESS,FACILITY_NULL,1);
}
returnMAKE_HRESULT(SEVERITY_SUCCESS,FACILITY_NULL,0);
}
static CString GetFileDescription( constwchar_t *fname )
{
DWORD q;
DWORD size=GetFileVersionInfoSizeEx(FILE_VER_GET_LOCALISED|FILE_VER_GET_NEUTRAL,fname,&q);
if (size)
{
std::vector<char> buf(size);
if (GetFileVersionInfoEx(FILE_VER_GET_LOCALISED|FILE_VER_GET_NEUTRAL,fname,0,size,&buf[0]))
{
wchar_t languages[100];
size=0;
DWORD len=_countof(languages);
GetUserPreferredUILanguages(MUI_LANGUAGE_ID,&size,languages,&len);
for (constwchar_t *lang=languages;*lang;lang+=Strlen(lang)+1)
{
wchar_t key[100];
Sprintf(key,_countof(key),L"\\StringFileInfo\\%s%04X\\FileDescription",lang,1200);
constwchar_t *text=NULL;
UINT q;
if (VerQueryValue(&buf[0],key,(void**)&text,&q))
return text;
}
DWORD *langs;
UINT count;
if (VerQueryValue(&buf[0],L"\\VarFileInfo\\Translation",(void**)&langs,&count))
{
count/=4;
for (DWORD i=0;i<count;i++)
{
if (HIWORD(langs[i])==1200)
{
wchar_t key[100];
Sprintf(key,_countof(key),L"\\StringFileInfo\\%04X%04X\\FileDescription",LOWORD(langs[i]),1200);
constwchar_t *text=NULL;
UINT q;
if (VerQueryValue(&buf[0],key,(void**)&text,&q))
return text;
}
}
}
}
}
returnCString();
}
STDMETHODIMP CStartMenuExt::InvokeCommand( CMINVOKECOMMANDINFO *pInfo )
{
if (pInfo->lpVerb==NULL || (!IS_INTRESOURCE(pInfo->lpVerb) && strcmp(pInfo->lpVerb,PIN_VERB)==0))
{
bool bDirectory=(*m_FileName && PathIsDirectory(m_FileName)!=FALSE);
bool bFakeDirectory=bDirectory && IsFakeFolder(m_FileName);
constwchar_t *fname=PathFindFileName(m_FileName);
constwchar_t *ext=PathFindExtension(fname);
if (m_bInPinFolder1)
{
// the file is in the user pin folder - unpin
if (!bFakeDirectory && _wcsicmp(ext,L".lnk")!=0)
return E_FAIL; // can only unpin fake folders and links
if (bFakeDirectory)
DeleteFakeFolder(m_FileName);
else
{
SetFileAttributes(m_FileName,GetFileAttributes(m_FileName)&~FILE_ATTRIBUTE_READONLY);
DeleteFile(m_FileName);
}
}
elseif (!m_bInPinFolder2)
{
// the file is not in any pin folder - pin
if (bFakeDirectory)
return E_FAIL; // can't pin fake folders
wchar_t locName[_MAX_PATH];
int locIndex;
bool bHasLocName=(*m_FileName && SHGetLocalizedName(m_FileName,locName,_countof(locName),&locIndex)==S_OK);
wchar_t finalPath[_MAX_PATH];
bool res=false;
if (bDirectory)
{
int len=Strlen(fname);
while (len>0 && (fname[len-1]=='\\' || fname[len-1]==':'))
len--;
wchar_t path[_MAX_PATH];
Sprintf(path,_countof(path),L"%s%.*s",m_PinFolder1,len,fname);
PathYetAnotherMakeUniqueName(finalPath,path,NULL,PathFindFileName(path));
res=CreateFakeFolder(m_FileName,finalPath);
}
else
{
wchar_t name[_MAX_FNAME];
name[0]=0;
if (m_FilePidl)
{
CComPtr<IShellItem> pItem;
SHCreateItemFromIDList(m_FilePidl,IID_IShellItem,(void**)&pItem);
if (pItem)
{
wchar_t *pName;
if (SUCCEEDED(pItem->GetDisplayName(SIGDN_NORMALDISPLAY,&pName)))
{
Strcpy(name,_countof(name),pName);
CoTaskMemFree(pName);
}
}
}
elseif (_wcsicmp(ext,L".exe")==0 && !bHasLocName)
{
CString desc=GetFileDescription(m_FileName);
Strcpy(name,_countof(name),desc);
}
if (!name[0])
{
memcpy(name,fname,(ext-fname)*2);
name[ext-fname]=0;
}
wchar_t path[_MAX_PATH];
Sprintf(path,_countof(path),L"%s%s.lnk",m_PinFolder1,name);
PathYetAnotherMakeUniqueName(finalPath,path,NULL,PathFindFileName(path));
if (m_FilePidl)
{
res=SUCCEEDED(CreateLink(NULL,m_FilePidl,NULL,finalPath));
}
elseif (_wcsicmp(ext,L".lnk")==0)
{
res=SUCCEEDED(CreateLink(m_FileName,NULL,NULL,finalPath));
}
else
{
wchar_t dir[_MAX_PATH];
Strcpy(dir,_countof(dir),m_FileName);
PathRemoveFileSpec(dir);
res=SUCCEEDED(CreateLink(m_FileName,NULL,dir,finalPath));
}
}
if (!res)
return E_FAIL;
if (bHasLocName)
{
SHSetLocalizedName(finalPath,locName,locIndex);
}
}
return S_OK;
}
return E_FAIL;
}
STDMETHODIMP CStartMenuExt::GetCommandString( UINT_PTR idCmd, UINT uFlags, UINT* pwReserved, LPSTR pszName, UINT cchMax )
{
if (idCmd==0)
{
if (uFlags==GCS_VERBA)
{
Strcpy(pszName,cchMax,PIN_VERB);
return S_OK;
}
return E_NOTIMPL;
}
return E_INVALIDARG;
}