1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 // UserRegistrar.cpp: Implementierung der Klasse UserRegistrar.
23 //
24 //////////////////////////////////////////////////////////////////////
25 
26 #include "userregistrar.hxx"
27 #include "registryvalueimpl.hxx"
28 #include "windowsregistry.hxx"
29 #include "registryexception.hxx"
30 
31 #ifdef _MSC_VER
32 #pragma warning(disable : 4350)
33 #endif
34 
35 //--------------------------------------
36 /**
37 */
UserRegistrar(const RegistrationContextInformation & RegContext)38 UserRegistrar::UserRegistrar(const RegistrationContextInformation& RegContext) :
39 	Registrar(RegContext)
40 {
41 	RegistryKey RegKey = WindowsRegistry().GetCurrentUserKey();
42 	m_RootKey = RegKey->OpenSubKey(L"Software\\Classes");
43 }
44 
45 //###################################
46 // Command
47 //###################################
48 
49 //--------------------------------------
50 /**
51 */
UnregisterAsHtmlEditorForInternetExplorer() const52 void UserRegistrar::UnregisterAsHtmlEditorForInternetExplorer() const
53 {
54     Registrar::UnregisterAsHtmlEditorForInternetExplorer();
55 
56     DeleteHtmFileAssociationKeys();
57 
58 	try
59 	{
60 		RegistryKey RegKey = m_RootKey->OpenSubKey(L"Applications");
61 		if ((0 == RegKey->GetSubValueCount()) && (0 == RegKey->GetSubKeyCount()))
62 		{
63 			RegKey->Close();
64 			m_RootKey->DeleteSubKey(L"Applications");
65 		}
66 	}
67 	catch(RegistryKeyNotFoundException&)
68 	{
69 	}
70 }
71 
72 //--------------------------------------
73 /**
74 */
RegisterAsDefaultShellHtmlEditor() const75 void UserRegistrar::RegisterAsDefaultShellHtmlEditor() const
76 {
77     RegistryKey LocalHtmKey = m_RootKey->CreateSubKey(L".htm");
78 
79     if (!LocalHtmKey->HasValue(DEFAULT_VALUE_NAME))
80     {
81         RegistryKey HKCRKey = WindowsRegistry().GetClassesRootKey();
82 
83         if (HKCRKey->HasSubKey(L".htm"))
84         {
85             RegistryKey RootHtmKey = HKCRKey->OpenSubKey(L".htm", false);
86 
87             if (RootHtmKey->HasValue(DEFAULT_VALUE_NAME))
88             {
89                 RegistryValue RegVal = RootHtmKey->GetValue(DEFAULT_VALUE_NAME);
90 
91                 std::wstring RootHtmFwdKey = RegVal->GetDataAsUniString();
92 
93                 if (HKCRKey->HasSubKey(RootHtmFwdKey))
94                 {
95                     m_RootKey->CreateSubKey(RootHtmFwdKey);
96                     LocalHtmKey->CopyValue(RootHtmKey, DEFAULT_VALUE_NAME);
97                 }
98             }
99         }
100     }
101 
102     // calling base class method
103     Registrar::RegisterAsDefaultShellHtmlEditor();
104 }
105 
106 //--------------------------------------
107 /**
108 */
UnregisterAsDefaultShellHtmlEditor() const109 void UserRegistrar::UnregisterAsDefaultShellHtmlEditor() const
110 {
111     // calling base class method
112     Registrar::UnregisterAsDefaultShellHtmlEditor();
113     DeleteHtmFileAssociationKeys();
114 }
115 
116 //--------------------------------------
117 /**
118 */
UnregisterForMsOfficeApplication(const std::wstring & FileExtension) const119 void UserRegistrar::UnregisterForMsOfficeApplication(
120         const std::wstring& FileExtension) const
121 {
122     /// calling base class method
123     Registrar::UnregisterForMsOfficeApplication(FileExtension);
124 
125     if (m_RootKey->HasSubKey(FileExtension))
126     {
127         RegistryKey RegKey = m_RootKey->OpenSubKey(FileExtension);
128 
129         if ((0 == RegKey->GetSubKeyCount()) && (0 == RegKey->GetSubValueCount()))
130         {
131             RegKey->Close();
132             m_RootKey->DeleteSubKey(FileExtension);
133         }
134     }
135 }
136 
137 //--------------------------------------
138 /**
139 */
GetRootKeyForDefHtmlEditorForIERegistration() const140 RegistryKey UserRegistrar::GetRootKeyForDefHtmlEditorForIERegistration() const
141 {
142     return WindowsRegistry().GetCurrentUserKey();
143 }
144 
145 //--------------------------------------
146 /**
147 */
DeleteHtmFileAssociationKeys() const148 void UserRegistrar::DeleteHtmFileAssociationKeys() const
149 {
150     // Later delete the created keys if they are empty and have not changed meanwhile.
151     // Remember: if we create a new registry key in the user part of the
152     // registry, changes to that key via the merged key HKEY_CLASSES_ROOT
153     // go into the user branch HKEY_CURRENT_USER and are not visible for other users.
154     // so we must carefully detect if the keys have not changed in order to prevent accidentally
155     // deleting a key and so destroying existing associations
156     // See MSDN: "Merged View of HKEY_CLASSES_ROOT"
157 }
158