home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 2008 September
/
PCWorld_2008-09_cd.bin
/
komunikace
/
kmeleon
/
K-Meleon1.1.3en-US.exe
/
chrome
/
newsfox.jar
/
content
/
newsfox
/
opml.js
< prev
next >
Wrap
Text File
|
2007-09-29
|
16KB
|
484 lines
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is NewsFox.
*
* The Initial Developer of the Original Code is
* Andy Frank <andy@andyfrank.com>.
* Portions created by the Initial Developer are Copyright (C) 2005-2007
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Andrey Gromyko <andrey@gromyko.name>
* Ron Pruitt <wa84it@gmail.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the LGPL or the GPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
const BACKUPS = "backups";
const NUMBACKUP = 5;
var numNewFeed = 0;
var fromBackup = true;
var opmlStack = new Array();
var stackIndex = 0;
////////////////////////////////////////////////////////////////
// Export OPML
////////////////////////////////////////////////////////////////
function exportOpml()
{
var picker = Components.classes["@mozilla.org/filepicker;1"].
createInstance(Components.interfaces.nsIFilePicker);
picker.init(window, "Export OPML", picker.modeSave);
picker.appendFilter("OPML Files (*.xml, *.opml)", "*.xml;*.opml");
picker.appendFilters(picker.filterAll);
picker.defaultString = "newsfox.opml";
var result = picker.show();
if(result == picker.returnOK || result == picker.returnReplace)
{
var file = picker.file;
if(file.exists()) file.remove(true);
file.create(file.NORMAL_FILE_TYPE, 0666);
expOpml(file,true);
}
}
function expOpml(file,report)
{
try
{
var srcTemplate = '<?xml version="1.0" encoding="UTF-8"?>';
srcTemplate += "<opml version=\"1.0\">\n";
srcTemplate += "<head>\n\t<title>NewsFox OPML Export</title>\n";
var currentDate = new Date();
srcTemplate += "\t<dateModified>" + currentDate.toString() + "</dateModified>\n";
srcTemplate += "</head>\n";
srcTemplate += "<body/>\n</opml>";
var opmlDoc = new DOMParser().parseFromString(srcTemplate, "text/xml");
var opmlBody = opmlDoc.getElementsByTagName("body")[0];
var groupNode, feedNode, feed;
for (var i=0; i<gFdGroup.length; i++)
{
groupNode = opmlDoc.createElement("outline");
groupNode.setAttribute("text",gFdGroup[i].title);
if (gFdGroup[i].search)
{
var srchdat = gFdGroup[i].srchdat;
groupNode.setAttribute("type","NFsearch");
groupNode.setAttribute("NFflagged",srchdat.flagged);
groupNode.setAttribute("NFunread",srchdat.unread);
groupNode.setAttribute("NFtext",srchdat.text);
groupNode.setAttribute("NFtextflags",srchdat.textflags);
groupNode.setAttribute("NFstartTime",srchdat.startTime);
groupNode.setAttribute("NFendTime",srchdat.endTime);
}
else
groupNode.setAttribute("type","NFgroup");
opmlBody.appendChild(groupNode);
for (var j=0; j<gFdGroup[i].list.length; j++)
{
feed = gFmodel.get(gFdGroup[i].list[j]);
feedNode = opmlDoc.createElement("outline");
feedNode.setAttribute("type","rss");
feedNode.setAttribute("text",feed.getDisplayName());
feedNode.setAttribute("xmlUrl",feed.url);
feedNode.setAttribute("htmlUrl",feed.homepage);
feedNode.setAttribute("NFuid",feed.uid);
feedNode.setAttribute("NFdeleteOld",feed.deleteOld);
feedNode.setAttribute("NFdeleteUnread",!feed.dontDeleteUnread);
feedNode.setAttribute("NFautoCheck",feed.autoCheck);
feedNode.setAttribute("NFstyle",feed.style);
groupNode.appendChild(feedNode);
}
}
var opmlSource = new XMLSerializer().serializeToString(opmlDoc);
var unicodeConverter = Components.classes['@mozilla.org/intl/scriptableunicodeconverter'].getService(Components.interfaces.nsIScriptableUnicodeConverter);
unicodeConverter.charset = "UTF-8";
opmlSource = unicodeConverter.ConvertFromUnicode(opmlSource);
var out = Components.classes['@mozilla.org/network/file-output-stream;1'].
createInstance(Components.interfaces.nsIFileOutputStream);
out.init(file, 2, 0x200, false); // open as "write only"
println(out, opmlSource);
out.close();
const NF_SB = document.getElementById("newsfox-string-bundle");
var rpt = NF_SB.getString('opmlexport');
if (report) alert(rpt);
}
catch (err)
{
if (report) alert("Export failed: " + err);
}
}
function backupOpml()
{
var now = new Date();
var nowFilename = getBackupFilename(now);
var file = getProfileDir();
file.append(BACKUPS);
if (!file.exists()) file.create(file.DIRECTORY_TYPE, 0750);
file.append(nowFilename);
if(file.exists())
return;
else
{
var backupDir = getProfileDir();
backupDir.append(BACKUPS);
var backupEnum = backupDir.directoryEntries;
var dirFiles = new Array();
var i = 0;
while (backupEnum.hasMoreElements())
dirFiles[i++] = backupEnum.getNext().QueryInterface(Components.interfaces.nsILocalFile);
for (i=0; i<dirFiles.length-(NUMBACKUP-1); i++)
dirFiles[i].remove(false);
file.create(file.NORMAL_FILE_TYPE, 0666);
expOpml(file,false);
}
}
function getBackupFilename(date)
{
var year = date.getFullYear();
var month = date.getMonth() + 1;
month = (month < 10 ? "0" : "") + month;
var date1 = date.getDate();
date1 = (date1 < 10 ? "0" : "") + date1;
var filename = "newsfox-"+year+"-"+month+"-"+date1+".opml";
return filename;
}
////////////////////////////////////////////////////////////////
// Import OPML
////////////////////////////////////////////////////////////////
function restoreBackup()
{
importOpml(true,true,false);
}
function importOpml(fromBackup,fromFile,removeDir)
{
if (removeDir)
{
for (var i=gFmodel.size()-1; i>=0; i--)
deleteFeedFromDisk(gFmodel.get(i));
makeNewModels();
var feedtree = document.getElementById("newsfox.feedTree");
feedtree.view = new FeedTreeModel();
}
if (!fromFile) // from URL
{
const NF_SB = document.getElementById("newsfox-string-bundle");
var prmpt = NF_SB.getString('opmlhttpimportprompt');
var url = window.prompt(prmpt,"http://","Import OPML");
if (url) impOpml(url,fromBackup);
return;
}
var picker = Components.classes["@mozilla.org/filepicker;1"].
createInstance(Components.interfaces.nsIFilePicker);
if (fromBackup)
{
var backupDir = getProfileDir();
backupDir.append(BACKUPS);
picker.displayDirectory = backupDir;
}
picker.init(window, "Import OPML", picker.modeOpen);
picker.appendFilter("OPML Files (*.xml, *.opml)", "*.xml;*.opml");
picker.appendFilters(picker.filterAll);
if(picker.show() == picker.returnOK)
{
var nsIPH = Components.classes["@mozilla.org/network/protocol;1?name=file"].createInstance(Components.interfaces.nsIFileProtocolHandler);
var url = nsIPH.getURLSpecFromFile(picker.file);
impOpml(url,fromBackup);
}
}
function makeNewModels()
{
var arttree = document.getElementById("newsfox.articleTree");
gCollect = new EmptyCollection();
arttree.view = null;
arttree.view = new ArticleTreeModel();
var file = getProfileDir();
file.append("master.xml");
if (file.exists()) file.remove(false);
file = getProfileDir();
file.append("master_index.xml");
if (file.exists()) file.remove(false);
file = getProfileDir();
file.append("master_group.xml");
if (file.exists()) file.remove(false);
gFmodel = new FdModel();
gFdGroup = new Array();
gIdx = new Indexes();
loadModels();
}
function impOpml(url,fromBackup)
{
try
{
loadingTooltip(true);
if (fromBackup) makeNewModels();
var fix = Components.classes['@mozilla.org/docshell/urifixup;1'].getService(Components.interfaces.nsIURIFixup);
url = fix.createFixupURI(url, fix.FIXUP_FLAG_ALLOW_KEYWORD_LOOKUP);
// Locks up browser - not sure why - same code as file.js
//var xml = document.implementation.createDocument("","",null);
//xml.async = false;
//xml.load(url.spec);
var req = new XMLHttpRequest();
req.open("GET", url.spec, false);
req.overrideMimeType("application/xml");
req.send(null);
var xml = req.responseXML;
opmlStack = xml.getElementsByTagName("outline");
numNewFeed = 0;
stackIndex = 0;
setTimeout(addOpmlFeed,1); // adds new feeds and then groups
}
catch (err)
{
alert("Import failed: " + err);
}
}
function addOpmlFeed()
{
if (stackIndex == opmlStack.length)
{
addOpmlGroups();
return;
}
setPmeter((100*stackIndex)/opmlStack.length);
var kid = opmlStack[stackIndex];
var isNew, url, j, uid;
url = kid.getAttribute("xmlUrl");
if (url != null)
{
isNew = true;
for (j=0; j<gFmodel.size(); j++)
if (url == gFmodel.get(j).url)
{
isNew = false;
kid.setAttribute("newsfox",j);
}
if (isNew)
{
var feed = createNewFeed(gFmodel, url, false, false);
kid.setAttribute("newsfox",gFmodel.size()-1);
var uid = kid.getAttribute("NFuid");
var ktext = kid.getAttribute("text");
if (ktext != null) feed.customName = ktext;
var khome = kid.getAttribute("htmlUrl");
if (khome != null) feed.homepage = khome;
if (uid != null)
{
feed.deleteOld = false;
if (kid.getAttribute("NFdeleteOld") == "true")
feed.deleteOld = !feed.deleteOld;
feed.dontDeleteUnread = false;
if (kid.getAttribute("NFdeleteUnread") == "false")
feed.dontDeleteUnread = !feed.dontDeleteUnread;
feed.autoCheck = false;
if (kid.getAttribute("NFautoCheck") == "true")
feed.autoCheck = !feed.autoCheck;
feed.style = kid.getAttribute("NFstyle");
}
if (fromBackup && uid != null)
{
// has to be unique if from an *unmodified* Newsfox file
var uniq = true;
for (j=0; j<gFmodel.size(); j++)
if (uid == gFmodel.get(j).uid) uniq = false;
if (uniq) feed.uid = feed.defaultName = uid;
}
numNewFeed++;
}
}
stackIndex++;
setTimeout(addOpmlFeed,1);
}
function addOpmlGroups()
{
try
{
var grpKids, feedNumber, grp, num;
var numNewGrp = 0;
for (i=0; i<opmlStack.length; i++)
{
var grptype = opmlStack[i].getAttribute("type");
if (grptype == "rss") continue;
grp = new FeedGroup();
if (grptype == "NFgroup")
grp.search = false;
else if (grptype == "NFsearch")
{
grp.search = true;
grp.srchdat.flagged = opmlStack[i].getAttribute("NFflagged");
grp.srchdat.unread = opmlStack[i].getAttribute("NFunread");
grp.srchdat.text = opmlStack[i].getAttribute("NFtext");
grp.srchdat.textflags = opmlStack[i].getAttribute("NFtextflags");
grp.srchdat.startTime = opmlStack[i].getAttribute("NFstartTime");
grp.srchdat.endTime = opmlStack[i].getAttribute("NFendTime");
}
grp.title = opmlStack[i].getAttribute("text");
grpKids = opmlStack[i].getElementsByTagName("outline");
for (j=0; j<grpKids.length; j++)
{
feedNumber = grpKids[j].getAttribute("newsfox");
if (feedNumber == null) continue;
grp.list.push(feedNumber);
}
isNew = true;
if (!grp.search)
{
for (j=0; j<gFdGroup.length; j++)
if (grp.title == gFdGroup[j].title)
if (grp.list.length == gFdGroup[j].list.length)
{
isNew = false;
for (var k=0; k<grp.list.length; k++)
if (grp.list[k] != gFdGroup[j].list[k]) isNew = true;
}
}
if (isNew)
{
num = gFdGroup.length;
gFdGroup.push(grp);
gIdx.fdgp.push(num);
gIdx.feed.push(-1);
gIdx.catg.push(0);
gIdx.open.push(false);
numNewGrp++;
}
}
var feedtree = document.getElementById("newsfox.feedTree");
var fRow = feedtree.treeBoxObject.getFirstVisibleRow();
feedtree.view = new FeedTreeModel();
if (fromBackup)
{
feedtree.view.selection.select(0);
markFlaggedUnread(false,false);
}
else
{
feedtree.view.selection.select(-1);
feedtree.treeBoxObject.scrollToRow(fRow);
}
saveModels();
setPmeter(0);
loadingTooltip(false);
const NF_SB = document.getElementById("newsfox-string-bundle");
if (opmlStack.length == 0)
{
var rptfail = NF_SB.getString('opmlfail');
alert(rptfail);
}
else
{
var rpt = NF_SB.getString('opmlimport');
var rptnfeed = NF_SB.getString('opmlnewfeeds');
var rptngroup = NF_SB.getString('opmlnewgroups');
alert(rpt + "\n\n" + numNewFeed + " " + rptnfeed + "\n" + numNewGrp + " " + rptngroup);
}
}
catch (err)
{
alert("Import failed: " + err);
}
}
////////////////////////////////////////////////////////////////
// Export RSS
////////////////////////////////////////////////////////////////
function exportRSS()
{
var picker = Components.classes["@mozilla.org/filepicker;1"].
createInstance(Components.interfaces.nsIFilePicker);
picker.init(window, "Export to RSS", picker.modeSave);
picker.appendFilter("XML Files (*.xml)", "*.xml");
picker.appendFilters(picker.filterAll);
picker.defaultString = "rss.xml";
var result = picker.show();
if(result == picker.returnOK || result == picker.returnReplace)
{
var file = picker.file;
if(file.exists()) file.remove(true);
file.create(file.NORMAL_FILE_TYPE, 0666);
var out = openOutputStream(file, 0x02 | 0x08 | 0x20);
println(out, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
println(out, "<rss version=\"2.0\">\n\t<channel>");
var title = "<![CDATA[Newsfox: " + toUTF8(gCollect.getTitle()) + "]]>";
println(out, "\t\t<title>" + title + "</title>");
println(out, "\t\t<link>file:///" + file.path.replace(/\\/g,"\/") + "</link>");
println(out, "\t\t<description>" + title + "</description>");
var now = new Date().toUTCString();
println(out, "\t\t<lastBuildDate>" + now + "</lastBuildDate>");
println(out, "\t\t<generator>Newsfox " + VERSION + "</generator>");
for (var i=0; i<gCollect.size(); i++)
{
var art = gCollect.get(i);
println(out, "\t\t<item>");
println(out, "\t\t\t<title><![CDATA[" + toUTF8(art.title) + "]]></title>");
println(out, "\t\t\t<link><![CDATA[" + art.link + "]]></link>");
if (art.id)
println(out, "\t\t\t<guid><![CDATA[" + art.id + "]]></guid>");
println(out, "\t\t\t<pubDate>" + art.date.toUTCString() + "</pubDate>");
if (art.category != "")
println(out, "\t\t\t<category><![CDATA[" + toUTF8(art.category) + "]]></category>");
var feed = gCollect.getFeed(i);
if (feed.url.substring(0,5) == "http:")
println(out, "\t\t\t<source url=\"" + encodeHTML(feed.url) + "\"><![CDATA[" + toUTF8(feed.getDisplayName()) + "]]></source>");
println(out, "\t\t\t<description><![CDATA[" + toUTF8(art.body) + "]]></description>");
for (var j=0; j<art.enclosures.length; j++)
{
var enc = art.enclosures[j];
println(out, "\t\t\t<enclosure url=\"" + encodeHTML(enc.url) + "\" type=\"" + encodeHTML(enc.type) + "\" length=\"" + encodeHTML(enc.length) + "\"/>");
}
println(out, "\t\t</item>");
}
println(out, "\t</channel>\n</rss>");
out.close();
}
}