Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 : #include <unotools/unotoolsdllapi.h>
20 :
21 : #ifndef INCLUDED_UNOTOOLS_TEMPFILE_HXX
22 : #define INCLUDED_UNOTOOLS_TEMPFILE_HXX
23 :
24 : #include <tools/stream.hxx>
25 :
26 : namespace utl
27 : {
28 :
29 : struct TempFile_Impl;
30 :
31 : /**
32 : The class TempFile gives access to temporary files in the local file system. Sometimes they are needed because a 3rd party
33 : code has a file name based interface, or some file access has to be done locally without transferring tons of bytes to or
34 : from a remote system.
35 : Creating a UCB content on a TempFile is only possible if a UCP for the local file system is present.
36 : TempFiles can always be accessed by SvFileStreams or Sot/SvStorages using the "physical" file name ( not the URL, because
37 : 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
38 : the UCB helper classes for streams. For convenience use UcbStreamHelper.
39 : A Tempfile always has a "physical" file name ( a file name in the local computers host notation ) but it has a
40 : "UCB compatible" URL only if a UCP for the local file system exists. This URL may have its own URL scheme
41 : ( not necessarily "file://" ! ). The TempFile class methods take this into account, but other simple conversions like
42 : the osl functions do not.
43 : So it is a potential error to convert between the filename and the URL of a TempFile object using functions or methods
44 : outside this class.
45 : */
46 :
47 : class UNOTOOLS_DLLPUBLIC TempFile
48 : {
49 : TempFile_Impl* pImp;
50 : bool bKillingFileEnabled;
51 :
52 : // prevent copy c'tor
53 : TempFile( const TempFile& );
54 : TempFile& operator=(const TempFile&);
55 :
56 : protected:
57 :
58 : public:
59 : /**
60 : Create a temporary file or directory, in the default tempfile folder or if possible in a given folder.
61 : This given folder ( the "parent" parameter ( if not NULL ) ) must be a "UCB compatible" URL.
62 : The temporary object is created in the local file system, even if there is no UCB that can access it.
63 : If the given folder is part of the local file system, the TempFile is created in this folder.
64 : */
65 : TempFile( const OUString* pParent=NULL, bool bDirectory=false );
66 :
67 : /**
68 : Same as above; additionally the name starts with some given characters followed by a counter ( example:
69 : rLeadingChars="abc" means "abc0","abc1" and so on, depending on existing files in the folder ).
70 : The extension string may be f.e. ".txt" or "", if no extension string is given, ".tmp" is used
71 : */
72 : TempFile( const OUString& rLeadingChars, const OUString* pExtension=NULL, const OUString* pParent=NULL,
73 : bool bDirectory=false);
74 :
75 : /**
76 : Same as above; additionally the name starts with some given characters followed by a counter ( example:
77 : rLeadingChars="abc" means "abc0","abc1" and so on, depending on existing files in the folder ).
78 : The extension string may be f.e. ".txt" or "", if no extension string is given, ".tmp" is used
79 : @param _bStartWithZero If set to false names will be generated like "abc","abc0","abc1"
80 : */
81 : TempFile( const OUString& rLeadingChars, bool _bStartWithZero, const OUString* pExtension=NULL, const OUString* pParent=NULL, bool bDirectory=false);
82 :
83 : /**
84 : TempFile will be removed from disk in dtor if EnableKillingTempFile was called before.
85 : Temporary directories will be removed recursively in that case.
86 : */
87 : ~TempFile();
88 :
89 : /**
90 : Returns sal_True if it has a valid file name.
91 : */
92 : bool IsValid() const;
93 :
94 : /**
95 : Returns the "UCB compatible" URL of the tempfile object.
96 : If you want to have the "physical" file name, use the GetFileName() method of this object, because these
97 : method uses the UCB for the conversion, but never use any external conversion functions for URLs into
98 : "physical" names.
99 : If no UCP is available for the local file system, an empty URL is returned. In this case you can't access
100 : the file as a UCB content !
101 : */
102 : OUString GetURL() const;
103 :
104 : /**
105 : Returns the "physical" name of the tempfile in host notation ( should only be used for 3rd party code
106 : with file name interfaces ).
107 : If you want to have the URL, use the GetURL() method of this object, but never use any external
108 : conversion functions for "physical" names into URLs.
109 : */
110 : OUString GetFileName() const;
111 :
112 : /**
113 : Returns a stream to the tempfiles data; the stream is owned by the tempfile object, so you have to keep this
114 : alive as long as you want to use the stream. If the TempFile object is destroyed, it also destroys the
115 : stream object, the underlying file is only deleted if EnableKillingFile( sal_True ) has been called before!
116 : */
117 : SvStream* GetStream( StreamMode eMode );
118 :
119 : /**
120 : Let the TempFile object close and destroy the owned stream object if any.
121 : */
122 : void CloseStream();
123 :
124 : /**
125 : If enabled the file will be removed from disk when the dtor is called ( default is not enabled )
126 : */
127 0 : void EnableKillingFile( bool bEnable=true )
128 0 : { bKillingFileEnabled = bEnable; }
129 :
130 : bool IsKillingFileEnabled() const
131 : { return bKillingFileEnabled; }
132 :
133 : /**
134 : Only create a "physical" file name for a temporary file that would be valid at that moment.
135 : Should only be used for 3rd party code with a file name interface that wants to create the file by itself.
136 : If you want to convert file name into a URL, always use class LocalFileHelper, but never use any
137 : conversion functions of osl.
138 : */
139 : static OUString CreateTempName( const OUString* pParent=NULL );
140 :
141 : /**
142 : The TempNameBaseDirectory is a subfolder in the folder that is passed as a "physical" file name in the
143 : SetTempNameBaseDirectory method.
144 : This subfolder will be used if a TempFile or TempName is created without a parent name or a parent name
145 : that does not belong to the local file system.
146 : The caller of the SetTempNameBase is responsible for deleting this folder and all temporary files in it.
147 : The return value of both methods is the complete "physical" name of the tempname base folder.
148 : It is not a URL because alle URLs must be "UCB compatible", so there may be no suitable URL at all.
149 : */
150 : static OUString SetTempNameBaseDirectory( const OUString &rBaseName );
151 : };
152 :
153 : }
154 :
155 : #endif
156 :
157 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|