1*b1cdbd2cSJim Jagielski /**************************************************************
2*b1cdbd2cSJim Jagielski  *
3*b1cdbd2cSJim Jagielski  * Licensed to the Apache Software Foundation (ASF) under one
4*b1cdbd2cSJim Jagielski  * or more contributor license agreements.  See the NOTICE file
5*b1cdbd2cSJim Jagielski  * distributed with this work for additional information
6*b1cdbd2cSJim Jagielski  * regarding copyright ownership.  The ASF licenses this file
7*b1cdbd2cSJim Jagielski  * to you under the Apache License, Version 2.0 (the
8*b1cdbd2cSJim Jagielski  * "License"); you may not use this file except in compliance
9*b1cdbd2cSJim Jagielski  * with the License.  You may obtain a copy of the License at
10*b1cdbd2cSJim Jagielski  *
11*b1cdbd2cSJim Jagielski  *   http://www.apache.org/licenses/LICENSE-2.0
12*b1cdbd2cSJim Jagielski  *
13*b1cdbd2cSJim Jagielski  * Unless required by applicable law or agreed to in writing,
14*b1cdbd2cSJim Jagielski  * software distributed under the License is distributed on an
15*b1cdbd2cSJim Jagielski  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b1cdbd2cSJim Jagielski  * KIND, either express or implied.  See the License for the
17*b1cdbd2cSJim Jagielski  * specific language governing permissions and limitations
18*b1cdbd2cSJim Jagielski  * under the License.
19*b1cdbd2cSJim Jagielski  *
20*b1cdbd2cSJim Jagielski  *************************************************************/
21*b1cdbd2cSJim Jagielski 
22*b1cdbd2cSJim Jagielski 
23*b1cdbd2cSJim Jagielski #include "unotools/unotoolsdllapi.h"
24*b1cdbd2cSJim Jagielski 
25*b1cdbd2cSJim Jagielski #ifndef _UNOTOOLS_TEMPFILE_HXX
26*b1cdbd2cSJim Jagielski #define _UNOTOOLS_TEMPFILE_HXX
27*b1cdbd2cSJim Jagielski 
28*b1cdbd2cSJim Jagielski #include <tools/string.hxx>
29*b1cdbd2cSJim Jagielski #include <tools/stream.hxx>
30*b1cdbd2cSJim Jagielski 
31*b1cdbd2cSJim Jagielski namespace utl
32*b1cdbd2cSJim Jagielski {
33*b1cdbd2cSJim Jagielski 
34*b1cdbd2cSJim Jagielski struct TempFile_Impl;
35*b1cdbd2cSJim Jagielski 
36*b1cdbd2cSJim Jagielski /**
37*b1cdbd2cSJim Jagielski     The class TempFile gives access to temporary files in the local file system. Sometimes they are needed because a 3rd party
38*b1cdbd2cSJim Jagielski     code has a file name based interface, or some file access has to be done locally without transferring tons of bytes to or
39*b1cdbd2cSJim Jagielski     from a remote system.
40*b1cdbd2cSJim Jagielski     Creating a UCB content on a TempFile is only possible if a UCP for the local file system is present.
41*b1cdbd2cSJim Jagielski     TempFiles can always be accessed by SvFileStreams or Sot/SvStorages using the "physical" file name ( not the URL, because
42*b1cdbd2cSJim Jagielski     this may be a non-file URL, see below ), but if a UCB content can be created, it is also possible to take the URL and use
43*b1cdbd2cSJim Jagielski     the UCB helper classes for streams. For convenience use UcbStreamHelper.
44*b1cdbd2cSJim Jagielski     A Tempfile always has a "physical" file name ( a file name in the local computers host notation ) but it has a
45*b1cdbd2cSJim Jagielski     "UCB compatible" URL only if a UCP for the local file system exists. This URL may have its own URL scheme
46*b1cdbd2cSJim Jagielski     ( not neccessarily "file://" ! ). The TempFile class methods take this into account, but other simple conversions like
47*b1cdbd2cSJim Jagielski     the osl functions do not.
48*b1cdbd2cSJim Jagielski     So it is a potential error to convert between the filename and the URL of a TempFile object using functions or methods
49*b1cdbd2cSJim Jagielski     outside this class.
50*b1cdbd2cSJim Jagielski */
51*b1cdbd2cSJim Jagielski 
52*b1cdbd2cSJim Jagielski class UNOTOOLS_DLLPUBLIC TempFile
53*b1cdbd2cSJim Jagielski {
54*b1cdbd2cSJim Jagielski     TempFile_Impl*  pImp;
55*b1cdbd2cSJim Jagielski     sal_Bool        bKillingFileEnabled;
56*b1cdbd2cSJim Jagielski 
57*b1cdbd2cSJim Jagielski protected:
58*b1cdbd2cSJim Jagielski 
59*b1cdbd2cSJim Jagielski public:
60*b1cdbd2cSJim Jagielski                     /**
61*b1cdbd2cSJim Jagielski                     Create a temporary file or directory, in the default tempfile folder or if possible in a given folder.
62*b1cdbd2cSJim Jagielski                     This given folder ( the "parent" parameter ( if not NULL ) ) must be a "UCB compatible" URL.
63*b1cdbd2cSJim Jagielski                     The temporary object is created in the local file system, even if there is no UCB that can access it.
64*b1cdbd2cSJim Jagielski                     If the given folder is part of the local file system, the TempFile is created in this folder.
65*b1cdbd2cSJim Jagielski                     */
66*b1cdbd2cSJim Jagielski                     TempFile( const String* pParent=NULL, sal_Bool bDirectory=sal_False );
67*b1cdbd2cSJim Jagielski 
68*b1cdbd2cSJim Jagielski                     /**
69*b1cdbd2cSJim Jagielski                     Same as above; additionally the name starts with some given characters followed by a counter ( example:
70*b1cdbd2cSJim Jagielski                     rLeadingChars="abc" means "abc0","abc1" and so on, depending on existing files in the folder ).
71*b1cdbd2cSJim Jagielski                     The extension string may be f.e. ".txt" or "", if no extension string is given, ".tmp" is used
72*b1cdbd2cSJim Jagielski                     */
73*b1cdbd2cSJim Jagielski                     TempFile( const String& rLeadingChars, const String* pExtension=NULL, const String* pParent=NULL,
74*b1cdbd2cSJim Jagielski                                 sal_Bool bDirectory=sal_False);
75*b1cdbd2cSJim Jagielski 
76*b1cdbd2cSJim Jagielski                     /**
77*b1cdbd2cSJim Jagielski                     Same as above; additionally the name starts with some given characters followed by a counter ( example:
78*b1cdbd2cSJim Jagielski                     rLeadingChars="abc" means "abc0","abc1" and so on, depending on existing files in the folder ).
79*b1cdbd2cSJim Jagielski                     The extension string may be f.e. ".txt" or "", if no extension string is given, ".tmp" is used
80*b1cdbd2cSJim Jagielski                         @param  _bStartWithZero If set to false names will be generated like "abc","abc0","abc1"
81*b1cdbd2cSJim Jagielski                     */
82*b1cdbd2cSJim Jagielski                     TempFile( const String& rLeadingChars,sal_Bool _bStartWithZero, const String* pExtension=NULL, const String* pParent=NULL,sal_Bool bDirectory=sal_False);
83*b1cdbd2cSJim Jagielski 
84*b1cdbd2cSJim Jagielski                     /**
85*b1cdbd2cSJim Jagielski                     TempFile will be removed from disk in dtor if EnableKillingTempFile was called before.
86*b1cdbd2cSJim Jagielski                     Temporary directories will be removed recursively in that case.
87*b1cdbd2cSJim Jagielski                     */
88*b1cdbd2cSJim Jagielski                     ~TempFile();
89*b1cdbd2cSJim Jagielski 
90*b1cdbd2cSJim Jagielski                     /**
91*b1cdbd2cSJim Jagielski                     Returns sal_True if it has a valid file name.
92*b1cdbd2cSJim Jagielski                     */
93*b1cdbd2cSJim Jagielski     sal_Bool        IsValid() const;
94*b1cdbd2cSJim Jagielski 
95*b1cdbd2cSJim Jagielski                     /**
96*b1cdbd2cSJim Jagielski                     Returns the "UCB compatible" URL of the tempfile object.
97*b1cdbd2cSJim Jagielski                     If you want to have the "physical" file name, use the GetFileName() method of this object, because these
98*b1cdbd2cSJim Jagielski                     method uses the UCB for the conversion, but never use any external conversion functions for URLs into
99*b1cdbd2cSJim Jagielski                     "physical" names.
100*b1cdbd2cSJim Jagielski                     If no UCP is available for the local file system, an empty URL is returned. In this case you can't access
101*b1cdbd2cSJim Jagielski                     the file as a UCB content !
102*b1cdbd2cSJim Jagielski                     */
103*b1cdbd2cSJim Jagielski     String          GetURL() const;
104*b1cdbd2cSJim Jagielski 
105*b1cdbd2cSJim Jagielski                     /**
106*b1cdbd2cSJim Jagielski                     Returns the "physical" name of the tempfile in host notation ( should only be used for 3rd party code
107*b1cdbd2cSJim Jagielski                     with file name interfaces ).
108*b1cdbd2cSJim Jagielski                     If you want to have the URL, use the GetURL() method of this object, but never use any external
109*b1cdbd2cSJim Jagielski                     conversion functions for "physical" names into URLs.
110*b1cdbd2cSJim Jagielski                     */
111*b1cdbd2cSJim Jagielski     String          GetFileName() const;
112*b1cdbd2cSJim Jagielski 
113*b1cdbd2cSJim Jagielski                     /**
114*b1cdbd2cSJim Jagielski                     Returns a stream to the tempfiles data; the stream is owned by the tempfile object, so you have to keep this
115*b1cdbd2cSJim Jagielski                     alive as long as you want to use the stream. If the TempFile object is destroyed, it also destroys the
116*b1cdbd2cSJim Jagielski                     stream object, the underlying file is only deleted if EnableKillingFile( sal_True ) has been called before!
117*b1cdbd2cSJim Jagielski                     */
118*b1cdbd2cSJim Jagielski     SvStream*       GetStream( StreamMode eMode );
119*b1cdbd2cSJim Jagielski 
120*b1cdbd2cSJim Jagielski                     /**
121*b1cdbd2cSJim Jagielski                     Let the TempFile object close and destroy the owned stream object if any.
122*b1cdbd2cSJim Jagielski                     */
123*b1cdbd2cSJim Jagielski     void            CloseStream();
124*b1cdbd2cSJim Jagielski 
125*b1cdbd2cSJim Jagielski                     /**
126*b1cdbd2cSJim Jagielski                     If enabled the file will be removed from disk when the dtor is called ( default is not enabled )
127*b1cdbd2cSJim Jagielski                     */
EnableKillingFile(sal_Bool bEnable=sal_True)128*b1cdbd2cSJim Jagielski     void            EnableKillingFile( sal_Bool bEnable=sal_True )
129*b1cdbd2cSJim Jagielski                     { bKillingFileEnabled = bEnable; }
130*b1cdbd2cSJim Jagielski 
IsKillingFileEnabled() const131*b1cdbd2cSJim Jagielski     sal_Bool        IsKillingFileEnabled() const
132*b1cdbd2cSJim Jagielski                     { return bKillingFileEnabled; }
133*b1cdbd2cSJim Jagielski 
134*b1cdbd2cSJim Jagielski                     /**
135*b1cdbd2cSJim Jagielski                     Only create a "physical" file name for a temporary file that would be valid at that moment.
136*b1cdbd2cSJim Jagielski                     Should only be used for 3rd party code with a file name interface that wants to create the file by itself.
137*b1cdbd2cSJim Jagielski                     If you want to convert file name into a URL, always use class LocalFileHelper, but never use any
138*b1cdbd2cSJim Jagielski                     conversion functions of osl.
139*b1cdbd2cSJim Jagielski                     */
140*b1cdbd2cSJim Jagielski     static String   CreateTempName( const String* pParent=NULL );
141*b1cdbd2cSJim Jagielski 
142*b1cdbd2cSJim Jagielski                     /**
143*b1cdbd2cSJim Jagielski                     The TempNameBaseDirectory is a subfolder in the folder that is passed as a "physical" file name in the
144*b1cdbd2cSJim Jagielski                     SetTempNameBaseDirectory method.
145*b1cdbd2cSJim Jagielski                     This subfolder will be used if a TempFile or TempName is created without a parent name or a parent name
146*b1cdbd2cSJim Jagielski                     that does not belong to the local file system.
147*b1cdbd2cSJim Jagielski                     The caller of the SetTempNameBase is responsible for deleting this folder and all temporary files in it.
148*b1cdbd2cSJim Jagielski                     The return value of both methods is the complete "physical" name of the tempname base folder.
149*b1cdbd2cSJim Jagielski                     It is not a URL because alle URLs must be "UCB compatible", so there may be no suitable URL at all.
150*b1cdbd2cSJim Jagielski                     */
151*b1cdbd2cSJim Jagielski     static String   SetTempNameBaseDirectory( const String &rBaseName );
152*b1cdbd2cSJim Jagielski     static String   GetTempNameBaseDirectory();
153*b1cdbd2cSJim Jagielski };
154*b1cdbd2cSJim Jagielski 
155*b1cdbd2cSJim Jagielski }
156*b1cdbd2cSJim Jagielski 
157*b1cdbd2cSJim Jagielski #endif
158