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