Branch data 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 : : #ifndef _SCRIPT_FRAMEWORK_MISCUTILS_HXX_
21 : : #define _SCRIPT_FRAMEWORK_MISCUTILS_HXX_
22 : :
23 : : #include <rtl/ustring.hxx>
24 : : #include <tools/urlobj.hxx>
25 : :
26 : : #include <ucbhelper/content.hxx>
27 : : #include <com/sun/star/uno/XComponentContext.hpp>
28 : : #include <com/sun/star/frame/XModel.hpp>
29 : : #include <com/sun/star/frame/XTransientDocumentsDocumentContentFactory.hpp>
30 : : #include <com/sun/star/beans/XPropertySet.hpp>
31 : : #include <com/sun/star/container/XContentEnumerationAccess.hpp>
32 : : #include <com/sun/star/ucb/XCommandEnvironment.hpp>
33 : : #include <com/sun/star/ucb/SimpleFileAccess.hpp>
34 : : #include <com/sun/star/ucb/XSimpleFileAccess2.hpp>
35 : : #include <com/sun/star/ucb/XContentAccess.hpp>
36 : : #include <com/sun/star/sdbc/XResultSet.hpp>
37 : : #include <com/sun/star/sdbc/XRow.hpp>
38 : : #include <com/sun/star/lang/XMultiComponentFactory.hpp>
39 : :
40 : :
41 : : #include "util.hxx"
42 : :
43 : : namespace css = ::com::sun::star;
44 : :
45 : : namespace sf_misc
46 : : {
47 : :
48 : : class MiscUtils
49 : : {
50 : : public:
51 : 0 : static css::uno::Sequence< ::rtl::OUString > allOpenTDocUrls( const css::uno::Reference< css::uno::XComponentContext >& xCtx)
52 : : {
53 : 0 : css::uno::Sequence< ::rtl::OUString > result;
54 : : try
55 : : {
56 [ # # ]: 0 : if ( !xCtx.is() )
57 : : {
58 : : return result;
59 : : }
60 [ # # ]: 0 : css::uno::Reference < css::ucb::XSimpleFileAccess2 > xSFA( css::ucb::SimpleFileAccess::create(xCtx) );
61 [ # # ]: 0 : if ( xSFA.is() )
62 : : {
63 [ # # ][ # # ]: 0 : result = xSFA->getFolderContents( OUSTR("vnd.sun.star.tdoc:/"), true );
[ # # ][ # # ]
[ # # ]
64 [ # # ]: 0 : }
65 : : }
66 [ # # ]: 0 : catch ( css::uno::Exception& )
67 : : {
68 : : }
69 : 0 : return result;
70 : : }
71 : :
72 : 32 : static ::rtl::OUString xModelToTdocUrl( const css::uno::Reference< css::frame::XModel >& xModel,
73 : : const css::uno::Reference< css::uno::XComponentContext >& xContext )
74 : : {
75 : : css::uno::Reference< css::lang::XMultiComponentFactory > xMCF(
76 [ + - ][ + - ]: 32 : xContext->getServiceManager() );
77 : : css::uno::Reference<
78 : 32 : css::frame::XTransientDocumentsDocumentContentFactory > xDocFac;
79 : : try
80 : : {
81 : : xDocFac =
82 : : css::uno::Reference<
83 : : css::frame::XTransientDocumentsDocumentContentFactory >(
84 [ + - ]: 32 : xMCF->createInstanceWithContext(
85 : : rtl::OUString(
86 : : RTL_CONSTASCII_USTRINGPARAM(
87 : : "com.sun.star.frame.TransientDocumentsDocumentContentFactory" ) ),
88 : 32 : xContext ),
89 [ + - ][ + - ]: 32 : css::uno::UNO_QUERY );
[ + - ][ + - ]
[ # # ]
90 : : }
91 [ # # ]: 0 : catch ( css::uno::Exception const & )
92 : : {
93 : : // handled below
94 : : }
95 : :
96 [ + - ]: 32 : if ( xDocFac.is() )
97 : : {
98 : : try
99 : : {
100 : : css::uno::Reference< css::ucb::XContent > xContent(
101 [ + - ][ + - ]: 32 : xDocFac->createDocumentContent( xModel ) );
102 [ + - ][ + - ]: 32 : return xContent->getIdentifier()->getContentIdentifier();
[ + - ][ + - ]
[ # # ]
103 : : }
104 [ # # ]: 0 : catch ( css::lang::IllegalArgumentException const & )
105 : : {
106 : : OSL_FAIL( "Invalid document model!" );
107 : : }
108 : : }
109 : :
110 : : OSL_FAIL( "Unable to obtain URL for document model!" );
111 : 32 : return rtl::OUString();
112 : : }
113 : 45 : static css::uno::Reference< css::frame::XModel > tDocUrlToModel( const ::rtl::OUString& url )
114 : : {
115 : 45 : css::uno::Any result;
116 : :
117 : : try
118 : : {
119 [ + - ][ + - ]: 45 : ::ucbhelper::Content root( url, NULL );
120 [ + - ]: 45 : ::rtl::OUString propName = OUSTR("DocumentModel");
121 [ + - ][ + - ]: 45 : result = getUCBProperty( root, propName );
[ # # # ]
122 : : }
123 [ # # ]: 0 : catch ( css::ucb::ContentCreationException& )
124 : : {
125 : : // carry on, empty value will be returned
126 : : }
127 [ # # ]: 0 : catch ( css::uno::RuntimeException& )
128 : : {
129 : : // carry on, empty value will be returned
130 : : }
131 : :
132 : : css::uno::Reference< css::frame::XModel > xModel(
133 [ + - ]: 45 : result, css::uno::UNO_QUERY );
134 : :
135 : 45 : return xModel;
136 : : }
137 : :
138 : :
139 : 45 : static css::uno::Any getUCBProperty( ::ucbhelper::Content& content, ::rtl::OUString& prop )
140 : : {
141 : 45 : css::uno::Any result;
142 : : try
143 : : {
144 [ + - ]: 45 : result = content.getPropertyValue( prop );
145 : : }
146 [ # # ]: 0 : catch ( css::uno::Exception& )
147 : : {
148 : : }
149 [ # # ]: 45 : return result;
150 : : }
151 : :
152 : : private:
153 : : static ::rtl::OUString parseLocationName( const ::rtl::OUString& location )
154 : : {
155 : : // strip out the last leaf of location name
156 : : // e.g. file://dir1/dir2/Blah.sxw - > Blah.sxw
157 : : ::rtl::OUString temp = location;
158 : : INetURLObject aURLObj( temp );
159 : : if ( !aURLObj.HasError() )
160 : : temp = aURLObj.getName( INetURLObject::LAST_SEGMENT, true, INetURLObject::DECODE_WITH_CHARSET );
161 : : return temp;
162 : : }
163 : :
164 : : };
165 : : } // namespace sf_misc
166 : : #endif //
167 : :
168 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|