OSDN Git Service

Add support for Windows 7 jump list
[winmerge-jp/winmerge-jp.git] / Src / JumpList.cpp
1 /** 
2  * @file  JumpList.cpp
3  *
4  * @brief Implementation file for JumpList helper functions.
5  *
6  */
7 #include "JumpList.h"
8 #include <ObjBase.h>
9 #include <ShlObj.h>
10 #if _MSC_VER >= 1600
11 #include <propvarutil.h>
12 #include <propkey.h>
13 #endif
14 #include "unicoder.h"
15
16 namespace
17 {
18
19 std::wstring g_appid;
20 TCHAR g_exe_path[260];
21
22 IShellLink *CreateShellLink(const String& app_path, const String& params, const String& title, const String& desc, int icon_index)
23 {
24 #if _MSC_VER >= 1600
25         IShellLink *pShellLink = NULL;
26         if (FAILED(CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
27                                     IID_IShellLink, (void **)&pShellLink)))
28                 return NULL;
29
30         String app_path2(app_path);
31         if (app_path.empty())
32         {
33                 if (g_exe_path[0] == '\0')
34                         GetModuleFileName(NULL, g_exe_path, sizeof(g_exe_path));
35                 app_path2 = g_exe_path;
36         }
37         pShellLink->SetPath(app_path2.c_str());
38         pShellLink->SetIconLocation(app_path2.c_str(), icon_index);
39         pShellLink->SetArguments(params.c_str());
40         pShellLink->SetDescription(desc.c_str());
41
42         IPropertyStore *pPS = NULL;
43         if (SUCCEEDED(pShellLink->QueryInterface(IID_IPropertyStore, (void **)&pPS)))
44         {
45                 std::wstring title2 = ucr::toUTF16((title.empty()) ? params : title);
46                 PROPVARIANT pv;
47                 InitPropVariantFromString(title2.c_str(), &pv);
48                 pPS->SetValue(PKEY_Title, pv);
49                 PropVariantClear(&pv);
50                 pPS->Commit();
51                 pPS->Release();
52         }
53
54         return pShellLink;
55 #else
56         return NULL;
57 #endif
58 }
59
60 }
61
62 namespace JumpList
63 {
64
65 bool SetCurrentProcessExplicitAppUserModelID(const std::wstring& appid)
66 {
67         g_appid = appid;
68         HMODULE hLibrary = GetModuleHandle(_T("shell32.dll"));
69         if (!hLibrary)
70                 return false;
71         HRESULT (__stdcall *pfnSetCurrentProcessExplicitAppUserModelID)(PCWSTR AppID) = 
72                 (HRESULT (__stdcall *)(PCWSTR))GetProcAddress(hLibrary, "SetCurrentProcessExplicitAppUserModelID");
73         if (!pfnSetCurrentProcessExplicitAppUserModelID)
74                 return false;
75         return pfnSetCurrentProcessExplicitAppUserModelID(appid.c_str()) == S_OK;
76 }
77
78 bool AddToRecentDocs(const String& app_path, const String& params, const String& title, const String& desc, int icon_index)
79 {
80 #if _MSC_VER >= 1600
81         SHARDAPPIDINFOLINK saiil;
82         saiil.pszAppID = g_appid.c_str();
83         saiil.psl = CreateShellLink(app_path, params, title, desc, icon_index);
84         if (!saiil.psl)
85                 return false;
86         SHAddToRecentDocs(SHARD_APPIDINFOLINK, &saiil);
87         saiil.psl->Release();
88         return true;
89 #else
90         return false;
91 #endif
92 }
93
94 }