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