Commit 99180164 authored by Dennis Beier's avatar Dennis Beier
Browse files

serverchoice works now

parent af58d00c
Subproject commit f9c8a18139d7fca63f68047119c9c5ff865396d9
Subproject commit ab62aa0f6e69a988dfad0fa85e1bf9a4b713d9fc
......@@ -2,10 +2,10 @@ QT += qml quick core network websockets sql quickcontrols2 multimedia svg xml co
SOURCES += \
main.cpp \
serverselector.cpp
models/serverchoicemodel.cpp
HEADERS += config.h \
serverselector.h
models/serverchoicemodel.h
RESOURCES += qml.qrc\
fonts.qrc\
......
......@@ -13,6 +13,7 @@
#include <QQuickStyle>
#include <QDebug>
#include <QtGlobal>
#include <models/serverchoicemodel.h>
#include "CustomModels/channelmodel.h"
#include "config.h"
......@@ -24,7 +25,6 @@
#include "CustomModels/usermodel.h"
#include "CustomModels/messagemodel.h"
#include "utils.h"
#include "serverselector.h"
#ifdef Q_OS_ANDROID
#include <signal.h>
......@@ -52,12 +52,13 @@ int main( int argc, char *argv[] )
ChannelModel channelModel;
ChannelModel groupsModel;
MessagesModel messageModel;
ServerChoiceModel serverChoices;
channelModel.moveToThread(QThread::currentThread());
groupsModel.moveToThread(QThread::currentThread());
userModel.moveToThread(QThread::currentThread());
messageModel.moveToThread(QThread::currentThread());
serverChoices.moveToThread(QThread::currentThread());
qDebug()<<"main thread: "<<QThread::currentThreadId();
int result = EXIT_FAILURE;
......@@ -102,15 +103,8 @@ int main( int argc, char *argv[] )
}
QQmlApplicationEngine engine;
QSharedPointer<RocketChatServerData> server(
new RocketChatServerData( "testUcomServer", SERVERADRESS,
"https://" + QString( SERVERADRESS ) + "/api/v1",
userModel, channelModel, groupsModel, groupsModel,messageModel )
);
RocketChat rocketChat( &app );
ServerSelector serverSelector(server);
QObject::connect(&serverSelector,&ServerSelector::serverSelected,&rocketChat,&RocketChat::registerServer);
RocketChat rocketChat( &app, channelModel,groupsModel,groupsModel,messageModel, userModel );
rocketChat.moveToThread(QThread::currentThread());
//rocketChat.registerServer( server );
QQmlContext *context = engine.rootContext();
......@@ -119,7 +113,6 @@ int main( int argc, char *argv[] )
QString buildDate(__DATE__ );
QString serverAdress(SERVERADRESS);
context->setContextProperty( "rocketChatController", &rocketChat );
context->setContextProperty( "serverSelector", &serverSelector );
#ifdef QT_DEBUG
context->setContextProperty( "debug", QVariant(true) );
#else
......@@ -135,6 +128,7 @@ int main( int argc, char *argv[] )
context->setContextProperty("serverAdress",QVariant(serverAdress));
context->setContextProperty("pathPrefix", Utils::getPathPrefix());
context->setContextProperty("cmessagesModel", &messageModel);
context->setContextProperty("serverChoices", &serverChoices);
engine.load( QUrl( QLatin1String( "qrc:/qml/main.qml" ) ) );
......
#include "serverchoicemodel.h"
ServerChoiceModel::ServerChoiceModel()
{
QString directory = QStandardPaths::writableLocation( QStandardPaths::AppLocalDataLocation );
QString filePath = directory + "/server.sqlite";
qDebug() << "SQLITE: " << filePath;
mDb.setDatabaseName( filePath );
QDir dbPath( directory );
if ( !dbPath.exists() ) {
dbPath.mkpath( directory );
}
QFile file( filePath );
if ( !file.exists() ) {
file.open( QIODevice::ReadWrite );
file.close();
}
if ( !mDb.open() ) {
qDebug() << mDb.lastError().text() + "file used:" + filePath;
throw std::runtime_error( mDb.lastError().text().toStdString() + "file used:" + filePath.toStdString() );
}
QSqlQuery query(mDb);
query.prepare( "CREATE TABLE IF NOT EXISTS server_choice "
"(id integer primary key,"
"hostname varchar(40),"
"description varchar(40))" );
if ( !query.exec() ) {
qWarning() <<"db error"<< query.lastError();
}
}
QStringList ServerChoiceModel::getDomainList() const
{
QStringList domains;
QSqlQuery domainQuery( mDb );
domainQuery.prepare( "Select * from server_choice" );
if ( !domainQuery.exec() ) {
qWarning() << domainQuery.lastError();
} else {
QSqlRecord rec = domainQuery.record();
int hostNameCol = rec.indexOf("hostname");
while ( domainQuery.next() ) {
QString domain = domainQuery.value( hostNameCol ).toString();
domains.append(domain);
}
}
domains.push_front("chat.fairkom.net");
return domains;
}
void ServerChoiceModel::addDomain(QString value)
{
QSqlQuery domainQuery( mDb );
beginResetModel();
domainQuery.prepare("INSERT INTO server_choice (hostname) VALUES(?)");
domainQuery.addBindValue(value);
if ( !domainQuery.exec() ) {
qWarning() <<"db error"<< domainQuery.lastError();
}
endResetModel();
}
void ServerChoiceModel::removeDomain(QString value)
{
QSqlQuery domainQuery( mDb );
beginResetModel();
domainQuery.prepare("DELETE FROM server_choice where hostname=?");
domainQuery.addBindValue(value);
domainQuery.exec();
endResetModel();
}
int ServerChoiceModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return getDomainList().count();
}
QVariant ServerChoiceModel::data(const QModelIndex &index, int role) const
{
if(role == 1)
return getDomainList()[index.row()];
return QVariant();
}
QHash<int, QByteArray> ServerChoiceModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[1] = "domain";
return roles;
}
QString ServerChoiceModel::getCurrentServer() const
{
return currentServer;
}
void ServerChoiceModel::setCurrentServer(const QString &value)
{
currentServer = value;
}
#ifndef SERVERCHOICEMODEL_H
#define SERVERCHOICEMODEL_H
#include <QDebug>
#include <QDir>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QSqlRecord>
#include <QStandardPaths>
#include <QDebug>
#include <exception>
#include <QSqlError>
#include <QIODevice>
#include <QTime>
#include <QDateTime>
#include <QMimeType>
#include <QSettings>
#include <QObject>
#include <QMetaObject>
#include <QtSql/QSqlDatabase>
#include <QFile>
#include <QMimeDatabase>
#include <QMimeType>
#include <QJsonDocument>
#include <QMap>
#include <QJsonObject>
#include <QAbstractListModel>
class ServerChoiceModel : public QAbstractListModel
{
Q_OBJECT
QSqlDatabase mDb = QSqlDatabase::addDatabase( "QSQLITE" ,"serverConnection");
public:
Q_PROPERTY(QString currentServer READ getCurrentServer WRITE setCurrentServer NOTIFY currentServerChanged)
ServerChoiceModel(void);
Q_INVOKABLE QStringList getDomainList(void) const;
Q_INVOKABLE void addDomain(QString);
Q_INVOKABLE void removeDomain(QString);
int rowCount( const QModelIndex &parent = QModelIndex() ) const;
QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const;
QString getCurrentServer() const;
void setCurrentServer(const QString &value);
signals:
void currentServerChanged(void);
protected:
QHash<int, QByteArray> roleNames() const;
QString currentServer;
};
#endif // SERVERCHOICEMODEL_H
......@@ -70,7 +70,7 @@ ApplicationWindow {
property bool mainViewPushed: false
property bool channelPushed: false
id: mainStack
initialItem: serverSelectionComponent
initialItem: loginComponent
function reset() {
rocketChatController.setUsername("default","")
rocketChatController.setPassword("default","")
......@@ -94,6 +94,9 @@ ApplicationWindow {
rocketChatController.setCurrentChannel("default","","")
}
}
function toServerChoice(){
mainStack.push("qrc:/qml/pages/ServerSelectionPage.qml");
}
pushEnter: Transition {
}
pushExit: Transition {
......
import QtQuick 2.8
import QtQuick.Controls 2.1
import QtQuick.Layouts 1.3
import Qt.labs.settings 1.0
import "qrc:/qml/style"
......@@ -9,42 +11,50 @@ import "qrc:/javascript/print.js" as Print
Item {
id: loginPage
// KDAB_TODO: This should be bound to a property of the RocketChat class
property bool showLoginForm: false
property string currentServer;
Settings{
id:currentServerSetting
property alias currentServerChoice: currentServer.text
Component.onCompleted: {
var current =serverChoices.currentServer;
if(serverChoices.currentServer){
currentServerChoice = serverChoices.currentServer;
}
rocketChatController.newServerByDomain(currentServerChoice)
showLoginForm = false
connectionIndicator.visible = true
}
}
Connections{
target: mainStack
onCurrentItemChanged:{
var current =serverChoices.currentServer;
if(serverChoices.currentServer)
currentServerSetting.currentServerChoice = serverChoices.currentServer;
}
}
function login() {
rocketChatController.login("default", loginUsername.text,
loginPassword.text)
showLoginForm = false
connectionIndicator.visible = true
loginStatus.text = ""
switchServerButton.visible = false
}
function checkConnectionState() {
console.log("Checking connection state")
loginPage.showLoginForm = rocketChatController.getDdpConnectionEstablished();
loginPage.showLoginForm = rocketChatController.getDdpConnectionEstablished()
connectionIndicator.visible = false
}
Column {
anchors.centerIn: parent
width: 400
height: 400
height: 500
spacing: 21
Image {
mipmap: true
width: 120
smooth: true
anchors.horizontalCenter: parent.horizontalCenter
fillMode: Image.PreserveAspectFit
source: "qrc:/res/images/ucom_logo_square_trans.svg"
}
Column {
//anchors.verticalCenter: parent.verticalCenter
......@@ -54,7 +64,33 @@ Item {
anchors.rightMargin: 65
spacing: 40
width: 340
height: 190
height: 500
Rectangle {
height: 28
anchors.left: parent.left
anchors.right: parent.right
color :"transparent"
Text {
id: currentServer
text: "demo.rocketchat.com"
anchors.left: parent.left
color: Colors.white
font.pointSize: Fonts.largeFontSize
}
Button{
anchors.right: parent.right
width: 28
height: 28
flat: true
StdImageHighDPI {
anchors.fill: parent
source: "qrc:/res/buttons/navigation/tabbar-settings.svg"
}
onClicked:{
mainStack.toServerChoice();
}
}
}
Text {
id: connectStatus
......@@ -66,6 +102,7 @@ Item {
return "not connected"
}
}
TextField {
id: loginUsername
width: parent.width
......@@ -76,11 +113,11 @@ Item {
rightPadding: 5
bottomPadding: 5
horizontalAlignment: TextField.AlignLeft
verticalAlignment: TextField.AlignBottom
verticalAlignment: TextField.AlignBottom
selectByMouse: true
text: ""
inputMethodHints: Qt.ImhNoAutoUppercase | Qt.ImhNoPredictiveText|
Qt.ImhPreferLowercase
inputMethodHints: Qt.ImhNoAutoUppercase | Qt.ImhNoPredictiveText
| Qt.ImhPreferLowercase
focus: true
placeholderText: qsTr("username or e-mail")
background: Rectangle {
......@@ -88,7 +125,6 @@ Item {
implicitWidth: parent.width
implicitHeight: 30
color: "transparent"
Rectangle {
height: 1
......@@ -97,7 +133,6 @@ Item {
anchors.bottom: parent.bottom
}
}
}
TextField {
......@@ -111,10 +146,9 @@ Item {
rightPadding: 5
bottomPadding: 5
horizontalAlignment: TextField.AlignLeft
verticalAlignment: TextField.AlignBottom
verticalAlignment: TextField.AlignBottom
inputMethodHints: Qt.ImhNoAutoUppercase | Qt.ImhNoPredictiveText
| Qt.ImhSensitiveData |
Qt.ImhPreferLowercase
| Qt.ImhSensitiveData | Qt.ImhPreferLowercase
echoMode: TextInput.Password
selectByMouse: true
......@@ -133,8 +167,6 @@ Item {
anchors.bottom: parent.bottom
}
}
}
StdButton {
......@@ -148,20 +180,6 @@ Item {
color: Colors.white
textColor: Colors.primary
}
StdButton {
id: switchServerButton
anchors.left: parent.left
anchors.right: parent.right
text: qsTr("switch Server")
checked: false
checkable: false
color: Colors.white
textColor: Colors.primary
onPressed: {
mainStack.pop()
}
}
BusyIndicator {
id: connectionIndicator
visible: true
......@@ -175,7 +193,7 @@ Item {
}
// KDAB_TODO: This should all be done differently
Connections{
Connections {
target: rocketChatController
onServerReady: checkConnectionState()
}
......@@ -183,7 +201,6 @@ Item {
checkConnectionState()
}
}
}
Connections {
target: loginPassword
......@@ -206,11 +223,11 @@ Item {
target: rocketChatController
onLoggedIn: {
Print.log("logged in")
//if (sessionCheckbox.checked)
//if (sessionCheckbox.checked)
mainStack.toMainView()
}
onHashLoggedIn:{
onHashLoggedIn: {
welcomePopup.open()
}
......@@ -219,30 +236,30 @@ Item {
loginStatus.text = qsTr("username or password wrong")
connectionIndicator.visible = false
loginPage.showLoginForm = true
switchServerButton.visible = true
}
onServerConnected: {
connectStatus.text = "connected"
if (rocketChatController.getPassword("default") != "" && rocketChatController.getUsername("default") != "") {
rocketChatController.login("default", rocketChatController.getUsername("default"),
rocketChatController.getPassword("default"))
if (rocketChatController.getPassword("default") != ""
&& rocketChatController.getUsername("default") != "") {
rocketChatController.login(
"default",
rocketChatController.getUsername("default"),
rocketChatController.getPassword("default"))
mainStack.toMainView()
} else {
loginPage.showLoginForm = true
}
}
onStorageReady:{
setUserData();
onStorageReady: {
setUserData()
}
}
function setUserData(){
function setUserData() {
var username = rocketChatController.getUsername("default")
var password = rocketChatController.getPassword("default")
if(username != "" && password != ""){
if (username !== "" && password !== "") {
loginPassword.text = password
loginUsername.text = username
mainStack.toMainView()
......@@ -250,9 +267,20 @@ Item {
}
Component.onCompleted: {
rocketChatController.setStatusBarColor(Colors.secondary)
if(rocketChatController.isStorageReady()){
setUserData();
if (rocketChatController.isStorageReady()) {
setUserData()
}
/*var username = rocketChatController.getPasswordFromDb("default")
var password = rocketChatController.getUsernameFromDb("default")
if (username !== "" && password !== "") {
mainStack.toMainView()
}*/
}
Timer{
running: false;
repeat: false;
onTriggered: {
rocketChatController.cur
}
}
}
......@@ -13,10 +13,6 @@ Page {
id:deletePopup
}
ListModel{
id:serverListModel
}
ListView {
anchors.bottom: addServerButton.top
anchors.bottomMargin: 25
......@@ -26,21 +22,15 @@ Page {
spacing: 1
clip:true
id: serverListView
model:serverListModel
model:serverChoices
delegate: MouseArea {
//color: Colors.grey
height: 58
anchors.left: parent.left
anchors.right: parent.right
onPressAndHold: {
console.log("test")
deletePopup.x = mouse.x
deletePopup.y = parent.y
deletePopup.open()
}
onPressed: {
serverSelector.selectServer(model.address)
serverChoices.currentServer = model.domain;
mainStack.push("qrc:/qml/pages/LoginPage.qml")
}
......@@ -66,7 +56,7 @@ Page {
Text {
id: serverName
text: model.name
text: model.domain
font.bold: true
textFormat: Text.PlainText
font.family: Fonts.opensans.name
......@@ -76,6 +66,16 @@ Page {
}
}
}
Button{
text:qsTr("delete")
onClicked: {
serverChoices.removeDomain(model.domain);
}
anchors.right: parent.right
flat: true
anchors.verticalCenter: parent.verticalCenter
visible: model.domain === "chat.fairkom.com"
}
}
Rectangle {
anchors.bottom: parent.bottom
......@@ -101,15 +101,4 @@ Page {
addServerEntry.open()
}
}
Connections{
target: serverSelector
onNewServerList:{
serverListModel.clear()
serverListModel.append(list)
}
}
Component.onCompleted: {
var list = serverSelector.getServerList()
serverListModel.append(list)
}
}
......@@ -80,7 +80,7 @@ Popup {
StdButton{
text: qsTr("add")
onPressed: {