Line data Source code
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 :
21 : #include "sdbcoretools.hxx"
22 : #include "dbastrings.hrc"
23 :
24 : #include <com/sun/star/beans/XPropertySet.hpp>
25 : #include <com/sun/star/beans/PropertyValue.hpp>
26 : #include <com/sun/star/container/XChild.hpp>
27 : #include <com/sun/star/util/XModifiable.hpp>
28 : #include <com/sun/star/sdb/XDocumentDataSource.hpp>
29 : #include <com/sun/star/task/XInteractionRequestStringResolver.hpp>
30 : #include <com/sun/star/embed/XTransactedObject.hpp>
31 : #include <com/sun/star/embed/ElementModes.hpp>
32 :
33 : #include <tools/diagnose_ex.h>
34 : #include <tools/debug.hxx>
35 : #include <comphelper/componentcontext.hxx>
36 : #include <comphelper/interaction.hxx>
37 : #include <rtl/ref.hxx>
38 : #include <rtl/ustrbuf.hxx>
39 :
40 : //.........................................................................
41 : namespace dbaccess
42 : {
43 : //.........................................................................
44 :
45 : using namespace ::com::sun::star::uno;
46 : using namespace ::com::sun::star::lang;
47 : using namespace ::com::sun::star::util;
48 : using namespace ::com::sun::star::io;
49 : using namespace ::com::sun::star::sdbc;
50 : using namespace ::com::sun::star::sdb;
51 : using namespace ::com::sun::star::beans;
52 : using namespace ::com::sun::star::task;
53 : using namespace ::com::sun::star::embed;
54 : using namespace ::com::sun::star::container;
55 :
56 : // =========================================================================
57 : // -------------------------------------------------------------------------
58 0 : void notifyDataSourceModified(const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxObject,sal_Bool _bModified)
59 : {
60 0 : Reference< XInterface > xDs = getDataSource( _rxObject );
61 0 : Reference<XDocumentDataSource> xDocumentDataSource(xDs,UNO_QUERY);
62 0 : if ( xDocumentDataSource.is() )
63 0 : xDs = xDocumentDataSource->getDatabaseDocument();
64 0 : Reference< XModifiable > xModi( xDs, UNO_QUERY );
65 0 : if ( xModi.is() )
66 0 : xModi->setModified(_bModified);
67 0 : }
68 :
69 : // -------------------------------------------------------------------------
70 0 : Reference< XInterface > getDataSource( const Reference< XInterface >& _rxDependentObject )
71 : {
72 0 : Reference< XInterface > xParent = _rxDependentObject;
73 0 : Reference< XInterface > xReturn;
74 0 : while( xParent.is() )
75 : {
76 0 : xReturn = xParent;
77 0 : Reference<XChild> xChild(xParent,UNO_QUERY);
78 0 : xParent.set(xChild.is() ? xChild->getParent() : Reference< XInterface >(),UNO_QUERY);
79 0 : }
80 0 : return xReturn;
81 : }
82 :
83 : // -----------------------------------------------------------------------------
84 0 : ::rtl::OUString extractExceptionMessage( const ::comphelper::ComponentContext& _rContext, const Any& _rError )
85 : {
86 0 : ::rtl::OUString sDisplayMessage;
87 :
88 : try
89 : {
90 0 : Reference< XInteractionRequestStringResolver > xStringResolver;
91 0 : if ( _rContext.createComponent( "com.sun.star.task.InteractionRequestStringResolver", xStringResolver ) )
92 : {
93 0 : ::rtl::Reference< ::comphelper::OInteractionRequest > pRequest( new ::comphelper::OInteractionRequest( _rError ) );
94 0 : ::rtl::Reference< ::comphelper::OInteractionApprove > pApprove( new ::comphelper::OInteractionApprove );
95 0 : pRequest->addContinuation( pApprove.get() );
96 0 : Optional< ::rtl::OUString > aMessage = xStringResolver->getStringFromInformationalRequest( pRequest.get() );
97 0 : if ( aMessage.IsPresent )
98 0 : sDisplayMessage = aMessage.Value;
99 0 : }
100 : }
101 0 : catch( const Exception& )
102 : {
103 : DBG_UNHANDLED_EXCEPTION();
104 : }
105 :
106 0 : if ( sDisplayMessage.isEmpty() )
107 : {
108 0 : Exception aExcept;
109 0 : _rError >>= aExcept;
110 :
111 0 : ::rtl::OUStringBuffer aBuffer;
112 0 : aBuffer.append( _rError.getValueTypeName() );
113 0 : aBuffer.appendAscii( ":\n" );
114 0 : aBuffer.append( aExcept.Message );
115 :
116 0 : sDisplayMessage = aBuffer.makeStringAndClear();
117 : }
118 :
119 0 : return sDisplayMessage;
120 : }
121 :
122 : namespace tools { namespace stor {
123 :
124 : // -----------------------------------------------------------------------------
125 0 : bool storageIsWritable_nothrow( const Reference< XStorage >& _rxStorage )
126 : {
127 0 : if ( !_rxStorage.is() )
128 0 : return false;
129 :
130 0 : sal_Int32 nMode = ElementModes::READ;
131 : try
132 : {
133 0 : Reference< XPropertySet > xStorageProps( _rxStorage, UNO_QUERY_THROW );
134 0 : xStorageProps->getPropertyValue(
135 0 : ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "OpenMode" ) ) ) >>= nMode;
136 : }
137 0 : catch( const Exception& )
138 : {
139 : DBG_UNHANDLED_EXCEPTION();
140 : }
141 0 : return ( nMode & ElementModes::WRITE ) != 0;
142 : }
143 :
144 : // -----------------------------------------------------------------------------
145 0 : bool commitStorageIfWriteable( const Reference< XStorage >& _rxStorage ) SAL_THROW(( IOException, WrappedTargetException, RuntimeException ))
146 : {
147 0 : bool bSuccess = false;
148 0 : Reference< XTransactedObject > xTrans( _rxStorage, UNO_QUERY );
149 0 : if ( xTrans.is() )
150 : {
151 0 : if ( storageIsWritable_nothrow( _rxStorage ) )
152 0 : xTrans->commit();
153 0 : bSuccess = true;
154 : }
155 0 : return bSuccess;
156 : }
157 :
158 : } } // tools::stor
159 :
160 : //.........................................................................
161 : } // namespace dbaccess
162 : //.........................................................................
163 :
164 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|