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