Still working on multi-accounts. Made some progress

This commit is contained in:
Scharel Clemens 2018-11-17 22:28:25 +01:00
parent 3f95d6ed1b
commit d2601ba749
8 changed files with 176 additions and 121 deletions

View file

@ -10,50 +10,51 @@ ApplicationWindow
ConfigurationGroup {
id: appSettings
path: "/apps/harbour-nextcloudnotes/settings"
synchronous: true
property int currentAccount: value("currentAccount", -1)
property var accountIDs: value("accountIDs", [])
//Component.onCompleted: clear()
}
ConfigurationGroup {
id: accounts
path: "/apps/harbour-nextcloudnotes/accounts"
ConfigurationGroup {
id: account
path: appSettings.accountIDs[appSettings.currentAccount]
property string name
property url server
property string username
property string password
property date update
property bool unsecureConnection: false
property bool unencryptedConnection: false
onPathChanged: {
console.log(scope.path + "/" + path + ": " + name)
console.log(name)
ConfigurationValue {
id: nextcloudUUIDs
key: "/apps/harbour-nextcloudnotes/settings/accountIDs"
defaultValue: []
onValueChanged: {
nextcloudAccounts.model = value
console.log("IDs changed: " + value)
for (var i = 0; i < value.length; i++) {
console.log("Account " + i + i === appSettings.currentAccount ? " (current)" : "")
console.log(" - " + nextcloudAccounts.itemAt(i).name)
console.log(" - " + nextcloudAccounts.itemAt(i).username + "@" + nextcloudAccounts.itemAt(i).server)
}
}
function uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
function add() {
var uuid = uuidv4()
return uuid
}
//Component.onCompleted: clear()
Component.onCompleted: nextcloudAccounts.model = value
}
NotesApi {
id: notes
Repeater {
id: nextcloudAccounts
//model: nextcloudUUIDs.value
delegate: NotesApi { uuid: nextcloudUUIDs.value[index] }
function add() {
push(uuidv4())
}
function push(uuid) {
var accountIDs = nextcloudUUIDs.value
accountIDs.push(uuid)
nextcloudUUIDs.value = accountIDs
}
function pop() {
var accountIDs = nextcloudUUIDs.value
accountIDs.pop()
nextcloudUUIDs.value = accountIDs
}
}
function uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
initialPage: Component { NotesPage { } }

View file

@ -19,12 +19,12 @@ Page {
}
}
ViewPlaceholder {
enabled: true // TODO
text: qsTr("Nextcloud Notes")
hintText: qsTr("for Sailfish OS")
}
VerticalScrollDecorator {}
}
ViewPlaceholder {
enabled: true // TODO
text: qsTr("Nextcloud Notes")
hintText: qsTr("for Sailfish OS")
}
}

View file

@ -5,22 +5,16 @@ import Nemo.Configuration 1.0
Dialog {
id: loginDialog
property var accountID
ConfigurationGroup {
id: account
path: "/apps/harbour-nextcloudnotes/accounts/" + accountID
}
property int account
canAccept: (nameField.text.length > 0 && serverField.acceptableInput && usernameField.text.length > 0 && passwordField.text.length > 0)
onAccepted: {
account.setValue("name", nameField.text)
account.setValue("server", serverField.text)
account.setValue("username", usernameField.text)
account.setValue("password", passwordField.text)
//accounts.itemAt(iAccount).unsecureConnection = unsecureConnectionTextSwitch.checked
//accounts.itemAt(iAccount).unencryptedConnection = unencryptedConnectionTextSwitch.checked
account.setValue("valid", true)
account.sync()
nextcloudAccounts.itemAt(account).name = nameField.text
nextcloudAccounts.itemAt(account).server = serverField.text
nextcloudAccounts.itemAt(account).username = usernameField.text
nextcloudAccounts.itemAt(account).password = passwordField.text
//dnextcloudAccounts.itemAt(account).unsecureConnection = unsecureConnectionTextSwitch.checked
//dnextcloudAccounts.itemAt(account).unencryptedConnection = unencryptedConnectionTextSwitch.checked
}
SilicaFlickable {
@ -48,7 +42,7 @@ Dialog {
id: nameField
focus: true
width: parent.width
text: account.value("name", qsTr("My Nextcloud account"), String)
text: nextcloudAccounts.itemAt(account).name.length <= 0 ? qsTr("Unnamed account") : nextcloudAccounts.itemAt(account).name
placeholderText: qsTr("Account name")
label: placeholderText
errorHighlight: text.length === 0// && focus === true
@ -60,7 +54,7 @@ Dialog {
TextField {
id: serverField
width: parent.width
text: account.value("server", "https://", String)
text: nextcloudAccounts.itemAt(account).server.length <= 0 ? "https://" : nextcloudAccounts.itemAt(account).server
placeholderText: qsTr("Nextcloud server")
label: placeholderText + " " + qsTr("(starting with \"https://\")")
inputMethodHints: Qt.ImhUrlCharactersOnly
@ -75,7 +69,7 @@ Dialog {
TextField {
id: usernameField
width: parent.width
text: account.value("username", "", String)
text: nextcloudAccounts.itemAt(account).username
placeholderText: qsTr("Username")
label: placeholderText
inputMethodHints: Qt.ImhNoPredictiveText | Qt.ImhNoAutoUppercase
@ -88,7 +82,7 @@ Dialog {
PasswordField {
id: passwordField
width: parent.width
text: account.value("password", "", String)
text: nextcloudAccounts.itemAt(account).password
label: placeholderText
errorHighlight: text.length === 0// && focus === true
EnterKey.enabled: text.length > 0

View file

@ -1,17 +1,55 @@
import QtQuick 2.0
import Sailfish.Silica 1.0
import Nemo.Configuration 1.0
Item {
property string uuid
property string name
property url server
property string username
property string password
property date update
property bool unsecureConnection
property bool unencryptedConnection
property var model: ListModel { }
property string file: StandardPaths.data + "/" + account.name + ".json"
property string file: StandardPaths.data + "/" + uuid + ".json"
property bool saveFile: false
property bool busy: false
//property date lastUpdate: new Date(0)
ConfigurationGroup {
id: account
path: "/apps/harbour-nextcloudnotes/accounts/" + uuid
}
Component.onCompleted: {
name = account.value("name", "", String)
server = account.value("server", "", String)
username = account.value("username", "", String)
password = account.value("password", "", String)
update = account.value("update", "", Date)
unsecureConnection = account.value("unsecureConnection", false, Boolean)
unencryptedConnection = account.value("unencryptedConnection", false, Boolean)
}
onUuidChanged: account.setValue("uuid", uuid)
onNameChanged: account.setValue("name", name)
onServerChanged: account.setValue("server", server)
onUsernameChanged: account.setValue("username", username)
onPasswordChanged: account.setValue("password", password)
onUpdateChanged: account.setValue("update", update)
onUnsecureConnectionChanged: account.setValue("unsecureConnection", unsecureConnection)
onUnencryptedConnectionChanged: account.setValue("unencryptedConnection", unencryptedConnection)
function clear() {
account.clear()
}
function callApi(method, data) {
busy = true
var endpoint = account.server + "/index.php/apps/notes/api/v0.2/notes"
var endpoint = server + "/index.php/apps/notes/api/v0.2/notes"
if (data && (method === "GET" || method === "PUT" || method === "DELETE"))
if (data.id)
endpoint = endpoint + "/" + data.id
@ -21,13 +59,14 @@ Item {
apiReq.setRequestHeader('User-Agent', 'SailfishOS/harbour-nextcloudnotes')
apiReq.setRequestHeader('OCS-APIRequest', 'true')
apiReq.setRequestHeader("Content-Type", "application/json")
apiReq.setRequestHeader("Authorization", "Basic " + Qt.btoa(account.username + ":" + account.password))
apiReq.setRequestHeader("Authorization", "Basic " + Qt.btoa(username + ":" + password))
apiReq.onreadystatechange = function() {
if (apiReq.readyState === XMLHttpRequest.DONE) {
if (apiReq.status === 200) {
console.log("Successfull request!")
console.log(apiReq.responseText)
//console.log(apiReq.responseText)
// TODO handle non arrays
model.clear()
var elements = JSON.parse(apiReq.responseText)
for (var element in elements) {
model.append(elements[element])
@ -96,7 +135,7 @@ Item {
filePut.open("PUT", file)
filePut.send(json)
model.clear()
account.update = new Date(0)
update = new Date(0)
status = 200
}
@ -123,7 +162,7 @@ Item {
function parseJson() {
var elements = JSON.parse(json)
if (elements === null) {
console.log("Error parsing " + account.name + "-JSON")
console.log("Error parsing " + uuid + "-JSON")
elements = ""
json = ""
return null

View file

@ -4,13 +4,24 @@ import Sailfish.Silica 1.0
Page {
id: page
onStatusChanged: {
if (status === PageStatus.Active) {
if (nextcloudAccounts.count > 0) {
nextcloudAccounts.itemAt(appSettings.currentAccount).getNotes()
}
else {
addAccountHint.restart()
}
}
}
SilicaListView {
id: notesList
anchors.fill: parent
spacing: Theme.paddingLarge
PullDownMenu {
busy: notes.busy
busy: nextcloudAccounts.itemAt(appSettings.currentAccount) ? nextcloudAccounts.itemAt(appSettings.currentAccount).busy : false
MenuItem {
text: qsTr("Settings")
@ -18,22 +29,23 @@ Page {
}
MenuItem {
text: qsTr("Add note")
enabled: !notes.busy
visible: account.server.length > 0
enabled: nextcloudAccounts.itemAt(appSettings.currentAccount) ? !nextcloudAccounts.itemAt(appSettings.currentAccount).busy : false
visible: appSettings.currentAccount >= 0
onClicked: console.log("Add note")
}
MenuItem {
text: qsTr("Reload")
enabled: !notes.busy
visible: account.server.length > 0
onClicked: notes.getNotes()
enabled: nextcloudAccounts.itemAt(appSettings.currentAccount) ? !nextcloudAccounts.itemAt(appSettings.currentAccount).busy : false
visible: appSettings.currentAccount >= 0
onClicked: nextcloudAccounts.itemAt(appSettings.currentAccount).getNotes()
}
MenuLabel {
visible: account.server.length > 0
text: qsTr("Last update") + ": " +
account.update.value !== 0 ?
new Date(account.update).toLocaleString(Qt.locale(), Locale.ShortFormat) :
qsTr("never")
visible: appSettings.currentAccount >= 0
text: nextcloudAccounts.itemAt(appSettings.currentAccount) ? (
qsTr("Last update") + ": " + (
new Date(nextcloudAccounts.itemAt(appSettings.currentAccount).update).valueOf() !== 0 ?
new Date(nextcloudAccounts.itemAt(appSettings.currentAccount).update).toLocaleString(Qt.locale(), Locale.ShortFormat) :
qsTr("never"))) : ""
//(new Date(appSettings.value("accountUpdates", [appSettings.currentAccount])).value === 0 ?
//new Date(appSettings.value("accountUpdates", [appSettings.currentAccount])).toLocaleString(Qt.locale(), Locale.ShortFormat) :
//qsTr("never"))
@ -52,8 +64,8 @@ Page {
currentIndex: -1
Component.onCompleted: {
if (account.valid) {
notes.getNotes()
if (nextcloudAccounts.itemAt(appSettings.currentAccount)) {
nextcloudAccounts.itemAt(appSettings.currentAccount).getNotes()
}
}
@ -63,7 +75,11 @@ Page {
//Component.onCompleted: notes.updateNote(1212725, "# Hello World!\nIs this working?", "Test")
//Component.onCompleted: notes.deleteNote(1212725)
model: notes.model
model: nextcloudAccounts.itemAt(appSettings.currentAccount)? nextcloudAccounts.itemAt(appSettings.currentAccount).model : 0
Connections {
target: appSettings
onCurrentAccountChanged: notesList.model = nextcloudAccounts.itemAt(appSettings.currentAccount)? nextcloudAccounts.itemAt(appSettings.currentAccount).model : 0
}
delegate: ListItem {
id: note
@ -141,26 +157,26 @@ Page {
id: busyIndicator
anchors.centerIn: parent
size: BusyIndicatorSize.Large
visible: notesList.count === 0 && notes.busy
visible: nextcloudAccounts.itemAt(appSettings.currentAccount) ? nextcloudAccounts.itemAt(appSettings.currentAccount).busy && notesList.count === 0 : false
running: visible
}
ViewPlaceholder {
enabled: notesList.count === 0 && !notes.busy && !noLoginPlaceholder.enabled
enabled: notesList.count === 0 && !busyIndicator.running && !noLoginPlaceholder.enabled
text: qsTr("No notes yet")
hintText: qsTr("Pull down to add a note")
}
ViewPlaceholder {
id: noLoginPlaceholder
enabled: appSettings.accountIDs.length <= 0
enabled: nextcloudUUIDs.value.length <= 0
text: qsTr("No account yet")
hintText: qsTr("Got to the settings to add an account")
}
TouchInteractionHint {
id: addAccountHint
Component.onCompleted: if(!account.valid) restart()
//Component.onCompleted: if(!account.valid) restart()
interactionMode: TouchInteraction.Pull
direction: TouchInteraction.Down
}

View file

@ -29,67 +29,71 @@ Page {
SectionHeader {
text: qsTr("Accounts")
}
Label {
id: noAccountsLabel
visible: appSettings.accountIDs.length <= 0
visible: nextcloudAccounts.count <= 0
text: qsTr("No Nextcloud account yet")
font.pixelSize: Theme.fontSizeLarge
color: Theme.secondaryHighlightColor
anchors.horizontalCenter: parent.horizontalCenter
}
Repeater {
model: appSettings.accountIDs.length
model: nextcloudAccounts.count
delegate: ListItem {
id: listItem
ConfigurationGroup {
id: account
path: "/apps/harbour-nextcloudnotes/accounts/" + appSettings.accountIDs[index]
}
id: accountListItem
contentHeight: textSwitch.height
highlighted: textSwitch.down
TextSwitch {
anchors.verticalCenter: parent.verticalCenter
id: textSwitch
automaticCheck: false
checked: index === appSettings.currentAccount
text: account.value("name", qsTr("Account") + " " + (index+1), String)
//enabled: account.value("valid", false, Boolean)
description: account.value("valid", false, Boolean) ? account.value("username", qsTr("user"), String) + "@" + account.value("server", qsTr("server"), String) : qsTr("Press and hold to configure")
onClicked: if (account.value("valid", false, Boolean)) appSettings.currentAccount = index
onPressAndHold: listItem.openMenu()
text: nextcloudAccounts.itemAt(index).name.length <= 0 ? qsTr("Unnamed account") : nextcloudAccounts.itemAt(index).name
description: nextcloudAccounts.itemAt(index).username + "@" + nextcloudAccounts.itemAt(index).server// : qsTr("Press and hold to configure")
onClicked: appSettings.currentAccount = index
onPressAndHold: openMenu()
}
menu: ContextMenu {
MenuItem {
text: qsTr("Configure")
text: qsTr("Edit")
onClicked: {
var login = pageStack.push(Qt.resolvedUrl("LoginDialog.qml"), { accountID: appSettings.accountIDs[index] })
var login = pageStack.push(Qt.resolvedUrl("LoginDialog.qml"), { account: index })
login.accepted.connect(function() {
update()
})
login.rejected.connect(function() {
})
}
}
/*MenuItem {
MenuItem {
visible: index === nextcloudAccounts.count-1
text: qsTr("Delete")
onClicked: {
accounts.itemAt(index).clear()
// TODO reorder items
accountListItem.remorseAction(qsTr("Deleting account"), function() {
nextcloudAccounts.itemAt(index).clear()
nextcloudAccounts.pop()
})
}
}*/
}
}
}
}
Button {
text: qsTr("Add account")
anchors.horizontalCenter: parent.horizontalCenter
onClicked: {
var login = pageStack.push(Qt.resolvedUrl("LoginDialog.qml"), { accountID: accounts.add() })
nextcloudAccounts.add()
var login = pageStack.push(Qt.resolvedUrl("LoginDialog.qml"), { account:nextcloudAccounts.count-1 })
login.accepted.connect(function() {
var tmpIDs = appSettings.accountIDs
tmpIDs.push(login.accountID)
appSettings.accountIDs = tmpIDs
console.log("Adding account " + nextcloudAccounts.itemAt(login.account).name)
if (appSettings.currentAccount < 0)
appSettings.currentAccount = nextcloudUUIDs.value.length-1
appWindow.update()
})
login.rejected.connect(function() {
nextcloudAccounts.pop()
})
}
}

View file

@ -1,7 +1,4 @@
#ifdef QT_QML_DEBUG
#include <QtQuick>
#endif
#include <sailfishapp.h>
int main(int argc, char *argv[])

View file

@ -65,7 +65,7 @@
<translation type="unfinished"></translation>
</message>
<message>
<source>My Nextcloud account</source>
<source>Unnamed account</source>
<translation type="unfinished"></translation>
</message>
</context>
@ -129,10 +129,6 @@
<source>never</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Open the settings to add a Nextcloud account</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>No account yet</source>
<translation type="unfinished"></translation>
@ -141,6 +137,10 @@
<source>Got to the settings to add an account</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Open the settings to configure your Nextcloud accounts</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>SettingsPage</name>
@ -157,23 +157,27 @@
<translation type="unfinished"></translation>
</message>
<message>
<source>user</source>
<source>No Nextcloud account yet</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>server</source>
<source>Add account</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Account</source>
<source>Edit</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Press and hold to configure</source>
<source>Unnamed account</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Configure</source>
<source>Delete</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Deleting account</source>
<translation type="unfinished"></translation>
</message>
</context>