~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to plasma/desktop/shell/plasmaapp.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *   Copyright 2006 Aaron Seigo <aseigo@kde.org>
 
3
 *   Copyright 2010 Chani Armitage <chani@kde.org>
 
4
 *
 
5
 *   This program is free software; you can redistribute it and/or modify
 
6
 *   it under the terms of the GNU Library General Public License as
 
7
 *   published by the Free Software Foundation; either version 2, or
 
8
 *   (at your option) any later version.
 
9
 *
 
10
 *   This program is distributed in the hope that it will be useful,
 
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *   GNU General Public License for more details
 
14
 *
 
15
 *   You should have received a copy of the GNU Library General Public
 
16
 *   License along with this program; if not, write to the
 
17
 *   Free Software Foundation, Inc.,
 
18
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
19
 */
 
20
 
 
21
#include "plasmaapp.h"
 
22
 
 
23
#ifdef Q_WS_WIN
 
24
#ifdef _WIN32_WINNT
 
25
#undef _WIN32_WINNT
 
26
#endif
 
27
#define _WIN32_WINNT 0x0500
 
28
#include <windows.h>
 
29
#endif
 
30
 
 
31
#include <unistd.h>
 
32
 
 
33
#ifndef _SC_PHYS_PAGES
 
34
    #ifdef Q_OS_FREEBSD
 
35
    #include <sys/types.h>
 
36
    #include <sys/sysctl.h>
 
37
    #endif
 
38
 
 
39
    #ifdef Q_OS_NETBSD
 
40
    #include <sys/param.h>
 
41
    #include <sys/sysctl.h>
 
42
    #endif
 
43
#endif
 
44
 
 
45
#include <QApplication>
 
46
#include <QDesktopWidget>
 
47
#include <QPixmapCache>
 
48
#include <QtDBus/QtDBus>
 
49
#include <QTimer>
 
50
#include <QVBoxLayout>
 
51
 
 
52
#include <KAction>
 
53
#include <KAuthorized>
 
54
#include <KCrash>
 
55
#include <KDebug>
 
56
#include <KCmdLineArgs>
 
57
#include <KGlobalAccel>
 
58
#include <KGlobalSettings>
 
59
#include <KNotification>
 
60
#include <KRun>
 
61
#include <KWindowSystem>
 
62
#include <KService>
 
63
#include <KIconLoader>
 
64
 
 
65
#include <ksmserver_interface.h>
 
66
 
 
67
#include <Plasma/AbstractToolBox>
 
68
#include <Plasma/AccessAppletJob>
 
69
#include <Plasma/AccessManager>
 
70
#include <Plasma/AuthorizationManager>
 
71
#include <Plasma/Containment>
 
72
#include <Plasma/Context>
 
73
#include <Plasma/Dialog>
 
74
#include <Plasma/Theme>
 
75
#include <Plasma/Wallpaper>
 
76
#include <Plasma/WindowEffects>
 
77
 
 
78
#include <kephal/screens.h>
 
79
 
 
80
#include <plasmagenericshell/backgrounddialog.h>
 
81
#include "kactivitycontroller.h"
 
82
 
 
83
#include "activity.h"
 
84
#include "appadaptor.h"
 
85
#include "controllerwindow.h"
 
86
#include "checkbox.h"
 
87
#include "desktopcorona.h"
 
88
#include "desktopview.h"
 
89
#include "interactiveconsole.h"
 
90
#include "kactivityinfo.h"
 
91
#include "panelshadows.h"
 
92
#include "panelview.h"
 
93
#include "plasma-shell-desktop.h"
 
94
#include "toolbutton.h"
 
95
#include "klistconfirmationdialog.h"
 
96
 
 
97
#ifdef Q_WS_X11
 
98
#include <X11/Xlib.h>
 
99
#include <X11/extensions/Xrender.h>
 
100
#endif
 
101
 
 
102
extern QString plasmaLocale;
 
103
 
 
104
PlasmaApp* PlasmaApp::self()
 
105
{
 
106
    if (!kapp) {
 
107
        return new PlasmaApp();
 
108
    }
 
109
 
 
110
    return qobject_cast<PlasmaApp*>(kapp);
 
111
}
 
112
 
 
113
PlasmaApp::PlasmaApp()
 
114
    : KUniqueApplication(),
 
115
      m_corona(0),
 
116
      m_panelHidden(0),
 
117
      m_mapper(new QSignalMapper(this)),
 
118
      m_startupSuspendWaitCount(0),
 
119
      m_ignoreDashboardClosures(false),
 
120
      m_pendingFixedDashboard(false),
 
121
      m_unlockCorona(false)
 
122
{
 
123
    kDebug() << "!!{} STARTUP TIME" << QTime().msecsTo(QTime::currentTime()) << "plasma app ctor start" << "(line:" << __LINE__ << ")";
 
124
    PlasmaApp::suspendStartup(true);
 
125
 
 
126
    if (KGlobalSettings::isMultiHead()) {
 
127
        KGlobal::locale()->setLanguage(plasmaLocale, KGlobal::config().data());
 
128
    }
 
129
 
 
130
    KGlobal::locale()->insertCatalog("libplasma");
 
131
    KGlobal::locale()->insertCatalog("plasmagenericshell");
 
132
    KCrash::setFlags(KCrash::AutoRestart);
 
133
 
 
134
    // why is the next line of code here here?
 
135
    //
 
136
    // plasma-desktop was once plasma. not a big deal, right?
 
137
    //
 
138
    // well, kglobalaccel has a policy of forever
 
139
    // reserving shortcuts. even if the application is not running, it will still
 
140
    // defend that application's right to using that global shortcut. this has,
 
141
    // at least to me, some very obvious negative impacts on usability, such as
 
142
    // making it difficult for the user to switch between applications of the
 
143
    // same type and use the same global shortcuts, or when the component changes
 
144
    // name as in plasma-desktop.
 
145
    //
 
146
    // applications can unregister each other's shortcuts, though, and so that's
 
147
    // exactly what we do here.
 
148
    //
 
149
    // i'd love to just rely on the kconf_update script, but kglobalaccel doesn't
 
150
    // listen to changes in its config file nor has a dbus interace to tickle it
 
151
    // into re-reading it and it starts too early in the start up sequence for
 
152
    // kconf_update to beat it to the config file.
 
153
    //
 
154
    // so we instead deal with a dbus roundtrip with kded
 
155
    // (8 context switches at minimum iirc?)
 
156
    // at every app start for something that really only needs to be done once
 
157
    // but which we can't know for sure when it has been done.
 
158
    //
 
159
    // if kglobalaccel ignored non-running apps or could be prompted into
 
160
    // reloading its config, this would be unecessary.
 
161
    //
 
162
    // what's kind of funny is that if plasma actually relied on kglobalaccel for
 
163
    // the plasmoid shortcuts (which it can't because layouts change too often and
 
164
    // can be stored/retreived making kglobalaccel management a non-option) this
 
165
    // would be a total disaster. As it is it's "just" potentially losing the user's
 
166
    // customization of the Ctrl+F12 default shortcut to bring up the dashboard.
 
167
    //
 
168
    // this line should be removed when we decide that 4.[012]->4.<current> is
 
169
    // no longer a supported upgrade path. sometime after dragons make a comeback
 
170
    // and can be seen circling the skies again.
 
171
    // - aseigo.
 
172
    KGlobalAccel::cleanComponent("plasma");
 
173
 
 
174
    m_panelViewCreationTimer.setSingleShot(true);
 
175
    m_panelViewCreationTimer.setInterval(0);
 
176
 
 
177
    m_desktopViewCreationTimer.setSingleShot(true);
 
178
    m_desktopViewCreationTimer.setInterval(0);
 
179
 
 
180
    new PlasmaAppAdaptor(this);
 
181
    QDBusConnection::sessionBus().registerObject("/App", this);
 
182
 
 
183
    // Enlarge application pixmap cache
 
184
    // Calculate the size required to hold background pixmaps for all screens.
 
185
    // Add 10% so that other (smaller) pixmaps can also be cached.
 
186
    int cacheSize = 0;
 
187
 
 
188
    if (KGlobalSettings::isMultiHead()) {
 
189
        int id = 0;
 
190
#ifdef Q_WS_X11
 
191
        Display *dpy = XOpenDisplay(NULL);
 
192
        if (dpy) {
 
193
            id = DefaultScreen(dpy);
 
194
            XCloseDisplay(dpy);
 
195
        }
 
196
#endif
 
197
        const QSize size = Kephal::ScreenUtils::screenSize(id);
 
198
        cacheSize += 4 * size.width() * size.height() / 1024;
 
199
    } else {
 
200
        const int numScreens = Kephal::ScreenUtils::numScreens();
 
201
        for (int i = 0; i < numScreens; i++) {
 
202
            QSize size = Kephal::ScreenUtils::screenSize(i);
 
203
            cacheSize += 4 * size.width() * size.height() / 1024;
 
204
        }
 
205
    }
 
206
    cacheSize += cacheSize / 10;
 
207
 
 
208
    // Calculate the size of physical system memory; _SC_PHYS_PAGES *
 
209
    // _SC_PAGESIZE is documented to be able to overflow 32-bit integers,
 
210
    // so apply a 10-bit shift. FreeBSD 6-STABLE doesn't have _SC_PHYS_PAGES
 
211
    // (it is documented in FreeBSD 7-STABLE as "Solaris and Linux extension")
 
212
    // so use sysctl in those cases.
 
213
#if defined(_SC_PHYS_PAGES)
 
214
    int memorySize = sysconf(_SC_PHYS_PAGES);
 
215
    memorySize *= sysconf(_SC_PAGESIZE) / 1024;
 
216
#else
 
217
#ifdef Q_OS_FREEBSD
 
218
    int sysctlbuf[2];
 
219
    size_t size = sizeof(sysctlbuf);
 
220
    int memorySize;
 
221
    // This could actually use hw.physmem instead, but I can't find
 
222
    // reliable documentation on how to read the value (which may
 
223
    // not fit in a 32 bit integer).
 
224
    if (!sysctlbyname("vm.stats.vm.v_page_size", sysctlbuf, &size, NULL, 0)) {
 
225
        memorySize = sysctlbuf[0] / 1024;
 
226
        size = sizeof(sysctlbuf);
 
227
        if (!sysctlbyname("vm.stats.vm.v_page_count", sysctlbuf, &size, NULL, 0)) {
 
228
            memorySize *= sysctlbuf[0];
 
229
        }
 
230
    }
 
231
#endif
 
232
#ifdef Q_OS_NETBSD
 
233
    size_t memorySize;
 
234
    size_t len;
 
235
    static int mib[] = { CTL_HW, HW_PHYSMEM };
 
236
 
 
237
    len = sizeof(memorySize);
 
238
    sysctl(mib, 2, &memorySize, &len, NULL, 0);
 
239
    memorySize /= 1024;
 
240
#endif
 
241
#ifdef Q_WS_WIN
 
242
    size_t memorySize;
 
243
 
 
244
    MEMORYSTATUSEX statex;
 
245
    statex.dwLength = sizeof (statex);
 
246
    GlobalMemoryStatusEx (&statex);
 
247
 
 
248
    memorySize = (statex.ullTotalPhys/1024) + (statex.ullTotalPageFile/1024);
 
249
#endif
 
250
    // If you have no suitable sysconf() interface and are not FreeBSD,
 
251
    // then you are out of luck and get a compile error.
 
252
#endif
 
253
 
 
254
    // Increase the pixmap cache size to 1% of system memory if it isn't already
 
255
    // larger so as to maximize cache usage. 1% of 1GB ~= 10MB.
 
256
    if (cacheSize < memorySize / 100) {
 
257
        cacheSize = memorySize / 100;
 
258
    }
 
259
 
 
260
    kDebug() << "Setting the pixmap cache size to" << cacheSize << "kilobytes";
 
261
    QPixmapCache::setCacheLimit(cacheSize);
 
262
 
 
263
    KAction *showAction = new KAction(this);
 
264
    showAction->setText(i18n("Show Dashboard"));
 
265
    showAction->setObjectName( QLatin1String("Show Dashboard" )); // NO I18N
 
266
    showAction->setGlobalShortcut(KShortcut(Qt::CTRL + Qt::Key_F12));
 
267
    connect(showAction, SIGNAL(triggered()), this, SLOT(toggleDashboard()));
 
268
 
 
269
    KGlobal::setAllowQuit(true);
 
270
    KGlobal::ref();
 
271
 
 
272
    connect(m_mapper, SIGNAL(mapped(const QString &)),
 
273
            this, SLOT(addRemotePlasmoid(const QString &)));
 
274
    connect(Plasma::AccessManager::self(),
 
275
            SIGNAL(finished(Plasma::AccessAppletJob*)),
 
276
            this, SLOT(plasmoidAccessFinished(Plasma::AccessAppletJob*)));
 
277
    connect(Plasma::AccessManager::self(),
 
278
            SIGNAL(remoteAppletAnnounced(Plasma::PackageMetadata)),
 
279
            this, SLOT(remotePlasmoidAdded(Plasma::PackageMetadata)));
 
280
 
 
281
    Plasma::AuthorizationManager::self()->setAuthorizationPolicy(Plasma::AuthorizationManager::PinPairing);
 
282
 
 
283
    QTimer::singleShot(0, this, SLOT(setupDesktop()));
 
284
    kDebug() << "!!{} STARTUP TIME" << QTime().msecsTo(QTime::currentTime()) << "plasma app ctor end" << "(line:" << __LINE__ << ")";
 
285
}
 
286
 
 
287
PlasmaApp::~PlasmaApp()
 
288
{
 
289
}
 
290
 
 
291
void PlasmaApp::setupDesktop()
 
292
{
 
293
#ifdef Q_WS_X11
 
294
    Atom atoms[5];
 
295
    const char * const atomNames[] = {"XdndAware", "XdndEnter", "XdndFinished", "XdndPosition", "XdndStatus"};
 
296
    XInternAtoms(QX11Info::display(), const_cast<char **>(atomNames), 5, False, atoms);
 
297
    m_XdndAwareAtom = atoms[0];
 
298
    m_XdndEnterAtom = atoms[1];
 
299
    m_XdndFinishedAtom = atoms[2];
 
300
    m_XdndPositionAtom = atoms[3];
 
301
    m_XdndStatusAtom = atoms[4];
 
302
    const int xdndversion = 5;
 
303
    m_XdndVersionAtom = (Atom)xdndversion;
 
304
#endif
 
305
 
 
306
    // intialize the default theme and set the font
 
307
    Plasma::Theme *theme = Plasma::Theme::defaultTheme();
 
308
    theme->setFont(AppSettings::desktopFont());
 
309
    m_panelShadows = new PanelShadows();
 
310
 
 
311
    // this line initializes the corona.
 
312
    corona();
 
313
 
 
314
    Kephal::Screens *screens = Kephal::Screens::self();
 
315
    connect(screens, SIGNAL(screenRemoved(int)), SLOT(screenRemoved(int)));
 
316
    connect(screens, SIGNAL(screenAdded(Kephal::Screen*)), SLOT(screenAdded(Kephal::Screen*)));
 
317
 
 
318
    if (AppSettings::perVirtualDesktopViews()) {
 
319
        connect(KWindowSystem::self(), SIGNAL(numberOfDesktopsChanged(int)),
 
320
                this, SLOT(checkVirtualDesktopViews(int)));
 
321
    }
 
322
 
 
323
    // free the memory possibly occupied by the background image of the
 
324
    // root window - login managers will typically set one
 
325
    QPalette palette;
 
326
    palette.setColor(desktop()->backgroundRole(), Qt::black);
 
327
    desktop()->setPalette(palette);
 
328
 
 
329
    connect(this, SIGNAL(aboutToQuit()), this, SLOT(cleanup()));
 
330
    kDebug() << "!!{} STARTUP TIME" << QTime().msecsTo(QTime::currentTime()) << "Plasma App SetupDesktop()" << "(line:" << __LINE__ << ")";
 
331
 
 
332
    // now connect up the creation timers and start them to get the views created
 
333
    connect(&m_panelViewCreationTimer, SIGNAL(timeout()), this, SLOT(createWaitingPanels()));
 
334
    connect(&m_desktopViewCreationTimer, SIGNAL(timeout()), this, SLOT(createWaitingDesktops()));
 
335
    m_panelViewCreationTimer.start();
 
336
    m_desktopViewCreationTimer.start();
 
337
}
 
338
 
 
339
void PlasmaApp::quit()
 
340
{
 
341
    if (m_corona) {
 
342
        cleanup();
 
343
        KGlobal::deref();
 
344
    }
 
345
}
 
346
 
 
347
void PlasmaApp::cleanup()
 
348
{
 
349
    if (!m_corona) {
 
350
        return;
 
351
    }
 
352
 
 
353
    m_corona->saveLayout();
 
354
 
 
355
    // save the mapping of Views to Containments at the moment
 
356
    // of application exit so we can restore that when we start again.
 
357
    KConfigGroup viewIds(KGlobal::config(), "ViewIds");
 
358
    viewIds.deleteGroup();
 
359
 
 
360
    foreach (PanelView *v, m_panels) {
 
361
        if (v->containment()) {
 
362
            viewIds.writeEntry(QString::number(v->containment()->id()), v->id());
 
363
        }
 
364
    }
 
365
 
 
366
    foreach (DesktopView *v, m_desktops) {
 
367
        if (v->containment()) {
 
368
            viewIds.writeEntry(QString::number(v->containment()->id()), v->id());
 
369
        }
 
370
    }
 
371
 
 
372
    QList<DesktopView*> desktops = m_desktops;
 
373
    m_desktops.clear();
 
374
    qDeleteAll(desktops);
 
375
 
 
376
    QList<PanelView*> panels = m_panels;
 
377
    m_panels.clear();
 
378
    qDeleteAll(panels);
 
379
 
 
380
    delete m_console.data();
 
381
    delete m_corona;
 
382
    m_corona = 0;
 
383
 
 
384
    delete m_panelShadows;
 
385
    m_panelShadows = 0;
 
386
 
 
387
    //TODO: This manual sync() should not be necessary. Remove it when
 
388
    // KConfig was fixed
 
389
    KGlobal::config()->sync();
 
390
}
 
391
 
 
392
void PlasmaApp::syncConfig()
 
393
{
 
394
    KGlobal::config()->sync();
 
395
}
 
396
 
 
397
void PlasmaApp::toggleDashboard()
 
398
{
 
399
    // we don't want to listen to dashboard closure signals when toggling
 
400
    // otherwise we get toggleDashboard -> dashboardClosed -> showDashboard
 
401
    // and the wrong state of shown dashboards occurs.
 
402
    m_ignoreDashboardClosures = true;
 
403
 
 
404
    const int currentDesktop = KWindowSystem::currentDesktop() - 1;
 
405
    foreach (DesktopView *view, m_desktops) {
 
406
        if (AppSettings::perVirtualDesktopViews()) {
 
407
            // always hide the dashboard if it isn't on the current desktop
 
408
            if (view->desktop() == currentDesktop) {
 
409
                view->toggleDashboard();
 
410
            }
 
411
        } else {
 
412
            view->toggleDashboard();
 
413
        }
 
414
    }
 
415
 
 
416
    m_ignoreDashboardClosures = false;
 
417
}
 
418
 
 
419
void PlasmaApp::showDashboard(bool show)
 
420
{
 
421
    // we don't want to listen to dashboard closure signals when showing/hiding
 
422
    // otherwise we get showDashboard -> dashboardClosed -> showDashboard
 
423
    // and that could end up badly :)
 
424
    m_ignoreDashboardClosures = true;
 
425
 
 
426
    const int currentDesktop = KWindowSystem::currentDesktop() - 1;
 
427
    foreach (DesktopView *view, m_desktops) {
 
428
        if (AppSettings::perVirtualDesktopViews()) {
 
429
            // always hide the dashboard if it isn't on the current desktop
 
430
            if (view->desktop() == currentDesktop) {
 
431
                view->showDashboard(show);
 
432
            }
 
433
        } else {
 
434
            view->showDashboard(show);
 
435
        }
 
436
    }
 
437
 
 
438
    m_ignoreDashboardClosures = false;
 
439
}
 
440
 
 
441
void PlasmaApp::dashboardClosed()
 
442
{
 
443
    if (!m_ignoreDashboardClosures) {
 
444
        showDashboard(false);
 
445
    }
 
446
}
 
447
 
 
448
void PlasmaApp::showInteractiveConsole()
 
449
{
 
450
    if (KGlobal::config()->isImmutable() || !KAuthorized::authorize("plasma-desktop/scripting_console")) {
 
451
        return;
 
452
    }
 
453
 
 
454
    InteractiveConsole *console = m_console.data();
 
455
    if (!console) {
 
456
        m_console = console = new InteractiveConsole(m_corona);
 
457
    }
 
458
 
 
459
    KWindowSystem::setOnDesktop(console->winId(), KWindowSystem::currentDesktop());
 
460
    console->show();
 
461
    console->raise();
 
462
    KWindowSystem::forceActiveWindow(console->winId());
 
463
}
 
464
 
 
465
void PlasmaApp::loadScriptInInteractiveConsole(const QString &script)
 
466
{
 
467
    showInteractiveConsole();
 
468
    if (m_console) {
 
469
        m_console.data()->loadScript(script);
 
470
    }
 
471
}
 
472
 
 
473
void PlasmaApp::panelHidden(bool hidden)
 
474
{
 
475
    if (hidden) {
 
476
        ++m_panelHidden;
 
477
        //kDebug() << "panel hidden" << m_panelHidden;
 
478
    } else {
 
479
        --m_panelHidden;
 
480
        if (m_panelHidden < 0) {
 
481
            kDebug() << "panelHidden(false) called too many times!";
 
482
            m_panelHidden = 0;
 
483
        }
 
484
        //kDebug() << "panel unhidden" << m_panelHidden;
 
485
    }
 
486
}
 
487
 
 
488
QList<PanelView*> PlasmaApp::panelViews() const
 
489
{
 
490
    return m_panels;
 
491
}
 
492
 
 
493
PanelShadows *PlasmaApp::panelShadows() const
 
494
{
 
495
    return m_panelShadows;
 
496
}
 
497
 
 
498
ControllerWindow *PlasmaApp::showWidgetExplorer(int screen, Plasma::Containment *containment)
 
499
{
 
500
    return showController(screen, containment, true);
 
501
}
 
502
 
 
503
void PlasmaApp::toggleActivityManager()
 
504
{
 
505
    const int currentScreen = m_corona->screenId(QCursor::pos());
 
506
 
 
507
    QWeakPointer<ControllerWindow> controllerPtr = m_widgetExplorers.value(currentScreen);
 
508
    ControllerWindow *controller = controllerPtr.data();
 
509
    if (controller) {
 
510
        controller->deleteLater();
 
511
        return;
 
512
    }
 
513
 
 
514
    //try to find the "active" containment
 
515
    int currentDesktop = -1;
 
516
    if (AppSettings::perVirtualDesktopViews()) {
 
517
        currentDesktop = KWindowSystem::currentDesktop() - 1;
 
518
    }
 
519
 
 
520
    Plasma::Containment *containment = m_corona->containmentForScreen(currentScreen, currentDesktop);
 
521
 
 
522
    showController(currentScreen, containment, false);
 
523
}
 
524
 
 
525
ControllerWindow *PlasmaApp::showController(int screen, Plasma::Containment *containment, bool widgetExplorerMode)
 
526
{
 
527
    if (!containment) {
 
528
        kDebug() << "no containment";
 
529
        return 0;
 
530
    }
 
531
 
 
532
    QWeakPointer<ControllerWindow> controllerPtr = m_widgetExplorers.value(screen);
 
533
    ControllerWindow *controller = controllerPtr.data();
 
534
 
 
535
    if (!controller) {
 
536
        //kDebug() << "controller not found for screen" << screen;
 
537
        controllerPtr = controller = new ControllerWindow(0);
 
538
        m_widgetExplorers.insert(screen, controllerPtr);
 
539
    }
 
540
 
 
541
    controller->setContainment(containment);
 
542
    if (!containment || containment->screen() != screen) {
 
543
        controller->setScreen(screen);
 
544
    }
 
545
 
 
546
    controller->setLocation(containment->location());
 
547
 
 
548
    if (widgetExplorerMode) {
 
549
        controller->showWidgetExplorer();
 
550
    } else {
 
551
        controller->showActivityManager();
 
552
    }
 
553
 
 
554
    controller->show();
 
555
    Plasma::WindowEffects::slideWindow(controller, Plasma::BottomEdge);
 
556
    KWindowSystem::setOnAllDesktops(controller->winId(), true);
 
557
    QTimer::singleShot(0, controller, SLOT(activate()));
 
558
    KWindowSystem::setState(controller->winId(), NET::SkipTaskbar | NET::SkipPager | NET::Sticky | NET::KeepAbove);
 
559
    return controller;
 
560
}
 
561
 
 
562
void PlasmaApp::hideController(int screen)
 
563
{
 
564
    QWeakPointer<ControllerWindow> controller = m_widgetExplorers.value(screen);
 
565
    if (controller) {
 
566
        controller.data()->hide();
 
567
    }
 
568
}
 
569
 
 
570
#ifdef Q_WS_X11
 
571
PanelView *PlasmaApp::findPanelForTrigger(WId trigger) const
 
572
{
 
573
    foreach (PanelView *panel, m_panels) {
 
574
        if (panel->unhideTrigger() == trigger) {
 
575
            return panel;
 
576
        }
 
577
    }
 
578
 
 
579
    return 0;
 
580
}
 
581
 
 
582
bool PlasmaApp::x11EventFilter(XEvent *event)
 
583
{
 
584
    if (m_panelHidden > 0 &&
 
585
        (event->type == ClientMessage ||
 
586
         (event->xany.send_event != True && (event->type == EnterNotify ||
 
587
                                             event->type == MotionNotify)))) {
 
588
 
 
589
        /*
 
590
        if (event->type == ClientMessage) {
 
591
            kDebug() << "client message with" << event->xclient.message_type << m_XdndEnterAtom << event->xcrossing.window;
 
592
        }
 
593
        */
 
594
 
 
595
        bool dndEnter = false;
 
596
        bool dndPosition = false;
 
597
        if (event->type == ClientMessage) {
 
598
            dndEnter = event->xclient.message_type == m_XdndEnterAtom;
 
599
            if (!dndEnter) {
 
600
                dndPosition = event->xclient.message_type == m_XdndPositionAtom;
 
601
                if (!dndPosition) {
 
602
                    //kDebug() << "FAIL!";
 
603
                    return KUniqueApplication::x11EventFilter(event);
 
604
                }
 
605
            } else {
 
606
                //kDebug() << "on enter" << event->xclient.data.l[0];
 
607
            }
 
608
        }
 
609
 
 
610
        PanelView *panel = findPanelForTrigger(event->xcrossing.window);
 
611
        //kDebug() << "panel?" << panel << ((dndEnter || dndPosition) ? "Drag and drop op" : "Mouse move op");
 
612
        if (panel) {
 
613
            if (dndEnter || dndPosition) {
 
614
                QPoint p;
 
615
 
 
616
                const unsigned long *l = (const unsigned long *)event->xclient.data.l;
 
617
                if (dndPosition) {
 
618
                    p = QPoint((l[2] & 0xffff0000) >> 16, l[2] & 0x0000ffff);
 
619
                } else {
 
620
                    p = QCursor::pos();
 
621
                }
 
622
 
 
623
                XClientMessageEvent response;
 
624
                response.type = ClientMessage;
 
625
                response.window = l[0];
 
626
                response.format = 32;
 
627
                response.data.l[0] = panel->winId(); //event->xcrossing.window;
 
628
 
 
629
                if (panel->hintOrUnhide(p, true)) {
 
630
                    response.message_type = m_XdndFinishedAtom;
 
631
                    response.data.l[1] = 0; // flags
 
632
                    response.data.l[2] = XNone;
 
633
                } else {
 
634
                    response.message_type = m_XdndStatusAtom;
 
635
                    response.data.l[1] = 0; // flags
 
636
                    response.data.l[2] = 0; // x, y
 
637
                    response.data.l[3] = 0; // w, h
 
638
                    response.data.l[4] = 0; // action
 
639
                }
 
640
 
 
641
                XSendEvent(QX11Info::display(), l[0], False, NoEventMask, (XEvent*)&response);
 
642
            } else if (event->type == EnterNotify) {
 
643
                panel->hintOrUnhide(QPoint(-1, -1));
 
644
                //kDebug() << "entry";
 
645
            //FIXME: this if it was possible to avoid the polling
 
646
            /*} else if (event->type == LeaveNotify) {
 
647
                panel->unhintHide();
 
648
            */
 
649
            } else if (event->type == MotionNotify) {
 
650
                XMotionEvent *motion = (XMotionEvent*)event;
 
651
                //kDebug() << "motion" << motion->x << motion->y << panel->location();
 
652
                panel->hintOrUnhide(QPoint(motion->x_root, motion->y_root));
 
653
            }
 
654
        }
 
655
    }
 
656
 
 
657
    return KUniqueApplication::x11EventFilter(event);
 
658
}
 
659
#endif
 
660
 
 
661
void PlasmaApp::screenRemoved(int id)
 
662
{
 
663
    kDebug() << "@@@@" << id;
 
664
    QMutableListIterator<DesktopView *> it(m_desktops);
 
665
    while (it.hasNext()) {
 
666
        DesktopView *view = it.next();
 
667
        if (view->screen() == id) {
 
668
            // the screen was removed, so we'll destroy the
 
669
            // corresponding view
 
670
            kDebug() << "@@@@removing the view for screen" << id;
 
671
            view->setContainment(0);
 
672
            it.remove();
 
673
            delete view;
 
674
        }
 
675
    }
 
676
 
 
677
    Kephal::Screen *primary = Kephal::Screens::self()->primaryScreen();
 
678
    QList<Kephal::Screen *> screens = Kephal::Screens::self()->screens();
 
679
    screens.removeAll(primary);
 
680
 
 
681
    // Now we process panels: if there is room on another screen for the panel,
 
682
    // we migrate the panel there, otherwise the view is deleted. The primary
 
683
    // screen is preferred in all cases.
 
684
    QMutableListIterator<PanelView*> pIt(m_panels);
 
685
    while (pIt.hasNext()) {
 
686
        PanelView *panel = pIt.next();
 
687
        if (panel->screen() == id) {
 
688
            Kephal::Screen *moveTo = 0;
 
689
            if (canRelocatePanel(panel, primary)) {
 
690
                moveTo = primary;
 
691
            } else {
 
692
                foreach (Kephal::Screen *screen, screens) {
 
693
                    if (canRelocatePanel(panel, screen)) {
 
694
                        moveTo = screen;
 
695
                        break;
 
696
                    }
 
697
                }
 
698
            }
 
699
 
 
700
            if (moveTo) {
 
701
                panel->migrateTo(moveTo->id());
 
702
            } else {
 
703
                pIt.remove();
 
704
                delete panel;
 
705
                continue;
 
706
            }
 
707
        }
 
708
 
 
709
        panel->updateStruts();
 
710
    }
 
711
}
 
712
 
 
713
void PlasmaApp::screenAdded(Kephal::Screen *screen)
 
714
{
 
715
    foreach (Plasma::Containment *containment, corona()->containments()) {
 
716
        if (isPanelContainment(containment) && containment->screen() == screen->id()) {
 
717
            m_panelsWaiting << containment;
 
718
            m_panelViewCreationTimer.start();
 
719
        }
 
720
    }
 
721
 
 
722
    foreach (PanelView *view, m_panels) {
 
723
        if (view->migratedFrom(screen->id())) {
 
724
            view->migrateTo(screen->id());
 
725
        }
 
726
    }
 
727
}
 
728
 
 
729
bool PlasmaApp::canRelocatePanel(PanelView * view, Kephal::Screen *screen)
 
730
{
 
731
    if (!screen || !view->containment()) {
 
732
        return false;
 
733
    }
 
734
 
 
735
    QRect newGeom = view->geometry();
 
736
    switch (view->location()) {
 
737
        case Plasma::TopEdge:
 
738
            newGeom.setY(screen->geom().y());
 
739
            newGeom.setX(view->offset());
 
740
            break;
 
741
        case Plasma::BottomEdge:
 
742
            newGeom.setY(screen->geom().bottom() - newGeom.height());
 
743
            newGeom.setX(view->offset());
 
744
            break;
 
745
        case Plasma::LeftEdge:
 
746
            newGeom.setX(screen->geom().left());
 
747
            newGeom.setY(view->offset());
 
748
            break;
 
749
        case Plasma::RightEdge:
 
750
            newGeom.setX(screen->geom().right() - newGeom.width());
 
751
            newGeom.setY(view->offset());
 
752
            break;
 
753
        default:
 
754
            break;
 
755
    }
 
756
 
 
757
    kDebug() << "testing:" << screen->id() << view << view->geometry() << view->location() << newGeom;
 
758
    foreach (PanelView *pv, m_panels) {
 
759
        kDebug() << pv << pv->screen() << pv->screen() << pv->location() << pv->geometry();
 
760
        if (pv != view &&
 
761
            pv->screen() == screen->id() &&
 
762
            pv->location() == view->location() &&
 
763
            pv->geometry().intersects(newGeom)) {
 
764
            return false;
 
765
            break;
 
766
        }
 
767
    }
 
768
 
 
769
    return true;
 
770
}
 
771
 
 
772
 
 
773
DesktopView* PlasmaApp::viewForScreen(int screen, int desktop) const
 
774
{
 
775
    foreach (DesktopView *view, m_desktops) {
 
776
        if (view->containment()) {
 
777
            kDebug() << "comparing" << view->containment()->screen() << screen;
 
778
        }
 
779
        if (view->containment() && view->containment()->screen() == screen && (desktop < 0 || view->containment()->desktop() == desktop)) {
 
780
            return view;
 
781
        }
 
782
    }
 
783
 
 
784
    return 0;
 
785
}
 
786
 
 
787
DesktopCorona* PlasmaApp::corona()
 
788
{
 
789
    if (!m_corona) {
 
790
        QTime t;
 
791
        t.start();
 
792
        DesktopCorona *c = new DesktopCorona(this);
 
793
        connect(c, SIGNAL(containmentAdded(Plasma::Containment*)),
 
794
                this, SLOT(containmentAdded(Plasma::Containment*)));
 
795
        connect(c, SIGNAL(configSynced()), this, SLOT(syncConfig()));
 
796
        connect(c, SIGNAL(screenOwnerChanged(int,int,Plasma::Containment*)),
 
797
                this, SLOT(containmentScreenOwnerChanged(int,int,Plasma::Containment*)));
 
798
 
 
799
        foreach (DesktopView *view, m_desktops) {
 
800
            connect(c, SIGNAL(screenOwnerChanged(int,int,Plasma::Containment*)),
 
801
                    view, SLOT(screenOwnerChanged(int,int,Plasma::Containment*)));
 
802
        }
 
803
 
 
804
        //actions!
 
805
        KAction *activityAction = c->addAction("manage activities");
 
806
        connect(activityAction, SIGNAL(triggered()), this, SLOT(toggleActivityManager()));
 
807
        activityAction->setText(i18n("Activities..."));
 
808
        activityAction->setIcon(KIcon("preferences-activities"));
 
809
        activityAction->setData(Plasma::AbstractToolBox::ConfigureTool);
 
810
        activityAction->setShortcut(KShortcut("alt+d, alt+a"));
 
811
        activityAction->setShortcutContext(Qt::ApplicationShortcut);
 
812
        activityAction->setGlobalShortcut(KShortcut(Qt::META + Qt::Key_Q));
 
813
 
 
814
        c->updateShortcuts();
 
815
 
 
816
        m_corona = c;
 
817
        c->setItemIndexMethod(QGraphicsScene::NoIndex);
 
818
        c->initializeLayout();
 
819
        c->processUpdateScripts();
 
820
        c->checkActivities();
 
821
        c->checkScreens();
 
822
        foreach (Plasma::Containment *containment, c->containments()) {
 
823
            if (containment->screen() != -1 && containment->wallpaper()) {
 
824
                ++m_startupSuspendWaitCount;
 
825
                connect(containment->wallpaper(), SIGNAL(update(QRectF)), this, SLOT(wallpaperCheckedIn()));
 
826
            }
 
827
        }
 
828
 
 
829
        QTimer::singleShot(5000, this, SLOT(wallpaperCheckInTimeout()));
 
830
        kDebug() << " ------------------------------------------>" << t.elapsed() << m_startupSuspendWaitCount;
 
831
    }
 
832
 
 
833
    return m_corona;
 
834
}
 
835
 
 
836
void PlasmaApp::wallpaperCheckInTimeout()
 
837
{
 
838
    if (m_startupSuspendWaitCount > 0) {
 
839
        m_startupSuspendWaitCount = 0;
 
840
        suspendStartup(false);
 
841
    }
 
842
}
 
843
 
 
844
void PlasmaApp::wallpaperCheckedIn()
 
845
{
 
846
    if (m_startupSuspendWaitCount < 1) {
 
847
        return;
 
848
    }
 
849
 
 
850
    --m_startupSuspendWaitCount;
 
851
    if (m_startupSuspendWaitCount < 1) {
 
852
        m_startupSuspendWaitCount = 0;
 
853
        suspendStartup(false);
 
854
    }
 
855
}
 
856
 
 
857
bool PlasmaApp::hasComposite()
 
858
{
 
859
//    return true;
 
860
#ifdef Q_WS_X11
 
861
    return KWindowSystem::compositingActive();
 
862
#else
 
863
    return false;
 
864
#endif
 
865
}
 
866
 
 
867
void PlasmaApp::suspendStartup(bool suspend)
 
868
{
 
869
    org::kde::KSMServerInterface ksmserver("org.kde.ksmserver", "/KSMServer", QDBusConnection::sessionBus());
 
870
 
 
871
    const QString startupID("workspace desktop");
 
872
    if (suspend) {
 
873
        ksmserver.suspendStartup(startupID);
 
874
    } else {
 
875
        ksmserver.resumeStartup(startupID);
 
876
    }
 
877
}
 
878
 
 
879
bool PlasmaApp::isPanelContainment(Plasma::Containment *containment)
 
880
{
 
881
    if (!containment) {
 
882
        return false;
 
883
    }
 
884
 
 
885
    Plasma::Containment::Type t = containment->containmentType();
 
886
 
 
887
    return t == Plasma::Containment::PanelContainment ||
 
888
           t == Plasma::Containment::CustomPanelContainment;
 
889
 
 
890
}
 
891
 
 
892
void PlasmaApp::createView(Plasma::Containment *containment)
 
893
{
 
894
    kDebug() << "!!{} STARTUP TIME" << QTime().msecsTo(QTime::currentTime()) << "Plasma App createView() start" << "(line:" << __LINE__ << ")";
 
895
    kDebug() << "Containment name:" << containment->name()
 
896
             << "| type" << containment->containmentType()
 
897
             <<  "| screen:" << containment->screen()
 
898
             <<  "| desktop:" << containment->desktop()
 
899
             << "| geometry:" << containment->geometry()
 
900
             << "| zValue:" << containment->zValue();
 
901
 
 
902
    // find the mapping of View to Containment, if any,
 
903
    // so we can restore things on start.
 
904
 
 
905
    if (isPanelContainment(containment)) {
 
906
        m_panelsWaiting << containment;
 
907
        m_panelViewCreationTimer.start();
 
908
    } else if (containment->screen() > -1 &&
 
909
               containment->screen() < m_corona->numScreens()) {
 
910
        if (AppSettings::perVirtualDesktopViews()) {
 
911
            if (containment->desktop() < 0 ||
 
912
                containment->desktop() > KWindowSystem::numberOfDesktops() - 1) {
 
913
                return;
 
914
            }
 
915
        }
 
916
 
 
917
        m_desktopsWaiting.append(containment);
 
918
        m_desktopViewCreationTimer.start();
 
919
    }
 
920
}
 
921
 
 
922
void PlasmaApp::setWmClass(WId id)
 
923
{
 
924
#ifdef Q_WS_X11
 
925
    //FIXME: if argb visuals enabled Qt will always set WM_CLASS as "qt-subapplication" no matter what
 
926
    //the application name is we set the proper XClassHint here, hopefully won't be necessary anymore when
 
927
    //qapplication will manage apps with argvisuals in a better way
 
928
    XClassHint classHint;
 
929
    classHint.res_name = const_cast<char*>("Plasma");
 
930
    classHint.res_class = const_cast<char*>("Plasma");
 
931
    XSetClassHint(QX11Info::display(), id, &classHint);
 
932
#endif
 
933
}
 
934
 
 
935
void PlasmaApp::createWaitingPanels()
 
936
{
 
937
    if (m_panelsWaiting.isEmpty()) {
 
938
        return;
 
939
    }
 
940
 
 
941
    const QList<QWeakPointer<Plasma::Containment> > containments = m_panelsWaiting;
 
942
    m_panelsWaiting.clear();
 
943
 
 
944
    foreach (QWeakPointer<Plasma::Containment> containmentPtr, containments) {
 
945
        Plasma::Containment *containment = containmentPtr.data();
 
946
        if (!containment) {
 
947
            continue;
 
948
        }
 
949
 
 
950
        foreach (PanelView *view, m_panels) {
 
951
            if (view->containment() == containment) {
 
952
                continue;
 
953
            }
 
954
        }
 
955
 
 
956
        if (containment->screen() < 0) {
 
957
            continue;
 
958
        }
 
959
 
 
960
        // try to relocate the panel if it is on a now-non-existent screen
 
961
        if (containment->screen() >= m_corona->numScreens()) {
 
962
            m_panelRelocationCandidates << containment;
 
963
            continue;
 
964
        }
 
965
 
 
966
        createPanelView(containment);
 
967
    }
 
968
 
 
969
    if (!m_panelRelocationCandidates.isEmpty()) {
 
970
        QTimer::singleShot(0, this, SLOT(relocatePanels()));
 
971
    }
 
972
}
 
973
 
 
974
void PlasmaApp::relocatePanels()
 
975
{
 
976
    // we go through relocatables last so that all other panels can be set up first,
 
977
    // preventing panel creation ordering to trip up the canRelocatePanel algorithm
 
978
    Kephal::Screen *primary = Kephal::Screens::self()->primaryScreen();
 
979
    QList<Kephal::Screen *> screens = Kephal::Screens::self()->screens();
 
980
    screens.removeAll(primary);
 
981
 
 
982
    foreach (QWeakPointer<Plasma::Containment> c, m_panelRelocationCandidates) {
 
983
        Plasma::Containment *containment = c.data();
 
984
        if (!containment) {
 
985
            continue;
 
986
        }
 
987
 
 
988
        Kephal::Screen *moveTo = 0;
 
989
        PanelView *panelView = createPanelView(containment);
 
990
        if (canRelocatePanel(panelView, primary)) {
 
991
            moveTo = primary;
 
992
        } else {
 
993
            foreach (Kephal::Screen *screen, screens) {
 
994
                if (canRelocatePanel(panelView, screen)) {
 
995
                    moveTo = screen;
 
996
                    break;
 
997
                }
 
998
            }
 
999
        }
 
1000
 
 
1001
        if (moveTo) {
 
1002
            panelView->migrateTo(moveTo->id());
 
1003
        } else {
 
1004
            m_panels.removeAll(panelView);
 
1005
            delete panelView;
 
1006
        }
 
1007
    }
 
1008
 
 
1009
    m_panelRelocationCandidates.clear();
 
1010
}
 
1011
 
 
1012
PanelView *PlasmaApp::createPanelView(Plasma::Containment *containment)
 
1013
{
 
1014
    KConfigGroup viewIds(KGlobal::config(), "ViewIds");
 
1015
    const int id = viewIds.readEntry(QString::number(containment->id()), 0);
 
1016
    PanelView *panelView = new PanelView(containment, id);
 
1017
 
 
1018
    connect(panelView, SIGNAL(destroyed(QObject*)), this, SLOT(panelRemoved(QObject*)));
 
1019
    m_panels << panelView;
 
1020
    panelView->show();
 
1021
    setWmClass(panelView->winId());
 
1022
    return panelView;
 
1023
}
 
1024
 
 
1025
void PlasmaApp::createWaitingDesktops()
 
1026
{
 
1027
    const QList<QWeakPointer<Plasma::Containment> > containments = m_desktopsWaiting;
 
1028
    m_desktopsWaiting.clear();
 
1029
 
 
1030
    foreach (QWeakPointer<Plasma::Containment> weakContainment, containments) {
 
1031
        if (weakContainment) {
 
1032
            Plasma::Containment *containment = weakContainment.data();
 
1033
            KConfigGroup viewIds(KGlobal::config(), "ViewIds");
 
1034
            const int id = viewIds.readEntry(QString::number(containment->id()), 0);
 
1035
 
 
1036
            const int desktop = AppSettings::perVirtualDesktopViews() ? containment->desktop() : -1;
 
1037
            if (desktop >= KWindowSystem::numberOfDesktops()) {
 
1038
                kDebug() << "not creating a view on desktop" << desktop << " as it does not exist";
 
1039
                continue;
 
1040
            }
 
1041
 
 
1042
            const int screen = containment->screen();
 
1043
            if (screen >= m_corona->numScreens() || screen < 0) {
 
1044
                kDebug() << "not creating a view on screen" << screen << "as it does not exist";
 
1045
                continue;
 
1046
            }
 
1047
 
 
1048
            DesktopView *view = viewForScreen(screen, desktop);
 
1049
 
 
1050
            if (view) {
 
1051
                kDebug() << "already had a view for" << containment->screen() << containment->desktop();
 
1052
                // we already have a view for this screen
 
1053
                continue;
 
1054
            }
 
1055
 
 
1056
            kDebug() << "creating a new view for" << containment->screen() << containment->desktop()
 
1057
                     << "and we have" << m_corona->numScreens() << "screens";
 
1058
 
 
1059
            // we have a new screen. neat.
 
1060
            view = new DesktopView(containment, id, 0);
 
1061
            connect(view, SIGNAL(dashboardClosed()), this, SLOT(dashboardClosed()));
 
1062
            if (m_corona) {
 
1063
                connect(m_corona, SIGNAL(screenOwnerChanged(int,int,Plasma::Containment*)),
 
1064
                        view, SLOT(screenOwnerChanged(int,int,Plasma::Containment*)));
 
1065
            }
 
1066
 
 
1067
            m_desktops.append(view);
 
1068
            view->show();
 
1069
            setWmClass(view->winId());
 
1070
        }
 
1071
    }
 
1072
    setFixedDashboard(fixedDashboard());
 
1073
}
 
1074
 
 
1075
void PlasmaApp::containmentAdded(Plasma::Containment *containment)
 
1076
{
 
1077
    if (isPanelContainment(containment)) {
 
1078
        foreach (PanelView * panel, m_panels) {
 
1079
            if (panel->containment() == containment) {
 
1080
                kDebug() << "not creating second PanelView with existing Containment!!";
 
1081
                return;
 
1082
            }
 
1083
        }
 
1084
    }
 
1085
 
 
1086
    createView(containment);
 
1087
 
 
1088
    disconnect(containment, 0, this, 0);
 
1089
    connect(containment, SIGNAL(configureRequested(Plasma::Containment*)),
 
1090
            this, SLOT(configureContainment(Plasma::Containment*)));
 
1091
 
 
1092
    if ((containment->containmentType() == Plasma::Containment::DesktopContainment ||
 
1093
         containment->containmentType() == Plasma::Containment::CustomContainment)) {
 
1094
        QAction *a = containment->action("remove");
 
1095
        delete a; //activities handle removal now
 
1096
        if (!(m_loadingActivity.isEmpty() || m_corona->offscreenWidgets().contains(containment))) {
 
1097
            Plasma::Context *context = containment->context();
 
1098
            if (context->currentActivityId().isEmpty()) {
 
1099
                //kDebug() << "@#$%@#$%@#$%@#$%#@$#@%@$#^%$&^$^$%#%$";
 
1100
                //kDebug() << "script->containment->activity";
 
1101
                Activity *activity = m_corona->activity(m_loadingActivity);
 
1102
                Q_ASSERT(activity);
 
1103
                activity->replaceContainment(containment);
 
1104
            }
 
1105
        }
 
1106
 
 
1107
        if (containment->containmentType() == Plasma::Containment::DesktopContainment) {
 
1108
            foreach (QAction *action, m_corona->actions()) {
 
1109
                containment->addToolBoxAction(action);
 
1110
            }
 
1111
        }
 
1112
    }
 
1113
 
 
1114
    if (!isPanelContainment(containment) && !KAuthorized::authorize("editable_desktop_icons")) {
 
1115
        containment->setImmutability(Plasma::SystemImmutable);
 
1116
    }
 
1117
}
 
1118
 
 
1119
void PlasmaApp::containmentScreenOwnerChanged(int wasScreen, int isScreen, Plasma::Containment *containment)
 
1120
{
 
1121
    Q_UNUSED(wasScreen)
 
1122
    kDebug() << "@@@was" << wasScreen << "is" << isScreen << (QObject*)containment << m_desktops.count();
 
1123
 
 
1124
    if (isScreen < 0) {
 
1125
        kDebug() << "@@@screen<0";
 
1126
        return;
 
1127
    }
 
1128
 
 
1129
    if (isPanelContainment(containment)) {
 
1130
        kDebug() << "@@@isPanel";
 
1131
        return;
 
1132
    }
 
1133
 
 
1134
    bool pvd = AppSettings::perVirtualDesktopViews();
 
1135
    foreach (DesktopView *view, m_desktops) {
 
1136
        if (view->screen() == isScreen && (!pvd || view->desktop() == containment->desktop())) {
 
1137
            kDebug() << "@@@@found view" << view;
 
1138
            return;
 
1139
        }
 
1140
    }
 
1141
 
 
1142
    kDebug() << "@@@@appending";
 
1143
    m_desktopsWaiting.append(containment);
 
1144
    m_desktopViewCreationTimer.start();
 
1145
}
 
1146
 
 
1147
void PlasmaApp::configureContainment(Plasma::Containment *containment)
 
1148
{
 
1149
    const QString id = "plasma_containment_settings_" + QString::number(containment->id());
 
1150
    BackgroundDialog *configDialog = qobject_cast<BackgroundDialog*>(KConfigDialog::exists(id));
 
1151
 
 
1152
    if (configDialog) {
 
1153
        configDialog->reloadConfig();
 
1154
    } else {
 
1155
        const QSize resolution = QApplication::desktop()->screenGeometry(containment->screen()).size();
 
1156
        Plasma::View *view = viewForScreen(containment->screen(), containment->desktop());
 
1157
 
 
1158
        if (!view) {
 
1159
            view = viewForScreen(desktop()->screenNumber(QCursor::pos()), containment->desktop());
 
1160
 
 
1161
            if (!view) {
 
1162
                if (m_desktops.count() < 1) {
 
1163
                    return;
 
1164
                }
 
1165
 
 
1166
                view = m_desktops.at(0);
 
1167
            }
 
1168
 
 
1169
        }
 
1170
 
 
1171
        KConfigSkeleton *nullManager = new KConfigSkeleton(0);
 
1172
        configDialog = new BackgroundDialog(resolution, containment, view, 0, id, nullManager);
 
1173
        configDialog->setAttribute(Qt::WA_DeleteOnClose);
 
1174
 
 
1175
        Activity *activity = m_corona->activity(containment->context()->currentActivityId());
 
1176
        Q_ASSERT(activity);
 
1177
        connect(configDialog, SIGNAL(containmentPluginChanged(Plasma::Containment*)),
 
1178
                activity, SLOT(replaceContainment(Plasma::Containment*)));
 
1179
        connect(configDialog, SIGNAL(destroyed(QObject*)), nullManager, SLOT(deleteLater()));
 
1180
    }
 
1181
 
 
1182
    configDialog->show();
 
1183
    KWindowSystem::setOnDesktop(configDialog->winId(), KWindowSystem::currentDesktop());
 
1184
    KWindowSystem::activateWindow(configDialog->winId());
 
1185
}
 
1186
 
 
1187
void PlasmaApp::cloneCurrentActivity()
 
1188
{
 
1189
    KActivityController controller;
 
1190
    //getting the current activity is *so* much easier than the current containment(s) :) :)
 
1191
    QString oldId = controller.currentActivity();
 
1192
    Activity *oldActivity = m_corona->activity(oldId);
 
1193
    QString newId = controller.addActivity(i18nc("%1 is the activity name", "copy of %1", oldActivity->name()));
 
1194
 
 
1195
    QString file = "activities/";
 
1196
    file += newId;
 
1197
    KConfig external(file, KConfig::SimpleConfig, "appdata");
 
1198
 
 
1199
    //copy the old config to the new location
 
1200
    oldActivity->save(external);
 
1201
    //kDebug() << "saved to" << file;
 
1202
 
 
1203
    //load the new one
 
1204
    controller.setCurrentActivity(newId);
 
1205
}
 
1206
 
 
1207
//TODO accomodate activities
 
1208
void PlasmaApp::setPerVirtualDesktopViews(bool perDesktopViews)
 
1209
{
 
1210
    if (perDesktopViews == perVirtualDesktopViews()) {
 
1211
        return;
 
1212
    }
 
1213
    AppSettings::setPerVirtualDesktopViews(perDesktopViews);
 
1214
    AppSettings::self()->writeConfig();
 
1215
 
 
1216
    disconnect(KWindowSystem::self(), SIGNAL(numberOfDesktopsChanged(int)),
 
1217
               this, SLOT(checkVirtualDesktopViews(int)));
 
1218
 
 
1219
    m_pendingFixedDashboard = fixedDashboard();
 
1220
 
 
1221
    if (perDesktopViews) {
 
1222
        connect(KWindowSystem::self(), SIGNAL(numberOfDesktopsChanged(int)),
 
1223
                this, SLOT(checkVirtualDesktopViews(int)));
 
1224
        checkVirtualDesktopViews(KWindowSystem::numberOfDesktops());
 
1225
        setFixedDashboard(m_pendingFixedDashboard);
 
1226
    } else {
 
1227
        QList<DesktopView *> perScreenViews;
 
1228
        foreach (DesktopView *view, m_desktops) {
 
1229
            if (view->containment()) {
 
1230
                view->containment()->setScreen(-1, -1);
 
1231
            }
 
1232
 
 
1233
            delete view;
 
1234
        }
 
1235
 
 
1236
        m_desktops.clear();
 
1237
        m_corona->checkScreens(true);
 
1238
    }
 
1239
}
 
1240
 
 
1241
bool PlasmaApp::perVirtualDesktopViews() const
 
1242
{
 
1243
    return AppSettings::perVirtualDesktopViews();
 
1244
}
 
1245
 
 
1246
void PlasmaApp::checkVirtualDesktopViews(int numDesktops)
 
1247
{
 
1248
    kDebug() << numDesktops;
 
1249
    if (AppSettings::perVirtualDesktopViews()) {
 
1250
        QMutableListIterator<DesktopView *> it(m_desktops);
 
1251
        while (it.hasNext()) {
 
1252
            DesktopView *view = it.next();
 
1253
            if (!view->containment() || view->desktop() < 0 || view->desktop() >= numDesktops)  {
 
1254
                delete view;
 
1255
                it.remove();
 
1256
            }
 
1257
        }
 
1258
    }
 
1259
 
 
1260
    m_corona->checkScreens(true);
 
1261
}
 
1262
 
 
1263
void PlasmaApp::setFixedDashboard(bool fixedDashboard)
 
1264
{
 
1265
    //TODO: should probably have one dashboard containment per screen
 
1266
    Plasma::Containment *c = 0;
 
1267
    m_pendingFixedDashboard = fixedDashboard;
 
1268
    if (fixedDashboard) {
 
1269
        foreach (Plasma::Containment *possibility, m_corona->containments()) {
 
1270
            if (possibility->pluginName() == "desktopDashboard") {
 
1271
                c = possibility;
 
1272
                break;
 
1273
            }
 
1274
        }
 
1275
 
 
1276
        if (!c) {
 
1277
            //avoid the containmentAdded signal being emitted
 
1278
            c = m_corona->addContainment("desktopDashboard");
 
1279
        }
 
1280
 
 
1281
        //everything failed? probably a badly packaged plasma
 
1282
        if (!c) {
 
1283
            return;
 
1284
        }
 
1285
        m_corona->addOffscreenWidget(c);
 
1286
    }
 
1287
 
 
1288
    QSize maxViewSize;
 
1289
    foreach (DesktopView *view, m_desktops) {
 
1290
        view->setDashboardContainment(c);
 
1291
        if (view->size().width() > maxViewSize.width() && view->size().height() > maxViewSize.height()) {
 
1292
            maxViewSize = view->size();
 
1293
        }
 
1294
    }
 
1295
 
 
1296
    if (fixedDashboard) {
 
1297
        c->resize(maxViewSize);
 
1298
    }
 
1299
 
 
1300
    m_corona->requestConfigSync();
 
1301
}
 
1302
 
 
1303
bool PlasmaApp::fixedDashboard() const
 
1304
{
 
1305
    if (m_desktops.isEmpty()) {
 
1306
        return m_pendingFixedDashboard;
 
1307
    }
 
1308
 
 
1309
    foreach (DesktopView *view, m_desktops) {
 
1310
        if (!view->dashboardFollowsDesktop()) {
 
1311
            return true;
 
1312
        }
 
1313
    }
 
1314
 
 
1315
    return false;
 
1316
}
 
1317
 
 
1318
void PlasmaApp::panelRemoved(QObject *panel)
 
1319
{
 
1320
    m_panels.removeAll((PanelView *)panel);
 
1321
}
 
1322
 
 
1323
void PlasmaApp::remotePlasmoidAdded(Plasma::PackageMetadata metadata)
 
1324
{
 
1325
    //kDebug();
 
1326
    if (m_desktops.isEmpty()) {
 
1327
        return;
 
1328
    }
 
1329
 
 
1330
    if (m_corona->immutability() == Plasma::SystemImmutable) {
 
1331
        kDebug() << "Corona is system locked";
 
1332
        return;
 
1333
    }
 
1334
 
 
1335
    // the notification ptr is automatically delete when the notification is closed
 
1336
    KNotification *notification = new KNotification("newplasmoid", m_desktops.at(0));
 
1337
    notification->setText(i18n("A new widget has become available on the network:<br><b>%1</b> - <i>%2</i>",
 
1338
                               metadata.name(), metadata.description()));
 
1339
 
 
1340
    // setup widget icon
 
1341
    if (!metadata.icon().isEmpty()) {
 
1342
        notification->setPixmap(KIcon(metadata.icon()).pixmap(IconSize(KIconLoader::Desktop)));
 
1343
    }
 
1344
 
 
1345
    // locked, but the user is able to unlock
 
1346
    if (m_corona->immutability() == Plasma::UserImmutable) {
 
1347
        m_unlockCorona = true;
 
1348
        notification->setActions(QStringList(i18n("Unlock and add to current activity")));
 
1349
    } else {
 
1350
        // immutability == Plasma::Mutable
 
1351
        notification->setActions(QStringList(i18n("Add to current activity")));
 
1352
    }
 
1353
 
 
1354
    m_mapper->setMapping(notification, metadata.remoteLocation().prettyUrl());
 
1355
    connect(notification, SIGNAL(action1Activated()), m_mapper, SLOT(map()));
 
1356
 
 
1357
    kDebug() << "firing notification";
 
1358
    notification->sendEvent();
 
1359
}
 
1360
 
 
1361
void PlasmaApp::addRemotePlasmoid(const QString &location)
 
1362
{
 
1363
    if (m_unlockCorona) {
 
1364
        m_unlockCorona = false;
 
1365
        m_corona->setImmutability(Plasma::Mutable);
 
1366
    }
 
1367
 
 
1368
    Plasma::AccessManager::self()->accessRemoteApplet(KUrl(location));
 
1369
}
 
1370
 
 
1371
void PlasmaApp::plasmoidAccessFinished(Plasma::AccessAppletJob *job)
 
1372
{
 
1373
    if (m_desktops.isEmpty()) {
 
1374
        return;
 
1375
    }
 
1376
 
 
1377
    Plasma::Containment *c = m_desktops.at(0)->containment();
 
1378
    if (c) {
 
1379
        kDebug() << "adding applet";
 
1380
        c->addApplet(job->applet(), QPointF(-1, -1), false);
 
1381
    }
 
1382
}
 
1383
 
 
1384
void PlasmaApp::createActivity(const QString &plugin)
 
1385
{
 
1386
    KActivityController controller;
 
1387
    QString id = controller.addActivity(i18nc("Default name for a new activity", "New Activity"));
 
1388
 
 
1389
    Activity *a = m_corona->activity(id);
 
1390
    Q_ASSERT(a);
 
1391
    a->setDefaultPlugin(plugin);
 
1392
 
 
1393
    controller.setCurrentActivity(id);
 
1394
}
 
1395
 
 
1396
void PlasmaApp::createActivityFromScript(const QString &script, const QString &name, const QString &icon, const QStringList &startupApps)
 
1397
{
 
1398
    KActivityController controller;
 
1399
    m_loadingActivity = controller.addActivity(name);
 
1400
    Activity *a = m_corona->activity(m_loadingActivity);
 
1401
    Q_ASSERT(a);
 
1402
    a->setIcon(icon);
 
1403
 
 
1404
    //kDebug() << "$$$$$$$$$$$$$$$$ begin script for" << m_loadingActivity;
 
1405
    m_corona->evaluateScripts(QStringList() << script);
 
1406
    //kDebug() << "$$$$$$$$$$$$$$$$ end script for" << m_loadingActivity;
 
1407
 
 
1408
    controller.setCurrentActivity(m_loadingActivity);
 
1409
    m_loadingActivity.clear();
 
1410
 
 
1411
    KListConfirmationDialog * confirmDialog = new KListConfirmationDialog(
 
1412
            i18n("Run applications"),
 
1413
            i18n("This activity template requests to run the following applications"),
 
1414
            i18n("Run selected"),
 
1415
            i18n("Run none")
 
1416
            );
 
1417
    connect(confirmDialog, SIGNAL(selected(QList<QVariant>)),
 
1418
            this, SLOT(executeCommands(QList<QVariant>)));
 
1419
 
 
1420
    foreach (const QString & exec, startupApps) {
 
1421
        QString realExec = exec;
 
1422
 
 
1423
        #define LazyReplace(VAR, VAL) \
 
1424
            if (realExec.contains(VAR)) realExec = realExec.replace(VAR, VAL);
 
1425
 
 
1426
        LazyReplace("$desktop",   KGlobalSettings::desktopPath());
 
1427
        LazyReplace("$autostart", KGlobalSettings::autostartPath());
 
1428
        LazyReplace("$documents", KGlobalSettings::documentPath());
 
1429
        LazyReplace("$music",     KGlobalSettings::musicPath());
 
1430
        LazyReplace("$video",     KGlobalSettings::videosPath());
 
1431
        LazyReplace("$downloads", KGlobalSettings::downloadPath());
 
1432
        LazyReplace("$pictures",  KGlobalSettings::picturesPath());
 
1433
 
 
1434
        QString name = realExec.split(" ")[0];
 
1435
 
 
1436
        KService::Ptr service = KService::serviceByDesktopName(name);
 
1437
 
 
1438
        if (service) {
 
1439
            confirmDialog->addItem(KIcon(service->icon()), service->name(),
 
1440
                    ((realExec == name) ? QString() : realExec), realExec, exec.split(" ").size() <= 2);
 
1441
        } else {
 
1442
            confirmDialog->addItem(KIcon("dialog-warning"), name,
 
1443
                    ((realExec == name) ? QString() : realExec), realExec, false);
 
1444
        }
 
1445
 
 
1446
        #undef LazyReplace
 
1447
    }
 
1448
 
 
1449
    confirmDialog->exec();
 
1450
}
 
1451
 
 
1452
void PlasmaApp::executeCommands(const QList < QVariant > & commands)
 
1453
{
 
1454
    foreach (const QVariant & command, commands) {
 
1455
        KRun::runCommand(command.toString(), 0);
 
1456
    }
 
1457
}
 
1458
 
 
1459
#include "plasmaapp.moc"