summaryrefslogtreecommitdiff
path: root/unotools/source/ucbhelper/xtempfile.cxx
blob: 2ee567d27e44a682da81c74f4759dee9c661fd2a (plain)
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
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
/*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: xtempfile.cxx,v $
 *
 *  $Revision: 1.12 $
 *
 *  last change: $Author: rt $ $Date: 2005-09-09 09:52:30 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU Lesser General Public License Version 2.1.
 *
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 ************************************************************************/

#ifndef _XTEMPFILE_HXX
#include <XTempFile.hxx>
#endif
#ifndef _CPPUHELPER_FACTORY_HXX_
#include <cppuhelper/factory.hxx>
#endif
#ifndef _CPPUHELPER_TYPEPROVIDER_HXX_
#include <cppuhelper/typeprovider.hxx>
#endif
#ifndef _COM_SUN_STAR_REGISTRY_XREGISTRYKEY_HPP
#include <com/sun/star/registry/XRegistryKey.hpp>
#endif
#ifndef _COM_SUN_STAR_BEANS_PROPERTYATTRIBUTE_HPP
#include <com/sun/star/beans/PropertyAttribute.hpp>
#endif
#ifndef _UNOTOOLS_TEMPFILE_HXX
#include <unotools/tempfile.hxx>
#endif
#ifndef _OSL_FILE_HXX_
#include <osl/file.hxx>
#endif
#ifndef _UTL_CONFIGMGR_HXX_
#include <unotools/configmgr.hxx>
#endif
#ifndef _URLOBJ_HXX
#include <tools/urlobj.hxx>
#endif
#ifndef _TOOLS_DEBUG_HXX
#include <tools/debug.hxx>
#endif

using namespace ::com::sun::star;

using com::sun::star::beans::XPropertySetInfo;
using com::sun::star::beans::XPropertySet;
using com::sun::star::beans::XPropertyChangeListener;
using com::sun::star::beans::XVetoableChangeListener;
using com::sun::star::beans::UnknownPropertyException;
using com::sun::star::beans::PropertyVetoException;
using com::sun::star::lang::WrappedTargetException;
using com::sun::star::registry::XRegistryKey;
using com::sun::star::registry::InvalidRegistryException;
using com::sun::star::uno::Any;
using com::sun::star::uno::RuntimeException;
using com::sun::star::uno::Type;
using com::sun::star::uno::Sequence;
using com::sun::star::uno::XInterface;
using com::sun::star::uno::XWeak;
using com::sun::star::uno::Reference;
using com::sun::star::io::NotConnectedException;
using com::sun::star::io::BufferSizeExceededException;
using com::sun::star::io::IOException;
using com::sun::star::io::XInputStream;
using com::sun::star::io::XOutputStream;
using com::sun::star::io::XSeekable;
using com::sun::star::lang::IllegalArgumentException;
using com::sun::star::lang::XMultiServiceFactory;
using com::sun::star::lang::XSingleServiceFactory;
using com::sun::star::lang::XTypeProvider;
using cppu::OWeakObject;
using rtl::OUString;
using osl::FileBase;
using osl::MutexGuard;
using utl::TempFile;
using namespace utl;

// copy define from desktop\source\app\appinit.cxx

#define DESKTOP_TEMPNAMEBASE_DIR    "/temp/soffice.tmp"

XTempFile::XTempFile ()
: mpStream( NULL )
, mbRemoveFile( sal_True )
, mbInClosed( sal_False )
, mbOutClosed( sal_False )
, mnCachedPos( 0 )
, mbHasCachedPos( sal_False )
{
    mpTempFile = new TempFile;
    mpTempFile->EnableKillingFile ( sal_True );
}

XTempFile::~XTempFile ()
{
    if ( mpTempFile )
        delete mpTempFile;
}

uno::Sequence< beans::Property > XTempFile::GetProps()
{
    static uno::Sequence< beans::Property >* pProps = NULL;
    if ( pProps == NULL )
    {
        ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() ) ;

        if ( pProps == NULL )
        {
            static uno::Sequence< beans::Property > aProps( 3 );

            aProps[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "RemoveFile" ) );
            aProps[0].Type = getCppuType( static_cast< sal_Bool* >( NULL ) );
            aProps[0].Attributes = beans::PropertyAttribute::TRANSIENT;
            aProps[1].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ResourceName" ) );
            aProps[1].Type = getCppuType( static_cast< ::rtl::OUString* >( NULL ) );
            aProps[1].Attributes = beans::PropertyAttribute::TRANSIENT | beans::PropertyAttribute::READONLY;
            aProps[2].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Uri" ) );
            aProps[2].Type = getCppuType( static_cast< ::rtl::OUString* >( NULL ) );
            aProps[2].Attributes = beans::PropertyAttribute::TRANSIENT | beans::PropertyAttribute::READONLY;

            pProps = &aProps;
        }
    }

    return *pProps;
}

// XInterface
Any SAL_CALL XTempFile::queryInterface( const Type& rType )
    throw (RuntimeException)
{
    return ::cppu::queryInterface ( rType                                       ,
                                // OWeakObject interfaces
                                reinterpret_cast< XInterface*> ( this ),
                                static_cast< XWeak*         > ( this ),
                                // my own interfaces
                                static_cast< XTypeProvider* > ( this ),
                                static_cast< XInputStream*  > ( this ),
                                static_cast< XOutputStream* > ( this ),
                                static_cast< XStream*       > ( this ),
                                static_cast< XTruncate*     > ( this ),
                                static_cast< XPropertySet*  > ( this ),
                                static_cast< XPropertySetInfo*  > ( this ),
                                static_cast< XServiceInfo*  > ( this ),
                                static_cast< XSeekable*     > ( this ) );
}
void SAL_CALL XTempFile::acquire(  )
    throw ()
{
    OWeakObject::acquire();
}
void SAL_CALL XTempFile::release(  )
        throw ()
{
    OWeakObject::release();
}

// XTypeProvider
Sequence< Type > SAL_CALL XTempFile::getTypes()
        throw( RuntimeException )
{
    static ::cppu::OTypeCollection* pTypeCollection = NULL;
    if ( pTypeCollection == NULL )
    {
        ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() ) ;

        if ( pTypeCollection == NULL )
        {
            static ::cppu::OTypeCollection aTypeCollection
                                    ( ::getCppuType( ( const Reference< XTypeProvider >* )NULL )
                                    , ::getCppuType( ( const Reference< XWeak >* )NULL )
                                    , ::getCppuType( ( const Reference< XInputStream >* )NULL )
                                    , ::getCppuType( ( const Reference< XOutputStream >* )NULL )
                                    , ::getCppuType( ( const Reference< XStream >* )NULL )
                                    , ::getCppuType( ( const Reference< XTruncate >* )NULL )
                                    , ::getCppuType( ( const Reference< XPropertySet >* )NULL )
                                    , ::getCppuType( ( const Reference< XPropertySetInfo >* )NULL )
                                    , ::getCppuType( ( const Reference< XServiceInfo >* )NULL )
                                    , ::getCppuType( ( const Reference< XSeekable >* )NULL ) );
            pTypeCollection = &aTypeCollection;
        }
    }

    return pTypeCollection->getTypes() ;
}

Sequence< sal_Int8 > SAL_CALL XTempFile::getImplementationId()
        throw( RuntimeException )
{
    static ::cppu::OImplementationId* pID = NULL ;

    if ( pID == NULL )
    {
        ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() ) ;

        if ( pID == NULL )
        {
            static ::cppu::OImplementationId aID( sal_False ) ;
            pID = &aID ;
        }
    }

    return pID->getImplementationId() ;
}

// XInputStream

sal_Int32 SAL_CALL XTempFile::readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
    throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
{
    MutexGuard aGuard( maMutex );
    if ( mbInClosed )
        throw NotConnectedException ( OUString(), const_cast < XWeak * > ( static_cast < const XWeak* > (this ) ) );

    checkConnected();
    if (nBytesToRead < 0)
        throw BufferSizeExceededException( OUString(), static_cast<XWeak*>(this));

    aData.realloc(nBytesToRead);

    sal_uInt32 nRead = mpStream->Read(static_cast < void* > ( aData.getArray() ), nBytesToRead);
    checkError();

    if (nRead < static_cast < sal_uInt32 > ( nBytesToRead ) )
        aData.realloc( nRead );

    if ( nBytesToRead > nRead )
    {
        // usually that means that the stream was read till the end
        // TODO/LATER: it is better to get rid of this optimization by avoiding using of multiple temporary files ( there should be only one temporary file? )
        mnCachedPos = mpStream->Tell();
        mbHasCachedPos = sal_True;

        mpStream = NULL;
        if ( mpTempFile )
            mpTempFile->CloseStream();
    }

    return nRead;
}
sal_Int32 SAL_CALL XTempFile::readSomeBytes( Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
    throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
{
    MutexGuard aGuard( maMutex );
    if ( mbInClosed )
        throw NotConnectedException ( OUString(), const_cast < XWeak * > ( static_cast < const XWeak* > (this ) ) );

    checkConnected();
    checkError();

    if (nMaxBytesToRead < 0)
        throw BufferSizeExceededException(OUString(), static_cast < XWeak* >( this ) );

    if (mpStream->IsEof())
    {
        aData.realloc(0);
        return 0;
    }
    else
        return readBytes(aData, nMaxBytesToRead);
}
void SAL_CALL XTempFile::skipBytes( sal_Int32 nBytesToSkip )
    throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
{
    MutexGuard aGuard( maMutex );
    if ( mbInClosed )
        throw NotConnectedException ( OUString(), const_cast < XWeak * > ( static_cast < const XWeak* > (this ) ) );

    checkConnected();
    checkError();
    mpStream->SeekRel(nBytesToSkip);
    checkError();
}
sal_Int32 SAL_CALL XTempFile::available(  )
    throw (NotConnectedException, IOException, RuntimeException)
{
    MutexGuard aGuard( maMutex );
    if ( mbInClosed )
        throw NotConnectedException ( OUString(), const_cast < XWeak * > ( static_cast < const XWeak* > (this ) ) );

    checkConnected();

    sal_uInt32 nPos = mpStream->Tell();
    checkError();

    mpStream->Seek(STREAM_SEEK_TO_END);
    checkError();

    sal_Int32 nAvailable = (sal_Int32)mpStream->Tell() - nPos;
    mpStream->Seek(nPos);
    checkError();

    return nAvailable;
}
void SAL_CALL XTempFile::closeInput(  )
    throw (NotConnectedException, IOException, RuntimeException)
{
    MutexGuard aGuard( maMutex );
    if ( mbInClosed )
        throw NotConnectedException ( OUString(), const_cast < XWeak * > ( static_cast < const XWeak* > (this ) ) );

    mbInClosed = sal_True;

    if ( mbOutClosed )
    {
        // stream will be deleted by TempFile implementation
        mpStream = NULL;

        if ( mpTempFile )
        {
            delete mpTempFile;
            mpTempFile = NULL;
        }
    }
}

// XSeekable

void SAL_CALL XTempFile::seek( sal_Int64 nLocation )
    throw (IllegalArgumentException, IOException, RuntimeException)
{
    MutexGuard aGuard( maMutex );
    checkConnected();
    if ( nLocation < 0 || nLocation > getLength() )
        throw IllegalArgumentException();

    mpStream->Seek((sal_uInt32) nLocation);
    checkError();
}
sal_Int64 SAL_CALL XTempFile::getPosition(  )
    throw (IOException, RuntimeException)
{
    MutexGuard aGuard( maMutex );
    checkConnected();

    sal_uInt32 nPos = mpStream->Tell();
    checkError();
    return (sal_Int64)nPos;
}
sal_Int64 SAL_CALL XTempFile::getLength(  )
    throw (IOException, RuntimeException)
{
    MutexGuard aGuard( maMutex );
    checkConnected();

    sal_uInt32 nCurrentPos = mpStream->Tell();
    checkError();

    mpStream->Seek(STREAM_SEEK_TO_END);
    sal_uInt32 nEndPos = mpStream->Tell();
    mpStream->Seek(nCurrentPos);

    checkError();

    return (sal_Int64)nEndPos;
}

// XOutputStream

void SAL_CALL XTempFile::writeBytes( const Sequence< sal_Int8 >& aData )
    throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
{
    MutexGuard aGuard( maMutex );
    if ( mbOutClosed )
        throw NotConnectedException ( OUString(), const_cast < XWeak * > ( static_cast < const XWeak* > (this ) ) );

    checkConnected();
    sal_uInt32 nWritten = mpStream->Write(aData.getConstArray(),aData.getLength());
    checkError();
    if  ( nWritten != (sal_uInt32)aData.getLength())
        throw BufferSizeExceededException( OUString(),static_cast < XWeak* > ( this ) );
}
void SAL_CALL XTempFile::flush(  )
    throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
{
    MutexGuard aGuard( maMutex );
    if ( mbOutClosed )
        throw NotConnectedException ( OUString(), const_cast < XWeak * > ( static_cast < const XWeak* > (this ) ) );

    checkConnected();
    mpStream->Flush();
    checkError();
}
void SAL_CALL XTempFile::closeOutput(  )
    throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
{
    MutexGuard aGuard( maMutex );
    if ( mbOutClosed )
        throw NotConnectedException ( OUString(), const_cast < XWeak * > ( static_cast < const XWeak* > (this ) ) );

    mbOutClosed = sal_True;

    // TODO/LATER: it is better to get rid of this optimization by avoiding using of multiple temporary files ( there should be only one temporary file? )
    if ( mpStream )
    {
        mnCachedPos = mpStream->Tell();
        mbHasCachedPos = sal_True;

        mpStream = NULL;
        if ( mpTempFile )
            mpTempFile->CloseStream();
    }

    if ( mbInClosed )
    {
        // stream will be deleted by TempFile implementation
        mpStream = NULL;

        if ( mpTempFile )
        {
            delete mpTempFile;
            mpTempFile = NULL;
        }
    }
}


void XTempFile::checkError () const
{
    if (!mpStream || mpStream->SvStream::GetError () != ERRCODE_NONE )
        throw NotConnectedException ( OUString(), const_cast < XWeak * > ( static_cast < const XWeak* > (this ) ) );
}
void XTempFile::checkConnected ()
{
    if (!mpStream && mpTempFile)
    {
        mpStream = mpTempFile->GetStream( STREAM_STD_READWRITE );
        if ( mpStream && mbHasCachedPos )
        {
            mpStream->Seek( mnCachedPos );
            if ( mpStream->SvStream::GetError () == ERRCODE_NONE )
            {
                mbHasCachedPos = sal_False;
                mnCachedPos = 0;
            }
            else
            {
                mpStream = NULL;
                mpTempFile->CloseStream();
            }
        }
    }

    if (!mpStream)
        throw NotConnectedException ( OUString(), const_cast < XWeak * > ( static_cast < const XWeak* > (this ) ) );
}

// XStream

Reference< XInputStream > SAL_CALL XTempFile::getInputStream()
    throw (RuntimeException)
{
    return Reference< XInputStream >( static_cast< OWeakObject* >( this ), ::com::sun::star::uno::UNO_QUERY );
}

Reference< XOutputStream > SAL_CALL XTempFile::getOutputStream()
    throw (RuntimeException)
{
    return Reference< XOutputStream >( static_cast< OWeakObject* >( this ), ::com::sun::star::uno::UNO_QUERY );
}

// XTruncate

void SAL_CALL XTempFile::truncate()
    throw (IOException, RuntimeException)
{
    MutexGuard aGuard( maMutex );
    checkConnected();
    // SetStreamSize() call does not change the position
    mpStream->Seek( 0 );
    mpStream->SetStreamSize( 0 );
    checkError();
}

// XPropertySetInfo
uno::Sequence< beans::Property > SAL_CALL XTempFile::getProperties()
    throw (uno::RuntimeException)
{
    ::osl::MutexGuard aGuard( maMutex );

    return GetProps();
}

beans::Property SAL_CALL XTempFile::getPropertyByName( const ::rtl::OUString& aName )
    throw (beans::UnknownPropertyException, uno::RuntimeException)
{
    ::osl::MutexGuard aGuard( maMutex );

    uno::Sequence< beans::Property > aProps = XTempFile::GetProps();
    for ( sal_Int32 nInd = 0; nInd < aProps.getLength(); nInd++ )
        if ( aName.equals( aProps[nInd].Name ) )
            return aProps[nInd];

    throw beans::UnknownPropertyException();
}

::sal_Bool SAL_CALL XTempFile::hasPropertyByName( const ::rtl::OUString& aName )
    throw (uno::RuntimeException)
{
    ::osl::MutexGuard aGuard( maMutex );

    uno::Sequence< beans::Property > aProps = XTempFile::GetProps();
    for ( sal_Int32 nInd = 0; nInd < aProps.getLength(); nInd++ )
        if ( aName.equals( aProps[nInd].Name ) )
            return sal_True;

    return sal_False;
}


// XPropertySet

Reference< XPropertySetInfo > SAL_CALL XTempFile::getPropertySetInfo(  )
    throw (RuntimeException)
{
    MutexGuard aGuard( maMutex );

    return Reference< XPropertySetInfo > ( static_cast< XPropertySetInfo* >( this ) );
}

void SAL_CALL XTempFile::setPropertyValue( const OUString& aPropertyName, const Any& aValue )
    throw (UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException)
{
    MutexGuard aGuard( maMutex );

    if ( !mpTempFile )
    {
        // the stream is already disconnected
        throw RuntimeException();
    }

    if ( aPropertyName.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "RemoveFile" ) ) )
    {
        if ( !( aValue >>= mbRemoveFile ) )
            throw IllegalArgumentException();

        mpTempFile->EnableKillingFile( mbRemoveFile );
    }
    // All other properties are read-only
    else if ( aPropertyName.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "ResourceName" ) ) ||
         aPropertyName.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "Uri" ) ) )
        throw IllegalArgumentException();
    else
        throw UnknownPropertyException();
}
Any SAL_CALL XTempFile::getPropertyValue( const OUString& PropertyName )
    throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
{
    MutexGuard aGuard( maMutex );

    if ( !mpTempFile )
    {
        // the stream is already disconnected
        throw RuntimeException();
    }

    Any aRet;

    if ( PropertyName.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "RemoveFile" ) ) )
        aRet <<= mbRemoveFile;
    else if ( PropertyName.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "ResourceName" ) ) )
        aRet <<= OUString ( mpTempFile->GetFileName() );
    else if ( PropertyName.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "Uri" ) ) )
        aRet <<= OUString ( mpTempFile->GetURL() );
    else
        throw UnknownPropertyException();
    return aRet;
}
void SAL_CALL XTempFile::addPropertyChangeListener( const OUString& aPropertyName, const Reference< XPropertyChangeListener >& xListener )
    throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
{
    DBG_ASSERT ( sal_False, "Listeners not implemented" );
}
void SAL_CALL XTempFile::removePropertyChangeListener( const OUString& aPropertyName, const Reference< XPropertyChangeListener >& aListener )
    throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
{
    DBG_ASSERT ( sal_False, "Listeners not implemented" );
}
void SAL_CALL XTempFile::addVetoableChangeListener( const OUString& PropertyName, const Reference< XVetoableChangeListener >& aListener )
    throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
{
    DBG_ASSERT ( sal_False, "Listeners not implemented" );
}
void SAL_CALL XTempFile::removeVetoableChangeListener( const OUString& PropertyName, const Reference< XVetoableChangeListener >& aListener )
    throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
{
    DBG_ASSERT ( sal_False, "Listeners not implemented" );
}

// XServiceInfo
::rtl::OUString SAL_CALL XTempFile::getImplementationName()
        throw (::com::sun::star::uno::RuntimeException)
{
    return getImplementationName_Static();
}

sal_Bool SAL_CALL XTempFile::supportsService(rtl::OUString const & rServiceName)
        throw (com::sun::star::uno::RuntimeException)
{
    return rServiceName == getSupportedServiceNames_Static()[0];
}

::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL XTempFile::getSupportedServiceNames()
        throw (::com::sun::star::uno::RuntimeException)
{
    return getSupportedServiceNames_Static();
}



OUString XTempFile::getImplementationName_Static ()
{
    return OUString ( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.io.comp.TempFile" ) );
}
Sequence < OUString > XTempFile::getSupportedServiceNames_Static()
{
    Sequence < OUString > aNames ( 1 );
    aNames[0] = OUString ( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.io.TempFile" ) );
    return aNames;
}
Reference < XInterface >SAL_CALL XTempFile_createInstance(
    const Reference< XMultiServiceFactory > & xMgr )
{
    return Reference< XInterface >( *new XTempFile );
}

Reference < XSingleServiceFactory > XTempFile::createServiceFactory_Static( Reference < XMultiServiceFactory > const & rServiceFactory )
{
    return cppu::createSingleFactory ( rServiceFactory, getImplementationName_Static(),
                                       XTempFile_createInstance,
                                       getSupportedServiceNames_Static());
}

static sal_Bool writeInfo( void * pRegistryKey,
                           const OUString & rImplementationName,
                              Sequence< OUString > const & rServiceNames )
{
    OUString aKeyName( RTL_CONSTASCII_USTRINGPARAM ( "/" ) );
    aKeyName += rImplementationName;
    aKeyName += OUString( RTL_CONSTASCII_USTRINGPARAM ( "/UNO/SERVICES" ) );

    Reference< XRegistryKey > xKey;
    try
    {
        xKey = static_cast< XRegistryKey * >(
                                    pRegistryKey )->createKey( aKeyName );
    }
    catch ( InvalidRegistryException const & )
    {
    }

    if ( !xKey.is() )
        return sal_False;

    sal_Bool bSuccess = sal_True;

    for ( sal_Int32 n = 0; n < rServiceNames.getLength(); ++n )
    {
        try
        {
            xKey->createKey( rServiceNames[ n ] );
        }
        catch ( InvalidRegistryException const & )
        {
            bSuccess = sal_False;
            break;
        }
    }
    return bSuccess;
}
// C functions to implement this as a component

extern "C" SAL_DLLPUBLIC_EXPORT void SAL_CALL component_getImplementationEnvironment(
                const sal_Char ** ppEnvTypeName, uno_Environment ** ppEnv )
{
    *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
}

/**
 * This function creates an implementation section in the registry and another subkey
 * for each supported service.
 * @param pServiceManager generic uno interface providing a service manager
 * @param pRegistryKey generic uno interface providing registry key to write
 */
extern "C" SAL_DLLPUBLIC_EXPORT sal_Bool SAL_CALL component_writeInfo( void* pServiceManager, void* pRegistryKey )
{
    return pRegistryKey &&
    writeInfo (pRegistryKey,
               XTempFile::getImplementationName_Static(),
               XTempFile::getSupportedServiceNames_Static() );
}


/**
 * This function is called to get service factories for an implementation.
 * @param pImplName name of implementation
 * @param pServiceManager generic uno interface providing a service manager to instantiate components
 * @param pRegistryKey registry data key to read and write component persistent data
 * @return a component factory (generic uno interface)
 */
extern "C" SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
    const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
{
    void * pRet = 0;
    Reference< XMultiServiceFactory > xSMgr(
            reinterpret_cast< XMultiServiceFactory * >( pServiceManager ) );
    Reference< XSingleServiceFactory > xFactory;

    if (XTempFile::getImplementationName_Static().compareToAscii( pImplName ) == 0)
        xFactory = XTempFile::createServiceFactory_Static ( xSMgr );

    if ( xFactory.is() )
    {
        xFactory->acquire();
        pRet = xFactory.get();
    }
    return pRet;
}