summaryrefslogtreecommitdiffstats
path: root/other-licenses/nsis/Contrib/ApplicationID/Set.cpp
blob: 82cb77e16f91b09f61170298752483b04fea362e (plain)
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
/*
 * Module : Set.cpp
 * Purpose: NSIS Plug-in for setting shortcut ApplicationID property
 * Created: 27/12/2009
 * Original code Copyright (c) 2009 Mike Anchor.  
 */

/*
 * Additional Mozilla contributions:
 *  Unicode support
 *  Jump list deletion on uninstall
 *  Pinned item removal on uninstall
 *  contrib: <jmathies@mozilla.com>
 */

#define INITGUID

#include <windows.h>
#include <shlobj.h>
#include <propvarutil.h>
#include <propkey.h>
#include <stdio.h>

#pragma comment (lib, "shlwapi.lib")

#define MAX_STRLEN 1024

typedef struct _stack_t {
  struct _stack_t *next;
  TCHAR text[MAX_PATH];
} stack_t;

stack_t **g_stacktop;
unsigned int g_stringsize;
TCHAR *g_variables;

// Indicates that an application supports dual desktop and immersive modes. In Windows 8, this property is only applicable for web browsers.
DEFINE_PROPERTYKEY(PKEY_AppUserModel_IsDualMode, 0x9F4C2855, 0x9F79, 0x4B39, 0xA8, 0xD0, 0xE1, 0xD4, 0x2D, 0xE1, 0xD5, 0xF3, 11);

int popstring(TCHAR *str, int len);
void pushstring(const TCHAR *str, int len);

extern "C" void __declspec(dllexport) Set(HWND hwndParent, int string_size, TCHAR *variables, stack_t **stacktop)
{
  g_stringsize = string_size;
  g_stacktop   = stacktop;
  g_variables  = variables;

  {
    IPropertyStore *m_pps = NULL;
    WCHAR wszPath[MAX_PATH];
    WCHAR wszAppID[MAX_PATH];
    TCHAR szPath[MAX_PATH];
    TCHAR szAppID[MAX_PATH];
    TCHAR szDualMode[MAX_PATH];
    bool success = false;

    ZeroMemory(wszPath, sizeof(wszPath));
    ZeroMemory(wszAppID, sizeof(wszAppID));
    ZeroMemory(szPath, sizeof(szPath));
    ZeroMemory(szAppID, sizeof(szAppID));
    ZeroMemory(szDualMode, sizeof(szDualMode));

    popstring(szPath, MAX_PATH);
    popstring(szAppID, MAX_PATH);
    bool dualMode = (popstring(szDualMode, MAX_PATH) == 0); // optional
#if !defined(UNICODE)
    MultiByteToWideChar(CP_ACP, 0, szPath, -1, wszPath, MAX_PATH);
    MultiByteToWideChar(CP_ACP, 0, szAppID, -1, wszAppID, MAX_PATH);
    if (dualMode && stricmp(szDualMode, "true") != 0) {
      dualMode = false;
    }
#else
    wcscpy_s(wszPath, szPath);
    wcscpy_s(wszAppID, szAppID);
    if (dualMode && _wcsicmp(szDualMode, L"true") != 0) {
      dualMode = false;
    }
#endif

    CoInitialize(NULL);

    if (SUCCEEDED(SHGetPropertyStoreFromParsingName(wszPath, NULL, GPS_READWRITE, IID_PPV_ARGS(&m_pps))))
    {
      PROPVARIANT propvar;
      if (SUCCEEDED(InitPropVariantFromString(wszAppID, &propvar))) {
        if (SUCCEEDED(m_pps->SetValue(PKEY_AppUserModel_ID, propvar))) {
          if (dualMode) {
            InitPropVariantFromBoolean(true, &propvar);
            m_pps->SetValue(PKEY_AppUserModel_IsDualMode, propvar);
          }
          if (SUCCEEDED(m_pps->Commit())) {
            success = true;
          }
        }
      }
    }    
    if (m_pps != NULL)
      m_pps->Release();

    CoUninitialize();

    pushstring(success == true ? TEXT("0") : TEXT("-1"), MAX_PATH);
  }
}

extern "C" void __declspec(dllexport) UninstallJumpLists(HWND hwndParent, int string_size, TCHAR *variables, stack_t **stacktop)
{
  g_stringsize = string_size;
  g_stacktop   = stacktop;
  g_variables  = variables;

  ICustomDestinationList *m_cdl = NULL;
  WCHAR wszAppID[MAX_PATH];
  TCHAR szAppID[MAX_PATH];
  bool success = false;

  ZeroMemory(wszAppID, sizeof(wszAppID));
  ZeroMemory(szAppID, sizeof(szAppID));

  popstring(szAppID, MAX_PATH);

#if !defined(UNICODE)
  MultiByteToWideChar(CP_ACP, 0, szAppID, -1, wszAppID, MAX_PATH);
#else
  wcscpy_s(wszAppID, szAppID);
#endif

  CoInitialize(NULL);
  
  CoCreateInstance(CLSID_DestinationList, NULL, CLSCTX_INPROC_SERVER,
                   IID_ICustomDestinationList, (void**)&m_cdl);

  if (m_cdl) {
    if (SUCCEEDED(m_cdl->DeleteList(wszAppID))) {
      success = true;
    }
  }

  if (m_cdl)
    m_cdl->Release();

  CoUninitialize();

  pushstring(success == true ? TEXT("0") : TEXT("-1"), MAX_PATH);
}

extern "C" void __declspec(dllexport) UninstallPinnedItem(HWND hwndParent, int string_size, TCHAR *variables, stack_t **stacktop)
{
  g_stringsize = string_size;
  g_stacktop   = stacktop;
  g_variables  = variables;

  IShellItem *pItem = NULL;
  IStartMenuPinnedList *pPinnedList = NULL;
  WCHAR wszPath[MAX_PATH];
  TCHAR szPath[MAX_PATH];
  bool success = false;

  ZeroMemory(wszPath, sizeof(wszPath));
  ZeroMemory(szPath, sizeof(szPath));

  popstring(szPath, MAX_PATH);

#if !defined(UNICODE)
  MultiByteToWideChar(CP_ACP, 0, szPath, -1, wszPath, MAX_PATH);
#else
  wcscpy_s(wszPath, szPath);
#endif

  CoInitialize(NULL);

  HRESULT hr;
  hr = SHCreateItemFromParsingName(wszPath, NULL, IID_PPV_ARGS(&pItem));

  if (SUCCEEDED(hr)) {

      hr = CoCreateInstance(CLSID_StartMenuPin, 
                            NULL, 
                            CLSCTX_INPROC_SERVER, 
                            IID_PPV_ARGS(&pPinnedList));
      
      if (SUCCEEDED(hr)) {
          hr = pPinnedList->RemoveFromList(pItem);
          pPinnedList->Release();
          success = true;
      }
      
      pItem->Release();
  }

  CoUninitialize();

  pushstring(success == true ? TEXT("0") : TEXT("-1"), MAX_PATH);
}

//Function: Removes the element from the top of the NSIS stack and puts it in the buffer
int popstring(TCHAR *str, int len)
{
  stack_t *th;
  if (!g_stacktop || !*g_stacktop) return 1;
  th=(*g_stacktop);
  lstrcpyn(str,th->text, len);
  *g_stacktop=th->next;
  GlobalFree((HGLOBAL)th);
  return 0;
}

//Function: Adds an element to the top of the NSIS stack
void pushstring(const TCHAR *str, int len)
{
  stack_t *th;

  if (!g_stacktop) return;
  th=(stack_t*)GlobalAlloc(GPTR, sizeof(stack_t) + len);
  lstrcpyn(th->text, str, len);
  th->next=*g_stacktop;
  *g_stacktop=th;
}