1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #include "vbatemplate.hxx"
20 #include "vbaautotextentry.hxx"
21 #include <com/sun/star/text/AutoTextContainer.hpp>
22 #include <comphelper/processfactory.hxx>
23 #include <tools/urlobj.hxx>
24 #include <rtl/character.hxx>
25 #include <osl/file.hxx>
28 using namespace ::ooo::vba
;
29 using namespace ::com::sun::star
;
31 static OUString
lcl_CheckGroupName( std::u16string_view aGroupName
)
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
++ )
37 sal_Unicode cChar
= aGroupName
[i
];
38 if (rtl::isAsciiAlphanumeric(cChar
) ||
39 cChar
== '_' || cChar
== 0x20)
45 return sRet
.makeStringAndClear();
48 SwVbaTemplate::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
))
53 SwVbaTemplate::~SwVbaTemplate()
58 SwVbaTemplate::getName()
61 if( !msFullUrl
.isEmpty() )
63 INetURLObject
aURL( msFullUrl
);
64 ::osl::File::getSystemPathFromFileURL( aURL
.GetLastName(), sName
);
70 SwVbaTemplate::getPath()
73 if( !msFullUrl
.isEmpty() )
75 INetURLObject
aURL( msFullUrl
);
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
);
84 SwVbaTemplate::AutoTextEntries( const uno::Any
& index
)
86 uno::Reference
< uno::XComponentContext
> xContext
= comphelper::getProcessComponentContext();
87 uno::Reference
< text::XAutoTextContainer2
> xAutoTextContainer
= text::AutoTextContainer::create( xContext
);
89 // the default template is "Normal.dot" in Word.
90 OUString
sGroup("Normal");
91 OUString sName
= getName();
92 sal_Int32 nIndex
= sName
.lastIndexOf( '.' );
95 sGroup
= sName
.copy( 0, sName
.lastIndexOf( '.' ) );
97 OUString sNewGroup
= lcl_CheckGroupName( sGroup
);
99 uno::Reference
< container::XIndexAccess
> xGroup
;
100 if( !xAutoTextContainer
->hasByName( sNewGroup
) )
102 throw uno::RuntimeException("Auto Text Entry doesn't exist" );
105 xGroup
.set( xAutoTextContainer
->getByName( sNewGroup
), uno::UNO_QUERY_THROW
);
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
);
114 SwVbaTemplate::getServiceImplName()
116 return "SwVbaTemplate";
119 uno::Sequence
< OUString
>
120 SwVbaTemplate::getServiceNames()
122 static uno::Sequence
< OUString
> const aServiceNames
124 "ooo.vba.word.Template"
126 return aServiceNames
;
129 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */