home *** CD-ROM | disk | FTP | other *** search
/ Freelog 100 / FreelogNo100-NovembreDecembre2010.iso / Multimedia / Songbird / Songbird_1.8.0-1800_windows-i686-msvc8.exe / jsmodules / FolderUtils.jsm < prev    next >
Text File  |  2010-08-30  |  6KB  |  199 lines

  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.  
  29. /**
  30.  * \file  FolderUtils.jsm
  31.  * \brief Javascript source for the folder utility services.
  32.  */
  33.  
  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.       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 downloadFolder() {
  181.     var downloadFolder = this.musicFolder;
  182.     
  183.     if(downloadFolder == null) {
  184.       var directorySvc = Cc["@mozilla.org/file/directory_service;1"]
  185.                            .getService(Ci.nsIProperties);
  186.       
  187.       // If the |musicFolder| is not available, default to the Desktop:
  188.       try {
  189.         downloadFolder = directorySvc.get("Desk", Ci.nsIFile);
  190.       }
  191.       catch(e) {
  192.         downloadFolder = null;
  193.       }
  194.     }
  195.     
  196.     return downloadFolder;
  197.   }
  198. };
  199.