1 // Registrar.cpp: Implementierung der Klasse Registrar.
3 //////////////////////////////////////////////////////////////////////
5 #include "registrar.hxx"
7 #ifndef _REGISTRYVALUEIMPL_HXX_
8 #include "RegistryValueImpl.hxx"
10 #include "windowsregistry.hxx"
11 #include "registryexception.hxx"
15 #pragma warning(disable: 4350 4482)
19 //----------------------------------------------------------
21 inline void OutputDebugStringFormat( LPCTSTR pFormat
, ... )
26 va_start( args
, pFormat
);
27 StringCchVPrintf( buffer
, sizeof(buffer
), pFormat
, args
);
28 OutputDebugString( buffer
);
31 static inline void OutputDebugStringFormat( LPCTSTR
, ... )
35 //----------------------------------------------------------
37 const int MSWORD
= 0x1;
38 const int MSEXCEL
= 0x2;
39 const int MSPOWERPOINT
= 0x4;
40 const int DEFAULT_HTML_EDITOR_FOR_IE
= 0x8;
41 const int HTML_EDITOR
= 0x10;
42 const int DEFAULT_SHELL_HTML_EDITOR
= 0x20;
44 namespace /* private */
46 const std::wstring HTM_OPENWITHLIST
= L
".htm\\OpenWithList";
47 const std::wstring APPLICATIONS
= L
"Applications";
48 const std::wstring SHELL_EDIT_COMMAND
= L
"shell\\edit\\command";
49 const std::wstring HTML_EDIT
= L
"HTML Edit";
50 const std::wstring HTML_EDIT_DISPLAY_NAME
= L
"Edit Display Name";
51 const std::wstring SHELL_EDIT_COMMAND_BACKUP
= L
"Shell Edit Cmd";
52 const std::wstring DEFAULT_HTML_EDITOR
= L
"Default HTML Editor";
53 const std::wstring MS_IE_DEF_HTML_EDITOR
= L
"Software\\Microsoft\\Internet Explorer\\Default HTML Editor";
54 const std::wstring MS_IE_DEF_HTML_EDITOR_SHL_EDIT_CMD
= L
"Software\\Microsoft\\Internet Explorer\\Default HTML Editor\\shell\\edit\\command";
57 Registrar::Registrar(const RegistrationContextInformation
& RegContext
) :
58 m_ContextInformation(RegContext
),
59 FORWARD_KEY_PREFIX(L
"OpenOffice.org"),//FORWARD_KEY_PREFIX(L"soffice6"),
60 DEFAULT_VALUE_NAME(L
""),
61 BACKUP_VALUE_NAME(L
"Backup"),
62 PRIVATE_BACKUP_KEY_NAME(L
"OpenOffice.org.reg4msdocmsi"),//PRIVATE_BACKUP_KEY_NAME(L"soffice6.bak"),
63 REGISTRATION_STATE(L
"Reg4MsDocState")
65 m_RootKey
= WindowsRegistry().GetClassesRootKey();
68 Registrar::~Registrar()
72 void Registrar::RegisterForMsWord() const
74 assert(m_RootKey
.get());
76 RegisterForMsOfficeApplication(
77 m_ContextInformation
.GetWordDocumentFileExtension(),
78 m_ContextInformation
.GetWordDocumentDisplayName(),
79 m_ContextInformation
.GetWordDocumentDefaultIconEntry(),
80 m_ContextInformation
.GetWordDocumentDefaultShellCommand(),
81 m_ContextInformation
.ShellNewCommandDisplayName(),
82 RegistrationContextInformation::Writer
);
84 RegisterForMsOfficeApplication(
85 m_ContextInformation
.GetMOOXWordDocumentFileExtension(),
86 m_ContextInformation
.GetMOOXWordDocumentDisplayName(),
87 m_ContextInformation
.GetWordDocumentDefaultIconEntry(),
88 m_ContextInformation
.GetWordDocumentDefaultShellCommand(),
89 m_ContextInformation
.ShellNewCommandDisplayName(),
90 RegistrationContextInformation::Writer
);
92 RegisterForMsOfficeApplication(
93 m_ContextInformation
.GetWordTemplateFileExtension(),
94 m_ContextInformation
.GetWordTemplateDisplayName(),
95 m_ContextInformation
.GetWordTemplateDefaultIconEntry(),
96 m_ContextInformation
.GetWordTemplateDefaultShellCommand(),
97 m_ContextInformation
.ShellNewCommandDisplayName(),
98 RegistrationContextInformation::Writer
);
100 RegisterForMsOfficeApplication(
101 m_ContextInformation
.GetRtfDocumentFileExtension(),
102 m_ContextInformation
.GetRtfDocumentDisplayName(),
103 m_ContextInformation
.GetRtfDocumentDefaultIconEntry(),
104 m_ContextInformation
.GetRtfDocumentDefaultShellCommand(),
105 m_ContextInformation
.ShellNewCommandDisplayName(),
106 RegistrationContextInformation::Writer
);
108 SaveRegisteredFor(MSWORD
);
111 void Registrar::UnregisterForMsWord() const
113 assert(m_RootKey
.get());
117 UnregisterForMsOfficeApplication(
118 m_ContextInformation
.GetWordDocumentFileExtension());
120 catch(RegistryKeyNotFoundException
&)
125 UnregisterForMsOfficeApplication(
126 m_ContextInformation
.GetMOOXWordDocumentFileExtension());
128 catch(RegistryKeyNotFoundException
&)
133 UnregisterForMsOfficeApplication(
134 m_ContextInformation
.GetWordTemplateFileExtension());
136 catch(RegistryKeyNotFoundException
&)
141 UnregisterForMsOfficeApplication(
142 m_ContextInformation
.GetRtfDocumentFileExtension());
144 catch(RegistryKeyNotFoundException
&)
147 SaveNotRegisteredFor(MSWORD
);
150 bool Registrar::QueryPreselectForMsApplication(const std::wstring
& file_extension
) const
152 bool preselect
= false;
154 // We use HKCR else we would not see that a registration for
155 // MS Office applications already exist if we are about to
156 // register in HKCU\Software\Classes
157 RegistryKey root_key
= WindowsRegistry().GetClassesRootKey();
159 if (!root_key
->HasSubKey(file_extension
))
162 OutputDebugStringFormat( TEXT("QueryPreselect: No SubKey found for (%s), preselected!\n"), file_extension
.c_str() );
166 RegistryKey RegKey
= root_key
->OpenSubKey(file_extension
, false);
168 if (RegKey
->HasValue(DEFAULT_VALUE_NAME
))
170 RegistryValue RegVal
= RegKey
->GetValue(DEFAULT_VALUE_NAME
);
172 if (REG_SZ
== RegVal
->GetType() &&
173 IsOpenOfficeRegisteredForMsApplication(RegVal
->GetDataAsUniString()))
176 OutputDebugStringFormat( TEXT("QueryPreselect: (%s) registered to Office, preselected!\n"), file_extension
.c_str() );
178 else if ( (REG_SZ
== RegVal
->GetType()) && ! root_key
->HasSubKey( RegVal
->GetDataAsUniString() ) )
181 OutputDebugStringFormat( TEXT("QueryPreselect: (%s) registered but destination is empty, preselected!\n"), file_extension
.c_str() );
187 OutputDebugStringFormat( TEXT("QueryPreselect: No default found for SubKey (%s), preselected!\n"), file_extension
.c_str() );
193 bool Registrar::QueryPreselectMsWordRegistration() const
195 return QueryPreselectForMsApplication(
196 m_ContextInformation
.GetWordDocumentFileExtension());
199 void Registrar::RegisterForMsExcel() const
201 assert(m_RootKey
.get());
203 RegisterForMsOfficeApplication(
204 m_ContextInformation
.GetExcelSheetFileExtension(),
205 m_ContextInformation
.GetExcelSheetDisplayName(),
206 m_ContextInformation
.GetExcelSheetDefaultIconEntry(),
207 m_ContextInformation
.GetExcelSheetDefaultShellCommand(),
208 m_ContextInformation
.ShellNewCommandDisplayName(),
209 RegistrationContextInformation::Calc
);
211 RegisterForMsOfficeApplication(
212 m_ContextInformation
.GetMOOXExcelSheetFileExtension(),
213 m_ContextInformation
.GetMOOXExcelSheetDisplayName(),
214 m_ContextInformation
.GetExcelSheetDefaultIconEntry(),
215 m_ContextInformation
.GetExcelSheetDefaultShellCommand(),
216 m_ContextInformation
.ShellNewCommandDisplayName(),
217 RegistrationContextInformation::Calc
);
219 RegisterForMsOfficeApplication(
220 m_ContextInformation
.GetExcelTemplateFileExtension(),
221 m_ContextInformation
.GetExcelTemplateDisplayName(),
222 m_ContextInformation
.GetExcelTemplateDefaultIconEntry(),
223 m_ContextInformation
.GetExcelTemplateDefaultShellCommand(),
224 m_ContextInformation
.ShellNewCommandDisplayName(),
225 RegistrationContextInformation::Calc
);
227 SaveRegisteredFor(MSEXCEL
);
230 void Registrar::UnregisterForMsExcel() const
232 assert(m_RootKey
.get());
236 UnregisterForMsOfficeApplication(
237 m_ContextInformation
.GetExcelSheetFileExtension());
239 catch(RegistryKeyNotFoundException
&)
244 UnregisterForMsOfficeApplication(
245 m_ContextInformation
.GetMOOXExcelSheetFileExtension());
247 catch(RegistryKeyNotFoundException
&)
252 UnregisterForMsOfficeApplication(
253 m_ContextInformation
.GetExcelTemplateFileExtension());
255 catch(RegistryKeyNotFoundException
&)
258 SaveNotRegisteredFor(MSEXCEL
);
261 bool Registrar::QueryPreselectMsExcelRegistration() const
263 return QueryPreselectForMsApplication(
264 m_ContextInformation
.GetExcelSheetFileExtension());
267 void Registrar::RegisterForMsPowerPoint() const
269 assert(m_RootKey
.get());
271 RegisterForMsOfficeApplication(
272 m_ContextInformation
.GetPowerPointDocumentFileExtension(),
273 m_ContextInformation
.GetPowerPointDocumentDisplayName(),
274 m_ContextInformation
.GetPowerPointDocumentDefaultIconEntry(),
275 m_ContextInformation
.GetPowerPointDocumentDefaultShellCommand(),
276 m_ContextInformation
.ShellNewCommandDisplayName(),
277 RegistrationContextInformation::Impress
);
279 RegisterForMsOfficeApplication(
280 m_ContextInformation
.GetMOOXPowerPointDocumentFileExtension(),
281 m_ContextInformation
.GetMOOXPowerPointDocumentDisplayName(),
282 m_ContextInformation
.GetPowerPointDocumentDefaultIconEntry(),
283 m_ContextInformation
.GetPowerPointDocumentDefaultShellCommand(),
284 m_ContextInformation
.ShellNewCommandDisplayName(),
285 RegistrationContextInformation::Impress
);
287 RegisterForMsOfficeApplication(
288 m_ContextInformation
.GetPowerPointShowFileExtension(),
289 m_ContextInformation
.GetPowerPointShowDisplayName(),
290 m_ContextInformation
.GetPowerPointShowDefaultIconEntry(),
291 m_ContextInformation
.GetPowerPointShowDefaultShellCommand(),
292 m_ContextInformation
.ShellNewCommandDisplayName(),
293 RegistrationContextInformation::Impress
);
295 RegisterForMsOfficeApplication(
296 m_ContextInformation
.GetPowerPointTemplateFileExtension(),
297 m_ContextInformation
.GetPowerPointTemplateDisplayName(),
298 m_ContextInformation
.GetPowerPointTemplateDefaultIconEntry(),
299 m_ContextInformation
.GetPowerPointTemplateDefaultShellCommand(),
300 m_ContextInformation
.ShellNewCommandDisplayName(),
301 RegistrationContextInformation::Impress
);
303 SaveRegisteredFor(MSPOWERPOINT
);
306 void Registrar::UnregisterForMsPowerPoint() const
308 assert(m_RootKey
.get());
312 UnregisterForMsOfficeApplication(
313 m_ContextInformation
.GetPowerPointDocumentFileExtension());
315 catch(RegistryKeyNotFoundException
&)
320 UnregisterForMsOfficeApplication(
321 m_ContextInformation
.GetMOOXPowerPointDocumentFileExtension());
323 catch(RegistryKeyNotFoundException
&)
328 UnregisterForMsOfficeApplication(
329 m_ContextInformation
.GetPowerPointShowFileExtension());
331 catch(RegistryKeyNotFoundException
&)
336 UnregisterForMsOfficeApplication(
337 m_ContextInformation
.GetPowerPointTemplateFileExtension());
339 catch(RegistryKeyNotFoundException
&)
342 SaveNotRegisteredFor(MSPOWERPOINT
);
345 //-----------------------------------------
348 bool Registrar::QueryPreselectMsPowerPointRegistration() const
350 return QueryPreselectForMsApplication( m_ContextInformation
.GetPowerPointDocumentFileExtension()) &&
351 QueryPreselectForMsApplication( m_ContextInformation
.GetPowerPointShowFileExtension());
354 //-----------------------------------------
355 /** The documentation says we have to
356 make the following entries to register
357 a html editor for the Internet Explorer
358 HKCR\.htm\OpenWithList\App Friendly Name\shell\edit\command
359 But the reality shows that this works only
360 with Internet Explorer 5.x
361 Internet Explorer 6.0 wants the follwoing
363 HKCR\.htm\OpenWithList\App.exe
364 HKCR\Applications\App.ex\shell\edit\command
366 void Registrar::RegisterAsHtmlEditorForInternetExplorer() const
368 assert(m_RootKey
.get());
370 std::wstring OOFriendlyAppName
= m_ContextInformation
.GetOpenOfficeFriendlyAppName();
372 std::wstring RegKeyName
= HTM_OPENWITHLIST
+ std::wstring(L
"\\") + OOFriendlyAppName
;
373 RegistryKey RegKey
= m_RootKey
->CreateSubKey(RegKeyName
);
375 RegKey
= RegKey
->CreateSubKey(SHELL_EDIT_COMMAND
);
377 RegistryValue
RegVal(
378 new RegistryValueImpl(
380 m_ContextInformation
.GetOpenOfficeCommandline(RegistrationContextInformation::Open
,
381 RegistrationContextInformation::Writer
)));
383 RegKey
->SetValue(RegVal
);
385 RegKeyName
= APPLICATIONS
+ std::wstring(L
"\\") + OOFriendlyAppName
;
386 RegKey
= m_RootKey
->CreateSubKey(RegKeyName
);
388 RegVal
->SetName(L
"FriendlyAppName");
389 RegVal
->SetValue(OOFriendlyAppName
);
390 RegKey
->SetValue(RegVal
);
392 RegKey
= RegKey
->CreateSubKey(SHELL_EDIT_COMMAND
);
393 RegVal
->SetName(DEFAULT_VALUE_NAME
);
395 m_ContextInformation
.GetOpenOfficeCommandline(RegistrationContextInformation::Open
,
396 RegistrationContextInformation::Writer
));
397 RegKey
->SetValue(RegVal
);
399 SaveRegisteredFor(HTML_EDITOR
);
402 void Registrar::UnregisterAsHtmlEditorForInternetExplorer() const
404 assert(m_RootKey
.get());
408 std::wstring OOFriendlyAppName
= m_ContextInformation
.GetOpenOfficeFriendlyAppName();
410 RegistryKey aRegKey
= m_RootKey
->OpenSubKey( APPLICATIONS
);
411 if ( aRegKey
->HasSubKey( OOFriendlyAppName
) )
412 aRegKey
->DeleteSubKeyTree( OOFriendlyAppName
);
414 aRegKey
= m_RootKey
->OpenSubKey( HTM_OPENWITHLIST
);
415 if ( aRegKey
->HasSubKey( OOFriendlyAppName
) )
416 aRegKey
->DeleteSubKeyTree( OOFriendlyAppName
);
418 catch(RegistryKeyNotFoundException
&)
421 SaveNotRegisteredFor(HTML_EDITOR
);
424 void Registrar::RegisterAsDefaultHtmlEditorForInternetExplorer() const
426 assert(m_RootKey
.get());
428 RegistryKey RegistrationRootKey
= GetRootKeyForDefHtmlEditorForIERegistration();
430 RegistryKey RegKey
= RegistrationRootKey
->CreateSubKey(MS_IE_DEF_HTML_EDITOR_SHL_EDIT_CMD
);
432 RegistryValue RegVal
= RegistryValue(new RegistryValueImpl(DEFAULT_VALUE_NAME
, L
""));
434 if (RegKey
->HasValue(DEFAULT_VALUE_NAME
))
436 RegVal
= RegKey
->GetValue(DEFAULT_VALUE_NAME
);
438 std::wstring CmdLine
= RegVal
->GetDataAsUniString();
440 if (std::wstring::npos
== CmdLine
.find(m_ContextInformation
.GetOpenOfficeExecutableName()))
442 RegistryKey BackupRegKey
= m_RootKey
->CreateSubKey(PRIVATE_BACKUP_KEY_NAME
+ L
"\\" + DEFAULT_HTML_EDITOR
);
444 if (RegKey
->HasValue(DEFAULT_VALUE_NAME
))
445 BackupRegKey
->CopyValue(RegKey
, DEFAULT_VALUE_NAME
);
447 RegKey
= RegistrationRootKey
->OpenSubKey(MS_IE_DEF_HTML_EDITOR
);
448 if (RegKey
->HasValue(L
"Description"))
449 BackupRegKey
->CopyValue(RegKey
, L
"Description");
454 m_ContextInformation
.GetOpenOfficeCommandline(RegistrationContextInformation::Open
,
455 RegistrationContextInformation::Writer
));
456 RegKey
= RegistrationRootKey
->OpenSubKey(MS_IE_DEF_HTML_EDITOR_SHL_EDIT_CMD
);
457 RegKey
->SetValue(RegVal
);
459 RegVal
->SetName(L
"Description");
460 RegVal
->SetValue(m_ContextInformation
.GetOpenOfficeFriendlyAppName());
461 RegKey
= RegistrationRootKey
->OpenSubKey(MS_IE_DEF_HTML_EDITOR
);
462 RegKey
->SetValue(RegVal
);
464 SaveRegisteredFor(DEFAULT_HTML_EDITOR_FOR_IE
);
467 void Registrar::UnregisterAsDefaultHtmlEditorForInternetExplorer() const
469 assert(m_RootKey
.get());
471 RegistryKey RegistrationRootKey
= GetRootKeyForDefHtmlEditorForIERegistration();
473 RegistryKey RegKey
= RegistrationRootKey
->OpenSubKey(MS_IE_DEF_HTML_EDITOR_SHL_EDIT_CMD
);
475 if (RegKey
->HasValue(DEFAULT_VALUE_NAME
))
477 RegistryValue RegVal
= RegKey
->GetValue(DEFAULT_VALUE_NAME
);
479 std::wstring CmdLine
= RegVal
->GetDataAsUniString();
481 if (std::wstring::npos
!= CmdLine
.find(m_ContextInformation
.GetOpenOfficeExecutableName()))
483 RegistryKey BackupRegKey
= m_RootKey
->OpenSubKey(PRIVATE_BACKUP_KEY_NAME
);
485 if (BackupRegKey
->HasSubKey(DEFAULT_HTML_EDITOR
))
487 BackupRegKey
= BackupRegKey
->OpenSubKey(DEFAULT_HTML_EDITOR
);
489 if (BackupRegKey
->HasValue(DEFAULT_VALUE_NAME
))
490 RegKey
->CopyValue(BackupRegKey
, DEFAULT_VALUE_NAME
);
492 RegKey
->DeleteValue(DEFAULT_VALUE_NAME
);
494 RegKey
= RegistrationRootKey
->OpenSubKey(MS_IE_DEF_HTML_EDITOR
);
496 if (BackupRegKey
->HasValue(L
"Description"))
497 RegKey
->CopyValue(BackupRegKey
, L
"Description");
499 RegKey
->DeleteValue(L
"Description");
503 RegKey
->DeleteValue(DEFAULT_VALUE_NAME
);
504 RegKey
= RegistrationRootKey
->OpenSubKey(MS_IE_DEF_HTML_EDITOR
);
505 RegKey
->DeleteValue(L
"Description");
510 SaveNotRegisteredFor(DEFAULT_HTML_EDITOR_FOR_IE
);
513 void Registrar::RegisterAsDefaultShellHtmlEditor() const
515 assert(m_RootKey
.get());
517 RegistryKey RegKey
= m_RootKey
->CreateSubKey(L
".htm");
519 RegistryValue RegVal
= RegistryValue(
520 new RegistryValueImpl(DEFAULT_VALUE_NAME
, L
""));
522 if (RegKey
->HasValue(DEFAULT_VALUE_NAME
))
523 RegVal
= RegKey
->GetValue(DEFAULT_VALUE_NAME
);
525 std::wstring HtmFwdKey
= RegVal
->GetDataAsUniString();
526 if (0 == HtmFwdKey
.length() || !m_RootKey
->HasSubKey(HtmFwdKey
))
529 RegKey
= m_RootKey
->CreateSubKey(HtmFwdKey
+ L
"\\" + SHELL_EDIT_COMMAND
);
531 if (RegKey
->HasValue(DEFAULT_VALUE_NAME
))
533 RegVal
= RegKey
->GetValue(DEFAULT_VALUE_NAME
);
535 std::wstring CmdLine
= RegVal
->GetDataAsUniString();
537 // backup old values if we are not in place
538 if (std::wstring::npos
== CmdLine
.find(m_ContextInformation
.GetOpenOfficeExecutableName()))
540 RegistryKey BackupRegKey
= m_RootKey
->CreateSubKey(PRIVATE_BACKUP_KEY_NAME
+ L
"\\" + HTML_EDIT
);
541 BackupRegKey
->CopyValue(RegKey
, DEFAULT_VALUE_NAME
, SHELL_EDIT_COMMAND_BACKUP
);
546 m_ContextInformation
.GetOpenOfficeCommandline(RegistrationContextInformation::Open
,
547 RegistrationContextInformation::Writer
));
549 RegKey
->SetValue(RegVal
);
551 SaveRegisteredFor(DEFAULT_SHELL_HTML_EDITOR
);
554 void Registrar::UnregisterAsDefaultShellHtmlEditor() const
556 assert(m_RootKey
.get());
560 RegistryKey RegKey
= m_RootKey
->OpenSubKey(L
".htm");
562 RegistryValue RegVal
= RegistryValue(
563 new RegistryValueImpl(DEFAULT_VALUE_NAME
, L
""));
565 if (RegKey
->HasValue(DEFAULT_VALUE_NAME
))
566 RegVal
= RegKey
->GetValue(DEFAULT_VALUE_NAME
);
568 std::wstring HtmFwdKey
= RegVal
->GetDataAsUniString();
570 if (0 == HtmFwdKey
.length() || !m_RootKey
->HasSubKey(HtmFwdKey
))
573 RegKey
= m_RootKey
->OpenSubKey(HtmFwdKey
+ L
"\\" + SHELL_EDIT_COMMAND
);
575 RegVal
= RegKey
->GetValue(DEFAULT_VALUE_NAME
);
577 std::wstring CmdLine
= RegVal
->GetDataAsUniString();
579 if (std::wstring::npos
!= CmdLine
.find(m_ContextInformation
.GetOpenOfficeExecutableName()))
581 RegistryKey BackupRegKey
= m_RootKey
->CreateSubKey(PRIVATE_BACKUP_KEY_NAME
+ L
"\\" + HTML_EDIT
);
583 if (BackupRegKey
->HasValue(SHELL_EDIT_COMMAND_BACKUP
))
584 RegKey
->CopyValue(BackupRegKey
, SHELL_EDIT_COMMAND_BACKUP
, DEFAULT_VALUE_NAME
);
586 RegKey
->DeleteValue(DEFAULT_VALUE_NAME
);
589 catch(RegistryKeyNotFoundException
&)
593 SaveNotRegisteredFor(DEFAULT_SHELL_HTML_EDITOR
);
596 void Registrar::SaveRegisteredFor(int State
) const
598 assert(m_RootKey
.get());
600 int NewState
= GetRegisterState();
602 SetRegisterState(NewState
);
605 void Registrar::SaveNotRegisteredFor(int State
) const
607 assert(m_RootKey
.get());
609 int NewState
= GetRegisterState();
611 SetRegisterState(NewState
);
614 int Registrar::GetRegisterState() const
618 RegistryKey RegKey
= m_RootKey
->CreateSubKey(PRIVATE_BACKUP_KEY_NAME
);
620 if (RegKey
->HasValue(REGISTRATION_STATE
))
622 RegistryValue RegVal
= RegKey
->GetValue(REGISTRATION_STATE
);
623 if (REG_DWORD
== RegVal
->GetType())
624 State
= RegVal
->GetDataAsInt();
630 void Registrar::SetRegisterState(int NewState
) const
632 RegistryKey RegKey
= m_RootKey
->CreateSubKey(PRIVATE_BACKUP_KEY_NAME
);
633 RegistryValue RegVal
= RegistryValue(new RegistryValueImpl(REGISTRATION_STATE
, NewState
));
634 RegKey
->SetValue(RegVal
);
637 bool Registrar::IsRegisteredFor(int State
) const
639 assert(m_RootKey
.get());
641 RegistryKey RegKey
= m_RootKey
->CreateSubKey(PRIVATE_BACKUP_KEY_NAME
);
645 if (RegKey
->HasValue(REGISTRATION_STATE
))
647 RegistryValue RegVal
= RegKey
->GetValue(REGISTRATION_STATE
);
648 if (REG_DWORD
== RegVal
->GetType())
649 SavedState
= RegVal
->GetDataAsInt();
652 return ((SavedState
& State
) == State
);
655 //--------------------------------------
656 /** Restore the last registration state (necessary for
658 void Registrar::RepairRegistrationState() const
660 assert(m_RootKey
.get());
662 if (IsRegisteredFor(MSWORD
))
665 if (IsRegisteredFor(MSEXCEL
))
666 RegisterForMsExcel();
668 if (IsRegisteredFor(MSPOWERPOINT
))
669 RegisterForMsPowerPoint();
671 if (IsRegisteredFor(DEFAULT_HTML_EDITOR_FOR_IE
))
672 RegisterAsDefaultHtmlEditorForInternetExplorer();
674 if (IsRegisteredFor(HTML_EDITOR
))
675 RegisterAsHtmlEditorForInternetExplorer();
677 if (IsRegisteredFor(DEFAULT_SHELL_HTML_EDITOR
))
678 RegisterAsDefaultShellHtmlEditor();
681 /** Unregisters all and delete all Registry keys we have written */
682 void Registrar::UnregisterAllAndCleanUpRegistry() const
684 assert(m_RootKey
.get());
686 if (IsRegisteredFor(MSWORD
))
687 UnregisterForMsWord();
689 if (IsRegisteredFor(MSEXCEL
))
690 UnregisterForMsExcel();
692 if (IsRegisteredFor(MSPOWERPOINT
))
693 UnregisterForMsPowerPoint();
695 if (IsRegisteredFor(DEFAULT_HTML_EDITOR_FOR_IE
))
696 UnregisterAsDefaultHtmlEditorForInternetExplorer();
698 if (IsRegisteredFor(HTML_EDITOR
))
699 UnregisterAsHtmlEditorForInternetExplorer();
701 if (IsRegisteredFor(DEFAULT_SHELL_HTML_EDITOR
))
702 UnregisterAsDefaultShellHtmlEditor();
704 if (m_RootKey
->HasSubKey(PRIVATE_BACKUP_KEY_NAME
))
705 m_RootKey
->DeleteSubKeyTree(PRIVATE_BACKUP_KEY_NAME
);
708 void Registrar::RegisterForMsOfficeApplication(
709 const std::wstring
& FileExtension
,
710 const std::wstring
& DocumentDisplayName
,
711 const std::wstring
& DefaultIconEntry
,
712 const std::wstring
& DefaultShellCommand
,
713 const std::wstring
& ShellNewCommandDisplayName
,
714 const RegistrationContextInformation::OFFICE_APPLICATION eOfficeApp
) const
716 assert(m_RootKey
.get());
718 std::wstring ForwardKeyName
= FORWARD_KEY_PREFIX
+ FileExtension
;
720 RegistryKey ForwardKey
= m_RootKey
->CreateSubKey(ForwardKeyName
);
721 RegistryValue
RegVal(new RegistryValueImpl(std::wstring(DEFAULT_VALUE_NAME
), DocumentDisplayName
));
722 ForwardKey
->SetValue(RegVal
);
724 RegistryKey RegKey
= ForwardKey
->CreateSubKey(L
"DefaultIcon");
725 RegVal
->SetValue(DefaultIconEntry
);
726 RegKey
->SetValue(RegVal
);
728 RegistryKey RegKeyShell
= ForwardKey
->CreateSubKey(L
"shell");
729 RegVal
->SetValue(DefaultShellCommand
);
730 RegKeyShell
->SetValue(RegVal
);
732 RegKey
= RegKeyShell
->CreateSubKey(L
"new");
733 RegVal
->SetValue(ShellNewCommandDisplayName
);
734 RegKey
->SetValue(RegVal
);
736 RegKey
= RegKey
->CreateSubKey(L
"command");
737 RegVal
->SetValue(m_ContextInformation
.GetOpenOfficeCommandline(RegistrationContextInformation::New
, eOfficeApp
));
738 RegKey
->SetValue(RegVal
);
740 RegKey
= RegKeyShell
->CreateSubKey(L
"open\\command");
741 RegVal
->SetValue(m_ContextInformation
.GetOpenOfficeCommandline(RegistrationContextInformation::Open
, eOfficeApp
));
742 RegKey
->SetValue(RegVal
);
744 RegKey
= RegKeyShell
->CreateSubKey(L
"print\\command");
745 RegVal
->SetValue(m_ContextInformation
.GetOpenOfficeCommandline(RegistrationContextInformation::Print
, eOfficeApp
));
746 RegKey
->SetValue(RegVal
);
748 RegKey
= RegKeyShell
->CreateSubKey(L
"printto\\command");
749 RegVal
->SetValue(m_ContextInformation
.GetOpenOfficeCommandline(RegistrationContextInformation::Printto
, eOfficeApp
));
750 RegKey
->SetValue(RegVal
);
752 // set the new forward key under the appropriate extension
753 RegKey
= m_RootKey
->CreateSubKey(FileExtension
);
755 if (RegKey
->HasValue(DEFAULT_VALUE_NAME
))
757 RegVal
= RegKey
->GetValue(DEFAULT_VALUE_NAME
);
759 if (REG_SZ
== RegVal
->GetType())
761 std::wstring str
= RegVal
->GetDataAsUniString();
762 if (!IsOpenOfficeRegisteredForMsApplication(str
))
763 ForwardKey
->CopyValue(RegKey
, DEFAULT_VALUE_NAME
, BACKUP_VALUE_NAME
);
767 RegVal
->SetValue(ForwardKeyName
);
768 RegKey
->SetValue(RegVal
);
771 void Registrar::UnregisterForMsOfficeApplication(const std::wstring
& FileExtension
) const
773 std::wstring FwdRegKeyName
= FORWARD_KEY_PREFIX
+ FileExtension
;
775 if (m_RootKey
->HasSubKey(FileExtension
))
777 RegistryKey RegKey
= m_RootKey
->OpenSubKey(FileExtension
);
779 if (RegKey
->HasValue(DEFAULT_VALUE_NAME
))
781 RegistryValue RegVal
= RegKey
->GetValue(DEFAULT_VALUE_NAME
);
782 if (REG_SZ
== RegVal
->GetType() &&
783 IsOpenOfficeRegisteredForMsApplication(RegVal
->GetDataAsUniString()))
785 RegistryKey FwdRegKey
= m_RootKey
->CreateSubKey(FwdRegKeyName
);
787 if (FwdRegKey
->HasValue(BACKUP_VALUE_NAME
))
788 RegKey
->CopyValue(FwdRegKey
, BACKUP_VALUE_NAME
, DEFAULT_VALUE_NAME
);
790 RegKey
->DeleteValue(DEFAULT_VALUE_NAME
);
795 if (m_RootKey
->HasSubKey(FwdRegKeyName
))
796 m_RootKey
->DeleteSubKeyTree(FwdRegKeyName
);
799 RegistryKey
Registrar::GetRootKeyForDefHtmlEditorForIERegistration() const
801 return WindowsRegistry().GetLocalMachineKey();
804 bool Registrar::IsOpenOfficeRegisteredForMsApplication(const std::wstring
& DocumentExtensionDefValue
) const
806 return (std::wstring::npos
!= DocumentExtensionDefValue
.find(FORWARD_KEY_PREFIX
));