sbFileUtils.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-2009 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 //
31 // Songbird file utilities.
32 //
33 //------------------------------------------------------------------------------
34 //------------------------------------------------------------------------------
35 
41 //------------------------------------------------------------------------------
42 //
43 // Songbird file utilities imported services.
44 //
45 //------------------------------------------------------------------------------
46 
47 // Self imports.
48 #include "sbFileUtils.h"
49 
50 // Mozilla imports.
51 #include <nsAutoPtr.h>
52 #include <nsComponentManagerUtils.h>
53 #include <nsILocalFile.h>
54 #include <nsMemory.h>
55 #include <nsStringGlue.h>
56 
57 // Windows imports.
58 #ifdef XP_WIN
59 #include <direct.h>
60 #include <windows.h>
61 #endif
62 
63 // Std C imports.
64 #ifndef XP_WIN
65 #include <unistd.h>
66 #endif
67 
68 
69 //------------------------------------------------------------------------------
70 //
71 // Songbird file utilities defs.
72 //
73 //------------------------------------------------------------------------------
74 
75 //
76 // MAXPATHLEN Maximum file path length.
77 //
78 
79 #ifndef MAXPATHLEN
80 #ifdef PATH_MAX
81 #define MAXPATHLEN PATH_MAX
82 #elif defined(MAX_PATH)
83 #define MAXPATHLEN MAX_PATH
84 #elif defined(_MAX_PATH)
85 #define MAXPATHLEN _MAX_PATH
86 #elif defined(CCHMAXPATH)
87 #define MAXPATHLEN CCHMAXPATH
88 #else
89 #define MAXPATHLEN 1024
90 #endif
91 #endif
92 
93 
94 //------------------------------------------------------------------------------
95 //
96 // Songbird file utilities nsISupports implementation.
97 //
98 //------------------------------------------------------------------------------
99 
101 
102 
103 //------------------------------------------------------------------------------
104 //
105 // Songbird file utilities sbIFileUtils implementation.
106 //
107 //------------------------------------------------------------------------------
108 
109 //
110 // Getters/setters.
111 //
112 
113 //-------------------------------------
114 //
115 // currentDir
116 //
117 
118 NS_IMETHODIMP
119 sbFileUtils::GetCurrentDir(nsIFile** aCurrentDir)
120 {
121  // Validate arguments.
122  NS_ENSURE_ARG_POINTER(aCurrentDir);
123 
124  // Function variables.
125  nsresult rv;
126 
127  // Get the current directory.
128  nsCOMPtr<nsILocalFile> currentDir;
129 #ifdef XP_WIN
130  WCHAR currentDirPath[MAX_PATH];
131  NS_ENSURE_TRUE(_wgetcwd(currentDirPath, NS_ARRAY_LENGTH(currentDirPath)),
132  NS_ERROR_FAILURE);
133  rv = NS_NewLocalFile(nsDependentString(currentDirPath),
134  PR_TRUE,
135  getter_AddRefs(currentDir));
136  NS_ENSURE_SUCCESS(rv, rv);
137 #else
138  char currentDirPath[MAXPATHLEN];
139  NS_ENSURE_TRUE(getcwd(currentDirPath, NS_ARRAY_LENGTH(currentDirPath)),
140  NS_ERROR_FAILURE);
141  rv = NS_NewNativeLocalFile(nsDependentCString(currentDirPath),
142  PR_TRUE,
143  getter_AddRefs(currentDir));
144  NS_ENSURE_SUCCESS(rv, rv);
145 #endif
146 
147  // Return results.
148  rv = CallQueryInterface(currentDir, aCurrentDir);
149  NS_ENSURE_SUCCESS(rv, rv);
150 
151  return NS_OK;
152 }
153 
154 NS_IMETHODIMP
155 sbFileUtils::SetCurrentDir(nsIFile* aCurrentDir)
156 {
157  // Validate arguments.
158  NS_ENSURE_ARG_POINTER(aCurrentDir);
159 
160  // Function variables.
161  nsresult rv;
162 
163  // Get the new current directory path.
164  nsAutoString currentDirPath;
165  rv = aCurrentDir->GetPath(currentDirPath);
166  NS_ENSURE_SUCCESS(rv, rv);
167 
168  // Set the new current directory.
169 #ifdef XP_WIN
170  NS_ENSURE_FALSE(_wchdir(currentDirPath.get()), NS_ERROR_FAILURE);
171 #else
172  NS_ENSURE_FALSE(chdir(NS_ConvertUTF16toUTF8(currentDirPath).get()),
173  NS_ERROR_FAILURE);
174 #endif
175 
176  return NS_OK;
177 }
178 
179 NS_IMETHODIMP
180 sbFileUtils::GetExactPath(const nsAString& aFilePath, nsAString& aExactPath)
181 {
182  // Default to an empty string
183  aExactPath.Truncate();
184 
185 #ifdef XP_WIN
186  long length = 0;
187  nsAutoArrayPtr<WCHAR> shortPath = NULL;
188  nsAutoArrayPtr<WCHAR> longPath = NULL;
189  // First obtain the size needed by passing NULL and 0.
190  length = ::GetShortPathNameW(aFilePath.BeginReading(),
191  NULL,
192  0);
193  if (length == 0) return NS_OK;
194 
195  // Dynamically allocate the correct size
196  shortPath = new WCHAR[length];
197  length = ::GetShortPathNameW(aFilePath.BeginReading(),
198  shortPath,
199  length);
200  if (length == 0) return NS_OK;
201 
202  length = 0;
203  // First obtain the size needed by passing NULL and 0.
204  length = ::GetLongPathNameW(shortPath,
205  NULL,
206  0);
207  if (length == 0) return NS_OK;
208 
209 
210  // Dynamically allocate the correct size
211  longPath = new WCHAR[length];
212  length = ::GetLongPathNameW(shortPath,
213  longPath,
214  length);
215  if (length == 0) return NS_OK;
216 
217  aExactPath.Assign(longPath);
218 #endif
219 
220  return NS_OK;
221 }
222 
223 //------------------------------------------------------------------------------
224 //
225 // Songbird file utilities public services.
226 //
227 //------------------------------------------------------------------------------
228 
234 {
235 }
236 
237 
243 {
244 }
245 
return NS_OK
virtual ~sbFileUtils()
NS_IMPL_THREADSAFE_ISUPPORTS1(sbDeviceCapsCompatibility, sbIDeviceCapsCompatibility) sbDeviceCapsCompatibility
#define MAXPATHLEN
Definition: sbFileUtils.cpp:89
NS_DECL_ISUPPORTS NS_DECL_SBIFILEUTILS sbFileUtils()