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