Friendiqa/source-android/js/helper.js

139 lines
5.3 KiB
JavaScript
Raw Normal View History

2017-01-26 21:55:31 +01:00
.pragma library
.import QtQuick.LocalStorage 2.0 as Sql
function friendicaRequest(login,api,rootwindow,callback) {
var xhrequest= new XMLHttpRequest();
xhrequest.onreadystatechange = function() {
//print(api+JSON.stringify(login));
if(xhrequest.readyState === XMLHttpRequest.DONE) {
2017-03-25 23:36:14 +01:00
try{ if (xhrequest.status=200){ //if (xhrequest.responseText!=""){
2017-01-26 21:55:31 +01:00
callback(xhrequest.responseText)
}else{
2017-05-11 22:15:34 +02:00
showMessage("Error","API:" +login.server+api+"\n NO RESPONSE"+xhrequest.statusText,rootwindow);
//callback(xhrequest.responseText)
2017-01-26 21:55:31 +01:00
}
}
2017-05-11 22:15:34 +02:00
catch (e){print(e);
showMessage("Error", xhrequest.responseText,rootwindow)
2017-01-26 21:55:31 +01:00
}
}
}
xhrequest.open("GET", login.server+api,true,login.username,Qt.atob(login.password));
xhrequest.send();
}
2017-11-07 21:57:40 +01:00
function friendicaPostRequest(login,api,data,method,rootwindow,callback) {
2017-01-26 21:55:31 +01:00
var xhrequest= new XMLHttpRequest();
xhrequest.onreadystatechange = function() {
2017-03-25 23:36:14 +01:00
//print(api+JSON.stringify(login)+Qt.atob(login.password));
2017-01-26 21:55:31 +01:00
if (xhrequest.readyState === XMLHttpRequest.HEADERS_RECEIVED) {
} else if(xhrequest.readyState === XMLHttpRequest.DONE) {
try{ if (xhrequest.responseText!=""){
callback(xhrequest.responseText)
}else{
showMessage("Error",api+" NO RESPONSE",rootwindow)
callback(xhrequest.responseText)
}
}
catch (e){
showMessage("Error", api+" "+e,rootwindow)
}
}
}
2017-11-07 21:57:40 +01:00
xhrequest.open(method, login.server+api,true,login.username,Qt.atob(login.password));
xhrequest.send(data);
2017-01-26 21:55:31 +01:00
}
2017-11-07 21:57:40 +01:00
2017-03-25 23:36:14 +01:00
function getCount(database,login,table,field,countvalue){
var db=Sql.LocalStorage.openDatabaseSync(database[0],database[1],database[2],database[3]);
var count=0;
db.transaction( function(tx) {
var countrs = tx.executeSql('SELECT COUNT(*) from '+table+' WHERE username= "'+ login.username +'" AND '+field+' = "'+countvalue+'"');
count = parseInt(countrs.rows.item(0)["COUNT(*)"])
})
return count
}
2017-01-26 21:55:31 +01:00
function friendicaWebRequest(url,rootwindow,callback) {
var xhrequest = new XMLHttpRequest();
xhrequest.onreadystatechange = function() {
if (xhrequest.readyState === XMLHttpRequest.HEADERS_RECEIVED) {
} else if(xhrequest.readyState === XMLHttpRequest.DONE) {
try{ callback(xhrequest.responseText);
}
2017-05-11 22:15:34 +02:00
catch (e){print(e);
2017-01-26 21:55:31 +01:00
showMessage("Error",url+" "+e, rootwindow)
}
}
}
xhrequest.open("GET", url,true);
xhrequest.send();
}
2017-03-25 23:36:14 +01:00
function readData(database,table,username,callback,filter,filtervalue, sort) { // reads and applies data from DB
2017-01-26 21:55:31 +01:00
if (filter){
var where = " AND "+ filter +" = '" + filtervalue+"'";
} else { var where="";}
if (username){
var user = ' where username= "'+ username +'"';
} else { var user='';}
2017-03-25 23:36:14 +01:00
if (sort){
var sortparam = " ORDER BY "+ sort;
} else { var sortparam="";}
2017-01-26 21:55:31 +01:00
var db=Sql.LocalStorage.openDatabaseSync(database[0],database[1],database[2],database[3]);
if(!db) { return; }
db.transaction( function(tx) {
2017-03-25 23:36:14 +01:00
//print('select * from '+table+user+where+sortparam);
2017-01-26 21:55:31 +01:00
var rsArray=[];
2017-03-25 23:36:14 +01:00
var rs = tx.executeSql('select * from '+table+user+where+sortparam);
2017-01-26 21:55:31 +01:00
for(var i = 0; i < rs.rows.length; i++) {
rsArray.push(rs.rows.item(i))
}
callback(rsArray);
});
}
function readField(field,database,table, username, callback,filter,filtervalue) { // reads and applies data from DB
if (filter){
var where = " AND "+ filter +" = '" + filtervalue+"'";
} else { var where="";}
var db=Sql.LocalStorage.openDatabaseSync(database[0],database[1],database[2],database[3]);
if(!db) { return; }
db.transaction( function(tx) {
//print('... read from database ' + field)
var rsArray=[];
//print('select DISTINCT '+field+' from '+table+' WHERE username="'+username+'"'+where+' ORDER BY '+field+' ASC');
var rs = tx.executeSql('select DISTINCT '+field+' from '+table+' WHERE username="'+username+'"'+where+' ORDER BY '+field+' ASC');
for(var i = 0; i < rs.rows.length; i++) {
rsArray.push(rs.rows.item(i)[field])
}
callback(rsArray);
});
}
2017-11-07 21:57:40 +01:00
function showMessage(header,message,rootwindow){//print("message: "+message);
2017-01-26 21:55:31 +01:00
var cleanmessage=message.replace(/"/g,"-");
var messageString='import QtQuick 2.0; import QtQuick.Dialogs 1.2; MessageDialog{ visible: true; title:"'+header+'";standardButtons: StandardButton.Ok; text:" '+cleanmessage+'"}';
var messageObject=Qt.createQmlObject(messageString,rootwindow,"messageOutput");
}
function inArray(list, prop, val) {
if (list.length > 0 ) {
2017-03-25 23:36:14 +01:00
for (var i in list) { if (list[i][prop] == val) {
2017-01-26 21:55:31 +01:00
return i;
}
}
} return -1;
}
function cleanArray(array) {
var arraystring=JSON.stringify(array);
arraystring=arraystring.replace(/[\[\]]/g , '');
return arraystring;
}
2017-05-11 22:15:34 +02:00
function cleanDate(date){
var cleanedDate= date.slice(0,3)+", "+date.slice(8,11)+date.slice(4,7)+date.slice(25,30)+date.slice(10,25);
return cleanedDate
}