Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / ucb / source / ucp / file / filtask.hxx
blobc2623c60be05fe909388552645a9306f64f8a1c9
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 .
19 #pragma once
21 #include <o3tl/sorted_vector.hxx>
22 #include <osl/file.hxx>
23 #include <rtl/ustring.hxx>
25 #include <osl/mutex.hxx>
26 #include <com/sun/star/uno/Sequence.hxx>
27 #include <com/sun/star/beans/PropertyChangeEvent.hpp>
28 #include <com/sun/star/ucb/XCommandInfo.hpp>
29 #include <com/sun/star/beans/Property.hpp>
30 #include <com/sun/star/beans/PropertyValue.hpp>
31 #include <com/sun/star/io/XStream.hpp>
32 #include <com/sun/star/io/XOutputStream.hpp>
33 #include <com/sun/star/io/XInputStream.hpp>
34 #include <com/sun/star/beans/XPropertySetInfo.hpp>
35 #include <com/sun/star/ucb/NumberedSortingInfo.hpp>
36 #include <com/sun/star/sdbc/XRow.hpp>
37 #include <com/sun/star/uno/XComponentContext.hpp>
38 #include <com/sun/star/ucb/XDynamicResultSet.hpp>
39 #include <com/sun/star/beans/XPropertyContainer.hpp>
40 #include <com/sun/star/beans/XPropertyAccess.hpp>
41 #include <com/sun/star/ucb/ContentInfo.hpp>
42 #include <com/sun/star/ucb/XCommandEnvironment.hpp>
43 #include <com/sun/star/ucb/XPersistentPropertySet.hpp>
44 #include <com/sun/star/ucb/XPropertySetRegistry.hpp>
45 #include <com/sun/star/task/XInteractionHandler.hpp>
46 #include <com/sun/star/task/XInteractionRequest.hpp>
47 #include "filerror.hxx"
48 #include "filnot.hxx"
49 #include <mutex>
50 #include <unordered_map>
51 #include <unordered_set>
52 #include <vector>
54 namespace fileaccess
56 class BaseContent;
57 class FileProvider;
58 class XPropertySetInfo_impl;
59 class XCommandInfo_impl;
60 class XResultSet_impl;
63 * The relevant methods in this class all have as first argument the CommandId,
64 * so if necessary, every method has access to its relevant XInteractionHandler and
65 * XProgressHandler.
69 class TaskManager
71 friend class XPropertySetInfo_impl;
72 friend class XResultSet_impl;
73 friend class XCommandInfo_impl;
75 private:
77 class TaskHandling
79 private:
81 bool m_bHandled;
82 sal_Int32 m_nErrorCode,m_nMinorCode;
83 css::uno::Reference< css::task::XInteractionHandler > m_xInteractionHandler;
84 css::uno::Reference< css::ucb::XCommandEnvironment > m_xCommandEnvironment;
87 public:
89 explicit TaskHandling(
90 css::uno::Reference< css::ucb::XCommandEnvironment > xCommandEnv )
91 : m_bHandled( false ),
92 m_nErrorCode( TASKHANDLER_NO_ERROR ),
93 m_nMinorCode( TASKHANDLER_NO_ERROR ),
94 m_xCommandEnvironment( std::move(xCommandEnv) )
98 void setHandled()
100 m_bHandled = true;
103 bool isHandled() const
105 return m_bHandled;
108 void clearError()
110 m_nErrorCode = TASKHANDLER_NO_ERROR;
111 m_nMinorCode = TASKHANDLER_NO_ERROR;
114 void installError( sal_Int32 nErrorCode,
115 sal_Int32 nMinorCode )
117 m_nErrorCode = nErrorCode;
118 m_nMinorCode = nMinorCode;
121 sal_Int32 getInstalledError() const
123 return m_nErrorCode;
126 sal_Int32 getMinorErrorCode() const
128 return m_nMinorCode;
131 css::uno::Reference< css::task::XInteractionHandler > const &
132 getInteractionHandler()
134 if( ! m_xInteractionHandler.is() && m_xCommandEnvironment.is() )
135 m_xInteractionHandler = m_xCommandEnvironment->getInteractionHandler();
137 return m_xInteractionHandler;
140 const css::uno::Reference< css::ucb::XCommandEnvironment >&
141 getCommandEnvironment() const
143 return m_xCommandEnvironment;
146 }; // end class TaskHandling
149 typedef std::unordered_map< sal_Int32,TaskHandling > TaskMap;
150 private:
152 std::mutex m_aMutex;
153 sal_Int32 m_nCommandId;
154 TaskMap m_aTaskMap;
157 public:
158 class MyProperty
160 private:
161 OUString PropertyName;
162 sal_Int32 Handle;
163 bool isNative;
164 css::uno::Type Typ; // Duplicates information in Value
165 css::uno::Any Value;
166 css::beans::PropertyState State;
167 sal_Int16 Attributes;
168 public:
169 explicit MyProperty( const OUString& thePropertyName );
170 MyProperty( bool theIsNative,
171 const OUString& thePropertyName,
172 sal_Int32 theHandle,
173 const css::uno::Type& theTyp,
174 const css::uno::Any& theValue,
175 const css::beans::PropertyState& theState,
176 sal_Int16 theAttributes );
178 inline const bool& IsNative() const;
179 const OUString& getPropertyName() const { return PropertyName; }
180 inline const sal_Int32& getHandle() const;
181 inline const css::uno::Type& getType() const;
182 inline const css::uno::Any& getValue() const;
183 inline const css::beans::PropertyState& getState() const;
184 inline const sal_Int16& getAttributes() const;
186 // The set* functions are declared const, because the key of "this" stays intact
187 inline void setValue( css::uno::Any theValue ) const;
188 inline void setState( const css::beans::PropertyState& theState ) const;
191 struct MyPropertyLess
193 bool operator()( const MyProperty& rKey1, const MyProperty& rKey2 ) const
195 return rKey1.getPropertyName() < rKey2.getPropertyName();
199 typedef o3tl::sorted_vector< MyProperty, MyPropertyLess > PropertySet;
201 class UnqPathData
203 public:
204 UnqPathData();
205 UnqPathData(UnqPathData&&);
206 ~UnqPathData();
208 PropertySet properties;
209 std::vector< Notifier* > notifier;
211 // Three views on the PersistentPropertySet
212 css::uno::Reference< css::ucb::XPersistentPropertySet > xS;
213 css::uno::Reference< css::beans::XPropertyContainer > xC;
214 css::uno::Reference< css::beans::XPropertyAccess > xA;
217 typedef std::unordered_map< OUString,UnqPathData > ContentMap;
219 TaskManager( const css::uno::Reference< css::uno::XComponentContext >& rxContext,
220 FileProvider* pProvider, bool bWithConfig );
221 ~TaskManager();
223 /// @throws css::ucb::DuplicateCommandIdentifierException
224 void startTask(
225 sal_Int32 CommandId,
226 const css::uno::Reference< css::ucb::XCommandEnvironment >& xCommandEnv );
228 sal_Int32 getCommandId();
232 * The error code may be one of the error codes defined in
233 * filerror.hxx.
234 * The minor code refines the information given in ErrorCode.
237 void installError( sal_Int32 CommandId,
238 sal_Int32 ErrorCode,
239 sal_Int32 minorCode = TASKHANDLER_NO_ERROR );
241 void retrieveError( sal_Int32 CommandId,
242 sal_Int32 &ErrorCode,
243 sal_Int32 &minorCode);
246 * Deinstalls the task and evaluates a possibly set error code.
247 * "endTask" throws in case an error code is set the corresponding exception.
250 void endTask( sal_Int32 CommandId,
251 // the physical URL of the object
252 const OUString& aUnqPath,
253 BaseContent* pContent);
257 * Handles an interactionrequest
260 void handleTask( sal_Int32 CommandId,
261 const css::uno::Reference< css::task::XInteractionRequest >& request );
264 * Clears any error which are set on the commandid
267 void clearError( sal_Int32 );
270 * This two methods register and deregister a change listener for the content belonging
271 * to URL aUnqPath
274 void registerNotifier( const OUString& aUnqPath,Notifier* pNotifier );
276 void deregisterNotifier( const OUString& aUnqPath,Notifier* pNotifier );
280 * Used to associate and deassociate a new property with
281 * the content belonging to URL UnqPath.
282 * The default value and the attributes are input
284 * @throws css::beans::PropertyExistException
285 * @throws css::beans::IllegalTypeException
286 * @throws css::uno::RuntimeException
289 void associate( const OUString& UnqPath,
290 const OUString& PropertyName,
291 const css::uno::Any& DefaultValue,
292 const sal_Int16 Attributes );
294 /// @throws css::beans::UnknownPropertyException
295 /// @throws css::beans::NotRemoveableException
296 /// @throws css::uno::RuntimeException
297 void deassociate( const OUString& UnqPath,
298 const OUString& PropertyName );
301 // Every method having a command id is not allowed to throw anything,
302 // but instead must install every error code in the task handler
306 * Given an xOutputStream, this method writes the content of the file belonging to
307 * URL aUnqPath into the XOutputStream
310 void page( sal_Int32 CommandId,
311 const OUString& aUnqPath,
312 const css::uno::Reference< css::io::XOutputStream >& xOutputStream );
316 * Given a file URL aUnqPath, this methods returns a XInputStream which reads from the open file.
319 css::uno::Reference< css::io::XInputStream >
320 open( sal_Int32 CommandId,
321 const OUString& aUnqPath,
322 bool bLock );
326 * Given a file URL aUnqPath, this methods returns a XStream which can be used
327 * to read and write from/to the file.
330 css::uno::Reference< css::io::XStream >
331 open_rw( sal_Int32 CommandId,
332 const OUString& aUnqPath,
333 bool bLock );
337 * This method returns the result set containing the children of the directory belonging
338 * to file URL aUnqPath
341 css::uno::Reference< css::ucb::XDynamicResultSet >
342 ls( sal_Int32 CommandId,
343 const OUString& aUnqPath,
344 const sal_Int32 OpenMode,
345 const css::uno::Sequence< css::beans::Property >& sProperty,
346 const css::uno::Sequence< css::ucb::NumberedSortingInfo > & sSortingInfo );
350 * Info methods
353 // Info for commands
354 css::uno::Reference< css::ucb::XCommandInfo >
355 info_c();
357 // Info for the properties
358 css::uno::Reference< css::beans::XPropertySetInfo >
359 info_p( const OUString& aUnqPath );
363 * Sets the values of the properties belonging to fileURL aUnqPath
366 css::uno::Sequence< css::uno::Any >
367 setv( const OUString& aUnqPath,
368 const css::uno::Sequence< css::beans::PropertyValue >& values );
372 * Reads the values of the properties belonging to fileURL aUnqPath;
373 * Returns an XRow object containing the values in the requested order.
376 css::uno::Reference< css::sdbc::XRow >
377 getv( sal_Int32 CommandId,
378 const OUString& aUnqPath,
379 const css::uno::Sequence< css::beans::Property >& properties );
382 /********************************************************************************/
383 /* transfer-commands */
384 /********************************************************************************/
387 * Moves the content belonging to fileURL srcUnqPath to fileURL dstUnqPath( files and directories )
390 void
391 move( sal_Int32 CommandId,
392 const OUString& srcUnqPath, // Full file(folder)-path
393 const OUString& dstUnqPath, // Path to the destination-directory
394 const sal_Int32 NameClash );
397 * Copies the content belonging to fileURL srcUnqPath to fileURL dstUnqPath ( files and directories )
400 void
401 copy( sal_Int32 CommandId, // See "move"
402 const OUString& srcUnqPath,
403 const OUString& dstUnqPath,
404 sal_Int32 NameClash );
406 enum class FileUrlType { Folder = 1, File = -1, Unknown = 0 };
409 * Deletes the content belonging to fileURL aUnqPath( recursively in case of directory )
412 bool
413 remove( sal_Int32 CommandId,
414 const OUString& aUnqPath,
415 FileUrlType eTypeToMove = FileUrlType::Unknown,
416 bool MustExist = true );
419 /********************************************************************************/
420 /* write and create - commandos */
421 /********************************************************************************/
424 * Creates new directory with given URL, recursively if necessary
425 * Return:: success of operation
428 bool
429 mkdir( sal_Int32 CommandId,
430 const OUString& aDirectoryName,
431 bool OverWrite );
435 * Creates new file with given URL.
436 * The content of aInputStream becomes the content of the file
437 * Return:: success of operation
440 bool
441 mkfil( sal_Int32 CommandId,
442 const OUString& aFileName,
443 bool OverWrite,
444 const css::uno::Reference< css::io::XInputStream >& aInputStream );
448 * writes to the file with given URL.
449 * The content of aInputStream becomes the content of the file
450 * Return:: success of operation
452 bool
453 write( sal_Int32 CommandId,
454 const OUString& aUnqPath,
455 bool OverWrite,
456 const css::uno::Reference< css::io::XInputStream >& aInputStream );
459 void insertDefaultProperties( const OUString& aUnqPath );
461 static css::uno::Sequence< css::ucb::ContentInfo >
462 queryCreatableContentsInfo();
465 /******************************************************************************/
466 /* */
467 /* mapping of file urls */
468 /* to uncpath and vice versa */
469 /* */
470 /******************************************************************************/
472 static bool getUnqFromUrl( const OUString& Url, OUString& Unq );
474 static bool getUrlFromUnq( const OUString& Unq, OUString& Url );
477 FileProvider* m_pProvider;
478 css::uno::Reference< css::uno::XComponentContext > m_xContext;
479 css::uno::Reference< css::ucb::XPropertySetRegistry > m_xFileRegistry;
481 private:
483 void insertDefaultProperties( std::unique_lock<std::mutex>& rGuard, const OUString& aUnqPath );
485 /********************************************************************************/
486 /* get eventListeners */
487 /********************************************************************************/
489 std::vector< ContentEventNotifier >
490 getContentEventListeners( const OUString& aName );
492 std::vector< ContentEventNotifier >
493 getContentDeletedEventListeners( const OUString& aName );
495 std::vector< ContentEventNotifier >
496 getContentExchangedEventListeners( const OUString& aOldPrefix,
497 const OUString& aNewPrefix,
498 bool withChildren );
500 std::vector< PropertyChangeNotifier >
501 getPropertyChangeNotifier( const OUString& aName );
503 std::vector< PropertySetInfoChangeNotifier >
504 getPropertySetListeners( const OUString& aName );
507 /********************************************************************************/
508 /* notify eventListeners */
509 /********************************************************************************/
511 static void notifyPropertyChanges(
512 const std::vector<PropertyChangeNotifier>& listeners,
513 const css::uno::Sequence<css::beans::PropertyChangeEvent>& seqChanged);
515 static void notifyContentExchanged(
516 const std::vector<ContentEventNotifier>& listeners_vec);
518 static void
519 notifyInsert(const std::vector<ContentEventNotifier>& listeners,
520 const OUString& aChildName);
522 static void
523 notifyContentDeleted(const std::vector<ContentEventNotifier>& listeners);
525 static void
526 notifyContentRemoved(const std::vector<ContentEventNotifier>& listeners,
527 const OUString& aChildName);
529 static void notifyPropertyAdded(
530 const std::vector<PropertySetInfoChangeNotifier>& listeners,
531 const OUString& aPropertyName);
533 static void notifyPropertyRemoved(
534 const std::vector<PropertySetInfoChangeNotifier>& listeners,
535 const OUString& aPropertyName);
537 /********************************************************************************/
538 /* remove persistent propertyset */
539 /********************************************************************************/
541 void erasePersistentSetWithoutChildren( const OUString& aUnqPath );
542 void erasePersistentSet( const OUString& aUnqPath,
543 bool withChildren = false );
545 /********************************************************************************/
546 /* copy persistent propertyset */
547 /* from srcUnqPath to dstUnqPath */
548 /********************************************************************************/
550 void copyPersistentSetWithoutChildren( const OUString& srcUnqPath,
551 const OUString& dstUnqPath );
552 void copyPersistentSet( const OUString& srcUnqPath,
553 const OUString& dstUnqPath,
554 bool withChildren );
557 // Special optimized method for getting the properties of a directoryitem, which
558 // is returned by osl::DirectoryItem::getNextItem()
560 bool
561 getv( const css::uno::Sequence< css::beans::Property >& properties,
562 osl::DirectoryItem& DirItem,
563 OUString& aUnqPath,
564 bool& bIsRegular,
565 css::uno::Reference< css::sdbc::XRow > & row );
569 * Load the properties from configuration, if create == true create them.
570 * The Properties are stored under the url belonging to it->first.
573 void load( const TaskManager::ContentMap::iterator& it,
574 bool create );
577 * Commit inserts the determined properties in the filestatus object into
578 * the internal map, so that is possible to determine on a subsequent
579 * setting of file properties which properties have changed without filestat
582 void
583 commit(
584 std::unique_lock<std::mutex>& rGuard,
585 const TaskManager::ContentMap::iterator& it,
586 const osl::FileStatus& aFileStatus );
589 * Given a Sequence of properties seq, this method determines the mask
590 * used to instantiate an osl::FileStatus, so that a call to
591 * osl::DirectoryItem::getFileStatus fills the required fields.
594 static void
595 getMaskFromProperties(
596 sal_Int32& n_Mask,
597 const css::uno::Sequence< css::beans::Property >& seq );
600 // Helper function for public copy
602 osl::FileBase::RC
603 copy_recursive(
604 const OUString& srcUnqPath,
605 const OUString& dstUnqPath,
606 FileUrlType TypeToCopy,
607 bool testExistence );
610 // Helper function for mkfil,mkdir and write
611 // Creates whole path
612 // returns success of the operation
613 // The call determines the errorCode, which should be used to install
614 // any error
616 bool
617 ensuredir( sal_Int32 CommandId,
618 const OUString& aDirectoryName,
619 sal_Int32 errorCode );
621 // General
622 ContentMap m_aContent;
625 public:
627 static constexpr OUStringLiteral FolderContentType =
628 u"application/vnd.sun.staroffice.fsys-folder";
629 static constexpr OUStringLiteral FileContentType =
630 u"application/vnd.sun.staroffice.fsys-file";
633 private:
635 PropertySet m_aDefaultProperties;
636 css::uno::Sequence< css::ucb::CommandInfo > m_sCommandInfo;
638 public:
639 // Miscellaneous:
640 // Methods for "writeComponentInfo" and "createComponentFactory"
642 static void getScheme( OUString& Scheme );
645 } // end namespace TaskHandling
647 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */