diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index 99e2d223d..a1b7fc249 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -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 diff --git a/src/gui/systray.cpp b/src/gui/systray.cpp index 5e73f23bb..d34f300bf 100644 --- a/src/gui/systray.cpp +++ b/src/gui/systray.cpp @@ -16,7 +16,7 @@ #include "systray.h" #include "theme.h" #include "config.h" -#include "tray/menumodel.h" +#include "tray/UserModel.h" #include #include @@ -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; diff --git a/src/gui/systray.h b/src/gui/systray.h index 2c817e8d6..2a0e22db7 100644 --- a/src/gui/systray.h +++ b/src/gui/systray.h @@ -19,7 +19,7 @@ #include #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(); diff --git a/src/gui/tray/UserModel.cpp b/src/gui/tray/UserModel.cpp new file mode 100644 index 000000000..841c2e4be --- /dev/null +++ b/src/gui/tray/UserModel.cpp @@ -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 UserModel::roleNames() const +{ + QHash roles; + roles[NameRole] = "name"; + roles[ServerRole] = "server"; + roles[AvatarRole] = "avatar"; + return roles; +} +} \ No newline at end of file diff --git a/src/gui/tray/menumodel.h b/src/gui/tray/UserModel.h similarity index 72% rename from src/gui/tray/menumodel.h rename to src/gui/tray/UserModel.h index b0e557ee7..0e6b381a6 100644 --- a/src/gui/tray/menumodel.h +++ b/src/gui/tray/UserModel.h @@ -4,21 +4,21 @@ #include #include +#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 _users; + User *_currentUser; }; } diff --git a/src/gui/tray/menumodel.cpp b/src/gui/tray/menumodel.cpp deleted file mode 100644 index ebe771058..000000000 --- a/src/gui/tray/menumodel.cpp +++ /dev/null @@ -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 UserModel::roleNames() const -{ - QHash roles; - roles[NameRole] = "name"; - roles[ServerRole] = "server"; - roles[AvatarRole] = "avatar"; - return roles; -} -} \ No newline at end of file diff --git a/src/gui/tray/window.qml b/src/gui/tray/window.qml index a816e3e8f..6d09cc642 100644 --- a/src/gui/tray/window.qml +++ b/src/gui/tray/window.qml @@ -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 }