[the-board] [firefox] Initial code for The Board's firefox extension
- From: Lucas Rocha <lucasr src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [the-board] [firefox] Initial code for The Board's firefox extension
- Date: Tue, 30 Nov 2010 18:26:43 +0000 (UTC)
commit d369e20c64c1a5e88e93d55d214e6683b875fe3a
Author: Lucas Rocha <lucasr gnome org>
Date: Mon Nov 29 16:15:38 2010 +0000
[firefox] Initial code for The Board's firefox extension
Allows adding note, labels, photos and videos from webpages.
src/firefox/chrome.manifest | 4 +
src/firefox/chrome/content/the-board.js | 282 ++++++++++++++++++++++++++++++
src/firefox/chrome/content/the-board.xul | 17 ++
src/firefox/install.rdf | 27 +++
4 files changed, 330 insertions(+), 0 deletions(-)
---
diff --git a/src/firefox/chrome.manifest b/src/firefox/chrome.manifest
new file mode 100644
index 0000000..8a59b53
--- /dev/null
+++ b/src/firefox/chrome.manifest
@@ -0,0 +1,4 @@
+content the-board chrome/content/
+
+# Firefox
+overlay chrome://browser/content/browser.xul chrome://the-board/content/the-board.xul
diff --git a/src/firefox/chrome/content/the-board.js b/src/firefox/chrome/content/the-board.js
new file mode 100644
index 0000000..8ba79ca
--- /dev/null
+++ b/src/firefox/chrome/content/the-board.js
@@ -0,0 +1,282 @@
+var _THE_BOARD_URL = "http://localhost:2010/";
+
+function callMethod(methodName, args, onLoad, onError) {
+ var req = new XMLHttpRequest();
+
+ var url = _THE_BOARD_URL + methodName;
+
+ if (args) {
+ url += "?";
+
+ var first = true;
+ for (argName in args) {
+ url += (first ? "" : "&") +
+ argName + "=" + args[argName];
+
+ first = false;
+ }
+ }
+
+ req.open("POST", encodeURI(url), true);
+
+ var onStateChange = function() {
+ if (req.readyState == 4 && req.status == 200) {
+ var json;
+
+ if (req.responseText) {
+ json = JSON.parse(req.responseText);
+ } else {
+ json = {};
+ }
+
+ onLoad(json);
+ }
+ }
+
+ var onErrorInternal = function() {
+ onError();
+ }
+
+ if (onLoad) {
+ req.onreadystatechange = onStateChange;
+ }
+
+ if (onError) {
+ req.onerror = onErrorInternal;
+ }
+
+ req.send(null);
+}
+
+function showNotification(title, description) {
+ try {
+ Cc['@mozilla.org/alerts-service;1'].
+ getService(Ci.nsIAlertsService).
+ showAlertNotification(null,
+ title,
+ description,
+ false, '', null);
+ } catch(e) {
+ // Prevents runtime error on platforms
+ // that don't implement nsIAlertsService
+ }
+}
+
+function showErrorTheBoardNotRunning() {
+ var title = 'The Board is not running';
+ var description = 'The Board must be running before ' +
+ 'you start adding content to it.';
+
+ showNotification(title, description);
+}
+
+function showErrorDownloadFailed() {
+ var title = 'Oops!';
+ var description = 'The Board could not download ' +
+ 'this file. Check your internet connection ' +
+ 'and try again?';
+
+ showNotification(title, description);
+}
+
+var userDirs = null;
+
+function downloadFile(fileUrl, dirType, onComplete, onError) {
+ if (!userDirs) {
+ callMethod("getUserDirs", null,
+ function(json) {
+ userDirs = json;
+ downloadFile(fileUrl, dirType, onComplete, onError);
+ },
+ showErrorTheBoardNotRunning);
+
+ return;
+ }
+
+ var dm = Cc["@mozilla.org/download-manager;1"].
+ getService(Ci.nsIDownloadManager);
+
+ var ioService = Cc["@mozilla.org/network/io-service;1"].
+ getService(Ci.nsIIOService);
+
+ var uri = ioService.newURI(fileUrl, null, null);
+ var url = uri.QueryInterface(Ci.nsIURL);
+
+ var msrv = Components.classes["@mozilla.org/mime;1"].
+ getService(Ci.nsIMIMEService);
+
+ var type = msrv.getTypeFromURI(uri);
+ var mime = msrv.getFromTypeAndExtension(type, "");
+
+ if (!(dirType in userDirs)) {
+ if (onError) {
+ onError();
+ }
+ return;
+ }
+
+ var targetDir = Cc["@mozilla.org/file/local;1"].
+ createInstance(Ci.nsILocalFile);
+
+ targetDir.initWithPath(userDirs[dirType]);
+
+ if (!targetDir.exists()) {
+ targetDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0755);
+ }
+
+ targetFile = targetDir.clone();
+ targetFile.append(url.fileName);
+
+ var index = 0;
+
+ while (targetFile.exists()) {
+ index++;
+
+ targetFile = targetDir.clone();
+
+ targetFile.append(url.fileBaseName +
+ " (" + index + ")." +
+ url.fileExtension);
+ }
+
+ var fileUri = ioService.newFileURI(targetFile);
+
+ var persist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"].
+ createInstance(Ci.nsIWebBrowserPersist);
+
+ persist.persistFlags =
+ Ci.nsIWebBrowserPersist.PERSIST_FLAGS_BYPASS_CACHE |
+ Ci.nsIWebBrowserPersist.PERSIST_FLAGS_CLEANUP_ON_FAILURE |
+ Ci.nsIWebBrowserPersist.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION;
+
+ var dl = dm.addDownload(dm.DOWNLOAD_TYPE_DOWNLOAD,
+ uri, fileUri,
+ "The Board (" + url.fileName + ")",
+ mime,
+ Math.round(Date.now() * 1000),
+ null,
+ persist);
+
+ let listener = {
+ onStateChange : function() {},
+ onProgressChange : function() {},
+ onSecurityChange : function() {},
+
+ onDownloadStateChange : function(state, aDownload) {
+ if (aDownload != dl) {
+ return;
+ }
+
+ switch (aDownload.state) {
+ case Ci.nsIDownloadManager.DOWNLOAD_FINISHED:
+ if (onComplete) {
+ onComplete(targetFile.path, targetFile.leafName);
+ }
+ break;
+ case Ci.nsIDownloadManager.DOWNLOAD_FAILED:
+ case Ci.nsIDownloadManager.DOWNLOAD_CANCELED:
+ if (onError) {
+ onError();
+ }
+ break;
+ default:
+ // do nothing
+ break;
+ }
+ }
+ };
+
+ dm.addListener(listener);
+
+ persist.progressListener =
+ dl.QueryInterface(Ci.nsIWebProgressListener);
+
+ persist.saveURI(dl.source, null, null, null, null, dl.targetFile);
+
+ return targetFile.path;
+}
+
+function addPhotoToTheBoard(imageUrl) {
+ var args = { id: "photo" };
+
+ var onFileDownloaded = function(filePath, filename) {
+ args.imageFilename = filePath;
+ args.text = filename;
+
+ callMethod("addThing", args,
+ null, showErrorTheBoardNotRunning);
+ };
+
+ downloadFile(imageUrl, 'pictures',
+ onFileDownloaded,
+ showErrorDownloadFailed);
+}
+
+function addVideoToTheBoard(videoUrl) {
+ var args = { id: "video" };
+
+ var onFileDownloaded = function(filePath, filename) {
+ args.videoFilename = filePath;
+ args.text = filename;
+
+ callMethod("addThing", args,
+ null, showErrorTheBoardNotRunning);
+ };
+
+ downloadFile(videoUrl, 'videos',
+ onFileDownloaded,
+ showErrorDownloadFailed);
+}
+
+function addNoteToTheBoard(text) {
+ var args = { id: "note",
+ text: text };
+
+ callMethod("addThing", args,
+ null, showErrorTheBoardNotRunning);
+}
+
+function addLabelToTheBoard(text) {
+ var args = { id: "label",
+ text: text };
+
+ callMethod("addThing", args,
+ null, showErrorTheBoardNotRunning);
+}
+
+function jsdump(str) {
+ Cc['@mozilla.org/consoleservice;1']
+ .getService(Ci.nsIConsoleService)
+ .logStringMessage(str);
+}
+
+function showHideMenuItem() {
+ var menuItem = document.getElementById("add-to-the-board");
+
+ menuItem.hidden = !gContextMenu.isTextSelected &&
+ !gContextMenu.onImage &&
+ !gContextMenu.onVideo &&
+ !gContextMenu.onLink;
+}
+
+function onMenuClick(info, tab) {
+ if (gContextMenu.onImage) {
+ addPhotoToTheBoard(document.popupNode.src);
+ } else if (gContextMenu.onVideo) {
+ addVideoToTheBoard(document.popupNode.currentSrc);
+ } else if (gContextMenu.onLink) {
+ addLabelToTheBoard(gContextMenu.linkURL);
+ } else if (gContextMenu.isTextSelected) {
+ var focusedWindow = document.commandDispatcher.focusedWindow;
+ var selection = focusedWindow.getSelection().toString();
+
+ addNoteToTheBoard(selection);
+ }
+}
+
+function init() {
+ var menu = document.getElementById("contentAreaContextMenu");
+ menu.addEventListener("popupshowing", showHideMenuItem, false);
+}
+
+window.addEventListener("load", init, false);
diff --git a/src/firefox/chrome/content/the-board.xul b/src/firefox/chrome/content/the-board.xul
new file mode 100644
index 0000000..41d5657
--- /dev/null
+++ b/src/firefox/chrome/content/the-board.xul
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!DOCTYPE overlay >
+<overlay id="the-board-overlay"
+ xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
+
+<script type="application/x-javascript"
+ src="chrome://the-board/content/the-board.js"/>
+
+<menupopup id="contentAreaContextMenu">
+ <menuseparator/>
+ <menuitem id="add-to-the-board"
+ label="Add to The Board"
+ oncommand="onMenuClick();" />
+</menupopup>
+
+</overlay>
diff --git a/src/firefox/install.rdf b/src/firefox/install.rdf
new file mode 100644
index 0000000..fe15b4c
--- /dev/null
+++ b/src/firefox/install.rdf
@@ -0,0 +1,27 @@
+<?xml version="1.0"?>
+
+<RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+ xmlns:em="http://www.mozilla.org/2004/em-rdf#">
+
+ <Description about="urn:mozilla:install-manifest">
+
+ <em:id>the-board-project gnome org</em:id>
+ <em:version>1.0</em:version>
+ <em:type>2</em:type>
+
+ <em:name>The Board</em:name>
+ <em:description>Allows you to add web content to The Board.</em:description>
+ <em:creator>Lucas Rocha</em:creator>
+ <em:homepageURL>http://live.gnome.org/TheBoardProject</em:homepageURL>
+
+ <em:targetApplication>
+ <Description>
+ <em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id>
+ <em:minVersion>3.1.*</em:minVersion>
+ <em:maxVersion>3.6.*</em:maxVersion>
+ </Description>
+ </em:targetApplication>
+
+ </Description>
+
+</RDF>
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]