sbM3UPlaylistWriter.js
Go to the documentation of this file.
1 /*
2  *=BEGIN SONGBIRD GPL
3  *
4  * This file is part of the Songbird web player.
5  *
6  * Copyright(c) 2005-2009 POTI, Inc.
7  * http://www.songbirdnest.com
8  *
9  * This file may be licensed under the terms of of the
10  * GNU General Public License Version 2 (the ``GPL'').
11  *
12  * Software distributed under the License is distributed
13  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
14  * express or implied. See the GPL for the specific language
15  * governing rights and limitations.
16  *
17  * You should have received a copy of the GPL along with this
18  * program. If not, go to http://www.gnu.org/licenses/gpl.html
19  * or write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  *=END SONGBIRD GPL
23  */
24 
28 const Cc = Components.classes;
29 const Ci = Components.interfaces;
30 const Cr = Components.results;
31 const Cu = Components.utils;
32 
33 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
34 Cu.import("resource://app/jsmodules/ArrayConverter.jsm");
35 Cu.import("resource://app/jsmodules/PlatformUtils.jsm");
36 
37 var PR_WRONLY = 0x02;
38 var PR_CREATE_FILE = 0x08;
39 var PR_TRUNCATE = 0x20;
41 
42 function sbM3UPlaylistWriter() {
43 }
44 
45 sbM3UPlaylistWriter.prototype = {
46  classDescription: "M3U Playlist Writer",
47  classID: Components.ID("{a11690c2-1dd1-11b2-82ba-8a4e72869f7a}"),
48  contractID: "@songbirdnest.com/Songbird/PlaylistWriter/M3U;1",
49  QueryInterface: XPCOMUtils.generateQI([Ci.sbIPlaylistWriter]),
50  _xpcom_categories: [{category: "playlist-writer", entry: "m3u"}],
51 
52  write: function(aFile, aMediaList, aContentType, aPlaylistFormatType) {
53  var foStream = Cc["@mozilla.org/network/file-output-stream;1"]
54  .createInstance(Ci.nsIFileOutputStream);
55 
56  // write, create, truncate
57  foStream.init(aFile,
58  PR_WRONLY| PR_CREATE_FILE | PR_TRUNCATE,
59  DEFAULT_PERMISSIONS,
60  0);
61 
62  var buildM3U = {
63  items: [],
64  onEnumerationBegin: function(aMediaList) {},
65  onEnumeratedItem: function(aMediaList, aMediaItem) {
66  var uri = aMediaItem.contentSrc;
67  // We don't allow non-file URIs to be written. At the moment.
68  if (!uri || uri.scheme != "file") { return; }
69 
70  try {
71  var f = uri.QueryInterface(Ci.nsIFileURL).file;
72  f.QueryInterface(Ci.nsILocalFile);
73 
74  // For Windows, since we store the content source in lowercase,
75  // get the case-sensitive path.
76  var platform = PlatformUtils.platformString;
77  if (platform == "Windows_NT") {
78  var fileUtils = Cc["@songbirdnest.com/Songbird/FileUtils;1"]
79  .getService(Ci.sbIFileUtils);
80  var exactPath = fileUtils.getExactPath(f.path);
81  if (exactPath) {
82  f.initWithPath(exactPath);
83  }
84  }
85 
86  // Show the directory containing the file and select the file.
87  // The returned ACString is converted to UTF-8 encoding in the
88  // getRelativeDescriptor method of nsLocalFileCommon.cpp*. Though
89  // m3u files have not historically used UTF-8 (hence m3u8), encoding
90  // in UTF-8 has worked properly thus far.
91  //
92  // * note: While the IDL explicitly states that the charset is
93  // undefined, the descriptor's charset will be UTF-8.
94  //
95  var data = f.getRelativeDescriptor(aFile.parent) + "\n";
96 
97  // Some devices are picky about path separators. If the playlist
98  // format type specifies a path separator, ensure we are using
99  // that separator.
100  if (aPlaylistFormatType && aPlaylistFormatType.pathSeparator) {
101  data = data.replace(/\//g, aPlaylistFormatType.pathSeparator);
102  }
103  foStream.write(data, data.length);
104  }
105  catch (e) {
106  Cu.reportError("Error writing M3U: " + e);
107  }
108  },
109  onEnumerationEnd: function(aMediaList, aResultCode) {}
110  };
111 
112  aMediaList.enumerateAllItems(buildM3U);
113  foStream.close();
114  buildM3U = null;
115  },
116 
117  name: function() {
118  return "Songbird M3U Writer";
119  },
120 
121  description: function() {
122  return "Write M3U playlists";
123  },
124 
125  supportedMIMETypes: function(aMIMECount) {
126  var mimeTypes = ["audio/mpegurl", "audio/x-mpegurl"];
127  aMIMECount.value = mimeTypes.length;
128  return mimeTypes;
129  },
130 
131  supportedFileExtensions: function(aExtCount) {
132  var exts = ["m3u", "m3u8"];
133  aExtCount.value = exts.length;
134  return exts;
135  },
136 };
137 
138 function NSGetModule(compMgr, fileSpec) {
139  return XPCOMUtils.generateModule([sbM3UPlaylistWriter]);
140 }
classDescription entry
Definition: FeedWriter.js:1427
var PR_CREATE_FILE
sbDeviceFirmwareAutoCheckForUpdate prototype contractID
sbOSDControlService prototype QueryInterface
sbDeviceFirmwareAutoCheckForUpdate prototype classDescription
const Cc
function NSGetModule(compMgr, fileSpec)
var PR_TRUNCATE
return null
Definition: FeedWriter.js:1143
function sbM3UPlaylistWriter()
var uri
Definition: FeedWriter.js:1135
var PR_WRONLY
sbDeviceFirmwareAutoCheckForUpdate prototype classID
var DEFAULT_PERMISSIONS
sbWindowsAutoPlayServiceCfg _xpcom_categories
const Cu
observe data
Definition: FeedWriter.js:1329
const Cr
const Ci