LibreOffice Module stoc (master) 1
defaultregistry.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <osl/mutex.hxx>
22#include <cppuhelper/weak.hxx>
26#include <rtl/ref.hxx>
27
28#include <com/sun/star/registry/XSimpleRegistry.hpp>
29#include <com/sun/star/lang/XServiceInfo.hpp>
30#include <com/sun/star/lang/XInitialization.hpp>
31#include <com/sun/star/container/XEnumerationAccess.hpp>
32
33namespace com::sun::star::uno { class XComponentContext; }
34
35using namespace css::uno;
36using namespace css::registry;
37using namespace css::lang;
38using namespace css::container;
39using namespace cppu;
40using namespace osl;
41
42namespace {
43
44class NestedRegistryImpl : public WeakAggImplHelper4 < XSimpleRegistry, XInitialization, XServiceInfo, XEnumerationAccess >
45{
46public:
47 NestedRegistryImpl( );
48
49 // XServiceInfo
50 virtual OUString SAL_CALL getImplementationName( ) override;
51 virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override;
52 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames( ) override;
53
54 // XInitialization
55 virtual void SAL_CALL initialize( const Sequence< Any >& aArguments ) override;
56
57 // XSimpleRegistry
58 virtual OUString SAL_CALL getURL() override;
59 virtual void SAL_CALL open( const OUString& rURL, sal_Bool bReadOnly, sal_Bool bCreate ) override;
60 virtual sal_Bool SAL_CALL isValid( ) override;
61 virtual void SAL_CALL close( ) override;
62 virtual void SAL_CALL destroy( ) override;
63 virtual Reference< XRegistryKey > SAL_CALL getRootKey( ) override;
64 virtual sal_Bool SAL_CALL isReadOnly( ) override;
65 virtual void SAL_CALL mergeKey( const OUString& aKeyName, const OUString& aUrl ) override;
66
67 // XEnumerationAccess
68 virtual Reference< XEnumeration > SAL_CALL createEnumeration( ) override;
69 virtual Type SAL_CALL getElementType( ) override;
70 virtual sal_Bool SAL_CALL hasElements( ) override;
71
72 friend class NestedKeyImpl;
73protected:
74 Mutex m_mutex;
75 sal_uInt32 m_state;
76 Reference<XSimpleRegistry> m_localReg;
77 Reference<XSimpleRegistry> m_defaultReg;
78
79};
80
81
82// class NestedKeyImpl the implementation of interface XRegistryKey
83
84class NestedKeyImpl : public WeakImplHelper< XRegistryKey >
85{
86public:
87 NestedKeyImpl( NestedRegistryImpl* pDefaultRegistry,
88 Reference<XRegistryKey>& localKey,
89 Reference<XRegistryKey>& defaultKey);
90
91 NestedKeyImpl( const OUString& aKeyName,
92 NestedKeyImpl* pKey);
93
94 // XRegistryKey
95 virtual OUString SAL_CALL getKeyName() override;
96 virtual sal_Bool SAL_CALL isReadOnly( ) override;
97 virtual sal_Bool SAL_CALL isValid( ) override;
98 virtual RegistryKeyType SAL_CALL getKeyType( const OUString& rKeyName ) override;
99 virtual RegistryValueType SAL_CALL getValueType( ) override;
100 virtual sal_Int32 SAL_CALL getLongValue( ) override;
101 virtual void SAL_CALL setLongValue( sal_Int32 value ) override;
102 virtual Sequence< sal_Int32 > SAL_CALL getLongListValue( ) override;
103 virtual void SAL_CALL setLongListValue( const css::uno::Sequence< sal_Int32 >& seqValue ) override;
104 virtual OUString SAL_CALL getAsciiValue( ) override;
105 virtual void SAL_CALL setAsciiValue( const OUString& value ) override;
106 virtual Sequence< OUString > SAL_CALL getAsciiListValue( ) override;
107 virtual void SAL_CALL setAsciiListValue( const css::uno::Sequence< OUString >& seqValue ) override;
108 virtual OUString SAL_CALL getStringValue( ) override;
109 virtual void SAL_CALL setStringValue( const OUString& value ) override;
110 virtual Sequence< OUString > SAL_CALL getStringListValue( ) override;
111 virtual void SAL_CALL setStringListValue( const css::uno::Sequence< OUString >& seqValue ) override;
112 virtual Sequence< sal_Int8 > SAL_CALL getBinaryValue( ) override;
113 virtual void SAL_CALL setBinaryValue( const css::uno::Sequence< sal_Int8 >& value ) override;
114 virtual Reference< XRegistryKey > SAL_CALL openKey( const OUString& aKeyName ) override;
115 virtual Reference< XRegistryKey > SAL_CALL createKey( const OUString& aKeyName ) override;
116 virtual void SAL_CALL closeKey( ) override;
117 virtual void SAL_CALL deleteKey( const OUString& rKeyName ) override;
118 virtual Sequence< Reference< XRegistryKey > > SAL_CALL openKeys( ) override;
119 virtual Sequence< OUString > SAL_CALL getKeyNames( ) override;
120 virtual sal_Bool SAL_CALL createLink( const OUString& aLinkName, const OUString& aLinkTarget ) override;
121 virtual void SAL_CALL deleteLink( const OUString& rLinkName ) override;
122 virtual OUString SAL_CALL getLinkTarget( const OUString& rLinkName ) override;
123 virtual OUString SAL_CALL getResolvedName( const OUString& aKeyName ) override;
124
125protected:
126 void computeChanges();
127 OUString computeName(const OUString& name);
128
129 OUString m_name;
130 sal_uInt32 m_state;
132 Reference<XRegistryKey> m_localKey;
133 Reference<XRegistryKey> m_defaultKey;
134};
135
136
137NestedKeyImpl::NestedKeyImpl( NestedRegistryImpl* pDefaultRegistry,
138 Reference<XRegistryKey>& localKey,
139 Reference<XRegistryKey>& defaultKey )
140 : m_state(pDefaultRegistry->m_state), m_xRegistry(pDefaultRegistry), m_localKey(localKey), m_defaultKey(defaultKey)
141{
142 if (m_localKey.is())
143 {
144 m_name = m_localKey->getKeyName();
145 }
146 else if (m_defaultKey.is())
147 {
148 m_name = m_defaultKey->getKeyName();
149 }
150}
151
152
153NestedKeyImpl::NestedKeyImpl( const OUString& rKeyName,
154 NestedKeyImpl* pKey)
155 : m_xRegistry(pKey->m_xRegistry)
156{
157 if (pKey->m_localKey.is() && pKey->m_localKey->isValid())
158 {
159 m_localKey = pKey->m_localKey->openKey(rKeyName);
160 }
161 if (pKey->m_defaultKey.is() && pKey->m_defaultKey->isValid())
162 {
163 m_defaultKey = pKey->m_defaultKey->openKey(rKeyName);
164 }
165
166 if (m_localKey.is())
167 {
168 m_name = m_localKey->getKeyName();
169 }
170 else if (m_defaultKey.is())
171 {
172 m_name = m_defaultKey->getKeyName();
173 }
174
175 m_state = m_xRegistry->m_state;
176}
177
178void NestedKeyImpl::computeChanges()
179{
180 Guard< Mutex > aGuard( m_xRegistry->m_mutex );
181 if ( m_state == m_xRegistry->m_state )
182 return;
183
184 Reference<XRegistryKey> rootKey(m_xRegistry->m_localReg->getRootKey());
185
186 Reference<XRegistryKey> tmpKey = rootKey->openKey(m_name);
187
188 if ( tmpKey.is() )
189 {
190 m_localKey = rootKey->openKey(m_name);
191 }
192
193 m_state = m_xRegistry->m_state;
194}
195
196
197// NestedKey_Impl::computeName()
198
199OUString NestedKeyImpl::computeName(const OUString& name)
200{
201 OUString resLocalName, resDefaultName;
202
203 Guard< Mutex > aGuard( m_xRegistry->m_mutex );
204 try
205 {
206 if ( m_localKey.is() && m_localKey->isValid() )
207 {
208 resLocalName = m_localKey->getResolvedName(name);
209 }
210 else if ( m_defaultKey.is() && m_defaultKey->isValid() )
211 {
212 return m_defaultKey->getResolvedName(name);
213 }
214
215 if ( !resLocalName.isEmpty() && m_xRegistry->m_defaultReg->isValid() )
216 {
217 Reference<XRegistryKey> localRoot(m_xRegistry->m_localReg->getRootKey());
218 Reference<XRegistryKey> defaultRoot(m_xRegistry->m_defaultReg->getRootKey());
219
220 resDefaultName = defaultRoot->getResolvedName(resLocalName);
221
222 sal_uInt32 count = 100;
223
224 while (resLocalName != resDefaultName && count > 0)
225 {
226 count--;
227
228 if (resLocalName.isEmpty() || resDefaultName.isEmpty())
229 throw InvalidRegistryException();
230
231 resLocalName = localRoot->getResolvedName(resDefaultName);
232 resDefaultName = defaultRoot->getResolvedName(resLocalName);
233 }
234 }
235 }
236 catch(InvalidRegistryException& )
237 {
238 }
239
240 return resLocalName;
241}
242
243
244OUString SAL_CALL NestedKeyImpl::getKeyName()
245{
246 Guard< Mutex > aGuard( m_xRegistry->m_mutex );
247 return m_name;
248}
249
250
251sal_Bool SAL_CALL NestedKeyImpl::isReadOnly( )
252{
253 Guard< Mutex > aGuard( m_xRegistry->m_mutex );
254 computeChanges();
255
256 if ( !m_localKey.is() || !m_localKey->isValid() )
257 throw InvalidRegistryException();
258
259 return m_localKey->isReadOnly();
260}
261
262
263sal_Bool SAL_CALL NestedKeyImpl::isValid( )
264{
265 Guard< Mutex > aGuard( m_xRegistry->m_mutex );
266 return ((m_localKey.is() && m_localKey->isValid()) ||
267 (m_defaultKey.is() && m_defaultKey->isValid()) );
268}
269
270
271RegistryKeyType SAL_CALL NestedKeyImpl::getKeyType( const OUString& rKeyName )
272{
273 Guard< Mutex > aGuard( m_xRegistry->m_mutex );
274 computeChanges();
275
276 if ( m_localKey.is() && m_localKey->isValid() )
277 {
278 return m_localKey->getKeyType(rKeyName);
279 }
280 else if ( m_defaultKey.is() && m_defaultKey->isValid() )
281 {
282 return m_defaultKey->getKeyType(rKeyName);
283 }
284
285 return RegistryKeyType_KEY;
286}
287
288
289RegistryValueType SAL_CALL NestedKeyImpl::getValueType( )
290{
291 Guard< Mutex > aGuard( m_xRegistry->m_mutex );
292 computeChanges();
293
294 if ( m_localKey.is() && m_localKey->isValid() )
295 {
296 return m_localKey->getValueType();
297 }
298 else if ( m_defaultKey.is() && m_defaultKey->isValid() )
299 {
300 return m_defaultKey->getValueType();
301 }
302
303 return RegistryValueType_NOT_DEFINED;
304}
305
306
307sal_Int32 SAL_CALL NestedKeyImpl::getLongValue( )
308{
309 Guard< Mutex > aGuard( m_xRegistry->m_mutex );
310 computeChanges();
311
312 if ( m_localKey.is() && m_localKey->isValid() )
313 {
314 return m_localKey->getLongValue();
315 }
316 else if ( m_defaultKey.is() && m_defaultKey->isValid() )
317 {
318 return m_defaultKey->getLongValue();
319 }
320 else
321 {
322 throw InvalidRegistryException();
323 }
324}
325
326
327void SAL_CALL NestedKeyImpl::setLongValue( sal_Int32 value )
328{
329 Guard< Mutex > aGuard( m_xRegistry->m_mutex );
330 computeChanges();
331
332 if ( m_localKey.is() && m_localKey->isValid() )
333 {
334 m_localKey->setLongValue(value);
335 }
336 else if ( m_defaultKey.is() && m_defaultKey->isValid() )
337 {
338 Reference<XRegistryKey> rootKey(m_xRegistry->m_localReg->getRootKey());
339 m_localKey = rootKey->createKey(m_name);
340 m_localKey->setLongValue(value);
341 m_state = m_xRegistry->m_state++;
342 }
343 else
344 {
345 throw InvalidRegistryException();
346 }
347}
348
349
350Sequence< sal_Int32 > SAL_CALL NestedKeyImpl::getLongListValue( )
351{
352 Guard< Mutex > aGuard( m_xRegistry->m_mutex );
353 computeChanges();
354
355 if ( m_localKey.is() && m_localKey->isValid() )
356 {
357 return m_localKey->getLongListValue();
358 }
359 else if ( m_defaultKey.is() && m_defaultKey->isValid() )
360 {
361 return m_defaultKey->getLongListValue();
362 }
363 else
364 {
365 throw InvalidRegistryException();
366 }
367}
368
369
370void SAL_CALL NestedKeyImpl::setLongListValue( const Sequence< sal_Int32 >& seqValue )
371{
372 Guard< Mutex > aGuard( m_xRegistry->m_mutex );
373 computeChanges();
374
375 if ( m_localKey.is() && m_localKey->isValid() )
376 {
377 m_localKey->setLongListValue(seqValue);
378 }
379 else if ( m_defaultKey.is() && m_defaultKey->isValid() )
380 {
381 Reference<XRegistryKey> rootKey(m_xRegistry->m_localReg->getRootKey());
382 m_localKey = rootKey->createKey(m_name);
383 m_localKey->setLongListValue(seqValue);
384 m_state = m_xRegistry->m_state++;
385 }
386 else
387 {
388 throw InvalidRegistryException();
389 }
390}
391
392
393OUString SAL_CALL NestedKeyImpl::getAsciiValue( )
394{
395 Guard< Mutex > aGuard( m_xRegistry->m_mutex );
396 computeChanges();
397
398 if ( m_localKey.is() && m_localKey->isValid() )
399 {
400 return m_localKey->getAsciiValue();
401 }
402 else if ( m_defaultKey.is() && m_defaultKey->isValid() )
403 {
404 return m_defaultKey->getAsciiValue();
405 }
406 else
407 {
408 throw InvalidRegistryException();
409 }
410}
411
412
413void SAL_CALL NestedKeyImpl::setAsciiValue( const OUString& value )
414{
415 Guard< Mutex > aGuard( m_xRegistry->m_mutex );
416 computeChanges();
417
418 if ( m_localKey.is() && m_localKey->isValid() )
419 {
420 m_localKey->setAsciiValue(value);
421 }
422 else if ( m_defaultKey.is() && m_defaultKey->isValid() )
423 {
424 Reference<XRegistryKey> rootKey(m_xRegistry->m_localReg->getRootKey());
425 m_localKey = rootKey->createKey(m_name);
426 m_localKey->setAsciiValue(value);
427 m_state = m_xRegistry->m_state++;
428 }
429 else
430 {
431 throw InvalidRegistryException();
432 }
433}
434
435
436Sequence< OUString > SAL_CALL NestedKeyImpl::getAsciiListValue( )
437{
438 Guard< Mutex > aGuard( m_xRegistry->m_mutex );
439 computeChanges();
440
441 if ( m_localKey.is() && m_localKey->isValid() )
442 {
443 return m_localKey->getAsciiListValue();
444 }
445 else if ( m_defaultKey.is() && m_defaultKey->isValid() )
446 {
447 return m_defaultKey->getAsciiListValue();
448 }
449 else
450 {
451 throw InvalidRegistryException();
452 }
453}
454
455
456void SAL_CALL NestedKeyImpl::setAsciiListValue( const Sequence< OUString >& seqValue )
457{
458 Guard< Mutex > aGuard( m_xRegistry->m_mutex );
459 computeChanges();
460
461 if ( m_localKey.is() && m_localKey->isValid() )
462 {
463 m_localKey->setAsciiListValue(seqValue);
464 }
465 else if ( m_defaultKey.is() && m_defaultKey->isValid() )
466 {
467 Reference<XRegistryKey> rootKey(m_xRegistry->m_localReg->getRootKey());
468 m_localKey = rootKey->createKey(m_name);
469 m_localKey->setAsciiListValue(seqValue);
470 m_state = m_xRegistry->m_state++;
471 }
472 else
473 {
474 throw InvalidRegistryException();
475 }
476}
477
478
479OUString SAL_CALL NestedKeyImpl::getStringValue( )
480{
481 Guard< Mutex > aGuard( m_xRegistry->m_mutex );
482 computeChanges();
483
484 if ( m_localKey.is() && m_localKey->isValid() )
485 {
486 return m_localKey->getStringValue();
487 }
488 else if ( m_defaultKey.is() && m_defaultKey->isValid() )
489 {
490 return m_defaultKey->getStringValue();
491 }
492 else
493 {
494 throw InvalidRegistryException();
495 }
496}
497
498
499void SAL_CALL NestedKeyImpl::setStringValue( const OUString& value )
500{
501 Guard< Mutex > aGuard( m_xRegistry->m_mutex );
502 computeChanges();
503
504 if ( m_localKey.is() && m_localKey->isValid() )
505 {
506 m_localKey->setStringValue(value);
507 }
508 else if ( m_defaultKey.is() && m_defaultKey->isValid() )
509 {
510 Reference<XRegistryKey> rootKey(m_xRegistry->m_localReg->getRootKey());
511 m_localKey = rootKey->createKey(m_name);
512 m_localKey->setStringValue(value);
513 m_state = m_xRegistry->m_state++;
514 }
515 else
516 {
517 throw InvalidRegistryException();
518 }
519}
520
521
522Sequence< OUString > SAL_CALL NestedKeyImpl::getStringListValue( )
523{
524 Guard< Mutex > aGuard( m_xRegistry->m_mutex );
525 computeChanges();
526
527 if ( m_localKey.is() && m_localKey->isValid() )
528 {
529 return m_localKey->getStringListValue();
530 }
531 else if ( m_defaultKey.is() && m_defaultKey->isValid() )
532 {
533 return m_defaultKey->getStringListValue();
534 }
535 else
536 {
537 throw InvalidRegistryException();
538 }
539}
540
541
542void SAL_CALL NestedKeyImpl::setStringListValue( const Sequence< OUString >& seqValue )
543{
544 Guard< Mutex > aGuard( m_xRegistry->m_mutex );
545 computeChanges();
546
547 if ( m_localKey.is() && m_localKey->isValid() )
548 {
549 m_localKey->setStringListValue(seqValue);
550 }
551 else if ( m_defaultKey.is() && m_defaultKey->isValid() )
552 {
553 Reference<XRegistryKey> rootKey(m_xRegistry->m_localReg->getRootKey());
554 m_localKey = rootKey->createKey(m_name);
555 m_localKey->setStringListValue(seqValue);
556 m_state = m_xRegistry->m_state++;
557 }
558 else
559 {
560 throw InvalidRegistryException();
561 }
562}
563
564
565Sequence< sal_Int8 > SAL_CALL NestedKeyImpl::getBinaryValue( )
566{
567 Guard< Mutex > aGuard( m_xRegistry->m_mutex );
568 computeChanges();
569
570 if ( m_localKey.is() && m_localKey->isValid() )
571 {
572 return m_localKey->getBinaryValue();
573 }
574 else if ( m_defaultKey.is() && m_defaultKey->isValid() )
575 {
576 return m_defaultKey->getBinaryValue();
577 }
578 else
579 {
580 throw InvalidRegistryException();
581 }
582}
583
584
585void SAL_CALL NestedKeyImpl::setBinaryValue( const Sequence< sal_Int8 >& value )
586{
587 Guard< Mutex > aGuard( m_xRegistry->m_mutex );
588 computeChanges();
589
590 if ( m_localKey.is() && m_localKey->isValid() )
591 {
592 m_localKey->setBinaryValue(value);
593 }
594 else if ( m_defaultKey.is() && m_defaultKey->isValid() )
595 {
596 Reference<XRegistryKey> rootKey(m_xRegistry->m_localReg->getRootKey());
597 m_localKey = rootKey->createKey(m_name);
598 m_localKey->setBinaryValue(value);
599 m_state = m_xRegistry->m_state++;
600 }
601 else
602 {
603 throw InvalidRegistryException();
604 }
605}
606
607
608Reference< XRegistryKey > SAL_CALL NestedKeyImpl::openKey( const OUString& aKeyName )
609{
610 Guard< Mutex > aGuard( m_xRegistry->m_mutex );
611 if ( !m_localKey.is() && !m_defaultKey.is() )
612 {
613 throw InvalidRegistryException();
614 }
615
616 OUString resolvedName = computeName(aKeyName);
617
618 if ( resolvedName.isEmpty() )
619 throw InvalidRegistryException();
620
621 Reference<XRegistryKey> localKey, defaultKey;
622
623 if ( m_localKey.is() && m_localKey->isValid() )
624 {
625 localKey = m_xRegistry->m_localReg->getRootKey()->openKey(resolvedName);
626 }
627 if ( m_defaultKey.is() && m_defaultKey->isValid() )
628 {
629 defaultKey = m_xRegistry->m_defaultReg->getRootKey()->openKey(resolvedName);
630 }
631
632 if ( localKey.is() || defaultKey.is() )
633 {
634 return new NestedKeyImpl(m_xRegistry.get(), localKey, defaultKey);
635 }
636 else
637 {
638 return Reference<XRegistryKey>();
639 }
640}
641
642
643Reference< XRegistryKey > SAL_CALL NestedKeyImpl::createKey( const OUString& aKeyName )
644{
645 Guard< Mutex > aGuard( m_xRegistry->m_mutex );
646 if ( (!m_localKey.is() && !m_defaultKey.is()) ||
647 (m_localKey.is() && m_localKey->isReadOnly()) )
648 {
649 throw InvalidRegistryException();
650 }
651
652 OUString resolvedName = computeName(aKeyName);
653
654 if ( resolvedName.isEmpty() )
655 throw InvalidRegistryException();
656
657 if ( m_localKey.is() && m_localKey->isValid() )
658 {
659 Reference<XRegistryKey> localKey, defaultKey;
660
661 localKey = m_xRegistry->m_localReg->getRootKey()->createKey(resolvedName);
662 if ( localKey.is() )
663 {
664 if ( m_defaultKey.is() && m_defaultKey->isValid() )
665 {
666 defaultKey = m_xRegistry->m_defaultReg->getRootKey()->openKey(resolvedName);
667 }
668
669 m_state = m_xRegistry->m_state++;
670
671 return new NestedKeyImpl(m_xRegistry.get(), localKey, defaultKey);
672 }
673 }
674 else
675 {
676 Reference<XRegistryKey> localKey, defaultKey;
677
678 if ( m_defaultKey.is() && m_defaultKey->isValid() )
679 {
680 Reference<XRegistryKey> rootKey(m_xRegistry->m_localReg->getRootKey());
681 m_localKey = rootKey->createKey(m_name);
682
683 localKey = m_xRegistry->m_localReg->getRootKey()->createKey(resolvedName);
684
685 if ( localKey.is() )
686 {
687 defaultKey = m_xRegistry->m_defaultReg->getRootKey()->openKey(resolvedName);
688
689 m_state = m_xRegistry->m_state++;
690
691 return new NestedKeyImpl(m_xRegistry.get(), localKey, defaultKey);
692 }
693 }
694 }
695
696 return Reference<XRegistryKey>();
697}
698
699
700void SAL_CALL NestedKeyImpl::closeKey( )
701{
702 Guard< Mutex > aGuard( m_xRegistry->m_mutex );
703 if ( m_localKey.is() && m_localKey->isValid() )
704 {
705 m_localKey->closeKey();
706 }
707 if ( m_defaultKey.is() && m_defaultKey->isValid() )
708 {
709 m_defaultKey->closeKey();
710 }
711}
712
713
714void SAL_CALL NestedKeyImpl::deleteKey( const OUString& rKeyName )
715{
716 Guard< Mutex > aGuard( m_xRegistry->m_mutex );
717 if ( !m_localKey.is() || !m_localKey->isValid() ||
718 m_localKey->isReadOnly() )
719 {
720 throw InvalidRegistryException();
721 }
722
723 OUString resolvedName = computeName(rKeyName);
724
725 if ( resolvedName.isEmpty() )
726 {
727 throw InvalidRegistryException();
728 }
729
730 m_xRegistry->m_localReg->getRootKey()->deleteKey(resolvedName);
731}
732
733
734Sequence< Reference< XRegistryKey > > SAL_CALL NestedKeyImpl::openKeys( )
735{
736 Guard< Mutex > aGuard( m_xRegistry->m_mutex );
737 if ( !m_localKey.is() && !m_defaultKey.is() )
738 {
739 throw InvalidRegistryException();
740 }
741
742 Sequence<OUString> localSeq, defaultSeq;
743
744 if ( m_localKey.is() && m_localKey->isValid() )
745 {
746 localSeq = m_localKey->getKeyNames();
747 }
748 if ( m_defaultKey.is() && m_defaultKey->isValid() )
749 {
750 defaultSeq = m_defaultKey->getKeyNames();
751 }
752
753 std::vector< Reference<XRegistryKey> > retVec;
754 retVec.reserve(localSeq.getLength() + defaultSeq.getLength());
755
756 auto lKeyNameToRegKey = [this](const OUString& rName) -> Reference<XRegistryKey> {
757 sal_Int32 lastIndex = rName.lastIndexOf('/');
758 OUString name = rName.copy(lastIndex);
759 return new NestedKeyImpl(name, this);
760 };
761
762 for (const auto& rKeyName : std::as_const(localSeq))
763 retVec.push_back(lKeyNameToRegKey(rKeyName));
764
765 for (const auto& rKeyName : std::as_const(defaultSeq))
766 {
767 if ( comphelper::findValue(localSeq, rKeyName) == -1 )
768 {
769 retVec.push_back(lKeyNameToRegKey(rKeyName));
770 }
771 }
772
773 return comphelper::containerToSequence(retVec);
774}
775
776
777Sequence< OUString > SAL_CALL NestedKeyImpl::getKeyNames( )
778{
779 Guard< Mutex > aGuard( m_xRegistry->m_mutex );
780 if ( !m_localKey.is() && !m_defaultKey.is() )
781 {
782 throw InvalidRegistryException();
783 }
784
785 Sequence<OUString> localSeq, defaultSeq;
786
787 if ( m_localKey.is() && m_localKey->isValid() )
788 {
789 localSeq = m_localKey->getKeyNames();
790 }
791 if ( m_defaultKey.is() && m_defaultKey->isValid() )
792 {
793 defaultSeq = m_defaultKey->getKeyNames();
794 }
795
796 return comphelper::combineSequences(localSeq, defaultSeq);
797}
798
799
800sal_Bool SAL_CALL NestedKeyImpl::createLink( const OUString& aLinkName, const OUString& aLinkTarget )
801{
802 Guard< Mutex > aGuard( m_xRegistry->m_mutex );
803
804 bool isCreated = false;
805 if ( !m_localKey.is() && !m_defaultKey.is() )
806 {
807 throw InvalidRegistryException();
808 }
809
810 OUString resolvedName;
811 sal_Int32 lastIndex = aLinkName.lastIndexOf('/');
812
813 if ( lastIndex > 0 )
814 {
815 OUString linkName = aLinkName.copy(0, lastIndex);
816
817 resolvedName = computeName(linkName);
818
819 if ( resolvedName.isEmpty() )
820 {
821 throw InvalidRegistryException();
822 }
823
824 resolvedName += aLinkName.subView(lastIndex);
825 }
826 else
827 {
828 if ( lastIndex == 0 )
829 resolvedName = m_name + aLinkName;
830 else
831 resolvedName = m_name + "/" + aLinkName;
832 }
833
834 if ( m_localKey.is() && m_localKey->isValid() )
835 {
836 isCreated = m_xRegistry->m_localReg->getRootKey()->createLink(resolvedName, aLinkTarget);
837 }
838 else
839 {
840 if ( m_defaultKey.is() && m_defaultKey->isValid() )
841 {
842 Reference<XRegistryKey> rootKey(m_xRegistry->m_localReg->getRootKey());
843 m_localKey = rootKey->createKey(m_name);
844
845 isCreated = m_xRegistry->m_localReg->getRootKey()->createLink(resolvedName, aLinkTarget);
846 }
847 }
848
849 if ( isCreated )
850 m_state = m_xRegistry->m_state++;
851
852 return isCreated;
853}
854
855
856void SAL_CALL NestedKeyImpl::deleteLink( const OUString& rLinkName )
857{
858 Guard< Mutex > aGuard( m_xRegistry->m_mutex );
859 if ( !m_localKey.is() && !m_defaultKey.is() )
860 {
861 throw InvalidRegistryException();
862 }
863
864 OUString resolvedName;
865 sal_Int32 lastIndex = rLinkName.lastIndexOf('/');
866
867 if ( lastIndex > 0 )
868 {
869 OUString linkName = rLinkName.copy(0, lastIndex);
870
871 resolvedName = computeName(linkName);
872
873 if ( resolvedName.isEmpty() )
874 {
875 throw InvalidRegistryException();
876 }
877
878 resolvedName += rLinkName.subView(lastIndex);
879 }
880 else
881 {
882 if ( lastIndex == 0 )
883 resolvedName = m_name + rLinkName;
884 else
885 resolvedName = m_name + "/" + rLinkName;
886 }
887
888 if ( !m_localKey.is() || !m_localKey->isValid() ||
889 m_localKey->isReadOnly() )
890 {
891 throw InvalidRegistryException();
892 }
893
894 m_xRegistry->m_localReg->getRootKey()->deleteLink(resolvedName);
895}
896
897
898OUString SAL_CALL NestedKeyImpl::getLinkTarget( const OUString& rLinkName )
899{
900 Guard< Mutex > aGuard( m_xRegistry->m_mutex );
901 if ( !m_localKey.is() && !m_defaultKey.is() )
902 {
903 throw InvalidRegistryException();
904 }
905
906 OUString resolvedName;
907 sal_Int32 lastIndex = rLinkName.lastIndexOf('/');
908
909 if ( lastIndex > 0 )
910 {
911 OUString linkName = rLinkName.copy(0, lastIndex);
912
913 resolvedName = computeName(linkName);
914
915 if ( resolvedName.isEmpty() )
916 {
917 throw InvalidRegistryException();
918 }
919
920 resolvedName += rLinkName.subView(lastIndex);
921 }
922 else
923 {
924 if ( lastIndex == 0 )
925 resolvedName = m_name + rLinkName;
926 else
927 resolvedName = m_name + "/" + rLinkName;
928 }
929
930 OUString linkTarget;
931 if ( m_localKey.is() && m_localKey->isValid() )
932 {
933 try
934 {
935 linkTarget = m_xRegistry->m_localReg->getRootKey()->getLinkTarget(resolvedName);
936 return linkTarget;
937 }
938 catch(InvalidRegistryException& )
939 {
940 }
941 }
942
943 if ( m_defaultKey.is() && m_defaultKey->isValid() )
944 linkTarget = m_xRegistry->m_defaultReg->getRootKey()->getLinkTarget(resolvedName);
945
946 return linkTarget;
947}
948
949
950OUString SAL_CALL NestedKeyImpl::getResolvedName( const OUString& aKeyName )
951{
952 Guard< Mutex > aGuard( m_xRegistry->m_mutex );
953 if ( !m_localKey.is() && !m_defaultKey.is() )
954 {
955 throw InvalidRegistryException();
956 }
957
958 OUString resolvedName = computeName(aKeyName);
959
960 if ( resolvedName.isEmpty() )
961 {
962 throw InvalidRegistryException();
963 }
964
965 return resolvedName;
966}
967
968
969// DefaultRegistry Implementation
970
971
972NestedRegistryImpl::NestedRegistryImpl( )
973 : m_state(0)
974{}
975
976class RegistryEnumueration : public WeakImplHelper< XEnumeration >
977{
978public:
979 RegistryEnumueration(
980 const Reference< XSimpleRegistry > &r1,
981 const Reference< XSimpleRegistry > &r2 )
982 : m_xReg1( r1 ) , m_xReg2( r2 )
983 {}
984public:
985 virtual sal_Bool SAL_CALL hasMoreElements( ) override;
986 virtual Any SAL_CALL nextElement( ) override;
987
988private:
989 Reference< XSimpleRegistry > m_xReg1;
990 Reference< XSimpleRegistry > m_xReg2;
991};
992
993sal_Bool RegistryEnumueration::hasMoreElements( )
994{
995 return m_xReg1.is() || m_xReg2.is();
996}
997
998Any RegistryEnumueration::nextElement( )
999{
1000 Any a;
1001 if( m_xReg1.is() )
1002 {
1003 a <<= m_xReg1;
1004 m_xReg1.clear();
1005 }
1006 else if( m_xReg2.is() )
1007 {
1008 a <<= m_xReg2;
1009 m_xReg2.clear();
1010 }
1011 else
1012 {
1013 throw NoSuchElementException( "NestedRegistry: no nextElement() !" );
1014 }
1015 return a;
1016}
1017
1018
1019Reference< XEnumeration > NestedRegistryImpl::createEnumeration( )
1020{
1021 MutexGuard guard( m_mutex );
1022 return new RegistryEnumueration( m_localReg, m_defaultReg );
1023}
1024
1025Type NestedRegistryImpl::getElementType( )
1026{
1027 return cppu::UnoType<decltype(m_localReg)>::get();
1028}
1029
1030sal_Bool SAL_CALL NestedRegistryImpl::hasElements( )
1031{
1032 MutexGuard guard( m_mutex );
1033 return m_localReg.is() || m_defaultReg.is();
1034}
1035
1036
1037OUString SAL_CALL NestedRegistryImpl::getImplementationName( )
1038{
1039 return "com.sun.star.comp.stoc.NestedRegistry";
1040}
1041
1042sal_Bool SAL_CALL NestedRegistryImpl::supportsService( const OUString& ServiceName )
1043{
1044 return cppu::supportsService(this, ServiceName);
1045}
1046
1047Sequence<OUString> SAL_CALL NestedRegistryImpl::getSupportedServiceNames( )
1048{
1049 Sequence< OUString > seqNames { "com.sun.star.registry.NestedRegistry" };
1050 return seqNames;
1051}
1052
1053
1054void SAL_CALL NestedRegistryImpl::initialize( const Sequence< Any >& aArguments )
1055{
1056 Guard< Mutex > aGuard( m_mutex );
1057 if ( (aArguments.getLength() == 2) &&
1058 (aArguments[0].getValueType().getTypeClass() == TypeClass_INTERFACE) &&
1059 (aArguments[1].getValueType().getTypeClass() == TypeClass_INTERFACE) )
1060 {
1061 aArguments[0] >>= m_localReg;
1062 aArguments[1] >>= m_defaultReg;
1063 if ( m_localReg == m_defaultReg )
1064 m_defaultReg.clear();
1065 }
1066}
1067
1068
1069OUString SAL_CALL NestedRegistryImpl::getURL()
1070{
1071 Guard< Mutex > aGuard( m_mutex );
1072 try
1073 {
1074 if ( m_localReg.is() && m_localReg->isValid() )
1075 return m_localReg->getURL();
1076 }
1077 catch(InvalidRegistryException& )
1078 {
1079 }
1080
1081 return OUString();
1082}
1083
1084
1085void SAL_CALL NestedRegistryImpl::open( const OUString&, sal_Bool, sal_Bool )
1086{
1087 throw InvalidRegistryException(
1088 "the 'open' method is not specified for a nested registry" );
1089}
1090
1091
1092sal_Bool SAL_CALL NestedRegistryImpl::isValid( )
1093{
1094 Guard< Mutex > aGuard( m_mutex );
1095 try
1096 {
1097 if ( (m_localReg.is() && m_localReg->isValid()) ||
1098 (m_defaultReg.is() && m_defaultReg->isValid()) )
1099 return true;
1100 }
1101 catch(InvalidRegistryException& )
1102 {
1103 }
1104
1105 return false;
1106}
1107
1108
1109void SAL_CALL NestedRegistryImpl::close( )
1110{
1111 Guard< Mutex > aGuard( m_mutex );
1112 if ( m_localReg.is() && m_localReg->isValid() )
1113 {
1114 m_localReg->close();
1115 }
1116 if ( m_defaultReg.is() && m_defaultReg->isValid() )
1117 {
1118 m_defaultReg->close();
1119 }
1120}
1121
1122
1123void SAL_CALL NestedRegistryImpl::destroy( )
1124{
1125 throw InvalidRegistryException(
1126 "the 'destroy' method is not specified for a nested registry" );
1127}
1128
1129
1130Reference< XRegistryKey > SAL_CALL NestedRegistryImpl::getRootKey( )
1131{
1132 Guard< Mutex > aGuard( m_mutex );
1133 if ( !m_localReg.is() || !m_localReg->isValid() )
1134 {
1135 throw InvalidRegistryException();
1136 }
1137
1138 Reference<XRegistryKey> localKey, defaultKey;
1139
1140 localKey = m_localReg->getRootKey();
1141
1142 if ( localKey.is() )
1143 {
1144 if ( m_defaultReg.is() && m_defaultReg->isValid() )
1145 {
1146 defaultKey = m_defaultReg->getRootKey();
1147 }
1148
1149 return new NestedKeyImpl(this, localKey, defaultKey);
1150 }
1151
1152 return Reference<XRegistryKey>();
1153}
1154
1155
1156sal_Bool SAL_CALL NestedRegistryImpl::isReadOnly( )
1157{
1158 Guard< Mutex > aGuard( m_mutex );
1159 try
1160 {
1161 if ( m_localReg.is() && m_localReg->isValid() )
1162 return m_localReg->isReadOnly();
1163 }
1164 catch(InvalidRegistryException& )
1165 {
1166 }
1167
1168 return false;
1169}
1170
1171
1172void SAL_CALL NestedRegistryImpl::mergeKey( const OUString&, const OUString& )
1173{
1174 throw css::uno::RuntimeException("css.registry.NestedRegistry::mergeKey: not implemented");
1175}
1176
1177} // namespace
1178
1179extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
1181 SAL_UNUSED_PARAMETER css::uno::XComponentContext *,
1182 css::uno::Sequence<css::uno::Any> const &)
1183{
1184 return cppu::acquire(new NestedRegistryImpl);
1185}
1186
1187/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_stoc_NestedRegistry_get_implementation(SAL_UNUSED_PARAMETER css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
bool close
const char * name
Mutex m_mutex
Definition: iafactory.cxx:88
Sequence< PropertyValue > aArguments
uno_Any a
OUString m_name
Definition: mergekeys.cxx:41
sal_Int32 findValue(const css::uno::Sequence< T1 > &_rList, const T2 &_rValue)
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
css::uno::Sequence< T > combineSequences(css::uno::Sequence< T > const &left, css::uno::Sequence< T > const &right)
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
Type
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
RegError REGISTRY_CALLTYPE setLongListValue(RegKeyHandle hKey, rtl_uString *keyName, sal_Int32 const *pValueList, sal_uInt32 len)
RegError REGISTRY_CALLTYPE createKey(RegKeyHandle hKey, rtl_uString *keyName, RegKeyHandle *phNewKey)
RegError REGISTRY_CALLTYPE setStringListValue(RegKeyHandle hKey, rtl_uString *keyName, char **pValueList, sal_uInt32 len)
RegError REGISTRY_CALLTYPE getStringListValue(RegKeyHandle hKey, rtl_uString *keyName, char ***pValueList, sal_uInt32 *pLen)
RegError REGISTRY_CALLTYPE getKeyNames(RegKeyHandle hKey, rtl_uString *keyName, rtl_uString ***pSubKeyNames, sal_uInt32 *pnSubKeys)
RegError REGISTRY_CALLTYPE getLongListValue(RegKeyHandle hKey, rtl_uString *keyName, sal_Int32 **pValueList, sal_uInt32 *pLen)
RegError REGISTRY_CALLTYPE getKeyName(RegKeyHandle hKey, rtl_uString **pKeyName)
RegError REGISTRY_CALLTYPE deleteKey(RegKeyHandle hKey, rtl_uString *keyName)
RegError REGISTRY_CALLTYPE openKey(RegKeyHandle hKey, rtl_uString *keyName, RegKeyHandle *phOpenKey)
RegError REGISTRY_CALLTYPE closeKey(RegKeyHandle hKey)
unsigned char sal_Bool