1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_dbaccess.hxx"
26 
27 #include "sdbcoretools.hxx"
28 #include "dbastrings.hrc"
29 
30 /** === begin UNO includes === **/
31 #include <com/sun/star/beans/XPropertySet.hpp>
32 #include <com/sun/star/beans/PropertyValue.hpp>
33 #include <com/sun/star/container/XChild.hpp>
34 #include <com/sun/star/util/XModifiable.hpp>
35 #include <com/sun/star/sdb/XDocumentDataSource.hpp>
36 #include <com/sun/star/task/XInteractionRequestStringResolver.hpp>
37 #include <com/sun/star/embed/XTransactedObject.hpp>
38 #include <com/sun/star/embed/ElementModes.hpp>
39 /** === end UNO includes === **/
40 
41 #include <tools/diagnose_ex.h>
42 #include <tools/debug.hxx>
43 #include <comphelper/componentcontext.hxx>
44 #include <comphelper/interaction.hxx>
45 #include <rtl/ref.hxx>
46 #include <rtl/ustrbuf.hxx>
47 
48 //.........................................................................
49 namespace dbaccess
50 {
51 //.........................................................................
52 
53 	using namespace ::com::sun::star::uno;
54 	using namespace ::com::sun::star::lang;
55 	using namespace ::com::sun::star::util;
56 	using namespace ::com::sun::star::io;
57 	using namespace ::com::sun::star::sdbc;
58 	using namespace ::com::sun::star::sdb;
59 	using namespace ::com::sun::star::beans;
60 	using namespace ::com::sun::star::task;
61 	using namespace ::com::sun::star::embed;
62 	using namespace ::com::sun::star::container;
63 
64     // =========================================================================
65     // -------------------------------------------------------------------------
notifyDataSourceModified(const::com::sun::star::uno::Reference<::com::sun::star::uno::XInterface> & _rxObject,sal_Bool _bModified)66     void notifyDataSourceModified(const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxObject,sal_Bool _bModified)
67     {
68         Reference< XInterface > xDs = getDataSource( _rxObject );
69 		Reference<XDocumentDataSource> xDocumentDataSource(xDs,UNO_QUERY);
70 		if ( xDocumentDataSource.is() )
71             xDs = xDocumentDataSource->getDatabaseDocument();
72         Reference< XModifiable > xModi( xDs, UNO_QUERY );
73 		if ( xModi.is() )
74 			xModi->setModified(_bModified);
75     }
76 
77     // -------------------------------------------------------------------------
getDataSource(const Reference<XInterface> & _rxDependentObject)78     Reference< XInterface > getDataSource( const Reference< XInterface >& _rxDependentObject )
79     {
80 	    Reference< XInterface > xParent = _rxDependentObject;
81 	    Reference< XInterface > xReturn;
82 	    while( xParent.is() )
83 	    {
84 		    xReturn = xParent;
85 		    Reference<XChild> xChild(xParent,UNO_QUERY);
86 		    xParent.set(xChild.is() ? xChild->getParent() : Reference< XInterface >(),UNO_QUERY);
87 	    }
88         return xReturn;
89     }
90 
91 // -----------------------------------------------------------------------------
extractExceptionMessage(const::comphelper::ComponentContext & _rContext,const Any & _rError)92     ::rtl::OUString extractExceptionMessage( const ::comphelper::ComponentContext& _rContext, const Any& _rError )
93     {
94         ::rtl::OUString sDisplayMessage;
95 
96         try
97         {
98             Reference< XInteractionRequestStringResolver > xStringResolver;
99             if ( _rContext.createComponent( "com.sun.star.task.InteractionRequestStringResolver", xStringResolver ) )
100             {
101                 ::rtl::Reference< ::comphelper::OInteractionRequest > pRequest( new ::comphelper::OInteractionRequest( _rError ) );
102                 ::rtl::Reference< ::comphelper::OInteractionApprove > pApprove( new ::comphelper::OInteractionApprove );
103                 pRequest->addContinuation( pApprove.get() );
104                 Optional< ::rtl::OUString > aMessage = xStringResolver->getStringFromInformationalRequest( pRequest.get() );
105                 if ( aMessage.IsPresent )
106                     sDisplayMessage = aMessage.Value;
107             }
108         }
109         catch( const Exception& )
110         {
111             DBG_UNHANDLED_EXCEPTION();
112         }
113 
114         if ( !sDisplayMessage.getLength() )
115         {
116             Exception aExcept;
117             _rError >>= aExcept;
118 
119             ::rtl::OUStringBuffer aBuffer;
120             aBuffer.append( _rError.getValueTypeName() );
121             aBuffer.appendAscii( ":\n" );
122             aBuffer.append( aExcept.Message );
123 
124             sDisplayMessage = aBuffer.makeStringAndClear();
125         }
126 
127         return sDisplayMessage;
128     }
129 
130     namespace tools { namespace stor {
131 
132     // -----------------------------------------------------------------------------
storageIsWritable_nothrow(const Reference<XStorage> & _rxStorage)133     bool storageIsWritable_nothrow( const Reference< XStorage >& _rxStorage )
134     {
135         if ( !_rxStorage.is() )
136             return false;
137 
138         sal_Int32 nMode = ElementModes::READ;
139         try
140         {
141             Reference< XPropertySet > xStorageProps( _rxStorage, UNO_QUERY_THROW );
142 			xStorageProps->getPropertyValue(
143                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "OpenMode" ) ) ) >>= nMode;
144         }
145         catch( const Exception& )
146         {
147             DBG_UNHANDLED_EXCEPTION();
148         }
149         return ( nMode & ElementModes::WRITE ) != 0;
150     }
151 
152     // -----------------------------------------------------------------------------
commitStorageIfWriteable(const Reference<XStorage> & _rxStorage)153     bool commitStorageIfWriteable( const Reference< XStorage >& _rxStorage ) SAL_THROW(( IOException, WrappedTargetException, RuntimeException ))
154     {
155         bool bSuccess = false;
156 	    Reference< XTransactedObject > xTrans( _rxStorage, UNO_QUERY );
157 	    if ( xTrans.is() )
158         {
159             if ( storageIsWritable_nothrow( _rxStorage ) )
160                 xTrans->commit();
161             bSuccess = true;
162         }
163         return bSuccess;
164     }
165 
166     } } // tools::stor
167 
168 //.........................................................................
169 }	// namespace dbaccess
170 //.........................................................................
171 
172