nss: upgrade to release 3.73
[LibreOffice.git] / sw / source / core / fields / macrofld.cxx
blobc9fd26870f99d3c191f60d4789841dc308c916f5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <doc.hxx>
21 #include <docufld.hxx>
22 #include <unofldmid.h>
23 #include <com/sun/star/uri/UriReferenceFactory.hpp>
24 #include <com/sun/star/uri/XVndSunStarScriptUrl.hpp>
25 #include <comphelper/processfactory.hxx>
26 #include <osl/diagnose.h>
28 using namespace ::com::sun::star;
30 SwMacroFieldType::SwMacroFieldType(SwDoc& rDocument)
31 : SwFieldType( SwFieldIds::Macro )
32 , m_rDoc(rDocument)
36 std::unique_ptr<SwFieldType> SwMacroFieldType::Copy() const
38 return std::make_unique<SwMacroFieldType>(m_rDoc);
41 SwMacroField::SwMacroField(SwMacroFieldType* pInitType,
42 const OUString& rLibAndName, const OUString& rText) :
43 SwField(pInitType), m_aMacro(rLibAndName), m_aText(rText), m_bIsScriptURL(false)
45 m_bIsScriptURL = isScriptURL(m_aMacro);
48 OUString SwMacroField::ExpandImpl(SwRootFrame const*const) const
50 return m_aText ;
53 std::unique_ptr<SwField> SwMacroField::Copy() const
55 return std::make_unique<SwMacroField>(static_cast<SwMacroFieldType*>(GetTyp()), m_aMacro, m_aText);
58 OUString SwMacroField::GetFieldName() const
60 return GetTyp()->GetName() + " " + m_aMacro;
63 OUString SwMacroField::GetLibName() const
65 // if it is a Scripting Framework macro return an empty string
66 if (m_bIsScriptURL)
68 return OUString();
71 if (!m_aMacro.isEmpty())
73 sal_Int32 nPos = m_aMacro.getLength();
75 for (sal_Int32 i = 0; i < 3 && nPos > 0; i++)
76 while (m_aMacro[--nPos] != '.' && nPos > 0) ;
78 return m_aMacro.copy(0, nPos);
81 OSL_FAIL("No LibName");
82 return OUString();
85 OUString SwMacroField::GetMacroName() const
87 if (!m_aMacro.isEmpty())
89 if (m_bIsScriptURL)
91 return m_aMacro;
93 else
95 sal_Int32 nPos = m_aMacro.getLength();
97 for (sal_Int32 i = 0; i < 3 && nPos > 0; i++)
98 while (m_aMacro[--nPos] != '.' && nPos > 0) ;
100 return m_aMacro.copy( ++nPos );
104 OSL_FAIL("No MacroName");
105 return OUString();
108 SvxMacro SwMacroField::GetSvxMacro() const
110 if (m_bIsScriptURL)
112 return SvxMacro(m_aMacro, OUString(), EXTENDED_STYPE);
114 else
116 return SvxMacro(GetMacroName(), GetLibName(), STARBASIC);
120 /// LibName and MacroName
121 void SwMacroField::SetPar1(const OUString& rStr)
123 m_aMacro = rStr;
124 m_bIsScriptURL = isScriptURL(m_aMacro);
127 /// Get macro
128 OUString SwMacroField::GetPar1() const
130 return m_aMacro;
133 /// set macro text
134 void SwMacroField::SetPar2(const OUString& rStr)
136 m_aText = rStr;
139 /// get macro text
140 OUString SwMacroField::GetPar2() const
142 return m_aText;
145 bool SwMacroField::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
147 switch( nWhichId )
149 case FIELD_PROP_PAR1:
150 rAny <<= GetMacroName();
151 break;
152 case FIELD_PROP_PAR2:
153 rAny <<= m_aText;
154 break;
155 case FIELD_PROP_PAR3:
156 rAny <<= GetLibName();
157 break;
158 case FIELD_PROP_PAR4:
159 rAny <<= m_bIsScriptURL ? GetMacroName() : OUString();
160 break;
161 default:
162 assert(false);
164 return true;
167 bool SwMacroField::PutValue( const uno::Any& rAny, sal_uInt16 nWhichId )
169 OUString sTmp;
170 switch( nWhichId )
172 case FIELD_PROP_PAR1:
173 rAny >>= sTmp;
174 CreateMacroString( m_aMacro, sTmp, GetLibName());
175 break;
176 case FIELD_PROP_PAR2:
177 rAny >>= m_aText;
178 break;
179 case FIELD_PROP_PAR3:
180 rAny >>= sTmp;
181 CreateMacroString(m_aMacro, GetMacroName(), sTmp );
182 break;
183 case FIELD_PROP_PAR4:
184 rAny >>= m_aMacro;
185 m_bIsScriptURL = isScriptURL(m_aMacro);
186 break;
187 default:
188 assert(false);
191 return true;
194 /// create an internally used macro name from the library and macro name parts
195 void SwMacroField::CreateMacroString(
196 OUString& rMacro,
197 const OUString& rMacroName,
198 const OUString& rLibraryName )
200 // concatenate library and name; use dot only if both strings have content
201 rMacro = rLibraryName;
202 if ( !rLibraryName.isEmpty() && !rMacroName.isEmpty() )
203 rMacro += ".";
204 rMacro += rMacroName;
207 bool SwMacroField::isScriptURL( const OUString& str )
211 uno::Reference<uno::XComponentContext> xContext = ::comphelper::getProcessComponentContext();
212 uno::Reference<uri::XUriReferenceFactory> xFactory = uri::UriReferenceFactory::create(xContext);
213 uno::Reference<uri::XVndSunStarScriptUrl> xUrl(xFactory->parse(str), uno::UNO_QUERY);
214 return xUrl.is();
216 catch (...)
219 return false;
222 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */