1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <doc.hxx>
#include <docufld.hxx>
#include <unofldmid.h>
#include <com/sun/star/uri/UriReferenceFactory.hpp>
#include <com/sun/star/uri/XVndSunStarScriptUrl.hpp>
#include <comphelper/processfactory.hxx>
#include <osl/diagnose.h>
using namespace ::com::sun::star;
SwMacroFieldType::SwMacroFieldType(SwDoc* pDocument)
: SwFieldType( SwFieldIds::Macro ),
m_pDoc(pDocument)
{
}
std::unique_ptr<SwFieldType> SwMacroFieldType::Copy() const
{
return std::make_unique<SwMacroFieldType>(m_pDoc);
}
SwMacroField::SwMacroField(SwMacroFieldType* pInitType,
const OUString& rLibAndName, const OUString& rText) :
SwField(pInitType), m_aMacro(rLibAndName), m_aText(rText), m_bIsScriptURL(false)
{
m_bIsScriptURL = isScriptURL(m_aMacro);
}
OUString SwMacroField::ExpandImpl(SwRootFrame const*const) const
{
return m_aText ;
}
std::unique_ptr<SwField> SwMacroField::Copy() const
{
return std::make_unique<SwMacroField>(static_cast<SwMacroFieldType*>(GetTyp()), m_aMacro, m_aText);
}
OUString SwMacroField::GetFieldName() const
{
return GetTyp()->GetName() + " " + m_aMacro;
}
OUString SwMacroField::GetLibName() const
{
// if it is a Scripting Framework macro return an empty string
if (m_bIsScriptURL)
{
return OUString();
}
if (!m_aMacro.isEmpty())
{
sal_Int32 nPos = m_aMacro.getLength();
for (sal_Int32 i = 0; i < 3 && nPos > 0; i++)
while (m_aMacro[--nPos] != '.' && nPos > 0) ;
return m_aMacro.copy(0, nPos);
}
OSL_FAIL("No LibName");
return OUString();
}
OUString SwMacroField::GetMacroName() const
{
if (!m_aMacro.isEmpty())
{
if (m_bIsScriptURL)
{
return m_aMacro;
}
else
{
sal_Int32 nPos = m_aMacro.getLength();
for (sal_Int32 i = 0; i < 3 && nPos > 0; i++)
while (m_aMacro[--nPos] != '.' && nPos > 0) ;
return m_aMacro.copy( ++nPos );
}
}
OSL_FAIL("No MacroName");
return OUString();
}
SvxMacro SwMacroField::GetSvxMacro() const
{
if (m_bIsScriptURL)
{
return SvxMacro(m_aMacro, OUString(), EXTENDED_STYPE);
}
else
{
return SvxMacro(GetMacroName(), GetLibName(), STARBASIC);
}
}
/// LibName and MacroName
void SwMacroField::SetPar1(const OUString& rStr)
{
m_aMacro = rStr;
m_bIsScriptURL = isScriptURL(m_aMacro);
}
/// Get macro
OUString SwMacroField::GetPar1() const
{
return m_aMacro;
}
/// set macro text
void SwMacroField::SetPar2(const OUString& rStr)
{
m_aText = rStr;
}
/// get macro text
OUString SwMacroField::GetPar2() const
{
return m_aText;
}
bool SwMacroField::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
{
switch( nWhichId )
{
case FIELD_PROP_PAR1:
rAny <<= GetMacroName();
break;
case FIELD_PROP_PAR2:
rAny <<= m_aText;
break;
case FIELD_PROP_PAR3:
rAny <<= GetLibName();
break;
case FIELD_PROP_PAR4:
rAny <<= m_bIsScriptURL ? GetMacroName() : OUString();
break;
default:
assert(false);
}
return true;
}
bool SwMacroField::PutValue( const uno::Any& rAny, sal_uInt16 nWhichId )
{
OUString sTmp;
switch( nWhichId )
{
case FIELD_PROP_PAR1:
rAny >>= sTmp;
CreateMacroString( m_aMacro, sTmp, GetLibName());
break;
case FIELD_PROP_PAR2:
rAny >>= m_aText;
break;
case FIELD_PROP_PAR3:
rAny >>= sTmp;
CreateMacroString(m_aMacro, GetMacroName(), sTmp );
break;
case FIELD_PROP_PAR4:
rAny >>= m_aMacro;
m_bIsScriptURL = isScriptURL(m_aMacro);
break;
default:
assert(false);
}
return true;
}
/// create an internally used macro name from the library and macro name parts
void SwMacroField::CreateMacroString(
OUString& rMacro,
const OUString& rMacroName,
const OUString& rLibraryName )
{
// concatenate library and name; use dot only if both strings have content
rMacro = rLibraryName;
if ( !rLibraryName.isEmpty() && !rMacroName.isEmpty() )
rMacro += ".";
rMacro += rMacroName;
}
bool SwMacroField::isScriptURL( const OUString& str )
{
try
{
uno::Reference<uno::XComponentContext> xContext = ::comphelper::getProcessComponentContext();
uno::Reference<uri::XUriReferenceFactory> xFactory = uri::UriReferenceFactory::create(xContext);
uno::Reference<uri::XVndSunStarScriptUrl> xUrl(xFactory->parse(str), uno::UNO_QUERY);
return xUrl.is();
}
catch (...)
{
}
return false;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|