forked from bruderstein/nppPluginManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPluginManager.cpp
More file actions
427 lines (329 loc) · 12.9 KB
/
PluginManager.cpp
File metadata and controls
427 lines (329 loc) · 12.9 KB
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*
This file is part of Plugin Manager Plugin for Notepad++
Copyright (C)2009-2010 Dave Brotherstone <davegb@pobox.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "precompiled_headers.h"
#include "PluginManager.h"
#include "PluginManagerVersion.h"
#include "AboutDialog.h"
#include "PluginManagerDialog.h"
#include "NotifyUpdatesDialog.h"
#include "SettingsDialog.h"
#include "Plugin.h"
#include "Utility.h"
#include "WcharMbcsConverter.h"
#include "libinstall/DownloadManager.h"
/* information for notepad */
#ifdef ALLOW_OVERRIDE_XML_URL
CONST INT nbFunc = 3;
#else
CONST INT nbFunc = 2;
#endif
CONST TCHAR PLUGIN_NAME[] = _T("Plugin Manager");
/* global values */
HANDLE g_hModule = NULL;
NppData nppData;
FuncItem funcItem[nbFunc];
#ifdef _UNICODE
BOOL g_isUnicode = TRUE;
#else
BOOL g_isUnicode = FALSE;
#endif
#ifdef _WIN64
BOOL g_isX64 = TRUE;
#else
BOOL g_isX64 = FALSE;
#endif
Options g_options;
SettingsDialog g_settingsDlg;
PluginList *g_pluginList = NULL;
winVer g_winVer;
/* dialog classes */
AboutDialog aboutDlg;
PluginManagerDialog pluginManagerDlg;
NotifyUpdatesDialog notifyUpdatesDlg;
/* settings */
TCHAR configPath[MAX_PATH];
TCHAR iniFilePath[MAX_PATH];
UINT startupChecks(LPVOID param);
void doReloadXml();
using namespace std;
/* main function of dll */
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD reasonForCall,
LPVOID /*lpReserved*/ )
{
g_hModule = hModule;
g_options.moduleInfo.setHModule(static_cast<HMODULE>(hModule));
switch (reasonForCall)
{
case DLL_PROCESS_ATTACH:
{
/* Set function pointers */
funcItem[0]._pFunc = doPluginManagerDlg;
funcItem[1]._pFunc = doAboutDlg;
/* Fill menu names */
_tcscpy_s(funcItem[0]._itemName, 64, _T("&Show Plugin Manager"));
_tcscpy_s(funcItem[1]._itemName, 64, _T("&About"));
/* Set shortcuts */
funcItem[0]._pShKey = NULL;
funcItem[1]._pShKey = NULL;
#ifdef ALLOW_OVERRIDE_XML_URL
funcItem[2]._pFunc = doReloadXml;
_tcscpy_s(funcItem[2]._itemName, 64, _T("&Refresh XML"));
funcItem[2]._pShKey = NULL;
#endif
DownloadManager::setUserAgent(_T("Notepad++/Plugin-Manager;v") _T(PLUGINMANAGERVERSION_STRING));
break;
}
case DLL_PROCESS_DETACH:
{
delete funcItem[0]._pShKey;
/* save settings */
saveSettings();
break;
}
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
return TRUE;
}
extern "C" __declspec(dllexport) void setInfo(NppData notpadPlusData)
{
/* stores notepad data */
nppData = notpadPlusData;
/* load data of plugin */
loadSettings();
/* initial dialogs */
//TemplateDlg.init((HINSTANCE)g_hModule, nppData, &pluginProp);
aboutDlg.init((HINSTANCE)g_hModule, nppData);
pluginManagerDlg.init((HINSTANCE)g_hModule, nppData);
g_options.moduleInfo.setHParent(nppData._nppHandle);
g_winVer = static_cast<winVer>(::SendMessage(nppData._nppHandle, NPPM_GETWINDOWSVERSION, 0, 0));
}
extern "C" __declspec(dllexport) CONST TCHAR * getName()
{
return PLUGIN_NAME;
}
extern "C" __declspec(dllexport) FuncItem * getFuncsArray(INT *nbF)
{
*nbF = nbFunc;
return funcItem;
}
/***
* beNotification()
*
* This function is called, if a notification in Scantilla/Notepad++ occurs
*/
extern "C" __declspec(dllexport) void beNotified(SCNotification *notifyCode)
{
switch(notifyCode->nmhdr.code)
{
case NPPN_READY:
/* When Notepad++ is ready, we'll kick off a thread to
* check if there's anything left for gpup to do.
* If there is, then we can invite the user if they want to
* do it now.
* Eventually, we'll do the plugins.xml download in this new thread
* and notify the user of any updates too.
Guess what, we do now.
*/
#ifdef ALLOW_OVERRIDE_XML_URL
::MessageBox(nppData._nppHandle, _T("The Plugin Manager is running as a special build that allows override of the XML download URL. This version should be used for testing purposes ONLY."), _T("Plugin Manager"), MB_ICONEXCLAMATION);
#endif
(void)::CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)startupChecks, NULL, 0, 0);
break;
}
}
/***
* messageProc()
*
* This function is called, if a notification from Notepad occurs
*/
extern "C" __declspec(dllexport) LRESULT messageProc(UINT /*message*/, WPARAM /*wParam*/, LPARAM /*lParam*/)
{
return TRUE;
}
#ifdef _UNICODE
extern "C" __declspec(dllexport) BOOL isUnicode()
{
return g_isUnicode;
}
#endif
/***
* loadSettings()
*
* Load the parameters of plugin
*/
void loadSettings(void)
{
/* initialize the config directory */
::SendMessage(nppData._nppHandle, NPPM_GETPLUGINSCONFIGDIR, MAX_PATH, (LPARAM)configPath);
/* Test if config path exist */
if (PathFileExists(configPath) == FALSE) {
::CreateDirectory(configPath, NULL);
}
_tcscpy_s(iniFilePath, MAX_PATH, configPath);
_tcscat_s(iniFilePath, MAX_PATH, PLUGINMANAGER_INI);
if (PathFileExists(iniFilePath) == FALSE)
{
::CloseHandle(::CreateFile(iniFilePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL));
}
g_options.notifyUpdates = ::GetPrivateProfileInt(SETTINGS_GROUP, KEY_NOTIFYUPDATES, 1, iniFilePath);
g_options.showUnstable = ::GetPrivateProfileInt(SETTINGS_GROUP, KEY_SHOWUNSTABLE, 0, iniFilePath);
g_options.forceHttp = ::GetPrivateProfileInt(SETTINGS_GROUP, KEY_FORCEHTTP, 0, iniFilePath);
g_options.useDevPluginList = ::GetPrivateProfileInt(SETTINGS_GROUP, KEY_USEDEVPLUGINLIST, 0, iniFilePath);
g_options.daysToCheck = ::GetPrivateProfileInt(SETTINGS_GROUP, KEY_DAYSTOCHECK, DAYSCHECK_DEFAULT, iniFilePath);
if (g_options.daysToCheck < DAYSCHECK_MIN)
{
g_options.daysToCheck = DAYSCHECK_MIN;
}
g_options.installLocation = static_cast<INSTALLLOCATION>(::GetPrivateProfileInt(SETTINGS_GROUP, KEY_INSTALLLOCATION, 2, iniFilePath));
g_options.appDataPluginsSupported = static_cast<BOOL>(::SendMessage(nppData._nppHandle, NPPM_GETAPPDATAPLUGINSALLOWED, 0, 0));
// TODO Test
// If AppData plugins are not supported, then reset the install location.
if (FALSE == g_options.appDataPluginsSupported)
{
if (g_options.installLocation == INSTALLLOC_APPDATA
|| g_options.installLocation == INSTALLLOC_ALLUSERS)
{
g_options.installLocation = INSTALLLOC_ALLUSERSNOAPPDATA;
}
}
// Else, if we've "upgraded" to support for all users, assume we want appdata plugins
else if (TRUE == g_options.appDataPluginsSupported
&& g_options.installLocation == INSTALLLOC_ALLUSERSNOAPPDATA)
{
g_options.installLocation = INSTALLLOC_APPDATA;
}
TCHAR tmpLastCheck[20];
::GetPrivateProfileString(SETTINGS_GROUP, KEY_LASTCHECK, _T("0"), tmpLastCheck, 20, iniFilePath);
g_options.lastCheck = (time_t)_ttol(tmpLastCheck);
#ifdef ALLOW_OVERRIDE_XML_URL
TCHAR tmpUrl[MAX_PATH];
::GetPrivateProfileString(SETTINGS_GROUP, KEY_OVERRIDEMD5URL, _T(""), tmpUrl, MAX_PATH, iniFilePath);
g_options.downloadMD5Url = tmpUrl;
::GetPrivateProfileString(SETTINGS_GROUP, KEY_OVERRIDEURL, _T(""), tmpUrl, MAX_PATH, iniFilePath);
g_options.downloadUrl = tmpUrl;
#endif
}
/***
* saveSettings()
*
* Saves the parameters of plugin
*/
void saveSettings(void)
{
TCHAR temp[16];
// Install location
_itot_s(g_options.installLocation, temp, 16, 10);
::WritePrivateProfileString(SETTINGS_GROUP, KEY_INSTALLLOCATION, temp, iniFilePath);
if (g_options.daysToCheck != DAYSCHECK_DEFAULT)
{
_itot_s(g_options.daysToCheck, temp, 16, 10);
::WritePrivateProfileString(SETTINGS_GROUP, KEY_DAYSTOCHECK, temp, iniFilePath);
}
_itot_s(g_options.notifyUpdates, temp, 16, 10);
::WritePrivateProfileString(SETTINGS_GROUP, KEY_NOTIFYUPDATES, temp, iniFilePath);
_itot_s(g_options.forceHttp, temp, 16, 10);
::WritePrivateProfileString(SETTINGS_GROUP, KEY_FORCEHTTP, temp, iniFilePath);
_itot_s(g_options.useDevPluginList, temp, 16, 10);
::WritePrivateProfileString(SETTINGS_GROUP, KEY_USEDEVPLUGINLIST, temp, iniFilePath);
_itot_s(g_options.showUnstable, temp, 16, 10);
::WritePrivateProfileString(SETTINGS_GROUP, KEY_SHOWUNSTABLE, temp, iniFilePath);
// Remove any leftover proxy credentials from previous versions
// (password encrypted, but they are no longer needed, so clear them out if they're there)
::WritePrivateProfileString(SETTINGS_GROUP, KEY_PROXYUSERNAME, NULL, iniFilePath);
::WritePrivateProfileString(SETTINGS_GROUP, KEY_PROXYPASSWORD, NULL, iniFilePath);
}
/**************************************************************************
* Interface functions
*/
void doAboutDlg(void)
{
aboutDlg.doDialog();
}
void doPluginManagerDlg()
{
pluginManagerDlg.doDialog();
}
UINT startupChecks(LPVOID /*param*/)
{
TCHAR tConfigPath[MAX_PATH];
TCHAR tNppPath[MAX_PATH];
::SendMessage(nppData._nppHandle, NPPM_GETPLUGINSCONFIGDIR, MAX_PATH, reinterpret_cast<LPARAM>(tConfigPath));
::SendMessage(nppData._nppHandle, NPPM_GETNPPDIRECTORY, MAX_PATH, reinterpret_cast<LPARAM>(tNppPath));
tstring configPathVar = tConfigPath;
configPathVar.append(_T("\\PluginManagerGpup.xml"));
TiXmlDocument gpupDoc(configPathVar);
if (gpupDoc.LoadFile()
&& gpupDoc.FirstChildElement(_T("install"))
&& !gpupDoc.FirstChildElement(_T("install"))->NoChildren())
{
int mbResult = ::MessageBox(nppData._nppHandle, _T("There are still some pending actions to complete installing or removing some plugins. Would you like to do these now (Notepad++ will be restarted)?"),
_T("Notepad++ Plugin Manager"), MB_YESNO | MB_ICONEXCLAMATION);
if (mbResult == IDYES)
{
configPathVar.insert(0, _T("-a \""));
configPathVar.append(_T("\""));
Utility::startGpup(nppData._nppHandle, tNppPath, configPathVar.c_str(), TRUE);
}
}
else
{
// Clean up temp directory
tstring tempDir = tConfigPath;
tempDir.append(_T("\\plugin_install_temp"));
Utility::removeDirectory(tempDir.c_str());
// Remove the Gpup file (if it exists, doesn't matter if it doesn't)
::DeleteFile(configPathVar.c_str());
}
time_t ltime;
time(<ime);
if (g_options.notifyUpdates && ((ltime - g_options.lastCheck) > (86400 * g_options.daysToCheck)))
{
// Store the check time
TCHAR tmp[20];
_ltot_s((long)ltime, tmp, 20, 10);
::WritePrivateProfileString(SETTINGS_GROUP, KEY_LASTCHECK, tmp, iniFilePath);
g_pluginList = new PluginList();
g_pluginList->init(&nppData);
pluginManagerDlg.setPluginList(g_pluginList);
g_pluginList->downloadList();
if (!g_pluginList->getUpdateablePlugins().empty())
{
notifyUpdatesDlg.init((HINSTANCE)g_hModule, nppData, g_pluginList);
// Check if there updates that we are not ignoring
if (notifyUpdatesDlg.updatesAvailable())
notifyUpdatesDlg.doModal();
}
}
return 0;
}
#ifdef ALLOW_OVERRIDE_XML_URL
void doReloadXml()
{
if (NULL == g_pluginList)
{
g_pluginList = new PluginList();
g_pluginList->init(&nppData);
pluginManagerDlg.setPluginList(g_pluginList);
}
g_pluginList->downloadList();
pluginManagerDlg.refreshLists();
::MessageBox(nppData._nppHandle, _T("XML Refreshed"), _T("Plugin Manager"), MB_OK |MB_ICONINFORMATION);
}
#endif