1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
21 #include "sdbcoretools.hxx"
22 #include "dbastrings.hrc"
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>
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>
40 //.........................................................................
43 //.........................................................................
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
;
56 // =========================================================================
57 // -------------------------------------------------------------------------
58 void notifyDataSourceModified(const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>& _rxObject
,sal_Bool _bModified
)
60 Reference
< XInterface
> xDs
= getDataSource( _rxObject
);
61 Reference
<XDocumentDataSource
> xDocumentDataSource(xDs
,UNO_QUERY
);
62 if ( xDocumentDataSource
.is() )
63 xDs
= xDocumentDataSource
->getDatabaseDocument();
64 Reference
< XModifiable
> xModi( xDs
, UNO_QUERY
);
66 xModi
->setModified(_bModified
);
69 // -------------------------------------------------------------------------
70 Reference
< XInterface
> getDataSource( const Reference
< XInterface
>& _rxDependentObject
)
72 Reference
< XInterface
> xParent
= _rxDependentObject
;
73 Reference
< XInterface
> xReturn
;
77 Reference
<XChild
> xChild(xParent
,UNO_QUERY
);
78 xParent
.set(xChild
.is() ? xChild
->getParent() : Reference
< XInterface
>(),UNO_QUERY
);
83 // -----------------------------------------------------------------------------
84 ::rtl::OUString
extractExceptionMessage( const ::comphelper::ComponentContext
& _rContext
, const Any
& _rError
)
86 ::rtl::OUString sDisplayMessage
;
90 Reference
< XInteractionRequestStringResolver
> xStringResolver
;
91 if ( _rContext
.createComponent( "com.sun.star.task.InteractionRequestStringResolver", xStringResolver
) )
93 ::rtl::Reference
< ::comphelper::OInteractionRequest
> pRequest( new ::comphelper::OInteractionRequest( _rError
) );
94 ::rtl::Reference
< ::comphelper::OInteractionApprove
> pApprove( new ::comphelper::OInteractionApprove
);
95 pRequest
->addContinuation( pApprove
.get() );
96 Optional
< ::rtl::OUString
> aMessage
= xStringResolver
->getStringFromInformationalRequest( pRequest
.get() );
97 if ( aMessage
.IsPresent
)
98 sDisplayMessage
= aMessage
.Value
;
101 catch( const Exception
& )
103 DBG_UNHANDLED_EXCEPTION();
106 if ( sDisplayMessage
.isEmpty() )
111 ::rtl::OUStringBuffer aBuffer
;
112 aBuffer
.append( _rError
.getValueTypeName() );
113 aBuffer
.appendAscii( ":\n" );
114 aBuffer
.append( aExcept
.Message
);
116 sDisplayMessage
= aBuffer
.makeStringAndClear();
119 return sDisplayMessage
;
122 namespace tools
{ namespace stor
{
124 // -----------------------------------------------------------------------------
125 bool storageIsWritable_nothrow( const Reference
< XStorage
>& _rxStorage
)
127 if ( !_rxStorage
.is() )
130 sal_Int32 nMode
= ElementModes::READ
;
133 Reference
< XPropertySet
> xStorageProps( _rxStorage
, UNO_QUERY_THROW
);
134 xStorageProps
->getPropertyValue(
135 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "OpenMode" ) ) ) >>= nMode
;
137 catch( const Exception
& )
139 DBG_UNHANDLED_EXCEPTION();
141 return ( nMode
& ElementModes::WRITE
) != 0;
144 // -----------------------------------------------------------------------------
145 bool commitStorageIfWriteable( const Reference
< XStorage
>& _rxStorage
) SAL_THROW(( IOException
, WrappedTargetException
, RuntimeException
))
147 bool bSuccess
= false;
148 Reference
< XTransactedObject
> xTrans( _rxStorage
, UNO_QUERY
);
151 if ( storageIsWritable_nothrow( _rxStorage
) )
160 //.........................................................................
161 } // namespace dbaccess
162 //.........................................................................
164 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */