android: Update app-specific/MIME type icons
[LibreOffice.git] / sw / source / uibase / misc / glshell.cxx
blobaece4e8c18b8283a56d31ed29e19f75d00a7170f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
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>
47 #define ShellClass_SwWebGlosDocShell
48 #define ShellClass_SwGlosDocShell
50 #include <sfx2/msg.hxx>
51 #include <swslots.hxx>
52 #include <memory>
53 #include <utility>
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 )
73 return;
75 if( !rSh.HasName() )
77 rReq.SetReturnValue( SfxBoolItem( 0, rSh.Save() ) );
79 else
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 );
95 else
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() );
124 rSh.EnterStdMode();
125 if( USHRT_MAX != nRet )
126 rSh.ResetModified();
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
157 if ( GetWrtShell() )
158 return ::lcl_Save( *GetWrtShell(), m_aGroupName, m_aShortName, m_aLongName );
159 else
161 SetModified( false );
162 return 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
187 if ( GetWrtShell() )
188 return ::lcl_Save( *GetWrtShell(), m_aGroupName, m_aShortName, m_aLongName );
189 else
191 SetModified( false );
192 return 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();
210 xDocSh = pDocSh;
211 pDocSh->DoInitNew();
212 pDocSh->SetLongName( sLongName );
213 pDocSh->SetShortName( rShortName);
214 pDocSh->SetGroupName( rGroup );
216 else
218 SwGlosDocShell* pDocSh = new SwGlosDocShell(bShow);
219 xDocSh = pDocSh;
220 pDocSh->DoInitNew();
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 );
253 // set the UI-title
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();
263 if ( bShow )
264 pFrame->GetFrame().Appear();
266 return xDocSh;
269 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */