More stuff regarding tray/UserModel.cpp

Signed-off-by: Dominique Fuchs <32204802+DominiqueFuchs@users.noreply.github.com>
This commit is contained in:
Dominique Fuchs 2019-12-03 18:50:34 +01:00
parent 53abf5a316
commit 4478399282
7 changed files with 124 additions and 143 deletions

View file

@ -108,7 +108,7 @@ set(client_SRCS
elidedlabel.cpp
iconjob.cpp
remotewipe.cpp
tray/menumodel.cpp
tray/UserModel.cpp
creds/credentialsfactory.cpp
creds/httpcredentialsgui.cpp
creds/oauth.cpp

View file

@ -16,7 +16,7 @@
#include "systray.h"
#include "theme.h"
#include "config.h"
#include "tray/menumodel.h"
#include "tray/UserModel.h"
#include <QQmlComponent>
#include <QQmlEngine>
@ -55,7 +55,6 @@ Systray::Systray() // TODO: make singleton, provide ::instance()
slotChangeActivityModel(AccountManager::instance()->accounts().first());
}
//_trayContext->setContextProperty("serverTest", QVariant("Test"));
//connect(AccountManager::instance(), &AccountManager::accountAdded,
// this, &Systray::slotChangeActivityModel);
}
@ -64,46 +63,6 @@ Systray::~Systray()
{
}
Q_INVOKABLE int Systray::numAccounts() const
{
return AccountManager::instance()->accounts().size();
}
// TODO: Lots of memory shifting here
// Probably OK because the avatar is not changing a trillion times per second
// But should consider moving to a generic ImageProvider helper class for img/QML-provision
Q_INVOKABLE QString Systray::currentAvatar() const
{
QByteArray bArray;
QBuffer buffer(&bArray);
buffer.open(QIODevice::WriteOnly);
AvatarJob::makeCircularAvatar(_currentAccount->account()->avatar()).save(&buffer, "PNG");
QString img("data:image/png;base64,");
img.append(QString::fromLatin1(bArray.toBase64().data()));
return img;
}
Q_INVOKABLE QString Systray::currentAccountServer() const
{
QString serverUrl = _currentAccount->account()->url().toString();
if (serverUrl.length() > 25) {
serverUrl.truncate(23);
serverUrl.append(QByteArray("..."));
}
return serverUrl;
}
Q_INVOKABLE QString Systray::currentAccountUser() const
{
QString userName = _currentAccount->account()->davDisplayName();
if (userName.length() > 19) {
userName.truncate(17);
userName.append(QByteArray("..."));
}
return userName;
}
void Systray::slotChangeActivityModel(const AccountStatePtr account)
{
_currentAccount = account;

View file

@ -19,7 +19,7 @@
#include <QQmlContext>
#include "accountmanager.h"
#include "tray/menumodel.h"
#include "tray/UserModel.h"
class QIcon;
@ -48,11 +48,6 @@ public:
void showMessage(const QString &title, const QString &message, MessageIcon icon = Information, int millisecondsTimeoutHint = 10000);
void setToolTip(const QString &tip);
Q_INVOKABLE QString currentAvatar() const;
Q_INVOKABLE QString currentAccountServer() const;
Q_INVOKABLE QString currentAccountUser() const;
Q_INVOKABLE int numAccounts() const;
signals:
void currentUserChanged();

108
src/gui/tray/UserModel.cpp Normal file
View file

@ -0,0 +1,108 @@
#include "accountmanager.h"
#include "UserModel.h"
namespace OCC {
User::User(const AccountStatePtr &account)
: _account(account)
{
}
QString User::name() const
{
return _account->account()->davDisplayName();
}
QString User::server() const
{
QString serverUrl = _account->account()->url().toString();
serverUrl.replace(QLatin1String("https://"), QLatin1String(""));
serverUrl.replace(QLatin1String("http://"), QLatin1String(""));
return serverUrl;
}
// TODO: Lots of memory shifting here
// Probably OK because the avatar is not changing a trillion times per second
// But should consider moving to a generic ImageProvider helper class for img/QML-provision
QString User::avatar() const
{
QByteArray bArray;
QBuffer buffer(&bArray);
buffer.open(QIODevice::WriteOnly);
AvatarJob::makeCircularAvatar(_account->account()->avatar()).save(&buffer, "PNG");
QString img("data:image/png;base64,");
img.append(QString::fromLatin1(bArray.toBase64().data()));
return img;
}
/*-------------------------------------------------------------------------------------*/
UserModel::UserModel(QObject *parent)
: QAbstractListModel()
, _currentUser()
{
for (int i = 0; i < AccountManager::instance()->accounts().size(); i++) {
addUser(User(AccountManager::instance()->accounts().at(i)));
}
_currentUser = &_users.first();
}
UserModel::~UserModel()
{
}
Q_INVOKABLE QString UserModel::currentUserAvatar()
{
return _currentUser->avatar();
}
Q_INVOKABLE QString UserModel::currentUserName()
{
return _currentUser->name();
}
Q_INVOKABLE QString UserModel::currentUserServer()
{
return _currentUser->server();
}
void UserModel::addUser(const User &user)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
_users << user;
endInsertRows();
}
int UserModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return _users.count();
}
QVariant UserModel::data(const QModelIndex &index, int role) const
{
if (index.row() < 0 || index.row() >= _users.count()) {
return QVariant();
}
const User &user = _users[index.row()];
if (role == NameRole) {
return user.name();
} else if (role == ServerRole) {
return user.server();
} else if (role == AvatarRole) {
return user.avatar();
}
return QVariant();
}
QHash<int, QByteArray> UserModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[NameRole] = "name";
roles[ServerRole] = "server";
roles[AvatarRole] = "avatar";
return roles;
}
}

View file

@ -4,21 +4,21 @@
#include <QAbstractListModel>
#include <QStringList>
#include "accountmanager.h"
namespace OCC {
class User
{
public:
User(const QString &name, const QString &server, const QString &avatar);
User(const AccountStatePtr &account);
QString name() const;
QString server() const;
QString avatar() const;
private:
QString _name;
QString _server;
QString _avatar;
AccountStatePtr _account;
};
class UserModel : public QAbstractListModel
@ -35,6 +35,10 @@ public:
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
Q_INVOKABLE QString currentUserAvatar();
Q_INVOKABLE QString currentUserName();
Q_INVOKABLE QString currentUserServer();
enum UserRoles {
NameRole = Qt::UserRole + 1,
ServerRole,
@ -46,6 +50,7 @@ protected:
private:
QList<User> _users;
User *_currentUser;
};
}

View file

@ -1,78 +0,0 @@
#include "accountmanager.h"
#include "menumodel.h"
namespace OCC {
User::User(const QString &name, const QString &server, const QString &avatar)
: _name(name)
, _server(server)
, _avatar(avatar)
{
}
QString User::name() const
{
return _name;
}
QString User::server() const
{
return _server;
}
QString User::avatar() const
{
return _avatar;
}
UserModel::UserModel(QObject *parent)
: QAbstractListModel()
{
for (size_t i = 0; i < AccountManager::instance()->accounts().size(); i++) {
addUser(User("test", "test", "test"));
}
}
UserModel::~UserModel()
{
}
void UserModel::addUser(const User &user)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
_users << user;
endInsertRows();
}
int UserModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return _users.count();
}
QVariant UserModel::data(const QModelIndex &index, int role) const
{
if (index.row() < 0 || index.row() >= _users.count()) {
return QVariant();
}
const User &user = _users[index.row()];
if (role == NameRole) {
return user.name();
} else if (role == ServerRole) {
return user.server();
} else if (role == AvatarRole) {
return user.avatar();
}
return QVariant();
}
QHash<int, QByteArray> UserModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[NameRole] = "name";
roles[ServerRole] = "server";
roles[AvatarRole] = "avatar";
return roles;
}
}

View file

@ -76,17 +76,9 @@ Window {
radius: 4
}
/*ListView {
model: systrayBackend
delegate: UserLine {
text: name
}
}*/
Instantiator {
model: systrayBackend
delegate: UserLine {
}
delegate: UserLine {}
onObjectAdded: accountMenu.insertItem(index, object)
onObjectRemoved: accountMenu.removeItem(object)
}
@ -156,7 +148,7 @@ Window {
height: (trayWindowHeaderBackground.height - 12)
Layout.leftMargin: 6
verticalAlignment: Qt.AlignCenter
source: systrayBackend.currentAvatar()
source: systrayBackend.currentUserAvatar()
Layout.preferredHeight: (trayWindowHeaderBackground.height -12)
Layout.preferredWidth: (trayWindowHeaderBackground.height -12)
}
@ -168,14 +160,14 @@ Window {
Layout.leftMargin: 6
Label {
id: currentAccountUser
text: systrayBackend.currentAccountUser()
text: systrayBackend.currentUserName()
color: "white"
font.pointSize: 9
font.bold: true
}
Label {
id: currentAccountServer
text: systrayBackend.currentAccountServer()
text: systrayBackend.currentUserServer()
color: "white"
font.pointSize: 8
}