xref: /trunk/main/cui/source/dialogs/winpluginlib.cxx (revision e715d8b7)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #if defined _MSC_VER
25 #pragma warning(push, 1)
26 #endif
27 #include <windows.h>
28 #if defined _MSC_VER
29 #pragma warning(pop)
30 #endif
31 #include <Winreg.h>
32 #include <Shlwapi.h>
33 #include <stdio.h>
34 
35 
36 #define SO_PATH_SIZE		4096
37 #define MOZ_PLUGIN_DLL_NAME "npsopluginmi.dll"
38 extern "C" {
39 int lc_isInstalled(const  char* realFilePath)
40 {
41 	HKEY hKeySoftware;
42 	HKEY hMozillaPlugins;
43 	HKEY hStarOffice;
44 	char sSoPath[SO_PATH_SIZE];
45 	char sPluginPath[SO_PATH_SIZE];
46 
47 	LONG ret;
48 	ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE,  "SOFTWARE",  0,  KEY_READ, &hKeySoftware);
49 	if(ret != ERROR_SUCCESS){
50 		ret = RegOpenKeyEx(HKEY_CURRENT_USER,  "SOFTWARE",  0,  KEY_READ, &hKeySoftware);
51 		if(ret != ERROR_SUCCESS){
52 			return -1;
53 		}
54 	}
55 	ret = RegOpenKeyEx(hKeySoftware,  "MozillaPlugins",  0,  KEY_READ, &hMozillaPlugins);
56 	if(ret != ERROR_SUCCESS){
57 		RegCloseKey(hKeySoftware);
58 		if( ret == ERROR_FILE_NOT_FOUND)
59 			return 1;
60 		else
61 			return -1;
62 	}
63 	ret = RegOpenKeyEx(hMozillaPlugins,  "@sun.com/npsopluginmi;version=1.0",  0,  KEY_READ, &hStarOffice);
64 	if(ret != ERROR_SUCCESS){
65 		RegCloseKey(hKeySoftware);
66 		RegCloseKey(hMozillaPlugins);
67 		if( ret == ERROR_FILE_NOT_FOUND)
68 			return 1;
69 		else
70 			return -1;
71 	}
72 
73 	if((realFilePath == NULL) || (strlen(realFilePath) == 0) || (strlen(realFilePath) >= SO_PATH_SIZE))
74 	    ret = -1;
75 	else{
76 	    sprintf(sSoPath,"%s", realFilePath);
77 	    ret = 0;
78 	}
79 	//ret =  GetCurrentDirectory( SO_PATH_SIZE, sSoPath);
80 	//ret = GetEnvironmentVariable("prog", sSoPath, SO_PATH_SIZE);
81 	// GetCurrentDirectory return the char number of the string
82 	if(ret == 0){
83 		DWORD  dType = REG_SZ;
84 		DWORD  dSize = SO_PATH_SIZE;
85 		ret = RegQueryValueEx (hStarOffice, "Path", NULL,  &dType , (LPBYTE) sPluginPath, &dSize);
86 		if(ret == ERROR_SUCCESS){
87 			if(strcmp(sPluginPath, sSoPath) == 0)
88 				ret = 0;
89 			else
90 				ret = 1;
91 		}
92 		else
93 			ret = -1;
94 	}
95 	else
96 		ret = -1;
97 	RegCloseKey(hStarOffice);
98 	RegCloseKey(hMozillaPlugins);
99 	RegCloseKey(hKeySoftware);
100 	return ret;
101 }
102 
103 int lc_uninstallPlugin(const  char*)
104 {
105 	HKEY hKeySoftware;
106 	HKEY hMozillaPlugins;
107 	HKEY hStarOffice;
108 
109 	LONG ret;
110 	ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE,  "SOFTWARE",  0,  KEY_READ|KEY_WRITE, &hKeySoftware);
111 	if(ret != ERROR_SUCCESS){
112 		ret = RegOpenKeyEx(HKEY_CURRENT_USER,  "SOFTWARE",  0,  KEY_READ|KEY_WRITE, &hKeySoftware);
113 		if(ret != ERROR_SUCCESS){
114 			return -1;
115 		}
116 	}
117 	ret = RegOpenKeyEx(hKeySoftware,  "MozillaPlugins",  0,  KEY_READ|KEY_WRITE, &hMozillaPlugins);
118 	if(ret != ERROR_SUCCESS){
119 		RegCloseKey(hKeySoftware);
120 		if( ret == ERROR_FILE_NOT_FOUND)
121 			return 0;
122 		else
123 			return -1;
124 	}
125 
126 	ret = RegOpenKeyEx(hMozillaPlugins,  "@sun.com/npsopluginmi;version=1.0",  0,  KEY_READ|KEY_WRITE, &hStarOffice);
127 	if(ret != ERROR_SUCCESS){
128 		RegCloseKey(hKeySoftware);
129 		RegCloseKey(hMozillaPlugins);
130 		if( ret == ERROR_FILE_NOT_FOUND)
131 			return 0;
132 		else
133 			return -1;
134 	}
135 	RegCloseKey(hStarOffice);
136 	ret = SHDeleteKey(hMozillaPlugins,  "@sun.com/npsopluginmi;version=1.0");
137 	if(ret != ERROR_SUCCESS){
138 		ret = -1;
139 	}
140 	RegFlushKey(hMozillaPlugins);
141 	RegCloseKey(hMozillaPlugins);
142 	RegCloseKey(hKeySoftware);
143 	return ret;
144 }
145 
146 int lc_installPlugin(const  char* realFilePath)
147 {
148 	HKEY hKeySoftware;
149 	HKEY hMozillaPlugins;
150 	HKEY hStarOffice;
151 	char sSoPath[SO_PATH_SIZE];
152 	DWORD  sState;
153 
154 
155 	LONG ret;
156 	ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE,  "SOFTWARE",  0,  KEY_READ|KEY_WRITE, &hKeySoftware);
157 	if(ret != ERROR_SUCCESS){
158 		ret = RegOpenKeyEx(HKEY_CURRENT_USER,  "SOFTWARE",  0,  KEY_READ|KEY_WRITE, &hKeySoftware);
159 		if(ret != ERROR_SUCCESS){
160 			return -1;
161 		}
162 	}
163 	ret = RegOpenKeyEx(hKeySoftware,  "MozillaPlugins",  0,  KEY_READ|KEY_WRITE, &hMozillaPlugins);
164 	if(ret != ERROR_SUCCESS){
165 		RegCreateKeyEx(hKeySoftware,
166 			"MozillaPlugins",
167 			0,
168 			NULL,
169 			REG_OPTION_NON_VOLATILE,
170 			KEY_READ|KEY_WRITE,
171 			NULL,
172 			&hMozillaPlugins,
173 			&sState);
174 	}
175 
176 	ret = RegCreateKeyEx(hMozillaPlugins,
177 		"@sun.com/npsopluginmi;version=1.0",
178 		0,
179 		NULL,
180 		REG_OPTION_NON_VOLATILE,
181 		KEY_READ|KEY_WRITE,
182 		NULL,
183         &hStarOffice,
184 		&sState);
185 	if(ret != ERROR_SUCCESS){
186 		RegCloseKey(hKeySoftware);
187 		RegCloseKey(hMozillaPlugins);
188 		return -1;
189 	}
190 
191 	RegFlushKey(hStarOffice);
192     RegFlushKey(hMozillaPlugins);
193 
194 
195 	if((realFilePath == NULL) || (strlen(realFilePath) == 0) || (strlen(realFilePath) >= SO_PATH_SIZE))
196 	    ret = -1;
197 	else{
198 	    sprintf(sSoPath,"%s", realFilePath);
199 	    ret = 0;
200 	}
201 
202 	//ret =  GetCurrentDirectory( SO_PATH_SIZE, sSoPath);
203 	// GetCurrentDirectory return the char number of the string
204 	if(ret == 0){
205 		ret = RegSetValueEx( hStarOffice, "Path", 0, REG_SZ,  (LPBYTE) sSoPath, strlen(sSoPath) + 1);
206 		if(ret == ERROR_SUCCESS)
207 			ret = 0;
208 		else
209 			ret = -1;
210 	}
211 	else
212 		ret = -1;
213     RegFlushKey(hStarOffice);
214     RegFlushKey(hMozillaPlugins);
215     RegCloseKey(hStarOffice);
216     RegCloseKey(hMozillaPlugins);
217 	RegCloseKey(hKeySoftware);
218 	RegFlushKey(HKEY_LOCAL_MACHINE);
219 
220 	return ret;
221 }
222 }
223