1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: MiscUtils.hxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef _SCRIPT_FRAMEWORK_MISCUTILS_HXX_
32 #define _SCRIPT_FRAMEWORK_MISCUTILS_HXX_
34 #include <rtl/ustring.hxx>
35 #include <tools/urlobj.hxx>
37 #include <ucbhelper/content.hxx>
38 #include <com/sun/star/uno/XComponentContext.hpp>
39 #include <com/sun/star/frame/XModel.hpp>
40 #include <com/sun/star/frame/XTransientDocumentsDocumentContentFactory.hpp>
41 #include <com/sun/star/beans/XPropertySet.hpp>
42 #include <com/sun/star/container/XContentEnumerationAccess.hpp>
43 #include <com/sun/star/ucb/XCommandEnvironment.hpp>
44 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
45 #include <com/sun/star/ucb/XContentAccess.hpp>
46 #include <com/sun/star/sdbc/XResultSet.hpp>
47 #include <com/sun/star/sdbc/XRow.hpp>
48 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
56 #define css ::com::sun::star
61 static css::uno::Sequence
< ::rtl::OUString
> allOpenTDocUrls( const css::uno::Reference
< css::uno::XComponentContext
>& xCtx
)
63 css::uno::Sequence
< ::rtl::OUString
> result
;
70 css::uno::Reference
< css::lang::XMultiComponentFactory
> xFac( xCtx
->getServiceManager(), css::uno::UNO_QUERY
);
73 css::uno::Reference
< com::sun::star::ucb::XSimpleFileAccess
> xSFA( xFac
->createInstanceWithContext( OUSTR("com.sun.star.ucb.SimpleFileAccess"), xCtx
), css::uno::UNO_QUERY
);
76 result
= xSFA
->getFolderContents( OUSTR("vnd.sun.star.tdoc:/"), true );
80 catch ( css::uno::Exception
& )
86 static ::rtl::OUString
xModelToTdocUrl( const css::uno::Reference
< css::frame::XModel
>& xModel
,
87 const css::uno::Reference
< css::uno::XComponentContext
>& xContext
)
89 css::uno::Reference
< css::lang::XMultiComponentFactory
> xMCF(
90 xContext
->getServiceManager() );
92 css::frame::XTransientDocumentsDocumentContentFactory
> xDocFac
;
97 css::frame::XTransientDocumentsDocumentContentFactory
>(
98 xMCF
->createInstanceWithContext(
100 RTL_CONSTASCII_USTRINGPARAM(
101 "com.sun.star.frame.TransientDocumentsDocumentContentFactory" ) ),
103 css::uno::UNO_QUERY
);
105 catch ( css::uno::Exception
const & )
114 css::uno::Reference
< css::ucb::XContent
> xContent(
115 xDocFac
->createDocumentContent( xModel
) );
116 return xContent
->getIdentifier()->getContentIdentifier();
118 catch ( css::lang::IllegalArgumentException
const & )
120 OSL_ENSURE( false, "Invalid document model!" );
124 OSL_ENSURE( false, "Unable to obtain URL for document model!" );
125 return rtl::OUString();
127 static css::uno::Reference
< css::frame::XModel
> tDocUrlToModel( const ::rtl::OUString
& url
)
129 css::uno::Any result
;
133 ::ucbhelper::Content
root( url
, NULL
);
134 ::rtl::OUString propName
= OUSTR("DocumentModel");
135 result
= getUCBProperty( root
, propName
);
137 catch ( css::ucb::ContentCreationException
& )
139 // carry on, empty value will be returned
141 catch ( css::uno::RuntimeException
& )
143 // carry on, empty value will be returned
146 css::uno::Reference
< css::frame::XModel
> xModel(
147 result
, css::uno::UNO_QUERY
);
153 static css::uno::Any
getUCBProperty( ::ucbhelper::Content
& content
, ::rtl::OUString
& prop
)
155 css::uno::Any result
;
158 result
= content
.getPropertyValue( prop
);
160 catch ( css::uno::Exception
& )
167 static ::rtl::OUString
parseLocationName( const ::rtl::OUString
& location
)
169 // strip out the last leaf of location name
170 // e.g. file://dir1/dir2/Blah.sxw - > Blah.sxw
171 ::rtl::OUString temp
= location
;
172 INetURLObject
aURLObj( temp
);
173 if ( !aURLObj
.HasError() )
174 temp
= aURLObj
.getName( INetURLObject::LAST_SEGMENT
, true, INetURLObject::DECODE_WITH_CHARSET
);
179 } // namespace sf_misc