sbTemporaryFileFactory.cpp
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 :miv */
3 /*
4  *=BEGIN SONGBIRD GPL
5  *
6  * This file is part of the Songbird web player.
7  *
8  * Copyright(c) 2005-2010 POTI, Inc.
9  * http://www.songbirdnest.com
10  *
11  * This file may be licensed under the terms of of the
12  * GNU General Public License Version 2 (the ``GPL'').
13  *
14  * Software distributed under the License is distributed
15  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
16  * express or implied. See the GPL for the specific language
17  * governing rights and limitations.
18  *
19  * You should have received a copy of the GPL along with this
20  * program. If not, go to http://www.gnu.org/licenses/gpl.html
21  * or write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23  *
24  *=END SONGBIRD GPL
25  */
26 
27 //------------------------------------------------------------------------------
28 //------------------------------------------------------------------------------
29 //
30 // Songbird temporary file factory.
31 //
32 //------------------------------------------------------------------------------
33 //------------------------------------------------------------------------------
34 
40 //------------------------------------------------------------------------------
41 //
42 // Songbird temporary file factory imported services.
43 //
44 //------------------------------------------------------------------------------
45 
46 // Self imports.
47 #include "sbTemporaryFileFactory.h"
48 
49 // Songbird imports.
50 #include <sbFileUtils.h>
51 #include <sbITemporaryFileService.h>
52 #include <sbStringUtils.h>
53 
54 // Mozilla imports.
55 #include <nsServiceManagerUtils.h>
56 
57 
58 //------------------------------------------------------------------------------
59 //
60 // Songbird temporary file factory nsISupports implementation.
61 //
62 //------------------------------------------------------------------------------
63 
65 
66 
67 //------------------------------------------------------------------------------
68 //
69 // Songbird temporary file factory sbITemporaryFileFactory implementation.
70 //
71 //------------------------------------------------------------------------------
72 
73 //-------------------------------------
74 //
75 // CreateFile
76 //
77 
78 NS_IMETHODIMP
79 sbTemporaryFileFactory::CreateFile(PRUint32 aType,
80  const nsAString& aBaseName,
81  const nsAString& aExtension,
82  nsIFile** _retval)
83 {
84  // Validate arguments and state.
85  NS_ENSURE_ARG_POINTER(_retval);
86 
87  // Function variables.
88  nsresult rv;
89 
90  // Ensure the root temporary directory exists.
91  rv = EnsureRootTemporaryDirectory();
92  NS_ENSURE_SUCCESS(rv, rv);
93 
94  // Start the temporary file object in the root temporary directory.
95  nsCOMPtr<nsIFile> file;
96  rv = mRootTemporaryDirectory->Clone(getter_AddRefs(file));
97  NS_ENSURE_SUCCESS(rv, rv);
98 
99  // Produce file name from base file name and extension and append to temporary
100  // file object.
101  nsAutoString fileName;
102  if (!aBaseName.IsEmpty()) {
103  fileName.Assign(aBaseName);
104  }
105  else {
106  fileName.Assign(NS_LITERAL_STRING("tmp"));
107  AppendInt(fileName, PR_Now());
108  }
109  if (!aExtension.IsEmpty()) {
110  fileName.Append(NS_LITERAL_STRING("."));
111  fileName.Append(aExtension);
112  }
113  rv = file->Append(fileName);
114  NS_ENSURE_SUCCESS(rv, rv);
115 
116  // Determine the desired temporary file permissions.
117  PRUint32 permissions;
118  if (aType == nsIFile::DIRECTORY_TYPE)
119  permissions = SB_DEFAULT_DIRECTORY_PERMISSIONS;
120  else
121  permissions = SB_DEFAULT_FILE_PERMISSIONS;
122 
123  // Create a unique temporary file.
124  rv = file->CreateUnique(aType, permissions);
125  NS_ENSURE_SUCCESS(rv, rv);
126 
127  // Return results.
128  file.forget(_retval);
129 
130  return NS_OK;
131 }
132 
133 
134 NS_IMETHODIMP
135 sbTemporaryFileFactory::Clear()
136 {
137  // Remove root temporary directory.
138  if (mRootTemporaryDirectory)
139  mRootTemporaryDirectory->Remove(PR_TRUE);
140  mRootTemporaryDirectory = nsnull;
141 
142  return NS_OK;
143 }
144 
145 
146 //
147 // Getters/setters.
148 //
149 
150 //-------------------------------------
151 //
152 // rootTemporaryDirectory
153 //
154 
155 NS_IMETHODIMP
156 sbTemporaryFileFactory::SetRootTemporaryDirectory
157  (nsIFile* aRootTemporaryDirectory)
158 {
159  mRootTemporaryDirectory = aRootTemporaryDirectory;
160  return NS_OK;
161 }
162 
163 NS_IMETHODIMP
164 sbTemporaryFileFactory::GetRootTemporaryDirectory
165  (nsIFile** aRootTemporaryDirectory)
166 {
167  NS_ENSURE_ARG_POINTER(aRootTemporaryDirectory);
168  nsresult rv = EnsureRootTemporaryDirectory();
169  NS_ENSURE_SUCCESS(rv, rv);
170  NS_ADDREF(*aRootTemporaryDirectory = mRootTemporaryDirectory);
171  return NS_OK;
172 }
173 
174 
175 //------------------------------------------------------------------------------
176 //
177 // Songbird temporary file factory public services.
178 //
179 //------------------------------------------------------------------------------
180 
181 //-------------------------------------
182 //
183 // sbTemporaryFileFactory
184 //
185 
187 {
188 }
189 
190 
191 //-------------------------------------
192 //
193 // ~sbTemporaryFileFactory
194 //
195 
197 {
198  // Clear the temporary files.
199  Clear();
200 }
201 
202 
203 //------------------------------------------------------------------------------
204 //
205 // Songbird temporary file factory private services.
206 //
207 //------------------------------------------------------------------------------
208 
209 //-------------------------------------
210 //
211 // EnsureRootTemporaryDirectory
212 //
213 
214 nsresult
215 sbTemporaryFileFactory::EnsureRootTemporaryDirectory()
216 {
217  nsresult rv;
218 
219  // Ensure that the root temporary directory exists.
220  if (!mRootTemporaryDirectory) {
221  // Get the temporary file services.
222  nsCOMPtr<sbITemporaryFileService>
223  temporaryFileService =
224  do_GetService("@songbirdnest.com/Songbird/TemporaryFileService;1", &rv);
225  NS_ENSURE_SUCCESS(rv, rv);
226 
227  // Create a root temporary directory.
228  rv = temporaryFileService->CreateFile
229  (nsIFile::DIRECTORY_TYPE,
230  SBVoidString(),
231  SBVoidString(),
232  getter_AddRefs(mRootTemporaryDirectory));
233  NS_ENSURE_SUCCESS(rv, rv);
234  }
235 
236  return NS_OK;
237 }
238 
239 
return NS_OK
#define SB_DEFAULT_FILE_PERMISSIONS
Definition: sbFileUtils.h:123
NS_DECL_ISUPPORTS NS_DECL_SBITEMPORARYFILEFACTORY sbTemporaryFileFactory()
NS_IMPL_THREADSAFE_ISUPPORTS1(sbDeviceCapsCompatibility, sbIDeviceCapsCompatibility) sbDeviceCapsCompatibility
static void AppendInt(nsAString &str, PRInt64 val)
Songbird Temporary File Factory Definitions.
#define SB_DEFAULT_DIRECTORY_PERMISSIONS
Definition: sbFileUtils.h:124
var file