1*6998d047SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*6998d047SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*6998d047SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*6998d047SAndrew Rist * distributed with this work for additional information
6*6998d047SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*6998d047SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*6998d047SAndrew Rist * "License"); you may not use this file except in compliance
9*6998d047SAndrew Rist * with the License. You may obtain a copy of the License at
10*6998d047SAndrew Rist *
11*6998d047SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*6998d047SAndrew Rist *
13*6998d047SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*6998d047SAndrew Rist * software distributed under the License is distributed on an
15*6998d047SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*6998d047SAndrew Rist * KIND, either express or implied. See the License for the
17*6998d047SAndrew Rist * specific language governing permissions and limitations
18*6998d047SAndrew Rist * under the License.
19*6998d047SAndrew Rist *
20*6998d047SAndrew Rist *************************************************************/
21*6998d047SAndrew Rist
22*6998d047SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir #ifndef _SCRIPT_FRAMEWORK_MISCUTILS_HXX_
25cdf0e10cSrcweir #define _SCRIPT_FRAMEWORK_MISCUTILS_HXX_
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include <rtl/ustring.hxx>
28cdf0e10cSrcweir #include <tools/urlobj.hxx>
29cdf0e10cSrcweir
30cdf0e10cSrcweir #include <ucbhelper/content.hxx>
31cdf0e10cSrcweir #include <com/sun/star/uno/XComponentContext.hpp>
32cdf0e10cSrcweir #include <com/sun/star/frame/XModel.hpp>
33cdf0e10cSrcweir #include <com/sun/star/frame/XTransientDocumentsDocumentContentFactory.hpp>
34cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
35cdf0e10cSrcweir #include <com/sun/star/container/XContentEnumerationAccess.hpp>
36cdf0e10cSrcweir #include <com/sun/star/ucb/XCommandEnvironment.hpp>
37cdf0e10cSrcweir #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
38cdf0e10cSrcweir #include <com/sun/star/ucb/XContentAccess.hpp>
39cdf0e10cSrcweir #include <com/sun/star/sdbc/XResultSet.hpp>
40cdf0e10cSrcweir #include <com/sun/star/sdbc/XRow.hpp>
41cdf0e10cSrcweir #include <com/sun/star/lang/XMultiComponentFactory.hpp>
42cdf0e10cSrcweir
43cdf0e10cSrcweir
44cdf0e10cSrcweir #include "util.hxx"
45cdf0e10cSrcweir
46cdf0e10cSrcweir namespace sf_misc
47cdf0e10cSrcweir {
48cdf0e10cSrcweir // for simplification
49cdf0e10cSrcweir #define css ::com::sun::star
50cdf0e10cSrcweir
51cdf0e10cSrcweir class MiscUtils
52cdf0e10cSrcweir {
53cdf0e10cSrcweir public:
allOpenTDocUrls(const css::uno::Reference<css::uno::XComponentContext> & xCtx)54cdf0e10cSrcweir static css::uno::Sequence< ::rtl::OUString > allOpenTDocUrls( const css::uno::Reference< css::uno::XComponentContext >& xCtx)
55cdf0e10cSrcweir {
56cdf0e10cSrcweir css::uno::Sequence< ::rtl::OUString > result;
57cdf0e10cSrcweir try
58cdf0e10cSrcweir {
59cdf0e10cSrcweir if ( !xCtx.is() )
60cdf0e10cSrcweir {
61cdf0e10cSrcweir return result;
62cdf0e10cSrcweir }
63cdf0e10cSrcweir css::uno::Reference < css::lang::XMultiComponentFactory > xFac( xCtx->getServiceManager(), css::uno::UNO_QUERY );
64cdf0e10cSrcweir if ( xFac.is() )
65cdf0e10cSrcweir {
66cdf0e10cSrcweir css::uno::Reference < com::sun::star::ucb::XSimpleFileAccess > xSFA( xFac->createInstanceWithContext( OUSTR("com.sun.star.ucb.SimpleFileAccess"), xCtx ), css::uno::UNO_QUERY );
67cdf0e10cSrcweir if ( xSFA.is() )
68cdf0e10cSrcweir {
69cdf0e10cSrcweir result = xSFA->getFolderContents( OUSTR("vnd.sun.star.tdoc:/"), true );
70cdf0e10cSrcweir }
71cdf0e10cSrcweir }
72cdf0e10cSrcweir }
73cdf0e10cSrcweir catch ( css::uno::Exception& )
74cdf0e10cSrcweir {
75cdf0e10cSrcweir }
76cdf0e10cSrcweir return result;
77cdf0e10cSrcweir }
78cdf0e10cSrcweir
xModelToTdocUrl(const css::uno::Reference<css::frame::XModel> & xModel,const css::uno::Reference<css::uno::XComponentContext> & xContext)79cdf0e10cSrcweir static ::rtl::OUString xModelToTdocUrl( const css::uno::Reference< css::frame::XModel >& xModel,
80cdf0e10cSrcweir const css::uno::Reference< css::uno::XComponentContext >& xContext )
81cdf0e10cSrcweir {
82cdf0e10cSrcweir css::uno::Reference< css::lang::XMultiComponentFactory > xMCF(
83cdf0e10cSrcweir xContext->getServiceManager() );
84cdf0e10cSrcweir css::uno::Reference<
85cdf0e10cSrcweir css::frame::XTransientDocumentsDocumentContentFactory > xDocFac;
86cdf0e10cSrcweir try
87cdf0e10cSrcweir {
88cdf0e10cSrcweir xDocFac =
89cdf0e10cSrcweir css::uno::Reference<
90cdf0e10cSrcweir css::frame::XTransientDocumentsDocumentContentFactory >(
91cdf0e10cSrcweir xMCF->createInstanceWithContext(
92cdf0e10cSrcweir rtl::OUString(
93cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM(
94cdf0e10cSrcweir "com.sun.star.frame.TransientDocumentsDocumentContentFactory" ) ),
95cdf0e10cSrcweir xContext ),
96cdf0e10cSrcweir css::uno::UNO_QUERY );
97cdf0e10cSrcweir }
98cdf0e10cSrcweir catch ( css::uno::Exception const & )
99cdf0e10cSrcweir {
100cdf0e10cSrcweir // handled below
101cdf0e10cSrcweir }
102cdf0e10cSrcweir
103cdf0e10cSrcweir if ( xDocFac.is() )
104cdf0e10cSrcweir {
105cdf0e10cSrcweir try
106cdf0e10cSrcweir {
107cdf0e10cSrcweir css::uno::Reference< css::ucb::XContent > xContent(
108cdf0e10cSrcweir xDocFac->createDocumentContent( xModel ) );
109cdf0e10cSrcweir return xContent->getIdentifier()->getContentIdentifier();
110cdf0e10cSrcweir }
111cdf0e10cSrcweir catch ( css::lang::IllegalArgumentException const & )
112cdf0e10cSrcweir {
113cdf0e10cSrcweir OSL_ENSURE( false, "Invalid document model!" );
114cdf0e10cSrcweir }
115cdf0e10cSrcweir }
116cdf0e10cSrcweir
117cdf0e10cSrcweir OSL_ENSURE( false, "Unable to obtain URL for document model!" );
118cdf0e10cSrcweir return rtl::OUString();
119cdf0e10cSrcweir }
tDocUrlToModel(const::rtl::OUString & url)120cdf0e10cSrcweir static css::uno::Reference< css::frame::XModel > tDocUrlToModel( const ::rtl::OUString& url )
121cdf0e10cSrcweir {
122cdf0e10cSrcweir css::uno::Any result;
123cdf0e10cSrcweir
124cdf0e10cSrcweir try
125cdf0e10cSrcweir {
126cdf0e10cSrcweir ::ucbhelper::Content root( url, NULL );
127cdf0e10cSrcweir ::rtl::OUString propName = OUSTR("DocumentModel");
128cdf0e10cSrcweir result = getUCBProperty( root, propName );
129cdf0e10cSrcweir }
130cdf0e10cSrcweir catch ( css::ucb::ContentCreationException& )
131cdf0e10cSrcweir {
132cdf0e10cSrcweir // carry on, empty value will be returned
133cdf0e10cSrcweir }
134cdf0e10cSrcweir catch ( css::uno::RuntimeException& )
135cdf0e10cSrcweir {
136cdf0e10cSrcweir // carry on, empty value will be returned
137cdf0e10cSrcweir }
138cdf0e10cSrcweir
139cdf0e10cSrcweir css::uno::Reference< css::frame::XModel > xModel(
140cdf0e10cSrcweir result, css::uno::UNO_QUERY );
141cdf0e10cSrcweir
142cdf0e10cSrcweir return xModel;
143cdf0e10cSrcweir }
144cdf0e10cSrcweir
145cdf0e10cSrcweir
getUCBProperty(::ucbhelper::Content & content,::rtl::OUString & prop)146cdf0e10cSrcweir static css::uno::Any getUCBProperty( ::ucbhelper::Content& content, ::rtl::OUString& prop )
147cdf0e10cSrcweir {
148cdf0e10cSrcweir css::uno::Any result;
149cdf0e10cSrcweir try
150cdf0e10cSrcweir {
151cdf0e10cSrcweir result = content.getPropertyValue( prop );
152cdf0e10cSrcweir }
153cdf0e10cSrcweir catch ( css::uno::Exception& )
154cdf0e10cSrcweir {
155cdf0e10cSrcweir }
156cdf0e10cSrcweir return result;
157cdf0e10cSrcweir }
158cdf0e10cSrcweir
159cdf0e10cSrcweir private:
parseLocationName(const::rtl::OUString & location)160cdf0e10cSrcweir static ::rtl::OUString parseLocationName( const ::rtl::OUString& location )
161cdf0e10cSrcweir {
162cdf0e10cSrcweir // strip out the last leaf of location name
163cdf0e10cSrcweir // e.g. file://dir1/dir2/Blah.sxw - > Blah.sxw
164cdf0e10cSrcweir ::rtl::OUString temp = location;
165cdf0e10cSrcweir INetURLObject aURLObj( temp );
166cdf0e10cSrcweir if ( !aURLObj.HasError() )
167cdf0e10cSrcweir temp = aURLObj.getName( INetURLObject::LAST_SEGMENT, true, INetURLObject::DECODE_WITH_CHARSET );
168cdf0e10cSrcweir return temp;
169cdf0e10cSrcweir }
170cdf0e10cSrcweir
171cdf0e10cSrcweir };
172cdf0e10cSrcweir } // namespace sf_misc
173cdf0e10cSrcweir #endif //
174