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 : TempFile( const TempFile& ) SAL_DELETED_FUNCTION;
54 : TempFile& operator=(const TempFile&) SAL_DELETED_FUNCTION;
55 :
56 : public:
57 : /**
58 : Create a temporary file or directory, in the default tempfile folder or if possible in a given folder.
59 : This given folder ( the "parent" parameter ( if not NULL ) ) must be a "UCB compatible" URL.
60 : The temporary object is created in the local file system, even if there is no UCB that can access it.
61 : If the given folder is part of the local file system, the TempFile is created in this folder.
62 : */
63 : TempFile( const OUString* pParent=NULL, bool bDirectory=false );
64 :
65 : /**
66 : Same as above; additionally the name starts with some given characters followed by a counter ( example:
67 : rLeadingChars="abc" means "abc0","abc1" and so on, depending on existing files in the folder ).
68 : The extension string may be f.e. ".txt" or "", if no extension string is given, ".tmp" is used
69 : @param _bStartWithZero If set to false names will be generated like "abc","abc0","abc1"
70 : */
71 : TempFile( const OUString& rLeadingChars, bool _bStartWithZero=true, const OUString* pExtension=NULL, const OUString* pParent=NULL, bool bDirectory=false);
72 :
73 : /**
74 : TempFile will be removed from disk in dtor if EnableKillingFile(true) was called before.
75 : Temporary directories will be removed recursively in that case.
76 : */
77 : ~TempFile();
78 :
79 : /**
80 : Returns sal_True if it has a valid file name.
81 : */
82 : bool IsValid() const;
83 :
84 : /**
85 : Returns the "UCB compatible" URL of the tempfile object.
86 : If you want to have the "physical" file name, use the GetFileName() method of this object, because these
87 : method uses the UCB for the conversion, but never use any external conversion functions for URLs into
88 : "physical" names.
89 : If no UCP is available for the local file system, an empty URL is returned. In this case you can't access
90 : the file as a UCB content !
91 : */
92 : OUString GetURL();
93 :
94 : /**
95 : Returns the "physical" name of the tempfile in host notation ( should only be used for 3rd party code
96 : with file name interfaces ).
97 : If you want to have the URL, use the GetURL() method of this object, but never use any external
98 : conversion functions for "physical" names into URLs.
99 : */
100 : OUString GetFileName() const;
101 :
102 : /**
103 : Returns a stream to the tempfiles data; the stream is owned by the tempfile object, so you have to keep this
104 : alive as long as you want to use the stream. If the TempFile object is destroyed, it also destroys the
105 : stream object, the underlying file is only deleted if EnableKillingFile(true) has been called before!
106 : */
107 : SvStream* GetStream( StreamMode eMode );
108 :
109 : /**
110 : Let the TempFile object close and destroy the owned stream object if any.
111 : */
112 : void CloseStream();
113 :
114 : /**
115 : If enabled the file will be removed from disk when the dtor is called ( default is not enabled )
116 : */
117 27695 : void EnableKillingFile( bool bEnable=true )
118 27695 : { bKillingFileEnabled = bEnable; }
119 :
120 : /**
121 : Only create a "physical" file name for a temporary file that would be valid at that moment.
122 : Should only be used for 3rd party code with a file name interface that wants to create the file by itself.
123 : If you want to convert file name into a URL, always use class LocalFileHelper, but never use any
124 : conversion functions of osl.
125 : */
126 : static OUString CreateTempName();
127 :
128 : /**
129 : The TempNameBaseDirectory is a subfolder in the folder that is passed as a "physical" file name in the
130 : SetTempNameBaseDirectory method.
131 : This subfolder will be used if a TempFile or TempName is created without a parent name or a parent name
132 : that does not belong to the local file system.
133 : The caller of the SetTempNameBase is responsible for deleting this folder and all temporary files in it.
134 : The return value of both methods is the complete "physical" name of the tempname base folder.
135 : It is not a URL because alle URLs must be "UCB compatible", so there may be no suitable URL at all.
136 : */
137 : static OUString SetTempNameBaseDirectory( const OUString &rBaseName );
138 : };
139 :
140 : }
141 :
142 : #endif
143 :
144 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|