xref: /AOO41X/main/scripting/source/inc/util/MiscUtils.hxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef _SCRIPT_FRAMEWORK_MISCUTILS_HXX_
29 #define _SCRIPT_FRAMEWORK_MISCUTILS_HXX_
30 
31 #include <rtl/ustring.hxx>
32 #include <tools/urlobj.hxx>
33 
34 #include <ucbhelper/content.hxx>
35 #include <com/sun/star/uno/XComponentContext.hpp>
36 #include <com/sun/star/frame/XModel.hpp>
37 #include <com/sun/star/frame/XTransientDocumentsDocumentContentFactory.hpp>
38 #include <com/sun/star/beans/XPropertySet.hpp>
39 #include <com/sun/star/container/XContentEnumerationAccess.hpp>
40 #include <com/sun/star/ucb/XCommandEnvironment.hpp>
41 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
42 #include <com/sun/star/ucb/XContentAccess.hpp>
43 #include <com/sun/star/sdbc/XResultSet.hpp>
44 #include <com/sun/star/sdbc/XRow.hpp>
45 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
46 
47 
48 #include "util.hxx"
49 
50 namespace sf_misc
51 {
52 // for simplification
53 #define css ::com::sun::star
54 
55 class MiscUtils
56 {
57 public:
58     static css::uno::Sequence< ::rtl::OUString > allOpenTDocUrls( const  css::uno::Reference< css::uno::XComponentContext >& xCtx)
59 {
60     css::uno::Sequence< ::rtl::OUString > result;
61     try
62     {
63         if ( !xCtx.is() )
64         {
65             return result;
66         }
67         css::uno::Reference < css::lang::XMultiComponentFactory > xFac( xCtx->getServiceManager(), css::uno::UNO_QUERY );
68         if ( xFac.is() )
69         {
70             css::uno::Reference < com::sun::star::ucb::XSimpleFileAccess > xSFA( xFac->createInstanceWithContext( OUSTR("com.sun.star.ucb.SimpleFileAccess"), xCtx ), css::uno::UNO_QUERY );
71             if ( xSFA.is() )
72             {
73                 result = xSFA->getFolderContents( OUSTR("vnd.sun.star.tdoc:/"), true );
74             }
75         }
76     }
77     catch ( css::uno::Exception& )
78     {
79     }
80     return result;
81 }
82 
83     static ::rtl::OUString xModelToTdocUrl( const css::uno::Reference< css::frame::XModel >& xModel,
84                                             const css::uno::Reference< css::uno::XComponentContext >& xContext )
85 {
86     css::uno::Reference< css::lang::XMultiComponentFactory > xMCF(
87         xContext->getServiceManager() );
88     css::uno::Reference<
89             css::frame::XTransientDocumentsDocumentContentFactory > xDocFac;
90     try
91     {
92         xDocFac =
93             css::uno::Reference<
94                 css::frame::XTransientDocumentsDocumentContentFactory >(
95                     xMCF->createInstanceWithContext(
96                         rtl::OUString(
97                             RTL_CONSTASCII_USTRINGPARAM(
98                                 "com.sun.star.frame.TransientDocumentsDocumentContentFactory" ) ),
99                         xContext ),
100                 css::uno::UNO_QUERY );
101     }
102     catch ( css::uno::Exception const & )
103     {
104         // handled below
105     }
106 
107     if ( xDocFac.is() )
108     {
109         try
110         {
111             css::uno::Reference< css::ucb::XContent > xContent(
112                 xDocFac->createDocumentContent( xModel ) );
113             return xContent->getIdentifier()->getContentIdentifier();
114         }
115         catch ( css::lang::IllegalArgumentException const & )
116         {
117             OSL_ENSURE( false, "Invalid document model!" );
118         }
119     }
120 
121     OSL_ENSURE( false, "Unable to obtain URL for document model!" );
122     return rtl::OUString();
123 }
124     static css::uno::Reference< css::frame::XModel > tDocUrlToModel( const ::rtl::OUString& url )
125 {
126     css::uno::Any result;
127 
128     try
129     {
130         ::ucbhelper::Content root( url, NULL );
131         ::rtl::OUString propName =  OUSTR("DocumentModel");
132         result = getUCBProperty( root, propName );
133     }
134     catch ( css::ucb::ContentCreationException& )
135     {
136         // carry on, empty value will be returned
137     }
138     catch ( css::uno::RuntimeException& )
139     {
140         // carry on, empty value will be returned
141     }
142 
143     css::uno::Reference< css::frame::XModel > xModel(
144         result, css::uno::UNO_QUERY );
145 
146     return xModel;
147 }
148 
149 
150     static css::uno::Any getUCBProperty( ::ucbhelper::Content& content, ::rtl::OUString& prop )
151 {
152     css::uno::Any result;
153     try
154     {
155         result = content.getPropertyValue( prop );
156     }
157     catch ( css::uno::Exception& )
158     {
159     }
160     return result;
161 }
162 
163 private:
164 static ::rtl::OUString parseLocationName( const ::rtl::OUString& location )
165 {
166     // strip out the last leaf of location name
167     // e.g. file://dir1/dir2/Blah.sxw - > Blah.sxw
168     ::rtl::OUString temp = location;
169     INetURLObject aURLObj( temp );
170     if ( !aURLObj.HasError() )
171         temp = aURLObj.getName( INetURLObject::LAST_SEGMENT, true, INetURLObject::DECODE_WITH_CHARSET );
172     return temp;
173 }
174 
175 };
176 } // namespace sf_misc
177 #endif //
178