1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
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>
31 #include <editeng/acorrcfg.hxx>
32 #include <sfx2/objface.hxx>
33 #include <sfx2/viewfrm.hxx>
36 #include <glshell.hxx>
38 #include <IDocumentUndoRedo.hxx>
39 #include <IDocumentDeviceAccess.hxx>
40 #include <IDocumentState.hxx>
41 #include <glosdoc.hxx>
42 #include <shellio.hxx>
45 #include <strings.hrc>
47 #define ShellClass_SwWebGlosDocShell
48 #define ShellClass_SwGlosDocShell
50 #include <sfx2/msg.hxx>
51 #include <swslots.hxx>
55 using namespace ::com::sun::star
;
57 SFX_IMPL_SUPERCLASS_INTERFACE(SwGlosDocShell
, SwDocShell
)
59 void SwGlosDocShell::InitInterface_Impl()
63 SFX_IMPL_SUPERCLASS_INTERFACE(SwWebGlosDocShell
, SwWebDocShell
)
65 void SwWebGlosDocShell::InitInterface_Impl()
70 static void lcl_Execute( SwDocShell
& rSh
, SfxRequest
& rReq
)
72 if ( rReq
.GetSlot() != SID_SAVEDOC
)
77 rReq
.SetReturnValue( SfxBoolItem( 0, rSh
.Save() ) );
81 const SfxBoolItem
* pRes
= static_cast< const SfxBoolItem
* >(
82 rSh
.ExecuteSlot( rReq
,
83 rSh
.SfxObjectShell::GetInterface() ));
84 if( pRes
->GetValue() )
85 rSh
.GetDoc()->getIDocumentState().ResetModified();
89 static void lcl_GetState( SwDocShell
& rSh
, SfxItemSet
& rSet
)
91 if( SfxItemState::DEFAULT
>= rSet
.GetItemState( SID_SAVEDOC
, false ))
93 if( !rSh
.GetDoc()->getIDocumentState().IsModified() )
94 rSet
.DisableItem( SID_SAVEDOC
);
96 rSet
.Put( SfxStringItem( SID_SAVEDOC
, SwResId(STR_SAVE_GLOSSARY
)));
100 static bool lcl_Save( SwWrtShell
& rSh
, const OUString
& rGroupName
,
101 const OUString
& rShortNm
, const OUString
& rLongNm
)
103 const SvxAutoCorrCfg
& rCfg
= SvxAutoCorrCfg::Get();
104 std::unique_ptr
<SwTextBlocks
> pBlock(::GetGlossaries()->GetGroupDoc( rGroupName
));
106 SvxMacro aStart
{ OUString(), OUString() };
107 SvxMacro aEnd
{ OUString(), OUString() };
108 SwGlossaryHdl
* pGlosHdl
;
110 pGlosHdl
= rSh
.GetView().GetGlosHdl();
111 pGlosHdl
->GetMacros( rShortNm
, aStart
, aEnd
, pBlock
.get() );
113 sal_uInt16 nRet
= rSh
.SaveGlossaryDoc( *pBlock
, rLongNm
, rShortNm
,
114 rCfg
.IsSaveRelFile(),
115 pBlock
->IsOnlyTextBlock( rShortNm
) );
117 if(aStart
.HasMacro() || aEnd
.HasMacro() )
119 SvxMacro
* pStart
= aStart
.HasMacro() ? &aStart
: nullptr;
120 SvxMacro
* pEnd
= aEnd
.HasMacro() ? &aEnd
: nullptr;
121 pGlosHdl
->SetMacros( rShortNm
, pStart
, pEnd
, pBlock
.get() );
125 if( USHRT_MAX
!= nRet
)
127 return nRet
!= USHRT_MAX
;
130 SwGlosDocShell::SwGlosDocShell(bool bNewShow
)
131 : SwDocShell( bNewShow
132 ? SfxObjectCreateMode::STANDARD
: SfxObjectCreateMode::INTERNAL
)
136 SwGlosDocShell::~SwGlosDocShell( )
140 void SwGlosDocShell::Execute( SfxRequest
& rReq
)
142 ::lcl_Execute( *this, rReq
);
145 void SwGlosDocShell::GetState( SfxItemSet
& rSet
)
147 ::lcl_GetState( *this, rSet
);
150 bool SwGlosDocShell::Save()
152 // In case of an API object which holds this document, it is possible that the WrtShell is already
153 // dead. For instance, if the doc is modified via this API object, and then, upon office shutdown,
154 // the document's view is closed (by the SFX framework) _before_ the API object is release and
155 // tries to save the doc, again.
156 // 96380 - 2002-03-03 - fs@openoffice.org
158 return ::lcl_Save( *GetWrtShell(), m_aGroupName
, m_aShortName
, m_aLongName
);
161 SetModified( false );
166 SwWebGlosDocShell::SwWebGlosDocShell()
170 SwWebGlosDocShell::~SwWebGlosDocShell( )
174 void SwWebGlosDocShell::Execute( SfxRequest
& rReq
)
176 ::lcl_Execute( *this, rReq
);
179 void SwWebGlosDocShell::GetState( SfxItemSet
& rSet
)
181 ::lcl_GetState( *this, rSet
);
184 bool SwWebGlosDocShell::Save()
186 // same comment as in SwGlosDocShell::Save - see there
188 return ::lcl_Save( *GetWrtShell(), m_aGroupName
, m_aShortName
, m_aLongName
);
191 SetModified( false );
196 SwDocShellRef
SwGlossaries::EditGroupDoc( const OUString
& rGroup
, const OUString
& rShortName
, bool bShow
)
198 SwDocShellRef xDocSh
;
200 std::unique_ptr
<SwTextBlocks
> pGroup
= GetGroupDoc( rGroup
);
201 if (pGroup
&& pGroup
->GetCount())
203 // query which view is registered. In WebWriter there is no normal view
204 SfxInterfaceId nViewId
= nullptr != SwView::Factory() ? SFX_INTERFACE_SFXDOCSH
: SfxInterfaceId(6);
205 const OUString sLongName
= pGroup
->GetLongName(pGroup
->GetIndex( rShortName
));
207 if( SfxInterfaceId(6) == nViewId
)
209 SwWebGlosDocShell
* pDocSh
= new SwWebGlosDocShell();
212 pDocSh
->SetLongName( sLongName
);
213 pDocSh
->SetShortName( rShortName
);
214 pDocSh
->SetGroupName( rGroup
);
218 SwGlosDocShell
* pDocSh
= new SwGlosDocShell(bShow
);
221 pDocSh
->SetLongName( sLongName
);
222 pDocSh
->SetShortName( rShortName
);
223 pDocSh
->SetGroupName( rGroup
);
226 // set document title
227 SfxViewFrame
* pFrame
= bShow
? SfxViewFrame::LoadDocument( *xDocSh
, nViewId
) : SfxViewFrame::LoadHiddenDocument( *xDocSh
, nViewId
);
228 const OUString
aDocTitle(SwResId( STR_GLOSSARY
) + " " + sLongName
);
230 bool const bDoesUndo
=
231 xDocSh
->GetDoc()->GetIDocumentUndoRedo().DoesUndo();
232 xDocSh
->GetDoc()->GetIDocumentUndoRedo().DoUndo( false );
234 xDocSh
->GetWrtShell()->InsertGlossary( *pGroup
, rShortName
);
235 if( !xDocSh
->GetDoc()->getIDocumentDeviceAccess().getPrinter( false ) )
237 // we create a default SfxPrinter.
238 // ItemSet is deleted by Sfx!
239 auto pSet
= std::make_unique
<SfxItemSetFixed
<
240 SID_PRINTER_NOTFOUND_WARN
, SID_PRINTER_NOTFOUND_WARN
,
241 SID_PRINTER_CHANGESTODOC
, SID_PRINTER_CHANGESTODOC
,
242 FN_PARAM_ADDPRINTER
, FN_PARAM_ADDPRINTER
>>
243 ( xDocSh
->GetDoc()->GetAttrPool() );
244 VclPtr
<SfxPrinter
> pPrinter
= VclPtr
<SfxPrinter
>::Create( std::move(pSet
) );
246 // and append it to the document.
247 xDocSh
->GetDoc()->getIDocumentDeviceAccess().setPrinter( pPrinter
, true, true );
250 xDocSh
->SetTitle( aDocTitle
);
254 uno::Reference
< frame::XTitle
> xTitle( xDocSh
->GetModel(), uno::UNO_QUERY_THROW
);
255 xTitle
->setTitle( aDocTitle
);
257 catch (const uno::Exception
&)
261 xDocSh
->GetDoc()->GetIDocumentUndoRedo().DoUndo( bDoesUndo
);
262 xDocSh
->GetDoc()->getIDocumentState().ResetModified();
264 pFrame
->GetFrame().Appear();
269 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */