1
0
mirror of https://github.com/KDE/latte-dock.git synced 2024-12-23 13:33:50 +03:00
latte-dock/app/dockcorona.cpp

652 lines
20 KiB
C++
Raw Normal View History

/*
2017-01-03 01:05:30 +03:00
* Copyright 2016 Smith AR <audoban@openmailbox.org>
* Michail Vourlakos <mvourlakos@gmail.com>
*
* This file is part of Latte-Dock
*
* Latte-Dock 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.
*
* Latte-Dock 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2016-12-31 00:25:27 +03:00
#include "dockcorona.h"
#include "dockview.h"
#include "packageplugins/shell/dockpackage.h"
#include "abstractwindowinterface.h"
#include "alternativeshelper.h"
2017-02-24 21:58:21 +03:00
#include "screenpool.h"
#include <QAction>
#include <QApplication>
#include <QScreen>
#include <QDebug>
#include <QDesktopWidget>
#include <QQmlContext>
#include <Plasma>
#include <Plasma/Corona>
#include <Plasma/Containment>
2016-12-31 00:25:27 +03:00
#include <KActionCollection>
#include <KPluginMetaData>
#include <KLocalizedString>
#include <KPackage/Package>
#include <KPackage/PackageLoader>
2017-01-29 08:16:28 +03:00
#include <KAboutData>
#include <KActivities/Consumer>
2016-12-30 10:20:06 +03:00
namespace Latte {
2017-02-26 20:37:46 +03:00
DockCorona::DockCorona(QObject *parent)
: Plasma::Corona(parent),
2017-02-24 21:58:21 +03:00
m_screenPool(new ScreenPool(KSharedConfig::openConfig(), this)),
2017-02-06 04:55:41 +03:00
m_activityConsumer(new KActivities::Consumer(this))
{
2016-12-31 00:25:27 +03:00
KPackage::Package package(new DockPackage(this));
2017-01-16 22:07:49 +03:00
if (!package.isValid()) {
qWarning() << staticMetaObject.className()
<< "the package" << package.metadata().rawData() << "is invalid!";
return;
} else {
qDebug() << staticMetaObject.className()
<< "the package" << package.metadata().rawData() << "is valid!";
}
2017-01-16 22:07:49 +03:00
setKPackage(package);
qmlRegisterTypes();
2016-12-31 00:25:27 +03:00
connect(this, &Corona::containmentAdded, this, &DockCorona::addDock);
if (m_activityConsumer && (m_activityConsumer->serviceStatus() == KActivities::Consumer::Running)) {
load();
}
connect(m_activityConsumer, &KActivities::Consumer::serviceStatusChanged, this, &DockCorona::load);
}
2016-12-31 00:25:27 +03:00
DockCorona::~DockCorona()
{
cleanConfig();
while (!containments().isEmpty()) {
//deleting a containment will remove it from the list due to QObject::destroyed connect in Corona
delete containments().first();
}
2017-01-16 22:07:49 +03:00
qDeleteAll(m_dockViews);
qDeleteAll(m_waitingDockViews);
m_dockViews.clear();
m_waitingDockViews.clear();
disconnect(m_activityConsumer, &KActivities::Consumer::serviceStatusChanged, this, &DockCorona::load);
delete m_activityConsumer;
qDebug() << "deleted" << this;
}
void DockCorona::load()
{
if (m_activityConsumer && (m_activityConsumer->serviceStatus() == KActivities::Consumer::Running) && m_activitiesStarting) {
2017-02-24 21:58:21 +03:00
disconnect(m_activityConsumer, &KActivities::Consumer::serviceStatusChanged, this, &DockCorona::load);
m_screenPool->load();
m_activitiesStarting = false;
2017-02-24 21:58:21 +03:00
// connect(qGuiApp, &QGuiApplication::screenAdded, this, &DockCorona::addOutput, Qt::UniqueConnection);
2017-02-24 21:58:21 +03:00
connect(qGuiApp, &QGuiApplication::primaryScreenChanged, this, &DockCorona::primaryOutputChanged, Qt::UniqueConnection);
// connect(qGuiApp, &QGuiApplication::screenRemoved, this, &DockCorona::screenRemoved, Qt::UniqueConnection);
connect(QApplication::desktop(), &QDesktopWidget::screenCountChanged, this, &DockCorona::screenCountChanged);
connect(m_screenPool, &ScreenPool::primaryPoolChanged, this, &DockCorona::screenCountChanged);
2017-02-24 21:58:21 +03:00
loadLayout();
}
}
void DockCorona::cleanConfig()
{
auto containmentsEntries = config()->group("Containments");
bool changed = false;
foreach (auto cId, containmentsEntries.groupList()) {
2017-02-06 04:55:41 +03:00
if (!containmentExists(cId.toUInt())) {
//cleanup obsolete containments
containmentsEntries.group(cId).deleteGroup();
changed = true;
qDebug() << "obsolete containment configuration deleted:" << cId;
} else {
//cleanup obsolete applets of running containments
auto appletsEntries = containmentsEntries.group(cId).group("Applets");
foreach (auto appletId, appletsEntries.groupList()) {
2017-02-06 04:55:41 +03:00
if (!appletExists(cId.toUInt(), appletId.toUInt())) {
appletsEntries.group(appletId).deleteGroup();
changed = true;
qDebug() << "obsolete applet configuration deleted:" << appletId;
}
}
}
}
if (changed) {
config()->sync();
qDebug() << "configuration file cleaned...";
}
}
2017-02-06 04:55:41 +03:00
bool DockCorona::containmentExists(uint id) const
{
foreach (auto containment, containments()) {
if (id == containment->id()) {
return true;
}
}
return false;
}
2017-02-06 04:55:41 +03:00
bool DockCorona::appletExists(uint containmentId, uint appletId) const
{
Plasma::Containment *containment = nullptr;
foreach (auto cont, containments()) {
if (containmentId == cont->id()) {
containment = cont;
break;
}
}
if (!containment) {
return false;
}
foreach (auto applet, containment->applets()) {
if (applet->id() == appletId) {
return true;
}
}
return false;
}
2017-02-24 21:58:21 +03:00
ScreenPool *DockCorona::screenPool() const
{
return m_screenPool;
}
2016-12-31 00:25:27 +03:00
int DockCorona::numScreens() const
{
return qGuiApp->screens().count();
}
2016-12-31 00:25:27 +03:00
QRect DockCorona::screenGeometry(int id) const
{
const auto screens = qGuiApp->screens();
2017-01-16 22:07:49 +03:00
if (id >= 0 && id < screens.count()) {
return screens[id]->geometry();
}
2017-01-16 22:07:49 +03:00
return qGuiApp->primaryScreen()->geometry();
}
2016-12-31 00:25:27 +03:00
QRegion DockCorona::availableScreenRegion(int id) const
{
2017-02-11 06:12:17 +03:00
return availableScreenRect(id);
//FIXME::: availableGeometry is probably broken
// in Qt, so this have to be updated as plasma is doing it
// for example the availableScreenRect
}
2016-12-31 00:25:27 +03:00
QRect DockCorona::availableScreenRect(int id) const
{
const auto screens = qGuiApp->screens();
2017-02-11 06:12:17 +03:00
const QScreen *screen = nullptr;
2017-01-16 22:07:49 +03:00
2017-02-11 06:12:17 +03:00
if (id >= 0 && id < screens.count())
screen = screens[id];
else
screen = qGuiApp->primaryScreen();
2017-02-11 06:12:17 +03:00
if (!screen)
return {};
2017-01-16 22:07:49 +03:00
2017-02-11 06:12:17 +03:00
auto available = screen->geometry();
2017-02-11 06:12:17 +03:00
for (const auto *view : m_dockViews) {
if (view && view->containment() && view->screen() == screen) {
auto dockRect = view->absGeometry();
// Usually availableScreenRect is used by the desktop,
// but Latte dont have desktop, then here just
// need calculate available space for top and bottom location,
// because the left and right are those who dodge others docks
2017-02-11 06:12:17 +03:00
switch (view->location()) {
case Plasma::Types::TopEdge:
available.setTopLeft({available.x(), dockRect.bottom()});
break;
case Plasma::Types::BottomEdge:
available.setBottomLeft({available.x(), dockRect.top()});
break;
}
}
}
2017-02-11 06:12:17 +03:00
return available;
}
2017-02-24 21:58:21 +03:00
void DockCorona::addOutput(QScreen *screen)
{
Q_ASSERT(screen);
/* qDebug() << "screen added +++ "<<screen->name();
foreach(auto scr, qGuiApp->screens()){
qDebug() << "Found screen: "<<scr->name();
}*/
/* foreach(auto cont, containments()) {
if (m_screenPool->connector(cont->screen()) == screen->name()) {
auto view = m_dockViews.take(cont);
if (!view) {
addDock(cont);
}
}
} */
2017-02-24 21:58:21 +03:00
}
void DockCorona::primaryOutputChanged()
{
qDebug() << "primary changed ### "<< qGuiApp->primaryScreen()->name();
foreach(auto scr, qGuiApp->screens()){
qDebug() << "Found screen: "<<scr->name();
}
//if (m_dockViews.count()==1 && qGuiApp->screens().size()==1) {
// foreach(auto view, m_dockViews) {
// view->setScreenToFollow(qGuiApp->primaryScreen());
// }
// }
2017-02-24 21:58:21 +03:00
}
void DockCorona::screenRemoved(QScreen *screen)
{
Q_ASSERT(screen);
/* qDebug() << "screen removed --- "<<screen->name();
foreach(auto scr, qGuiApp->screens()){
qDebug() << "Found screen: "<<scr->name();
}*/
/* if (m_dockViews.size() > 1) {
foreach(auto cont, containments()) {
if (m_screenPool->connector(cont->screen()) == screen->name()) {
auto view = m_dockViews.take(cont);
if (view) {
view->deleteLater();
}
}
}
} */
}
void DockCorona::screenCountChanged()
{
QTimer::singleShot(2500, this, &DockCorona::screenCountChangedTimer);
}
void DockCorona::screenCountChangedTimer()
{
qDebug() << "screen count changed -+-+ "<< qGuiApp->screens().size();
qDebug() << "adding consideration....";
foreach(auto scr, qGuiApp->screens()){
qDebug() << "Found screen: "<<scr->name();
foreach(auto cont, containments()) {
int id = cont->screen();
if (id == -1){
id = cont->lastScreen();
}
if ((m_screenPool->connector(id) == scr->name()) && (!m_dockViews.contains(cont))) {
qDebug() << "screen Count signal: view must be added... for:"<< scr->name();
addDock(cont);
}
}
}
qDebug() << "removing consideration....";
foreach(auto view, m_dockViews){
bool found{false};
foreach(auto scr, qGuiApp->screens()){
int id = view->containment()->screen();
if (id == -1){
id = view->containment()->lastScreen();
}
if(scr->name() == view->currentScreen()){
found = true;
break;
}
}
if (!found && (m_dockViews.size()>1) && m_dockViews.contains(view->containment())) {
qDebug() << "screen Count signal: view must be deleted... for:"<<view->currentScreen();
auto viewToDelete = m_dockViews.take(view->containment());
viewToDelete->deleteLater();
} else {
view->reconsiderScreen();
}
}
qDebug() << "end of screens count change....";
2017-02-24 21:58:21 +03:00
}
2016-12-31 00:25:27 +03:00
int DockCorona::primaryScreenId() const
{
2017-02-13 19:53:54 +03:00
//this is not the proper way because kwin probably uses a different
//index of screens...
//This needs a lot of testing...
2017-02-24 21:58:21 +03:00
return m_screenPool->id(qGuiApp->primaryScreen()->name());
}
int DockCorona::docksCount(int screen) const
{
if (screen == -1)
return 0;
2017-01-16 22:07:49 +03:00
int docks{0};
2017-01-16 22:07:49 +03:00
for (const auto &view : m_dockViews) {
if (view && view->containment()
&& view->containment()->screen() == screen
&& !view->containment()->destroyed()) {
++docks;
}
}
2017-01-16 22:07:49 +03:00
2017-01-28 20:34:03 +03:00
// qDebug() << docks << "docks on screen:" << screen;
return docks;
}
void DockCorona::closeApplication()
{
qGuiApp->quit();
}
2017-01-29 08:16:28 +03:00
void DockCorona::aboutApplication()
{
if (aboutDialog) {
aboutDialog->hide();
aboutDialog->deleteLater();
}
aboutDialog = new KAboutApplicationDialog(KAboutData::applicationData());
connect(aboutDialog.data(), &QDialog::finished, aboutDialog.data(), &QObject::deleteLater);
WindowSystem::self().skipTaskBar(*aboutDialog);
aboutDialog->show();
}
2016-12-31 00:25:27 +03:00
QList<Plasma::Types::Location> DockCorona::freeEdges(int screen) const
{
using Plasma::Types;
QList<Types::Location> edges{Types::BottomEdge, Types::LeftEdge,
Types::TopEdge, Types::RightEdge};
//when screen=-1 is passed then the primaryScreenid is used
int fixedScreen = (screen == -1) ? primaryScreenId() : screen;
2017-01-16 22:07:49 +03:00
2017-01-16 21:24:46 +03:00
for (auto *view : m_dockViews) {
if (view && view->containment()
&& view->containment()->screen() == fixedScreen) {
edges.removeOne(view->location());
}
}
2017-01-16 22:07:49 +03:00
return edges;
}
2016-12-31 00:25:27 +03:00
int DockCorona::screenForContainment(const Plasma::Containment *containment) const
{
//FIXME: indexOf is not a proper way to support multi-screen
// as for environment to environment the indexes change
// also there is the following issue triggered
// from dockView adaptToScreen()
//
// in a multi-screen environment that
// primary screen is not set to 0 it was
// created an endless showing loop at
// startup (catch-up race) between
// screen:0 and primaryScreen
2017-02-24 21:58:21 +03:00
//case in which this containment is child of an applet, hello systray :)
if (Plasma::Applet *parentApplet = qobject_cast<Plasma::Applet *>(containment->parent())) {
if (Plasma::Containment *cont = parentApplet->containment()) {
return screenForContainment(cont);
} else {
return -1;
}
}
//if the panel views already exist, base upon them
DockView *view = m_dockViews.value(containment);
if (view && view->screen()) {
return m_screenPool->id(view->screen()->name());
}
//Failed? fallback on lastScreen()
//lastScreen() is the correct screen for panels
//It is also correct for desktops *that have the correct activity()*
//a containment with lastScreen() == 0 but another activity,
//won't be associated to a screen
// qDebug() << "ShellCorona screenForContainment: " << containment << " Last screen is " << containment->lastScreen();
2017-02-24 21:58:21 +03:00
for (auto screen : qGuiApp->screens()) {
// containment->lastScreen() == m_screenPool->id(screen->name()) to check if the lastScreen refers to a screen that exists/it's known
if (containment->lastScreen() == m_screenPool->id(screen->name()) &&
(containment->activity() == m_activityConsumer->currentActivity() ||
containment->containmentType() == Plasma::Types::PanelContainment || containment->containmentType() == Plasma::Types::CustomPanelContainment)) {
2017-02-24 21:58:21 +03:00
return containment->lastScreen();
}
}
return -1;
}
2016-12-31 00:25:27 +03:00
void DockCorona::addDock(Plasma::Containment *containment)
{
if (!containment || !containment->kPackage().isValid()) {
qWarning() << "the requested containment plugin can not be located or loaded";
return;
}
2017-01-16 22:07:49 +03:00
2017-01-16 21:24:46 +03:00
auto metadata = containment->kPackage().metadata();
2017-01-16 22:07:49 +03:00
if (metadata.pluginId() == "org.kde.plasma.private.systemtray") {
2017-01-21 22:12:27 +03:00
if (metadata.pluginId() != "org.kde.latte.containment")
return;
}
2017-01-16 21:24:46 +03:00
for (auto *dock : m_dockViews) {
if (dock->containment() == containment)
2016-12-28 16:17:36 +03:00
return;
}
2017-01-16 22:07:49 +03:00
QScreen *nextScreen{qGuiApp->primaryScreen()};
int id = containment->screen();
if (id == -1) {
id = containment->lastScreen();
}
if (id >= 0) {
QString connector = m_screenPool->connector(id);
bool found{false};
foreach(auto scr, qGuiApp->screens()){
if (scr && scr->name() == connector){
found=true;
nextScreen = scr;
break;
}
}
if (!found)
return;
}
2016-12-31 00:25:27 +03:00
qDebug() << "Adding dock for container...";
qDebug() << "screen!!! :" << containment->screen() << " - "<<m_screenPool->connector(containment->screen());
auto dockView = new DockView(this, nextScreen);
dockView->init();
dockView->setContainment(containment);
connect(containment, &QObject::destroyed, this, &DockCorona::dockContainmentDestroyed);
connect(containment, &Plasma::Applet::destroyedChanged, this, &DockCorona::destroyedChanged);
connect(containment, &Plasma::Applet::locationChanged, this, &DockCorona::dockLocationChanged);
connect(containment, &Plasma::Containment::appletAlternativesRequested
, this, &DockCorona::showAlternativesForApplet, Qt::QueuedConnection);
dockView->show();
m_dockViews[containment] = dockView;
2017-01-16 21:24:46 +03:00
emit docksCountChanged();
}
void DockCorona::destroyedChanged(bool destroyed)
{
qDebug() << "dock containment destroyed changed!!!!";
Plasma::Containment *sender = qobject_cast<Plasma::Containment *>(QObject::sender());
2017-01-16 22:07:49 +03:00
if (!sender) {
return;
}
2017-01-16 22:07:49 +03:00
if (destroyed) {
m_waitingDockViews[sender] = m_dockViews.take(static_cast<Plasma::Containment *>(sender));
} else {
m_dockViews[sender] = m_waitingDockViews.take(static_cast<Plasma::Containment *>(sender));
}
2017-01-16 22:07:49 +03:00
2017-01-16 21:24:46 +03:00
emit docksCountChanged();
}
void DockCorona::dockContainmentDestroyed(QObject *cont)
{
qDebug() << "dock containment destroyed!!!!";
auto view = m_waitingDockViews.take(static_cast<Plasma::Containment *>(cont));
2017-01-16 22:07:49 +03:00
2017-01-16 21:24:46 +03:00
if (view)
2017-01-18 20:49:15 +03:00
delete view;
2017-01-16 22:07:49 +03:00
2017-01-16 21:24:46 +03:00
emit docksCountChanged();
}
void DockCorona::showAlternativesForApplet(Plasma::Applet *applet)
{
const QString alternativesQML = kPackage().filePath("appletalternativesui");
2017-02-24 21:58:21 +03:00
if (alternativesQML.isEmpty()) {
return;
}
KDeclarative::QmlObject *qmlObj = new KDeclarative::QmlObject(this);
qmlObj->setInitializationDelayed(true);
qmlObj->setSource(QUrl::fromLocalFile(alternativesQML));
AlternativesHelper *helper = new AlternativesHelper(applet, qmlObj);
qmlObj->rootContext()->setContextProperty(QStringLiteral("alternativesHelper"), helper);
m_alternativesObjects << qmlObj;
qmlObj->completeInitialization();
connect(qmlObj->rootObject(), SIGNAL(visibleChanged(bool)),
this, SLOT(alternativesVisibilityChanged(bool)));
2017-02-24 21:58:21 +03:00
connect(applet, &Plasma::Applet::destroyedChanged, this, [this, qmlObj](bool destroyed) {
if (!destroyed) {
return;
}
2017-02-24 21:58:21 +03:00
QMutableListIterator<KDeclarative::QmlObject *> it(m_alternativesObjects);
2017-02-24 21:58:21 +03:00
while (it.hasNext()) {
KDeclarative::QmlObject *obj = it.next();
2017-02-24 21:58:21 +03:00
if (obj == qmlObj) {
it.remove();
obj->deleteLater();
}
}
});
}
void DockCorona::alternativesVisibilityChanged(bool visible)
{
if (visible) {
return;
}
QObject *root = sender();
QMutableListIterator<KDeclarative::QmlObject *> it(m_alternativesObjects);
2017-02-24 21:58:21 +03:00
while (it.hasNext()) {
KDeclarative::QmlObject *obj = it.next();
2017-02-24 21:58:21 +03:00
if (obj->rootObject() == root) {
it.remove();
obj->deleteLater();
}
}
}
2016-12-31 00:25:27 +03:00
void DockCorona::loadDefaultLayout()
{
qDebug() << "loading default layout";
//! Settting mutable for create a containment
setImmutability(Plasma::Types::Mutable);
QVariantList args;
2016-12-25 16:58:14 +03:00
auto defaultContainment = createContainmentDelayed("org.kde.latte.containment", args);
defaultContainment->setContainmentType(Plasma::Types::PanelContainment);
defaultContainment->init();
2017-01-16 22:07:49 +03:00
if (!defaultContainment || !defaultContainment->kPackage().isValid()) {
qWarning() << "the requested containment plugin can not be located or loaded";
return;
}
2017-01-16 22:07:49 +03:00
auto config = defaultContainment->config();
2016-12-28 16:17:36 +03:00
defaultContainment->restore(config);
QList<Plasma::Types::Location> edges = freeEdges(defaultContainment->screen());
2017-01-16 22:07:49 +03:00
if (edges.count() > 0) {
defaultContainment->setLocation(edges.at(0));
} else {
defaultContainment->setLocation(Plasma::Types::BottomEdge);
}
2017-01-16 22:07:49 +03:00
2016-12-28 16:17:36 +03:00
defaultContainment->updateConstraints(Plasma::Types::StartupCompletedConstraint);
defaultContainment->save(config);
requestConfigSync();
defaultContainment->flushPendingConstraintsEvents();
emit containmentAdded(defaultContainment);
emit containmentCreated(defaultContainment);
addDock(defaultContainment);
2016-12-29 00:07:17 +03:00
defaultContainment->createApplet(QStringLiteral("org.kde.latte.plasmoid"));
defaultContainment->createApplet(QStringLiteral("org.kde.plasma.analogclock"));
}
2016-12-31 00:25:27 +03:00
inline void DockCorona::qmlRegisterTypes() const
{
qmlRegisterType<QScreen>();
}
2016-12-30 10:20:06 +03:00
}