Debian Codegraph @

ucb/source/ucp/file/bc.cxx

raw
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 <rtl/uri.hxx>
21 #include <rtl/ustrbuf.hxx>
22 #include <rtl/ref.hxx>
23
24 #include <comphelper/diagnose_ex.hxx>
25 #include <com/sun/star/lang/NoSupportException.hpp>
26 #include <com/sun/star/sdbc/SQLException.hpp>
27 #include <com/sun/star/ucb/IllegalIdentifierException.hpp>
28 #include <com/sun/star/ucb/OpenMode.hpp>
29 #include <com/sun/star/beans/IllegalTypeException.hpp>
30 #include <com/sun/star/io/XActiveDataStreamer.hpp>
31 #include <com/sun/star/io/XOutputStream.hpp>
32 #include <com/sun/star/io/XActiveDataSink.hpp>
33 #include <com/sun/star/ucb/NameClash.hpp>
34 #include <comphelper/fileurl.hxx>
35 #include <cppuhelper/supportsservice.hxx>
36 #include <utility>
37 #include "filglob.hxx"
38 #include "filid.hxx"
39 #include "filrow.hxx"
40 #include "bc.hxx"
41 #include "prov.hxx"
42 #include "filerror.hxx"
43 #include "filinsreq.hxx"
44
45 using namespace fileaccess;
46 using namespace com::sun::star;
47 using namespace com::sun::star::uno;
48 using namespace com::sun::star::ucb;
49
50 #if OSL_DEBUG_LEVEL > 0
51 #define THROW_WHERE SAL_WHERE
52 #else
53 #define THROW_WHERE ""
54 #endif
55
56 class fileaccess::PropertyListeners
57 {
58 typedef comphelper::OInterfaceContainerHelper4<beans::XPropertiesChangeListener> ContainerHelper;
59 std::unordered_map<OUString, ContainerHelper> m_aMap;
60
61 public:
62 void disposeAndClear(std::unique_lock<std::mutex>& rGuard, const lang::EventObject& rEvt)
63 {
64 // create a copy, because do not fire event in a guarded section
65 std::unordered_map<OUString, ContainerHelper> tempMap = std::move(m_aMap);
66 for (auto& rPair : tempMap)
67 rPair.second.disposeAndClear(rGuard, rEvt);
68 }
69 void addInterface(std::unique_lock<std::mutex>& rGuard, const OUString& rKey, const uno::Reference<beans::XPropertiesChangeListener>& rListener)
70 {
71 m_aMap[rKey].addInterface(rGuard, rListener);
72 }
73 void removeInterface(std::unique_lock<std::mutex>& rGuard, const OUString& rKey, const uno::Reference<beans::XPropertiesChangeListener>& rListener)
74 {
75 // search container with id rKey
76 auto iter = m_aMap.find(rKey);
77 // container found?
78 if (iter != m_aMap.end())
79 iter->second.removeInterface(rGuard, rListener);
80 }
81 std::vector< OUString > getContainedTypes(std::unique_lock<std::mutex>& rGuard) const
82 {
83 std::vector<OUString> aInterfaceTypes;
84 aInterfaceTypes.reserve(m_aMap.size());
85 for (const auto& rPair : m_aMap)
86 // are interfaces added to this container?
87 if (rPair.second.getLength(rGuard))
88 // yes, put the type in the array
89 aInterfaceTypes.push_back(rPair.first);
90 return aInterfaceTypes;
91 }
92 comphelper::OInterfaceContainerHelper4<beans::XPropertiesChangeListener>* getContainer(std::unique_lock<std::mutex>& , const OUString& rKey)
93 {
94 auto iter = m_aMap.find(rKey);
95 if (iter != m_aMap.end())
96 return &iter->second;
97 return nullptr;
98 }
99 };
100
101
102 /****************************************************************************************/
103 /* */
104 /* BaseContent */
105 /* */
106 /****************************************************************************************/
107
108
109 // Private Constructor for just inserted Contents
110
111 BaseContent::BaseContent( TaskManager* pMyShell,
112 OUString parentName,
113 bool bFolder )
114 : m_pMyShell( pMyShell ),
115 m_aUncPath(std::move( parentName )),
116 m_bFolder( bFolder ),
117 m_nState( JustInserted )
118 {
119 m_pMyShell->m_pProvider->acquire();
120 // No registering, since we have no name
121 }
122
123
124 // Constructor for full featured Contents
125
126 BaseContent::BaseContent( TaskManager* pMyShell,
127 const Reference< XContentIdentifier >& xContentIdentifier,
128 OUString aUncPath, sal_uInt16 nState )
129 : m_pMyShell( pMyShell ),
130 m_xContentIdentifier( xContentIdentifier ),
131 m_aUncPath(std::move( aUncPath )),
132 m_bFolder( false ),
133 m_nState( nState )
134 {
135 m_pMyShell->m_pProvider->acquire();
136 m_pMyShell->registerNotifier( m_aUncPath,this );
137 m_pMyShell->insertDefaultProperties( m_aUncPath );
138 }
139
140
141 BaseContent::~BaseContent( )
142 {
143 if( ( m_nState & FullFeatured ) || ( m_nState & Deleted ) )
144 {
145 m_pMyShell->deregisterNotifier( m_aUncPath,this );
146 }
147 m_pMyShell->m_pProvider->release();
148 }
149
150
151 // XComponent
152
153
154 void SAL_CALL
155 BaseContent::addEventListener( const Reference< lang::XEventListener >& Listener )
156 {
157 std::unique_lock aGuard( m_aMutex );
158
159 m_aDisposeEventListeners.addInterface( aGuard, Listener );
160 }
161
162
163 void SAL_CALL
164 BaseContent::removeEventListener( const Reference< lang::XEventListener >& Listener )
165 {
166 std::unique_lock aGuard( m_aMutex );
167
168 m_aDisposeEventListeners.removeInterface( aGuard, Listener );
169 }
170
171
172 void SAL_CALL
173 BaseContent::dispose()
174 {
175 lang::EventObject aEvt;
176 aEvt.Source = static_cast< XContent* >( this );
177
178 std::unique_lock aGuard( m_aMutex );
179
180 std::unique_ptr<PropertyListeners> pPropertyListener = std::move(m_pPropertyListener);
181
182 m_aDisposeEventListeners.disposeAndClear( aGuard, aEvt );
183 m_aContentEventListeners.disposeAndClear( aGuard, aEvt );
184
185 if( pPropertyListener )
186 pPropertyListener->disposeAndClear( aGuard, aEvt );
187
188 m_aPropertySetInfoChangeListeners.disposeAndClear( aGuard, aEvt );
189 }
190
191 // XServiceInfo
192 OUString SAL_CALL
193 BaseContent::getImplementationName()
194 {
195 return u"com.sun.star.comp.ucb.FileContent"_ustr;
196 }
197
198 sal_Bool SAL_CALL
199 BaseContent::supportsService( const OUString& ServiceName )
200 {
201 return cppu::supportsService( this, ServiceName );
202 }
203
204 Sequence< OUString > SAL_CALL
205 BaseContent::getSupportedServiceNames()
206 {
207 Sequence<OUString> ret { u"com.sun.star.ucb.FileContent"_ustr };
208 return ret;
209 }
210
211 // XCommandProcessor
212
213
214 sal_Int32 SAL_CALL
215 BaseContent::createCommandIdentifier()
216 {
217 return m_pMyShell->getCommandId();
218 }
219
220
221 void SAL_CALL
222 BaseContent::abort( sal_Int32 /*CommandId*/ )
223 {
224 }
225
226
227 Any SAL_CALL
228 BaseContent::execute( const Command& aCommand,
229 sal_Int32 CommandId,
230 const Reference< XCommandEnvironment >& Environment )
231 {
232 if( ! CommandId )
233 // A Command with commandid zero cannot be aborted
234 CommandId = createCommandIdentifier();
235
236 m_pMyShell->startTask( CommandId,
237 Environment );
238
239 Any aAny;
240
241 if (aCommand.Name == "getPropertySetInfo") // No exceptions
242 {
243 aAny <<= getPropertySetInfo();
244 }
245 else if (aCommand.Name == "getCommandInfo") // no exceptions
246 {
247 aAny <<= getCommandInfo();
248 }
249 else if ( aCommand.Name == "setPropertyValues" )
250 {
251 Sequence< beans::PropertyValue > sPropertyValues;
252
253 if( ! ( aCommand.Argument >>= sPropertyValues ) )
254 m_pMyShell->installError( CommandId,
255 TaskHandlerErr::WRONG_SETPROPERTYVALUES_ARGUMENT );
256 else
257 aAny <<= setPropertyValues( CommandId,sPropertyValues ); // calls endTask by itself
258 }
259 else if ( aCommand.Name == "getPropertyValues" )
260 {
261 Sequence< beans::Property > ListOfRequestedProperties;
262
263 if( ! ( aCommand.Argument >>= ListOfRequestedProperties ) )
264 m_pMyShell->installError( CommandId,
265 TaskHandlerErr::WRONG_GETPROPERTYVALUES_ARGUMENT );
266 else
267 aAny <<= getPropertyValues( CommandId,
268 ListOfRequestedProperties );
269 }
270 else if ( aCommand.Name == "open" )
271 {
272 OpenCommandArgument2 aOpenArgument;
273 if( ! ( aCommand.Argument >>= aOpenArgument ) )
274 m_pMyShell->installError( CommandId,
275 TaskHandlerErr::WRONG_OPEN_ARGUMENT );
276 else
277 {
278 Reference< XDynamicResultSet > result = open( CommandId,aOpenArgument );
279 if( result.is() )
280 aAny <<= result;
281 }
282 }
283 else if ( aCommand.Name == "delete" )
284 {
285 if( ! aCommand.Argument.has< bool >() )
286 m_pMyShell->installError( CommandId,
287 TaskHandlerErr::WRONG_DELETE_ARGUMENT );
288 else
289 deleteContent( CommandId );
290 }
291 else if ( aCommand.Name == "transfer" )
292 {
293 TransferInfo aTransferInfo;
294 if( ! ( aCommand.Argument >>= aTransferInfo ) )
295 m_pMyShell->installError( CommandId,
296 TaskHandlerErr::WRONG_TRANSFER_ARGUMENT );
297 else
298 transfer( CommandId, aTransferInfo );
299 }
300 else if ( aCommand.Name == "insert" )
301 {
302 InsertCommandArgument aInsertArgument;
303 if( ! ( aCommand.Argument >>= aInsertArgument ) )
304 m_pMyShell->installError( CommandId,
305 TaskHandlerErr::WRONG_INSERT_ARGUMENT );
306 else
307 insert( CommandId,aInsertArgument );
308 }
309 else if ( aCommand.Name == "getCasePreservingURL" )
310 {
311 Reference< sdbc::XRow > xRow = getPropertyValues( CommandId, { { u"CasePreservingURL"_ustr, -1, cppu::UnoType<sal_Bool>::get(), 0 } });
312 OUString CasePreservingURL = xRow->getString(1);
313 if(!xRow->wasNull())
314 aAny <<= CasePreservingURL;
315 }
316 else if ( aCommand.Name == "createNewContent" )
317 {
318 ucb::ContentInfo aArg;
319 if ( !( aCommand.Argument >>= aArg ) )
320 m_pMyShell->installError( CommandId,
321 TaskHandlerErr::WRONG_CREATENEWCONTENT_ARGUMENT );
322 else
323 aAny <<= createNewContent( aArg );
324 }
325 else
326 m_pMyShell->installError( CommandId,
327 TaskHandlerErr::UNSUPPORTED_COMMAND );
328
329
330 // This is the only function allowed to throw an exception
331 endTask( CommandId );
332
333 return aAny;
334 }
335
336
337 void SAL_CALL
338 BaseContent::addPropertiesChangeListener(
339 const Sequence< OUString >& PropertyNames,
340 const Reference< beans::XPropertiesChangeListener >& Listener )
341 {
342 if( ! Listener.is() )
343 return;
344
345 std::unique_lock aGuard( m_aMutex );
346
347 if( ! m_pPropertyListener )
348 m_pPropertyListener.reset( new PropertyListeners );
349
350
351 if( !PropertyNames.hasElements() )
352 m_pPropertyListener->addInterface( aGuard, OUString(),Listener );
353 else
354 {
355 Reference< beans::XPropertySetInfo > xProp = m_pMyShell->info_p( m_aUncPath );
356 for( const auto& rName : PropertyNames )
357 if( xProp->hasPropertyByName( rName ) )
358 m_pPropertyListener->addInterface( aGuard, rName,Listener );
359 }
360 }
361
362
363 void SAL_CALL
364 BaseContent::removePropertiesChangeListener( const Sequence< OUString >& PropertyNames,
365 const Reference< beans::XPropertiesChangeListener >& Listener )
366 {
367 if( ! Listener.is() )
368 return;
369
370 std::unique_lock aGuard( m_aMutex );
371
372 if( ! m_pPropertyListener )
373 return;
374
375 for( const auto& rName : PropertyNames )
376 m_pPropertyListener->removeInterface( aGuard, rName,Listener );
377
378 m_pPropertyListener->removeInterface( aGuard, OUString(), Listener );
379 }
380
381
382 // XContent
383
384
385 Reference< ucb::XContentIdentifier > SAL_CALL
386 BaseContent::getIdentifier()
387 {
388 return m_xContentIdentifier;
389 }
390
391
392 OUString SAL_CALL
393 BaseContent::getContentType()
394 {
395 if( !( m_nState & Deleted ) )
396 {
397 if( m_nState & JustInserted )
398 {
399 if ( m_bFolder )
400 return TaskManager::FolderContentType;
401 else
402 return TaskManager::FileContentType;
403 }
404 else
405 {
406 try
407 {
408 // Who am I ?
409 Reference< sdbc::XRow > xRow = getPropertyValues( -1, { { u"IsDocument"_ustr, -1, cppu::UnoType<sal_Bool>::get(), 0 } });
410 bool IsDocument = xRow->getBoolean( 1 );
411
412 if ( !xRow->wasNull() )
413 {
414 if ( IsDocument )
415 return TaskManager::FileContentType;
416 else
417 return TaskManager::FolderContentType;
418 }
419 else
420 {
421 OSL_FAIL( "BaseContent::getContentType - Property value was null!" );
422 }
423 }
424 catch (const sdbc::SQLException&)
425 {
426 TOOLS_WARN_EXCEPTION("ucb.ucp.file", "");
427 }
428 }
429 }
430
431 return OUString();
432 }
433
434
435 void SAL_CALL
436 BaseContent::addContentEventListener(
437 const Reference< XContentEventListener >& Listener )
438 {
439 std::unique_lock aGuard( m_aMutex );
440
441 m_aContentEventListeners.addInterface( aGuard, Listener );
442 }
443
444
445 void SAL_CALL
446 BaseContent::removeContentEventListener(
447 const Reference< XContentEventListener >& Listener )
448 {
449 std::unique_lock aGuard( m_aMutex );
450
451 m_aContentEventListeners.removeInterface( aGuard, Listener );
452 }
453
454
455 // XPropertyContainer
456
457
458 void SAL_CALL
459 BaseContent::addProperty(
460 const OUString& Name,
461 sal_Int16 Attributes,
462 const Any& DefaultValue )
463 {
464 if( ( m_nState & JustInserted ) || ( m_nState & Deleted ) || Name.isEmpty() )
465 {
466 throw lang::IllegalArgumentException( THROW_WHERE, uno::Reference< uno::XInterface >(), 0 );
467 }
468
469 m_pMyShell->associate( m_aUncPath,Name,DefaultValue,Attributes );
470 }
471
472
473 void SAL_CALL
474 BaseContent::removeProperty( const OUString& Name )
475 {
476 if( m_nState & Deleted )
477 throw beans::UnknownPropertyException( Name );
478
479 m_pMyShell->deassociate( m_aUncPath, Name );
480 }
481
482
483 // XContentCreator
484
485
486 Sequence< ContentInfo > SAL_CALL
487 BaseContent::queryCreatableContentsInfo()
488 {
489 return TaskManager::queryCreatableContentsInfo();
490 }
491
492
493 Reference< XContent > SAL_CALL
494 BaseContent::createNewContent( const ContentInfo& Info )
495 {
496 // Check type.
497 if ( Info.Type.isEmpty() )
498 return Reference< XContent >();
499
500 bool bFolder = Info.Type == TaskManager::FolderContentType;
501 if ( !bFolder )
502 {
503 if ( Info.Type != TaskManager::FileContentType )
504 {
505 // Neither folder nor file to create!
506 return Reference< XContent >();
507 }
508 }
509
510 // Who am I ?
511 bool IsDocument = false;
512
513 try
514 {
515 Reference< sdbc::XRow > xRow = getPropertyValues( -1, { { u"IsDocument"_ustr, -1, cppu::UnoType<sal_Bool>::get(), 0 } });
516 IsDocument = xRow->getBoolean( 1 );
517
518 if ( xRow->wasNull() )
519 {
520 IsDocument = false;
521 // OSL_FAIL( // "BaseContent::createNewContent - Property value was null!" );
522 // return Reference< XContent >();
523 }
524 }
525 catch (const sdbc::SQLException&)
526 {
527 TOOLS_WARN_EXCEPTION("ucb.ucp.file", "");
528 return Reference< XContent >();
529 }
530
531 OUString dstUncPath;
532
533 if( IsDocument )
534 {
535 // KSO: Why is a document a XContentCreator? This is quite unusual.
536 dstUncPath = getParentName( m_aUncPath );
537 }
538 else
539 dstUncPath = m_aUncPath;
540
541 return new BaseContent( m_pMyShell, dstUncPath, bFolder );
542 }
543
544
545 // XPropertySetInfoChangeNotifier
546
547
548 void SAL_CALL
549 BaseContent::addPropertySetInfoChangeListener(
550 const Reference< beans::XPropertySetInfoChangeListener >& Listener )
551 {
552 std::unique_lock aGuard( m_aMutex );
553
554 m_aPropertySetInfoChangeListeners.addInterface( aGuard, Listener );
555 }
556
557
558 void SAL_CALL
559 BaseContent::removePropertySetInfoChangeListener(
560 const Reference< beans::XPropertySetInfoChangeListener >& Listener )
561 {
562 std::unique_lock aGuard( m_aMutex );
563
564 m_aPropertySetInfoChangeListeners.removeInterface( aGuard, Listener );
565 }
566
567
568 // XChild
569
570
571 Reference< XInterface > SAL_CALL
572 BaseContent::getParent()
573 {
574 OUString ParentUnq = getParentName( m_aUncPath );
575 OUString ParentUrl;
576
577
578 bool err = fileaccess::TaskManager::getUrlFromUnq( ParentUnq, ParentUrl );
579 if( err )
580 return Reference< XInterface >( nullptr );
581
582 rtl::Reference<FileContentIdentifier> Identifier = new FileContentIdentifier( ParentUnq );
583
584 try
585 {
586 return Reference<XInterface>( m_pMyShell->m_pProvider->queryContent( Identifier ), UNO_QUERY );
587 }
588 catch (const IllegalIdentifierException&)
589 {
590 return Reference< XInterface >();
591 }
592 }
593
594
595 void SAL_CALL
596 BaseContent::setParent(
597 const Reference< XInterface >& )
598 {
599 throw lang::NoSupportException( THROW_WHERE );
600 }
601
602
603 // Private Methods
604
605
606 Reference< XCommandInfo >
607 BaseContent::getCommandInfo()
608 {
609 if( m_nState & Deleted )
610 return Reference< XCommandInfo >();
611
612 return m_pMyShell->info_c();
613 }
614
615
616 Reference< beans::XPropertySetInfo >
617 BaseContent::getPropertySetInfo()
618 {
619 if( m_nState & Deleted )
620 return Reference< beans::XPropertySetInfo >();
621
622 return m_pMyShell->info_p( m_aUncPath );
623 }
624
625 Reference< sdbc::XRow >
626 BaseContent::getPropertyValues(
627 sal_Int32 nMyCommandIdentifier,
628 const Sequence< beans::Property >& PropertySet )
629 {
630 sal_Int32 nProps = PropertySet.getLength();
631 if ( !nProps )
632 return Reference< sdbc::XRow >();
633
634 if( m_nState & Deleted )
635 {
636 Sequence< Any > aValues( nProps );
637 return Reference< sdbc::XRow >( new XRow_impl( m_pMyShell, aValues ) );
638 }
639
640 if( m_nState & JustInserted )
641 {
642 Sequence< Any > aValues( nProps );
643 Any* pValues = aValues.getArray();
644
645 const beans::Property* pProps = PropertySet.getConstArray();
646
647 for ( sal_Int32 n = 0; n < nProps; ++n )
648 {
649 const beans::Property& rProp = pProps[ n ];
650 Any& rValue = pValues[ n ];
651
652 if ( rProp.Name == "ContentType" )
653 {
654 rValue <<= (m_bFolder ? TaskManager::FolderContentType
655 : TaskManager::FileContentType);
656 }
657 else if ( rProp.Name == "IsFolder" )
658 {
659 rValue <<= m_bFolder;
660 }
661 else if ( rProp.Name == "IsDocument" )
662 {
663 rValue <<= !m_bFolder;
664 }
665 }
666
667 return Reference< sdbc::XRow >(
668 new XRow_impl( m_pMyShell, aValues ) );
669 }
670
671 return m_pMyShell->getv( nMyCommandIdentifier,
672 m_aUncPath,
673 PropertySet );
674 }
675
676
677 Sequence< Any >
678 BaseContent::setPropertyValues(
679 sal_Int32 nMyCommandIdentifier,
680 const Sequence< beans::PropertyValue >& Values )
681 {
682 if( m_nState & Deleted )
683 { // To do
684 return Sequence< Any >( Values.getLength() );
685 }
686
687 static constexpr OUString Title(u"Title"_ustr);
688
689 // Special handling for files which have to be inserted
690 if( m_nState & JustInserted )
691 {
692 for( const auto& rValue : Values )
693 {
694 if( rValue.Name == Title )
695 {
696 OUString NewTitle;
697 if( rValue.Value >>= NewTitle )
698 {
699 if ( m_nState & NameForInsertionSet )
700 {
701 // User wants to set another Title before "insert".
702 // m_aUncPath contains previous own URI.
703
704 sal_Int32 nLastSlash = m_aUncPath.lastIndexOf( '/' );
705 bool bTrailingSlash = false;
706 if ( nLastSlash == m_aUncPath.getLength() - 1 )
707 {
708 bTrailingSlash = true;
709 nLastSlash
710 = m_aUncPath.lastIndexOf( '/', nLastSlash );
711 }
712
713 OSL_ENSURE( nLastSlash != -1,
714 "BaseContent::setPropertyValues: "
715 "Invalid URL!" );
716
717 OUStringBuffer aBuf(
718 m_aUncPath.subView( 0, nLastSlash + 1 ) );
719
720 if ( !NewTitle.isEmpty() )
721 {
722 aBuf.append( NewTitle );
723 if ( bTrailingSlash )
724 aBuf.append( '/' );
725 }
726 else
727 {
728 m_nState &= ~NameForInsertionSet;
729 }
730
731 m_aUncPath = aBuf.makeStringAndClear();
732 }
733 else
734 {
735 if ( !NewTitle.isEmpty() )
736 {
737 // Initial Title before "insert".
738 // m_aUncPath contains parent's URI.
739
740 if( !m_aUncPath.endsWith( "/" ) )
741 m_aUncPath += "/";
742
743 m_aUncPath += rtl::Uri::encode( NewTitle,
744 rtl_UriCharClassPchar,
745 rtl_UriEncodeIgnoreEscapes,
746 RTL_TEXTENCODING_UTF8 );
747 m_nState |= NameForInsertionSet;
748 }
749 }
750 }
751 }
752 }
753
754 return Sequence< Any >( Values.getLength() );
755 }
756 else
757 {
758 Sequence< Any > ret = m_pMyShell->setv( m_aUncPath, // Does not handle Title
759 Values );
760 auto retRange = asNonConstRange(ret);
761
762 // Special handling Title: Setting Title is equivalent to a renaming of the underlying file
763 for( sal_Int32 i = 0; i < Values.getLength(); ++i )
764 {
765 if( Values[i].Name != Title )
766 continue; // handled by setv
767
768 OUString NewTitle;
769 if( !( Values[i].Value >>= NewTitle ) )
770 {
771 retRange[i] <<= beans::IllegalTypeException( THROW_WHERE );
772 break;
773 }
774 else if( NewTitle.isEmpty() )
775 {
776 retRange[i] <<= lang::IllegalArgumentException( THROW_WHERE, uno::Reference< uno::XInterface >(), 0 );
777 break;
778 }
779
780
781 OUString aDstName = getParentName( m_aUncPath );
782 if( !aDstName.endsWith("/") )
783 aDstName += "/";
784
785 aDstName += rtl::Uri::encode( NewTitle,
786 rtl_UriCharClassPchar,
787 rtl_UriEncodeIgnoreEscapes,
788 RTL_TEXTENCODING_UTF8 );
789
790 m_pMyShell->move( nMyCommandIdentifier, // move notifies the children also;
791 m_aUncPath,
792 aDstName,
793 NameClash::KEEP );
794
795 try
796 {
797 endTask( nMyCommandIdentifier );
798 }
799 catch(const Exception& e)
800 {
801 retRange[i] <<= e;
802 }
803
804 // NameChanges come back through a ContentEvent
805 break; // only handling Title
806 } // end for
807
808 return ret;
809 }
810 }
811
812
813 Reference< XDynamicResultSet >
814 BaseContent::open(
815 sal_Int32 nMyCommandIdentifier,
816 const OpenCommandArgument2& aCommandArgument )
817 {
818 Reference< XDynamicResultSet > retValue;
819
820 if( m_nState & Deleted )
821 {
822 m_pMyShell->installError( nMyCommandIdentifier,
823 TaskHandlerErr::DELETED_STATE_IN_OPEN_COMMAND );
824 }
825 else if( m_nState & JustInserted )
826 {
827 m_pMyShell->installError( nMyCommandIdentifier,
828 TaskHandlerErr::INSERTED_STATE_IN_OPEN_COMMAND );
829 }
830 else
831 {
832 if( aCommandArgument.Mode == OpenMode::DOCUMENT ||
833 aCommandArgument.Mode == OpenMode::DOCUMENT_SHARE_DENY_NONE )
834
835 {
836 Reference< io::XOutputStream > outputStream( aCommandArgument.Sink,UNO_QUERY );
837 if( outputStream.is() )
838 {
839 m_pMyShell->page( nMyCommandIdentifier,
840 m_aUncPath,
841 outputStream );
842 }
843
844 bool bLock = ( aCommandArgument.Mode != OpenMode::DOCUMENT_SHARE_DENY_NONE );
845
846 Reference< io::XActiveDataSink > activeDataSink( aCommandArgument.Sink,UNO_QUERY );
847 if( activeDataSink.is() )
848 {
849 activeDataSink->setInputStream( m_pMyShell->open( nMyCommandIdentifier,
850 m_aUncPath,
851 bLock ) );
852 }
853
854 Reference< io::XActiveDataStreamer > activeDataStreamer( aCommandArgument.Sink,UNO_QUERY );
855 if( activeDataStreamer.is() )
856 {
857 activeDataStreamer->setStream( m_pMyShell->open_rw( nMyCommandIdentifier,
858 m_aUncPath,
859 bLock ) );
860 }
861 }
862 else if ( aCommandArgument.Mode == OpenMode::ALL ||
863 aCommandArgument.Mode == OpenMode::FOLDERS ||
864 aCommandArgument.Mode == OpenMode::DOCUMENTS )
865 {
866 retValue = m_pMyShell->ls( nMyCommandIdentifier,
867 m_aUncPath,
868 aCommandArgument.Mode,
869 aCommandArgument.Properties,
870 aCommandArgument.SortingInfo );
871 }
872 // else if( aCommandArgument.Mode ==
873 // OpenMode::DOCUMENT_SHARE_DENY_NONE ||
874 // aCommandArgument.Mode ==
875 // OpenMode::DOCUMENT_SHARE_DENY_WRITE )
876 // m_pMyShell->installError( nMyCommandIdentifier,
877 // TaskHandlerErr::UNSUPPORTED_OPEN_MODE,
878 // aCommandArgument.Mode);
879 else
880 m_pMyShell->installError( nMyCommandIdentifier,
881 TaskHandlerErr::UNSUPPORTED_OPEN_MODE,
882 aCommandArgument.Mode);
883 }
884
885 return retValue;
886 }
887
888
889 void
890 BaseContent::deleteContent( sal_Int32 nMyCommandIdentifier )
891 {
892 if( m_nState & Deleted )
893 return;
894
895 if( m_pMyShell->remove( nMyCommandIdentifier,m_aUncPath ) )
896 m_nState |= Deleted;
897 }
898
899 void
900 BaseContent::transfer( sal_Int32 nMyCommandIdentifier,
901 const TransferInfo& aTransferInfo )
902 {
903 if( m_nState & Deleted )
904 return;
905
906 if( !comphelper::isFileUrl(aTransferInfo.SourceURL) )
907 {
908 m_pMyShell->installError( nMyCommandIdentifier,
909 TaskHandlerErr::TRANSFER_INVALIDSCHEME );
910 return;
911 }
912
913 OUString srcUnc;
914 if( fileaccess::TaskManager::getUnqFromUrl( aTransferInfo.SourceURL,srcUnc ) )
915 {
916 m_pMyShell->installError( nMyCommandIdentifier,
917 TaskHandlerErr::TRANSFER_INVALIDURL );
918 return;
919 }
920
921 OUString srcUncPath = srcUnc;
922
923 // Determine the new title !
924 OUString NewTitle;
925 if( !aTransferInfo.NewTitle.isEmpty() )
926 NewTitle = rtl::Uri::encode( aTransferInfo.NewTitle,
927 rtl_UriCharClassPchar,
928 rtl_UriEncodeIgnoreEscapes,
929 RTL_TEXTENCODING_UTF8 );
930 else
931 NewTitle = srcUncPath.copy( 1 + srcUncPath.lastIndexOf( '/' ) );
932
933 // Is destination a document or a folder ?
934 Reference< sdbc::XRow > xRow = getPropertyValues( nMyCommandIdentifier,{ { u"IsDocument"_ustr, -1, cppu::UnoType<sal_Bool>::get(), 0 } } );
935 bool IsDocument = xRow->getBoolean( 1 );
936 if( xRow->wasNull() )
937 { // Destination file type could not be determined
938 m_pMyShell->installError( nMyCommandIdentifier,
939 TaskHandlerErr::TRANSFER_DESTFILETYPE );
940 return;
941 }
942
943 OUString dstUncPath;
944 if( IsDocument )
945 { // as sibling
946 sal_Int32 lastSlash = m_aUncPath.lastIndexOf( '/' );
947 dstUncPath = m_aUncPath.copy(0,lastSlash );
948 }
949 else
950 // as child
951 dstUncPath = m_aUncPath;
952
953 dstUncPath += "/" + NewTitle;
954
955 sal_Int32 NameClash = aTransferInfo.NameClash;
956
957 if( aTransferInfo.MoveData )
958 m_pMyShell->move( nMyCommandIdentifier,srcUncPath,dstUncPath,NameClash );
959 else
960 m_pMyShell->copy( nMyCommandIdentifier,srcUncPath,dstUncPath,NameClash );
961 }
962
963
964 void BaseContent::insert( sal_Int32 nMyCommandIdentifier,
965 const InsertCommandArgument& aInsertArgument )
966 {
967 if( m_nState & FullFeatured )
968 {
969 m_pMyShell->write( nMyCommandIdentifier,
970 m_aUncPath,
971 aInsertArgument.ReplaceExisting,
972 aInsertArgument.Data );
973 return;
974 }
975
976 if( ! ( m_nState & JustInserted ) )
977 {
978 m_pMyShell->installError( nMyCommandIdentifier,
979 TaskHandlerErr::NOFRESHINSERT_IN_INSERT_COMMAND );
980 return;
981 }
982
983 // Inserts the content, which has the flag m_bIsFresh
984
985 if( ! (m_nState & NameForInsertionSet) )
986 {
987 m_pMyShell->installError( nMyCommandIdentifier,
988 TaskHandlerErr::NONAMESET_INSERT_COMMAND );
989 return;
990 }
991
992 // Inserting a document or a file?
993 bool bDocument = false;
994
995 Reference< sdbc::XRow > xRow = getPropertyValues( -1, { { u"IsDocument"_ustr, -1, cppu::UnoType<sal_Bool>::get(), 0 } });
996
997 bool contentTypeSet = true; // is set to false, if contentType not set
998 try
999 {
1000 bDocument = xRow->getBoolean( 1 );
1001 if( xRow->wasNull() )
1002 contentTypeSet = false;
1003
1004 }
1005 catch (const sdbc::SQLException&)
1006 {
1007 TOOLS_WARN_EXCEPTION("ucb.ucp.file", "");
1008 contentTypeSet = false;
1009 }
1010
1011 if( ! contentTypeSet )
1012 {
1013 m_pMyShell->installError( nMyCommandIdentifier,
1014 TaskHandlerErr::NOCONTENTTYPE_INSERT_COMMAND );
1015 return;
1016 }
1017
1018
1019 bool success = false;
1020 if( bDocument )
1021 success = m_pMyShell->mkfil( nMyCommandIdentifier,
1022 m_aUncPath,
1023 aInsertArgument.ReplaceExisting,
1024 aInsertArgument.Data );
1025 else
1026 {
1027 while( ! success )
1028 {
1029 success = m_pMyShell->mkdir( nMyCommandIdentifier,
1030 m_aUncPath,
1031 aInsertArgument.ReplaceExisting );
1032 if( success )
1033 break;
1034
1035 XInteractionRequestImpl aRequestImpl(
1036 rtl::Uri::decode(
1037 OUString(getTitle(m_aUncPath)),
1038 rtl_UriDecodeWithCharset,
1039 RTL_TEXTENCODING_UTF8),
1040 getXWeak(),
1041 m_pMyShell,nMyCommandIdentifier);
1042 uno::Reference<task::XInteractionRequest> const& xReq(aRequestImpl.getRequest());
1043
1044 m_pMyShell->handleTask( nMyCommandIdentifier, xReq );
1045 if (aRequestImpl.aborted() || aRequestImpl.newName().isEmpty())
1046 // means aborting
1047 break;
1048
1049 // determine new uncpath
1050 m_pMyShell->clearError( nMyCommandIdentifier );
1051 m_aUncPath = getParentName( m_aUncPath );
1052 if( !m_aUncPath.endsWith( "/" ) )
1053 m_aUncPath += "/";
1054
1055 m_aUncPath += rtl::Uri::encode( aRequestImpl.newName(),
1056 rtl_UriCharClassPchar,
1057 rtl_UriEncodeIgnoreEscapes,
1058 RTL_TEXTENCODING_UTF8 );
1059 }
1060 }
1061
1062 if ( ! success )
1063 return;
1064
1065 m_xContentIdentifier.set( new FileContentIdentifier( m_aUncPath ) );
1066
1067 m_pMyShell->registerNotifier( m_aUncPath,this );
1068 m_pMyShell->insertDefaultProperties( m_aUncPath );
1069
1070 m_nState = FullFeatured;
1071 }
1072
1073
1074 void BaseContent::endTask( sal_Int32 CommandId )
1075 {
1076 // This is the only function allowed to throw an exception
1077 m_pMyShell->endTask( CommandId,m_aUncPath,this );
1078 }
1079
1080
1081 std::optional<ContentEventNotifier>
1082 BaseContent::cDEL()
1083 {
1084 m_nState |= Deleted;
1085
1086 std::unique_lock aGuard( m_aMutex );
1087 if( m_aContentEventListeners.getLength(aGuard) == 0 )
1088 return {};
1089
1090 return ContentEventNotifier( m_pMyShell,
1091 this,
1092 m_xContentIdentifier,
1093 m_aContentEventListeners.getElements(aGuard) );
1094 }
1095
1096
1097 std::optional<ContentEventNotifier>
1098 BaseContent::cEXC( const OUString& aNewName )
1099 {
1100 std::unique_lock aGuard( m_aMutex );
1101
1102 Reference< XContentIdentifier > xOldRef = m_xContentIdentifier;
1103 m_aUncPath = aNewName;
1104 m_xContentIdentifier = new FileContentIdentifier( aNewName );
1105
1106 if( m_aContentEventListeners.getLength(aGuard) == 0 )
1107 return {};
1108 return ContentEventNotifier( m_pMyShell,
1109 this,
1110 m_xContentIdentifier,
1111 xOldRef,
1112 m_aContentEventListeners.getElements(aGuard) );
1113 }
1114
1115
1116 std::optional<ContentEventNotifier>
1117 BaseContent::cCEL()
1118 {
1119 std::unique_lock aGuard( m_aMutex );
1120 if( m_aContentEventListeners.getLength(aGuard) == 0 )
1121 return {};
1122 return ContentEventNotifier( m_pMyShell,
1123 this,
1124 m_xContentIdentifier,
1125 m_aContentEventListeners.getElements(aGuard) );
1126 }
1127
1128 std::optional<PropertySetInfoChangeNotifier>
1129 BaseContent::cPSL()
1130 {
1131 std::unique_lock aGuard( m_aMutex );
1132 if( m_aPropertySetInfoChangeListeners.getLength(aGuard) == 0 )
1133 return {};
1134 return PropertySetInfoChangeNotifier( this, m_aPropertySetInfoChangeListeners.getElements(aGuard) );
1135 }
1136
1137
1138 std::optional<PropertyChangeNotifier>
1139 BaseContent::cPCL()
1140 {
1141 std::unique_lock aGuard( m_aMutex );
1142
1143 if (!m_pPropertyListener)
1144 return {};
1145
1146 const std::vector< OUString > seqNames = m_pPropertyListener->getContainedTypes(aGuard);
1147 if( seqNames.empty() )
1148 return {};
1149
1150 ListenerMap listener;
1151 for( const auto& rName : seqNames )
1152 {
1153 comphelper::OInterfaceContainerHelper4<beans::XPropertiesChangeListener>* pContainer = m_pPropertyListener->getContainer(aGuard, rName);
1154 if (!pContainer)
1155 continue;
1156 listener[rName] = pContainer->getElements(aGuard);
1157 }
1158
1159 return PropertyChangeNotifier( this, std::move(listener) );
1160 }
1161
1162
1163 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */