/** * @author diogenes */ function local_messages() { this.dbGears = null; this.localServer = null; this.store = null; this.fileSubmitter = null; } local_messages.prototype.init_local_messages = function(){ if(this.dbGears == null) this.dbGears = google.gears.factory.create('beta.database'); if(this.localServer==null) this.localServer = google.gears.factory.create('beta.localserver'); if(this.store==null) this.store = this.localServer.createStore('test-store'); if(this.fileSubmitter == null) this.fileSubmitter = this.store.createFileSubmitter(); this.dbGears.open('database-test'); this.dbGears.execute('create table if not exists folder (folder text,uid_usuario int,unique(folder,uid_usuario))'); this.dbGears.execute('create table if not exists mail' + ' (mail blob,original_id int,original_folder text,header blob,timestamp int,uid_usuario int,unseen int,id_folder int,' + ' ffrom text, subject text, fto text, cc text, body text, size int,unique (original_id,original_folder,uid_usuario))'); this.dbGears.execute('create table if not exists anexo' + ' (id_mail int,nome_anexo text,url text,pid int)'); //some people that used old version of local messages could not have the size column. If it's the first version //with local messages you're using in expresso, this part of code can be removed try { this.dbGears.execute('alter table mail add column size int'); }catch(Exception) { } var rs = this.dbGears.execute('select rowid,header from mail where size is null'); while(rs.isValidRow()) { var temp = connector.unserialize(rs.field(1)); this.dbGears.execute('update mail set size='+temp.Size+' where rowid='+rs.field(0)); rs.next(); } //end of temporary code } local_messages.prototype.drop_tables = function() { this.init_local_messages(); var rs = this.dbGears.execute('select url from anexo'); while(rs.isValidRow()) { this.store.remove(rs.field(0)); rs.next(); } this.dbGears.execute('drop table folder'); this.dbGears.execute('drop table mail'); this.dbGears.execute('drop table anexo'); this.finalize(); } local_messages.prototype.insert_mail = function(msg_info,msg_header,anexos,folder) { try { this.init_local_messages(); var unseen = 0; var login = msg_info.login; var original_id = msg_info.msg_number; var original_folder = msg_info.msg_folder; //This fields needs to be separeted to search. var from = connector.serialize(msg_info.from); var subject = msg_info.subject; var body = msg_info.body; var to = connector.serialize(msg_info.toaddress2); var cc = connector.serialize(msg_info.cc); var size = msg_header.Size; //do not duplicate this information msg_info.from = null; msg_info.subject = null; msg_info.body = null; msg_info.to = null; msg_info.cc = null; msg_header.Size=null; //If the mail was archieved in the same date the user received it, the date cames with the time. //here I solved it if(msg_header.udate.indexOf(":")!=-1) { msg_header.udate = msg_header.aux_date; } /** * The importance attribute can be empty, and javascript consider as null causing nullpointer. */ if((msg_header.Importance == null) || (msg_header.Importance == "")) msg_header.Importance = "Normal"; msg_header.aux_date = null; var mail = connector.serialize(msg_info); var header = connector.serialize(msg_header); var timestamp = msg_info.timestamp; var id_folder; if((folder==null) || (folder=="local_root")) folder = "Inbox"; else folder = folder.substr(6);//take off the word "local_" var rs = this.dbGears.execute("select rowid from folder where folder=? and uid_usuario=?",[folder,account_id]); if(rs.isValidRow()) id_folder=rs.field(0); else { this.dbGears.execute("insert into folder (folder,uid_usuario) values (?,?)",["Inbox",account_id]); id_folder = this.dbGears.lastInsertRowId; } if(msg_info.Unseen=="U") unseen = 1; this.dbGears.execute("insert into mail (mail,original_id,original_folder,header,timestamp,uid_usuario,unseen,id_folder,ffrom,subject,fto,cc,body,size) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)",[mail,original_id,original_folder,header,timestamp,login,unseen,id_folder,from,subject,to,cc,body,size]); var call_back = function() { } this.store.capture(msg_info.url_export_file,call_back); var id_mail = this.dbGears.lastInsertRowId; this.insert_attachments(id_mail,anexos); this.finalize(); return true; } catch (error) { this.finalize(); return false; } } local_messages.prototype.get_local_mail = function(id_mail) { this.init_local_messages(); var rs = this.dbGears.execute("select mail.rowid,mail.mail,mail.ffrom,mail.subject,mail.body,mail.fto,mail.cc,folder.folder from mail inner join folder on mail.id_folder=folder.rowid where mail.rowid="+id_mail); var retorno = null; if(rs.isValidRow()) { retorno = rs.field(1); } retorno = connector.unserialize(retorno); retorno['from'] = connector.unserialize(rs.field(2)); retorno['subject'] = rs.field(3); retorno['body'] = rs.field(4); retorno['to'] = connector.unserialize(rs.field(5)); retorno['cc'] = connector.unserialize(rs.field(6)); retorno['local_message'] = true; retorno['msg_folder'] = "local_"+rs.field(7); //Now it's a local folder retorno['msg_number'] = rs.field(0); //the message number is the rowid this.finalize(); return retorno; } local_messages.prototype.insert_attachments = function(id_msg,anexos) { //insert_mail already close and open gears. for (var i = 0; i < anexos.length; i++) { this.dbGears.execute("insert into anexo (id_mail,nome_anexo,url,pid) values (?,?,?,?)", [id_msg, anexos[i]['name'],anexos[i]['url'],anexos[i]['pid']]); this.save_anexos(anexos[i]['url']); } } local_messages.prototype.save_anexos = function (url) { //insert_mail already close and open gears. var call_back = function(url,success,captureId) { //alert("Capturado: " + url); } //alert(url); this.store.capture(url,call_back); } local_messages.prototype.get_local_range_msgs = function(folder,msg_range_begin,emails_per_page,sort,sort_reverse,search) { this.init_local_messages(); var retorno = new Array(); msg_range_begin--; filter = " "; if(search=="FLAGGED") { filter = "and (header like '%\"Flagged\";s:1:\"F%' or header like '%\"Importance\";s:5:\"High%') "; } if(search=="UNSEEN") { filter = "and unseen = 1 "; } if(search=="SEEN") { filter = "and unseen = 0 "; } if (search=="ANSWERED") { filter = "and header like '%\"Answered\";s:1:\"A%' "; } sql = 'select rowid,header,size,timestamp,unseen from ('; sql += 'select mail.rowid as rowid,mail.header as header,mail.size as size,mail.timestamp as timestamp,mail.unseen as unseen,ltrim(ltrim(substr(UPPER(ffrom),21,length(ffrom)),\':\'),\'"\') as order_from,mail.subject from mail inner join folder on mail.id_folder=folder.rowid where mail.uid_usuario=? and folder.folder=? and lower(mail.ffrom) not like lower(?) '; sql += filter; sql += 'union '; sql += 'select mail.rowid as rowid,mail.header as header,mail.size as size,mail.timestamp as timestamp,mail.unseen as unseen,ltrim(ltrim(substr(UPPER(fto),7,length(fto)),\':\'),\'"\') as order_from,mail.subject from mail inner join folder on mail.id_folder=folder.rowid where mail.uid_usuario=? and folder.folder=? and lower(mail.ffrom) like lower(?) '; sql += filter; sql += ') order by ' if(sort == 'SORTFROM') { sql += 'order_from '; } if(sort == 'SORTARRIVAL') { sql += 'timestamp '; } if(sort == 'SORTSIZE') { sql += 'size '; } if(sort == 'SORTSUBJECT') { sql += 'UPPER(subject) '; } sql+= sort_reverse==0?"ASC ":"DESC "; sql +='limit ?,? '; var rs = this.dbGears.execute(sql,[account_id,folder,'%'+Element("user_email").value+'%',account_id,folder,'%'+Element("user_email").value+'%',msg_range_begin,emails_per_page]); var cont = 0; var rs3 = this.dbGears.execute('select count(*) from mail inner join folder on mail.id_folder=folder.rowid where mail.uid_usuario=? and folder.folder=?'+filter,[account_id,folder]); while (rs.isValidRow()) { //var email = rs.field(1); var head = rs.field(1); var codigoMail = rs.field(0); var rs2 = this.dbGears.execute('select count(*) from anexo where id_mail = '+codigoMail); var head_unserialized = connector.unserialize(head); head_unserialized.Size = rs.field(2); if(rs.field(4)==1) head_unserialized.Unseen = 'U'; //var email_unserialized = connector.unserialize(email); retorno[cont] = head_unserialized; retorno[cont]['msg_number'] = codigoMail; cont++; rs.next(); } retorno['num_msgs'] = rs3.field(0); rs3.close(); rs.close(); if(cont>0) rs2.close(); this.finalize(); return retorno; } local_messages.prototype.get_url_anexo = function(msg_number,pid) { this.init_local_messages(); var retorno; var rs = this.dbGears.execute("select url from anexo where id_mail="+msg_number+" and pid = '"+pid+"'"); retorno = rs.field(0) this.finalize(); return retorno; } local_messages.prototype.getInputFileFromAnexo = function (element,url) { this.init_local_messages(); this.fileSubmitter.setFileInputElement(element,url); this.finalize(); } local_messages.prototype.finalize = function() { this.dbGears.close(); this.dbGears = null; } local_messages.prototype.delete_msgs = function(msgs_number,border_ID) { this.init_local_messages(); var rs = this.dbGears.execute("select url from anexo where id_mail in ("+msgs_number+")"); while(rs.isValidRow()) { this.store.remove(rs.field(0)); rs.next(); } this.dbGears.execute("delete from anexo where id_mail in ("+msgs_number+")"); this.dbGears.execute("delete from mail where rowid in ("+msgs_number+")"); this.finalize(); if (msgs_number.length == 1) write_msg(get_lang("The message was deleted.")); else write_msg(get_lang("The messages were deleted.")); mail_msg = Element("tbody_box"); try { msgs_exploded = msgs_number.split(","); }catch(error) { msgs_exploded = new Array(); msgs_exploded[0] = msgs_number; } var msg_to_delete; for (var i=0; i"); var friendly_filters = new Array(); if (filters[0].indexOf('ALL') != -1) { //all filters... friendly_filters.push("SUBJECT"); friendly_filters.push(filters[1]); friendly_filters.push("BODY"); friendly_filters.push(filters[1]); friendly_filters.push("FROM"); friendly_filters.push(filters[1]); friendly_filters.push("TO"); friendly_filters.push(filters[1]); friendly_filters.push("CC"); friendly_filters.push(filters[1]); } else { friendly_filters[0] = filters[0]; for (var i in filters) { if (i != 0) { var temp = filters[i].split(" "); friendly_filters.push(temp[0]); friendly_filters.push(temp[1]); } } } var sql = "select mail.header,folder.folder,mail.rowid,size from mail inner join folder on mail.id_folder=folder.rowid where mail.uid_usuario="+account_id + " and folder.folder in ("; for(var fnum in folders) { sql+="'"+folders[fnum]+"'"; if(fnum= 1){ return tmp + " kb"; }else{ return size + " b"; } } local_messages.prototype.aux_convert_filter_field = function(filter) { if((filter=="SUBJECT ") || (filter=="SUBJECT")) return "subject"; else if((filter=="BODY ") || (filter=="BODY")) return "body"; else if((filter=="FROM ") || (filter=="FROM")) return "ffrom"; else if((filter=="TO ") || (filter=="TO")) return "fto"; else if((filter=="CC ") || (filter=="CC")) return "cc"; } local_messages.prototype.has_local_mails = function() { this.init_local_messages(); var rs = this.dbGears.execute("select rowid from folder limit 0,1"); var retorno; if(rs.isValidRow()) retorno = true; else retorno = false; this.finalize(); return retorno; } var expresso_local_messages; expresso_local_messages = new local_messages();