1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-22 00:17:58 +01:00
Files
pentadactyl-pm/chrome/content/vimperator/bookmarks.js
2007-05-04 18:04:11 +00:00

244 lines
7.9 KiB
JavaScript

/*
* 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);
res.tags is an array of tags
res.title is the title or "" if no one was given
res.url is the url as a string
returns false, if parsing failed
*/
function parseBookmarkString(str, res)
{
res.tags = [];
res.title = null;
res.url = null;
var re_title = /^\s*((-t|--title)\s+(\w+|\".*\"))(.*)/;
var re_tags = /^\s*((-T|--tags)\s+((\w+)(,\w+)*))(.*)/;
var re_url = /^\s*(\".+\"|\S+)(.*)/;
var match_tags = null;
var match_title = null;
var match_url = null;
while(!str.match(/^\s*$/))
{
/* first check for --tags */
match_tags = str.match(re_tags);
if(match_tags != null)
{
str = match_tags[match_tags.length-1]; // the last captured parenthesis is the rest of the string
tags = match_tags[3].split(",");
res.tags = res.tags.concat(tags);
}
else /* then for --titles */
{
match_title = str.match(re_title);
if(match_title != null)
{
// only one title allowed
if (res.title != null)
return false;
str = match_title[match_title.length-1]; // the last captured parenthesis is the rest of the string
title = match_title[3];
if(title.charAt(0) == '"')
title = title.substring(1,title.length-1);
res.title = title;
}
else /* at last check for an url */
{
match_url = str.match(re_url);
if (match_url != null)
{
// only one url allowed
if (res.url != null)
return false;
str = match_url[match_url.length-1]; // the last captured parenthesis is the rest of the string
url = match_url[1];
if(url.charAt(0) == '"')
url = url.substring(1,url.length-1);
res.url = url;
}
else return false; // no url, tag or title found but still text left, abort
}
}
}
return true;
}
function Bookmarks()
{
const search_service = Components.classes["@mozilla.org/browser/search-service;1"].
getService(Components.interfaces.nsIBrowserSearchService);
/* also ensures that each search engine has a vimperator-friendly alias */
this.getSearchEngines = function()
{
var search_engines = [];
var firefox_engines = search_service.getVisibleEngines({ });
for(var i in firefox_engines)
{
if (!firefox_engines[i].alias || !firefox_engines[i].alias.match(/^[a-z0-9_]+$/))
{
var alias = firefox_engines[i].name.replace(/^\W*(\w+).*/, "$1").toLowerCase();
firefox_engines[i].alias = alias;
}
search_engines.push([firefox_engines[i].alias, firefox_engines[i].description]);
}
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;
}
// 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
this.getSearchURL = function(text, engine_name)
{
var url = null;
if(!engine_name || engine_name == "")
engine_name = get_pref("defsearch", "google");
// first checks the search engines for a match
var engine = search_service.getEngineByAlias(engine_name);
if(engine)
{
if(text)
url = engine.getSubmission(text, null).uri.spec;
else
url = engine.searchForm;
}
else // check for keyword urls
{
for (var i in g_keywords)
{
if(g_keywords[i][0] == engine_name)
{
if (text == null)
text = "";
url = g_keywords[i][2].replace(/%s/g, encodeURIComponent(text));
break;
}
}
}
// if we came here, the engine_name is neither
return url;
}
logMessage("Bookmarks initialized.");
}
var bookmarks = new Bookmarks(); // FIXME, must it really be here? doesn't work in vimperator.js
// vim: set fdm=marker sw=4 ts=4 et: