1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #if defined _MSC_VER
29 #pragma warning(push, 1)
30 #endif
31 #include <windows.h>
32 #if defined _MSC_VER
33 #pragma warning(pop)
34 #endif
35 #include <Winreg.h>
36 #include <Shlwapi.h>
37 #include <stdio.h>
38 
39 
40 #define SO_PATH_SIZE		4096
41 #define MOZ_PLUGIN_DLL_NAME "npsopluginmi.dll"
42 extern "C" {
43 int lc_isInstalled(const  char* realFilePath)
44 {
45 	HKEY hKeySoftware;
46 	HKEY hMozillaPlugins;
47 	HKEY hStarOffice;
48 	char sSoPath[SO_PATH_SIZE];
49 	char sPluginPath[SO_PATH_SIZE];
50 
51 	LONG ret;
52 	ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE,  "SOFTWARE",  0,  KEY_READ, &hKeySoftware);
53 	if(ret != ERROR_SUCCESS){
54 		ret = RegOpenKeyEx(HKEY_CURRENT_USER,  "SOFTWARE",  0,  KEY_READ, &hKeySoftware);
55 		if(ret != ERROR_SUCCESS){
56 			return -1;
57 		}
58 	}
59 	ret = RegOpenKeyEx(hKeySoftware,  "MozillaPlugins",  0,  KEY_READ, &hMozillaPlugins);
60 	if(ret != ERROR_SUCCESS){
61 		RegCloseKey(hKeySoftware);
62 		if( ret == ERROR_FILE_NOT_FOUND)
63 			return 1;
64 		else
65 			return -1;
66 	}
67 	ret = RegOpenKeyEx(hMozillaPlugins,  "@sun.com/npsopluginmi;version=1.0",  0,  KEY_READ, &hStarOffice);
68 	if(ret != ERROR_SUCCESS){
69 		RegCloseKey(hKeySoftware);
70 		RegCloseKey(hMozillaPlugins);
71 		if( ret == ERROR_FILE_NOT_FOUND)
72 			return 1;
73 		else
74 			return -1;
75 	}
76 
77 	if((realFilePath == NULL) || (strlen(realFilePath) == 0) || (strlen(realFilePath) >= SO_PATH_SIZE))
78 	    ret = -1;
79 	else{
80 	    sprintf(sSoPath,"%s", realFilePath);
81 	    ret = 0;
82 	}
83 	//ret =  GetCurrentDirectory( SO_PATH_SIZE, sSoPath);
84 	//ret = GetEnvironmentVariable("prog", sSoPath, SO_PATH_SIZE);
85 	// GetCurrentDirectory return the char number of the string
86 	if(ret == 0){
87 		DWORD  dType = REG_SZ;
88 		DWORD  dSize = SO_PATH_SIZE;
89 		ret = RegQueryValueEx (hStarOffice, "Path", NULL,  &dType , (LPBYTE) sPluginPath, &dSize);
90 		if(ret == ERROR_SUCCESS){
91 			if(strcmp(sPluginPath, sSoPath) == 0)
92 				ret = 0;
93 			else
94 				ret = 1;
95 		}
96 		else
97 			ret = -1;
98 	}
99 	else
100 		ret = -1;
101 	RegCloseKey(hStarOffice);
102 	RegCloseKey(hMozillaPlugins);
103 	RegCloseKey(hKeySoftware);
104 	return ret;
105 }
106 
107 int lc_uninstallPlugin(const  char*)
108 {
109 	HKEY hKeySoftware;
110 	HKEY hMozillaPlugins;
111 	HKEY hStarOffice;
112 
113 	LONG ret;
114 	ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE,  "SOFTWARE",  0,  KEY_READ|KEY_WRITE, &hKeySoftware);
115 	if(ret != ERROR_SUCCESS){
116 		ret = RegOpenKeyEx(HKEY_CURRENT_USER,  "SOFTWARE",  0,  KEY_READ|KEY_WRITE, &hKeySoftware);
117 		if(ret != ERROR_SUCCESS){
118 			return -1;
119 		}
120 	}
121 	ret = RegOpenKeyEx(hKeySoftware,  "MozillaPlugins",  0,  KEY_READ|KEY_WRITE, &hMozillaPlugins);
122 	if(ret != ERROR_SUCCESS){
123 		RegCloseKey(hKeySoftware);
124 		if( ret == ERROR_FILE_NOT_FOUND)
125 			return 0;
126 		else
127 			return -1;
128 	}
129 
130 	ret = RegOpenKeyEx(hMozillaPlugins,  "@sun.com/npsopluginmi;version=1.0",  0,  KEY_READ|KEY_WRITE, &hStarOffice);
131 	if(ret != ERROR_SUCCESS){
132 		RegCloseKey(hKeySoftware);
133 		RegCloseKey(hMozillaPlugins);
134 		if( ret == ERROR_FILE_NOT_FOUND)
135 			return 0;
136 		else
137 			return -1;
138 	}
139 	RegCloseKey(hStarOffice);
140 	ret = SHDeleteKey(hMozillaPlugins,  "@sun.com/npsopluginmi;version=1.0");
141 	if(ret != ERROR_SUCCESS){
142 		ret = -1;
143 	}
144 	RegFlushKey(hMozillaPlugins);
145 	RegCloseKey(hMozillaPlugins);
146 	RegCloseKey(hKeySoftware);
147 	return ret;
148 }
149 
150 int lc_installPlugin(const  char* realFilePath)
151 {
152 	HKEY hKeySoftware;
153 	HKEY hMozillaPlugins;
154 	HKEY hStarOffice;
155 	char sSoPath[SO_PATH_SIZE];
156 	DWORD  sState;
157 
158 
159 	LONG ret;
160 	ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE,  "SOFTWARE",  0,  KEY_READ|KEY_WRITE, &hKeySoftware);
161 	if(ret != ERROR_SUCCESS){
162 		ret = RegOpenKeyEx(HKEY_CURRENT_USER,  "SOFTWARE",  0,  KEY_READ|KEY_WRITE, &hKeySoftware);
163 		if(ret != ERROR_SUCCESS){
164 			return -1;
165 		}
166 	}
167 	ret = RegOpenKeyEx(hKeySoftware,  "MozillaPlugins",  0,  KEY_READ|KEY_WRITE, &hMozillaPlugins);
168 	if(ret != ERROR_SUCCESS){
169 		RegCreateKeyEx(hKeySoftware,
170 			"MozillaPlugins",
171 			0,
172 			NULL,
173 			REG_OPTION_NON_VOLATILE,
174 			KEY_READ|KEY_WRITE,
175 			NULL,
176 			&hMozillaPlugins,
177 			&sState);
178 	}
179 
180 	ret = RegCreateKeyEx(hMozillaPlugins,
181 		"@sun.com/npsopluginmi;version=1.0",
182 		0,
183 		NULL,
184 		REG_OPTION_NON_VOLATILE,
185 		KEY_READ|KEY_WRITE,
186 		NULL,
187         &hStarOffice,
188 		&sState);
189 	if(ret != ERROR_SUCCESS){
190 		RegCloseKey(hKeySoftware);
191 		RegCloseKey(hMozillaPlugins);
192 		return -1;
193 	}
194 
195 	RegFlushKey(hStarOffice);
196     RegFlushKey(hMozillaPlugins);
197 
198 
199 	if((realFilePath == NULL) || (strlen(realFilePath) == 0) || (strlen(realFilePath) >= SO_PATH_SIZE))
200 	    ret = -1;
201 	else{
202 	    sprintf(sSoPath,"%s", realFilePath);
203 	    ret = 0;
204 	}
205 
206 	//ret =  GetCurrentDirectory( SO_PATH_SIZE, sSoPath);
207 	// GetCurrentDirectory return the char number of the string
208 	if(ret == 0){
209 		ret = RegSetValueEx( hStarOffice, "Path", 0, REG_SZ,  (LPBYTE) sSoPath, strlen(sSoPath) + 1);
210 		if(ret == ERROR_SUCCESS)
211 			ret = 0;
212 		else
213 			ret = -1;
214 	}
215 	else
216 		ret = -1;
217     RegFlushKey(hStarOffice);
218     RegFlushKey(hMozillaPlugins);
219     RegCloseKey(hStarOffice);
220     RegCloseKey(hMozillaPlugins);
221 	RegCloseKey(hKeySoftware);
222 	RegFlushKey(HKEY_LOCAL_MACHINE);
223 
224 	return ret;
225 }
226 }
227