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
|
/* -*- 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/.
*/
#include <svx/annotation/Annotation.hxx>
#include <svx/annotation/ObjectAnnotationData.hxx>
#include <svx/svdpage.hxx>
#include <tools/json_writer.hxx>
#include <sfx2/viewsh.hxx>
#include <unotools/datetime.hxx>
#include <comphelper/lok.hxx>
#include <LibreOfficeKit/LibreOfficeKitEnums.h>
using namespace css;
namespace sdr::annotation
{
namespace
{
OString lcl_LOKGetCommentPayload(CommentNotificationType nType, Annotation& rAnnotation)
{
tools::JsonWriter aJsonWriter;
{
auto aCommentNode = aJsonWriter.startNode("comment");
aJsonWriter.put(
"action",
(nType == CommentNotificationType::Add
? "Add"
: (nType == CommentNotificationType::Remove
? "Remove"
: (nType == CommentNotificationType::Modify ? "Modify" : "???"))));
aJsonWriter.put("id", rAnnotation.GetId());
if (nType != CommentNotificationType::Remove)
{
aJsonWriter.put("id", rAnnotation.GetId());
aJsonWriter.put("author", rAnnotation.GetAuthor());
aJsonWriter.put("dateTime", utl::toISO8601(rAnnotation.GetDateTime()));
aJsonWriter.put("text", rAnnotation.GetText());
SdrPage const* pPage = rAnnotation.getPage();
aJsonWriter.put("parthash", pPage ? OString::number(pPage->GetUniqueID()) : OString());
geometry::RealPoint2D const aPoint = rAnnotation.GetPosition();
geometry::RealSize2D const aSize = rAnnotation.GetSize();
tools::Rectangle aRectangle(
Point(std::round(o3tl::toTwips(aPoint.X, o3tl::Length::mm)),
std::round(o3tl::toTwips(aPoint.Y, o3tl::Length::mm))),
Size(std::round(o3tl::toTwips(aSize.Width, o3tl::Length::mm)),
std::round(o3tl::toTwips(aSize.Height, o3tl::Length::mm))));
aJsonWriter.put("rectangle", aRectangle.toString());
}
}
return aJsonWriter.finishAndGetAsOString();
}
/** Undo/redo a modification of an annotation - change of annotation data */
class UndoAnnotation : public SdrUndoAction
{
public:
explicit UndoAnnotation(Annotation& rAnnotation)
: SdrUndoAction(*rAnnotation.GetModel())
, mrAnnotation(rAnnotation)
{
maUndoData.get(mrAnnotation);
}
void Undo() override
{
maRedoData.get(mrAnnotation);
maUndoData.set(mrAnnotation);
LOKCommentNotifyAll(CommentNotificationType::Modify, mrAnnotation);
}
void Redo() override
{
maUndoData.get(mrAnnotation);
maRedoData.set(mrAnnotation);
LOKCommentNotifyAll(CommentNotificationType::Modify, mrAnnotation);
}
protected:
Annotation& mrAnnotation;
AnnotationData maUndoData;
AnnotationData maRedoData;
};
} // anonymous ns
void LOKCommentNotify(CommentNotificationType nType, const SfxViewShell* pViewShell,
Annotation& rAnnotation)
{
// callbacks only if tiled annotations are explicitly turned off by LOK client
if (!comphelper::LibreOfficeKit::isActive() || comphelper::LibreOfficeKit::isTiledAnnotations())
return;
OString aPayload = lcl_LOKGetCommentPayload(nType, rAnnotation);
pViewShell->libreOfficeKitViewCallback(LOK_CALLBACK_COMMENT, aPayload);
}
void LOKCommentNotifyAll(CommentNotificationType nType, Annotation& rAnnotation)
{
// callbacks only if tiled annotations are explicitly turned off by LOK client
if (!comphelper::LibreOfficeKit::isActive() || comphelper::LibreOfficeKit::isTiledAnnotations())
return;
OString aPayload = lcl_LOKGetCommentPayload(nType, rAnnotation);
const SfxViewShell* pViewShell = SfxViewShell::GetFirst();
while (pViewShell)
{
pViewShell->libreOfficeKitViewCallback(LOK_CALLBACK_COMMENT, aPayload);
pViewShell = SfxViewShell::GetNext(*pViewShell);
}
}
void AnnotationData::get(Annotation& rAnnotation)
{
m_Position = rAnnotation.GetPosition();
m_Size = rAnnotation.GetSize();
m_Author = rAnnotation.GetAuthor();
m_Initials = rAnnotation.GetInitials();
m_DateTime = rAnnotation.GetDateTime();
m_Text = rAnnotation.GetText();
}
void AnnotationData::set(Annotation& rAnnotation)
{
rAnnotation.SetPosition(m_Position);
rAnnotation.SetSize(m_Size);
rAnnotation.SetAuthor(m_Author);
rAnnotation.SetInitials(m_Initials);
rAnnotation.SetDateTime(m_DateTime);
rAnnotation.SetText(m_Text);
}
Annotation::Annotation(const css::uno::Reference<css::uno::XComponentContext>& rxContext,
SdrPage* pPage)
: cppu::PropertySetMixin<office::XAnnotation>(rxContext, IMPLEMENTS_PROPERTY_SET,
uno::Sequence<OUString>())
, mpPage(pPage)
{
}
SdrModel* Annotation::GetModel() const
{
return mpPage != nullptr ? &mpPage->getSdrModelFromSdrPage() : nullptr;
}
uno::Any Annotation::queryInterface(uno::Type const& type)
{
return comphelper::WeakComponentImplHelper<office::XAnnotation>::queryInterface(type);
}
std::unique_ptr<SdrUndoAction> Annotation::createUndoAnnotation()
{
return std::make_unique<UndoAnnotation>(*this);
}
OUString Annotation::GetText()
{
uno::Reference<text::XText> xText(getTextRange());
return xText->getString();
}
void Annotation::SetText(OUString const& rText)
{
uno::Reference<text::XText> xText(getTextRange());
return xText->setString(rText);
}
uno::Reference<text::XText> SAL_CALL Annotation::getTextRange()
{
std::unique_lock g(m_aMutex);
if (!m_TextRange.is() && mpPage != nullptr)
{
m_TextRange = sdr::annotation::TextApiObject::create(&mpPage->getSdrModelFromSdrPage());
}
return m_TextRange;
}
void Annotation::disposing(std::unique_lock<std::mutex>& /*rGuard*/)
{
mpPage = nullptr;
if (m_TextRange.is())
{
m_TextRange->dispose();
m_TextRange.clear();
}
}
SdrObject* Annotation::findAnnotationObject()
{
SdrPage const* pPage = getPage();
if (!pPage)
return nullptr;
for (size_t i = 0; i < pPage->GetObjCount(); ++i)
{
SdrObject* pObject = pPage->GetObj(i);
if (pObject->isAnnotationObject()
&& pObject->getAnnotationData()->mxAnnotation.get() == this)
return pObject;
}
return nullptr;
}
} // namespace sdr::annotation
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|