Skip to content

Commit

Permalink
Listing private servers from a json file (close #1347)
Browse files Browse the repository at this point in the history
JamTaba will load the private servers data saved in a
private_server.json file.
  • Loading branch information
elieserdejesus committed Jun 29, 2020
1 parent 77bfc03 commit b94bb28
Show file tree
Hide file tree
Showing 12 changed files with 181 additions and 10 deletions.
27 changes: 27 additions & 0 deletions src/Common/Configurator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const QString Configurator::CACHE_FOLDER_NAME = "Cache";
const QString Configurator::LOG_CONFIG_FILE_NAME = "logging.ini";
const QString Configurator::THEMES_FOLDER_NAME = "Themes";
const QString Configurator::THEMES_FOLDER_IN_RESOURCES = ":/css/themes";
const QString Configurator::PRIVATE_SERVERS_FILE_NAME = "private_servers.json";

// from https://sites.google.com/a/embeddedlab.org/community/technical-articles/qt/qt-posts/howtodocoloredloggingusingqtdebug
#define COLOR_DEBUG "\033[35;1m"
Expand All @@ -48,6 +49,11 @@ QString Configurator::getDebugColor(const QMessageLogContext &context)
return COLOR_DEBUG;
}

QString Configurator::getPrivateServersFilePath() const
{
return getBaseDir().filePath(PRIVATE_SERVERS_FILE_NAME);
}

void Configurator::logHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
QByteArray localMsg = msg.toLocal8Bit();
Expand Down Expand Up @@ -205,6 +211,8 @@ bool Configurator::setUp()

exportLogIniFile(); // copy log config file from resources to user hard disk

exportPrivateServersExampleFile();

setupLogConfigFile();

std::set_terminate(Configurator::terminateHandler);
Expand Down Expand Up @@ -394,6 +402,25 @@ void Configurator::exportLogIniFile()
}
}

void Configurator::exportPrivateServersExampleFile()
{
QString privateServerFileExample = "private_servers_example.json";
QString privateServersExampleFilePath(baseDir.absoluteFilePath(privateServerFileExample));

QFile file(privateServersExampleFilePath);
if (!file.exists()) {
qDebug(jtConfigurator) << "Private servers example file file don't exist in' :" << privateServersExampleFilePath;

// copy the example file from resources to 'filePath'
QFile file(":/" + privateServerFileExample);
bool result = file.copy(privateServersExampleFilePath);
if (result)
qDebug(jtConfigurator) << "Private servers example file copied in :" << privateServersExampleFilePath;
else
qDebug(jtConfigurator) << "FAILED to copy the Private servers example file in :" << privateServersExampleFilePath << "Error:" << file.errorString();
}
}

QString Configurator::getPresetPath(const QString &presetFileName)
{
if (!presetsDir.exists()) {
Expand Down
4 changes: 4 additions & 0 deletions src/Common/Configurator.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class Configurator
QDir getBaseDir() const;
QDir getThemesDir() const;

QString getPrivateServersFilePath() const;

// Presets
QString getPresetPath(const QString &JsonFile); // used by Settings
QStringList getPresetFilesNames(bool fullpath); // all the files names in a folder
Expand All @@ -48,6 +50,7 @@ class Configurator
static const QString PRESETS_FOLDER_NAME;
static const QString CACHE_FOLDER_NAME;
static const QString LOG_CONFIG_FILE_NAME;
static const QString PRIVATE_SERVERS_FILE_NAME;
static const QString THEMES_FOLDER_NAME;
static const QString THEMES_FOLDER_IN_RESOURCES;

Expand All @@ -71,6 +74,7 @@ class Configurator
void createFoldersTree();
void initializeDirs(); // this function is implemented in ConfiguratorStandalone.cpp and in ConfiguratorVST.cpp. Only the correct .cpp file is included in .pro files.
void exportLogIniFile();
void exportPrivateServersExampleFile();
void setupLogConfigFile();

void exportThemes() const;
Expand Down
10 changes: 7 additions & 3 deletions src/Common/MainController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1019,10 +1019,14 @@ void MainController::tryConnectInNinjamServer(const login::RoomInfo &ninjamRoom,
QString serverIp = ninjamRoom.getName();
int serverPort = ninjamRoom.getPort();
QString userName = getUserName();
QString pass = (password.isNull() || password.isEmpty()) ? "" : password;
QString userPass = (password.isNull() || password.isEmpty()) ? "" : password;

this->ninjamService->startServerConnection(serverIp, serverPort, userName, channels,
pass);
if (ninjamRoom.hasPreferredUserCredentials()) {
userName = ninjamRoom.getPreferredUserName();
userPass = ninjamRoom.getPreferredUserPass();
}

this->ninjamService->startServerConnection(serverIp, serverPort, userName, channels, userPass);
} else {
qCritical() << "user name not choosed yet!";
}
Expand Down
27 changes: 26 additions & 1 deletion src/Common/gui/JamRoomViewPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <QDebug>
#include <QButtonGroup>
#include <QPainter>

#include "MainController.h"
#include "ninjam/client/User.h"
Expand Down Expand Up @@ -128,6 +129,27 @@ void JamRoomViewPanel::updateUserLocation(const QString &userIP)

}

void JamRoomViewPanel::paintEvent(QPaintEvent *ev)
{
QFrame::paintEvent(ev);

if (getRoomInfo().isPrivateServer()) {
QPainter painter(this);
painter.setPen(QPen(QBrush(Qt::black), 1.0));
painter.drawRect(0, 0, width()-1, height()-1);
}
}

QSize JamRoomViewPanel::sizeHint() const
{
auto hint = QFrame::sizeHint();

if (roomInfo.isPrivateServer())
hint.setHeight(60);

return hint;
}

void JamRoomViewPanel::changeEvent(QEvent *e)
{
if (e->type() == QEvent::LanguageChange){
Expand Down Expand Up @@ -197,10 +219,13 @@ void JamRoomViewPanel::refresh(const login::RoomInfo &roomInfo)

updateMap();

setProperty("empty", roomInfo.isEmpty());
setProperty("empty", roomInfo.isEmpty() && !roomInfo.isPrivateServer());

updateStyleSheet();

if (roomInfo.isPrivateServer())
ui->labelRoomStatus->setText(tr("Private Server"));

}

void JamRoomViewPanel::updateStyleSheet()
Expand Down
2 changes: 2 additions & 0 deletions src/Common/gui/JamRoomViewPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class JamRoomViewPanel : public QFrame

protected:
void changeEvent(QEvent *) override;
void paintEvent(QPaintEvent *) override;
QSize sizeHint() const override;

private slots:
void toggleRoomListening();
Expand Down
2 changes: 1 addition & 1 deletion src/Common/gui/JamRoomViewPanel.ui
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
<item row="0" column="0">
<widget class="QLabel" name="labelName">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Maximum">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
Expand Down
68 changes: 65 additions & 3 deletions src/Common/gui/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,9 @@ void MainWindow::centerBusyDialog()

bool MainWindow::jamRoomLessThan(const login::RoomInfo &r1, const login::RoomInfo &r2)
{
if( r1.isPrivateServer())
return true;

return r1.getNonBotUsersCount() > r2.getNonBotUsersCount();
}

Expand Down Expand Up @@ -1187,16 +1190,72 @@ bool MainWindow::canUseTwoColumnLayoutInPublicRooms() const
return ui.contentTabWidget->width() >= 860;
}

QList<login::RoomInfo> MainWindow::loadPrivateServersFromJson(const QFileInfo &privateServersFile)
{
QList<login::RoomInfo> privateServers;

QFile jsonFile(privateServersFile.absoluteFilePath());

if (jsonFile.exists() && jsonFile.open(QFile::ReadOnly)) {
auto root = QJsonDocument::fromJson(jsonFile.readAll()).object();
if (root.contains("servers")) {
auto serversArray = root["servers"].toArray();
for (int i = 0; i < serversArray.size(); ++i) {
auto serverJson = serversArray[i].toObject();
if (serverJson.contains("server_name") && serverJson.contains("server_max_users")) {
auto serverName = serverJson["server_name"].toString("Error");
auto serverMaxUsers = serverJson["server_max_users"].toInt(8);

auto indexOfSeparator = serverName.indexOf(":");
if (indexOfSeparator > 0) {
auto name = serverName.left(indexOfSeparator);
auto port = serverName.right(serverName.length() - (indexOfSeparator + 1)).toInt();
auto userName = serverJson["user_name"].toString(mainController->getUserName());
auto userPass = serverJson["user_pass"].toString(QString());
RoomInfo privateServer(name, port, serverMaxUsers);
privateServer.setPreferredUserCredentials(userName, userPass);
privateServers.append(privateServer);
}
}
}
}
}
else {
if (!jsonFile.exists()) {
qDebug() << "The private servers json file not found!";
}
else {
qDebug() << "Error opening private server json file: " << jsonFile.errorString();
}
}

return privateServers;
}

void MainWindow::refreshPublicRoomsList(const QList<login::RoomInfo> &publicRooms)
{
if (publicRooms.isEmpty())
auto privateServersFilePath = Configurator::getInstance()->getPrivateServersFilePath();

QFileInfo privateServersFileInfo(privateServersFilePath);

if (publicRooms.isEmpty() && !privateServersFileInfo.exists())
return;

hideBusyDialog();

QList<login::RoomInfo> sortedRooms(publicRooms);
qSort(sortedRooms.begin(), sortedRooms.end(), jamRoomLessThan);

// put the private servers (if available) at the first positions
int totalPrivateServers = 0;
if (privateServersFileInfo.exists()) {
auto privateServers = loadPrivateServersFromJson(privateServersFileInfo);
for (int i = 0; i < privateServers.size(); ++i) {
sortedRooms.insert(0, privateServers[i]);
}
totalPrivateServers = privateServers.size();
}

int index = 0;
bool twoCollumns = canUseTwoColumnLayoutInPublicRooms();
for (const auto &roomInfo : sortedRooms) {
Expand All @@ -1218,6 +1277,7 @@ void MainWindow::refreshPublicRoomsList(const QList<login::RoomInfo> &publicRoom
}
auto layout = dynamic_cast<QGridLayout *>(ui.allRoomsContent->layout());
layout->addWidget(roomViewPanel, rowIndex, collumnIndex);

index++;
}

Expand Down Expand Up @@ -2534,8 +2594,10 @@ void MainWindow::updatePublicRoomsListLayout()
{
QList<login::RoomInfo> roomInfos;

for (auto roomView : roomViewPanels)
roomInfos.append(roomView->getRoomInfo());
for (auto roomView : roomViewPanels) {
if (!roomView->getRoomInfo().isPrivateServer())
roomInfos.append(roomView->getRoomInfo());
}

refreshPublicRoomsList(roomInfos);
}
Expand Down
3 changes: 3 additions & 0 deletions src/Common/gui/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <QCameraInfo>
#include <QVideoFrame>
#include <QComboBox>
#include <QFileInfo>

class PreferencesDialog;
class LocalTrackView;
Expand Down Expand Up @@ -376,6 +377,8 @@ private slots:

void wireNinjamSignals();

QList<login::RoomInfo> loadPrivateServersFromJson(const QFileInfo &privateServersFile);

int timerID; // timer used to refresh the entire GUI: animations, peak meters, etc
static const quint8 DEFAULT_REFRESH_RATE;
static const quint8 MAX_REFRESH_RATE;
Expand Down
16 changes: 14 additions & 2 deletions src/Common/loginserver/LoginService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,27 @@ RoomInfo::RoomInfo(const QString &roomName, int roomPort,
users(users),
bpi(bpi),
bpm(bpm),
streamUrl(streamUrl)
streamUrl(streamUrl),
isPrivate(false)
{
//
}

RoomInfo::RoomInfo(const QString &roomName, int roomPort, int maxUsers, int maxChannels) :
RoomInfo(roomName, roomPort, maxUsers, QList<login::UserInfo>(), maxChannels, 0, 0, "")
{
//
isPrivate = true;
}

void RoomInfo::setPreferredUserCredentials(const QString userName, const QString userPass)
{
userCredentials.name = userName;
userCredentials.pass = userPass;
}

bool RoomInfo::hasPreferredUserCredentials() const
{
return !userCredentials.name.isEmpty();
}

QString RoomInfo::getUniqueName() const {
Expand Down
17 changes: 17 additions & 0 deletions src/Common/loginserver/LoginService.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class RoomInfo

bool isEmpty() const;
bool isFull() const;
bool isPrivateServer() const { return isPrivate; }

int getPort() const;

Expand All @@ -71,6 +72,13 @@ class RoomInfo

QString getUniqueName() const;

void setPreferredUserCredentials(const QString userName, const QString userPass = QString());

bool hasPreferredUserCredentials() const;

QString getPreferredUserName() const { return userCredentials.name; }
QString getPreferredUserPass() const { return userCredentials.pass; }

protected:

QString name;
Expand All @@ -86,6 +94,15 @@ class RoomInfo

int bpi;
int bpm;

struct UserCredentials {
QString name;
QString pass;
};

UserCredentials userCredentials;

bool isPrivate;
};

inline bool RoomInfo::isFull() const
Expand Down
1 change: 1 addition & 0 deletions src/resources/jamtaba.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -1366,6 +1366,7 @@
<file>instruments/saxophone.png</file>
<file>instruments/flute.png</file>
<file>instruments/trombone.png</file>
<file>private_servers_example.json</file>
</qresource>
<qresource prefix="/tiles">
<file>map/0/0/0.png</file>
Expand Down
14 changes: 14 additions & 0 deletions src/resources/private_servers_example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"servers": [
{
"server_name": "example_server.com:2049",
"server_max_users": "4"
},
{
"server_name": "another_example_server.com:2050",
"server_max_users": "4",
"user_name": "my cool name",
"user_pass": "1234"
}
]
}

0 comments on commit b94bb28

Please sign in to comment.