Update ooo320-m1
[ooovba.git] / setup_native / source / win32 / customactions / reg4msdoc / register.cxx
blobb61405b6dcd4142e094b22c8eadfb73ba893a168
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: register.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "register.hxx"
32 #include "registryexception.hxx"
33 #include "registrationcontextinformation.hxx"
34 #include "userregistrar.hxx"
35 #include "windowsregistry.hxx"
36 #include "stringconverter.hxx"
37 #include "msihelper.hxx"
39 #ifdef _MSC_VER
40 #pragma warning(push, 1) /* disable warnings within system headers */
41 #pragma warning(disable: 4917)
42 #endif
43 #include <shlobj.h>
44 #ifdef _MSC_VER
45 #pragma warning(pop)
46 #endif
49 #include <assert.h>
50 #ifdef _MSC_VER
51 #pragma warning(disable: 4350)
52 #endif
54 typedef std::auto_ptr<Registrar> RegistrarPtr;
56 namespace /* private */
58 RegistrarPtr CreateRegistrar(bool InstallForAllUser, const RegistrationContextInformation& RegCtx)
60 RegistrarPtr RegPtr;
62 if (InstallForAllUser)
63 RegPtr = RegistrarPtr(new Registrar(RegCtx));
64 else
65 RegPtr = RegistrarPtr(new UserRegistrar(RegCtx));
67 return RegPtr;
69 } // namespace private
71 bool query_preselect_registration_for_ms_application(MSIHANDLE handle, int Register)
73 bool preselect = false;
75 try
77 RegistrationContextInformation RegContext(handle, GetOfficeExecutablePath(handle));
78 RegistrarPtr CurrentRegistrar = CreateRegistrar(IsAllUserInstallation(handle), RegContext);
80 if (Register & MSWORD)
81 preselect = CurrentRegistrar->QueryPreselectMsWordRegistration();
82 else if (Register & MSEXCEL)
83 preselect = CurrentRegistrar->QueryPreselectMsExcelRegistration();
84 else if (Register & MSPOWERPOINT)
85 preselect = CurrentRegistrar->QueryPreselectMsPowerPointRegistration();
87 catch(RegistryException&)
89 assert(false);
91 return preselect;
94 //-----------------------------------------
95 // registers StarOffice for MS document
96 // types and as default HTML editor if
97 // specified
98 //-----------------------------------------
100 void Register4MsDoc(MSIHANDLE handle, int Register)
104 RegistrationContextInformation RegContext(handle, GetOfficeExecutablePath(handle));
105 RegistrarPtr CurrentRegistrar = CreateRegistrar(IsAllUserInstallation(handle), RegContext);
107 if ((Register & MSWORD))
108 CurrentRegistrar->RegisterForMsWord();
110 if ((Register & MSEXCEL))
111 CurrentRegistrar->RegisterForMsExcel();
113 if ((Register & MSPOWERPOINT))
114 CurrentRegistrar->RegisterForMsPowerPoint();
116 if ((Register & HTML_EDITOR))
117 CurrentRegistrar->RegisterAsHtmlEditorForInternetExplorer();
119 if ((Register & DEFAULT_SHELL_HTML_EDITOR))
121 CurrentRegistrar->RegisterAsDefaultHtmlEditorForInternetExplorer();
122 CurrentRegistrar->RegisterAsDefaultShellHtmlEditor();
125 catch(RegistryException&)
127 assert(false);
130 if (Register)
131 SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0);
134 void Unregister4MsDoc(MSIHANDLE handle, int Unregister)
138 RegistrationContextInformation RegContext(handle, GetOfficeExecutablePath(handle));
139 RegistrarPtr CurrentRegistrar = CreateRegistrar(IsAllUserInstallation(handle), RegContext);
141 if ((Unregister & MSWORD) && CurrentRegistrar->IsRegisteredFor(MSWORD))
142 CurrentRegistrar->UnregisterForMsWord();
144 if ((Unregister & HTML_EDITOR) && CurrentRegistrar->IsRegisteredFor(HTML_EDITOR))
145 CurrentRegistrar->UnregisterAsHtmlEditorForInternetExplorer();
147 if ((Unregister & MSEXCEL) && CurrentRegistrar->IsRegisteredFor(MSEXCEL))
148 CurrentRegistrar->UnregisterForMsExcel();
150 if ((Unregister & MSPOWERPOINT) && CurrentRegistrar->IsRegisteredFor(MSPOWERPOINT))
151 CurrentRegistrar->UnregisterForMsPowerPoint();
153 if ((Unregister & DEFAULT_HTML_EDITOR_FOR_IE) && CurrentRegistrar->IsRegisteredFor(DEFAULT_HTML_EDITOR_FOR_IE))
154 CurrentRegistrar->UnregisterAsDefaultHtmlEditorForInternetExplorer();
156 if ((Unregister & DEFAULT_SHELL_HTML_EDITOR) && CurrentRegistrar->IsRegisteredFor(DEFAULT_SHELL_HTML_EDITOR))
157 CurrentRegistrar->UnregisterAsDefaultShellHtmlEditor();
159 catch(RegistryException&)
161 assert(false);
164 if (Unregister)
165 SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0);
168 //-----------------------------------------
169 // restores the entries for the selected
170 // registry entries
171 // Algorithm:
173 // 1.
174 // Target key exist (e.g. '.doc')
175 // Default value == soffice.?
176 // Backup key != empty
177 // Action: Replace Default value with backup
178 // key
180 // 2.
181 // Target key exist
182 // Default value == soffice.?
183 // Backup key == empty
184 // Action: delete default value
186 // 3.
187 // Target key exist
188 // Default value != soffice.?
189 // Action: nop
191 // 4.
192 // Target key does not exist
193 // Action: nop
194 //-----------------------------------------
196 void Unregister4MsDocAll(MSIHANDLE handle)
200 RegistrationContextInformation RegContext(handle, GetOfficeExecutablePath(handle));
201 RegistrarPtr CurrentRegistrar = CreateRegistrar(IsAllUserInstallation(handle), RegContext);
203 CurrentRegistrar->UnregisterAllAndCleanUpRegistry();
205 catch(RegistryException&)
207 assert(false);
211 //-----------------------------------------
212 // restores lost settings formerly made
213 // with Register4MsDoc
214 //-----------------------------------------
216 void RepairRegister4MsDocSettings(MSIHANDLE handle)
220 RegistrationContextInformation RegContext(handle, GetOfficeExecutablePath(handle));
221 RegistrarPtr CurrentRegistrar = CreateRegistrar(IsAllUserInstallation(handle), RegContext);
223 CurrentRegistrar->RepairRegistrationState();
225 catch(RegistryException&)
227 assert(false);
231 bool IsRegisteredFor(MSIHANDLE handle, int State)
233 bool Registered = false;
237 RegistrationContextInformation RegContext(handle, GetOfficeExecutablePath(handle));
238 RegistrarPtr CurrentRegistrar = CreateRegistrar(IsAllUserInstallation(handle), RegContext);
240 Registered = CurrentRegistrar->IsRegisteredFor(State);
242 catch(RegistryException&)
244 assert(false);
246 return Registered;
249 #define SO60_UNINSTALL_KEY L"Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\StarOffice 6.0"
250 #define SO_BACKUP_KEY L"soffice6.bak"
251 #define REGMSDOCSTATE L"Reg4MsDocState"
252 #define SOFTWARE_CLASSES L"Software\\Classes"
254 int FixReturnRegistrationState(MSIHANDLE handle)
256 int registration_state = 0;
260 WindowsRegistry registry;
262 RegistryValue rv_regmsdocstate = RegistryValue(
263 new RegistryValueImpl(REGMSDOCSTATE, 0));
265 RegistryKey so_bak_key;
267 if (IsAllUserInstallation(handle))
269 RegistryKey hkcr_key = registry.GetClassesRootKey();
271 if (hkcr_key->HasSubKey(SO_BACKUP_KEY))
272 so_bak_key = hkcr_key->OpenSubKey(SO_BACKUP_KEY);
273 else
274 so_bak_key = hkcr_key->CreateSubKey(SO_BACKUP_KEY);
276 if (!so_bak_key->HasValue(REGMSDOCSTATE))
278 // set a defined value
279 so_bak_key->SetValue(rv_regmsdocstate);
281 RegistryKey hklm_key = registry.GetLocalMachineKey();
283 if (hklm_key->HasSubKey(SO60_UNINSTALL_KEY))
285 RegistryKey so_uninst_key =
286 hklm_key->OpenSubKey(SO60_UNINSTALL_KEY);
288 if (so_uninst_key->HasValue(REGMSDOCSTATE))
289 so_bak_key->CopyValue(so_uninst_key, REGMSDOCSTATE);
293 else
295 RegistryKey hkcu_classes_key =
296 registry.GetCurrentUserKey()->OpenSubKey(SOFTWARE_CLASSES);
298 so_bak_key = hkcu_classes_key->CreateSubKey(SO_BACKUP_KEY);
300 if (!so_bak_key->HasValue(REGMSDOCSTATE))
302 // set a defined value
303 so_bak_key->SetValue(rv_regmsdocstate);
305 RegistryKey hklm_sftw_classes =
306 registry.GetLocalMachineKey()->OpenSubKey(SOFTWARE_CLASSES, false);
308 RegistryKey so_bak_key_old;
310 if (hklm_sftw_classes->HasSubKey(SO_BACKUP_KEY))
312 so_bak_key_old = hklm_sftw_classes->OpenSubKey(SO_BACKUP_KEY, false);
314 if (so_bak_key_old->HasValue(REGMSDOCSTATE))
315 so_bak_key->CopyValue(so_bak_key_old, REGMSDOCSTATE);
317 else // try the uninstall key
319 RegistryKey hklm_key = registry.GetLocalMachineKey();
321 if (hklm_key->HasSubKey(SO60_UNINSTALL_KEY))
323 RegistryKey so_uninst_key =
324 hklm_key->OpenSubKey(SO60_UNINSTALL_KEY);
326 if (so_uninst_key->HasValue(REGMSDOCSTATE))
327 so_bak_key->CopyValue(so_uninst_key, REGMSDOCSTATE);
333 rv_regmsdocstate = so_bak_key->GetValue(REGMSDOCSTATE);
334 registration_state = rv_regmsdocstate->GetDataAsInt();
336 catch(RegistryException&)
338 registration_state = 0;
341 return registration_state;