1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #include "register.hxx"
29*cdf0e10cSrcweir #include "registryexception.hxx"
30*cdf0e10cSrcweir #include "registrationcontextinformation.hxx"
31*cdf0e10cSrcweir #include "userregistrar.hxx"
32*cdf0e10cSrcweir #include "windowsregistry.hxx"
33*cdf0e10cSrcweir #include "stringconverter.hxx"
34*cdf0e10cSrcweir #include "msihelper.hxx"
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #ifdef _MSC_VER
37*cdf0e10cSrcweir #pragma warning(push, 1) /* disable warnings within system headers */
38*cdf0e10cSrcweir #pragma warning(disable: 4917)
39*cdf0e10cSrcweir #endif
40*cdf0e10cSrcweir #include <shlobj.h>
41*cdf0e10cSrcweir #ifdef _MSC_VER
42*cdf0e10cSrcweir #pragma warning(pop)
43*cdf0e10cSrcweir #endif
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir #include <assert.h>
47*cdf0e10cSrcweir #ifdef _MSC_VER
48*cdf0e10cSrcweir #pragma warning(disable: 4350)
49*cdf0e10cSrcweir #endif
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir typedef std::auto_ptr<Registrar> RegistrarPtr;
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir namespace /* private */
54*cdf0e10cSrcweir {
55*cdf0e10cSrcweir     RegistrarPtr CreateRegistrar(bool InstallForAllUser, const RegistrationContextInformation& RegCtx)
56*cdf0e10cSrcweir     {
57*cdf0e10cSrcweir         RegistrarPtr RegPtr;
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir         if (InstallForAllUser)
60*cdf0e10cSrcweir             RegPtr = RegistrarPtr(new Registrar(RegCtx));
61*cdf0e10cSrcweir         else
62*cdf0e10cSrcweir             RegPtr = RegistrarPtr(new UserRegistrar(RegCtx));
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir         return RegPtr;
65*cdf0e10cSrcweir     }
66*cdf0e10cSrcweir } // namespace private
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir bool query_preselect_registration_for_ms_application(MSIHANDLE handle, int Register)
69*cdf0e10cSrcweir {
70*cdf0e10cSrcweir     bool preselect = false;
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir     try
73*cdf0e10cSrcweir     {
74*cdf0e10cSrcweir         RegistrationContextInformation RegContext(handle, GetOfficeExecutablePath(handle));
75*cdf0e10cSrcweir         RegistrarPtr CurrentRegistrar = CreateRegistrar(IsAllUserInstallation(handle), RegContext);
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir         if (Register & MSWORD)
78*cdf0e10cSrcweir             preselect = CurrentRegistrar->QueryPreselectMsWordRegistration();
79*cdf0e10cSrcweir         else if (Register & MSEXCEL)
80*cdf0e10cSrcweir             preselect = CurrentRegistrar->QueryPreselectMsExcelRegistration();
81*cdf0e10cSrcweir         else if (Register & MSPOWERPOINT)
82*cdf0e10cSrcweir             preselect = CurrentRegistrar->QueryPreselectMsPowerPointRegistration();
83*cdf0e10cSrcweir     }
84*cdf0e10cSrcweir     catch(RegistryException&)
85*cdf0e10cSrcweir     {
86*cdf0e10cSrcweir         assert(false);
87*cdf0e10cSrcweir     }
88*cdf0e10cSrcweir     return preselect;
89*cdf0e10cSrcweir }
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir //-----------------------------------------
92*cdf0e10cSrcweir // registers StarOffice for MS document
93*cdf0e10cSrcweir // types and as default HTML editor if
94*cdf0e10cSrcweir // specified
95*cdf0e10cSrcweir //-----------------------------------------
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir void Register4MsDoc(MSIHANDLE handle, int Register)
98*cdf0e10cSrcweir {
99*cdf0e10cSrcweir     try
100*cdf0e10cSrcweir     {
101*cdf0e10cSrcweir         RegistrationContextInformation RegContext(handle, GetOfficeExecutablePath(handle));
102*cdf0e10cSrcweir         RegistrarPtr CurrentRegistrar = CreateRegistrar(IsAllUserInstallation(handle), RegContext);
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir         if ((Register & MSWORD))
105*cdf0e10cSrcweir             CurrentRegistrar->RegisterForMsWord();
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir         if ((Register & MSEXCEL))
108*cdf0e10cSrcweir             CurrentRegistrar->RegisterForMsExcel();
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir         if ((Register & MSPOWERPOINT))
111*cdf0e10cSrcweir             CurrentRegistrar->RegisterForMsPowerPoint();
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir         if ((Register & HTML_EDITOR))
114*cdf0e10cSrcweir             CurrentRegistrar->RegisterAsHtmlEditorForInternetExplorer();
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir         if ((Register & DEFAULT_SHELL_HTML_EDITOR))
117*cdf0e10cSrcweir         {
118*cdf0e10cSrcweir             CurrentRegistrar->RegisterAsDefaultHtmlEditorForInternetExplorer();
119*cdf0e10cSrcweir             CurrentRegistrar->RegisterAsDefaultShellHtmlEditor();
120*cdf0e10cSrcweir         }
121*cdf0e10cSrcweir     }
122*cdf0e10cSrcweir     catch(RegistryException&)
123*cdf0e10cSrcweir     {
124*cdf0e10cSrcweir         assert(false);
125*cdf0e10cSrcweir     }
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir     if (Register)
128*cdf0e10cSrcweir         SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0);
129*cdf0e10cSrcweir }
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir void Unregister4MsDoc(MSIHANDLE handle, int Unregister)
132*cdf0e10cSrcweir {
133*cdf0e10cSrcweir     try
134*cdf0e10cSrcweir     {
135*cdf0e10cSrcweir         RegistrationContextInformation RegContext(handle, GetOfficeExecutablePath(handle));
136*cdf0e10cSrcweir         RegistrarPtr CurrentRegistrar = CreateRegistrar(IsAllUserInstallation(handle), RegContext);
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir         if ((Unregister & MSWORD) && CurrentRegistrar->IsRegisteredFor(MSWORD))
139*cdf0e10cSrcweir             CurrentRegistrar->UnregisterForMsWord();
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir         if ((Unregister & HTML_EDITOR) && CurrentRegistrar->IsRegisteredFor(HTML_EDITOR))
142*cdf0e10cSrcweir             CurrentRegistrar->UnregisterAsHtmlEditorForInternetExplorer();
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir         if ((Unregister & MSEXCEL) && CurrentRegistrar->IsRegisteredFor(MSEXCEL))
145*cdf0e10cSrcweir             CurrentRegistrar->UnregisterForMsExcel();
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir         if ((Unregister & MSPOWERPOINT) && CurrentRegistrar->IsRegisteredFor(MSPOWERPOINT))
148*cdf0e10cSrcweir             CurrentRegistrar->UnregisterForMsPowerPoint();
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir         if ((Unregister & DEFAULT_HTML_EDITOR_FOR_IE) && CurrentRegistrar->IsRegisteredFor(DEFAULT_HTML_EDITOR_FOR_IE))
151*cdf0e10cSrcweir             CurrentRegistrar->UnregisterAsDefaultHtmlEditorForInternetExplorer();
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir         if ((Unregister & DEFAULT_SHELL_HTML_EDITOR) && CurrentRegistrar->IsRegisteredFor(DEFAULT_SHELL_HTML_EDITOR))
154*cdf0e10cSrcweir             CurrentRegistrar->UnregisterAsDefaultShellHtmlEditor();
155*cdf0e10cSrcweir     }
156*cdf0e10cSrcweir     catch(RegistryException&)
157*cdf0e10cSrcweir     {
158*cdf0e10cSrcweir         assert(false);
159*cdf0e10cSrcweir     }
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir 	if (Unregister)
162*cdf0e10cSrcweir 		SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0);
163*cdf0e10cSrcweir }
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir //-----------------------------------------
166*cdf0e10cSrcweir // restores the entries for the selected
167*cdf0e10cSrcweir // registry entries
168*cdf0e10cSrcweir // Algorithm:
169*cdf0e10cSrcweir //
170*cdf0e10cSrcweir // 1.
171*cdf0e10cSrcweir // Target key exist (e.g. '.doc')
172*cdf0e10cSrcweir // Default value == soffice.?
173*cdf0e10cSrcweir // Backup key != empty
174*cdf0e10cSrcweir // Action: Replace Default value with backup
175*cdf0e10cSrcweir // key
176*cdf0e10cSrcweir //
177*cdf0e10cSrcweir // 2.
178*cdf0e10cSrcweir // Target key exist
179*cdf0e10cSrcweir // Default value == soffice.?
180*cdf0e10cSrcweir // Backup key == empty
181*cdf0e10cSrcweir // Action: delete default value
182*cdf0e10cSrcweir //
183*cdf0e10cSrcweir // 3.
184*cdf0e10cSrcweir // Target key exist
185*cdf0e10cSrcweir // Default value != soffice.?
186*cdf0e10cSrcweir // Action: nop
187*cdf0e10cSrcweir //
188*cdf0e10cSrcweir // 4.
189*cdf0e10cSrcweir // Target key does not exist
190*cdf0e10cSrcweir // Action: nop
191*cdf0e10cSrcweir //-----------------------------------------
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir void Unregister4MsDocAll(MSIHANDLE handle)
194*cdf0e10cSrcweir {
195*cdf0e10cSrcweir     try
196*cdf0e10cSrcweir     {
197*cdf0e10cSrcweir         RegistrationContextInformation RegContext(handle, GetOfficeExecutablePath(handle));
198*cdf0e10cSrcweir         RegistrarPtr CurrentRegistrar = CreateRegistrar(IsAllUserInstallation(handle), RegContext);
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir         CurrentRegistrar->UnregisterAllAndCleanUpRegistry();
201*cdf0e10cSrcweir     }
202*cdf0e10cSrcweir     catch(RegistryException&)
203*cdf0e10cSrcweir     {
204*cdf0e10cSrcweir         assert(false);
205*cdf0e10cSrcweir     }
206*cdf0e10cSrcweir }
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir //-----------------------------------------
209*cdf0e10cSrcweir // restores lost settings formerly made
210*cdf0e10cSrcweir // with Register4MsDoc
211*cdf0e10cSrcweir //-----------------------------------------
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir void RepairRegister4MsDocSettings(MSIHANDLE handle)
214*cdf0e10cSrcweir {
215*cdf0e10cSrcweir     try
216*cdf0e10cSrcweir     {
217*cdf0e10cSrcweir         RegistrationContextInformation RegContext(handle, GetOfficeExecutablePath(handle));
218*cdf0e10cSrcweir         RegistrarPtr CurrentRegistrar = CreateRegistrar(IsAllUserInstallation(handle), RegContext);
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir         CurrentRegistrar->RepairRegistrationState();
221*cdf0e10cSrcweir     }
222*cdf0e10cSrcweir     catch(RegistryException&)
223*cdf0e10cSrcweir     {
224*cdf0e10cSrcweir         assert(false);
225*cdf0e10cSrcweir     }
226*cdf0e10cSrcweir }
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir bool IsRegisteredFor(MSIHANDLE handle, int State)
229*cdf0e10cSrcweir {
230*cdf0e10cSrcweir     bool Registered = false;
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir     try
233*cdf0e10cSrcweir     {
234*cdf0e10cSrcweir         RegistrationContextInformation RegContext(handle, GetOfficeExecutablePath(handle));
235*cdf0e10cSrcweir         RegistrarPtr CurrentRegistrar = CreateRegistrar(IsAllUserInstallation(handle), RegContext);
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir         Registered = CurrentRegistrar->IsRegisteredFor(State);
238*cdf0e10cSrcweir     }
239*cdf0e10cSrcweir     catch(RegistryException&)
240*cdf0e10cSrcweir     {
241*cdf0e10cSrcweir         assert(false);
242*cdf0e10cSrcweir     }
243*cdf0e10cSrcweir     return Registered;
244*cdf0e10cSrcweir }
245*cdf0e10cSrcweir 
246*cdf0e10cSrcweir #define SO60_UNINSTALL_KEY L"Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\StarOffice 6.0"
247*cdf0e10cSrcweir #define SO_BACKUP_KEY      L"soffice6.bak"
248*cdf0e10cSrcweir #define REGMSDOCSTATE      L"Reg4MsDocState"
249*cdf0e10cSrcweir #define SOFTWARE_CLASSES   L"Software\\Classes"
250*cdf0e10cSrcweir 
251*cdf0e10cSrcweir int FixReturnRegistrationState(MSIHANDLE handle)
252*cdf0e10cSrcweir {
253*cdf0e10cSrcweir 	int registration_state = 0;
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir 	try
256*cdf0e10cSrcweir 	{
257*cdf0e10cSrcweir 		WindowsRegistry registry;
258*cdf0e10cSrcweir 
259*cdf0e10cSrcweir 		RegistryValue rv_regmsdocstate = RegistryValue(
260*cdf0e10cSrcweir 			new RegistryValueImpl(REGMSDOCSTATE, 0));
261*cdf0e10cSrcweir 
262*cdf0e10cSrcweir 		RegistryKey so_bak_key;
263*cdf0e10cSrcweir 
264*cdf0e10cSrcweir 		if (IsAllUserInstallation(handle))
265*cdf0e10cSrcweir 		{
266*cdf0e10cSrcweir 			RegistryKey hkcr_key = registry.GetClassesRootKey();
267*cdf0e10cSrcweir 
268*cdf0e10cSrcweir 			if (hkcr_key->HasSubKey(SO_BACKUP_KEY))
269*cdf0e10cSrcweir 				so_bak_key = hkcr_key->OpenSubKey(SO_BACKUP_KEY);
270*cdf0e10cSrcweir 			else
271*cdf0e10cSrcweir 				so_bak_key = hkcr_key->CreateSubKey(SO_BACKUP_KEY);
272*cdf0e10cSrcweir 
273*cdf0e10cSrcweir 			if (!so_bak_key->HasValue(REGMSDOCSTATE))
274*cdf0e10cSrcweir 			{
275*cdf0e10cSrcweir 				// set a defined value
276*cdf0e10cSrcweir 				so_bak_key->SetValue(rv_regmsdocstate);
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir 				RegistryKey hklm_key = registry.GetLocalMachineKey();
279*cdf0e10cSrcweir 
280*cdf0e10cSrcweir 				if (hklm_key->HasSubKey(SO60_UNINSTALL_KEY))
281*cdf0e10cSrcweir 				{
282*cdf0e10cSrcweir 					RegistryKey so_uninst_key =
283*cdf0e10cSrcweir 						hklm_key->OpenSubKey(SO60_UNINSTALL_KEY);
284*cdf0e10cSrcweir 
285*cdf0e10cSrcweir 					if (so_uninst_key->HasValue(REGMSDOCSTATE))
286*cdf0e10cSrcweir 						so_bak_key->CopyValue(so_uninst_key, REGMSDOCSTATE);
287*cdf0e10cSrcweir 				}
288*cdf0e10cSrcweir 			}
289*cdf0e10cSrcweir 		}
290*cdf0e10cSrcweir 		else
291*cdf0e10cSrcweir 		{
292*cdf0e10cSrcweir 			RegistryKey hkcu_classes_key =
293*cdf0e10cSrcweir 				registry.GetCurrentUserKey()->OpenSubKey(SOFTWARE_CLASSES);
294*cdf0e10cSrcweir 
295*cdf0e10cSrcweir 			so_bak_key = hkcu_classes_key->CreateSubKey(SO_BACKUP_KEY);
296*cdf0e10cSrcweir 
297*cdf0e10cSrcweir 			if (!so_bak_key->HasValue(REGMSDOCSTATE))
298*cdf0e10cSrcweir 			{
299*cdf0e10cSrcweir 				// set a defined value
300*cdf0e10cSrcweir 				so_bak_key->SetValue(rv_regmsdocstate);
301*cdf0e10cSrcweir 
302*cdf0e10cSrcweir 				RegistryKey hklm_sftw_classes =
303*cdf0e10cSrcweir 					registry.GetLocalMachineKey()->OpenSubKey(SOFTWARE_CLASSES, false);
304*cdf0e10cSrcweir 
305*cdf0e10cSrcweir 				RegistryKey so_bak_key_old;
306*cdf0e10cSrcweir 
307*cdf0e10cSrcweir 				if (hklm_sftw_classes->HasSubKey(SO_BACKUP_KEY))
308*cdf0e10cSrcweir 				{
309*cdf0e10cSrcweir 					so_bak_key_old = hklm_sftw_classes->OpenSubKey(SO_BACKUP_KEY, false);
310*cdf0e10cSrcweir 
311*cdf0e10cSrcweir 					if (so_bak_key_old->HasValue(REGMSDOCSTATE))
312*cdf0e10cSrcweir 						so_bak_key->CopyValue(so_bak_key_old, REGMSDOCSTATE);
313*cdf0e10cSrcweir 				}
314*cdf0e10cSrcweir 				else // try the uninstall key
315*cdf0e10cSrcweir 				{
316*cdf0e10cSrcweir 					RegistryKey hklm_key = registry.GetLocalMachineKey();
317*cdf0e10cSrcweir 
318*cdf0e10cSrcweir 					if (hklm_key->HasSubKey(SO60_UNINSTALL_KEY))
319*cdf0e10cSrcweir 					{
320*cdf0e10cSrcweir 						RegistryKey so_uninst_key =
321*cdf0e10cSrcweir 							hklm_key->OpenSubKey(SO60_UNINSTALL_KEY);
322*cdf0e10cSrcweir 
323*cdf0e10cSrcweir 						if (so_uninst_key->HasValue(REGMSDOCSTATE))
324*cdf0e10cSrcweir 							so_bak_key->CopyValue(so_uninst_key, REGMSDOCSTATE);
325*cdf0e10cSrcweir 					}
326*cdf0e10cSrcweir 				}
327*cdf0e10cSrcweir 			}
328*cdf0e10cSrcweir 		}
329*cdf0e10cSrcweir 
330*cdf0e10cSrcweir 		rv_regmsdocstate = so_bak_key->GetValue(REGMSDOCSTATE);
331*cdf0e10cSrcweir 		registration_state = rv_regmsdocstate->GetDataAsInt();
332*cdf0e10cSrcweir 	}
333*cdf0e10cSrcweir 	catch(RegistryException&)
334*cdf0e10cSrcweir 	{
335*cdf0e10cSrcweir 		registration_state = 0;
336*cdf0e10cSrcweir 	}
337*cdf0e10cSrcweir 
338*cdf0e10cSrcweir 	return registration_state;
339*cdf0e10cSrcweir }
340*cdf0e10cSrcweir 
341