1 // UserRegistrar.cpp: Implementierung der Klasse UserRegistrar.
2 //
3 //////////////////////////////////////////////////////////////////////
4 
5 #include "userregistrar.hxx"
6 #include "registryvalueimpl.hxx"
7 #include "windowsregistry.hxx"
8 #include "registryexception.hxx"
9 
10 #ifdef _MSC_VER
11 #pragma warning(disable : 4350)
12 #endif
13 
14 //--------------------------------------
15 /**
16 */
17 UserRegistrar::UserRegistrar(const RegistrationContextInformation& RegContext) :
18 	Registrar(RegContext)
19 {
20 	RegistryKey RegKey = WindowsRegistry().GetCurrentUserKey();
21 	m_RootKey = RegKey->OpenSubKey(L"Software\\Classes");
22 }
23 
24 //###################################
25 // Command
26 //###################################
27 
28 //--------------------------------------
29 /**
30 */
31 void UserRegistrar::UnregisterAsHtmlEditorForInternetExplorer() const
32 {
33     Registrar::UnregisterAsHtmlEditorForInternetExplorer();
34 
35     DeleteHtmFileAssociationKeys();
36 
37 	try
38 	{
39 		RegistryKey RegKey = m_RootKey->OpenSubKey(L"Applications");
40 		if ((0 == RegKey->GetSubValueCount()) && (0 == RegKey->GetSubKeyCount()))
41 		{
42 			RegKey->Close();
43 			m_RootKey->DeleteSubKey(L"Applications");
44 		}
45 	}
46 	catch(RegistryKeyNotFoundException&)
47 	{
48 	}
49 }
50 
51 //--------------------------------------
52 /**
53 */
54 void UserRegistrar::RegisterAsDefaultShellHtmlEditor() const
55 {
56     RegistryKey LocalHtmKey = m_RootKey->CreateSubKey(L".htm");
57 
58     if (!LocalHtmKey->HasValue(DEFAULT_VALUE_NAME))
59     {
60         RegistryKey HKCRKey = WindowsRegistry().GetClassesRootKey();
61 
62         if (HKCRKey->HasSubKey(L".htm"))
63         {
64             RegistryKey RootHtmKey = HKCRKey->OpenSubKey(L".htm", false);
65 
66             if (RootHtmKey->HasValue(DEFAULT_VALUE_NAME))
67             {
68                 RegistryValue RegVal = RootHtmKey->GetValue(DEFAULT_VALUE_NAME);
69 
70                 std::wstring RootHtmFwdKey = RegVal->GetDataAsUniString();
71 
72                 if (HKCRKey->HasSubKey(RootHtmFwdKey))
73                 {
74                     m_RootKey->CreateSubKey(RootHtmFwdKey);
75                     LocalHtmKey->CopyValue(RootHtmKey, DEFAULT_VALUE_NAME);
76                 }
77             }
78         }
79     }
80 
81     // calling base class method
82     Registrar::RegisterAsDefaultShellHtmlEditor();
83 }
84 
85 //--------------------------------------
86 /**
87 */
88 void UserRegistrar::UnregisterAsDefaultShellHtmlEditor() const
89 {
90     // calling base class method
91     Registrar::UnregisterAsDefaultShellHtmlEditor();
92     DeleteHtmFileAssociationKeys();
93 }
94 
95 //--------------------------------------
96 /**
97 */
98 void UserRegistrar::UnregisterForMsOfficeApplication(
99         const std::wstring& FileExtension) const
100 {
101     /// calling base class method
102     Registrar::UnregisterForMsOfficeApplication(FileExtension);
103 
104     if (m_RootKey->HasSubKey(FileExtension))
105     {
106         RegistryKey RegKey = m_RootKey->OpenSubKey(FileExtension);
107 
108         if ((0 == RegKey->GetSubKeyCount()) && (0 == RegKey->GetSubValueCount()))
109         {
110             RegKey->Close();
111             m_RootKey->DeleteSubKey(FileExtension);
112         }
113     }
114 }
115 
116 //--------------------------------------
117 /**
118 */
119 RegistryKey UserRegistrar::GetRootKeyForDefHtmlEditorForIERegistration() const
120 {
121     return WindowsRegistry().GetCurrentUserKey();
122 }
123 
124 //--------------------------------------
125 /**
126 */
127 void UserRegistrar::DeleteHtmFileAssociationKeys() const
128 {
129     // Later delete the created keys if they are empty and have not changed meanwhile.
130     // Remeber: if we create a new registry key in the user part of the
131     // registry, changes to that key via the merged key HKEY_CLASSES_ROOT
132     // go into the user branch HKEY_CURRENT_USER and are not visible for other users.
133     // so we must carefully detect if the keys have not changed in order to prevent accidentally
134     // deleting a key and so destroying existing associations
135     // See MSDN: "Merged View of HKEY_CLASSES_ROOT"
136 }
137