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