aboutsummaryrefslogtreecommitdiffstats
path: root/src/livenodeengine.cpp
blob: 5feb843818e27a921415697caa3712c62002c098 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
/****************************************************************************
**
** Copyright (C) 2016 Pelagicore AG
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QmlLive tool.
**
** $QT_BEGIN_LICENSE:GPL-QTAS$
** Commercial License Usage
** Licensees holding valid commercial Qt Automotive Suite licenses may use
** this file in accordance with the commercial license agreement provided
** with the Software or, alternatively, in accordance with the terms
** contained in a written agreement between you and The Qt Company.  For
** licensing terms and conditions see https://www.qt.io/terms-conditions.
** For further information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
** SPDX-License-Identifier: GPL-3.0
**
****************************************************************************/

#include "livenodeengine.h"
#include "liveruntime.h"
#include "qmlhelper.h"
#include "contentpluginfactory.h"
#include "imageadapter.h"
#include "fontadapter.h"

#include "QtQml/qqml.h"
#include "QtQuick/private/qquickpixmapcache_p.h"

// TODO: create proxy configuration settings, controlled by command line and ui

#ifdef QMLLIVE_DEBUG
#define DEBUG qDebug()
#else
#define DEBUG if (0) qDebug()
#endif

namespace {
const char *const OVERLAY_PATH_PREFIX = "qml-live-overlay--";
const char OVERLAY_PATH_SEPARATOR = '-';
}

/*!
 * \class LiveNodeEngine
 * \brief The LiveNodeEngine class instantiates QML components in cooperation with LiveHubEngine.
 * \inmodule qmllive
 *
 * LiveNodeEngine provides ways to reload QML documents based incoming requests
 * from a hub. A hub can be connected via a RemotePublisher/RemoteReceiver pair.
 *
 * The primary use case is to allow loading of QML components instantiating
 * QQuickWindow, i.e., inheriting QML Window. A fallbackView can be set in order
 * to support also QML Item based components.
 *
 * In Addition to showing QML files the LiveNodeEngine can be extended by plugins to show any other filetype.
 * One need to set the Plugin path to the right destination and the LiveNodeEngine will load all the plugins
 * it finds there.
 *
 * \sa {Custom Runtime}, {ContentPlugin Example}
 */

/*!
 *   \enum LiveNodeEngine::WorkspaceOption
 *
 *   This enum type controls optional workspace related features:
 *
 *   \value NoWorkspaceOption
 *          No optional feature is enabled.
 *   \value LoadDummyData
 *          Enables loading of dummy QML data - QML documents located in the
 *          "dummydata" subdirectory of the workspace directory.
 *   \value AllowUpdates
 *          Enables receiving updates to workspace documents.
 *   \value UpdatesAsOverlay
 *          With this option enabled, updates can be received even if workspace
 *          is read only. Updates will be stored in a writable overlay stacked
 *          over the original workspace with the help of
 *          QQmlAbstractUrlInterceptor. Requires \l AllowUpdates.
 *
 * \sa {QmlLive Runtime}
 */

class OverlayUrlInterceptor : public QObject, public QQmlAbstractUrlInterceptor
{
    Q_OBJECT

public:
    OverlayUrlInterceptor(const QString &basePath, const QString &overlayPath,
                          QQmlAbstractUrlInterceptor *otherInterceptor, QObject *parent)
        : QObject(parent)
        , m_base(basePath)
        , m_overlay(overlayPath)
        , m_otherInterceptor(otherInterceptor)
    {
        Q_ASSERT(!basePath.isEmpty());
        Q_ASSERT(!overlayPath.isEmpty());

        QDirIterator it(m_overlay.absolutePath(), QDir::AllEntries | QDir::NoDotAndDotDot,
                        QDirIterator::Subdirectories);
        while (it.hasNext()) {
            QString overlayingPath = it.next();
            QString document = m_overlay.relativeFilePath(overlayingPath);
            m_mappings.insert(QUrl::fromLocalFile(m_base.absoluteFilePath(document)),
                              QUrl::fromLocalFile(overlayingPath));
        }
    }

    QDir overlay() const { return m_overlay; }

    QString reserve(const LiveDocument &document)
    {
        QWriteLocker locker(&m_lock);

        QString overlayingPath = document.absoluteFilePathIn(m_overlay);
        m_mappings.insert(QUrl::fromLocalFile(document.absoluteFilePathIn(m_base)),
                          QUrl::fromLocalFile(overlayingPath));
        return overlayingPath;
    }

    // From QQmlAbstractUrlInterceptor
    QUrl intercept(const QUrl &url, DataType type) Q_DECL_OVERRIDE
    {
        Q_UNUSED(type);

        QReadLocker locker(&m_lock);

        const QUrl url_ = m_otherInterceptor ? m_otherInterceptor->intercept(url, type) : url;
        return m_mappings.value(url_, url_);
    }

private:
    QReadWriteLock m_lock;
    QDir m_base;
    QDir m_overlay;
    QQmlAbstractUrlInterceptor *m_otherInterceptor;
    QHash<QUrl, QUrl> m_mappings;
};

/*!
 * Standard constructor using \a parent as parent
 */
LiveNodeEngine::LiveNodeEngine(QObject *parent)
    : QObject(parent)
    , m_runtime(new LiveRuntime(this))
    , m_xOffset(0)
    , m_yOffset(0)
    , m_rotation(0)
    , m_delayReload(new QTimer(this))
    , m_pluginFactory(new ContentPluginFactory(this))
    , m_activePlugin(0)
{
    m_delayReload->setInterval(250);
    m_delayReload->setSingleShot(true);
    connect(m_delayReload, &QTimer::timeout, this, &LiveNodeEngine::reloadDocument);
}

/*!
 * Destructor
 */
LiveNodeEngine::~LiveNodeEngine()
{
    destroyOverlay();
}

/*!
 * The QML engine to be used for loading QML components
 */
QQmlEngine *LiveNodeEngine::qmlEngine() const
{
    return m_qmlEngine;
}

/*!
 * Sets \a qmlEngine as the QML engine to be used for loading QML components
 */
void LiveNodeEngine::setQmlEngine(QQmlEngine *qmlEngine)
{
    Q_ASSERT(!this->qmlEngine());
    Q_ASSERT(qmlEngine);

    m_qmlEngine = qmlEngine;

    connect(m_qmlEngine.data(), &QQmlEngine::warnings, this, &LiveNodeEngine::logErrors);

    m_qmlEngine->rootContext()->setContextProperty("livert", m_runtime);
}

/*!
 * The QQuickView for displaying QML Item based components, i.e., those not
 * creating own windows.
 */
QQuickView *LiveNodeEngine::fallbackView() const
{
    return m_fallbackView;
}

/*!
 * Sets \a fallbackView as the QQuickView for displaying QML Item based
 * components, i.e., those not creating own windows.
 */
void LiveNodeEngine::setFallbackView(QQuickView *fallbackView)
{
    Q_ASSERT(qmlEngine());
    Q_ASSERT(fallbackView);
    Q_ASSERT(fallbackView->engine() == qmlEngine());

    if (fallbackView && fallbackView->engine() != m_qmlEngine)
        qCritical() << "LiveNodeEngine::fallbackView must use the QmlEngine instance set as LiveNodeEngine::qmlEngine";

    m_fallbackView = fallbackView;
}

/*!
 * Sets the x-offset \a offset of window
 */
void LiveNodeEngine::setXOffset(int offset)
{
    if (m_activeWindow)
        m_activeWindow->contentItem()->setX(offset);

    m_xOffset = offset;
}

/*!
 * Returns the current x-offset of the window
 */
int LiveNodeEngine::xOffset() const
{
    return m_xOffset;
}

/*!
 * Sets the y-offset \a offset of window
 */
void LiveNodeEngine::setYOffset(int offset)
{
    if (m_activeWindow)
        m_activeWindow->contentItem()->setY(offset);

    m_yOffset = offset;
}

/*!
 * Returns the current y-offset of the window
 */
int LiveNodeEngine::yOffset() const
{
    return m_yOffset;
}

/*!
 * Sets the rotation \a rotation of window around the center
 */
void LiveNodeEngine::setRotation(int rotation)
{
    if (m_activeWindow) {
        m_activeWindow->contentItem()->setRotation(0);
        const QPointF center(m_activeWindow->width() / 2, m_activeWindow->height() / 2);
        m_activeWindow->contentItem()->setTransformOriginPoint(center);
        m_activeWindow->contentItem()->setRotation(rotation);
    }

    m_rotation = rotation;
}

/*!
 * Return the current rotation angle
 */
int LiveNodeEngine::rotation() const
{
    return m_rotation;
}

/*!
 * Allows to initialize active document with an instance preloaded beyond
 * LiveNodeEngine's control.
 *
 * This can be called at most once and only before a document has been loaded
 * with loadDocument().
 *
 * \a document is the source of the component that was used to instantiate the
 * \a object. \a window should be either the \a object itself or the
 * fallbackView(). \a errors (if any) will be added to log.
 *
 * Note that \a window will be destroyed on next loadDocument() call unless it
 * is the fallbackView(). \a object will be destroyed unconditionally.
 */
void LiveNodeEngine::usePreloadedDocument(const LiveDocument &document, QObject *object,
                                          QQuickWindow *window, const QList<QQmlError> &errors)
{
    LIVE_ASSERT(m_activeFile.isNull(), return);
    LIVE_ASSERT(!document.isNull(), return);

    m_activeFile = document;

    if (!m_activeFile.existsIn(m_workspace)) {
        QQmlError error;
        error.setUrl(QUrl::fromLocalFile(m_activeFile.absoluteFilePathIn(m_workspace)));
        error.setDescription(tr("File not found"));
        emit logErrors(QList<QQmlError>() << error);
    }

    m_object = object;
    m_activeWindow = window;

    if (m_activeWindow) {
        m_activeWindowConnections << connect(m_activeWindow.data(), &QWindow::widthChanged,
                                             this, &LiveNodeEngine::onSizeChanged);
        m_activeWindowConnections << connect(m_activeWindow.data(), &QWindow::heightChanged,
                                             this, &LiveNodeEngine::onSizeChanged);
        onSizeChanged();
    }

    emit activeDocumentChanged(m_activeFile);
    emit documentLoaded();
    emit activeWindowChanged(m_activeWindow);
    emit logErrors(errors);
}

/*!
 * This is an overloaded function provided for convenience. It is suitable for
 * use with QQmlApplicationEngine.
 *
 * Tries to resolve \a document against current workspace(). \a window is the
 * root object. \a errors (if any) will be added to log.
 */
void LiveNodeEngine::usePreloadedDocument(const QString &document, QQuickWindow *window,
                                          const QList<QQmlError> &errors)
{
    LIVE_ASSERT(m_activeFile.isNull(), return);

    LiveDocument resolved = LiveDocument::resolve(workspace(), document);
    if (resolved.isNull()) {
        qWarning() << "Failed to resolve preloaded document path:" << document
                   << "Workspace: " << workspace();
        return;
    }

    usePreloadedDocument(resolved, window, window, errors);
}

/*!
 * Loads or reloads the given \a document onto the QML view. Clears any caches.
 *
 * The activeDocumentChanged() signal is emitted when this results in change of
 * the activeDocument().
 *
 * \sa documentLoaded()
 */
void LiveNodeEngine::loadDocument(const LiveDocument& document)
{
    DEBUG << "LiveNodeEngine::loadDocument: " << document;

    LiveDocument oldActiveFile = m_activeFile;

    m_activeFile = document;

    if (m_activeFile != oldActiveFile)
        emit activeDocumentChanged(m_activeFile);

    if (!m_activeFile.isNull())
        reloadDocument();
}

/*!
 * Starts a timer to reload the view with a delay.
 *
 * A delay reload is important to avoid constant reloads, while many changes
 * appear.
 */
void LiveNodeEngine::delayReload()
{
    m_delayReload->start();
}

/*!
 * Checks if the QtQuick Controls module exists for the content adapters
 */
void LiveNodeEngine::checkQmlFeatures()
{
    foreach (const QString &importPath, m_qmlEngine->importPathList()) {
        QDir dir(importPath);
        if (dir.exists("QtQuick/Controls") &&
            dir.exists("QtQuick/Layouts") &&
            dir.exists("QtQuick/Dialogs")) {
            m_quickFeatures |= ContentAdapterInterface::QtQuickControls;
        }
    }
}

QUrl LiveNodeEngine::errorScreenUrl() const
{
    return m_quickFeatures.testFlag(ContentAdapterInterface::QtQuickControls)
        ? QUrl("qrc:/livert/error_qt5_controls.qml")
        : QUrl("qrc:/livert/error_qt5.qml");
}

/*!
 * Reloads the active QML document.
 *
 * Emits documentLoaded() when finished.
 *
 * If \l fallbackView is set, its \c source will be cleared, whether the view
 * was previously used or not.
 */
void LiveNodeEngine::reloadDocument()
{
    Q_ASSERT(qmlEngine());

    while (!m_activeWindowConnections.isEmpty()) {
        disconnect(m_activeWindowConnections.takeLast());
    }

    // Do this unconditionally!
    if (m_fallbackView)
        m_fallbackView->setSource(QUrl());

    m_activeWindow = 0;

    delete m_object;

    QQuickPixmap::purgeCache();
    m_qmlEngine->clearComponentCache();

    checkQmlFeatures();

    qInfo() << "----------------------------------------";
    qInfo() << "QmlLive: (Re)loading" << m_activeFile;

    emit clearLog();

    const QUrl originalUrl = QUrl::fromLocalFile(m_activeFile.absoluteFilePathIn(m_workspace));
    const QUrl url = queryDocumentViewer(originalUrl);

    auto showErrorScreen = [this] {
        Q_ASSERT(m_fallbackView);
        m_fallbackView->setResizeMode(QQuickView::SizeRootObjectToView);
        m_fallbackView->setSource(errorScreenUrl());
        m_activeWindow = m_fallbackView;
    };

    auto logError = [this, url](const QString &description) {
        QQmlError error;
        error.setObject(m_object);
        error.setUrl(url);
        error.setLine(0);
        error.setColumn(0);
        error.setDescription(description);
        emit logErrors(QList<QQmlError>() << error);
    };

    QScopedPointer<QQmlComponent> component(new QQmlComponent(m_qmlEngine));
    if (url.path().endsWith(QLatin1String(".qml"), Qt::CaseInsensitive)) {
        component->loadUrl(url);
        m_object = component->create();
    } else if (url == originalUrl) {
        logError(tr("LiveNodeEngine: Cannot display this file type"));
    } else {
        logError(tr("LiveNodeEngine: Internal error: Cannot display this file type"));
    }

    if (!component->isReady()) {
        if (component->isLoading()) {
            qCritical() << "Component did not load synchronously."
                        << "URL:" << url.toString()
                        << "(original URL:" << originalUrl.toString() << ")";
        } else {
            emit logErrors(component->errors());
            delete m_object;
            if (m_fallbackView)
                showErrorScreen();
        }
    } else if (QQuickWindow *window = qobject_cast<QQuickWindow *>(m_object)) {
        // TODO (why) is this needed?
        m_qmlEngine->setIncubationController(window->incubationController());
        m_activeWindow = window;
    } else if (QQuickItem *item = qobject_cast<QQuickItem *>(m_object)) {
        if (m_fallbackView) {
            const bool hasEmptySize = QSize(item->width(), item->height()).isEmpty();
            if ((m_activePlugin && m_activePlugin->isFullScreen()) || hasEmptySize)
                m_fallbackView->setResizeMode(QQuickView::SizeRootObjectToView);
            else
                m_fallbackView->setResizeMode(QQuickView::SizeViewToRootObject);
            component->setParent(m_fallbackView);
            m_fallbackView->setContent(url, component.take(), m_object);
            m_activeWindow = m_fallbackView;
        } else {
            logError(tr("LiveNodeEngine: Cannot display this component: "
                        "Root object is not a QQuickWindow and no LiveNodeEngine::fallbackView set."));
        }
    } else {
        logError(tr("LiveNodeEngine: Cannot display this component: "
                    "Root object is not a QQuickWindow nor a QQuickItem."));
        if (m_fallbackView)
            showErrorScreen();
    }

    if (m_activeWindow) {
        m_activeWindowConnections << connect(m_activeWindow.data(), &QWindow::widthChanged,
                                             this, &LiveNodeEngine::onSizeChanged);
        m_activeWindowConnections << connect(m_activeWindow.data(), &QWindow::heightChanged,
                                             this, &LiveNodeEngine::onSizeChanged);
        onSizeChanged();
    }

    emit documentLoaded();
    emit activeWindowChanged(m_activeWindow);

    if (m_fallbackView && m_fallbackView != m_activeWindow)
        m_fallbackView->close();

    // Delay showing the window after activeWindowChanged is handled by
    // WindowWidget::setHostedWindow() - it would be destroyed there anyway.
    // (Applies when this is instantiated for the bench.)
    if (m_activeWindow)
        m_activeWindow->show();
}

/*!
 * Updates \a content of the given workspace \a document when enabled.
 *
 * The behavior of this function is controlled by WorkspaceOptions passed to setWorkspace().
 */
void LiveNodeEngine::updateDocument(const LiveDocument &document, const QByteArray &content)
{
    if (!(m_workspaceOptions & AllowUpdates)) {
        return;
    }

    QString filePath = (m_workspaceOptions & UpdatesAsOverlay)
        ? m_overlayUrlInterceptor->reserve(document)
        : document.absoluteFilePathIn(m_workspace);

    QString dirPath = QFileInfo(filePath).absoluteDir().absolutePath();
    QDir().mkpath(dirPath);
    QFile file(filePath);
    if (!file.open(QIODevice::WriteOnly)) {
        qWarning() << "Unable to save file: " << file.errorString();
        return;
    }
    file.write(content);
    file.close();

    if (!m_activeFile.isNull())
        delayReload();
}


/*!
 * Allows to adapt a \a url to display not native QML documents (e.g. images).
 */
QUrl LiveNodeEngine::queryDocumentViewer(const QUrl& url)
{
    initPlugins();

    foreach (ContentAdapterInterface *adapter, m_plugins) {
        if (adapter->canAdapt(url)) {
            adapter->cleanUp();
            adapter->setAvailableFeatures(m_quickFeatures);

            m_activePlugin = adapter;

            return adapter->adapt(url, m_qmlEngine->rootContext());
        }
    }

    m_activePlugin = 0;

    return url;
}

/*!
 * Returns the current workspace path.
 */
QString LiveNodeEngine::workspace() const
{
    return m_workspace.absolutePath();
}

/*!
 * Sets the current workspace to \a path. Documents location will be adjusted based on
 * this workspace path. Certain features can be controled by passing \a options.
 *
 * \sa WorkspaceOptions
 */
void LiveNodeEngine::setWorkspace(const QString &path, WorkspaceOptions options)
{
    Q_ASSERT(qmlEngine());

    m_workspace = QDir(path);
    m_workspaceOptions = options;

    if (m_workspaceOptions & LoadDummyData)
        QmlHelper::loadDummyData(m_qmlEngine, m_workspace.absolutePath());

    if ((m_workspaceOptions & UpdatesAsOverlay) && !(m_workspaceOptions & AllowUpdates)) {
        qWarning() << "Got UpdatesAsOverlay without AllowUpdates. Enabling AllowUpdates.";
        m_workspaceOptions |= AllowUpdates;
    }

    if (m_workspaceOptions & UpdatesAsOverlay)
        initOverlay();

    emit workspaceChanged(workspace());
}

void LiveNodeEngine::initOverlay()
{
    Q_ASSERT(m_workspaceOptions & UpdatesAsOverlay);

    QSettings settings;
    QString overlayBasePath = QDir::tempPath() + QDir::separator() + QLatin1String(OVERLAY_PATH_PREFIX);
    if (!settings.organizationName().isEmpty()) // See QCoreApplication::organizationName's docs
        overlayBasePath += settings.organizationName() + QLatin1Char(OVERLAY_PATH_SEPARATOR);
    overlayBasePath += settings.applicationName();

    // With temporary overlay allow parallel execution
    QTemporaryDir overlay(overlayBasePath);
    if (!overlay.isValid())
#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
        qFatal("Failed to create overlay directory: %s", qPrintable(overlay.errorString()));
#else
        qFatal("Failed to create overlay directory");
#endif
    overlay.setAutoRemove(false);

    m_overlayUrlInterceptor = new OverlayUrlInterceptor(m_workspace.path(),
        overlay.path(), qmlEngine()->urlInterceptor(), this);
    qmlEngine()->setUrlInterceptor(m_overlayUrlInterceptor);
}

void LiveNodeEngine::destroyOverlay()
{
    if (m_workspaceOptions & UpdatesAsOverlay) {
        // Better be paranoid than sorry.
        bool safe = m_overlayUrlInterceptor->overlay().absolutePath().startsWith(QDir::tempPath() + QDir::separator());
        Q_ASSERT(safe);
        if (!safe || !m_overlayUrlInterceptor->overlay().removeRecursively())
            qWarning() << "Failed to remove overlay directory";
    }
}

/*!
 * Sets the pluginPath to \a path.
 *
 * The pluginPath will be used to load QmlLive plugins
 * \sa {ContentPlugin Example}
 */
void LiveNodeEngine::setPluginPath(const QString &path)
{
    m_pluginFactory->setPluginPath(path);
}

/*!
 * Returns the current pluginPath
 */
QString LiveNodeEngine::pluginPath() const
{
    return m_pluginFactory->pluginPath();
}

/*!
 * Returns the current active document.
 */
LiveDocument LiveNodeEngine::activeDocument() const
{
    return m_activeFile;
}

/*!
 * Returns the active content adapter plugin
 */
ContentAdapterInterface *LiveNodeEngine::activePlugin() const
{
    return m_activePlugin;
}

/*!
 * Returns the current active window.
 * \sa activeWindowChanged()
 */
QQuickWindow *LiveNodeEngine::activeWindow() const
{
    return m_activeWindow;
}

/*!
 * Loads all plugins found in the Pluginpath
 */
void LiveNodeEngine::initPlugins()
{
    if (m_plugins.isEmpty()) {
        m_pluginFactory->load();
        m_plugins.append(m_pluginFactory->plugins());
        m_plugins.append(new ImageAdapter(this));
        m_plugins.append(new FontAdapter(this));
    }
}

/*!
 * Handles size changes and updates the view according
 */
void LiveNodeEngine::onSizeChanged()
{
    Q_ASSERT(m_activeWindow != 0);

    if (m_activeWindow->width() != -1 && m_activeWindow->height() != -1) {
        m_runtime->setScreenWidth(m_activeWindow->width());
        m_runtime->setScreenHeight(m_activeWindow->height());
    }

    setRotation(m_rotation);
}

/*!
 * \fn void LiveNodeEngine::activeDocumentChanged(const LiveDocument& document)
 *
 * The document \a document was loaded with loadDocument() and is now the
 * activeDocument(). This signal is only emitted when the new document differs
 * from the previously loaded one.
 *
 * \sa documentLoaded()
 */

/*!
 * \fn void LiveNodeEngine::clearLog()
 *
 * Requested to clear the log
 */

/*!
 * \fn void LiveNodeEngine::logIgnoreMessages(bool)
 *
 * Requsted to ignore the Messages when \a on is true
 */

/*!
 * \fn void LiveNodeEngine::documentLoaded()
 *
 * The signal is emitted when the document has finished loading.
 */

/*!
 * \fn void LiveNodeEngine::activeWindowChanged(QQuickWindow *window)
 *
 * The signal is emitted when the activeWindow has changed by changing or
 * reloading the document. \a window is the newly activated window.
 */

/*!
 * \fn void LiveNodeEngine::logErrors(const QList<QQmlError> &errors)
 *
 * Log the Errors \a errors
 */

/*!
 * \fn void LiveNodeEngine::workspaceChanged(const QString &workspace)
 *
 * This signal is emitted after workspace is changed with setWorkspace(). \a
 * workspace is the new workspace path.
 *
 * \sa workspace()
 */

#include "livenodeengine.moc"