android: Update app-specific/MIME type icons
[LibreOffice.git] / sw / source / core / fields / macrofld.cxx
blob244edae398d1e079aec5f56401761afd114c2df3
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 <utility>
27 #include <osl/diagnose.h>
29 using namespace ::com::sun::star;
31 SwMacroFieldType::SwMacroFieldType(SwDoc& rDocument)
32 : SwFieldType( SwFieldIds::Macro )
33 , m_rDoc(rDocument)
37 std::unique_ptr<SwFieldType> SwMacroFieldType::Copy() const
39 return std::make_unique<SwMacroFieldType>(m_rDoc);
42 SwMacroField::SwMacroField(SwMacroFieldType* pInitType,
43 OUString aLibAndName, OUString aText) :
44 SwField(pInitType), m_aMacro(std::move(aLibAndName)), m_aText(std::move(aText)), m_bIsScriptURL(false)
46 m_bIsScriptURL = isScriptURL(m_aMacro);
49 OUString SwMacroField::ExpandImpl(SwRootFrame const*const) const
51 return m_aText ;
54 std::unique_ptr<SwField> SwMacroField::Copy() const
56 return std::make_unique<SwMacroField>(static_cast<SwMacroFieldType*>(GetTyp()), m_aMacro, m_aText);
59 OUString SwMacroField::GetFieldName() const
61 return GetTyp()->GetName() + " " + m_aMacro;
64 OUString SwMacroField::GetLibName() const
66 // if it is a Scripting Framework macro return an empty string
67 if (m_bIsScriptURL)
69 return OUString();
72 if (!m_aMacro.isEmpty())
74 sal_Int32 nPos = m_aMacro.getLength();
76 for (sal_Int32 i = 0; i < 3 && nPos > 0; i++)
77 while (m_aMacro[--nPos] != '.' && nPos > 0) ;
79 return m_aMacro.copy(0, nPos);
82 OSL_FAIL("No LibName");
83 return OUString();
86 OUString SwMacroField::GetMacroName() const
88 if (!m_aMacro.isEmpty())
90 if (m_bIsScriptURL)
92 return m_aMacro;
94 else
96 sal_Int32 nPos = m_aMacro.getLength();
98 for (sal_Int32 i = 0; i < 3 && nPos > 0; i++)
99 while (m_aMacro[--nPos] != '.' && nPos > 0) ;
101 return m_aMacro.copy( ++nPos );
105 OSL_FAIL("No MacroName");
106 return OUString();
109 SvxMacro SwMacroField::GetSvxMacro() const
111 if (m_bIsScriptURL)
113 return SvxMacro(m_aMacro, OUString(), EXTENDED_STYPE);
115 else
117 return SvxMacro(GetMacroName(), GetLibName(), STARBASIC);
121 /// LibName and MacroName
122 void SwMacroField::SetPar1(const OUString& rStr)
124 m_aMacro = rStr;
125 m_bIsScriptURL = isScriptURL(m_aMacro);
128 /// Get macro
129 OUString SwMacroField::GetPar1() const
131 return m_aMacro;
134 /// set macro text
135 void SwMacroField::SetPar2(const OUString& rStr)
137 m_aText = rStr;
140 /// get macro text
141 OUString SwMacroField::GetPar2() const
143 return m_aText;
146 bool SwMacroField::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
148 switch( nWhichId )
150 case FIELD_PROP_PAR1:
151 rAny <<= GetMacroName();
152 break;
153 case FIELD_PROP_PAR2:
154 rAny <<= m_aText;
155 break;
156 case FIELD_PROP_PAR3:
157 rAny <<= GetLibName();
158 break;
159 case FIELD_PROP_PAR4:
160 rAny <<= m_bIsScriptURL ? GetMacroName() : OUString();
161 break;
162 default:
163 assert(false);
165 return true;
168 bool SwMacroField::PutValue( const uno::Any& rAny, sal_uInt16 nWhichId )
170 OUString sTmp;
171 switch( nWhichId )
173 case FIELD_PROP_PAR1:
174 rAny >>= sTmp;
175 CreateMacroString( m_aMacro, sTmp, GetLibName());
176 break;
177 case FIELD_PROP_PAR2:
178 rAny >>= m_aText;
179 break;
180 case FIELD_PROP_PAR3:
181 rAny >>= sTmp;
182 CreateMacroString(m_aMacro, GetMacroName(), sTmp );
183 break;
184 case FIELD_PROP_PAR4:
185 rAny >>= m_aMacro;
186 m_bIsScriptURL = isScriptURL(m_aMacro);
187 break;
188 default:
189 assert(false);
192 return true;
195 /// create an internally used macro name from the library and macro name parts
196 void SwMacroField::CreateMacroString(
197 OUString& rMacro,
198 std::u16string_view rMacroName,
199 const OUString& rLibraryName )
201 // concatenate library and name; use dot only if both strings have content
202 rMacro = rLibraryName;
203 if ( !rLibraryName.isEmpty() && !rMacroName.empty() )
204 rMacro += ".";
205 rMacro += rMacroName;
208 bool SwMacroField::isScriptURL( const OUString& str )
212 uno::Reference<uno::XComponentContext> xContext = ::comphelper::getProcessComponentContext();
213 uno::Reference<uri::XUriReferenceFactory> xFactory = uri::UriReferenceFactory::create(xContext);
214 uno::Reference<uri::XVndSunStarScriptUrl> xUrl(xFactory->parse(str), uno::UNO_QUERY);
215 return xUrl.is();
217 catch (...)
220 return false;
223 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */