auto_vcard/chrome/content/autovcard.js
2018-06-03 15:31:49 +02:00

249 lines
7.4 KiB
JavaScript

const Cc = Components.classes;
const Ci = Components.interfaces;
var autovcard = {
db_connection: null,
statement: null,
to: "",
to_row_id: 100,
result: false,
manually: false,
init : function(){
this.to = "";
this.to_row_id = 100;
this.result = false;
this.manually = false;
gMsgCompose.QueryInterface(Ci.nsIMsgCompose).addMsgSendListener(this.send_listener);
this.find_to();
this.db_init();
this.check();
},
nospace : function (str) {
var re = new RegExp(/^(\s|\u00A0)+/g);
return str.replace(re, '');
},
check: function(){
this.to = this.nospace(this.to);
if (this.to == ''){return};
this.statement.params.parameter = this.to;
this.result=true;
this.statement.executeAsync({
handleResult: function(aResultSet) {
var ok = false;
for (let row = aResultSet.getNextRow(); row; row = aResultSet.getNextRow()) {
let value = row.getResultByName("email");
if (value == autovcard.to){ok = true;break};
};
if(ok){
autovcard.manually = true;
autovcard.delete_vcard();
}else{
autovcard.manually = false;
autovcard.attach_vcard();
}
},
handleError: function(aError) {
},
handleCompletion: function(aReason) {
autovcard.result=false;
}
});
},
find_to: function(){
//
// alert('tt');
for (var i = 1; i < 100; i++) {
var totype = document.getElementById('addressCol1#'+i);
if(totype == null){
this.to_row_id = 100;
this.to = '';
//this.manually = false;
this.manually = false;
//
// alert('tt');
this.delete_vcard();
break;
};
if((totype.value =='addr_to') && (document.getElementById('addressCol2#'+i).value != '')){
if((this.to_row_id == i) && (this.to == this.nospace(document.getElementById('addressCol2#'+i).value))){
break;
}
this.to_row_id = i;
this.to = document.getElementById('addressCol2#'+i).value;
this.check();
break;
}
}
},
delete_vcard : function(){
if(this.manually){
document.getElementById("autovcard_button").setAttribute('label',document.getElementById("autovcard_button").getAttribute('label_attach'));
document.getElementById("autovcard_button").className =('toolbarbutton-1 autovcard_attach');
document.getElementById("autovcard_button").setAttribute('disabled','false');
}else{
document.getElementById("autovcard_button").setAttribute('disabled','true');
}
gMsgCompose.compFields.attachVCard = false;
document.getElementById("cmd_attachVCard").setAttribute('checked','false');
},
attach_vcard : function(){
document.getElementById("autovcard_button").setAttribute('label',document.getElementById("autovcard_button").getAttribute('label_delete'));
document.getElementById("autovcard_button").className =('toolbarbutton-1 autovcard_delete');
document.getElementById("autovcard_button").setAttribute('disabled','false');
gMsgCompose.compFields.attachVCard = true;
document.getElementById("cmd_attachVCard").setAttribute('checked','true');
},
totype_changed : function(e){
var id = e.target.parentNode.parentNode.id;
id = id.slice(id.lastIndexOf('#')+1);
if(e.target.value =='addr_to'){
if ((id < this.to_row_id) && (document.getElementById('addressCol2#'+id).value != '')){
this.to_row_id = id;
this.to = document.getElementById('addressCol2#'+id).value;
this.check();
}
}else{
if(id == this.to_row_id){
this.find_to();
}
}
},
to_changed : function(e){
var id = e.target.id;
id = id.slice(id.lastIndexOf('#')+1);
if(document.getElementById('addressCol1#'+id).value =='addr_to'){
if((id == this.to_row_id) && (e.target.value == '')){
this.find_to();
}else{
if((id <= this.to_row_id) && (e.target.value != '')){
this.to_row_id = id;
this.to = e.target.value;
this.check();
}
}
}
},
send_listener : {
onStopSending: function(i, s, m, r){
if(s == '0'){
autovcard.to = autovcard.nospace(autovcard.to);
var statement = autovcard.db_connection.createStatement("INSERT INTO email VALUES(:parameter)");
statement.params.parameter = autovcard.to;
try{
statement.execute();
} catch (e) {};
};
},
onSendNotPerformed: function(){},
onGetDraftFolderURI: function(){},
onProgress: function(){},
onStartSending: function(){},
onStatus: function(){}
},
db_init: function() {
var dirService = Cc["@mozilla.org/file/directory_service;1"].
getService(Ci.nsIProperties);
var dbFile = dirService.get("ProfD", Ci.nsIFile);
dbFile.append("autovcard.sqlite");
var dbService = Cc["@mozilla.org/storage/service;1"].
getService(Ci.mozIStorageService);
var dbConnection;
if (!dbFile.exists())
dbConnection = this.db_create(dbService, dbFile);
else {
dbConnection = dbService.openDatabase(dbFile);
}
this.db_connection = dbConnection;
this.statement = this.db_connection.createStatement("SELECT * FROM email WHERE email = :parameter UNION SELECT 'empty'");
},
db_create: function(aDBService, aDBFile) {
var dbConnection = aDBService.openDatabase(aDBFile);
dbConnection.createTable('email', "email TEXT PRIMARY KEY");
return dbConnection;
},
add_note: function(){
if(document.getElementById("cmd_attachVCard").getAttribute('checked') =='true'){
var comment;
if(this.manually){
comment = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefService).getBranch("autovcard.label.options.").getCharPref("comment_manually");
}else{
comment = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefService).getBranch("autovcard.label.options.").getCharPref("comment");
}
GetCurrentEditor().beginTransaction();
GetCurrentEditor().endOfDocument();
if (GetCurrentEditorType() == "textmail" || GetCurrentEditorType() == "text"){
GetCurrentEditor().insertText(comment);
}else{
GetCurrentEditor().insertHTML(comment.replace(/\n+/g,"<br>"));
}
GetCurrentEditor().endTransaction();
}
},
check_vcard: function(command,i){
if (!i){
this.find_to();
setTimeout(function(){autovcard.check_vcard(command,1);},100);
return;
};
if(this.result && i < 20){
setTimeout(function(){autovcard.check_vcard(command,i++);},100);
return;
};
if((document.getElementById("cmd_attachVCard").getAttribute('checked') =='true') && !gMsgCompose.identity.escapedVCard){
var params = {vcard:null, out:null};
window.openDialog("chrome://autovcard/content/dialog.xul", "",
"chrome, dialog, modal, resizable=yes,centerscreen", params).focus();
if (params.out == 'ok') {
gMsgCompose.identity.escapedVCard = params.vcard;
}else{
this.manually = true;
this.delete_vcard();
}
}
goDoCommand(command);
},
button_click: function(){
this.manually = true;
if(document.getElementById("cmd_attachVCard").getAttribute('checked') =='true'){
this.delete_vcard();
}else{
this.attach_vcard();
}
}
};
window.addEventListener("compose-window-init", function(){autovcard.init();}, true);
window.addEventListener("compose-send-message", function(){autovcard.add_note();}, true);