FolderUtils.jsm
Go to the documentation of this file.
1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 :miv */
3 /*
4 //
5 // BEGIN SONGBIRD GPL
6 //
7 // This file is part of the Songbird web player.
8 //
9 // Copyright(c) 2005-2008 POTI, Inc.
10 // http://songbirdnest.com
11 //
12 // This file may be licensed under the terms of of the
13 // GNU General Public License Version 2 (the "GPL").
14 //
15 // Software distributed under the License is distributed
16 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
17 // express or implied. See the GPL for the specific language
18 // governing rights and limitations.
19 //
20 // You should have received a copy of the GPL along with this
21 // program. If not, go to http://www.gnu.org/licenses/gpl.html
22 // or write to the Free Software Foundation, Inc.,
23 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 //
25 // END SONGBIRD GPL
26 //
27 */
28 
34 //------------------------------------------------------------------------------
35 //
36 // Folder utility JSM configuration.
37 //
38 //------------------------------------------------------------------------------
39 
40 EXPORTED_SYMBOLS = ["FolderUtils"];
41 
42 
43 //------------------------------------------------------------------------------
44 //
45 // Folder utility imported services.
46 //
47 //------------------------------------------------------------------------------
48 
49 Components.utils.import("resource://app/jsmodules/PlatformUtils.jsm");
50 Components.utils.import("resource://app/jsmodules/SBDataRemoteUtils.jsm");
51 Components.utils.import("resource://app/jsmodules/StringUtils.jsm");
52 
53 
54 //------------------------------------------------------------------------------
55 //
56 // Folder utility defs.
57 //
58 //------------------------------------------------------------------------------
59 
60 const Cc = Components.classes;
61 const Ci = Components.interfaces;
62 const Cr = Components.results
63 
64 var FolderUtils = {
65  get musicFolder() {
66  // Get the user home directory.
67  var directorySvc = Cc["@mozilla.org/file/directory_service;1"]
68  .getService(Ci.nsIProperties);
69 
70  var homeDir = null;
71  if (directorySvc.has("Home")) {
72  homeDir = directorySvc.get("Home", Ci.nsIFile);
73  }
74  var musicDir = null;
75 
76  // Get the default scan directory path.
77  var defaultScanPath = null;
78  var platform = PlatformUtils.platformString;
79 
80  switch (platform) {
81  case "Windows_NT":
82  try {
83  musicDir = directorySvc.get("Music", Ci.nsIFile);
84  }
85  catch(e) {
86  musicDir = null;
87  }
88 
89  if(musicDir && musicDir.exists() && musicDir.isReadable()) {
90  // Excellent!
91  defaultScanPath = musicDir;
92  }
93  else {
94  // Somewhat crappy fallback
95  let folder = directorySvc.get("Pers", Ci.nsIFile).clone();
96 
97  // First, check for "X:<Path To User Home>\My Documents\My Music".
98  folder.append(SBString("folder_paths.music.windows", "My Music"));
99 
100  if(folder.exists() && folder.isReadable()) {
101  defaultScanPath = folder;
102  }
103  else if (homeDir) {
104  //Second, check for "X:<Path To User Home>\Music".
105 
106  folder = homeDir.clone();
107  folder.append(SBString("folder_paths.music.vista", "Music"));
108 
109  if(folder.exists() && folder.isReadable()) {
110  defaultScanPath = folder;
111  }
112  }
113  }
114  break;
115 
116  case "Darwin":
117  try {
118  musicDir = directorySvc.get("Music", Ci.nsIFile);
119  }
120  catch(e) {
121  musicDir = null;
122  }
123 
124  if(musicDir && musicDir.exists() && musicDir.isReadable()) {
125  // Excellent!
126  defaultScanPath = musicDir;
127  }
128  else if (homeDir) {
129  // Also, somewhat crappy fallback
130  let defaultScanDir = homeDir.clone();
131  defaultScanDir.append(SBString("folder_paths.music.darwin", "Music"));
132  if (defaultScanDir.exists()) {
133  defaultScanPath = defaultScanDir;
134  }
135  }
136  break;
137 
138  case "SunOS":
139  case "Linux":
140  // Linux / Unix in general has "XDGMusic" as the directory service
141  // definitino. Attempt to get the Music folder using that first.
142  try {
143  musicDir = directorySvc.get("XDGMusic", Ci.nsIFile);
144  }
145  catch(e) {
146  musicDir = null;
147  }
148 
149  if(musicDir && musicDir.exists() && musicDir.isReadable()) {
150  // Excellent!
151  defaultScanPath = musicDir;
152  }
153  else if (homeDir) {
154  let folder = homeDir.clone();
155  let folderName = SBString("folder_paths.music.linux", "Music");
156  folder.append(folderName);
157 
158  if(folder.exists() && folder.isReadable()) {
159  defaultScanPath = folder;
160  }
161  else {
162  folder = homeDir.clone();
163  folder.append(folderName.toLowerCase());
164 
165  if(folder.exists() && folder.isReadable()) {
166  defaultScanPath = folder;
167  }
168  }
169  }
170  break;
171 
172  default:
173  defaultScanPath = null;
174  break;
175  }
176 
177  return defaultScanPath;
178  },
179 
180  get videoFolder() {
181  // Get the user home directory.
182  var directorySvc = Cc["@mozilla.org/file/directory_service;1"]
183  .getService(Ci.nsIProperties);
184 
185  var homeDir = null;
186  if (directorySvc.has("Home")) {
187  homeDir = directorySvc.get("Home", Ci.nsIFile);
188  }
189  var mediaDir = null;
190 
191  // Get the default scan directory path.
192  var defaultScanPath = null;
193  var platform = PlatformUtils.platformString;
194 
195  switch (platform) {
196  case "Windows_NT":
197  try {
198  mediaDir = directorySvc.get("Video", Ci.nsIFile);
199  }
200  catch(e) {
201  mediaDir = null;
202  }
203 
204  if(mediaDir && mediaDir.exists() && mediaDir.isReadable()) {
205  // Excellent!
206  defaultScanPath = mediaDir;
207  }
208  else {
209  // Somewhat crappy fallback
210  let folder = directorySvc.get("Pers", Ci.nsIFile).clone();
211 
212  // First, check for "X:<Path To User Home>\My Documents\My Videos".
213  folder.append(SBString("folder_paths.videos.windows", "My Videos"));
214 
215  if(folder.exists() && folder.isReadable()) {
216  defaultScanPath = folder;
217  }
218  else if (homeDir) {
219  //Second, check for "X:<Path To User Home>\Videos".
220 
221  folder = homeDir.clone();
222  folder.append(SBString("folder_paths.videos.vista", "Videos"));
223 
224  if(folder.exists() && folder.isReadable()) {
225  defaultScanPath = folder;
226  }
227  }
228  }
229  break;
230 
231  case "Darwin":
232  try {
233  mediaDir = directorySvc.get("Movies", Ci.nsIFile);
234  }
235  catch(e) {
236  mediaDir = null;
237  }
238 
239  if(mediaDir && mediaDir.exists() && mediaDir.isReadable()) {
240  // Excellent!
241  defaultScanPath = mediaDir;
242  }
243  else if (homeDir) {
244  // Also, somewhat crappy fallback
245  let defaultScanDir = homeDir.clone();
246  defaultScanDir.append(SBString("folder_paths.videos.darwin", "Movies"));
247  if (defaultScanDir.exists()) {
248  defaultScanPath = defaultScanDir;
249  }
250  }
251  break;
252 
253  case "SunOS":
254  case "Linux":
255  // Linux / Unix in general has "XDGVids" as the directory service
256  // definitino. Attempt to get the videos folder using that first.
257  try {
258  mediaDir = directorySvc.get("XDGVids", Ci.nsIFile);
259  }
260  catch(e) {
261  mediaDir = null;
262  }
263 
264  if(mediaDir && mediaDir.exists() && mediaDir.isReadable()) {
265  // Excellent!
266  defaultScanPath = mediaDir;
267  }
268  else if (homeDir) {
269  let folder = homeDir.clone();
270  let folderName = SBString("folder_paths.videos.linux", "Videos");
271  folder.append(folderName);
272 
273  if(folder.exists() && folder.isReadable()) {
274  defaultScanPath = folder;
275  }
276  else {
277  folder = homeDir.clone();
278  folder.append(folderName.toLowerCase());
279 
280  if(folder.exists() && folder.isReadable()) {
281  defaultScanPath = folder;
282  }
283  }
284  }
285  break;
286 
287  default:
288  defaultScanPath = null;
289  break;
290  }
291 
292  return defaultScanPath;
293  },
294 
295  getMediaFolder: function (aContentType) {
296  switch (aContentType) {
297  case "audio": return this.musicFolder
298  case Ci.sbIMediaList.CONTENTTYPE_AUDIO: return this.musicFolder;
299  case "video": return this.videoFolder;
300  case Ci.sbIMediaList.CONTENTTYPE_VIDEO: return this.videoFolder;
301  }
302 
303  return null;
304  },
305 
306  getDownloadFolder: function (aContentType) {
307  var downloadFolder = this.getMediaFolder(aContentType);
308 
309  if(downloadFolder == null) {
310  var directorySvc = Cc["@mozilla.org/file/directory_service;1"]
311  .getService(Ci.nsIProperties);
312 
313  // If the media folder is not available, default to the Desktop:
314  try {
315  downloadFolder = directorySvc.get("Desk", Ci.nsIFile);
316  }
317  catch(e) {
318  downloadFolder = null;
319  }
320  }
321 
322  return downloadFolder;
323  }
324 };
EXPORTED_SYMBOLS
Definition: FolderUtils.jsm:40
const Cr
Definition: FolderUtils.jsm:62
function SBString(aKey, aDefault, aStringBundle)
Definition: StringUtils.jsm:93
const Cc
Definition: FolderUtils.jsm:60
const Ci
Definition: FolderUtils.jsm:61
return null
Definition: FeedWriter.js:1143