1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 // Registrar.cpp: Implementierung der Klasse Registrar.
4 //////////////////////////////////////////////////////////////////////
6 #include "registrar.hxx"
8 #include "RegistryValueImpl.hxx"
9 #include "windowsregistry.hxx"
10 #include "registryexception.hxx"
14 #pragma warning(disable: 4350 4482)
18 //----------------------------------------------------------
20 inline void OutputDebugStringFormat( LPCTSTR pFormat
, ... )
25 va_start( args
, pFormat
);
26 StringCchVPrintf( buffer
, sizeof(buffer
), pFormat
, args
);
27 OutputDebugString( buffer
);
30 static inline void OutputDebugStringFormat( LPCTSTR
, ... )
34 //----------------------------------------------------------
36 const int MSWORD
= 0x1;
37 const int MSEXCEL
= 0x2;
38 const int MSPOWERPOINT
= 0x4;
39 const int DEFAULT_HTML_EDITOR_FOR_IE
= 0x8;
40 const int HTML_EDITOR
= 0x10;
41 const int DEFAULT_SHELL_HTML_EDITOR
= 0x20;
43 namespace /* private */
45 const std::wstring HTM_OPENWITHLIST
= L
".htm\\OpenWithList";
46 const std::wstring APPLICATIONS
= L
"Applications";
47 const std::wstring SHELL_EDIT_COMMAND
= L
"shell\\edit\\command";
48 const std::wstring HTML_EDIT
= L
"HTML Edit";
49 const std::wstring HTML_EDIT_DISPLAY_NAME
= L
"Edit Display Name";
50 const std::wstring SHELL_EDIT_COMMAND_BACKUP
= L
"Shell Edit Cmd";
51 const std::wstring DEFAULT_HTML_EDITOR
= L
"Default HTML Editor";
52 const std::wstring MS_IE_DEF_HTML_EDITOR
= L
"Software\\Microsoft\\Internet Explorer\\Default HTML Editor";
53 const std::wstring MS_IE_DEF_HTML_EDITOR_SHL_EDIT_CMD
= L
"Software\\Microsoft\\Internet Explorer\\Default HTML Editor\\shell\\edit\\command";
56 Registrar::Registrar(const RegistrationContextInformation
& RegContext
) :
57 m_ContextInformation(RegContext
),
58 FORWARD_KEY_PREFIX(L
"LibreOffice"),
59 DEFAULT_VALUE_NAME(L
""),
60 BACKUP_VALUE_NAME(L
"Backup"),
61 PRIVATE_BACKUP_KEY_NAME(L
"LibreOffice.reg4msdocmsi"),
62 REGISTRATION_STATE(L
"Reg4MsDocState")
64 m_RootKey
= WindowsRegistry().GetClassesRootKey();
67 Registrar::~Registrar()
71 void Registrar::RegisterForMsWord() const
73 assert(m_RootKey
.get());
75 RegisterForMsOfficeApplication(
76 m_ContextInformation
.GetWordDocumentFileExtension(),
77 m_ContextInformation
.GetWordDocumentDisplayName(),
78 m_ContextInformation
.GetWordDocumentDefaultIconEntry(),
79 m_ContextInformation
.GetWordDocumentDefaultShellCommand(),
80 m_ContextInformation
.ShellNewCommandDisplayName(),
81 RegistrationContextInformation::Writer
);
83 RegisterForMsOfficeApplication(
84 m_ContextInformation
.GetMOOXWordDocumentFileExtension(),
85 m_ContextInformation
.GetMOOXWordDocumentDisplayName(),
86 m_ContextInformation
.GetWordDocumentDefaultIconEntry(),
87 m_ContextInformation
.GetWordDocumentDefaultShellCommand(),
88 m_ContextInformation
.ShellNewCommandDisplayName(),
89 RegistrationContextInformation::Writer
);
91 RegisterForMsOfficeApplication(
92 m_ContextInformation
.GetWordTemplateFileExtension(),
93 m_ContextInformation
.GetWordTemplateDisplayName(),
94 m_ContextInformation
.GetWordTemplateDefaultIconEntry(),
95 m_ContextInformation
.GetWordTemplateDefaultShellCommand(),
96 m_ContextInformation
.ShellNewCommandDisplayName(),
97 RegistrationContextInformation::Writer
);
99 RegisterForMsOfficeApplication(
100 m_ContextInformation
.GetRtfDocumentFileExtension(),
101 m_ContextInformation
.GetRtfDocumentDisplayName(),
102 m_ContextInformation
.GetRtfDocumentDefaultIconEntry(),
103 m_ContextInformation
.GetRtfDocumentDefaultShellCommand(),
104 m_ContextInformation
.ShellNewCommandDisplayName(),
105 RegistrationContextInformation::Writer
);
107 SaveRegisteredFor(MSWORD
);
110 void Registrar::UnregisterForMsWord() const
112 assert(m_RootKey
.get());
116 UnregisterForMsOfficeApplication(
117 m_ContextInformation
.GetWordDocumentFileExtension());
119 catch(RegistryKeyNotFoundException
&)
124 UnregisterForMsOfficeApplication(
125 m_ContextInformation
.GetMOOXWordDocumentFileExtension());
127 catch(RegistryKeyNotFoundException
&)
132 UnregisterForMsOfficeApplication(
133 m_ContextInformation
.GetWordTemplateFileExtension());
135 catch(RegistryKeyNotFoundException
&)
140 UnregisterForMsOfficeApplication(
141 m_ContextInformation
.GetRtfDocumentFileExtension());
143 catch(RegistryKeyNotFoundException
&)
146 SaveNotRegisteredFor(MSWORD
);
149 bool Registrar::QueryPreselectForMsApplication(const std::wstring
& file_extension
) const
151 bool preselect
= false;
153 // We use HKCR else we would not see that a registration for
154 // MS Office applications already exist if we are about to
155 // register in HKCU\Software\Classes
156 RegistryKey root_key
= WindowsRegistry().GetClassesRootKey();
158 if (!root_key
->HasSubKey(file_extension
))
161 OutputDebugStringFormat( TEXT("QueryPreselect: No SubKey found for (%s), preselected!\n"), file_extension
.c_str() );
165 RegistryKey RegKey
= root_key
->OpenSubKey(file_extension
, false);
167 if (RegKey
->HasValue(DEFAULT_VALUE_NAME
))
169 RegistryValue RegVal
= RegKey
->GetValue(DEFAULT_VALUE_NAME
);
171 if (REG_SZ
== RegVal
->GetType() &&
172 IsOpenOfficeRegisteredForMsApplication(RegVal
->GetDataAsUniString()))
175 OutputDebugStringFormat( TEXT("QueryPreselect: (%s) registered to Office, preselected!\n"), file_extension
.c_str() );
177 else if ( (REG_SZ
== RegVal
->GetType()) && ! root_key
->HasSubKey( RegVal
->GetDataAsUniString() ) )
180 OutputDebugStringFormat( TEXT("QueryPreselect: (%s) registered but destination is empty, preselected!\n"), file_extension
.c_str() );
186 OutputDebugStringFormat( TEXT("QueryPreselect: No default found for SubKey (%s), preselected!\n"), file_extension
.c_str() );
192 bool Registrar::QueryPreselectMsWordRegistration() const
194 return QueryPreselectForMsApplication(
195 m_ContextInformation
.GetWordDocumentFileExtension());
198 void Registrar::RegisterForMsExcel() const
200 assert(m_RootKey
.get());
202 RegisterForMsOfficeApplication(
203 m_ContextInformation
.GetExcelSheetFileExtension(),
204 m_ContextInformation
.GetExcelSheetDisplayName(),
205 m_ContextInformation
.GetExcelSheetDefaultIconEntry(),
206 m_ContextInformation
.GetExcelSheetDefaultShellCommand(),
207 m_ContextInformation
.ShellNewCommandDisplayName(),
208 RegistrationContextInformation::Calc
);
210 RegisterForMsOfficeApplication(
211 m_ContextInformation
.GetMOOXExcelSheetFileExtension(),
212 m_ContextInformation
.GetMOOXExcelSheetDisplayName(),
213 m_ContextInformation
.GetExcelSheetDefaultIconEntry(),
214 m_ContextInformation
.GetExcelSheetDefaultShellCommand(),
215 m_ContextInformation
.ShellNewCommandDisplayName(),
216 RegistrationContextInformation::Calc
);
218 RegisterForMsOfficeApplication(
219 m_ContextInformation
.GetExcelTemplateFileExtension(),
220 m_ContextInformation
.GetExcelTemplateDisplayName(),
221 m_ContextInformation
.GetExcelTemplateDefaultIconEntry(),
222 m_ContextInformation
.GetExcelTemplateDefaultShellCommand(),
223 m_ContextInformation
.ShellNewCommandDisplayName(),
224 RegistrationContextInformation::Calc
);
226 SaveRegisteredFor(MSEXCEL
);
229 void Registrar::UnregisterForMsExcel() const
231 assert(m_RootKey
.get());
235 UnregisterForMsOfficeApplication(
236 m_ContextInformation
.GetExcelSheetFileExtension());
238 catch(RegistryKeyNotFoundException
&)
243 UnregisterForMsOfficeApplication(
244 m_ContextInformation
.GetMOOXExcelSheetFileExtension());
246 catch(RegistryKeyNotFoundException
&)
251 UnregisterForMsOfficeApplication(
252 m_ContextInformation
.GetMOOXPowerPointDocumentFileExtension());
254 catch(RegistryKeyNotFoundException
&)
259 UnregisterForMsOfficeApplication(
260 m_ContextInformation
.GetExcelTemplateFileExtension());
262 catch(RegistryKeyNotFoundException
&)
265 SaveNotRegisteredFor(MSEXCEL
);
268 bool Registrar::QueryPreselectMsExcelRegistration() const
270 return QueryPreselectForMsApplication(
271 m_ContextInformation
.GetExcelSheetFileExtension());
274 void Registrar::RegisterForMsPowerPoint() const
276 assert(m_RootKey
.get());
278 RegisterForMsOfficeApplication(
279 m_ContextInformation
.GetPowerPointDocumentFileExtension(),
280 m_ContextInformation
.GetPowerPointDocumentDisplayName(),
281 m_ContextInformation
.GetPowerPointDocumentDefaultIconEntry(),
282 m_ContextInformation
.GetPowerPointDocumentDefaultShellCommand(),
283 m_ContextInformation
.ShellNewCommandDisplayName(),
284 RegistrationContextInformation::Impress
);
286 RegisterForMsOfficeApplication(
287 m_ContextInformation
.GetMOOXPowerPointDocumentFileExtension(),
288 m_ContextInformation
.GetMOOXPowerPointDocumentDisplayName(),
289 m_ContextInformation
.GetPowerPointDocumentDefaultIconEntry(),
290 m_ContextInformation
.GetPowerPointDocumentDefaultShellCommand(),
291 m_ContextInformation
.ShellNewCommandDisplayName(),
292 RegistrationContextInformation::Impress
);
294 RegisterForMsOfficeApplication(
295 m_ContextInformation
.GetPowerPointShowFileExtension(),
296 m_ContextInformation
.GetPowerPointShowDisplayName(),
297 m_ContextInformation
.GetPowerPointShowDefaultIconEntry(),
298 m_ContextInformation
.GetPowerPointShowDefaultShellCommand(),
299 m_ContextInformation
.ShellNewCommandDisplayName(),
300 RegistrationContextInformation::Impress
);
302 RegisterForMsOfficeApplication(
303 m_ContextInformation
.GetPowerPointTemplateFileExtension(),
304 m_ContextInformation
.GetPowerPointTemplateDisplayName(),
305 m_ContextInformation
.GetPowerPointTemplateDefaultIconEntry(),
306 m_ContextInformation
.GetPowerPointTemplateDefaultShellCommand(),
307 m_ContextInformation
.ShellNewCommandDisplayName(),
308 RegistrationContextInformation::Impress
);
310 SaveRegisteredFor(MSPOWERPOINT
);
313 void Registrar::UnregisterForMsPowerPoint() const
315 assert(m_RootKey
.get());
319 UnregisterForMsOfficeApplication(
320 m_ContextInformation
.GetPowerPointDocumentFileExtension());
322 catch(RegistryKeyNotFoundException
&)
327 UnregisterForMsOfficeApplication(
328 m_ContextInformation
.GetPowerPointShowFileExtension());
330 catch(RegistryKeyNotFoundException
&)
335 UnregisterForMsOfficeApplication(
336 m_ContextInformation
.GetPowerPointTemplateFileExtension());
338 catch(RegistryKeyNotFoundException
&)
341 SaveNotRegisteredFor(MSPOWERPOINT
);
344 //-----------------------------------------
347 bool Registrar::QueryPreselectMsPowerPointRegistration() const
349 return QueryPreselectForMsApplication( m_ContextInformation
.GetPowerPointDocumentFileExtension()) &&
350 QueryPreselectForMsApplication( m_ContextInformation
.GetPowerPointShowFileExtension());
353 //-----------------------------------------
354 /** The documentation says we have to
355 make the following entries to register
356 a html editor for the Internet Explorer
357 HKCR\.htm\OpenWithList\App Friendly Name\shell\edit\command
358 But the reality shows that this works only
359 with Internet Explorer 5.x
360 Internet Explorer 6.0 wants the follwoing
362 HKCR\.htm\OpenWithList\App.exe
363 HKCR\Applications\App.ex\shell\edit\command
365 void Registrar::RegisterAsHtmlEditorForInternetExplorer() const
367 assert(m_RootKey
.get());
369 std::wstring OOFriendlyAppName
= m_ContextInformation
.GetOpenOfficeFriendlyAppName();
371 std::wstring RegKeyName
= HTM_OPENWITHLIST
+ std::wstring(L
"\\") + OOFriendlyAppName
;
372 RegistryKey RegKey
= m_RootKey
->CreateSubKey(RegKeyName
);
374 RegKey
= RegKey
->CreateSubKey(SHELL_EDIT_COMMAND
);
376 RegistryValue
RegVal(
377 new RegistryValueImpl(
379 m_ContextInformation
.GetOpenOfficeCommandline(RegistrationContextInformation::Open
,
380 RegistrationContextInformation::Writer
)));
382 RegKey
->SetValue(RegVal
);
384 RegKeyName
= APPLICATIONS
+ std::wstring(L
"\\") + OOFriendlyAppName
;
385 RegKey
= m_RootKey
->CreateSubKey(RegKeyName
);
387 RegVal
->SetName(L
"FriendlyAppName");
388 RegVal
->SetValue(OOFriendlyAppName
);
389 RegKey
->SetValue(RegVal
);
391 RegKey
= RegKey
->CreateSubKey(SHELL_EDIT_COMMAND
);
392 RegVal
->SetName(DEFAULT_VALUE_NAME
);
394 m_ContextInformation
.GetOpenOfficeCommandline(RegistrationContextInformation::Open
,
395 RegistrationContextInformation::Writer
));
396 RegKey
->SetValue(RegVal
);
398 SaveRegisteredFor(HTML_EDITOR
);
401 void Registrar::UnregisterAsHtmlEditorForInternetExplorer() const
403 assert(m_RootKey
.get());
407 std::wstring OOFriendlyAppName
= m_ContextInformation
.GetOpenOfficeFriendlyAppName();
409 RegistryKey aRegKey
= m_RootKey
->OpenSubKey( APPLICATIONS
);
410 if ( aRegKey
->HasSubKey( OOFriendlyAppName
) )
411 aRegKey
->DeleteSubKeyTree( OOFriendlyAppName
);
413 aRegKey
= m_RootKey
->OpenSubKey( HTM_OPENWITHLIST
);
414 if ( aRegKey
->HasSubKey( OOFriendlyAppName
) )
415 aRegKey
->DeleteSubKeyTree( OOFriendlyAppName
);
417 catch(RegistryKeyNotFoundException
&)
420 SaveNotRegisteredFor(HTML_EDITOR
);
423 void Registrar::RegisterAsDefaultHtmlEditorForInternetExplorer() const
425 assert(m_RootKey
.get());
427 RegistryKey RegistrationRootKey
= GetRootKeyForDefHtmlEditorForIERegistration();
429 RegistryKey RegKey
= RegistrationRootKey
->CreateSubKey(MS_IE_DEF_HTML_EDITOR_SHL_EDIT_CMD
);
431 RegistryValue RegVal
= RegistryValue(new RegistryValueImpl(DEFAULT_VALUE_NAME
, L
""));
433 if (RegKey
->HasValue(DEFAULT_VALUE_NAME
))
435 RegVal
= RegKey
->GetValue(DEFAULT_VALUE_NAME
);
437 std::wstring CmdLine
= RegVal
->GetDataAsUniString();
439 if (std::wstring::npos
== CmdLine
.find(m_ContextInformation
.GetOpenOfficeExecutableName()))
441 RegistryKey BackupRegKey
= m_RootKey
->CreateSubKey(PRIVATE_BACKUP_KEY_NAME
+ L
"\\" + DEFAULT_HTML_EDITOR
);
443 if (RegKey
->HasValue(DEFAULT_VALUE_NAME
))
444 BackupRegKey
->CopyValue(RegKey
, DEFAULT_VALUE_NAME
);
446 RegKey
= RegistrationRootKey
->OpenSubKey(MS_IE_DEF_HTML_EDITOR
);
447 if (RegKey
->HasValue(L
"Description"))
448 BackupRegKey
->CopyValue(RegKey
, L
"Description");
453 m_ContextInformation
.GetOpenOfficeCommandline(RegistrationContextInformation::Open
,
454 RegistrationContextInformation::Writer
));
455 RegKey
= RegistrationRootKey
->OpenSubKey(MS_IE_DEF_HTML_EDITOR_SHL_EDIT_CMD
);
456 RegKey
->SetValue(RegVal
);
458 RegVal
->SetName(L
"Description");
459 RegVal
->SetValue(m_ContextInformation
.GetOpenOfficeFriendlyAppName());
460 RegKey
= RegistrationRootKey
->OpenSubKey(MS_IE_DEF_HTML_EDITOR
);
461 RegKey
->SetValue(RegVal
);
463 SaveRegisteredFor(DEFAULT_HTML_EDITOR_FOR_IE
);
466 void Registrar::UnregisterAsDefaultHtmlEditorForInternetExplorer() const
468 assert(m_RootKey
.get());
470 RegistryKey RegistrationRootKey
= GetRootKeyForDefHtmlEditorForIERegistration();
472 RegistryKey RegKey
= RegistrationRootKey
->OpenSubKey(MS_IE_DEF_HTML_EDITOR_SHL_EDIT_CMD
);
474 if (RegKey
->HasValue(DEFAULT_VALUE_NAME
))
476 RegistryValue RegVal
= RegKey
->GetValue(DEFAULT_VALUE_NAME
);
478 std::wstring CmdLine
= RegVal
->GetDataAsUniString();
480 if (std::wstring::npos
!= CmdLine
.find(m_ContextInformation
.GetOpenOfficeExecutableName()))
482 RegistryKey BackupRegKey
= m_RootKey
->OpenSubKey(PRIVATE_BACKUP_KEY_NAME
);
484 if (BackupRegKey
->HasSubKey(DEFAULT_HTML_EDITOR
))
486 BackupRegKey
= BackupRegKey
->OpenSubKey(DEFAULT_HTML_EDITOR
);
488 if (BackupRegKey
->HasValue(DEFAULT_VALUE_NAME
))
489 RegKey
->CopyValue(BackupRegKey
, DEFAULT_VALUE_NAME
);
491 RegKey
->DeleteValue(DEFAULT_VALUE_NAME
);
493 RegKey
= RegistrationRootKey
->OpenSubKey(MS_IE_DEF_HTML_EDITOR
);
495 if (BackupRegKey
->HasValue(L
"Description"))
496 RegKey
->CopyValue(BackupRegKey
, L
"Description");
498 RegKey
->DeleteValue(L
"Description");
502 RegKey
->DeleteValue(DEFAULT_VALUE_NAME
);
503 RegKey
= RegistrationRootKey
->OpenSubKey(MS_IE_DEF_HTML_EDITOR
);
504 RegKey
->DeleteValue(L
"Description");
509 SaveNotRegisteredFor(DEFAULT_HTML_EDITOR_FOR_IE
);
512 void Registrar::RegisterAsDefaultShellHtmlEditor() const
514 assert(m_RootKey
.get());
516 RegistryKey RegKey
= m_RootKey
->CreateSubKey(L
".htm");
518 RegistryValue RegVal
= RegistryValue(
519 new RegistryValueImpl(DEFAULT_VALUE_NAME
, L
""));
521 if (RegKey
->HasValue(DEFAULT_VALUE_NAME
))
522 RegVal
= RegKey
->GetValue(DEFAULT_VALUE_NAME
);
524 std::wstring HtmFwdKey
= RegVal
->GetDataAsUniString();
525 if (0 == HtmFwdKey
.length() || !m_RootKey
->HasSubKey(HtmFwdKey
))
528 RegKey
= m_RootKey
->CreateSubKey(HtmFwdKey
+ L
"\\" + SHELL_EDIT_COMMAND
);
530 if (RegKey
->HasValue(DEFAULT_VALUE_NAME
))
532 RegVal
= RegKey
->GetValue(DEFAULT_VALUE_NAME
);
534 std::wstring CmdLine
= RegVal
->GetDataAsUniString();
536 // backup old values if we are not in place
537 if (std::wstring::npos
== CmdLine
.find(m_ContextInformation
.GetOpenOfficeExecutableName()))
539 RegistryKey BackupRegKey
= m_RootKey
->CreateSubKey(PRIVATE_BACKUP_KEY_NAME
+ L
"\\" + HTML_EDIT
);
540 BackupRegKey
->CopyValue(RegKey
, DEFAULT_VALUE_NAME
, SHELL_EDIT_COMMAND_BACKUP
);
545 m_ContextInformation
.GetOpenOfficeCommandline(RegistrationContextInformation::Open
,
546 RegistrationContextInformation::Writer
));
548 RegKey
->SetValue(RegVal
);
550 SaveRegisteredFor(DEFAULT_SHELL_HTML_EDITOR
);
553 void Registrar::UnregisterAsDefaultShellHtmlEditor() const
555 assert(m_RootKey
.get());
559 RegistryKey RegKey
= m_RootKey
->OpenSubKey(L
".htm");
561 RegistryValue RegVal
= RegistryValue(
562 new RegistryValueImpl(DEFAULT_VALUE_NAME
, L
""));
564 if (RegKey
->HasValue(DEFAULT_VALUE_NAME
))
565 RegVal
= RegKey
->GetValue(DEFAULT_VALUE_NAME
);
567 std::wstring HtmFwdKey
= RegVal
->GetDataAsUniString();
569 if (0 == HtmFwdKey
.length() || !m_RootKey
->HasSubKey(HtmFwdKey
))
572 RegKey
= m_RootKey
->OpenSubKey(HtmFwdKey
+ L
"\\" + SHELL_EDIT_COMMAND
);
574 RegVal
= RegKey
->GetValue(DEFAULT_VALUE_NAME
);
576 std::wstring CmdLine
= RegVal
->GetDataAsUniString();
578 if (std::wstring::npos
!= CmdLine
.find(m_ContextInformation
.GetOpenOfficeExecutableName()))
580 RegistryKey BackupRegKey
= m_RootKey
->CreateSubKey(PRIVATE_BACKUP_KEY_NAME
+ L
"\\" + HTML_EDIT
);
582 if (BackupRegKey
->HasValue(SHELL_EDIT_COMMAND_BACKUP
))
583 RegKey
->CopyValue(BackupRegKey
, SHELL_EDIT_COMMAND_BACKUP
, DEFAULT_VALUE_NAME
);
585 RegKey
->DeleteValue(DEFAULT_VALUE_NAME
);
588 catch(RegistryKeyNotFoundException
&)
592 SaveNotRegisteredFor(DEFAULT_SHELL_HTML_EDITOR
);
595 void Registrar::SaveRegisteredFor(int State
) const
597 assert(m_RootKey
.get());
599 int NewState
= GetRegisterState();
601 SetRegisterState(NewState
);
604 void Registrar::SaveNotRegisteredFor(int State
) const
606 assert(m_RootKey
.get());
608 int NewState
= GetRegisterState();
610 SetRegisterState(NewState
);
613 int Registrar::GetRegisterState() const
617 RegistryKey RegKey
= m_RootKey
->CreateSubKey(PRIVATE_BACKUP_KEY_NAME
);
619 if (RegKey
->HasValue(REGISTRATION_STATE
))
621 RegistryValue RegVal
= RegKey
->GetValue(REGISTRATION_STATE
);
622 if (REG_DWORD
== RegVal
->GetType())
623 State
= RegVal
->GetDataAsInt();
629 void Registrar::SetRegisterState(int NewState
) const
631 RegistryKey RegKey
= m_RootKey
->CreateSubKey(PRIVATE_BACKUP_KEY_NAME
);
632 RegistryValue RegVal
= RegistryValue(new RegistryValueImpl(REGISTRATION_STATE
, NewState
));
633 RegKey
->SetValue(RegVal
);
636 bool Registrar::IsRegisteredFor(int State
) const
638 assert(m_RootKey
.get());
640 RegistryKey RegKey
= m_RootKey
->CreateSubKey(PRIVATE_BACKUP_KEY_NAME
);
644 if (RegKey
->HasValue(REGISTRATION_STATE
))
646 RegistryValue RegVal
= RegKey
->GetValue(REGISTRATION_STATE
);
647 if (REG_DWORD
== RegVal
->GetType())
648 SavedState
= RegVal
->GetDataAsInt();
651 return ((SavedState
& State
) == State
);
654 //--------------------------------------
655 /** Restore the last registration state (necessary for
657 void Registrar::RepairRegistrationState() const
659 assert(m_RootKey
.get());
661 if (IsRegisteredFor(MSWORD
))
664 if (IsRegisteredFor(MSEXCEL
))
665 RegisterForMsExcel();
667 if (IsRegisteredFor(MSPOWERPOINT
))
668 RegisterForMsPowerPoint();
670 if (IsRegisteredFor(DEFAULT_HTML_EDITOR_FOR_IE
))
671 RegisterAsDefaultHtmlEditorForInternetExplorer();
673 if (IsRegisteredFor(HTML_EDITOR
))
674 RegisterAsHtmlEditorForInternetExplorer();
676 if (IsRegisteredFor(DEFAULT_SHELL_HTML_EDITOR
))
677 RegisterAsDefaultShellHtmlEditor();
680 /** Unregisters all and delete all Registry keys we have written */
681 void Registrar::UnregisterAllAndCleanUpRegistry() const
683 assert(m_RootKey
.get());
685 if (IsRegisteredFor(MSWORD
))
686 UnregisterForMsWord();
688 if (IsRegisteredFor(MSEXCEL
))
689 UnregisterForMsExcel();
691 if (IsRegisteredFor(MSPOWERPOINT
))
692 UnregisterForMsPowerPoint();
694 if (IsRegisteredFor(DEFAULT_HTML_EDITOR_FOR_IE
))
695 UnregisterAsDefaultHtmlEditorForInternetExplorer();
697 if (IsRegisteredFor(HTML_EDITOR
))
698 UnregisterAsHtmlEditorForInternetExplorer();
700 if (IsRegisteredFor(DEFAULT_SHELL_HTML_EDITOR
))
701 UnregisterAsDefaultShellHtmlEditor();
703 if (m_RootKey
->HasSubKey(PRIVATE_BACKUP_KEY_NAME
))
704 m_RootKey
->DeleteSubKeyTree(PRIVATE_BACKUP_KEY_NAME
);
707 void Registrar::RegisterForMsOfficeApplication(
708 const std::wstring
& FileExtension
,
709 const std::wstring
& DocumentDisplayName
,
710 const std::wstring
& DefaultIconEntry
,
711 const std::wstring
& DefaultShellCommand
,
712 const std::wstring
& ShellNewCommandDisplayName
,
713 const RegistrationContextInformation::OFFICE_APPLICATION eOfficeApp
) const
715 assert(m_RootKey
.get());
717 std::wstring ForwardKeyName
= FORWARD_KEY_PREFIX
+ FileExtension
;
719 RegistryKey ForwardKey
= m_RootKey
->CreateSubKey(ForwardKeyName
);
720 RegistryValue
RegVal(new RegistryValueImpl(std::wstring(DEFAULT_VALUE_NAME
), DocumentDisplayName
));
721 ForwardKey
->SetValue(RegVal
);
723 RegistryKey RegKey
= ForwardKey
->CreateSubKey(L
"DefaultIcon");
724 RegVal
->SetValue(DefaultIconEntry
);
725 RegKey
->SetValue(RegVal
);
727 RegistryKey RegKeyShell
= ForwardKey
->CreateSubKey(L
"shell");
728 RegVal
->SetValue(DefaultShellCommand
);
729 RegKeyShell
->SetValue(RegVal
);
731 RegKey
= RegKeyShell
->CreateSubKey(L
"new");
732 RegVal
->SetValue(ShellNewCommandDisplayName
);
733 RegKey
->SetValue(RegVal
);
735 RegKey
= RegKey
->CreateSubKey(L
"command");
736 RegVal
->SetValue(m_ContextInformation
.GetOpenOfficeCommandline(RegistrationContextInformation::New
, eOfficeApp
));
737 RegKey
->SetValue(RegVal
);
739 RegKey
= RegKeyShell
->CreateSubKey(L
"open\\command");
740 RegVal
->SetValue(m_ContextInformation
.GetOpenOfficeCommandline(RegistrationContextInformation::Open
, eOfficeApp
));
741 RegKey
->SetValue(RegVal
);
743 RegKey
= RegKeyShell
->CreateSubKey(L
"print\\command");
744 RegVal
->SetValue(m_ContextInformation
.GetOpenOfficeCommandline(RegistrationContextInformation::Print
, eOfficeApp
));
745 RegKey
->SetValue(RegVal
);
747 RegKey
= RegKeyShell
->CreateSubKey(L
"printto\\command");
748 RegVal
->SetValue(m_ContextInformation
.GetOpenOfficeCommandline(RegistrationContextInformation::Printto
, eOfficeApp
));
749 RegKey
->SetValue(RegVal
);
751 // set the new forward key under the appropriate extension
752 RegKey
= m_RootKey
->CreateSubKey(FileExtension
);
754 if (RegKey
->HasValue(DEFAULT_VALUE_NAME
))
756 RegVal
= RegKey
->GetValue(DEFAULT_VALUE_NAME
);
758 if (REG_SZ
== RegVal
->GetType())
760 std::wstring str
= RegVal
->GetDataAsUniString();
761 if (!IsOpenOfficeRegisteredForMsApplication(str
))
762 ForwardKey
->CopyValue(RegKey
, DEFAULT_VALUE_NAME
, BACKUP_VALUE_NAME
);
766 RegVal
->SetValue(ForwardKeyName
);
767 RegKey
->SetValue(RegVal
);
770 void Registrar::UnregisterForMsOfficeApplication(const std::wstring
& FileExtension
) const
772 std::wstring FwdRegKeyName
= FORWARD_KEY_PREFIX
+ FileExtension
;
774 if (m_RootKey
->HasSubKey(FileExtension
))
776 RegistryKey RegKey
= m_RootKey
->OpenSubKey(FileExtension
);
778 if (RegKey
->HasValue(DEFAULT_VALUE_NAME
))
780 RegistryValue RegVal
= RegKey
->GetValue(DEFAULT_VALUE_NAME
);
781 if (REG_SZ
== RegVal
->GetType() &&
782 IsOpenOfficeRegisteredForMsApplication(RegVal
->GetDataAsUniString()))
784 RegistryKey FwdRegKey
= m_RootKey
->CreateSubKey(FwdRegKeyName
);
786 if (FwdRegKey
->HasValue(BACKUP_VALUE_NAME
))
787 RegKey
->CopyValue(FwdRegKey
, BACKUP_VALUE_NAME
, DEFAULT_VALUE_NAME
);
789 RegKey
->DeleteValue(DEFAULT_VALUE_NAME
);
794 if (m_RootKey
->HasSubKey(FwdRegKeyName
))
795 m_RootKey
->DeleteSubKeyTree(FwdRegKeyName
);
798 RegistryKey
Registrar::GetRootKeyForDefHtmlEditorForIERegistration() const
800 return WindowsRegistry().GetLocalMachineKey();
803 bool Registrar::IsOpenOfficeRegisteredForMsApplication(const std::wstring
& DocumentExtensionDefValue
) const
805 return (std::wstring::npos
!= DocumentExtensionDefValue
.find(FORWARD_KEY_PREFIX
));
808 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */