LibreOffice Module scripting (master) 1
MiscUtils.hxx
Go to the documentation of this file.
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#pragma once
21
22#include <rtl/ustring.hxx>
23#include <osl/diagnose.h>
24
25#include <ucbhelper/content.hxx>
26#include <com/sun/star/uno/XComponentContext.hpp>
27#include <com/sun/star/frame/XModel.hpp>
28#include <com/sun/star/frame/XTransientDocumentsDocumentContentFactory.hpp>
29#include <com/sun/star/ucb/ContentCreationException.hpp>
30#include <com/sun/star/ucb/SimpleFileAccess.hpp>
31#include <com/sun/star/lang/IllegalArgumentException.hpp>
32#include <com/sun/star/lang/XMultiComponentFactory.hpp>
34
35namespace sf_misc
36{
37
39{
40public:
41
42static css::uno::Sequence< OUString > allOpenTDocUrls( const css::uno::Reference< css::uno::XComponentContext >& xCtx)
43{
44 css::uno::Sequence< OUString > result;
45 try
46 {
47 if ( !xCtx.is() )
48 {
49 return result;
50 }
51 css::uno::Reference < css::ucb::XSimpleFileAccess3 > xSFA( css::ucb::SimpleFileAccess::create(xCtx) );
52 result = xSFA->getFolderContents( "vnd.sun.star.tdoc:/", true );
53 }
54 catch ( css::uno::Exception& )
55 {
56 }
57 return result;
58}
59
60static OUString xModelToTdocUrl( const css::uno::Reference< css::frame::XModel >& xModel,
61 const css::uno::Reference< css::uno::XComponentContext >& xContext )
62{
63 css::uno::Reference< css::lang::XMultiComponentFactory > xMCF(
64 xContext->getServiceManager() );
65 css::uno::Reference<
66 css::frame::XTransientDocumentsDocumentContentFactory > xDocFac;
67 try
68 {
69 xDocFac.set(xMCF->createInstanceWithContext(
70 "com.sun.star.frame.TransientDocumentsDocumentContentFactory",
71 xContext ),
72 css::uno::UNO_QUERY );
73 }
74 catch ( css::uno::Exception const & )
75 {
76 // handled below
77 }
78
79 if ( xDocFac.is() )
80 {
81 try
82 {
83 css::uno::Reference< css::ucb::XContent > xContent(
84 xDocFac->createDocumentContent( xModel ) );
85 return xContent->getIdentifier()->getContentIdentifier();
86 }
87 catch ( css::lang::IllegalArgumentException const & )
88 {
89 OSL_FAIL( "Invalid document model!" );
90 }
91 }
92
93 OSL_FAIL( "Unable to obtain URL for document model!" );
94 return OUString();
95}
96
97static css::uno::Reference< css::frame::XModel > tDocUrlToModel( const OUString& url )
98{
99 css::uno::Any result;
100
101 try
102 {
104 result = getUCBProperty( root, "DocumentModel" );
105 }
106 catch ( css::ucb::ContentCreationException& )
107 {
108 // carry on, empty value will be returned
109 }
110 catch ( css::uno::RuntimeException& )
111 {
112 // carry on, empty value will be returned
113 }
114
115 css::uno::Reference< css::frame::XModel > xModel(
116 result, css::uno::UNO_QUERY );
117
118 return xModel;
119}
120
121
122static css::uno::Any getUCBProperty( ::ucbhelper::Content& content, OUString const & prop )
123{
124 css::uno::Any result;
125 try
126 {
127 result = content.getPropertyValue( prop );
128 }
129 catch ( css::uno::Exception& )
130 {
131 }
132 return result;
133}
134
135};
136} // namespace sf_misc
137
138/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static css::uno::Any getUCBProperty(::ucbhelper::Content &content, OUString const &prop)
Definition: MiscUtils.hxx:122
static css::uno::Reference< css::frame::XModel > tDocUrlToModel(const OUString &url)
Definition: MiscUtils.hxx:97
static css::uno::Sequence< OUString > allOpenTDocUrls(const css::uno::Reference< css::uno::XComponentContext > &xCtx)
Definition: MiscUtils.hxx:42
static OUString xModelToTdocUrl(const css::uno::Reference< css::frame::XModel > &xModel, const css::uno::Reference< css::uno::XComponentContext > &xContext)
Definition: MiscUtils.hxx:60
css::uno::Any getPropertyValue(const OUString &rPropertyName)
Reference< XComponentContext > getProcessComponentContext()
Reference< XModel > xModel
Any result