Abstract API token sys into a singleton component

This commit is contained in:
Gioacchino Mazzurco 2017-04-11 15:48:16 +02:00
parent 533dbef0c7
commit 49b0de6ac7
7 changed files with 120 additions and 69 deletions

View File

@ -22,5 +22,7 @@
<file>qml/icons/emblem-locked.png</file>
<file>qml/BusyOverlay.qml</file>
<file>qml/URI.js</file>
<file>qml/TokensManager.qml</file>
<file>qml/qmldir</file>
</qresource>
</RCC>

View File

@ -19,6 +19,7 @@
import QtQuick 2.0
import QtQuick.Controls 2.0
import org.retroshare.qml_components.LibresapiLocalClient 1.0
import "." //Needed for TokensManager singleton
Item
{
@ -35,7 +36,7 @@ Item
{
chatModel.json = par.response
token = JSON.parse(par.response).statetoken
mainWindow.registerToken(token, refreshData)
TokensManager.registerToken(token, refreshData)
if(chatListView.visible)
{

View File

@ -21,6 +21,7 @@ import QtQuick.Controls 2.0
import QtQuick.Dialogs 1.2
import QtQml.Models 2.2
import org.retroshare.qml_components.LibresapiLocalClient 1.0
import "." //Needed for TokensManager singleton
Item
{
@ -53,7 +54,7 @@ Item
if (contactsListModel.model.count < 1)
contactsListModel.json = par.response
var token = JSON.parse(par.response).statetoken
mainWindow.registerToken(token, refreshContacts)
TokensManager.registerToken(token, refreshContacts)
contactsSortWorker.sendMessage(
{'action': 'refreshContacts', 'response': par.response})
}
@ -69,7 +70,7 @@ Item
console.log("contactsView.refreshOwnCallback(par)", visible)
var json = JSON.parse(par.response)
var token = json.statetoken
mainWindow.registerToken(token, refreshOwn)
TokensManager.registerToken(token, refreshOwn)
if(json.data.length > 0)
{
@ -95,7 +96,7 @@ Item
{
console.log("contactsView.refreshUnreadCB()", visible)
var json = JSON.parse(par.response)
mainWindow.registerToken(json.statetoken, refreshUnread)
TokensManager.registerToken(json.statetoken, refreshUnread)
contactsSortWorker.sendMessage(
{'action': 'refreshUnread', 'response': par.response})
}

View File

@ -0,0 +1,108 @@
/*
* RetroShare Android QML App
* Copyright (C) 2017 Gioacchino Mazzurco <gio@eigenlab.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
pragma Singleton
import QtQml 2.2
import org.retroshare.qml_components.LibresapiLocalClient 1.0
QtObject
{
id: tokensManager
property var tokens: ({})
function registerToken(token, callback)
{
if (Array.isArray(tokens[token]))
{
// Do not register if it is registered already
var arrLen = tokens[token].length
for(var i=0; i<arrLen; ++i)
{
if(callback === tokens[token][i])
{
console.warn("tokensManager.registerToken(token, callback)",
"Attempt to register same callback twice for:",
i, token, callback)
return
}
}
tokens[token].push(callback)
}
else tokens[token] = [callback]
}
function tokenExpire(token)
{
if(Array.isArray(tokens[token]))
{
var arrLen = tokens[token].length
for(var i=0; i<arrLen; ++i)
{
var tokCallback = tokens[token][i]
if (typeof tokCallback == 'function')
{
console.log("event token", token, tokCallback)
tokCallback()
}
}
}
delete tokens[token]
}
function isTokenValid(token) { return Array.isArray(tokens[token]) }
property alias refreshInterval: refreshTokensTimer.interval
property LibresapiLocalClient refreshTokensApi: LibresapiLocalClient
{
id: refreshTokensApi
onResponseReceived:
{
var jsonData = JSON.parse(msg).data
var arrayLength = jsonData.length
for (var i = 0; i < arrayLength; i++)
{
tokensManager.tokenExpire(jsonData[i])
}
}
Component.onCompleted:
{
if(QT_DEBUG) debug = false
openConnection(apiSocketPath)
refreshTokensTimer.start()
}
function refreshTokens()
{
request("/statetokenservice/*",
'['+Object.keys(tokensManager.tokens)+']')
}
}
property Timer refreshTokensTimer: Timer
{
id: refreshTokensTimer
interval: 1500
repeat: true
triggeredOnStart: true
onTriggered: refreshTokensApi.refreshTokens()
}
}

View File

@ -20,6 +20,7 @@ import QtQuick 2.0
import QtQuick.Controls 2.0
import QtQuick.Dialogs 1.2
import "jsonpath.js" as JSONPath
import "." //Needed for TokensManager singleton
Item
{
@ -33,7 +34,7 @@ Item
{
jsonModel.json = par.response
token = JSON.parse(par.response).statetoken
mainWindow.registerToken(token, refreshData)
TokensManager.registerToken(token, refreshData)
}
function refreshData()
{ if(visible) rsApi.request("/peers/*", "", refreshDataCallback) }

View File

@ -20,6 +20,7 @@ import QtQuick 2.2
import QtQuick.Controls 2.0
import org.retroshare.qml_components.LibresapiLocalClient 1.0
import "URI.js" as URI
import "." //Needed for TokensManager singleton
ApplicationWindow
{
@ -31,38 +32,11 @@ ApplicationWindow
property string pgp_name
property var tokens: ({})
function registerToken(token, callback)
{
if (Array.isArray(tokens[token])) tokens[token].push(callback)
else tokens[token] = [callback]
}
function tokenExpire(token)
{
if(Array.isArray(tokens[token]))
{
var arrLen = tokens[token].length
for(var i=0; i<arrLen; ++i)
{
var tokCallback = tokens[token][i]
if (typeof tokCallback == 'function')
{
console.log("event token", token, tokCallback)
tokCallback()
}
}
}
delete tokens[token]
}
function isTokenValid(token) { return Array.isArray(tokens[token]) }
function handleIntentUri(uriStr)
{
console.log("handleIntentUri", JSON.stringify(URI.parse(uriStr), null, 1))
}
header: ToolBar
{
id: toolBar
@ -199,43 +173,6 @@ ApplicationWindow
]
}
LibresapiLocalClient
{
id: refreshTokensApi
onResponseReceived:
{
var jsonData = JSON.parse(msg).data
var arrayLength = jsonData.length;
for (var i = 0; i < arrayLength; i++)
mainWindow.tokenExpire(jsonData[i])
}
Component.onCompleted:
{
if(QT_DEBUG) debug = false
openConnection(apiSocketPath)
refreshTokensTimer.start()
}
function refreshTokens()
{
var ret = request("/statetokenservice/*",
'['+Object.keys(mainWindow.tokens)+']')
if(ret < 1) stackView.state = "core_down"
}
}
Timer
{
id: refreshTokensTimer
interval: 1500
repeat: true
triggeredOnStart: true
onTriggered: refreshTokensApi.refreshTokens()
}
Timer
{
id: coreStateCheckTimer

View File

@ -0,0 +1 @@
singleton TokensManager 1.0 TokensManager.qml