LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / sfx2 / source / appl / appmisc.cxx
blobeb5f0ce21062f31ea977e4ca2017d71e94205b3c
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 <config_folders.h>
21 #include <ucbhelper/content.hxx>
23 #include <vcl/canvastools.hxx>
24 #include <vcl/vectorgraphicdata.hxx>
25 #include <com/sun/star/rendering/XIntegerReadOnlyBitmap.hpp>
26 #include <com/sun/star/beans/PropertyValue.hpp>
27 #include <com/sun/star/graphic/Primitive2DTools.hpp>
28 #include <com/sun/star/uno/Reference.h>
29 #include <unotools/configmgr.hxx>
30 #include <comphelper/processfactory.hxx>
31 #include <rtl/bootstrap.hxx>
32 #include <svl/stritem.hxx>
33 #include <tools/urlobj.hxx>
35 #include <sfx2/app.hxx>
36 #include <appdata.hxx>
37 #include <sfx2/dispatch.hxx>
38 #include <sfx2/module.hxx>
39 #include <sfx2/msgpool.hxx>
40 #include <sfx2/sfxsids.hrc>
41 #include <sfx2/viewfrm.hxx>
42 #include <sfx2/objface.hxx>
43 #include <basegfx/matrix/b2dhommatrixtools.hxx>
44 #include <drawinglayer/primitive2d/transformprimitive2d.hxx>
46 using namespace ::com::sun::star;
47 using namespace ::com::sun::star::uno;
48 using namespace ::com::sun::star::util;
49 using namespace ::com::sun::star::beans;
50 using namespace ::com::sun::star::container;
52 #define ShellClass_SfxApplication
53 #include <sfxslots.hxx>
55 SFX_IMPL_INTERFACE(SfxApplication,SfxShell)
57 void SfxApplication::InitInterface_Impl()
59 GetStaticInterface()->RegisterStatusBar(StatusBarId::GenericStatusBar);
61 GetStaticInterface()->RegisterChildWindow(SID_DOCKWIN_0);
62 GetStaticInterface()->RegisterChildWindow(SID_DOCKWIN_1);
63 GetStaticInterface()->RegisterChildWindow(SID_DOCKWIN_2);
64 GetStaticInterface()->RegisterChildWindow(SID_DOCKWIN_3);
65 GetStaticInterface()->RegisterChildWindow(SID_DOCKWIN_4);
66 GetStaticInterface()->RegisterChildWindow(SID_DOCKWIN_5);
67 GetStaticInterface()->RegisterChildWindow(SID_DOCKWIN_6);
68 GetStaticInterface()->RegisterChildWindow(SID_DOCKWIN_7);
69 GetStaticInterface()->RegisterChildWindow(SID_DOCKWIN_8);
70 GetStaticInterface()->RegisterChildWindow(SID_DOCKWIN_9);
73 /** Returns the running SfxProgress for the entire application or 0 if
74 none is running for the entire application.
76 [Cross-reference]
78 <SfxProgress::GetActiveProgress(SfxViewFrame*)>
79 <SfxViewFrame::GetProgress()const>
81 SfxProgress* SfxApplication::GetProgress() const
83 return pImpl->pProgress;
86 SfxModule* SfxApplication::GetModule_Impl()
88 SfxModule* pModule = SfxModule::GetActiveModule();
89 if ( !pModule )
90 pModule = SfxModule::GetActiveModule( SfxViewFrame::GetFirst( nullptr, false ) );
91 if( pModule )
92 return pModule;
93 else
95 OSL_FAIL( "No module!" );
96 return nullptr;
100 bool SfxApplication::IsDowning() const { return pImpl->bDowning; }
101 SfxDispatcher* SfxApplication::GetAppDispatcher_Impl() { return &*pImpl->pAppDispat; }
102 SfxSlotPool& SfxApplication::GetAppSlotPool_Impl() const { return *pImpl->pSlotPool; }
104 static bool FileExists( const INetURLObject& rURL )
106 bool bRet = false;
108 if( rURL.GetProtocol() != INetProtocol::NotValid )
112 ::ucbhelper::Content aCnt( rURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), uno::Reference< ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
113 OUString aTitle;
115 aCnt.getPropertyValue("Title") >>= aTitle;
116 bRet = ( !aTitle.isEmpty() );
118 catch(const Exception&)
120 return false;
124 return bRet;
127 bool SfxApplication::loadBrandSvg(const char *pName, BitmapEx &rBitmap, int nWidth)
129 // Load from disk
131 OUString aBaseName = "/" + OUString::createFromAscii( pName );
133 OUString uri = "$BRAND_BASE_DIR/" LIBO_ETC_FOLDER + aBaseName + ".svg";
134 rtl::Bootstrap::expandMacros( uri );
136 INetURLObject aObj( uri );
137 if ( !FileExists(aObj) )
138 return false;
140 VectorGraphicData aVectorGraphicData(aObj.PathToFileName(), VectorGraphicDataType::Svg);
142 // transform into [0,0,width,width*aspect] std dimensions
144 basegfx::B2DRange aRange(aVectorGraphicData.getRange());
145 const double fAspectRatio(
146 aRange.getHeight() == 0.0 ? 1.0 : aRange.getWidth()/aRange.getHeight());
147 basegfx::B2DHomMatrix aTransform(
148 basegfx::utils::createTranslateB2DHomMatrix(
149 -aRange.getMinX(),
150 -aRange.getMinY()));
151 aTransform.scale(
152 aRange.getWidth() == 0.0 ? 1.0 : nWidth / aRange.getWidth(),
153 (aRange.getHeight() == 0.0
154 ? 1.0 : nWidth / fAspectRatio / aRange.getHeight()));
155 const drawinglayer::primitive2d::Primitive2DReference xTransformRef(
156 new drawinglayer::primitive2d::TransformPrimitive2D(
157 aTransform,
158 aVectorGraphicData.getPrimitive2DSequence()));
160 // UNO dance to render from drawinglayer
162 uno::Reference< uno::XComponentContext > xContext(::comphelper::getProcessComponentContext());
166 const uno::Reference< graphic::XPrimitive2DRenderer > xPrimitive2DRenderer =
167 graphic::Primitive2DTools::create( xContext );
169 // cancel out rasterize's mm2pixel conversion
170 // see fFactor100th_mmToInch in
171 // drawinglayer/source/drawinglayeruno/xprimitive2drenderer.cxx
172 const double fFakeDPI=2.54 * 1000.0;
174 geometry::RealRectangle2D aRealRect(
175 0, 0,
176 nWidth, nWidth / fAspectRatio);
178 const uno::Reference< rendering::XBitmap > xBitmap(
179 xPrimitive2DRenderer->rasterize(
180 drawinglayer::primitive2d::Primitive2DSequence(&xTransformRef, 1),
181 uno::Sequence< beans::PropertyValue >(),
182 fFakeDPI,
183 fFakeDPI,
184 aRealRect,
185 500000));
187 if(xBitmap.is())
189 const uno::Reference< rendering::XIntegerReadOnlyBitmap> xIntBmp(xBitmap, uno::UNO_QUERY_THROW);
190 rBitmap = vcl::unotools::bitmapExFromXBitmap(xIntBmp);
191 return true;
194 catch(const uno::Exception&)
196 OSL_ENSURE(false, "Got no graphic::XPrimitive2DRenderer (!)" );
198 return false;
201 /** loads the application logo as used in the impress slideshow pause screen */
202 BitmapEx SfxApplication::GetApplicationLogo(tools::Long nWidth)
204 BitmapEx aBitmap;
205 SfxApplication::loadBrandSvg("shell/about", aBitmap, nWidth);
206 return aBitmap;
209 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */