cid#1640468 Dereference after null check
[LibreOffice.git] / svx / source / gallery2 / galexpl.cxx
blobb1e70919c9282b4144bca8573454cf5c219af86f
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 .
21 #include <svx/gallery1.hxx>
22 #include <svx/galtheme.hxx>
23 #include <svx/gallery.hxx>
24 #include <galobj.hxx>
26 namespace
28 SfxListener& theLockListener()
30 static SfxListener SINGLETON;
31 return SINGLETON;
36 bool GalleryExplorer::FillThemeList( std::vector<OUString>& rThemeList )
38 Gallery* pGal = ::Gallery::GetGalleryInstance();
40 if( pGal )
42 for( sal_uInt32 i = 0, nCount = pGal->GetThemeCount(); i < nCount; i++ )
44 const GalleryThemeEntry* pEntry = pGal->GetThemeInfo( i );
46 if( pEntry && !pEntry->IsReadOnly() && !pEntry->IsHidden() )
47 rThemeList.push_back(pEntry->GetThemeName());
51 return !rThemeList.empty();
54 bool GalleryExplorer::FillObjList( std::u16string_view rThemeName, std::vector<OUString> &rObjList )
56 Gallery* pGal = ::Gallery::GetGalleryInstance();
58 if( pGal )
60 SfxListener aListener;
61 GalleryTheme* pTheme = pGal->AcquireTheme( rThemeName, aListener );
63 if( pTheme )
65 for( sal_uInt32 i = 0, nCount = pTheme->GetObjectCount(); i < nCount; i++ )
66 rObjList.push_back( pTheme->GetObjectURL( i ).GetMainURL( INetURLObject::DecodeMechanism::NONE ) );
68 pGal->ReleaseTheme( pTheme, aListener );
72 return !rObjList.empty();
75 bool GalleryExplorer::FillObjList( const sal_uInt32 nThemeId, std::vector<OUString> &rObjList )
77 Gallery* pGal = ::Gallery::GetGalleryInstance();
79 if (!pGal)
80 return false;
82 return FillObjList( pGal->GetThemeName( nThemeId ), rObjList );
85 bool GalleryExplorer::FillObjListTitle( const sal_uInt32 nThemeId, std::vector< OUString >& rList )
87 Gallery* pGal = ::Gallery::GetGalleryInstance();
88 if( pGal )
90 SfxListener aListener;
91 GalleryTheme* pTheme = pGal->AcquireTheme( pGal->GetThemeName( nThemeId ), aListener );
93 if( pTheme )
95 for( sal_uInt32 i = 0, nCount = pTheme->GetObjectCount(); i < nCount; i++ )
97 std::unique_ptr<SgaObject> pObj = pTheme->AcquireObject( i );
98 if ( pObj )
100 OUString aTitle( pObj->GetTitle() );
101 rList.push_back( aTitle );
104 pGal->ReleaseTheme( pTheme, aListener );
107 return !rList.empty();
110 bool GalleryExplorer::InsertURL( std::u16string_view rThemeName, std::u16string_view rURL )
112 Gallery* pGal = ::Gallery::GetGalleryInstance();
113 bool bRet = false;
115 if( pGal )
117 SfxListener aListener;
118 GalleryTheme* pTheme = pGal->AcquireTheme( rThemeName, aListener );
120 if( pTheme )
122 INetURLObject aURL( rURL );
123 DBG_ASSERT( aURL.GetProtocol() != INetProtocol::NotValid, "invalid URL" );
124 bRet = pTheme->InsertURL( aURL );
125 pGal->ReleaseTheme( pTheme, aListener );
129 return bRet;
132 bool GalleryExplorer::InsertURL( sal_uInt32 nThemeId, std::u16string_view rURL )
134 Gallery* pGal = ::Gallery::GetGalleryInstance();
135 return pGal && InsertURL( pGal->GetThemeName( nThemeId ), rURL );
138 bool GalleryExplorer::GetGraphicObj( std::u16string_view rThemeName, sal_uInt32 nPos,
139 Graphic* pGraphic )
141 Gallery* pGal = ::Gallery::GetGalleryInstance();
142 bool bRet = false;
144 if( pGal )
146 SfxListener aListener;
147 GalleryTheme* pTheme = pGal->AcquireTheme( rThemeName, aListener );
149 if( pTheme )
151 if( pGraphic )
152 bRet = bRet || pTheme->GetGraphic( nPos, *pGraphic );
154 pGal->ReleaseTheme( pTheme, aListener );
158 return bRet;
161 bool GalleryExplorer::GetGraphicObj( sal_uInt32 nThemeId, sal_uInt32 nPos,
162 Graphic* pGraphic )
164 Gallery* pGal = ::Gallery::GetGalleryInstance();
165 return pGal && GetGraphicObj( pGal->GetThemeName( nThemeId ), nPos, pGraphic );
168 sal_uInt32 GalleryExplorer::GetSdrObjCount( std::u16string_view rThemeName )
170 Gallery* pGal = ::Gallery::GetGalleryInstance();
171 sal_uInt32 nRet = 0;
173 if( pGal )
175 SfxListener aListener;
176 GalleryTheme* pTheme = pGal->AcquireTheme( rThemeName, aListener );
178 if( pTheme )
180 for( sal_uInt32 i = 0, nCount = pTheme->GetObjectCount(); i < nCount; i++ )
181 if( SgaObjKind::SvDraw == pTheme->GetObjectKind( i ) )
182 nRet++;
184 pGal->ReleaseTheme( pTheme, aListener );
188 return nRet;
191 sal_uInt32 GalleryExplorer::GetSdrObjCount( sal_uInt32 nThemeId )
193 Gallery* pGal = ::Gallery::GetGalleryInstance();
194 return( pGal ? GetSdrObjCount( pGal->GetThemeName( nThemeId ) ) : 0 );
197 bool GalleryExplorer::GetSdrObj( std::u16string_view rThemeName, sal_uInt32 nSdrModelPos,
198 SdrModel* pModel, BitmapEx* pThumb )
200 Gallery* pGal = ::Gallery::GetGalleryInstance();
201 bool bRet = false;
203 if( pGal )
205 SfxListener aListener;
206 GalleryTheme* pTheme = pGal->AcquireTheme( rThemeName, aListener );
208 if( pTheme )
210 for( sal_uInt32 i = 0, nCount = pTheme->GetObjectCount(), nActPos = 0; ( i < nCount ) && !bRet; i++ )
212 if( SgaObjKind::SvDraw == pTheme->GetObjectKind( i ) )
214 if( nActPos++ == nSdrModelPos )
216 if( pModel )
217 bRet = pTheme->GetModel(i, *pModel);
219 if( pThumb )
220 bRet = bRet || pTheme->GetThumb( i, *pThumb );
225 pGal->ReleaseTheme( pTheme, aListener );
229 return bRet;
232 bool GalleryExplorer::GetSdrObj( sal_uInt32 nThemeId, sal_uInt32 nSdrModelPos,
233 SdrModel* pModel, BitmapEx* pThumb )
235 Gallery* pGal = ::Gallery::GetGalleryInstance();
236 return pGal && GetSdrObj( pGal->GetThemeName( nThemeId ), nSdrModelPos, pModel, pThumb );
239 bool GalleryExplorer::BeginLocking( std::u16string_view rThemeName )
241 Gallery* pGal = ::Gallery::GetGalleryInstance();
242 bool bRet = false;
244 if( pGal )
246 GalleryTheme* pTheme = pGal->AcquireTheme( rThemeName, theLockListener() );
248 if( pTheme )
250 pTheme->LockTheme();
251 bRet = true;
255 return bRet;
258 bool GalleryExplorer::BeginLocking( sal_uInt32 nThemeId )
260 Gallery* pGal = ::Gallery::GetGalleryInstance();
261 return pGal && BeginLocking( pGal->GetThemeName( nThemeId ) );
264 bool GalleryExplorer::EndLocking( std::u16string_view rThemeName )
266 Gallery* pGal = ::Gallery::GetGalleryInstance();
267 bool bRet = false;
269 if( pGal )
271 SfxListener aListener;
272 GalleryTheme* pTheme = pGal->AcquireTheme( rThemeName, aListener );
274 if( pTheme )
276 const bool bReleaseLockedTheme = pTheme->UnlockTheme();
278 // release acquired theme
279 pGal->ReleaseTheme( pTheme, aListener );
281 if( bReleaseLockedTheme )
283 // release locked theme
284 pGal->ReleaseTheme( pTheme, theLockListener() );
285 bRet = true;
290 return bRet;
293 bool GalleryExplorer::EndLocking( sal_uInt32 nThemeId )
295 Gallery* pGal = ::Gallery::GetGalleryInstance();
296 return pGal && EndLocking( pGal->GetThemeName( nThemeId ) );
299 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */