operations completely delegated to coronabase

This commit is contained in:
Marco Martin 2012-02-28 20:40:28 +01:00
parent 26d4132e42
commit 7c959ff917
3 changed files with 25 additions and 292 deletions

View File

@ -111,78 +111,22 @@ void Corona::saveLayout(const QString &configName) const
void Corona::exportLayout(KConfigGroup &config, QList<Containment*> containments) void Corona::exportLayout(KConfigGroup &config, QList<Containment*> containments)
{ {
foreach (const QString &group, config.groupList()) { d->coronaBase->exportLayout(config, containments);
KConfigGroup cg(&config, group);
cg.deleteGroup();
}
//temporarily unlock so that removal works
ImmutabilityType oldImm = immutability();
d->immutability = Mutable;
KConfigGroup dest(&config, "Containments");
KConfigGroup dummy;
foreach (Plasma::Containment *c, containments) {
c->save(dummy);
c->config().reparent(&dest);
//ensure the containment is unlocked
//this is done directly because we have to bypass any SystemImmutable checks
c->Applet::d->immutability = Mutable;
foreach (Applet *a, c->applets()) {
a->d->immutability = Mutable;
}
c->destroy(false);
}
//restore immutability
d->immutability = oldImm;
config.sync();
} }
void Corona::requestConfigSync() void Corona::requestConfigSync()
{ {
// constant controlling how long between requesting a configuration sync d->coronaBase->requestConfigSync();
// and one happening should occur. currently 10 seconds
static const int CONFIG_SYNC_TIMEOUT = 10000;
// TODO: should we check into our immutability before doing this?
//NOTE: this is a pretty simplistic model: we simply save no more than CONFIG_SYNC_TIMEOUT
// after the first time this is called. not much of a heuristic for save points, but
// it should at least compress these activities a bit and provide a way for applet
// authors to ween themselves from the sync() disease. A more interesting/dynamic
// algorithm for determining when to actually sync() to disk might be better, though.
if (!d->configSyncTimer.isActive()) {
d->configSyncTimer.start(CONFIG_SYNC_TIMEOUT);
}
} }
void Corona::requireConfigSync() void Corona::requireConfigSync()
{ {
d->syncConfig(); d->coronaBase->requestConfigSync();
} }
void Corona::initializeLayout(const QString &configName) void Corona::initializeLayout(const QString &configName)
{ {
clearContainments(); d->coronaBase->initializeLayout();
loadLayout(configName);
if (d->containments.isEmpty()) {
loadDefaultLayout();
if (!d->containments.isEmpty()) {
requestConfigSync();
}
}
if (config()->isImmutable()) {
setImmutability(SystemImmutable);
} else {
KConfigGroup coronaConfig(config(), "General");
setImmutability((ImmutabilityType)coronaConfig.readEntry("immutability", (int)Mutable));
}
} }
bool containmentSortByPosition(const Containment *c1, const Containment *c2) bool containmentSortByPosition(const Containment *c1, const Containment *c2)
@ -268,16 +212,7 @@ QList<Plasma::Containment *> Corona::importLayout(const KConfigGroup &conf)
Containment *Corona::containmentForScreen(int screen, int desktop) const Containment *Corona::containmentForScreen(int screen, int desktop) const
{ {
foreach (Containment *containment, d->containments) { return d->coronaBase->containmentForScreen(screen, desktop);
if (containment->screen() == screen &&
(desktop < 0 || containment->desktop() == desktop) &&
(containment->containmentType() == Containment::DesktopContainment ||
containment->containmentType() == Containment::CustomContainment)) {
return containment;
}
}
return 0;
} }
Containment *Corona::containmentForScreen(int screen, int desktop, Containment *Corona::containmentForScreen(int screen, int desktop,
@ -288,23 +223,17 @@ Containment *Corona::containmentForScreen(int screen, int desktop,
QList<Containment*> Corona::containments() const QList<Containment*> Corona::containments() const
{ {
return d->containments; return d->coronaBase->containments();
} }
void Corona::clearContainments() void Corona::clearContainments()
{ {
foreach (Containment *containment, d->containments) { d->coronaBase->clearContainments();
containment->clearApplets();
}
} }
KSharedConfigPtr Corona::config() const KSharedConfigPtr Corona::config() const
{ {
if (!d->config) { return d->coronaBase->config();
d->config = KSharedConfig::openConfig(d->configName, KConfig::SimpleConfig);
}
return d->config;
} }
Containment *Corona::addContainment(const QString &name, const QVariantList &args) Containment *Corona::addContainment(const QString &name, const QVariantList &args)
@ -573,272 +502,96 @@ void Corona::loadDefaultLayout()
void Corona::setPreferredToolBoxPlugin(const Containment::Type type, const QString &plugin) void Corona::setPreferredToolBoxPlugin(const Containment::Type type, const QString &plugin)
{ {
d->toolBoxPlugins[type] = plugin; d->coronaBase->setPreferredToolBoxPlugin(type, plugin);
//TODO: react to plugin changes on the fly? still don't see the use case (maybe for laptops that become tablets?)
} }
QString Corona::preferredToolBoxPlugin(const Containment::Type type) const QString Corona::preferredToolBoxPlugin(const Containment::Type type) const
{ {
return d->toolBoxPlugins.value(type); return d->coronaBase->preferredToolBoxPlugin(type);
} }
ImmutabilityType Corona::immutability() const ImmutabilityType Corona::immutability() const
{ {
return d->immutability; return d->coronaBase->immutability();
} }
void Corona::setImmutability(const ImmutabilityType immutable) void Corona::setImmutability(const ImmutabilityType immutable)
{ {
if (d->immutability == immutable || d->immutability == SystemImmutable) { d->coronaBase->setImmutability(immutable);
return;
}
#ifndef NDEBUG
kDebug() << "setting immutability to" << immutable;
#endif
d->immutability = immutable;
d->updateContainmentImmutability();
//tell non-containments that might care (like plasmaapp or a custom corona)
emit immutabilityChanged(immutable);
//update our actions
QAction *action = d->actions.action("lock widgets");
if (action) {
if (d->immutability == SystemImmutable) {
action->setEnabled(false);
action->setVisible(false);
} else {
bool unlocked = d->immutability == Mutable;
action->setText(unlocked ? i18n("Lock Widgets") : i18n("Unlock Widgets"));
action->setIcon(KIcon(unlocked ? "object-locked" : "object-unlocked"));
action->setEnabled(true);
action->setVisible(true);
}
}
if (d->immutability != SystemImmutable) {
KConfigGroup cg(config(), "General");
// we call the dptr member directly for locked since isImmutable()
// also checks kiosk and parent containers
cg.writeEntry("immutability", (int)d->immutability);
requestConfigSync();
}
} }
QList<Plasma::Location> Corona::freeEdges(int screen) const QList<Plasma::Location> Corona::freeEdges(int screen) const
{ {
QList<Plasma::Location> freeEdges; return d->coronaBase->freeEdges(screen);
freeEdges << Plasma::TopEdge << Plasma::BottomEdge
<< Plasma::LeftEdge << Plasma::RightEdge;
foreach (Containment *containment, containments()) {
if (containment->screen() == screen &&
freeEdges.contains(containment->location())) {
freeEdges.removeAll(containment->location());
}
}
return freeEdges;
} }
QAction *Corona::action(QString name) const QAction *Corona::action(QString name) const
{ {
return d->actions.action(name); return d->coronaBase->action(name);
} }
void Corona::addAction(QString name, QAction *action) void Corona::addAction(QString name, QAction *action)
{ {
d->actions.addAction(name, action); d->coronaBase->addAction(name, action);
} }
KAction* Corona::addAction(QString name) KAction* Corona::addAction(QString name)
{ {
return d->actions.addAction(name); return d->coronaBase->addAction(name);
} }
QList<QAction*> Corona::actions() const QList<QAction*> Corona::actions() const
{ {
return d->actions.actions(); return d->coronaBase->actions();
} }
void Corona::enableAction(const QString &name, bool enable) void Corona::enableAction(const QString &name, bool enable)
{ {
QAction *action = d->actions.action(name); d->coronaBase->enableAction(name, enable);
if (action) {
action->setEnabled(enable);
action->setVisible(enable);
}
} }
void Corona::updateShortcuts() void Corona::updateShortcuts()
{ {
QMutableListIterator<QWeakPointer<KActionCollection> > it(d->actionCollections); d->coronaBase->updateShortcuts();
while (it.hasNext()) {
it.next();
KActionCollection *collection = it.value().data();
if (!collection) {
// get rid of KActionCollections that have been deleted behind our backs
it.remove();
continue;
}
collection->readSettings();
if (d->shortcutsDlg) {
d->shortcutsDlg.data()->addCollection(collection);
}
}
} }
void Corona::addShortcuts(KActionCollection *newShortcuts) void Corona::addShortcuts(KActionCollection *newShortcuts)
{ {
d->actionCollections << newShortcuts; d->coronaBase->addShortcuts(newShortcuts);
if (d->shortcutsDlg) {
d->shortcutsDlg.data()->addCollection(newShortcuts);
}
} }
void Corona::setContainmentActionsDefaults(Containment::Type containmentType, const ContainmentActionsPluginsConfig &config) void Corona::setContainmentActionsDefaults(Containment::Type containmentType, const ContainmentActionsPluginsConfig &config)
{ {
d->containmentActionsDefaults.insert(containmentType, config); d->coronaBase->setContainmentActionsDefaults(containmentType, config);
} }
ContainmentActionsPluginsConfig Corona::containmentActionsDefaults(Containment::Type containmentType) ContainmentActionsPluginsConfig Corona::containmentActionsDefaults(Containment::Type containmentType)
{ {
return d->containmentActionsDefaults.value(containmentType); return d->coronaBase->containmentActionsDefaults(containmentType);
} }
void Corona::setDialogManager(AbstractDialogManager *dialogManager) void Corona::setDialogManager(AbstractDialogManager *dialogManager)
{ {
d->dialogManager = dialogManager; d->coronaBase->setDialogManager(dialogManager);
} }
AbstractDialogManager *Corona::dialogManager() AbstractDialogManager *Corona::dialogManager()
{ {
return d->dialogManager.data(); return d->coronaBase->dialogManager();
} }
CoronaPrivate::CoronaPrivate(Corona *corona) CoronaPrivate::CoronaPrivate(Corona *corona)
: q(corona), : q(corona)
immutability(Mutable),
config(0),
actions(corona)
{ {
if (KGlobal::hasMainComponent()) {
configName = KGlobal::mainComponent().componentName() + "-appletsrc";
} else {
configName = "plasma-appletsrc";
}
} }
CoronaPrivate::~CoronaPrivate() CoronaPrivate::~CoronaPrivate()
{ {
qDeleteAll(containments);
} }
void CoronaPrivate::init() void CoronaPrivate::init()
{ {
q->setStickyFocus(true); q->setStickyFocus(true);
configSyncTimer.setSingleShot(true);
QObject::connect(&configSyncTimer, SIGNAL(timeout()), q, SLOT(syncConfig()));
//some common actions
actions.setConfigGroup("Shortcuts");
KAction *lockAction = actions.addAction("lock widgets");
QObject::connect(lockAction, SIGNAL(triggered(bool)), q, SLOT(toggleImmutability()));
lockAction->setText(i18n("Lock Widgets"));
lockAction->setAutoRepeat(true);
lockAction->setIcon(KIcon("object-locked"));
lockAction->setData(AbstractToolBox::ControlTool);
lockAction->setShortcut(KShortcut("alt+d, l"));
lockAction->setShortcutContext(Qt::ApplicationShortcut);
//FIXME this doesn't really belong here. desktop KCM maybe?
//but should the shortcuts be per-app or really-global?
//I don't know how to make kactioncollections use plasmarc
KAction *action = actions.addAction("configure shortcuts");
QObject::connect(action, SIGNAL(triggered()), q, SLOT(showShortcutConfig()));
action->setText(i18n("Shortcut Settings"));
action->setIcon(KIcon("configure-shortcuts"));
action->setAutoRepeat(false);
action->setData(AbstractToolBox::ConfigureTool);
//action->setShortcut(KShortcut("ctrl+h"));
action->setShortcutContext(Qt::ApplicationShortcut);
//fake containment/applet actions
KActionCollection *containmentActions = AppletPrivate::defaultActions(q); //containment has to start with applet stuff
ContainmentPrivate::addDefaultActions(containmentActions); //now it's really containment
actionCollections << &actions << AppletPrivate::defaultActions(q) << containmentActions;
q->updateShortcuts();
}
void CoronaPrivate::showShortcutConfig()
{
//show a kshortcutsdialog with the actions
KShortcutsDialog *dlg = shortcutsDlg.data();
if (!dlg) {
dlg = new KShortcutsDialog();
dlg->setModal(false);
dlg->setAttribute(Qt::WA_DeleteOnClose, true);
QObject::connect(dlg, SIGNAL(saved()), q, SIGNAL(shortcutsChanged()));
dlg->addCollection(&actions);
QMutableListIterator<QWeakPointer<KActionCollection> > it(actionCollections);
while (it.hasNext()) {
it.next();
KActionCollection *collection = it.value().data();
if (!collection) {
// get rid of KActionCollections that have been deleted behind our backs
it.remove();
continue;
}
dlg->addCollection(collection);
}
}
KWindowSystem::setOnDesktop(dlg->winId(), KWindowSystem::currentDesktop());
dlg->configure();
dlg->raise();
}
void CoronaPrivate::toggleImmutability()
{
if (immutability == Mutable) {
q->setImmutability(UserImmutable);
} else {
q->setImmutability(Mutable);
}
}
void CoronaPrivate::updateContainmentImmutability()
{
foreach (Containment *c, containments) {
// we need to tell each containment that immutability has been altered
c->updateConstraints(ImmutableConstraint);
}
}
void CoronaPrivate::containmentDestroyed(QObject *obj)
{
// we do a static_cast here since it really isn't an Containment by this
// point anymore since we are in the qobject dtor. we don't actually
// try and do anything with it, we just need the value of the pointer
// so this unsafe looking code is actually just fine.
Containment* containment = static_cast<Plasma::Containment*>(obj);
int index = containments.indexOf(containment);
if (index > -1) {
containments.removeAt(index);
q->requestConfigSync();
}
}
void CoronaPrivate::syncConfig()
{
q->config()->sync();
emit q->configSynced();
} }

View File

@ -470,11 +470,7 @@ protected:
private: private:
CoronaPrivate *const d; CoronaPrivate *const d;
Q_PRIVATE_SLOT(d, void containmentDestroyed(QObject*))
Q_PRIVATE_SLOT(d, void offscreenWidgetDestroyed(QObject *)) Q_PRIVATE_SLOT(d, void offscreenWidgetDestroyed(QObject *))
Q_PRIVATE_SLOT(d, void syncConfig())
Q_PRIVATE_SLOT(d, void toggleImmutability())
Q_PRIVATE_SLOT(d, void showShortcutConfig())
friend class CoronaPrivate; friend class CoronaPrivate;
friend class View; friend class View;

View File

@ -39,28 +39,12 @@ public:
~CoronaPrivate(); ~CoronaPrivate();
void init(); void init();
void showShortcutConfig();
void toggleImmutability();
void updateContainmentImmutability();
void containmentDestroyed(QObject *obj);
void syncConfig();
void offscreenWidgetDestroyed(QObject *); void offscreenWidgetDestroyed(QObject *);
static bool s_positioningContainments; static bool s_positioningContainments;
Corona *q; Corona *q;
ImmutabilityType immutability;
QString configName;
KSharedConfigPtr config;
QTimer configSyncTimer;
QList<Containment*> containments;
QHash<uint, QGraphicsWidget*> offscreenWidgets; QHash<uint, QGraphicsWidget*> offscreenWidgets;
KActionCollection actions;
QMap<Containment::Type, ContainmentActionsPluginsConfig> containmentActionsDefaults;
QWeakPointer<KShortcutsDialog> shortcutsDlg;
QWeakPointer<AbstractDialogManager> dialogManager;
QHash<Containment::Type, QString> toolBoxPlugins;
QList<QWeakPointer<KActionCollection> > actionCollections;
CoronaBase *coronaBase; CoronaBase *coronaBase;
}; };