Skip to content

Commit

Permalink
* Update user registration code to resize Avatar image when user
Browse files Browse the repository at this point in the history
registered
* Fix a bug that will show just first Avatar image when user re-join
with same name
  • Loading branch information
j-tag committed Apr 26, 2016
1 parent d37e360 commit ea5e3e5
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 28 deletions.
4 changes: 2 additions & 2 deletions ChocalServer.pro
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ SOURCES = *.qml \
*.js
}

VERSION = 1.0.0
VERSION = 1.0.1
VERSION_MAJOR = 1
VERSION_MINOR = 0
VERSION_REVISION = 0
VERSION_REVISION = 1
VERSION_NUMBER = $$VERSION_MAJOR$$VERSION_MINOR$$VERSION_REVISION

DEFINES += VERSION=\\\"$$VERSION\\\"
Expand Down
33 changes: 20 additions & 13 deletions FileIO.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#include "FileIO.hpp"
#include <QCryptographicHash>
#include <QDebug>
#include <QUuid>

FileIO::FileIO()
{
Expand Down Expand Up @@ -41,38 +38,48 @@ bool FileIO::decodeAndWrite(const QString& source, const QString& data)
return true;
}

bool FileIO::setUserAvatar(const QString &name, const QString &data)
bool FileIO::setUserAvatar(const QString &user_key, const QString &data)
{
if(!m_tmpAvatarDir.isValid()) {
return false;
}

QString avatar_path = this->getAvatarPath(name);
QImage image;
QByteArray dataBytes, resultBytes;
QBuffer buffer(&resultBytes, this);
QString avatar_path = this->getAvatarPath(user_key);

return this->decodeAndWrite(avatar_path, data);
dataBytes.append(data);
// Resize image
image.loadFromData(QByteArray::fromBase64(dataBytes));
QImage(image.scaled(128, 128, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation)).save(&buffer, "JPG");
buffer.open(QIODevice::WriteOnly);

return this->decodeAndWrite(avatar_path, resultBytes.toBase64());
}

bool FileIO::hasAvatar(const QString &name)
bool FileIO::hasAvatar(const QString &user_key)
{
return QFile::exists(this->getAvatarPath(name));
return QFile::exists(this->getAvatarPath(user_key));
}

QString FileIO::getAvatarPath(const QString &name)
QString FileIO::getAvatarPath(const QString &user_key)
{
if(!m_tmpAvatarDir.isValid()) {
return 0;
}

if(name == 0) {
if(user_key == 0) {
return this->m_tmpAvatarDir.path();
}

return m_tmpAvatarDir.path().append("/").append(name);
// Note that FileIO::setUserAvatar() method will generate JPG files
return m_tmpAvatarDir.path().append("/").append(user_key).append(".jpg");
}

QUrl FileIO::getAvatarUrl(const QString &name)
QUrl FileIO::getAvatarUrl(const QString &user_key)
{
return QUrl::fromLocalFile(this->getAvatarPath(name));
return QUrl::fromLocalFile(this->getAvatarPath(user_key));
}

QString FileIO::getImagePath(const QString &name)
Expand Down
13 changes: 9 additions & 4 deletions FileIO.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
#include <QTextStream>
#include <QTemporaryDir>
#include <QUrl>
#include <QCryptographicHash>
#include <QImage>
#include <QUuid>
#include <QBuffer>
#include <QDebug>

class FileIO : public QObject
{
Expand All @@ -17,10 +22,10 @@ class FileIO : public QObject
public slots:
bool write(const QString& source, const QString& data);
bool decodeAndWrite(const QString& source, const QString& data);
bool setUserAvatar(const QString& name, const QString& data);
bool hasAvatar(const QString& name);
QString getAvatarPath(const QString& name=0);
QUrl getAvatarUrl(const QString& name);
bool setUserAvatar(const QString& user_key, const QString& data);
bool hasAvatar(const QString& user_key);
QString getAvatarPath(const QString& user_key=0);
QUrl getAvatarUrl(const QString& user_key);
QString getImagePath(const QString& name);
QString decodeImage(const QString& data);
QString getNewUserKey();
Expand Down
2 changes: 1 addition & 1 deletion MessageDelegate.qml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Item {
x: 10

fillMode: Image.PreserveAspectCrop
source: getAvatar(name)
source: getAvatar(getUserKeyByName(name))

// Circle effect
layer.enabled: true
Expand Down
4 changes: 3 additions & 1 deletion Server.qml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ WebSocketServer {
}

onErrorStringChanged: {
appendInfoMessage(qsTr("Server error: %1").arg(errorString))
if(errorString !== "") {
appendInfoMessage(qsTr("Server error: %1").arg(errorString))
}
}

}
Expand Down
2 changes: 1 addition & 1 deletion UserDelegate.qml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Rectangle {
x: 10

fillMode: Image.PreserveAspectCrop
source: main.getAvatar(name)
source: main.getAvatar(user_key)

// Circle effect
layer.enabled: true
Expand Down
29 changes: 23 additions & 6 deletions main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ ApplicationWindow {

// Set user Avatar
if(typeof json.image !== "undefined") {
fileio.setUserAvatar(json.name, json.image);
fileio.setUserAvatar(user_key, json.image);
}

// Everything is ok, so add user
Expand Down Expand Up @@ -468,15 +468,15 @@ ApplicationWindow {
return true
}

// Get avatar path by user name
function getAvatar(name) {
// Get avatar path by user key
function getAvatar(user_key) {

if(name === undefined || name === null || name === ""
|| name === getUserName("SYSTEM") || !fileio.hasAvatar(name)) {
if(typeof user_key === "undefined" || user_key === null || user_key === ""
|| !fileio.hasAvatar(user_key)) {
return "qrc:/img/img/no-avatar.png"
}

return fileio.getAvatarUrl(name);
return fileio.getAvatarUrl(user_key);
}

// Get user name by its user key
Expand All @@ -488,6 +488,23 @@ ApplicationWindow {
return userModel.get(user_keys_index[user_key]).name
}

// Get user key by its name
function getUserKeyByName(name) {
if(name === "Server") {
return qsTr("SYSTEM")
}

for(var i = 0; i < userModel.count; ++i) {
var user = userModel.get(i);

if(user.name === name) {
return user.user_key
}
}

return false
}

// Check to see if user key is valid or not
function isValidUserKey(user_key) {
return typeof user_keys_index[user_key] !== 'undefined'
Expand Down

0 comments on commit ea5e3e5

Please sign in to comment.