132b1fd08SAndrew Rist /**************************************************************
2*102561b7Smseidel  *
332b1fd08SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
432b1fd08SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
532b1fd08SAndrew Rist  * distributed with this work for additional information
632b1fd08SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
732b1fd08SAndrew Rist  * to you under the Apache License, Version 2.0 (the
832b1fd08SAndrew Rist  * "License"); you may not use this file except in compliance
932b1fd08SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*102561b7Smseidel  *
1132b1fd08SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*102561b7Smseidel  *
1332b1fd08SAndrew Rist  * Unless required by applicable law or agreed to in writing,
1432b1fd08SAndrew Rist  * software distributed under the License is distributed on an
1532b1fd08SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1632b1fd08SAndrew Rist  * KIND, either express or implied.  See the License for the
1732b1fd08SAndrew Rist  * specific language governing permissions and limitations
1832b1fd08SAndrew Rist  * under the License.
19*102561b7Smseidel  *
2032b1fd08SAndrew Rist  *************************************************************/
2132b1fd08SAndrew Rist 
2232b1fd08SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "register.hxx"
25cdf0e10cSrcweir #include "registryexception.hxx"
26cdf0e10cSrcweir #include "registrationcontextinformation.hxx"
27cdf0e10cSrcweir #include "userregistrar.hxx"
28cdf0e10cSrcweir #include "windowsregistry.hxx"
29cdf0e10cSrcweir #include "stringconverter.hxx"
30cdf0e10cSrcweir #include "msihelper.hxx"
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #ifdef _MSC_VER
33cdf0e10cSrcweir #pragma warning(push, 1) /* disable warnings within system headers */
34cdf0e10cSrcweir #pragma warning(disable: 4917)
35cdf0e10cSrcweir #endif
36cdf0e10cSrcweir #include <shlobj.h>
37cdf0e10cSrcweir #ifdef _MSC_VER
38cdf0e10cSrcweir #pragma warning(pop)
39cdf0e10cSrcweir #endif
40cdf0e10cSrcweir 
41cdf0e10cSrcweir 
42cdf0e10cSrcweir #include <assert.h>
43cdf0e10cSrcweir #ifdef _MSC_VER
44cdf0e10cSrcweir #pragma warning(disable: 4350)
45cdf0e10cSrcweir #endif
46cdf0e10cSrcweir 
47cdf0e10cSrcweir typedef std::auto_ptr<Registrar> RegistrarPtr;
48cdf0e10cSrcweir 
49cdf0e10cSrcweir namespace /* private */
50cdf0e10cSrcweir {
CreateRegistrar(bool InstallForAllUser,const RegistrationContextInformation & RegCtx)51cdf0e10cSrcweir     RegistrarPtr CreateRegistrar(bool InstallForAllUser, const RegistrationContextInformation& RegCtx)
52cdf0e10cSrcweir     {
53cdf0e10cSrcweir         RegistrarPtr RegPtr;
54*102561b7Smseidel 
55cdf0e10cSrcweir         if (InstallForAllUser)
56cdf0e10cSrcweir             RegPtr = RegistrarPtr(new Registrar(RegCtx));
57cdf0e10cSrcweir         else
58cdf0e10cSrcweir             RegPtr = RegistrarPtr(new UserRegistrar(RegCtx));
59*102561b7Smseidel 
60cdf0e10cSrcweir         return RegPtr;
61cdf0e10cSrcweir     }
62cdf0e10cSrcweir } // namespace private
63cdf0e10cSrcweir 
query_preselect_registration_for_ms_application(MSIHANDLE handle,int Register)64cdf0e10cSrcweir bool query_preselect_registration_for_ms_application(MSIHANDLE handle, int Register)
65*102561b7Smseidel {
66cdf0e10cSrcweir     bool preselect = false;
67*102561b7Smseidel 
68cdf0e10cSrcweir     try
69cdf0e10cSrcweir     {
70cdf0e10cSrcweir         RegistrationContextInformation RegContext(handle, GetOfficeExecutablePath(handle));
71cdf0e10cSrcweir         RegistrarPtr CurrentRegistrar = CreateRegistrar(IsAllUserInstallation(handle), RegContext);
72*102561b7Smseidel 
73cdf0e10cSrcweir         if (Register & MSWORD)
74cdf0e10cSrcweir             preselect = CurrentRegistrar->QueryPreselectMsWordRegistration();
75cdf0e10cSrcweir         else if (Register & MSEXCEL)
76cdf0e10cSrcweir             preselect = CurrentRegistrar->QueryPreselectMsExcelRegistration();
77cdf0e10cSrcweir         else if (Register & MSPOWERPOINT)
78cdf0e10cSrcweir             preselect = CurrentRegistrar->QueryPreselectMsPowerPointRegistration();
79cdf0e10cSrcweir     }
80cdf0e10cSrcweir     catch(RegistryException&)
81cdf0e10cSrcweir     {
82cdf0e10cSrcweir         assert(false);
83*102561b7Smseidel     }
84cdf0e10cSrcweir     return preselect;
85*102561b7Smseidel }
86cdf0e10cSrcweir 
87cdf0e10cSrcweir //-----------------------------------------
88*102561b7Smseidel // registers OpenOffice for MS document
89cdf0e10cSrcweir // types and as default HTML editor if
90cdf0e10cSrcweir // specified
91cdf0e10cSrcweir //-----------------------------------------
92cdf0e10cSrcweir 
Register4MsDoc(MSIHANDLE handle,int Register)93cdf0e10cSrcweir void Register4MsDoc(MSIHANDLE handle, int Register)
94*102561b7Smseidel {
95cdf0e10cSrcweir     try
96*102561b7Smseidel     {
97cdf0e10cSrcweir         RegistrationContextInformation RegContext(handle, GetOfficeExecutablePath(handle));
98cdf0e10cSrcweir         RegistrarPtr CurrentRegistrar = CreateRegistrar(IsAllUserInstallation(handle), RegContext);
99*102561b7Smseidel 
100cdf0e10cSrcweir         if ((Register & MSWORD))
101*102561b7Smseidel             CurrentRegistrar->RegisterForMsWord();
102*102561b7Smseidel 
103cdf0e10cSrcweir         if ((Register & MSEXCEL))
104cdf0e10cSrcweir             CurrentRegistrar->RegisterForMsExcel();
105*102561b7Smseidel 
106cdf0e10cSrcweir         if ((Register & MSPOWERPOINT))
107cdf0e10cSrcweir             CurrentRegistrar->RegisterForMsPowerPoint();
108*102561b7Smseidel 
109cdf0e10cSrcweir         if ((Register & HTML_EDITOR))
110cdf0e10cSrcweir             CurrentRegistrar->RegisterAsHtmlEditorForInternetExplorer();
111*102561b7Smseidel 
112cdf0e10cSrcweir         if ((Register & DEFAULT_SHELL_HTML_EDITOR))
113cdf0e10cSrcweir         {
114cdf0e10cSrcweir             CurrentRegistrar->RegisterAsDefaultHtmlEditorForInternetExplorer();
115cdf0e10cSrcweir             CurrentRegistrar->RegisterAsDefaultShellHtmlEditor();
116cdf0e10cSrcweir         }
117cdf0e10cSrcweir     }
118cdf0e10cSrcweir     catch(RegistryException&)
119cdf0e10cSrcweir     {
120cdf0e10cSrcweir         assert(false);
121cdf0e10cSrcweir     }
122*102561b7Smseidel 
123cdf0e10cSrcweir     if (Register)
124*102561b7Smseidel         SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0);
125cdf0e10cSrcweir }
126cdf0e10cSrcweir 
Unregister4MsDoc(MSIHANDLE handle,int Unregister)127cdf0e10cSrcweir void Unregister4MsDoc(MSIHANDLE handle, int Unregister)
128*102561b7Smseidel {
129cdf0e10cSrcweir     try
130cdf0e10cSrcweir     {
131cdf0e10cSrcweir         RegistrationContextInformation RegContext(handle, GetOfficeExecutablePath(handle));
132cdf0e10cSrcweir         RegistrarPtr CurrentRegistrar = CreateRegistrar(IsAllUserInstallation(handle), RegContext);
133*102561b7Smseidel 
134cdf0e10cSrcweir         if ((Unregister & MSWORD) && CurrentRegistrar->IsRegisteredFor(MSWORD))
135cdf0e10cSrcweir             CurrentRegistrar->UnregisterForMsWord();
136*102561b7Smseidel 
137cdf0e10cSrcweir         if ((Unregister & HTML_EDITOR) && CurrentRegistrar->IsRegisteredFor(HTML_EDITOR))
138cdf0e10cSrcweir             CurrentRegistrar->UnregisterAsHtmlEditorForInternetExplorer();
139*102561b7Smseidel 
140cdf0e10cSrcweir         if ((Unregister & MSEXCEL) && CurrentRegistrar->IsRegisteredFor(MSEXCEL))
141cdf0e10cSrcweir             CurrentRegistrar->UnregisterForMsExcel();
142*102561b7Smseidel 
143cdf0e10cSrcweir         if ((Unregister & MSPOWERPOINT) && CurrentRegistrar->IsRegisteredFor(MSPOWERPOINT))
144cdf0e10cSrcweir             CurrentRegistrar->UnregisterForMsPowerPoint();
145*102561b7Smseidel 
146cdf0e10cSrcweir         if ((Unregister & DEFAULT_HTML_EDITOR_FOR_IE) && CurrentRegistrar->IsRegisteredFor(DEFAULT_HTML_EDITOR_FOR_IE))
147cdf0e10cSrcweir             CurrentRegistrar->UnregisterAsDefaultHtmlEditorForInternetExplorer();
148*102561b7Smseidel 
149cdf0e10cSrcweir         if ((Unregister & DEFAULT_SHELL_HTML_EDITOR) && CurrentRegistrar->IsRegisteredFor(DEFAULT_SHELL_HTML_EDITOR))
150cdf0e10cSrcweir             CurrentRegistrar->UnregisterAsDefaultShellHtmlEditor();
151cdf0e10cSrcweir     }
152cdf0e10cSrcweir     catch(RegistryException&)
153cdf0e10cSrcweir     {
154cdf0e10cSrcweir         assert(false);
155cdf0e10cSrcweir     }
156*102561b7Smseidel 
157cdf0e10cSrcweir 	if (Unregister)
158*102561b7Smseidel 		SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0);
159cdf0e10cSrcweir }
160cdf0e10cSrcweir 
161cdf0e10cSrcweir //-----------------------------------------
162cdf0e10cSrcweir // restores the entries for the selected
163cdf0e10cSrcweir // registry entries
164cdf0e10cSrcweir // Algorithm:
165cdf0e10cSrcweir //
166*102561b7Smseidel // 1.
167cdf0e10cSrcweir // Target key exist (e.g. '.doc')
168cdf0e10cSrcweir // Default value == soffice.?
169cdf0e10cSrcweir // Backup key != empty
170cdf0e10cSrcweir // Action: Replace Default value with backup
171cdf0e10cSrcweir // key
172cdf0e10cSrcweir //
173*102561b7Smseidel // 2.
174cdf0e10cSrcweir // Target key exist
175cdf0e10cSrcweir // Default value == soffice.?
176cdf0e10cSrcweir // Backup key == empty
177cdf0e10cSrcweir // Action: delete default value
178cdf0e10cSrcweir //
179cdf0e10cSrcweir // 3.
180cdf0e10cSrcweir // Target key exist
181cdf0e10cSrcweir // Default value != soffice.?
182cdf0e10cSrcweir // Action: nop
183cdf0e10cSrcweir //
184cdf0e10cSrcweir // 4.
185cdf0e10cSrcweir // Target key does not exist
186*102561b7Smseidel // Action: nop
187cdf0e10cSrcweir //-----------------------------------------
188cdf0e10cSrcweir 
Unregister4MsDocAll(MSIHANDLE handle)189cdf0e10cSrcweir void Unregister4MsDocAll(MSIHANDLE handle)
190*102561b7Smseidel {
191cdf0e10cSrcweir     try
192cdf0e10cSrcweir     {
193cdf0e10cSrcweir         RegistrationContextInformation RegContext(handle, GetOfficeExecutablePath(handle));
194cdf0e10cSrcweir         RegistrarPtr CurrentRegistrar = CreateRegistrar(IsAllUserInstallation(handle), RegContext);
195*102561b7Smseidel 
196cdf0e10cSrcweir         CurrentRegistrar->UnregisterAllAndCleanUpRegistry();
197cdf0e10cSrcweir     }
198cdf0e10cSrcweir     catch(RegistryException&)
199cdf0e10cSrcweir     {
200cdf0e10cSrcweir         assert(false);
201cdf0e10cSrcweir     }
202cdf0e10cSrcweir }
203cdf0e10cSrcweir 
204cdf0e10cSrcweir //-----------------------------------------
205cdf0e10cSrcweir // restores lost settings formerly made
206cdf0e10cSrcweir // with Register4MsDoc
207cdf0e10cSrcweir //-----------------------------------------
208cdf0e10cSrcweir 
RepairRegister4MsDocSettings(MSIHANDLE handle)209cdf0e10cSrcweir void RepairRegister4MsDocSettings(MSIHANDLE handle)
210*102561b7Smseidel {
211cdf0e10cSrcweir     try
212cdf0e10cSrcweir     {
213cdf0e10cSrcweir         RegistrationContextInformation RegContext(handle, GetOfficeExecutablePath(handle));
214cdf0e10cSrcweir         RegistrarPtr CurrentRegistrar = CreateRegistrar(IsAllUserInstallation(handle), RegContext);
215*102561b7Smseidel 
216cdf0e10cSrcweir         CurrentRegistrar->RepairRegistrationState();
217cdf0e10cSrcweir     }
218cdf0e10cSrcweir     catch(RegistryException&)
219cdf0e10cSrcweir     {
220cdf0e10cSrcweir         assert(false);
221cdf0e10cSrcweir     }
222cdf0e10cSrcweir }
223cdf0e10cSrcweir 
IsRegisteredFor(MSIHANDLE handle,int State)224cdf0e10cSrcweir bool IsRegisteredFor(MSIHANDLE handle, int State)
225*102561b7Smseidel {
226cdf0e10cSrcweir     bool Registered = false;
227*102561b7Smseidel 
228cdf0e10cSrcweir     try
229cdf0e10cSrcweir     {
230cdf0e10cSrcweir         RegistrationContextInformation RegContext(handle, GetOfficeExecutablePath(handle));
231cdf0e10cSrcweir         RegistrarPtr CurrentRegistrar = CreateRegistrar(IsAllUserInstallation(handle), RegContext);
232*102561b7Smseidel 
233cdf0e10cSrcweir         Registered = CurrentRegistrar->IsRegisteredFor(State);
234cdf0e10cSrcweir     }
235cdf0e10cSrcweir     catch(RegistryException&)
236cdf0e10cSrcweir     {
237cdf0e10cSrcweir         assert(false);
238cdf0e10cSrcweir     }
239cdf0e10cSrcweir     return Registered;
240cdf0e10cSrcweir }
241cdf0e10cSrcweir 
242cdf0e10cSrcweir #define SO60_UNINSTALL_KEY L"Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\StarOffice 6.0"
243cdf0e10cSrcweir #define SO_BACKUP_KEY      L"soffice6.bak"
244cdf0e10cSrcweir #define REGMSDOCSTATE      L"Reg4MsDocState"
245cdf0e10cSrcweir #define SOFTWARE_CLASSES   L"Software\\Classes"
246cdf0e10cSrcweir 
FixReturnRegistrationState(MSIHANDLE handle)247cdf0e10cSrcweir int FixReturnRegistrationState(MSIHANDLE handle)
248cdf0e10cSrcweir {
249cdf0e10cSrcweir 	int registration_state = 0;
250cdf0e10cSrcweir 
251cdf0e10cSrcweir 	try
252cdf0e10cSrcweir 	{
253*102561b7Smseidel 		WindowsRegistry registry;
254cdf0e10cSrcweir 
255cdf0e10cSrcweir 		RegistryValue rv_regmsdocstate = RegistryValue(
256cdf0e10cSrcweir 			new RegistryValueImpl(REGMSDOCSTATE, 0));
257*102561b7Smseidel 
258cdf0e10cSrcweir 		RegistryKey so_bak_key;
259cdf0e10cSrcweir 
260cdf0e10cSrcweir 		if (IsAllUserInstallation(handle))
261cdf0e10cSrcweir 		{
262cdf0e10cSrcweir 			RegistryKey hkcr_key = registry.GetClassesRootKey();
263*102561b7Smseidel 
264cdf0e10cSrcweir 			if (hkcr_key->HasSubKey(SO_BACKUP_KEY))
265cdf0e10cSrcweir 				so_bak_key = hkcr_key->OpenSubKey(SO_BACKUP_KEY);
266cdf0e10cSrcweir 			else
267*102561b7Smseidel 				so_bak_key = hkcr_key->CreateSubKey(SO_BACKUP_KEY);
268cdf0e10cSrcweir 
269cdf0e10cSrcweir 			if (!so_bak_key->HasValue(REGMSDOCSTATE))
270*102561b7Smseidel 			{
271cdf0e10cSrcweir 				// set a defined value
272cdf0e10cSrcweir 				so_bak_key->SetValue(rv_regmsdocstate);
273cdf0e10cSrcweir 
274cdf0e10cSrcweir 				RegistryKey hklm_key = registry.GetLocalMachineKey();
275cdf0e10cSrcweir 
276cdf0e10cSrcweir 				if (hklm_key->HasSubKey(SO60_UNINSTALL_KEY))
277cdf0e10cSrcweir 				{
278*102561b7Smseidel 					RegistryKey so_uninst_key =
279cdf0e10cSrcweir 						hklm_key->OpenSubKey(SO60_UNINSTALL_KEY);
280cdf0e10cSrcweir 
281cdf0e10cSrcweir 					if (so_uninst_key->HasValue(REGMSDOCSTATE))
282*102561b7Smseidel 						so_bak_key->CopyValue(so_uninst_key, REGMSDOCSTATE);
283*102561b7Smseidel 				}
284*102561b7Smseidel 			}
285cdf0e10cSrcweir 		}
286*102561b7Smseidel 		else
287cdf0e10cSrcweir 		{
288*102561b7Smseidel 			RegistryKey hkcu_classes_key =
289cdf0e10cSrcweir 				registry.GetCurrentUserKey()->OpenSubKey(SOFTWARE_CLASSES);
290*102561b7Smseidel 
291cdf0e10cSrcweir 			so_bak_key = hkcu_classes_key->CreateSubKey(SO_BACKUP_KEY);
292*102561b7Smseidel 
293cdf0e10cSrcweir 			if (!so_bak_key->HasValue(REGMSDOCSTATE))
294cdf0e10cSrcweir 			{
295cdf0e10cSrcweir 				// set a defined value
296cdf0e10cSrcweir 				so_bak_key->SetValue(rv_regmsdocstate);
297cdf0e10cSrcweir 
298*102561b7Smseidel 				RegistryKey hklm_sftw_classes =
299cdf0e10cSrcweir 					registry.GetLocalMachineKey()->OpenSubKey(SOFTWARE_CLASSES, false);
300*102561b7Smseidel 
301cdf0e10cSrcweir 				RegistryKey so_bak_key_old;
302cdf0e10cSrcweir 
303cdf0e10cSrcweir 				if (hklm_sftw_classes->HasSubKey(SO_BACKUP_KEY))
304cdf0e10cSrcweir 				{
305cdf0e10cSrcweir 					so_bak_key_old = hklm_sftw_classes->OpenSubKey(SO_BACKUP_KEY, false);
306*102561b7Smseidel 
307cdf0e10cSrcweir 					if (so_bak_key_old->HasValue(REGMSDOCSTATE))
308*102561b7Smseidel 						so_bak_key->CopyValue(so_bak_key_old, REGMSDOCSTATE);
309cdf0e10cSrcweir 				}
310cdf0e10cSrcweir 				else // try the uninstall key
311cdf0e10cSrcweir 				{
312cdf0e10cSrcweir 					RegistryKey hklm_key = registry.GetLocalMachineKey();
313cdf0e10cSrcweir 
314cdf0e10cSrcweir 					if (hklm_key->HasSubKey(SO60_UNINSTALL_KEY))
315cdf0e10cSrcweir 					{
316*102561b7Smseidel 						RegistryKey so_uninst_key =
317cdf0e10cSrcweir 							hklm_key->OpenSubKey(SO60_UNINSTALL_KEY);
318cdf0e10cSrcweir 
319cdf0e10cSrcweir 						if (so_uninst_key->HasValue(REGMSDOCSTATE))
320*102561b7Smseidel 							so_bak_key->CopyValue(so_uninst_key, REGMSDOCSTATE);
321cdf0e10cSrcweir 					}
322cdf0e10cSrcweir 				}
323cdf0e10cSrcweir 			}
324*102561b7Smseidel 		}
325*102561b7Smseidel 
326cdf0e10cSrcweir 		rv_regmsdocstate = so_bak_key->GetValue(REGMSDOCSTATE);
327cdf0e10cSrcweir 		registration_state = rv_regmsdocstate->GetDataAsInt();
328cdf0e10cSrcweir 	}
329cdf0e10cSrcweir 	catch(RegistryException&)
330cdf0e10cSrcweir 	{
331cdf0e10cSrcweir 		registration_state = 0;
332cdf0e10cSrcweir 	}
333cdf0e10cSrcweir 
334cdf0e10cSrcweir 	return registration_state;
335cdf0e10cSrcweir }
336cdf0e10cSrcweir 
337