LibreOffice Module dbaccess (master) 1
sdbcoretools.cxx
Go to the documentation of this file.
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 <sdbcoretools.hxx>
21
22#include <com/sun/star/beans/XPropertySet.hpp>
23#include <com/sun/star/container/XChild.hpp>
24#include <com/sun/star/util/XModifiable.hpp>
25#include <com/sun/star/sdb/XDocumentDataSource.hpp>
26#include <com/sun/star/task/InteractionRequestStringResolver.hpp>
27#include <com/sun/star/embed/XTransactedObject.hpp>
28#include <com/sun/star/embed/ElementModes.hpp>
29
32#include <rtl/ref.hxx>
33
34namespace dbaccess
35{
36
37 using namespace ::com::sun::star::uno;
38 using namespace ::com::sun::star::lang;
39 using namespace ::com::sun::star::util;
40 using namespace ::com::sun::star::io;
41 using namespace ::com::sun::star::sdbc;
42 using namespace ::com::sun::star::sdb;
43 using namespace ::com::sun::star::beans;
44 using namespace ::com::sun::star::task;
45 using namespace ::com::sun::star::embed;
46 using namespace ::com::sun::star::container;
47
48 void notifyDataSourceModified(const css::uno::Reference< css::uno::XInterface >& _rxObject)
49 {
50 Reference< XInterface > xDs = getDataSource( _rxObject );
51 Reference<XDocumentDataSource> xDocumentDataSource(xDs,UNO_QUERY);
52 if ( xDocumentDataSource.is() )
53 xDs = xDocumentDataSource->getDatabaseDocument();
54 Reference< XModifiable > xModi( xDs, UNO_QUERY );
55 if ( xModi.is() )
56 xModi->setModified(true);
57 }
58
59 Reference< XInterface > getDataSource( const Reference< XInterface >& _rxDependentObject )
60 {
61 Reference< XInterface > xParent = _rxDependentObject;
62 Reference< XInterface > xReturn;
63 while( xParent.is() )
64 {
65 xReturn = xParent;
66 Reference<XChild> xChild(xParent,UNO_QUERY);
67 xParent.set(xChild.is() ? xChild->getParent() : Reference< XInterface >(),UNO_QUERY);
68 }
69 return xReturn;
70 }
71
72 OUString extractExceptionMessage( const Reference<XComponentContext> & _rContext, const Any& _rError )
73 {
74 OUString sDisplayMessage;
75
76 try
77 {
78 Reference< XInteractionRequestStringResolver > xStringResolver = InteractionRequestStringResolver::create(_rContext);
79
80 ::rtl::Reference pRequest( new ::comphelper::OInteractionRequest( _rError ) );
81 ::rtl::Reference pApprove( new ::comphelper::OInteractionApprove );
82 pRequest->addContinuation( pApprove );
83 Optional< OUString > aMessage = xStringResolver->getStringFromInformationalRequest( pRequest );
84 if ( aMessage.IsPresent )
85 sDisplayMessage = aMessage.Value;
86 }
87 catch( const Exception& )
88 {
89 DBG_UNHANDLED_EXCEPTION("dbaccess");
90 }
91
92 if ( sDisplayMessage.isEmpty() )
93 {
94 Exception aExcept;
95 _rError >>= aExcept;
96
97 sDisplayMessage = _rError.getValueTypeName() +
98 ":\n" +
99 aExcept.Message;
100 }
101
102 return sDisplayMessage;
103 }
104
105 namespace tools::stor {
106
107 bool storageIsWritable_nothrow( const Reference< XStorage >& _rxStorage )
108 {
109 if ( !_rxStorage.is() )
110 return false;
111
112 sal_Int32 nMode = ElementModes::READ;
113 try
114 {
115 Reference< XPropertySet > xStorageProps( _rxStorage, UNO_QUERY_THROW );
116 xStorageProps->getPropertyValue( "OpenMode" ) >>= nMode;
117 }
118 catch( const Exception& )
119 {
120 DBG_UNHANDLED_EXCEPTION("dbaccess");
121 }
122 return ( nMode & ElementModes::WRITE ) != 0;
123 }
124
125 bool commitStorageIfWriteable( const Reference< XStorage >& _rxStorage )
126 {
127 bool bSuccess = false;
128 Reference< XTransactedObject > xTrans( _rxStorage, UNO_QUERY );
129 if ( xTrans.is() )
130 {
131 if ( storageIsWritable_nothrow( _rxStorage ) )
132 xTrans->commit();
133 bSuccess = true;
134 }
135 return bSuccess;
136 }
137
138}
139
140} // namespace dbaccess
141
142/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define DBG_UNHANDLED_EXCEPTION(...)
@ Exception
bool storageIsWritable_nothrow(const css::uno::Reference< css::embed::XStorage > &_rxStorage)
bool commitStorageIfWriteable(const css::uno::Reference< css::embed::XStorage > &_rxStorage)
commits a given storage if it's not readonly
css::uno::Reference< css::uno::XInterface > getDataSource(const css::uno::Reference< css::uno::XInterface > &_rxDependentObject)
void notifyDataSourceModified(const css::uno::Reference< css::uno::XInterface > &_rxObject)
OUString extractExceptionMessage(const css::uno::Reference< css::uno::XComponentContext > &_rContext, const css::uno::Any &_rError)
retrieves a to-be-displayed string for a given caught exception;