1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-30 06:32:27 +01:00

fully objectized History() and Bookmarks()

This commit is contained in:
Martin Stubenschrott
2007-05-18 15:36:17 +00:00
parent ac7c396023
commit 31a43298f6
5 changed files with 263 additions and 241 deletions

View File

@@ -1,84 +1,3 @@
/*
* low-level BOOKMARK and HISTORY handling
*
* these commands try to be generic and don't use any GUI handling code
* for higher-level functions look into commands.js
*/
function getProperty( aInput, aArc, DS )
{
var node;
node = DS.GetTarget( aInput, aArc, true );
if( node instanceof Components.interfaces.nsIRDFResource ) {
return node.Value;
}
if( node instanceof Components.interfaces.nsIRDFLiteral ) {
return node.Value;
}
return "";
}
function addBookmark(title, uri)
{
folder = RDF.GetResource("NC:BookmarksRoot");
var rSource = BookmarksUtils.createBookmark(title, uri, null, title);
var selection = BookmarksUtils.getSelectionFromResource(rSource);
var target = BookmarksUtils.getTargetFromFolder(folder);
BookmarksUtils.insertAndCheckSelection("newbookmark", selection, target);
//also update bookmark cache
g_bookmarks.unshift([uri, title]);
}
/* no idea what it does, it Just Works (TM)
*
* returns number of deleted bookmarks
*/
function deleteBookmark(url)
{
var deleted = 0;
// gNC_NS for trunk, NC_NS for 1.X
try {var pNC_NS; pNC_NS = gNC_NS;} catch (err) { pNC_NS = NC_NS;}
if(! BMSVC || ! BMDS || ! RDF || ! pNC_NS ) return null;
if ( !url) return null; // just in case
var curfolder = RDF.GetResource("NC:BookmarksRoot");
var urlArc = RDF.GetResource(pNC_NS+"URL");
var urlLiteral = RDF.GetLiteral(url);
if (BMDS.hasArcIn(urlLiteral, urlArc)) {
var bmResources, bmResource, title, uri, type, ptype;
bmResources = BMSVC.GetSources(urlArc, urlLiteral, true);
while (bmResources.hasMoreElements()) {
bmResource = bmResources.getNext();
type = BookmarksUtils.resolveType(bmResource);
if (type != "ImmutableBookmark") {
ptype = BookmarksUtils.resolveType(BMSVC.getParent(bmResource));
// alert(type);
// if ( type == "Folder") // store the current folder
// curfolder = bmResource;
if ( (type == "Bookmark" || type == "IEFavorite") && ptype != "Livemark") {
title = BookmarksUtils.getProperty(bmResource, pNC_NS+"Name");
uri = BookmarksUtils.getProperty(bmResource, pNC_NS+"URL");
if (uri == url)
{
RDFC.Init(BMDS, BMSVC.getParent(bmResource));
RDFC.RemoveElement(bmResource, true);
deleted++;
}
}
}
}
}
// also update bookmark cache, if we removed at least one bookmark
if(deleted > 0)
bookmarks_loaded = false;
return deleted;
}
/* call the function like this:
var res = new Object();
parseBookmarkString("-t tag1,tag2 -T title http://www.orf.at", res);
@@ -147,20 +66,140 @@ function parseBookmarkString(str, res)
}
}
}
return true;
}
/*
* also includes methods for dealing with
* keywords and search engines
*/
function Bookmarks()
{
////////////////////////////////////////////////////////////////////////////////
////////////////////// PRIVATE SECTION /////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
const search_service = Components.classes["@mozilla.org/browser/search-service;1"].
getService(Components.interfaces.nsIBrowserSearchService);
const rdf_service = Components.classes["@mozilla.org/rdf/rdf-service;1"].
getService( Components.interfaces.nsIRDFService );
var bookmarks = null;
var keywords = null;
if(get_pref("preload"))
setTimeout(function() { load(); } , 100);
function load()
{
// update our bookmark cache
var root = rdf_service.GetResource("NC:BookmarksRoot");
bookmarks = new Array(); // also clear our bookmark cache
keywords = new Array();
var bmarks = []; // here getAllChildren will store the bookmarks
BookmarksUtils.getAllChildren(root, bmarks);
for(var bm in bmarks)
{
if (bmarks[bm][0] && bmarks[bm][1])
bookmarks.push([bmarks[bm][1].Value, bmarks[bm][0].Value ]);
// keyword
if(bmarks[bm][1] && bmarks[bm][2])
keywords.push([bmarks[bm][2].Value, bmarks[bm][0].Value, bmarks[bm][1].Value]);
}
}
////////////////////////////////////////////////////////////////////////////////
////////////////////// PUBLIC SECTION //////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/*
* @return a new Array() of our bookmarks
*/
this.get = function()
{
if (!bookmarks)
load();
return bookmarks;
}
/**
* @TODO: keyword support
*/
this.add = function (title, uri, keyword)
{
if(!bookmarks)
load();
folder = rdf_service.GetResource("NC:BookmarksRoot");
var rSource = BookmarksUtils.createBookmark(title, uri, keyword, title);
var selection = BookmarksUtils.getSelectionFromResource(rSource);
var target = BookmarksUtils.getTargetFromFolder(folder);
BookmarksUtils.insertAndCheckSelection("newbookmark", selection, target);
//also update bookmark cache
bookmarks.unshift([uri, title]);
return true;
}
/* no idea what it does, it Just Works (TM)
*
* @returns number of deleted bookmarks
*/
this.remove = function(url)
{
var deleted = 0;
if(!url)
return 0;
// gNC_NS for trunk, NC_NS for 1.X
//try { var pNC_NS; pNC_NS = gNC_NS;} catch (err) { pNC_NS = NC_NS;}
if (!BMSVC || !BMDS || !RDF || !gNC_NS) // defined from firefox
return 0;
var curfolder = RDF.GetResource("NC:BookmarksRoot");
var urlArc = RDF.GetResource(gNC_NS + "URL");
var urlLiteral = RDF.GetLiteral(url);
if (BMDS.hasArcIn(urlLiteral, urlArc))
{
var bmResources, bmResource, title, uri, type, ptype;
bmResources = BMSVC.GetSources(urlArc, urlLiteral, true);
while (bmResources.hasMoreElements())
{
bmResource = bmResources.getNext();
type = BookmarksUtils.resolveType(bmResource);
if (type != "ImmutableBookmark") {
ptype = BookmarksUtils.resolveType(BMSVC.getParent(bmResource));
// alert(type);
// if ( type == "Folder") // store the current folder
// curfolder = bmResource;
if ( (type == "Bookmark" || type == "IEFavorite") && ptype != "Livemark")
{
title = BookmarksUtils.getProperty(bmResource, gNC_NS + "Name");
uri = BookmarksUtils.getProperty(bmResource, gNC_NS + "URL");
if (uri == url)
{
RDFC.Init(BMDS, BMSVC.getParent(bmResource));
RDFC.RemoveElement(bmResource, true);
deleted++;
}
}
}
}
}
// also update bookmark cache, if we removed at least one bookmark
if(deleted > 0)
load();
return deleted;
}
/* also ensures that each search engine has a vimperator-friendly alias */
this.getSearchEngines = function()
{
var search_engines = [];
var search_engines = new Array();
var firefox_engines = search_service.getVisibleEngines({ });
for(var i in firefox_engines)
{
@@ -175,35 +214,18 @@ function Bookmarks()
return search_engines;
}
// FIXME: for now g_keywords is generated by get_bookmarks_completion, WILL HAVE TO CHANGE
// format of returned array:
// [keyword, helptext, url]
this.getKeywords = function()
{
return g_keywords;
if(!keywords)
load();
return keywords;
}
// xxx: probably remove these functions
// this.getDefaultEngine = function()
// {
// return search_service.currentEngine;
// }
// this.setDefaultEngine = function(alias)
// {
// var engine = search_service.getEngineByAlias(alias);
// if(engine)
// search_service.currentEngine = engine;
// else
// echoerr("Error: Search engine with alias '" + alias + "' does not exist");
// }
// this.getEngine = function(alias)
// {
// var engine = search_service.getEngineByAlias(alias);
// return engine;
// }
// if the engine name is null, it uses the default search engine
// returns a url for the search string
// @returns the url for the search string
this.getSearchURL = function(text, engine_name)
{
var url = null;
@@ -221,27 +243,114 @@ function Bookmarks()
}
else // check for keyword urls
{
for (var i in g_keywords)
if(!keywords)
load();
for (var i in keywords)
{
if(g_keywords[i][0] == engine_name)
if(keywords[i][0] == engine_name)
{
if (text == null)
text = "";
url = g_keywords[i][2].replace(/%s/g, encodeURIComponent(text));
url = keywords[i][2].replace(/%s/g, encodeURIComponent(text));
break;
}
}
}
// if we came here, the engine_name is neither
// if we came here, the engine_name is neither a search engine or URL
return url;
}
logMessage("Bookmarks initialized.");
logMessage("Bookmarks initialized");
}
var bookmarks = new Bookmarks(); // FIXME, must it really be here? doesn't work in vimperator.js
function History()
{
const rdf_service = Components.classes["@mozilla.org/rdf/rdf-service;1"].
getService( Components.interfaces.nsIRDFService );
const global_history_service = Components.classes["@mozilla.org/browser/global-history;2"].
getService(Components.interfaces.nsIRDFDataSource);
var history = null;
if(get_pref("preload"))
setTimeout(function() { load(); } , 100);
function load()
{
history = new Array();
var historytree = document.getElementById("hiddenHistoryTree");
if (!historytree)
return;
if (historytree.hidden)
{
historytree.hidden = false;
historytree.database.AddDataSource(global_history_service);
}
if (!historytree.ref)
historytree.ref = "NC:HistoryRoot";
var nameResource = rdf_service.GetResource(gNC_NS + "Name");
var builder = historytree.builder.QueryInterface(Components.interfaces.nsIXULTreeBuilder);
var count = historytree.view.rowCount;
for (var i = count-1; i >= 0; i--)
{
var res = builder.getResourceAtIndex(i);
var url = res.Value;
var title;
var titleRes = historytree.database.GetTarget(res, nameResource, true);
if (!titleRes)
continue;
var titleLiteral = titleRes.QueryInterface(Components.interfaces.nsIRDFLiteral);
if(titleLiteral)
title = titleLiteral.Value;
else
title = "";
history.push([url, title]);
}
}
////////////////////////////////////////////////////////////////////////////////
////////////////////// PUBLIC SECTION //////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/*
* @return a new Array() of our bookmarks
*/
this.get = function()
{
if (!history)
load();
return history;
}
this.add = function (uri, title)
{
if(!history)
load();
// XXX: check if fast enough
history = history.filter(function(elem) {
return elem[0] != uri;
});
// for(var i in history)
// {
// if(g_history[i][0] == url)
// return;
// }
// g_history.unshift([url, title]);
history.unshift([uri, title]);
return true;
}
logMessage("History initialized");
}
Vimperator.prototype.quickmarks = new function()
{