summaryrefslogtreecommitdiff
path: root/sw/source/ui/index/cntex.cxx
blob: 4d8542eee6fb87e6a7d4d52595e9e50cb1e56710 (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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/* -*- 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 <com/sun/star/text/XDocumentIndexesSupplier.hpp>
#include <com/sun/star/text/XTextSectionsSupplier.hpp>
#include <com/sun/star/text/XDependentTextField.hpp>
#include <com/sun/star/text/XDocumentIndex.hpp>
#include <com/sun/star/text/ChapterFormat.hpp>
#include <com/sun/star/text/XTextSection.hpp>
#include <com/sun/star/beans/PropertyValues.hpp>
#include <com/sun/star/ucb/XCommandEnvironment.hpp>
#include <comphelper/string.hxx>
#include <comphelper/servicehelper.hxx>
#include <tools/UnitConversion.hxx>
#include <comphelper/diagnose_ex.hxx>
#include <wrtsh.hxx>
#include <view.hxx>
#include <cnttab.hxx>
#include <unoprnms.hxx>
#include <unotools.hxx>
#include <unotxdoc.hxx>
#include <docsh.hxx>

#include <SwStyleNameMapper.hxx>
#include <swuicnttab.hxx>

using namespace ::com::sun::star;
using namespace ::com::sun::star::text;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::container;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::ucb;
using namespace ::com::sun::star::uno;
using namespace com::sun::star::ui::dialogs;

static void lcl_SetProp( uno::Reference< XPropertySetInfo > const & xInfo,
                           uno::Reference< XPropertySet > const & xProps,
                         OUString const & aPropName, const OUString& rValue)
{
    if(xInfo->hasPropertyByName(aPropName))
    {
        uno::Any aValue;
        aValue <<= rValue;
        xProps->setPropertyValue(aPropName, aValue);
    }
}

static void lcl_SetProp( uno::Reference< XPropertySetInfo > const & xInfo,
                           uno::Reference< XPropertySet > const & xProps,
                           OUString const & aPropName, sal_Int16 nValue )
{
    if(xInfo->hasPropertyByName(aPropName))
    {
        uno::Any aValue;
        aValue <<= nValue;
        xProps->setPropertyValue(aPropName, aValue);
    }
}

static void lcl_SetBOOLProp(
                uno::Reference< beans::XPropertySetInfo > const & xInfo,
                uno::Reference< beans::XPropertySet > const & xProps,
                OUString const & aPropName, bool bValue )
{
    if(xInfo->hasPropertyByName(aPropName))
    {
        xProps->setPropertyValue(aPropName, Any(bValue));
    }
}

IMPL_LINK_NOARG(SwMultiTOXTabDialog, CreateExample_Hdl, SwOneExampleFrame&, void)
{
    try
    {
        uno::Reference< frame::XModel > & xModel = m_xExampleFrame->GetModel();
        auto pDoc = comphelper::getFromUnoTunnel<SwXTextDocument>(xModel);

        if( pDoc )
            pDoc->GetDocShell()->LoadStyles_( *m_rWrtShell.GetView().GetDocShell(), true );

        uno::Reference< text::XTextSectionsSupplier >  xSectionSupplier(
                                                 xModel, uno::UNO_QUERY);
        uno::Reference< container::XNameAccess >  xSections =
                                        xSectionSupplier->getTextSections();

        for(int i = 0; i < 7; ++i )
        {
            OUString sTmp = "IndexSection_" + OUString::number(i);
            uno::Any aSection = xSections->getByName( sTmp );
            aSection >>= m_vTypeData[i].m_pxIndexSections->xContainerSection;
        }
        uno::Reference< text::XDocumentIndexesSupplier >  xIdxSupp(xModel, uno::UNO_QUERY);
        uno::Reference< container::XIndexAccess >  xIdxs = xIdxSupp->getDocumentIndexes();
        int n = xIdxs->getCount();
        while(n)
        {
            n--;
            uno::Any aIdx = xIdxs->getByIndex(n);
            uno::Reference< text::XDocumentIndex >  xIdx;
            aIdx >>= xIdx;
            xIdx->dispose();
        }
        CreateOrUpdateExample(m_eCurrentTOXType.eType);
    }
    catch (const Exception&)
    {
        TOOLS_WARN_EXCEPTION( "sw", "::CreateExample()");
    }
}

void SwMultiTOXTabDialog::CreateOrUpdateExample(
    TOXTypes nTOXIndex, sal_uInt16 nPage, sal_uInt16 nCurrentLevel)
{
    if(!m_xExampleFrame || !m_xExampleFrame->IsInitialized())
        return;

    try
    {
        static const char* IndexServiceNames[] =
        {
            "com.sun.star.text.DocumentIndex",
            "com.sun.star.text.UserIndex",
            "com.sun.star.text.ContentIndex",
            "com.sun.star.text.IllustrationsIndex",
            "com.sun.star.text.ObjectIndex",
            "com.sun.star.text.TableIndex",
            "com.sun.star.text.Bibliography"
        };

        OSL_ENSURE(m_vTypeData[nTOXIndex].m_pxIndexSections &&
                        m_vTypeData[nTOXIndex].m_pxIndexSections->xContainerSection.is(),
                            "Section not created");
        uno::Reference< frame::XModel > & xModel = m_xExampleFrame->GetModel();
        bool bInitialCreate = true;
        if(!m_vTypeData[nTOXIndex].m_pxIndexSections->xDocumentIndex.is())
        {
             bInitialCreate = true;
             if(!m_vTypeData[nTOXIndex].m_pxIndexSections->xContainerSection.is())
                 throw uno::RuntimeException();
             uno::Reference< text::XTextRange >  xAnchor = m_vTypeData[nTOXIndex].m_pxIndexSections->xContainerSection->getAnchor();
             xAnchor = xAnchor->getStart();
             uno::Reference< text::XTextCursor >  xCursor = xAnchor->getText()->createTextCursorByRange(xAnchor);

             uno::Reference< lang::XMultiServiceFactory >  xFact(xModel, uno::UNO_QUERY);

             OUString sIndexTypeName(OUString::createFromAscii( IndexServiceNames[
                    nTOXIndex <= TOX_AUTHORITIES ? nTOXIndex : TOX_USER] ));
             m_vTypeData[nTOXIndex].m_pxIndexSections->xDocumentIndex.set(xFact->createInstance(sIndexTypeName), uno::UNO_QUERY);
             uno::Reference< text::XTextContent >  xContent = m_vTypeData[nTOXIndex].m_pxIndexSections->xDocumentIndex;
             xCursor->getText()->insertTextContent(xCursor, xContent, false);
        }
        for(sal_uInt16 i = 0 ; i <= TOX_AUTHORITIES; i++)
        {
            uno::Reference< beans::XPropertySet >  xSectPr(m_vTypeData[i].m_pxIndexSections->xContainerSection, uno::UNO_QUERY);
            if(xSectPr.is())
            {
                xSectPr->setPropertyValue(UNO_NAME_IS_VISIBLE, Any(i == nTOXIndex));
            }
        }
        // set properties
        uno::Reference< beans::XPropertySet >  xIdxProps(m_vTypeData[nTOXIndex].m_pxIndexSections->xDocumentIndex, uno::UNO_QUERY);
        uno::Reference< beans::XPropertySetInfo >  xInfo = xIdxProps->getPropertySetInfo();
        SwTOXDescription& rDesc = GetTOXDescription(m_eCurrentTOXType);
        SwTOIOptions nIdxOptions = rDesc.GetIndexOptions();
        if(bInitialCreate || !nPage || nPage == TOX_PAGE_SELECT)
        {
            //title
            if(rDesc.GetTitle())
                lcl_SetProp(xInfo, xIdxProps, UNO_NAME_TITLE, *rDesc.GetTitle());

            //stylenames
            SwTOXElement  nContentOptions = rDesc.GetContentOptions();
            if(xInfo->hasPropertyByName(UNO_NAME_LEVEL_PARAGRAPH_STYLES))
            {
                bool bOn( nContentOptions&SwTOXElement::Template );
                uno::Any aStyleNames(xIdxProps->getPropertyValue(UNO_NAME_LEVEL_PARAGRAPH_STYLES));
                uno::Reference< container::XIndexReplace >  xAcc;
                aStyleNames >>= xAcc;

                for(sal_uInt16 i = 0; i < MAXLEVEL; i++)
                {
                    OUString sLevel;
                    if(bOn)
                        sLevel = rDesc.GetStyleNames(i);
                    const sal_Int32 nStyles =
                        comphelper::string::getTokenCount(sLevel, TOX_STYLE_DELIMITER);
                    uno::Sequence<OUString> aStyles(nStyles);
                    OUString* pArr = aStyles.getArray();
                    sal_Int32 nPos {0};
                    for(sal_Int32 nStyle = 0; nStyle < nStyles; ++nStyle)
                        pArr[nStyle] = sLevel.getToken(0, TOX_STYLE_DELIMITER, nPos);
                    uno::Any aAny(&aStyles, cppu::UnoType<uno::Sequence<OUString>>::get());
                    xAcc->replaceByIndex(i, aAny);
                }
            }
            lcl_SetProp(xInfo, xIdxProps, UNO_NAME_LEVEL, static_cast<sal_Int16>(rDesc.GetLevel()));
            lcl_SetBOOLProp(xInfo, xIdxProps, UNO_NAME_CREATE_FROM_MARKS,           bool(nContentOptions & SwTOXElement::Mark        ));
            lcl_SetBOOLProp(xInfo, xIdxProps, UNO_NAME_CREATE_FROM_OUTLINE,         bool(nContentOptions & SwTOXElement::OutlineLevel));
            lcl_SetBOOLProp(xInfo, xIdxProps, UNO_NAME_CREATE_FROM_EMBEDDED_OBJECTS,bool(nContentOptions & SwTOXElement::Ole            ));
            lcl_SetBOOLProp(xInfo, xIdxProps, UNO_NAME_CREATE_FROM_TABLES ,         bool(nContentOptions & SwTOXElement::Table          ));
            lcl_SetBOOLProp(xInfo, xIdxProps, UNO_NAME_CREATE_FROM_GRAPHIC_OBJECTS, bool(nContentOptions & SwTOXElement::Graphic        ));
            lcl_SetBOOLProp(xInfo, xIdxProps, UNO_NAME_CREATE_FROM_TEXT_FRAMES,     bool(nContentOptions & SwTOXElement::Frame          ));
            lcl_SetBOOLProp(xInfo, xIdxProps, UNO_NAME_CREATE_FROM_LABELS,          bool(nContentOptions & SwTOXElement::Sequence       ));
            lcl_SetBOOLProp(xInfo, xIdxProps, UNO_NAME_HIDE_TAB_LEADER_AND_PAGE_NUMBERS, bool(nContentOptions & SwTOXElement::TableLeader       ));
            lcl_SetBOOLProp(xInfo, xIdxProps, UNO_NAME_TAB_IN_TOC,                  bool(nContentOptions & SwTOXElement::TableInToc       ));
            lcl_SetBOOLProp(xInfo, xIdxProps, UNO_NAME_TOC_NEWLINE,                 bool(nContentOptions & SwTOXElement::Newline));
            lcl_SetBOOLProp(xInfo, xIdxProps, UNO_NAME_TOC_PARAGRAPH_OUTLINE_LEVEL, bool(nContentOptions & SwTOXElement::ParagraphOutlineLevel));

            lcl_SetBOOLProp(xInfo, xIdxProps, UNO_NAME_CREATE_FROM_CHAPTER, rDesc.IsFromChapter());
            lcl_SetBOOLProp(xInfo, xIdxProps, UNO_NAME_IS_PROTECTED, rDesc.IsReadonly());

            lcl_SetBOOLProp(xInfo, xIdxProps, UNO_NAME_USE_COMBINED_ENTRIES,        bool(nIdxOptions & SwTOIOptions::SameEntry        ));
            lcl_SetBOOLProp(xInfo, xIdxProps, UNO_NAME_USE_P_P,                     bool(nIdxOptions & SwTOIOptions::FF                   ));
            lcl_SetBOOLProp(xInfo, xIdxProps, UNO_NAME_IS_CASE_SENSITIVE,           bool(nIdxOptions & SwTOIOptions::CaseSensitive     ));
            lcl_SetBOOLProp(xInfo, xIdxProps, UNO_NAME_USE_KEY_AS_ENTRY,            bool(nIdxOptions & SwTOIOptions::KeyAsEntry     ));
            lcl_SetBOOLProp(xInfo, xIdxProps, UNO_NAME_USE_ALPHABETICAL_SEPARATORS, bool(nIdxOptions & SwTOIOptions::AlphaDelimiter));
            lcl_SetBOOLProp(xInfo, xIdxProps, UNO_NAME_USE_DASH,                    bool(nIdxOptions & SwTOIOptions::Dash             ));
            lcl_SetBOOLProp(xInfo, xIdxProps, UNO_NAME_USE_UPPER_CASE,              bool(nIdxOptions & SwTOIOptions::InitialCaps     ));

            OUString aTmpName( SwStyleNameMapper::GetSpecialExtraProgName( rDesc.GetSequenceName() ) );
            lcl_SetProp(xInfo, xIdxProps, UNO_NAME_LABEL_CATEGORY, aTmpName );
            lcl_SetBOOLProp(xInfo, xIdxProps, UNO_NAME_CREATE_FROM_LABELS,  !rDesc.IsCreateFromObjectNames());

            sal_Int16 nSet = text::ChapterFormat::NAME_NUMBER;
            switch (rDesc.GetCaptionDisplay())
            {
                case CAPTION_COMPLETE:  nSet = text::ChapterFormat::NAME_NUMBER;break;
                case CAPTION_NUMBER  :  nSet = text::ChapterFormat::NUMBER; break;
                case CAPTION_TEXT    :  nSet = text::ChapterFormat::NAME;      break;
            }
            lcl_SetProp(xInfo, xIdxProps, UNO_NAME_LABEL_DISPLAY_TYPE, nSet);

            SwTOOElements  nOLEOptions = rDesc.GetOLEOptions();
            lcl_SetBOOLProp(xInfo, xIdxProps, UNO_NAME_CREATE_FROM_STAR_MATH,   bool(SwTOOElements::Math &nOLEOptions           ));
            lcl_SetBOOLProp(xInfo, xIdxProps, UNO_NAME_CREATE_FROM_STAR_CHART,  bool(SwTOOElements::Chart    &nOLEOptions       ));
            lcl_SetBOOLProp(xInfo, xIdxProps, UNO_NAME_CREATE_FROM_STAR_CALC,   bool(SwTOOElements::Calc &nOLEOptions           ));
            lcl_SetBOOLProp(xInfo, xIdxProps, UNO_NAME_CREATE_FROM_STAR_DRAW,   bool(SwTOOElements::DrawImpress&nOLEOptions));
            lcl_SetBOOLProp(xInfo, xIdxProps, UNO_NAME_CREATE_FROM_OTHER_EMBEDDED_OBJECTS, bool(SwTOOElements::Other & nOLEOptions));
        }
        const SwForm* pForm = GetForm(m_eCurrentTOXType);
        if(bInitialCreate || !nPage || nPage == TOX_PAGE_ENTRY)
        {
            lcl_SetBOOLProp(xInfo, xIdxProps, UNO_NAME_IS_COMMA_SEPARATED, pForm->IsCommaSeparated());
            lcl_SetBOOLProp(xInfo, xIdxProps, UNO_NAME_USE_ALPHABETICAL_SEPARATORS, bool(nIdxOptions&SwTOIOptions::AlphaDelimiter));
            const bool bUseCurrent = nCurrentLevel < pForm->GetFormMax();
            const sal_uInt16 nStartLevel = bUseCurrent ? nCurrentLevel : 0;
            const sal_uInt16 nEndLevel = bUseCurrent ? nCurrentLevel : pForm->GetFormMax() - 1;
            if(xInfo->hasPropertyByName(UNO_NAME_LEVEL_FORMAT))
            {
                for(sal_uInt16 nCurrLevel = nStartLevel; nCurrLevel <= nEndLevel; nCurrLevel++)
                {
                    OUString sTokenType;
                    uno::Sequence< beans::PropertyValues> aSequPropVals(10);
                    tools::Long nTokenIndex = 0;
                    tools::Long nParamCount = 2;

                    // #i24377#
                    SwFormTokens aPattern = pForm->GetPattern(nCurrLevel);

                    for(const auto& aToken : aPattern)
                    {
                        if( aSequPropVals.getLength() <= nTokenIndex)
                            aSequPropVals.realloc(nTokenIndex + 10);

                        switch(aToken.eTokenType)
                        {
                            case TOKEN_ENTRY_NO     :
                                sTokenType = "TokenEntryNumber";
                                // numbering for content index
                            break;
                            case TOKEN_ENTRY_TEXT   :
                            case TOKEN_ENTRY        :
                                sTokenType = "TokenEntryText";
                            break;
                            case TOKEN_TAB_STOP     :
                                nParamCount += 3;
                                sTokenType = "TokenTabStop";
                            break;
                            case TOKEN_TEXT         :
                                sTokenType = "TokenText";
                                nParamCount += 1;
                            break;
                            case TOKEN_PAGE_NUMS    :
                                sTokenType = "TokenPageNumber";
                            break;
                            case TOKEN_CHAPTER_INFO :
                                sTokenType = "TokenChapterInfo";
                            break;
                            case TOKEN_LINK_START   :
                                sTokenType = "TokenHyperlinkStart";
                            break;
                            case TOKEN_LINK_END     :
                                sTokenType = "TokenHyperlinkEnd";
                            break;
                            case TOKEN_AUTHORITY :
                            {
                                sTokenType = "TokenBibliographyDataField";
                            }
                            break;
                            default:; //prevent warning
                        }
                        beans::PropertyValues aPropVals(nParamCount);
                        beans::PropertyValue* pPropValArr = aPropVals.getArray();
                        pPropValArr[0].Name = "TokenType";
                        pPropValArr[0].Value <<= sTokenType;
                        pPropValArr[1].Name = "CharacterStyleName";
                        pPropValArr[1].Value <<= aToken.sCharStyleName;
                        if(TOKEN_TAB_STOP == aToken.eTokenType)
                        {
                            pPropValArr[2].Name = "TabStopRightAligned";
                            pPropValArr[2].Value <<= SvxTabAdjust::End == aToken.eTabAlign;
                            pPropValArr[3].Name = "TabStopFillCharacter";
                            pPropValArr[3].Value <<= OUString(aToken.cTabFillChar);
                            pPropValArr[4].Name = "TabStopPosition";
                            SwTwips nTempPos = aToken.nTabStopPosition >= 0 ?
                                                            aToken.nTabStopPosition : 0;
                            nTempPos = convertTwipToMm100(nTempPos);
                            pPropValArr[4].Value <<= static_cast<sal_Int32>(nTempPos);
                        }
                        else if(TOKEN_TEXT == aToken.eTokenType)
                        {
                            pPropValArr[2].Name = "Text";
                            pPropValArr[2].Value <<= aToken.sText;
                        }
                        beans::PropertyValues* pValues = aSequPropVals.getArray();
                        pValues[nTokenIndex] = aPropVals;
                        nTokenIndex++;
                    }
                    aSequPropVals.realloc(nTokenIndex);

                    uno::Any aFormatAccess = xIdxProps->getPropertyValue(UNO_NAME_LEVEL_FORMAT);
                    OSL_ENSURE(aFormatAccess.getValueType() == cppu::UnoType<container::XIndexReplace>::get(),
                        "wrong property type");

                    uno::Reference< container::XIndexReplace >  xFormatAccess;
                    aFormatAccess >>= xFormatAccess;
                    uno::Any aLevelProp(&aSequPropVals, cppu::UnoType<uno::Sequence<beans::PropertyValues>>::get());
                    xFormatAccess->replaceByIndex(nCurrLevel, aLevelProp);
                }
            }
        }
        if(bInitialCreate || !nPage || nPage == TOX_PAGE_STYLES)
        {
            lcl_SetProp(xInfo, xIdxProps, "ParaStyleHeading", pForm->GetTemplate(0));
            sal_uInt16 nOffset = 0;
            sal_uInt16 nEndLevel = 2;
            switch(m_eCurrentTOXType.eType)
            {
                case  TOX_INDEX:
                {
                    nOffset = 1;
                    nEndLevel = 4;
                    lcl_SetProp(xInfo, xIdxProps, "ParaStyleSeparator", pForm->GetTemplate(1));
                }
                break;
                case TOX_CONTENT :
                    nEndLevel = 11;
                break;
                default:; //prevent warning
            }
            for(sal_uInt16 i = 1; i < nEndLevel; i++)
            {
                lcl_SetProp(xInfo,
                    xIdxProps,
                    "ParaStyleLevel" + OUString::number( i ),
                    pForm->GetTemplate(i + nOffset));
            }
        }
        m_vTypeData[nTOXIndex].m_pxIndexSections->xDocumentIndex->update();

    }
    catch (const Exception&)
    {
        TOOLS_WARN_EXCEPTION( "sw", "::CreateExample()");
    }
    m_xExampleFrame->Invalidate();
}

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