summaryrefslogtreecommitdiff
path: root/sw/source/core/model/SearchResultLocator.cxx
blob: a0e405b2ed33330dd02dc99a736993ddae5b581f (plain)
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
/* -*- 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 <SearchResultLocator.hxx>
#include <node.hxx>
#include <drawdoc.hxx>
#include <frame.hxx>
#include <cntfrm.hxx>
#include <viewsh.hxx>
#include <IDocumentDrawModelAccess.hxx>
#include <IDocumentLayoutAccess.hxx>

#include <tools/XmlWalker.hxx>
#include <tools/stream.hxx>

#include <svx/svdpage.hxx>
#include <svx/svdobj.hxx>

namespace sw::search
{
void SearchResultLocator::findOne(LocationResult& rResult, SearchIndexData const& rSearchIndexData)
{
    if (rSearchIndexData.meType == NodeType::WriterNode)
    {
        SwNodes const& rNodes = mpDocument->GetNodes();
        if (rSearchIndexData.mnNodeIndex >= rNodes.Count())
            return;
        SwNode* pNode = rNodes[rSearchIndexData.mnNodeIndex];

        auto* pContentNode = pNode->GetContentNode();
        auto* pShell = mpDocument->getIDocumentLayoutAccess().GetCurrentViewShell();

        if (pContentNode && pShell)
        {
            const SwFrame* pFrame
                = pContentNode->getLayoutFrame(pShell->GetLayout(), nullptr, nullptr);
            SwRect const& rArea = pFrame->getFrameArea();

            rResult.mbFound = true;
            rResult.maRectangles.emplace_back(rArea.Left(), rArea.Top(),
                                              rArea.Left() + rArea.Width(),
                                              rArea.Top() + rArea.Height());
        }
    }
    else if (rSearchIndexData.meType == NodeType::SdrObject)
    {
        IDocumentDrawModelAccess& rDrawModelAccess = mpDocument->getIDocumentDrawModelAccess();
        auto* pModel = rDrawModelAccess.GetDrawModel();
        for (sal_uInt16 nPage = 0; nPage < pModel->GetPageCount(); ++nPage)
        {
            SdrPage* pPage = pModel->GetPage(nPage);
            for (size_t nObject = 0; nObject < pPage->GetObjCount(); ++nObject)
            {
                SdrObject* pObject = pPage->GetObj(nObject);
                if (pObject)
                {
                    if (pObject->GetName() == rSearchIndexData.maObjectName)
                    {
                        auto aRect = o3tl::convert(pObject->GetLogicRect(), o3tl::Length::mm100,
                                                   o3tl::Length::twip);
                        rResult.mbFound = true;
                        rResult.maRectangles.emplace_back(aRect.Left(), aRect.Top(),
                                                          aRect.Left() + aRect.GetWidth(),
                                                          aRect.Top() + aRect.GetHeight());
                    }
                }
            }
        }
    }
}

LocationResult SearchResultLocator::find(std::vector<SearchIndexData> const& rSearchIndexDataVector)
{
    LocationResult aResult;
    for (auto const& rSearchIndexData : rSearchIndexDataVector)
        findOne(aResult, rSearchIndexData);

    return aResult;
}

LocationResult SearchResultLocator::findForPayload(const char* pPayload)
{
    LocationResult aResult;

    const OString aPayloadString(pPayload);

    SvMemoryStream aStream(const_cast<char*>(aPayloadString.getStr()), aPayloadString.getLength(),
                           StreamMode::READ);
    tools::XmlWalker aWalker;

    if (!aWalker.open(&aStream))
        return aResult;

    if (aWalker.name() == "indexing")
    {
        std::vector<sw::search::SearchIndexData> aDataVector;
        aWalker.children();
        while (aWalker.isValid())
        {
            if (aWalker.name() == "paragraph")
            {
                OString sType = aWalker.attribute("type");
                OString sIndex = aWalker.attribute("index");

                if (!sType.isEmpty() && !sIndex.isEmpty())
                {
                    sw::search::SearchIndexData aData;
                    aData.mnNodeIndex = sIndex.toInt32();
                    aData.meType = sw::search::NodeType(sType.toInt32());

                    aDataVector.push_back(aData);
                }
            }
            aWalker.next();
        }
        aWalker.parent();

        if (!aDataVector.empty())
        {
            for (auto const& rSearchIndexData : aDataVector)
                findOne(aResult, rSearchIndexData);
        }
    }

    return aResult;
}

} // end sw namespace

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */