1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <config_features.h>
#if defined(MACOSX) && HAVE_FEATURE_READONLY_INSTALLSET
#define __ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES 0
#include <premac.h>
#include <Foundation/Foundation.h>
#include <postmac.h>
#endif
#include "sal/config.h"
#include <comphelper/processfactory.hxx>
#include <comphelper/string.hxx>
#include <osl/thread.h>
#include <tools/vcompat.hxx>
#include <ucbhelper/content.hxx>
#include <unotools/ucbstreamhelper.hxx>
#include <unotools/pathoptions.hxx>
#include <sfx2/docfile.hxx>
#include "svx/gallery.hxx"
#include "gallery.hrc"
#include "svx/galmisc.hxx"
#include "svx/galtheme.hxx"
#include "svx/gallery1.hxx"
#include <com/sun/star/sdbc/XResultSet.hpp>
#include <com/sun/star/ucb/XContentAccess.hpp>
#include <memory>
using namespace ::com::sun::star;
static bool FileExists( const INetURLObject &rURL, const rtl::OUString &rExt )
{
INetURLObject aURL( rURL );
aURL.setExtension( rExt );
return FileExists( aURL );
}
GalleryThemeEntry::GalleryThemeEntry( bool bCreateUniqueURL,
const INetURLObject& rBaseURL, const OUString& rName,
bool _bReadOnly, bool _bNewFile,
sal_uInt32 _nId, bool _bThemeNameFromResource ) :
nId ( _nId ),
bReadOnly ( _bReadOnly ),
bThemeNameFromResource ( _bThemeNameFromResource )
{
INetURLObject aURL( rBaseURL );
DBG_ASSERT( aURL.GetProtocol() != INetProtocol::NotValid, "invalid URL" );
if (bCreateUniqueURL)
{
INetURLObject aBaseNoCase( ImplGetURLIgnoreCase( rBaseURL ) );
aURL = aBaseNoCase;
static sal_Int32 nIdx = 0;
while( FileExists( aURL, "thm" ) )
{ // create new URLs
nIdx++;
aURL = aBaseNoCase;
aURL.setName( aURL.getName() + OUString::number(nIdx));
}
}
aURL.setExtension( "thm" );
aThmURL = ImplGetURLIgnoreCase( aURL );
aURL.setExtension( "sdg" );
aSdgURL = ImplGetURLIgnoreCase( aURL );
aURL.setExtension( "sdv" );
aSdvURL = ImplGetURLIgnoreCase( aURL );
aURL.setExtension( "str" );
aStrURL = ImplGetURLIgnoreCase( aURL );
SetModified( _bNewFile );
aName = ReadStrFromIni( "name" );
// This is awful - we shouldn't use these resources if we
// possibly can avoid them
if( aName.isEmpty() && nId && bThemeNameFromResource )
aName = GAL_RESSTR( RID_GALLERYSTR_THEME_START + (sal_uInt16) nId );
if( aName.isEmpty() )
aName = rName;
}
INetURLObject GalleryThemeEntry::ImplGetURLIgnoreCase( const INetURLObject& rURL )
{
INetURLObject aURL( rURL );
// check original file name
if( !FileExists( aURL ) )
{
// check upper case file name
aURL.setName( aURL.getName().toAsciiUpperCase() );
if(!FileExists( aURL ) )
{
// check lower case file name
aURL.setName( aURL.getName().toAsciiLowerCase() );
}
}
return aURL;
}
void GalleryThemeEntry::SetName( const OUString& rNewName )
{
if( aName != rNewName )
{
aName = rNewName;
SetModified( true );
bThemeNameFromResource = false;
}
}
void GalleryThemeEntry::SetId( sal_uInt32 nNewId, bool bResetThemeName )
{
nId = nNewId;
SetModified( true );
bThemeNameFromResource = ( nId && bResetThemeName );
}
class GalleryThemeCacheEntry
{
private:
const GalleryThemeEntry* mpThemeEntry;
GalleryTheme* mpTheme;
public:
GalleryThemeCacheEntry( const GalleryThemeEntry* pThemeEntry, GalleryTheme* pTheme ) :
mpThemeEntry( pThemeEntry ), mpTheme( pTheme ) {}
~GalleryThemeCacheEntry() { delete mpTheme; }
const GalleryThemeEntry* GetThemeEntry() const { return mpThemeEntry; }
GalleryTheme* GetTheme() const { return mpTheme; }
};
Gallery::Gallery( const OUString& rMultiPath )
: bMultiPath ( false )
{
ImplLoad( rMultiPath );
}
Gallery::~Gallery()
{
// erase theme list
for ( size_t i = 0, n = aThemeList.size(); i < n; ++i )
delete aThemeList[ i ];
aThemeList.clear();
}
Gallery* Gallery::GetGalleryInstance()
{
static Gallery* s_pGallery = nullptr;
if (!s_pGallery)
{
::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
if (!s_pGallery)
{
s_pGallery = new Gallery( SvtPathOptions().GetGalleryPath() );
}
}
return s_pGallery;
}
void Gallery::ImplLoad( const OUString& rMultiPath )
{
const sal_Int32 nTokenCount = comphelper::string::getTokenCount(rMultiPath, ';');
bool bIsReadOnlyDir;
bMultiPath = ( nTokenCount > 0 );
INetURLObject aCurURL(SvtPathOptions().GetConfigPath());
ImplLoadSubDirs( aCurURL, bIsReadOnlyDir );
if( !bIsReadOnlyDir )
aUserURL = aCurURL;
if( bMultiPath )
{
aRelURL = INetURLObject( rMultiPath.getToken(0, ';') );
for( sal_Int32 i = 0; i < nTokenCount; ++i )
{
aCurURL = INetURLObject(rMultiPath.getToken(i, ';'));
ImplLoadSubDirs( aCurURL, bIsReadOnlyDir );
if( !bIsReadOnlyDir )
aUserURL = aCurURL;
}
}
else
aRelURL = INetURLObject( rMultiPath );
DBG_ASSERT( aUserURL.GetProtocol() != INetProtocol::NotValid, "no writable Gallery user directory available" );
DBG_ASSERT( aRelURL.GetProtocol() != INetProtocol::NotValid, "invalid URL" );
}
void Gallery::ImplLoadSubDirs( const INetURLObject& rBaseURL, bool& rbDirIsReadOnly )
{
rbDirIsReadOnly = false;
try
{
uno::Reference< ucb::XCommandEnvironment > xEnv;
::ucbhelper::Content aCnt( rBaseURL.GetMainURL( INetURLObject::NO_DECODE ), xEnv, comphelper::getProcessComponentContext() );
uno::Sequence<OUString> aProps { "Url" };
uno::Reference< sdbc::XResultSet > xResultSet( aCnt.createCursor( aProps, ::ucbhelper::INCLUDE_DOCUMENTS_ONLY ) );
#if defined(MACOSX) && HAVE_FEATURE_READONLY_INSTALLSET
if( rBaseURL.GetProtocol() == INetProtocol::File )
{
const char *appBundle = [[[NSBundle mainBundle] bundlePath] UTF8String];
OUString path = rBaseURL.GetURLPath();
if( path.startsWith( OUString( appBundle, strlen( appBundle ), RTL_TEXTENCODING_UTF8 ) + "/" ) )
rbDirIsReadOnly = true;
}
#else
try
{
// check readonlyness the very hard way
INetURLObject aTestURL( rBaseURL );
OUString aTestFile( "cdefghij.klm" );
aTestURL.Append( aTestFile );
std::unique_ptr<SvStream> pTestStm(::utl::UcbStreamHelper::CreateStream( aTestURL.GetMainURL( INetURLObject::NO_DECODE ), StreamMode::WRITE ));
if( pTestStm )
{
pTestStm->WriteInt32( sal_Int32(1) );
if( pTestStm->GetError() )
rbDirIsReadOnly = true;
pTestStm.reset();
KillFile( aTestURL );
}
else
rbDirIsReadOnly = true;
}
catch( const ucb::ContentCreationException& )
{
}
catch( const uno::RuntimeException& )
{
}
catch( const uno::Exception& )
{
}
#endif
if( xResultSet.is() )
{
uno::Reference< ucb::XContentAccess > xContentAccess( xResultSet, uno::UNO_QUERY );
if( xContentAccess.is() )
{
static const char s_sTitle[] = "Title";
static const char s_sIsReadOnly[] = "IsReadOnly";
static const char s_sSDG_EXT[] = "sdg";
static const char s_sSDV_EXT[] = "sdv";
while( xResultSet->next() )
{
INetURLObject aThmURL( xContentAccess->queryContentIdentifierString() );
if(aThmURL.GetExtension().equalsIgnoreAsciiCase("thm"))
{
INetURLObject aSdgURL( aThmURL); aSdgURL.SetExtension( s_sSDG_EXT );
INetURLObject aSdvURL( aThmURL ); aSdvURL.SetExtension( s_sSDV_EXT );
OUString aTitle;
try
{
::ucbhelper::Content aThmCnt( aThmURL.GetMainURL( INetURLObject::NO_DECODE ), xEnv, comphelper::getProcessComponentContext() );
::ucbhelper::Content aSdgCnt( aSdgURL.GetMainURL( INetURLObject::NO_DECODE ), xEnv, comphelper::getProcessComponentContext() );
::ucbhelper::Content aSdvCnt( aSdvURL.GetMainURL( INetURLObject::NO_DECODE ), xEnv, comphelper::getProcessComponentContext() );
try
{
aThmCnt.getPropertyValue( s_sTitle ) >>= aTitle;
}
catch( const uno::RuntimeException& )
{
}
catch( const uno::Exception& )
{
}
if( !aTitle.isEmpty() )
{
bool bReadOnly = false;
try
{
aThmCnt.getPropertyValue( s_sIsReadOnly ) >>= bReadOnly;
}
catch( const uno::RuntimeException& )
{
}
catch( const uno::Exception& )
{
}
if( !bReadOnly )
{
try
{
aSdgCnt.getPropertyValue( s_sTitle ) >>= aTitle;
}
catch( const css::uno::RuntimeException& )
{
}
catch( const css::uno::Exception& )
{
}
if( !aTitle.isEmpty() )
{
try
{
aSdgCnt.getPropertyValue( s_sIsReadOnly ) >>= bReadOnly;
}
catch( const uno::RuntimeException& )
{
}
catch( const uno::Exception& )
{
}
}
}
if( !bReadOnly )
{
try
{
aSdvCnt.getPropertyValue( s_sTitle ) >>= aTitle;
}
catch( const css::uno::RuntimeException& )
{
}
catch( const css::uno::Exception& )
{
}
if( !aTitle.isEmpty() )
{
try
{
aSdvCnt.getPropertyValue( s_sIsReadOnly ) >>= bReadOnly;
}
catch( const uno::RuntimeException& )
{
}
catch( const uno::Exception& )
{
}
}
}
GalleryThemeEntry* pEntry = GalleryTheme::CreateThemeEntry( aThmURL, rbDirIsReadOnly || bReadOnly );
if( pEntry )
aThemeList.push_back( pEntry );
}
}
catch( const ucb::ContentCreationException& )
{
}
catch( const uno::RuntimeException& )
{
}
catch( const uno::Exception& )
{
}
}
}
}
}
}
catch( const ucb::ContentCreationException& )
{
}
catch( const uno::RuntimeException& )
{
}
catch( const uno::Exception& )
{
}
}
GalleryThemeEntry* Gallery::ImplGetThemeEntry( const OUString& rThemeName )
{
GalleryThemeEntry* pFound = nullptr;
if( !rThemeName.isEmpty() )
{
for ( size_t i = 0, n = aThemeList.size(); i < n && !pFound; ++i )
if( rThemeName == aThemeList[ i ]->GetThemeName() )
pFound = aThemeList[ i ];
}
return pFound;
}
OUString Gallery::GetThemeName( sal_uIntPtr nThemeId ) const
{
GalleryThemeEntry* pFound = nullptr;
for ( size_t i = 0, n = aThemeList.size(); i < n && !pFound; ++i )
{
GalleryThemeEntry* pEntry = aThemeList[ i ];
if( nThemeId == pEntry->GetId() )
pFound = pEntry;
}
// try fallback, if no entry was found
if( !pFound )
{
OUString aFallback;
switch( nThemeId )
{
case GALLERY_THEME_3D:
aFallback = GAL_RESSTR(RID_GALLERYSTR_THEME_3D);
break;
case GALLERY_THEME_BULLETS:
aFallback = GAL_RESSTR(RID_GALLERYSTR_THEME_BULLETS);
break;
case GALLERY_THEME_HOMEPAGE:
aFallback = GAL_RESSTR(RID_GALLERYSTR_THEME_HOMEPAGE);
break;
case GALLERY_THEME_POWERPOINT:
aFallback = GAL_RESSTR(RID_GALLERYSTR_THEME_POWERPOINT);
break;
case GALLERY_THEME_FONTWORK:
aFallback = GAL_RESSTR(RID_GALLERYSTR_THEME_FONTWORK);
break;
case GALLERY_THEME_FONTWORK_VERTICAL:
aFallback = GAL_RESSTR(RID_GALLERYSTR_THEME_FONTWORK_VERTICAL);
break;
case GALLERY_THEME_SOUNDS:
aFallback = GAL_RESSTR(RID_GALLERYSTR_THEME_SOUNDS);
break;
case RID_GALLERYSTR_THEME_ARROWS:
case RID_GALLERYSTR_THEME_COMPUTERS:
case RID_GALLERYSTR_THEME_DIAGRAMS:
case RID_GALLERYSTR_THEME_EDUCATION:
case RID_GALLERYSTR_THEME_ENVIRONMENT:
case RID_GALLERYSTR_THEME_FINANCE:
case RID_GALLERYSTR_THEME_PEOPLE:
case RID_GALLERYSTR_THEME_SYMBOLS:
case RID_GALLERYSTR_THEME_TRANSPORT:
case RID_GALLERYSTR_THEME_TXTSHAPES:
aFallback = GAL_RESSTR(static_cast<sal_uInt32>(nThemeId));
break;
default:
break;
}
pFound = const_cast<Gallery*>(this)->ImplGetThemeEntry(aFallback);
}
return( pFound ? pFound->GetThemeName() : OUString() );
}
bool Gallery::HasTheme( const OUString& rThemeName )
{
return( ImplGetThemeEntry( rThemeName ) != nullptr );
}
bool Gallery::CreateTheme( const OUString& rThemeName )
{
bool bRet = false;
if( !HasTheme( rThemeName ) && ( GetUserURL().GetProtocol() != INetProtocol::NotValid ) )
{
INetURLObject aURL( GetUserURL() );
aURL.Append( rThemeName );
GalleryThemeEntry* pNewEntry = new GalleryThemeEntry(
true, aURL, rThemeName,
false, true, 0, false );
aThemeList.push_back( pNewEntry );
delete( new GalleryTheme( this, pNewEntry ) );
Broadcast( GalleryHint( GalleryHintType::THEME_CREATED, rThemeName ) );
bRet = true;
}
return bRet;
}
void Gallery::RenameTheme( const OUString& rOldName, const OUString& rNewName )
{
GalleryThemeEntry* pThemeEntry = ImplGetThemeEntry( rOldName );
// check if the new theme name is already present
if( pThemeEntry && !HasTheme( rNewName ) && !pThemeEntry->IsReadOnly() )
{
SfxListener aListener;
GalleryTheme* pThm = AcquireTheme( rOldName, aListener );
if( pThm )
{
const OUString aOldName( rOldName );
pThemeEntry->SetName( rNewName );
pThm->ImplWrite();
Broadcast( GalleryHint( GalleryHintType::THEME_RENAMED, aOldName, pThm->GetName() ) );
ReleaseTheme( pThm, aListener );
}
}
}
bool Gallery::RemoveTheme( const OUString& rThemeName )
{
GalleryThemeEntry* pThemeEntry = ImplGetThemeEntry( rThemeName );
bool bRet = false;
if( pThemeEntry && !pThemeEntry->IsReadOnly() )
{
Broadcast( GalleryHint( GalleryHintType::CLOSE_THEME, rThemeName ) );
SfxListener aListener;
GalleryTheme* pThm = AcquireTheme( rThemeName, aListener );
if( pThm )
{
INetURLObject aThmURL( pThm->GetThmURL() );
INetURLObject aSdgURL( pThm->GetSdgURL() );
INetURLObject aSdvURL( pThm->GetSdvURL() );
INetURLObject aStrURL( pThm->GetSdvURL() );
ReleaseTheme( pThm, aListener );
KillFile( aThmURL );
KillFile( aSdgURL );
KillFile( aSdvURL );
KillFile( aStrURL );
}
for ( GalleryThemeList::iterator it = aThemeList.begin(); it != aThemeList.end(); ++it )
{
if ( pThemeEntry == *it ) {
delete pThemeEntry;
aThemeList.erase( it );
break;
}
}
Broadcast( GalleryHint( GalleryHintType::THEME_REMOVED, rThemeName ) );
bRet = true;
}
return bRet;
}
GalleryTheme* Gallery::ImplGetCachedTheme(const GalleryThemeEntry* pThemeEntry)
{
GalleryTheme* pTheme = nullptr;
if( pThemeEntry )
{
for (GalleryCacheThemeList::const_iterator it = aThemeCache.begin(); it != aThemeCache.end(); ++it)
{
if (pThemeEntry == (*it)->GetThemeEntry())
{
pTheme = (*it)->GetTheme();
break;
}
}
if( !pTheme )
{
INetURLObject aURL = pThemeEntry->GetThmURL();
DBG_ASSERT( aURL.GetProtocol() != INetProtocol::NotValid, "invalid URL" );
if( FileExists( aURL ) )
{
std::unique_ptr<SvStream> pIStm(::utl::UcbStreamHelper::CreateStream( aURL.GetMainURL( INetURLObject::NO_DECODE ), StreamMode::READ ));
if( pIStm )
{
try
{
pTheme = new GalleryTheme( this, const_cast<GalleryThemeEntry*>(pThemeEntry) );
ReadGalleryTheme( *pIStm, *pTheme );
if( pIStm->GetError() )
{
delete pTheme;
pTheme = nullptr;
}
}
catch (const css::ucb::ContentCreationException&)
{
}
}
}
if( pTheme )
aThemeCache.push_back( new GalleryThemeCacheEntry( pThemeEntry, pTheme ));
}
}
return pTheme;
}
void Gallery::ImplDeleteCachedTheme( GalleryTheme* pTheme )
{
for (GalleryCacheThemeList::iterator it = aThemeCache.begin(); it != aThemeCache.end(); ++it)
{
if (pTheme == (*it)->GetTheme())
{
delete *it;
aThemeCache.erase(it);
break;
}
}
}
GalleryTheme* Gallery::AcquireTheme( const OUString& rThemeName, SfxListener& rListener )
{
GalleryTheme* pTheme = nullptr;
GalleryThemeEntry* pThemeEntry = ImplGetThemeEntry( rThemeName );
if( pThemeEntry && ( ( pTheme = ImplGetCachedTheme( pThemeEntry ) ) != nullptr ) )
rListener.StartListening( *pTheme );
return pTheme;
}
void Gallery::ReleaseTheme( GalleryTheme* pTheme, SfxListener& rListener )
{
if( pTheme )
{
rListener.EndListening( *pTheme );
if( !pTheme->HasListeners() )
ImplDeleteCachedTheme( pTheme );
}
}
bool GalleryThemeEntry::IsDefault() const
{
return ( nId > 0 ) && ( nId != ( RID_GALLERYSTR_THEME_MYTHEME - RID_GALLERYSTR_THEME_START ) );
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|