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