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