Coverage Report

Created: 2025-07-07 10:01

/src/libreoffice/sw/source/uibase/misc/glshell.cxx
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
5
 * This Source Code Form is subject to the terms of the Mozilla Public
6
 * License, v. 2.0. If a copy of the MPL was not distributed with this
7
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
 *
9
 * This file incorporates work covered by the following license notice:
10
 *
11
 *   Licensed to the Apache Software Foundation (ASF) under one or more
12
 *   contributor license agreements. See the NOTICE file distributed
13
 *   with this work for additional information regarding copyright
14
 *   ownership. The ASF licenses this file to you under the Apache
15
 *   License, Version 2.0 (the "License"); you may not use this file
16
 *   except in compliance with the License. You may obtain a copy of
17
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18
 */
19
20
#include <sal/config.h>
21
22
#include <com/sun/star/frame/XModel.hpp>
23
#include <com/sun/star/frame/XTitle.hpp>
24
#include <svl/eitem.hxx>
25
#include <svl/stritem.hxx>
26
#include <sfx2/printer.hxx>
27
#include <sfx2/request.hxx>
28
#include <svl/macitem.hxx>
29
#include <gloshdl.hxx>
30
31
#include <editeng/acorrcfg.hxx>
32
#include <sfx2/objface.hxx>
33
#include <sfx2/viewfrm.hxx>
34
#include <wrtsh.hxx>
35
#include <view.hxx>
36
#include <glshell.hxx>
37
#include <doc.hxx>
38
#include <IDocumentUndoRedo.hxx>
39
#include <IDocumentDeviceAccess.hxx>
40
#include <IDocumentState.hxx>
41
#include <glosdoc.hxx>
42
#include <shellio.hxx>
43
#include <initui.hxx>
44
#include <cmdid.h>
45
#include <strings.hrc>
46
47
#define ShellClass_SwWebGlosDocShell
48
#define ShellClass_SwGlosDocShell
49
50
#include <sfx2/msg.hxx>
51
#include <swslots.hxx>
52
#include <memory>
53
#include <utility>
54
55
using namespace ::com::sun::star;
56
57
SFX_IMPL_SUPERCLASS_INTERFACE(SwGlosDocShell, SwDocShell)
58
59
void SwGlosDocShell::InitInterface_Impl()
60
9
{
61
9
}
62
63
SFX_IMPL_SUPERCLASS_INTERFACE(SwWebGlosDocShell, SwWebDocShell)
64
65
void SwWebGlosDocShell::InitInterface_Impl()
66
9
{
67
9
}
68
69
70
static void lcl_Execute( SwDocShell& rSh, SfxRequest& rReq )
71
0
{
72
0
    if ( rReq.GetSlot() != SID_SAVEDOC )
73
0
        return;
74
75
0
    if( !rSh.HasName() )
76
0
    {
77
0
        rReq.SetReturnValue( SfxBoolItem( 0, rSh.Save() ) );
78
0
    }
79
0
    else
80
0
    {
81
0
        const SfxPoolItemHolder& rResult(
82
0
            rSh.ExecuteSlot(rReq,
83
0
            rSh.SfxObjectShell::GetInterface()));
84
0
        const SfxBoolItem* pRes(static_cast< const SfxBoolItem* >(rResult.getItem()));
85
0
        if( pRes->GetValue() )
86
0
            rSh.GetDoc()->getIDocumentState().ResetModified();
87
0
    }
88
0
}
89
90
static void lcl_GetState( SwDocShell& rSh, SfxItemSet& rSet )
91
0
{
92
0
    if( SfxItemState::DEFAULT >= rSet.GetItemState( SID_SAVEDOC, false ))
93
0
    {
94
0
        if( !rSh.GetDoc()->getIDocumentState().IsModified() )
95
0
            rSet.DisableItem( SID_SAVEDOC );
96
0
        else
97
0
            rSet.Put( SfxStringItem( SID_SAVEDOC, SwResId(STR_SAVE_GLOSSARY)));
98
0
    }
99
0
}
100
101
static bool lcl_Save( SwWrtShell& rSh, const OUString& rGroupName,
102
                const OUString& rShortNm, const OUString& rLongNm )
103
0
{
104
0
    const SvxAutoCorrCfg& rCfg = SvxAutoCorrCfg::Get();
105
0
    std::unique_ptr<SwTextBlocks> pBlock(::GetGlossaries()->GetGroupDoc( rGroupName ));
106
107
0
    SvxMacro aStart { OUString(), OUString() };
108
0
    SvxMacro aEnd { OUString(), OUString() };
109
0
    SwGlossaryHdl* pGlosHdl;
110
111
0
    pGlosHdl = rSh.GetView().GetGlosHdl();
112
0
    pGlosHdl->GetMacros( rShortNm, aStart, aEnd, pBlock.get() );
113
114
0
    sal_uInt16 nRet = rSh.SaveGlossaryDoc( *pBlock, rLongNm, rShortNm,
115
0
                                rCfg.IsSaveRelFile(),
116
0
                                pBlock->IsOnlyTextBlock( rShortNm ) );
117
118
0
    if(aStart.HasMacro() || aEnd.HasMacro() )
119
0
    {
120
0
        SvxMacro* pStart = aStart.HasMacro() ? &aStart : nullptr;
121
0
        SvxMacro* pEnd = aEnd.HasMacro() ? &aEnd : nullptr;
122
0
        pGlosHdl->SetMacros( rShortNm, pStart, pEnd, pBlock.get() );
123
0
    }
124
125
0
    rSh.EnterStdMode();
126
0
    if( USHRT_MAX != nRet )
127
0
        rSh.ResetModified();
128
0
    return nRet != USHRT_MAX;
129
0
}
130
131
SwGlosDocShell::SwGlosDocShell(bool bNewShow)
132
0
    : SwDocShell( bNewShow
133
0
            ? SfxObjectCreateMode::STANDARD : SfxObjectCreateMode::INTERNAL )
134
0
{
135
0
}
136
137
SwGlosDocShell::~SwGlosDocShell(  )
138
0
{
139
0
}
140
141
void SwGlosDocShell::Execute( SfxRequest& rReq )
142
0
{
143
0
    ::lcl_Execute( *this, rReq );
144
0
}
145
146
void SwGlosDocShell::GetState( SfxItemSet& rSet )
147
0
{
148
0
    ::lcl_GetState( *this, rSet );
149
0
}
150
151
bool SwGlosDocShell::Save()
152
0
{
153
    // In case of an API object which holds this document, it is possible that the WrtShell is already
154
    // dead. For instance, if the doc is modified via this API object, and then, upon office shutdown,
155
    // the document's view is closed (by the SFX framework) _before_ the API object is release and
156
    // tries to save the doc, again.
157
    // 96380 - 2002-03-03 - fs@openoffice.org
158
0
    if ( GetWrtShell() )
159
0
        return ::lcl_Save( *GetWrtShell(), m_aGroupName, m_aShortName, m_aLongName );
160
0
    else
161
0
    {
162
0
        SetModified( false );
163
0
        return false;
164
0
    }
165
0
}
166
167
SwWebGlosDocShell::SwWebGlosDocShell()
168
0
{
169
0
}
170
171
SwWebGlosDocShell::~SwWebGlosDocShell(  )
172
0
{
173
0
}
174
175
void SwWebGlosDocShell::Execute( SfxRequest& rReq )
176
0
{
177
0
    ::lcl_Execute( *this, rReq );
178
0
}
179
180
void SwWebGlosDocShell::GetState( SfxItemSet& rSet )
181
0
{
182
0
    ::lcl_GetState( *this, rSet );
183
0
}
184
185
bool SwWebGlosDocShell::Save()
186
0
{
187
    // same comment as in SwGlosDocShell::Save - see there
188
0
    if ( GetWrtShell() )
189
0
        return ::lcl_Save( *GetWrtShell(), m_aGroupName, m_aShortName, m_aLongName );
190
0
    else
191
0
    {
192
0
        SetModified( false );
193
0
        return false;
194
0
    }
195
0
}
196
197
SwDocShellRef SwGlossaries::EditGroupDoc( const OUString& rGroup, const OUString& rShortName, bool bShow )
198
0
{
199
0
    SwDocShellRef xDocSh;
200
201
0
    std::unique_ptr<SwTextBlocks> pGroup = GetGroupDoc( rGroup );
202
0
    if (pGroup && pGroup->GetCount())
203
0
    {
204
        // query which view is registered. In WebWriter there is no normal view
205
0
        SfxInterfaceId nViewId = nullptr != SwView::Factory() ? SFX_INTERFACE_SFXDOCSH : SfxInterfaceId(6);
206
0
        const OUString sLongName = pGroup->GetLongName(pGroup->GetIndex( rShortName ));
207
208
0
        if( SfxInterfaceId(6) == nViewId )
209
0
        {
210
0
            rtl::Reference<SwWebGlosDocShell> pDocSh = new SwWebGlosDocShell();
211
0
            xDocSh = pDocSh;
212
0
            pDocSh->DoInitNew();
213
0
            pDocSh->SetLongName( sLongName );
214
0
            pDocSh->SetShortName( rShortName);
215
0
            pDocSh->SetGroupName( rGroup );
216
0
        }
217
0
        else
218
0
        {
219
0
            rtl::Reference<SwGlosDocShell> pDocSh = new SwGlosDocShell(bShow);
220
0
            xDocSh = pDocSh;
221
0
            pDocSh->DoInitNew();
222
0
            pDocSh->SetLongName( sLongName );
223
0
            pDocSh->SetShortName( rShortName );
224
0
            pDocSh->SetGroupName( rGroup );
225
0
        }
226
227
        // set document title
228
0
        SfxViewFrame* pFrame = bShow ? SfxViewFrame::LoadDocument( *xDocSh, nViewId ) : SfxViewFrame::LoadHiddenDocument( *xDocSh, nViewId );
229
0
        const OUString aDocTitle(SwResId( STR_GLOSSARY ) + " " + sLongName);
230
231
0
        bool const bDoesUndo =
232
0
            xDocSh->GetDoc()->GetIDocumentUndoRedo().DoesUndo();
233
0
        xDocSh->GetDoc()->GetIDocumentUndoRedo().DoUndo( false );
234
235
0
        xDocSh->GetWrtShell()->InsertGlossary( *pGroup, rShortName );
236
0
        if( !xDocSh->GetDoc()->getIDocumentDeviceAccess().getPrinter( false ) )
237
0
        {
238
            // we create a default SfxPrinter.
239
            // ItemSet is deleted by Sfx!
240
0
            auto pSet = std::make_unique<SfxItemSetFixed<
241
0
                    SID_PRINTER_NOTFOUND_WARN, SID_PRINTER_NOTFOUND_WARN,
242
0
                    SID_PRINTER_CHANGESTODOC, SID_PRINTER_CHANGESTODOC,
243
0
                    FN_PARAM_ADDPRINTER, FN_PARAM_ADDPRINTER>>
244
0
                        ( xDocSh->GetDoc()->GetAttrPool() );
245
0
            VclPtr<SfxPrinter> pPrinter = VclPtr<SfxPrinter>::Create( std::move(pSet) );
246
247
            // and append it to the document.
248
0
            xDocSh->GetDoc()->getIDocumentDeviceAccess().setPrinter( pPrinter, true, true );
249
0
        }
250
251
0
        xDocSh->SetTitle( aDocTitle );
252
0
        try
253
0
        {
254
            // set the UI-title
255
0
            uno::Reference< frame::XTitle > xTitle( xDocSh->GetModel(), uno::UNO_QUERY );
256
0
            if (xTitle)
257
0
                xTitle->setTitle( aDocTitle );
258
0
        }
259
0
        catch (const uno::Exception&)
260
0
        {
261
0
        }
262
263
0
        xDocSh->GetDoc()->GetIDocumentUndoRedo().DoUndo( bDoesUndo );
264
0
        xDocSh->GetDoc()->getIDocumentState().ResetModified();
265
0
        if ( bShow )
266
0
            pFrame->GetFrame().Appear();
267
0
    }
268
0
    return xDocSh;
269
0
}
270
271
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */