2013-03-20 13:03:49 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) by Cédric Bellegarde <gnumdk@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
|
2019-12-02 16:10:18 +03:00
|
|
|
#include "accountmanager.h"
|
2013-03-20 13:03:49 +04:00
|
|
|
#include "systray.h"
|
2014-07-11 02:31:24 +04:00
|
|
|
#include "theme.h"
|
2017-03-27 12:12:28 +03:00
|
|
|
#include "config.h"
|
2019-12-03 19:25:11 +03:00
|
|
|
#include "tray/menumodel.h"
|
2013-03-20 13:03:49 +04:00
|
|
|
|
2019-12-02 16:10:18 +03:00
|
|
|
#include <QQmlComponent>
|
|
|
|
#include <QQmlEngine>
|
|
|
|
|
2013-03-22 14:34:21 +04:00
|
|
|
#ifdef USE_FDO_NOTIFICATIONS
|
2013-03-20 13:03:49 +04:00
|
|
|
#include <QDBusConnection>
|
2013-03-22 14:58:55 +04:00
|
|
|
#include <QDBusInterface>
|
2013-03-20 13:03:49 +04:00
|
|
|
#include <QDBusMessage>
|
|
|
|
#include <QDBusPendingCall>
|
2013-03-22 14:58:55 +04:00
|
|
|
#define NOTIFICATIONS_SERVICE "org.freedesktop.Notifications"
|
|
|
|
#define NOTIFICATIONS_PATH "/org/freedesktop/Notifications"
|
|
|
|
#define NOTIFICATIONS_IFACE "org.freedesktop.Notifications"
|
2013-03-20 13:03:49 +04:00
|
|
|
#endif
|
|
|
|
|
2014-11-10 00:34:07 +03:00
|
|
|
namespace OCC {
|
2013-10-01 15:51:40 +04:00
|
|
|
|
2019-12-02 16:10:18 +03:00
|
|
|
Systray::Systray() // TODO: make singleton, provide ::instance()
|
|
|
|
: _currentAccount(nullptr)
|
|
|
|
, _trayContext(nullptr)
|
2019-12-03 19:25:11 +03:00
|
|
|
, _accountMenuModel(nullptr)
|
2019-12-02 16:10:18 +03:00
|
|
|
{
|
2019-12-03 00:45:14 +03:00
|
|
|
// Create QML tray engine, build component, set C++ backend context used in window.qml
|
2019-12-02 16:10:18 +03:00
|
|
|
QQmlEngine *engine = new QQmlEngine;
|
2019-12-03 00:45:14 +03:00
|
|
|
QQmlComponent systray(engine, QUrl(QStringLiteral("qrc:/qml/src/gui/tray/init.qml")));
|
2019-12-02 16:10:18 +03:00
|
|
|
_trayContext = engine->contextForObject(systray.create());
|
|
|
|
|
2019-12-03 19:25:11 +03:00
|
|
|
_accountMenuModel = new UserModel();
|
|
|
|
systray.engine()->rootContext()->setContextProperty("systrayBackend", _accountMenuModel);
|
2019-12-03 00:45:14 +03:00
|
|
|
|
2019-12-02 16:10:18 +03:00
|
|
|
// TODO: hack to pass the icon to QML
|
|
|
|
//ctxt->setContextProperty("theme", QLatin1String("colored"));
|
|
|
|
//ctxt->setContextProperty("filename", "state-offline");
|
2019-12-03 00:45:14 +03:00
|
|
|
|
|
|
|
if (!AccountManager::instance()->accounts().isEmpty()) {
|
|
|
|
|
|
|
|
slotChangeActivityModel(AccountManager::instance()->accounts().first());
|
|
|
|
}
|
|
|
|
|
|
|
|
//_trayContext->setContextProperty("serverTest", QVariant("Test"));
|
|
|
|
//connect(AccountManager::instance(), &AccountManager::accountAdded,
|
|
|
|
// this, &Systray::slotChangeActivityModel);
|
2019-12-02 16:10:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Systray::~Systray()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-12-03 19:25:11 +03:00
|
|
|
Q_INVOKABLE int Systray::numAccounts() const
|
|
|
|
{
|
|
|
|
return AccountManager::instance()->accounts().size();
|
|
|
|
}
|
|
|
|
|
2019-12-03 09:24:59 +03:00
|
|
|
// 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
|
2019-12-03 00:45:14 +03:00
|
|
|
{
|
2019-12-03 09:24:59 +03:00
|
|
|
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;
|
2019-12-03 00:45:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2019-12-02 16:10:18 +03:00
|
|
|
{
|
2019-12-03 00:45:14 +03:00
|
|
|
_currentAccount = account;
|
|
|
|
emit currentUserChanged();
|
2019-12-02 16:10:18 +03:00
|
|
|
}
|
|
|
|
|
2013-03-20 13:03:49 +04:00
|
|
|
void Systray::showMessage(const QString &title, const QString &message, MessageIcon icon, int millisecondsTimeoutHint)
|
|
|
|
{
|
2013-03-22 14:34:21 +04:00
|
|
|
#ifdef USE_FDO_NOTIFICATIONS
|
2019-11-29 19:06:35 +03:00
|
|
|
if (QDBusInterface(NOTIFICATIONS_SERVICE, NOTIFICATIONS_PATH, NOTIFICATIONS_IFACE).isValid()) {
|
2017-03-27 12:12:28 +03:00
|
|
|
QList<QVariant> args = QList<QVariant>() << APPLICATION_NAME << quint32(0) << APPLICATION_ICON_NAME
|
2013-03-22 14:58:55 +04:00
|
|
|
<< title << message << QStringList() << QVariantMap() << qint32(-1);
|
|
|
|
QDBusMessage method = QDBusMessage::createMethodCall(NOTIFICATIONS_SERVICE, NOTIFICATIONS_PATH, NOTIFICATIONS_IFACE, "Notify");
|
|
|
|
method.setArguments(args);
|
|
|
|
QDBusConnection::sessionBus().asyncCall(method);
|
2013-05-22 19:39:49 +04:00
|
|
|
} else
|
2015-02-07 20:23:09 +03:00
|
|
|
#endif
|
|
|
|
#ifdef Q_OS_OSX
|
|
|
|
if (canOsXSendUserNotification()) {
|
|
|
|
sendOsXUserNotification(title, message);
|
|
|
|
} else
|
2013-05-22 19:39:49 +04:00
|
|
|
#endif
|
|
|
|
{
|
2013-03-22 14:58:55 +04:00
|
|
|
QSystemTrayIcon::showMessage(title, message, icon, millisecondsTimeoutHint);
|
|
|
|
}
|
2013-03-22 14:34:21 +04:00
|
|
|
}
|
2013-10-01 15:51:40 +04:00
|
|
|
|
2013-11-23 03:05:50 +04:00
|
|
|
void Systray::setToolTip(const QString &tip)
|
|
|
|
{
|
|
|
|
QSystemTrayIcon::setToolTip(tr("%1: %2").arg(Theme::instance()->appNameGUI(), tip));
|
|
|
|
}
|
|
|
|
|
2014-11-10 00:34:07 +03:00
|
|
|
} // namespace OCC
|