summaryrefslogtreecommitdiff
path: root/sfx2
diff options
context:
space:
mode:
authorAshod Nakashian <ashod.nakashian@collabora.co.uk>2020-07-24 11:33:46 -0400
committerAshod Nakashian <ash@collabora.com>2020-08-10 04:45:07 +0200
commitb7b81c68dc406cfa16d959f22417a3c3ed7888d5 (patch)
tree26134afd5402ac6bfb6c24b26d6a5133f7419050 /sfx2
parent02cde0be4fec9f4c77f535e5053888ef1e37fad6 (diff)
sfx2: lok: refactor notifications and const correctness
This reduces the stringification and reuses the notificaiton helpers to reduce code duplication. Change-Id: Icf9f9c50f3eeee61a0ded741d39fed37cfcc8da1 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/99972 Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoffice@gmail.com> Reviewed-by: Ashod Nakashian <ash@collabora.com>
Diffstat (limited to 'sfx2')
-rw-r--r--sfx2/source/view/lokhelper.cxx69
1 files changed, 41 insertions, 28 deletions
diff --git a/sfx2/source/view/lokhelper.cxx b/sfx2/source/view/lokhelper.cxx
index 03241d065a78..864523e50fd6 100644
--- a/sfx2/source/view/lokhelper.cxx
+++ b/sfx2/source/view/lokhelper.cxx
@@ -73,7 +73,7 @@ static LOKDeviceFormFactor g_deviceFormFactor = LOKDeviceFormFactor::UNKNOWN;
int SfxLokHelper::createView()
{
SfxViewFrame* pViewFrame = SfxViewFrame::GetFirst();
- if (!pViewFrame)
+ if (pViewFrame == nullptr)
return -1;
SfxViewShell* pPrevViewShell = SfxViewShell::Current();
ViewShellDocId nId;
@@ -82,7 +82,7 @@ int SfxLokHelper::createView()
SfxRequest aRequest(pViewFrame, SID_NEWWINDOW);
pViewFrame->ExecView_Impl(aRequest);
SfxViewShell* pViewShell = SfxViewShell::Current();
- if (!pViewShell)
+ if (pViewShell == nullptr)
return -1;
if (pPrevViewShell)
pViewShell->SetDocId(nId);
@@ -92,15 +92,15 @@ int SfxLokHelper::createView()
void SfxLokHelper::destroyView(int nId)
{
SfxApplication* pApp = SfxApplication::Get();
- if (!pApp)
+ if (pApp == nullptr)
return;
- int nViewShellId = nId;
+ const ViewShellId nViewShellId(nId);
SfxViewShellArr_Impl& rViewArr = pApp->GetViewShells_Impl();
- for (SfxViewShell* pViewShell : rViewArr)
+ for (const SfxViewShell* pViewShell : rViewArr)
{
- if (static_cast<sal_Int32>(pViewShell->GetViewShellId()) == nViewShellId)
+ if (pViewShell->GetViewShellId() == nViewShellId)
{
SfxViewFrame* pViewFrame = pViewShell->GetViewFrame();
SfxRequest aRequest(pViewFrame, SID_CLOSEWIN);
@@ -113,15 +113,15 @@ void SfxLokHelper::destroyView(int nId)
void SfxLokHelper::setView(int nId)
{
SfxApplication* pApp = SfxApplication::Get();
- if (!pApp)
+ if (pApp == nullptr)
return;
- int nViewShellId = nId;
+ const ViewShellId nViewShellId(nId);
SfxViewShellArr_Impl& rViewArr = pApp->GetViewShells_Impl();
- for (SfxViewShell* pViewShell : rViewArr)
+ for (const SfxViewShell* pViewShell : rViewArr)
{
- if (static_cast<sal_Int32>(pViewShell->GetViewShellId()) == nViewShellId)
+ if (pViewShell->GetViewShellId() == nViewShellId)
{
DisableCallbacks dc;
@@ -145,7 +145,7 @@ void SfxLokHelper::setView(int nId)
}
-int SfxLokHelper::getView(SfxViewShell* pViewShell)
+int SfxLokHelper::getView(const SfxViewShell* pViewShell)
{
if (!pViewShell)
pViewShell = SfxViewShell::Current();
@@ -290,38 +290,50 @@ static OString lcl_escapeQuotes(const OString &rStr)
return aBuf.makeStringAndClear();
}
-static OString lcl_generateJSON(SfxViewShell* pView, const boost::property_tree::ptree& rTree)
+static OString lcl_generateJSON(const SfxViewShell* pView, const boost::property_tree::ptree& rTree)
{
+ assert(pView != nullptr && "pView must be valid");
boost::property_tree::ptree aMessageProps = rTree;
aMessageProps.put("viewId", SfxLokHelper::getView(pView));
aMessageProps.put("part", pView->getPart());
std::stringstream aStream;
boost::property_tree::write_json(aStream, aMessageProps, false /* pretty */);
- return OString(aStream.str().c_str()).trim();
+ const std::string aString = aStream.str();
+ return OString(aString.c_str(), aString.size()).trim();
}
-void SfxLokHelper::notifyOtherView(SfxViewShell* pThisView, SfxViewShell const* pOtherView, int nType, const OString& rKey, const OString& rPayload)
+static inline OString lcl_generateJSON(const SfxViewShell* pView, const OString& rKey,
+ const OString& rPayload)
{
+ assert(pView != nullptr && "pView must be valid");
+ return OStringLiteral("{ \"viewId\": \"") + OString::number(SfxLokHelper::getView(pView))
+ + "\", \"part\": \"" + OString::number(pView->getPart()) + "\", \"" + rKey + "\": \""
+ + lcl_escapeQuotes(rPayload) + "\" }";
+}
+
+void SfxLokHelper::notifyOtherView(const SfxViewShell* pThisView, SfxViewShell const* pOtherView,
+ int nType, const OString& rKey, const OString& rPayload)
+{
+ assert(pThisView != nullptr && "pThisView must be valid");
if (DisableCallbacks::disabled())
return;
- OString aPayload = OStringLiteral("{ \"viewId\": \"") + OString::number(SfxLokHelper::getView(pThisView)) +
- "\", \"part\": \"" + OString::number(pThisView->getPart()) +
- "\", \"" + rKey + "\": \"" + lcl_escapeQuotes(rPayload) + "\" }";
-
+ const OString aPayload = lcl_generateJSON(pThisView, rKey, rPayload);
pOtherView->libreOfficeKitViewCallback(nType, aPayload.getStr());
}
-void SfxLokHelper::notifyOtherView(SfxViewShell* pThisView, SfxViewShell const* pOtherView, int nType,
- const boost::property_tree::ptree& rTree)
+void SfxLokHelper::notifyOtherView(const SfxViewShell* pThisView, SfxViewShell const* pOtherView,
+ int nType, const boost::property_tree::ptree& rTree)
{
+ assert(pThisView != nullptr && "pThisView must be valid");
if (DisableCallbacks::disabled())
return;
pOtherView->libreOfficeKitViewCallback(nType, lcl_generateJSON(pThisView, rTree).getStr());
}
-void SfxLokHelper::notifyOtherViews(SfxViewShell* pThisView, int nType, const OString& rKey, const OString& rPayload)
+void SfxLokHelper::notifyOtherViews(const SfxViewShell* pThisView, int nType, const OString& rKey,
+ const OString& rPayload)
{
if (DisableCallbacks::disabled())
return;
@@ -336,7 +348,8 @@ void SfxLokHelper::notifyOtherViews(SfxViewShell* pThisView, int nType, const OS
}
}
-void SfxLokHelper::notifyOtherViews(SfxViewShell* pThisView, int nType, const boost::property_tree::ptree& rTree)
+void SfxLokHelper::notifyOtherViews(const SfxViewShell* pThisView, int nType,
+ const boost::property_tree::ptree& rTree)
{
if (SfxLokHelper::getViewsCount() <= 1 || DisableCallbacks::disabled())
return;
@@ -400,26 +413,26 @@ void SfxLokHelper::notifyWindow(const SfxViewShell* pThisView,
const OUString& rAction,
const std::vector<vcl::LOKPayloadItem>& rPayload)
{
- assert(pThisView);
+ assert(pThisView != nullptr && "pThisView must be valid");
if (SfxLokHelper::getViewsCount() <= 0 || nLOKWindowId == 0 || DisableCallbacks::disabled())
return;
OStringBuffer aPayload;
- aPayload.append("{ \"id\": \"").append(OString::number(nLOKWindowId)).append("\"");
- aPayload.append(", \"action\": \"").append(OUStringToOString(rAction, RTL_TEXTENCODING_UTF8)).append("\"");
+ aPayload.append("{ \"id\": \"").append(OString::number(nLOKWindowId)).append('"');
+ aPayload.append(", \"action\": \"").append(OUStringToOString(rAction, RTL_TEXTENCODING_UTF8)).append('"');
for (const auto& rItem: rPayload)
{
if (!rItem.first.isEmpty() && !rItem.second.isEmpty())
{
aPayload.append(", \"").append(rItem.first).append("\": \"")
- .append(rItem.second).append("\"");
+ .append(rItem.second).append('"');
}
}
- aPayload.append("}");
+ aPayload.append('}');
- auto s = aPayload.makeStringAndClear();
+ const OString s = aPayload.makeStringAndClear();
pThisView->libreOfficeKitViewCallback(LOK_CALLBACK_WINDOW, s.getStr());
}