fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / framework / source / fwi / classes / protocolhandlercache.cxx
blob9d71c8c38428477b17d029fd74c00417e1594770
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 /*TODO
21 - change "singleton" behaviour by using new helper ::comhelper::SingletonRef
22 - rename method exist() to existHandlerForURL() or similar one
23 - may it's a good idea to replace struct ProtocolHandler by css::beans::NamedValue type?!
26 #include <classes/protocolhandlercache.hxx>
27 #include <classes/converter.hxx>
29 #include <tools/wldcrd.hxx>
30 #include <unotools/configpaths.hxx>
31 #include <rtl/ustrbuf.hxx>
32 #include <vcl/svapp.hxx>
34 #define SETNAME_HANDLER "HandlerSet" // name of configuration set inside package
36 namespace framework{
38 /**
39 @short overloaded index operator of hash map to support pattern key search
40 @descr All keys inside this hash map are URL pattern which points to an uno
41 implementation name of a protocol handler service which is registered
42 for this pattern. This operator makes it easy to find such registered
43 handler by using a full qualified URL and compare it with all pattern
44 keys.
46 @param sURL
47 the full qualified URL which should match to a registered pattern
49 @return An iterator which points to the found item inside the hash or PatternHash::end()
50 if no pattern match this given <var>sURL</var>.
52 PatternHash::iterator PatternHash::findPatternKey( const OUString& sURL )
54 PatternHash::iterator pItem = this->begin();
55 while( pItem!=this->end() )
57 WildCard aPattern(pItem->first);
58 if (aPattern.Matches(sURL))
59 break;
60 ++pItem;
62 return pItem;
65 /**
66 @short initialize static member of class HandlerCache
67 @descr We use a singleton pattern to implement this handler cache.
68 That means it use two static member list to hold all necessary information
69 and a ref count mechanism to create/destroy it on demand.
71 HandlerHash* HandlerCache::m_pHandler = NULL;
72 PatternHash* HandlerCache::m_pPattern = NULL;
73 sal_Int32 HandlerCache::m_nRefCount = 0;
74 HandlerCFGAccess* HandlerCache::m_pConfig = NULL;
76 /**
77 @short ctor of the cache of all registered protoco handler
78 @descr It tries to open the right configuration package automatically
79 and fill the internal structures. After that the cache can be
80 used for read access on this data and perform some search
81 operations on it.
83 HandlerCache::HandlerCache()
85 SolarMutexGuard aGuard;
87 if (m_nRefCount==0)
89 m_pHandler = new HandlerHash();
90 m_pPattern = new PatternHash();
91 m_pConfig = new HandlerCFGAccess(PACKAGENAME_PROTOCOLHANDLER);
92 m_pConfig->read(&m_pHandler,&m_pPattern);
93 m_pConfig->setCache(this);
96 ++m_nRefCount;
99 /**
100 @short dtor of the cache
101 @descr It frees all used memory. In further implementations (may if we support write access too)
102 it's a good place to flush changes back to the configuration - but not needed yet.
104 HandlerCache::~HandlerCache()
106 SolarMutexGuard aGuard;
108 if( m_nRefCount==1)
110 m_pConfig->setCache(NULL);
111 m_pHandler->free();
112 m_pPattern->free();
114 delete m_pConfig;
115 delete m_pHandler;
116 delete m_pPattern;
117 m_pConfig = NULL;
118 m_pHandler= NULL;
119 m_pPattern= NULL;
122 --m_nRefCount;
126 @short dtor of the cache
127 @descr It frees all used memory. In further implementations (may if we support write access too)
128 it's a good place to flush changes back to the configuration - but not needed yet.
130 bool HandlerCache::search( const OUString& sURL, ProtocolHandler* pReturn ) const
132 bool bFound = false;
134 SolarMutexGuard aGuard;
136 PatternHash::const_iterator pItem = m_pPattern->findPatternKey(sURL);
137 if (pItem!=m_pPattern->end())
139 *pReturn = (*m_pHandler)[pItem->second];
140 bFound = true;
143 return bFound;
147 @short search for a registered handler by using an URL struct
148 @descr We combine necessary parts of this struct to a valid URL string
149 and call our other search method ...
150 It's a helper for outside code.
152 bool HandlerCache::search( const css::util::URL& aURL, ProtocolHandler* pReturn ) const
154 return search( aURL.Complete, pReturn );
157 void HandlerCache::takeOver(HandlerHash* pHandler, PatternHash* pPattern)
159 SolarMutexGuard aGuard;
161 HandlerHash* pOldHandler = m_pHandler;
162 PatternHash* pOldPattern = m_pPattern;
164 m_pHandler = pHandler;
165 m_pPattern = pPattern;
167 pOldHandler->free();
168 pOldPattern->free();
169 delete pOldHandler;
170 delete pOldPattern;
174 @short dtor of the config access class
175 @descr It opens the configuration package automatically by using base class mechanism.
176 After that "read()" method of this class should be called to use it.
178 @param sPackage
179 specifies the package name of the configuration data which should be used
181 HandlerCFGAccess::HandlerCFGAccess( const OUString& sPackage )
182 : ConfigItem(sPackage)
183 , m_pCache(0)
185 css::uno::Sequence< OUString > lListenPaths(1);
186 lListenPaths[0] = SETNAME_HANDLER;
187 EnableNotification(lListenPaths);
191 @short use base class mechanism to fill given structures
192 @descr User use us as a wrapper between configuration api and his internal structures.
193 He give us some pointer to his member and we fill it.
195 @param pHandler
196 pointer to a list of protocol handler infos
198 @param pPattern
199 reverse map of handler pattern to her uno names
201 void HandlerCFGAccess::read( HandlerHash** ppHandler ,
202 PatternHash** ppPattern )
204 // list of all uno implementation names without encoding
205 css::uno::Sequence< OUString > lNames = GetNodeNames( SETNAME_HANDLER, ::utl::CONFIG_NAME_LOCAL_PATH );
206 sal_Int32 nSourceCount = lNames.getLength();
207 sal_Int32 nTargetCount = nSourceCount;
208 // list of all full qualified path names of configuration entries
209 css::uno::Sequence< OUString > lFullNames ( nTargetCount );
211 // expand names to full path names
212 sal_Int32 nSource=0;
213 sal_Int32 nTarget=0;
214 for( nSource=0; nSource<nSourceCount; ++nSource )
216 OUStringBuffer sPath( SETNAME_HANDLER );
217 sPath.append(CFG_PATH_SEPARATOR);
218 sPath.append(lNames[nSource]);
219 sPath.append(CFG_PATH_SEPARATOR);
220 sPath.append(PROPERTY_PROTOCOLS);
222 lFullNames[nTarget] = sPath.makeStringAndClear();
223 ++nTarget;
226 // get values at all
227 css::uno::Sequence< css::uno::Any > lValues = GetProperties( lFullNames );
228 SAL_WARN_IF( lFullNames.getLength()!=lValues.getLength(), "fwk", "HandlerCFGAccess::read(): Miss some configuration values of handler set!" );
230 // fill structures
231 nSource = 0;
232 for( nTarget=0; nTarget<nTargetCount; ++nTarget )
234 // create it new for every loop to guarantee a real empty object!
235 ProtocolHandler aHandler;
236 aHandler.m_sUNOName = ::utl::extractFirstFromConfigurationPath(lNames[nSource]);
238 // unpack all values of this handler
239 css::uno::Sequence< OUString > lTemp;
240 lValues[nTarget] >>= lTemp;
241 aHandler.m_lProtocols = Converter::convert_seqOUString2OUStringList(lTemp);
243 // register his pattern into the performance search hash
244 for (OUStringList::iterator pItem =aHandler.m_lProtocols.begin();
245 pItem!=aHandler.m_lProtocols.end();
246 ++pItem )
248 (**ppPattern)[*pItem] = lNames[nSource];
251 // insert the handler info into the normal handler cache
252 (**ppHandler)[lNames[nSource]] = aHandler;
253 ++nSource;
257 void HandlerCFGAccess::Notify(const css::uno::Sequence< OUString >& /*lPropertyNames*/)
259 HandlerHash* pHandler = new HandlerHash;
260 PatternHash* pPattern = new PatternHash;
262 read(&pHandler, &pPattern);
263 if (m_pCache)
264 m_pCache->takeOver(pHandler, pPattern);
265 else
267 delete pHandler;
268 delete pPattern;
272 void HandlerCFGAccess::ImplCommit()
276 } // namespace framework
278 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */