Skip to content

Commit

Permalink
Implement a QFieldCloud server combobox to ease multi-server environm…
Browse files Browse the repository at this point in the history
…ents
  • Loading branch information
nirvn committed Sep 4, 2024
1 parent 75ee040 commit c8d5261
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 12 deletions.
24 changes: 23 additions & 1 deletion src/core/qfieldcloudconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,20 @@ QString QFieldCloudConnection::defaultUrl()
return QStringLiteral( "https://app.qfield.cloud" );
}

QStringList QFieldCloudConnection::urls() const
{
QStringList savedUrls = QSettings().value( QStringLiteral( "/QFieldCloud/urls" ) ).toStringList();
if ( !savedUrls.contains( defaultUrl() ) )
{
savedUrls.prepend( defaultUrl() );
}
if ( !savedUrls.contains( mUrl ) )
{
savedUrls << mUrl;
}
return savedUrls;
}

QString QFieldCloudConnection::username() const
{
return mUsername;
Expand Down Expand Up @@ -218,15 +232,23 @@ void QFieldCloudConnection::login()
setToken( token );
}

QSettings settings;
mUsername = resp.value( QStringLiteral( "username" ) ).toString();
QSettings().setValue( "/QFieldCloud/username", mUsername );
settings.setValue( QStringLiteral( "/QFieldCloud/username" ), mUsername );
emit usernameChanged();

mAvatarUrl = resp.value( QStringLiteral( "avatar_url" ) ).toString();
emit avatarUrlChanged();
mUserInformation = CloudUserInformation( mUsername, resp.value( QStringLiteral( "email" ) ).toString() );
emit userInformationChanged();

QStringList savedUrls = settings.value( QStringLiteral( "/QFieldCloud/urls" ), QStringList() << defaultUrl() ).toStringList();
if ( !savedUrls.contains( mUrl ) )
{
savedUrls << mUrl;
settings.setValue( QStringLiteral( "/QFieldCloud/urls" ), savedUrls );
emit urlsChanged();
}
setStatus( ConnectionStatus::LoggedIn );
} );
}
Expand Down
7 changes: 7 additions & 0 deletions src/core/qfieldcloudconnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class QFieldCloudConnection : public QObject
Q_PROPERTY( QString avatarUrl READ avatarUrl NOTIFY avatarUrlChanged )
Q_PROPERTY( QString url READ url WRITE setUrl NOTIFY urlChanged )
Q_PROPERTY( QString defaultUrl READ defaultUrl CONSTANT )
Q_PROPERTY( QStringList urls READ urls NOTIFY urlsChanged )

Q_PROPERTY( ConnectionStatus status READ status NOTIFY statusChanged )
Q_PROPERTY( ConnectionState state READ state NOTIFY stateChanged )
Expand Down Expand Up @@ -100,6 +101,11 @@ class QFieldCloudConnection : public QObject
*/
static QString defaultUrl();

/**
* Returns the connections URLs successfully logged in in the past.
*/
Q_INVOKABLE QStringList urls() const;

QString username() const;
void setUsername( const QString &username );

Expand Down Expand Up @@ -162,6 +168,7 @@ class QFieldCloudConnection : public QObject
void passwordChanged();
void avatarUrlChanged();
void urlChanged();
void urlsChanged();
void statusChanged();
void stateChanged();
void tokenChanged();
Expand Down
55 changes: 44 additions & 11 deletions src/qml/QFieldCloudLogin.qml
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,50 @@ Item {
wrapMode: Text.WordWrap
}

QfTextField {
id: serverUrlField
ComboBox {
id: serverUrlComboBox
Layout.preferredWidth: parent.width / 1.3
Layout.alignment: Qt.AlignHCenter
visible: cloudConnection.status === QFieldCloudConnection.Disconnected && (prefixUrlWithProtocol(cloudConnection.url) !== cloudConnection.defaultUrl || isServerUrlEditingActive)
enabled: visible
font: Theme.defaultFont
horizontalAlignment: Text.AlignHCenter
text: prefixUrlWithProtocol(cloudConnection.url) === cloudConnection.defaultUrl ? '' : cloudConnection.url
editable: true
model: [''].concat(cloudConnection.urls)

onTextChanged: text = text.replace(/\s+/g, '')
onEditingFinished: cloudConnection.url = text ? prefixUrlWithProtocol(text) : cloudConnection.defaultUrl
Keys.onReturnPressed: loginFormSumbitHandler()
Component.onCompleted: {
if (cloudConnection.url != cloudConnection.defaultUrl) {
currentIndex = find(cloudConnection.url);
}
}

onModelChanged: {
if (cloudConnection.url != cloudConnection.defaultUrl) {
currentIndex = find(cloudConnection.url);
}
}

function prefixUrlWithProtocol(url) {
if (!url || url.startsWith('http://') || url.startsWith('https://'))
return url;
return 'https://' + url;
onDisplayTextChanged: {
serverUrlField.text = displayText;
}

contentItem: QfTextField {
id: serverUrlField

inputMethodHints: Qt.ImhNoPredictiveText | Qt.ImhNoAutoUppercase | Qt.ImhPreferLowercase
Layout.preferredWidth: parent.width / 1.3
Layout.alignment: Qt.AlignHCenter
visible: cloudConnection.status === QFieldCloudConnection.Disconnected
enabled: visible
font: Theme.defaultFont
horizontalAlignment: Text.AlignHCenter
text: parent.displayText

onTextChanged: text = text.replace(/\s+/g, '')
Keys.onReturnPressed: loginFormSumbitHandler()
}

background: Rectangle {
color: "transparent"
}
}

Expand Down Expand Up @@ -223,12 +249,19 @@ Item {
}
}

function prefixUrlWithProtocol(url) {
if (!url || url.startsWith('http://') || url.startsWith('https://'))
return url;
return 'https://' + url;
}

function loginFormSumbitHandler() {
if (cloudConnection.status == QFieldCloudConnection.LoggedIn) {
cloudConnection.logout();
} else {
cloudConnection.username = usernameField.text;
cloudConnection.password = passwordField.text;
cloudConnection.url = serverUrlField.text !== '' && prefixUrlWithProtocol(serverUrlField.text) !== cloudConnection.defaultUrl ? prefixUrlWithProtocol(serverUrlField.text) : cloudConnection.defaultUrl;
cloudConnection.login();
}
}
Expand Down

0 comments on commit c8d5261

Please sign in to comment.