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