LibreOffice Module sw (master) 1
vbatemplate.cxx
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#include "vbatemplate.hxx"
20#include "vbaautotextentry.hxx"
21#include <com/sun/star/text/AutoTextContainer.hpp>
23#include <tools/urlobj.hxx>
24#include <rtl/character.hxx>
25#include <osl/file.hxx>
26#include <utility>
27
28using namespace ::ooo::vba;
29using namespace ::com::sun::star;
30
31static OUString lcl_CheckGroupName( std::u16string_view aGroupName )
32{
33 OUStringBuffer sRet(aGroupName.size());
34 //group name should contain only A-Z and a-z and spaces
35 for( size_t i = 0; i < aGroupName.size(); i++ )
36 {
37 sal_Unicode cChar = aGroupName[i];
38 if (rtl::isAsciiAlphanumeric(cChar) ||
39 cChar == '_' || cChar == 0x20)
40 {
41 sRet.append(cChar);
42 }
43 }
44 sRet.strip(' ');
45 return sRet.makeStringAndClear();
46}
47
48SwVbaTemplate::SwVbaTemplate( const uno::Reference< ooo::vba::XHelperInterface >& rParent, const uno::Reference< uno::XComponentContext >& rContext, OUString aFullUrl )
49 : SwVbaTemplate_BASE( rParent, rContext ), msFullUrl(std::move( aFullUrl ))
50{
51}
52
54{
55}
56
57OUString
59{
60 OUString sName;
61 if( !msFullUrl.isEmpty() )
62 {
64 ::osl::File::getSystemPathFromFileURL( aURL.GetLastName(), sName );
65 }
66 return sName;
67}
68
69OUString
71{
72 OUString sPath;
73 if( !msFullUrl.isEmpty() )
74 {
76 OUString sURL( aURL.GetMainURL( INetURLObject::DecodeMechanism::ToIUri ) );
77 sURL = sURL.copy( 0, sURL.getLength() - aURL.GetLastName().getLength() - 1 );
78 ::osl::File::getSystemPathFromFileURL( sURL, sPath );
79 }
80 return sPath;
81}
82
83uno::Any SAL_CALL
85{
86 uno::Reference< uno::XComponentContext > xContext = comphelper::getProcessComponentContext();
87 uno::Reference< text::XAutoTextContainer2 > xAutoTextContainer = text::AutoTextContainer::create( xContext );
88
89 // the default template is "Normal.dot" in Word.
90 OUString sGroup("Normal");
91 OUString sName = getName();
92 sal_Int32 nIndex = sName.lastIndexOf( '.' );
93 if( nIndex > 0 )
94 {
95 sGroup = sName.copy( 0, sName.lastIndexOf( '.' ) );
96 }
97 OUString sNewGroup = lcl_CheckGroupName( sGroup );
98
99 uno::Reference< container::XIndexAccess > xGroup;
100 if( !xAutoTextContainer->hasByName( sNewGroup ) )
101 {
102 throw uno::RuntimeException("Auto Text Entry doesn't exist" );
103 }
104
105 xGroup.set( xAutoTextContainer->getByName( sNewGroup ), uno::UNO_QUERY_THROW );
106
107 uno::Reference< XCollection > xCol( new SwVbaAutoTextEntries( this, mxContext, xGroup ) );
108 if( index.hasValue() )
109 return xCol->Item( index, uno::Any() );
110 return uno::Any( xCol );
111}
112
113OUString
115{
116 return "SwVbaTemplate";
117}
118
119uno::Sequence< OUString >
121{
122 static uno::Sequence< OUString > const aServiceNames
123 {
124 "ooo.vba.word.Template"
125 };
126 return aServiceNames;
127}
128
129/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::uno::XComponentContext > mxContext
virtual ~SwVbaTemplate() override
Definition: vbatemplate.cxx:53
virtual OUString getServiceImplName() override
virtual OUString SAL_CALL getPath() override
Definition: vbatemplate.cxx:70
virtual OUString SAL_CALL getName() override
Definition: vbatemplate.cxx:58
virtual css::uno::Sequence< OUString > getServiceNames() override
virtual css::uno::Any SAL_CALL AutoTextEntries(const css::uno::Any &index) override
Definition: vbatemplate.cxx:84
SwVbaTemplate(const css::uno::Reference< ooo::vba::XHelperInterface > &rParent, const css::uno::Reference< css::uno::XComponentContext > &rContext, OUString)
Definition: vbatemplate.cxx:48
OUString msFullUrl
Definition: vbatemplate.hxx:30
URL aURL
Sequence< OUString > aServiceNames
OUString sName
sal_Int32 nIndex
Reference< XComponentContext > getProcessComponentContext()
int i
index
sal_uInt16 sal_Unicode
static OUString lcl_CheckGroupName(std::u16string_view aGroupName)
Definition: vbatemplate.cxx:31