Created
February 19, 2009 15:25
-
-
Save edgargoncalves/66959 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Source code for Buxfer transaction posting with Ubiquity commands. | |
// By Edgar Gonçalves. | |
// Version 0.5 | |
// Sun Jun 28 2009. | |
// Changes in 0.5: Parser2-ready, now. | |
// Roles, different syntax, selection is supported. | |
// Multiple suggestions bug fixed. | |
// Changes in 0.4: Redone command syntax. Now: "BUXFER-SPEND 43.2 IN something". Removed twixfer. | |
// Changes in 0.3: internal fixes, global variables unified into a single one, to avoid name clashes. | |
var BUXFERUBIQUITY = { | |
////////////////////////////// | |
//Public Variables: | |
////////////////////////////// | |
buxferUrl: "www.buxfer.com", | |
buxferAPIUrl: "https://www.buxfer.com/api/", | |
currencyRegexp: /^\d+([.,]?\d\d?)$/, | |
////////////////////////////// | |
// Public Methods: | |
////////////////////////////// | |
// gets username and password for a given domain, from Firefox's login-manager. | |
getLoginInfo: function (domain, fuzzySearch) { | |
var loginManager = Components.classes["@mozilla.org/login-manager;1"] | |
.getService(Components.interfaces.nsILoginManager); | |
var domains = []; | |
if(fuzzySearch) { | |
domains.push("http://" + domain); | |
domains.push("http://www." + domain); | |
domains.push("https://" + domain); | |
domains.push("https://www." + domain); | |
} else { | |
domains = [domain]; | |
} | |
for(var i = 0; i < domains.length; i++) { | |
for(var j = 0; j < domains.length; j++) { | |
var logins = loginManager.findLogins({}, domains[i], domains[j], null); | |
if(logins.length > 0) | |
return {username: logins[0].username, | |
password: logins[0].password}; | |
} | |
} | |
return null; | |
}, | |
// Function called to log in the user in buxfer. | |
// Receives the function (with a token and an uid) to be performed after the login is done. | |
loginUser: function (successCallback) { | |
if ( !(successCallback)) { | |
successCallback = function(token, uid){}; | |
} | |
var username, password; | |
var loginInfo = this.getLoginInfo(this.buxferUrl, true); | |
if (loginInfo) { | |
username = loginInfo.username; | |
password = loginInfo.password; | |
} else { | |
// We have no login stored, send the user to the Buxfer and tell him/her to login | |
displayMessage(_("You have to login to Buxfer before using buxfer-spend.")); | |
Utils.openUrlInBrowser(this.buxferUrl); | |
return; | |
} | |
var onloadHandler = function(data, status) { | |
if(status !== "success") { | |
displayMessage(_("Error logging in: ") + status); | |
return; | |
} | |
var jsonResponse = Utils.decodeJson(data).response; | |
if(jsonResponse.status === "OK"){ | |
successCallback(jsonResponse.token, jsonResponse.uid); | |
}else{ | |
displayMessage(_("Error logging in: ") + jsonResponse.status); | |
} | |
}; | |
var loginurl = this.buxferAPIUrl + | |
"login.json?userid=" + | |
encodeURIComponent(username) + | |
"&password=" + encodeURIComponent(password); | |
jQuery.get( loginurl, onloadHandler); | |
}, | |
// adds a buxfer transaction: | |
addTransaction: function (description, amount) { | |
var that = this; | |
this.loginUser( function(token, uid) { | |
var transactionUrl = that.buxferAPIUrl + "add_transaction.json"; | |
var onloadHandler = function(data, status) { | |
if(status !== "success"){ | |
displayMessage(_("Error logging adding it: ") + status); | |
return; | |
} | |
var jsonResponse = Utils.decodeJson(data).response; | |
if(jsonResponse.status !== "OK"){ | |
displayMessage(_("Error adding transaction : ")+jsonResponse.status); | |
} | |
if (!jsonResponse.transactionAdded){ | |
displayMessage(_("Transaction wasn't added...")); | |
return; | |
} | |
if (jsonResponse.parseStatus != "success"){ | |
displayMessage(_("Transaction wasn't parsed correctly...")); | |
return; | |
} | |
displayMessage(_("Buxfer successfully added your transaction")); | |
}; | |
var text = description + " " + amount; | |
var params = "token="+token+"&format=sms&text="+encodeURIComponent(text); | |
jQuery.ajax( { | |
type: "POST", | |
data: params, | |
dataType: "text", | |
url: transactionUrl, | |
cache: false, | |
success: onloadHandler, | |
error: function (XMLHttpRequest, textStatus, errorThrown) { | |
displayMessage(_("Something very wrong happened: ") + textStatus + ", " + errorThrown);} | |
} | |
); | |
}); | |
}, | |
// preview function for Ubiquity: | |
previewBuxferEnteredText: function (pblock, args, verb) { | |
var msg = "Tell Buxfer to " + verb + " ${amount} EUR with ${description}."; | |
var data = { | |
amount: (args.object.text || "0").replace(",","."), | |
description: args.instrument.text || CmdUtils.getSelection() || _("Undescribed transaction") | |
}; | |
pblock.innerHTML = CmdUtils.renderTemplate( msg, data ); | |
}, | |
// execution function for Ubiquity: | |
executeBuxferAdd: function (args, verb) { | |
var amount = (args.object.text || "0").replace(",","."); | |
if (amount == 0) { | |
displayMessage(_("Amount shouln't be 0...")); | |
return; | |
} | |
var description = args.instrument.text || CmdUtils.getSelection() || _("Undescribed transaction"); | |
this.addTransaction(description, (verb == "spend"? "" : "+") + amount); | |
} | |
}; | |
CmdUtils.CreateCommand({ | |
names: ["spend", "buxfer-spend"], | |
homepage: "http://http://sites.google.com/site/edgargoncalves/work/software/ubiquity", | |
author: {name: "Edgar Gonçalves", email: "edgar.goncalves@gmail.com"}, | |
arguments: [ {role: 'object', nountype: BUXFERUBIQUITY.currencyRegexp, label: 'amount'}, | |
{role: 'instrument', nountype: noun_arb_text, label: 'something'}], | |
icon: "https://www.buxfer.com/media/favicon/favicon-moneybag.ico", | |
description:"Adds a transaction to <a href='http://www.buxfer.com'>Buxfer</a>, spending money with something.", | |
help:"Adds a <a href='http://www.buxfer.com'>Buxfer</a> transaction, spending money with something.<br/>" + | |
" Usage: "spend AMOUNT with SOMETHING"", | |
preview: function (pblock, args) { | |
BUXFERUBIQUITY.previewBuxferEnteredText(pblock, args, "spend"); | |
}, | |
execute: function(args) { | |
BUXFERUBIQUITY.executeBuxferAdd(args, "spend"); | |
} | |
}); | |
CmdUtils.CreateCommand({ | |
names: ["receive", "buxfer-receive"], | |
homepage: "http://http://sites.google.com/site/edgargoncalves/work/software/ubiquity", | |
author: {name: "Edgar Gonçalves", email: "edgar.goncalves@gmail.com"}, | |
arguments: [ {role: 'object', nountype: BUXFERUBIQUITY.currencyRegexp, label: 'amount'}, | |
{role: 'instrument', nountype: noun_arb_text, label: 'something'} ], | |
icon: "https://www.buxfer.com/media/favicon/favicon-moneybag.ico", | |
description:"Adds a transaction to <a href='http://www.buxfer.com'>Buxfer</a>, receiving money for something.", | |
help:"Adds a <a href='http://www.buxfer.com'>Buxfer</a> transaction, receiving money for something.<br/>" + | |
" Usage: "spend AMOUNT with SOMETHING"", | |
preview: function (pblock, args) { | |
BUXFERUBIQUITY.previewBuxferEnteredText(pblock, args, "receive"); | |
}, | |
execute: function(args) { | |
BUXFERUBIQUITY.executeBuxferAdd(args, "receive"); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment