1*32b1fd08SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*32b1fd08SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*32b1fd08SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*32b1fd08SAndrew Rist  * distributed with this work for additional information
6*32b1fd08SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*32b1fd08SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*32b1fd08SAndrew Rist  * "License"); you may not use this file except in compliance
9*32b1fd08SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*32b1fd08SAndrew Rist  *
11*32b1fd08SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*32b1fd08SAndrew Rist  *
13*32b1fd08SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*32b1fd08SAndrew Rist  * software distributed under the License is distributed on an
15*32b1fd08SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*32b1fd08SAndrew Rist  * KIND, either express or implied.  See the License for the
17*32b1fd08SAndrew Rist  * specific language governing permissions and limitations
18*32b1fd08SAndrew Rist  * under the License.
19*32b1fd08SAndrew Rist  *
20*32b1fd08SAndrew Rist  *************************************************************/
21*32b1fd08SAndrew Rist 
22*32b1fd08SAndrew 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 {
51cdf0e10cSrcweir     RegistrarPtr CreateRegistrar(bool InstallForAllUser, const RegistrationContextInformation& RegCtx)
52cdf0e10cSrcweir     {
53cdf0e10cSrcweir         RegistrarPtr RegPtr;
54cdf0e10cSrcweir 
55cdf0e10cSrcweir         if (InstallForAllUser)
56cdf0e10cSrcweir             RegPtr = RegistrarPtr(new Registrar(RegCtx));
57cdf0e10cSrcweir         else
58cdf0e10cSrcweir             RegPtr = RegistrarPtr(new UserRegistrar(RegCtx));
59cdf0e10cSrcweir 
60cdf0e10cSrcweir         return RegPtr;
61cdf0e10cSrcweir     }
62cdf0e10cSrcweir } // namespace private
63cdf0e10cSrcweir 
64cdf0e10cSrcweir bool query_preselect_registration_for_ms_application(MSIHANDLE handle, int Register)
65cdf0e10cSrcweir {
66cdf0e10cSrcweir     bool preselect = false;
67cdf0e10cSrcweir 
68cdf0e10cSrcweir     try
69cdf0e10cSrcweir     {
70cdf0e10cSrcweir         RegistrationContextInformation RegContext(handle, GetOfficeExecutablePath(handle));
71cdf0e10cSrcweir         RegistrarPtr CurrentRegistrar = CreateRegistrar(IsAllUserInstallation(handle), RegContext);
72cdf0e10cSrcweir 
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);
83cdf0e10cSrcweir     }
84cdf0e10cSrcweir     return preselect;
85cdf0e10cSrcweir }
86cdf0e10cSrcweir 
87cdf0e10cSrcweir //-----------------------------------------
88cdf0e10cSrcweir // registers StarOffice for MS document
89cdf0e10cSrcweir // types and as default HTML editor if
90cdf0e10cSrcweir // specified
91cdf0e10cSrcweir //-----------------------------------------
92cdf0e10cSrcweir 
93cdf0e10cSrcweir void Register4MsDoc(MSIHANDLE handle, int Register)
94cdf0e10cSrcweir {
95cdf0e10cSrcweir     try
96cdf0e10cSrcweir     {
97cdf0e10cSrcweir         RegistrationContextInformation RegContext(handle, GetOfficeExecutablePath(handle));
98cdf0e10cSrcweir         RegistrarPtr CurrentRegistrar = CreateRegistrar(IsAllUserInstallation(handle), RegContext);
99cdf0e10cSrcweir 
100cdf0e10cSrcweir         if ((Register & MSWORD))
101cdf0e10cSrcweir             CurrentRegistrar->RegisterForMsWord();
102cdf0e10cSrcweir 
103cdf0e10cSrcweir         if ((Register & MSEXCEL))
104cdf0e10cSrcweir             CurrentRegistrar->RegisterForMsExcel();
105cdf0e10cSrcweir 
106cdf0e10cSrcweir         if ((Register & MSPOWERPOINT))
107cdf0e10cSrcweir             CurrentRegistrar->RegisterForMsPowerPoint();
108cdf0e10cSrcweir 
109cdf0e10cSrcweir         if ((Register & HTML_EDITOR))
110cdf0e10cSrcweir             CurrentRegistrar->RegisterAsHtmlEditorForInternetExplorer();
111cdf0e10cSrcweir 
112cdf0e10cSrcweir         if ((Register & DEFAULT_SHELL_HTML_EDITOR))
113cdf0e10cSrcweir         {
114cdf0e10cSrcweir             CurrentRegistrar->RegisterAsDefaultHtmlEditorForInternetExplorer();
115cdf0e10cSrcweir             CurrentRegistrar->RegisterAsDefaultShellHtmlEditor();
116cdf0e10cSrcweir         }
117cdf0e10cSrcweir     }
118cdf0e10cSrcweir     catch(RegistryException&)
119cdf0e10cSrcweir     {
120cdf0e10cSrcweir         assert(false);
121cdf0e10cSrcweir     }
122cdf0e10cSrcweir 
123cdf0e10cSrcweir     if (Register)
124cdf0e10cSrcweir         SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0);
125cdf0e10cSrcweir }
126cdf0e10cSrcweir 
127cdf0e10cSrcweir void Unregister4MsDoc(MSIHANDLE handle, int Unregister)
128cdf0e10cSrcweir {
129cdf0e10cSrcweir     try
130cdf0e10cSrcweir     {
131cdf0e10cSrcweir         RegistrationContextInformation RegContext(handle, GetOfficeExecutablePath(handle));
132cdf0e10cSrcweir         RegistrarPtr CurrentRegistrar = CreateRegistrar(IsAllUserInstallation(handle), RegContext);
133cdf0e10cSrcweir 
134cdf0e10cSrcweir         if ((Unregister & MSWORD) && CurrentRegistrar->IsRegisteredFor(MSWORD))
135cdf0e10cSrcweir             CurrentRegistrar->UnregisterForMsWord();
136cdf0e10cSrcweir 
137cdf0e10cSrcweir         if ((Unregister & HTML_EDITOR) && CurrentRegistrar->IsRegisteredFor(HTML_EDITOR))
138cdf0e10cSrcweir             CurrentRegistrar->UnregisterAsHtmlEditorForInternetExplorer();
139cdf0e10cSrcweir 
140cdf0e10cSrcweir         if ((Unregister & MSEXCEL) && CurrentRegistrar->IsRegisteredFor(MSEXCEL))
141cdf0e10cSrcweir             CurrentRegistrar->UnregisterForMsExcel();
142cdf0e10cSrcweir 
143cdf0e10cSrcweir         if ((Unregister & MSPOWERPOINT) && CurrentRegistrar->IsRegisteredFor(MSPOWERPOINT))
144cdf0e10cSrcweir             CurrentRegistrar->UnregisterForMsPowerPoint();
145cdf0e10cSrcweir 
146cdf0e10cSrcweir         if ((Unregister & DEFAULT_HTML_EDITOR_FOR_IE) && CurrentRegistrar->IsRegisteredFor(DEFAULT_HTML_EDITOR_FOR_IE))
147cdf0e10cSrcweir             CurrentRegistrar->UnregisterAsDefaultHtmlEditorForInternetExplorer();
148cdf0e10cSrcweir 
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     }
156cdf0e10cSrcweir 
157cdf0e10cSrcweir 	if (Unregister)
158cdf0e10cSrcweir 		SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0);
159cdf0e10cSrcweir }
160cdf0e10cSrcweir 
161cdf0e10cSrcweir //-----------------------------------------
162cdf0e10cSrcweir // restores the entries for the selected
163cdf0e10cSrcweir // registry entries
164cdf0e10cSrcweir // Algorithm:
165cdf0e10cSrcweir //
166cdf0e10cSrcweir // 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 //
173cdf0e10cSrcweir // 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
186cdf0e10cSrcweir // Action: nop
187cdf0e10cSrcweir //-----------------------------------------
188cdf0e10cSrcweir 
189cdf0e10cSrcweir void Unregister4MsDocAll(MSIHANDLE handle)
190cdf0e10cSrcweir {
191cdf0e10cSrcweir     try
192cdf0e10cSrcweir     {
193cdf0e10cSrcweir         RegistrationContextInformation RegContext(handle, GetOfficeExecutablePath(handle));
194cdf0e10cSrcweir         RegistrarPtr CurrentRegistrar = CreateRegistrar(IsAllUserInstallation(handle), RegContext);
195cdf0e10cSrcweir 
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 
209cdf0e10cSrcweir void RepairRegister4MsDocSettings(MSIHANDLE handle)
210cdf0e10cSrcweir {
211cdf0e10cSrcweir     try
212cdf0e10cSrcweir     {
213cdf0e10cSrcweir         RegistrationContextInformation RegContext(handle, GetOfficeExecutablePath(handle));
214cdf0e10cSrcweir         RegistrarPtr CurrentRegistrar = CreateRegistrar(IsAllUserInstallation(handle), RegContext);
215cdf0e10cSrcweir 
216cdf0e10cSrcweir         CurrentRegistrar->RepairRegistrationState();
217cdf0e10cSrcweir     }
218cdf0e10cSrcweir     catch(RegistryException&)
219cdf0e10cSrcweir     {
220cdf0e10cSrcweir         assert(false);
221cdf0e10cSrcweir     }
222cdf0e10cSrcweir }
223cdf0e10cSrcweir 
224cdf0e10cSrcweir bool IsRegisteredFor(MSIHANDLE handle, int State)
225cdf0e10cSrcweir {
226cdf0e10cSrcweir     bool Registered = false;
227cdf0e10cSrcweir 
228cdf0e10cSrcweir     try
229cdf0e10cSrcweir     {
230cdf0e10cSrcweir         RegistrationContextInformation RegContext(handle, GetOfficeExecutablePath(handle));
231cdf0e10cSrcweir         RegistrarPtr CurrentRegistrar = CreateRegistrar(IsAllUserInstallation(handle), RegContext);
232cdf0e10cSrcweir 
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 
247cdf0e10cSrcweir int FixReturnRegistrationState(MSIHANDLE handle)
248cdf0e10cSrcweir {
249cdf0e10cSrcweir 	int registration_state = 0;
250cdf0e10cSrcweir 
251cdf0e10cSrcweir 	try
252cdf0e10cSrcweir 	{
253cdf0e10cSrcweir 		WindowsRegistry registry;
254cdf0e10cSrcweir 
255cdf0e10cSrcweir 		RegistryValue rv_regmsdocstate = RegistryValue(
256cdf0e10cSrcweir 			new RegistryValueImpl(REGMSDOCSTATE, 0));
257cdf0e10cSrcweir 
258cdf0e10cSrcweir 		RegistryKey so_bak_key;
259cdf0e10cSrcweir 
260cdf0e10cSrcweir 		if (IsAllUserInstallation(handle))
261cdf0e10cSrcweir 		{
262cdf0e10cSrcweir 			RegistryKey hkcr_key = registry.GetClassesRootKey();
263cdf0e10cSrcweir 
264cdf0e10cSrcweir 			if (hkcr_key->HasSubKey(SO_BACKUP_KEY))
265cdf0e10cSrcweir 				so_bak_key = hkcr_key->OpenSubKey(SO_BACKUP_KEY);
266cdf0e10cSrcweir 			else
267cdf0e10cSrcweir 				so_bak_key = hkcr_key->CreateSubKey(SO_BACKUP_KEY);
268cdf0e10cSrcweir 
269cdf0e10cSrcweir 			if (!so_bak_key->HasValue(REGMSDOCSTATE))
270cdf0e10cSrcweir 			{
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 				{
278cdf0e10cSrcweir 					RegistryKey so_uninst_key =
279cdf0e10cSrcweir 						hklm_key->OpenSubKey(SO60_UNINSTALL_KEY);
280cdf0e10cSrcweir 
281cdf0e10cSrcweir 					if (so_uninst_key->HasValue(REGMSDOCSTATE))
282cdf0e10cSrcweir 						so_bak_key->CopyValue(so_uninst_key, REGMSDOCSTATE);
283cdf0e10cSrcweir 				}
284cdf0e10cSrcweir 			}
285cdf0e10cSrcweir 		}
286cdf0e10cSrcweir 		else
287cdf0e10cSrcweir 		{
288cdf0e10cSrcweir 			RegistryKey hkcu_classes_key =
289cdf0e10cSrcweir 				registry.GetCurrentUserKey()->OpenSubKey(SOFTWARE_CLASSES);
290cdf0e10cSrcweir 
291cdf0e10cSrcweir 			so_bak_key = hkcu_classes_key->CreateSubKey(SO_BACKUP_KEY);
292cdf0e10cSrcweir 
293cdf0e10cSrcweir 			if (!so_bak_key->HasValue(REGMSDOCSTATE))
294cdf0e10cSrcweir 			{
295cdf0e10cSrcweir 				// set a defined value
296cdf0e10cSrcweir 				so_bak_key->SetValue(rv_regmsdocstate);
297cdf0e10cSrcweir 
298cdf0e10cSrcweir 				RegistryKey hklm_sftw_classes =
299cdf0e10cSrcweir 					registry.GetLocalMachineKey()->OpenSubKey(SOFTWARE_CLASSES, false);
300cdf0e10cSrcweir 
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);
306cdf0e10cSrcweir 
307cdf0e10cSrcweir 					if (so_bak_key_old->HasValue(REGMSDOCSTATE))
308cdf0e10cSrcweir 						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 					{
316cdf0e10cSrcweir 						RegistryKey so_uninst_key =
317cdf0e10cSrcweir 							hklm_key->OpenSubKey(SO60_UNINSTALL_KEY);
318cdf0e10cSrcweir 
319cdf0e10cSrcweir 						if (so_uninst_key->HasValue(REGMSDOCSTATE))
320cdf0e10cSrcweir 							so_bak_key->CopyValue(so_uninst_key, REGMSDOCSTATE);
321cdf0e10cSrcweir 					}
322cdf0e10cSrcweir 				}
323cdf0e10cSrcweir 			}
324cdf0e10cSrcweir 		}
325cdf0e10cSrcweir 
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