pluginspec.cpp 30 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
con's avatar
con committed
5
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
hjk's avatar
hjk committed
7
** Contact: Nokia Corporation (info@qt.nokia.com)
con's avatar
con committed
8
**
9
**
10
** GNU Lesser General Public License Usage
11
**
hjk's avatar
hjk committed
12
13
14
15
16
17
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21
22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23
24
25
26
27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
Tobias Hunger's avatar
Tobias Hunger committed
29
** Nokia at info@qt.nokia.com.
con's avatar
con committed
30
**
31
**************************************************************************/
hjk's avatar
hjk committed
32

con's avatar
con committed
33
#include "pluginspec.h"
34

con's avatar
con committed
35
36
37
38
39
#include "pluginspec_p.h"
#include "iplugin.h"
#include "iplugin_p.h"
#include "pluginmanager.h"

Alessandro Portale's avatar
Alessandro Portale committed
40
#include <QtCore/QDir>
con's avatar
con committed
41
42
43
44
45
46
47
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
#include <QtCore/QXmlStreamReader>
#include <QtCore/QRegExp>
#include <QtCore/QCoreApplication>
#include <QtDebug>

48
#ifdef Q_OS_LINUX
49
50
// Using the patched version breaks on Fedora 10, KDE4.2.2/Qt4.5.
#   define USE_UNPATCHED_QPLUGINLOADER 1
51
52
53
#else
#   define USE_UNPATCHED_QPLUGINLOADER 1
#endif
54
55
56

#if USE_UNPATCHED_QPLUGINLOADER

57
58
#   include <QtCore/QPluginLoader>
    typedef QT_PREPEND_NAMESPACE(QPluginLoader) PluginLoader;
59
60
61

#else

62
63
#   include "patchedpluginloader.cpp"
    typedef PatchedPluginLoader PluginLoader;
64
65
66

#endif

con's avatar
con committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*!
    \class ExtensionSystem::PluginDependency
    \brief Struct that contains the name and required compatible version number of a plugin's dependency.

    This reflects the data of a dependency tag in the plugin's xml description file.
    The name and version are used to resolve the dependency, i.e. a plugin with the given name and
    plugin \c {compatibility version <= dependency version <= plugin version} is searched for.

    See also ExtensionSystem::IPlugin for more information about plugin dependencies and
    version matching.
*/

/*!
    \variable ExtensionSystem::PluginDependency::name
    String identifier of the plugin.
*/

/*!
    \variable ExtensionSystem::PluginDependency::version
    Version string that a plugin must match to fill this dependency.
*/

con's avatar
con committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*!
    \variable ExtensionSystem::PluginDependency::type
    Defines whether the dependency is required or optional.
    \sa ExtensionSystem::PluginDependency::Type
*/

/*!
    \enum ExtensionSystem::PluginDependency::Type
    Whether the dependency is required or optional.
    \value Required
           Dependency needs to be there.
    \value Optional
           Dependency is not necessarily needed. You need to make sure that
           the plugin is able to load without this dependency installed, so
           for example you may not link to the dependency's library.
*/

con's avatar
con committed
106
107
108
109
110
111
/*!
    \class ExtensionSystem::PluginSpec
    \brief Contains the information of the plugins xml description file and
    information about the plugin's current state.

    The plugin spec is also filled with more information as the plugin
Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
112
    goes through its loading process (see PluginSpec::State).
con's avatar
con committed
113
114
115
116
117
118
119
120
121
122
123
124
125
    If an error occurs, the plugin spec is the place to look for the
    error details.
*/

/*!
    \enum ExtensionSystem::PluginSpec::State

    The plugin goes through several steps while being loaded.
    The state gives a hint on what went wrong in case of an error.

    \value  Invalid
            Starting point: Even the xml description file was not read.
    \value  Read
Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
126
            The xml description file has been successfully read, and its
con's avatar
con committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
            information is available via the PluginSpec.
    \value  Resolved
            The dependencies given in the description file have been
            successfully found, and are available via the dependencySpecs() method.
    \value  Loaded
            The plugin's library is loaded and the plugin instance created
            (available through plugin()).
    \value  Initialized
            The plugin instance's IPlugin::initialize() method has been called
            and returned a success value.
    \value  Running
            The plugin's dependencies are successfully initialized and
            extensionsInitialized has been called. The loading process is
            complete.
    \value Stopped
142
            The plugin has been shut down, i.e. the plugin's IPlugin::aboutToShutdown() method has been called.
con's avatar
con committed
143
144
145
    \value Deleted
            The plugin instance has been deleted.
*/
con's avatar
con committed
146

con's avatar
con committed
147
148
149
using namespace ExtensionSystem;
using namespace ExtensionSystem::Internal;

con's avatar
con committed
150
151
152
153
154
155
156
157
158
/*!
    \fn uint qHash(const ExtensionSystem::PluginDependency &value)
    \internal
*/
uint ExtensionSystem::qHash(const ExtensionSystem::PluginDependency &value)
{
    return qHash(value.name);
}

con's avatar
con committed
159
/*!
160
    \fn bool PluginDependency::operator==(const PluginDependency &other) const
con's avatar
con committed
161
162
    \internal
*/
con's avatar
con committed
163
bool PluginDependency::operator==(const PluginDependency &other) const
con's avatar
con committed
164
{
con's avatar
con committed
165
    return name == other.name && version == other.version && type == other.type;
con's avatar
con committed
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
}

/*!
    \fn PluginSpec::PluginSpec()
    \internal
*/
PluginSpec::PluginSpec()
    : d(new PluginSpecPrivate(this))
{
}

/*!
    \fn PluginSpec::~PluginSpec()
    \internal
*/
PluginSpec::~PluginSpec()
{
    delete d;
    d = 0;
}

/*!
    \fn QString PluginSpec::name() const
    The plugin name. This is valid after the PluginSpec::Read state is reached.
*/
QString PluginSpec::name() const
{
    return d->name;
}

/*!
    \fn QString PluginSpec::version() const
    The plugin version. This is valid after the PluginSpec::Read state is reached.
*/
QString PluginSpec::version() const
{
    return d->version;
}

/*!
    \fn QString PluginSpec::compatVersion() const
    The plugin compatibility version. This is valid after the PluginSpec::Read state is reached.
*/
QString PluginSpec::compatVersion() const
{
    return d->compatVersion;
}

/*!
    \fn QString PluginSpec::vendor() const
    The plugin vendor. This is valid after the PluginSpec::Read state is reached.
*/
QString PluginSpec::vendor() const
{
    return d->vendor;
}

/*!
    \fn QString PluginSpec::copyright() const
    The plugin copyright. This is valid after the PluginSpec::Read state is reached.
*/
QString PluginSpec::copyright() const
{
    return d->copyright;
}

/*!
    \fn QString PluginSpec::license() const
    The plugin license. This is valid after the PluginSpec::Read state is reached.
*/
QString PluginSpec::license() const
{
    return d->license;
}

/*!
    \fn QString PluginSpec::description() const
    The plugin description. This is valid after the PluginSpec::Read state is reached.
*/
QString PluginSpec::description() const
{
    return d->description;
}

/*!
    \fn QString PluginSpec::url() const
    The plugin url where you can find more information about the plugin. This is valid after the PluginSpec::Read state is reached.
*/
QString PluginSpec::url() const
{
    return d->url;
}

259
260
261
262
263
264
265
266
267
268
269
/*!
    \fn QString PluginSpec::category() const
    The category that the plugin belongs to. Categories are groups of plugins which allow for keeping them together in the UI.
    Returns an empty string if the plugin does not belong to a category.
*/
QString PluginSpec::category() const
{
    return d->category;
}

/*!
270
271
272
273
274
275
276
277
    \fn bool PluginSpec::isExperimental() const
    Returns if the plugin has its experimental flag set.
*/
bool PluginSpec::isExperimental() const
{
    return d->experimental;
}

278
279
280
281
282
283
284
285
286
287
/*!
    Returns if the plugin is disabled by default.
    This might be because the plugin is experimental, or because
    the plugin manager's settings define it as disabled by default.
*/
bool PluginSpec::isDisabledByDefault() const
{
    return d->disabledByDefault;
}

288
289
290
/*!
    \fn bool PluginSpec::isEnabled() const
    Returns if the plugin is loaded at startup. True by default - the user can change it from the Plugin settings.
291
*/
con's avatar
con committed
292
bool PluginSpec::isEnabled() const
293
{
con's avatar
con committed
294
    return d->enabled;
295
296
}

297
298
299
300
301
302
/*!
    \fn bool PluginSpec::isDisabledIndirectly() const
    Returns true if loading was not done due to user unselecting this plugin or its dependencies,
    or if command-line parameter -noload was used.
*/
bool PluginSpec::isDisabledIndirectly() const
303
{
304
    return d->disabledIndirectly;
305
306
}

con's avatar
con committed
307
308
309
310
311
312
313
314
315
316
/*!
    \fn QList<PluginDependency> PluginSpec::dependencies() const
    The plugin dependencies. This is valid after the PluginSpec::Read state is reached.
*/
QList<PluginDependency> PluginSpec::dependencies() const
{
    return d->dependencies;
}

/*!
317
    \fn PluginSpec::PluginArgumentDescriptions PluginSpec::argumentDescriptions() const
con's avatar
con committed
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
    Returns a list of descriptions of command line arguments the plugin processes.
*/

PluginSpec::PluginArgumentDescriptions PluginSpec::argumentDescriptions() const
{
    return d->argumentDescriptions;
}

/*!
    \fn QString PluginSpec::location() const
    The absolute path to the directory containing the plugin xml description file
    this PluginSpec corresponds to.
*/
QString PluginSpec::location() const
{
    return d->location;
}

/*!
    \fn QString PluginSpec::filePath() const
    The absolute path to the plugin xml description file (including the file name)
    this PluginSpec corresponds to.
*/
QString PluginSpec::filePath() const
{
    return d->filePath;
}

/*!
    \fn QStringList PluginSpec::arguments() const
    Command line arguments specific to that plugin. Set at startup
*/

QStringList PluginSpec::arguments() const
{
    return d->arguments;
}

/*!
    \fn void PluginSpec::setArguments(const QStringList &arguments)
    Set the command line arguments specific to that plugin to \a arguments.
*/

void PluginSpec::setArguments(const QStringList &arguments)
{
    d->arguments = arguments;
}

/*!
    \fn PluginSpec::addArgument(const QString &argument)
    Adds \a argument to the command line arguments specific to that plugin.
*/

void PluginSpec::addArgument(const QString &argument)
{
    d->arguments.push_back(argument);
}


/*!
    \fn PluginSpec::State PluginSpec::state() const
    The state in which the plugin currently is.
    See the description of the PluginSpec::State enum for details.
*/
PluginSpec::State PluginSpec::state() const
{
    return d->state;
}

/*!
    \fn bool PluginSpec::hasError() const
    Returns whether an error occurred while reading/starting the plugin.
*/
bool PluginSpec::hasError() const
{
    return d->hasError;
}

/*!
    \fn QString PluginSpec::errorString() const
    Detailed, possibly multi-line, error description in case of an error.
*/
QString PluginSpec::errorString() const
{
    return d->errorString;
}

/*!
    \fn bool PluginSpec::provides(const QString &pluginName, const QString &version) const
    Returns if this plugin can be used to fill in a dependency of the given
    \a pluginName and \a version.

        \sa PluginSpec::dependencies()
*/
bool PluginSpec::provides(const QString &pluginName, const QString &version) const
{
    return d->provides(pluginName, version);
}

/*!
    \fn IPlugin *PluginSpec::plugin() const
    The corresponding IPlugin instance, if the plugin library has already been successfully loaded,
    i.e. the PluginSpec::Loaded state is reached.
*/
IPlugin *PluginSpec::plugin() const
{
    return d->plugin;
}

/*!
    \fn QList<PluginSpec *> PluginSpec::dependencySpecs() const
    Returns the list of dependencies, already resolved to existing plugin specs.
    Valid if PluginSpec::Resolved state is reached.

    \sa PluginSpec::dependencies()
*/
con's avatar
con committed
434
QHash<PluginDependency, PluginSpec *> PluginSpec::dependencySpecs() const
con's avatar
con committed
435
436
437
438
439
440
441
{
    return d->dependencySpecs;
}

//==========PluginSpecPrivate==================

namespace {
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
    const char PLUGIN[] = "plugin";
    const char PLUGIN_NAME[] = "name";
    const char PLUGIN_VERSION[] = "version";
    const char PLUGIN_COMPATVERSION[] = "compatVersion";
    const char PLUGIN_EXPERIMENTAL[] = "experimental";
    const char VENDOR[] = "vendor";
    const char COPYRIGHT[] = "copyright";
    const char LICENSE[] = "license";
    const char DESCRIPTION[] = "description";
    const char URL[] = "url";
    const char CATEGORY[] = "category";
    const char DEPENDENCYLIST[] = "dependencyList";
    const char DEPENDENCY[] = "dependency";
    const char DEPENDENCY_NAME[] = "name";
    const char DEPENDENCY_VERSION[] = "version";
    const char DEPENDENCY_TYPE[] = "type";
    const char DEPENDENCY_TYPE_SOFT[] = "optional";
    const char DEPENDENCY_TYPE_HARD[] = "required";
    const char ARGUMENTLIST[] = "argumentList";
    const char ARGUMENT[] = "argument";
    const char ARGUMENT_NAME[] = "name";
    const char ARGUMENT_PARAMETER[] = "parameter";
con's avatar
con committed
464
465
466
467
468
469
}
/*!
    \fn PluginSpecPrivate::PluginSpecPrivate(PluginSpec *spec)
    \internal
*/
PluginSpecPrivate::PluginSpecPrivate(PluginSpec *spec)
470
    :
471
472
    experimental(false),
    disabledByDefault(false),
con's avatar
con committed
473
    enabled(true),
474
    disabledIndirectly(false),
475
    plugin(0),
con's avatar
con committed
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
    state(PluginSpec::Invalid),
    hasError(false),
    q(spec)
{
}

/*!
    \fn bool PluginSpecPrivate::read(const QString &fileName)
    \internal
*/
bool PluginSpecPrivate::read(const QString &fileName)
{
    name
        = version
        = compatVersion
        = vendor
        = copyright
        = license
        = description
        = url
496
        = category
con's avatar
con committed
497
498
499
500
501
502
503
504
        = location
        = "";
    state = PluginSpec::Invalid;
    hasError = false;
    errorString = "";
    dependencies.clear();
    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly))
505
        return reportError(tr("Cannot open file %1 for reading: %2")
506
                           .arg(QDir::toNativeSeparators(file.fileName()), file.errorString()));
con's avatar
con committed
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
    QFileInfo fileInfo(file);
    location = fileInfo.absolutePath();
    filePath = fileInfo.absoluteFilePath();
    QXmlStreamReader reader(&file);
    while (!reader.atEnd()) {
        reader.readNext();
        switch (reader.tokenType()) {
        case QXmlStreamReader::StartElement:
            readPluginSpec(reader);
            break;
        default:
            break;
        }
    }
    if (reader.hasError())
        return reportError(tr("Error parsing file %1: %2, at line %3, column %4")
                .arg(file.fileName())
                .arg(reader.errorString())
                .arg(reader.lineNumber())
                .arg(reader.columnNumber()));
    state = PluginSpec::Read;
    return true;
}

con's avatar
con committed
531
void PluginSpec::setEnabled(bool value)
532
{
con's avatar
con committed
533
    d->enabled = value;
534
535
}

536
537
538
539
540
void PluginSpec::setDisabledByDefault(bool value)
{
    d->disabledByDefault = value;
}

541
542
543
544
545
void PluginSpec::setDisabledIndirectly(bool value)
{
    d->disabledIndirectly = value;
}

con's avatar
con committed
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
/*!
    \fn bool PluginSpecPrivate::reportError(const QString &err)
    \internal
*/
bool PluginSpecPrivate::reportError(const QString &err)
{
    errorString = err;
    hasError = true;
    return false;
}

static inline QString msgAttributeMissing(const char *elt, const char *attribute)
{
    return QCoreApplication::translate("PluginSpec", "'%1' misses attribute '%2'").arg(QLatin1String(elt), QLatin1String(attribute));
}

static inline QString msgInvalidFormat(const char *content)
{
    return QCoreApplication::translate("PluginSpec", "'%1' has invalid format").arg(content);
}

static inline QString msgInvalidElement(const QString &name)
{
    return QCoreApplication::translate("PluginSpec", "Invalid element '%1'").arg(name);
}

static inline QString msgUnexpectedClosing(const QString &name)
{
    return QCoreApplication::translate("PluginSpec", "Unexpected closing element '%1'").arg(name);
}

static inline QString msgUnexpectedToken()
{
    return QCoreApplication::translate("PluginSpec", "Unexpected token");
}

/*!
    \fn void PluginSpecPrivate::readPluginSpec(QXmlStreamReader &reader)
    \internal
*/
void PluginSpecPrivate::readPluginSpec(QXmlStreamReader &reader)
{
    QString element = reader.name().toString();
    if (element != QString(PLUGIN)) {
        reader.raiseError(QCoreApplication::translate("PluginSpec", "Expected element '%1' as top level element").arg(PLUGIN));
        return;
    }
    name = reader.attributes().value(PLUGIN_NAME).toString();
    if (name.isEmpty()) {
        reader.raiseError(msgAttributeMissing(PLUGIN, PLUGIN_NAME));
        return;
    }
    version = reader.attributes().value(PLUGIN_VERSION).toString();
    if (version.isEmpty()) {
        reader.raiseError(msgAttributeMissing(PLUGIN, PLUGIN_VERSION));
        return;
    }
    if (!isValidVersion(version)) {
        reader.raiseError(msgInvalidFormat(PLUGIN_VERSION));
        return;
    }
    compatVersion = reader.attributes().value(PLUGIN_COMPATVERSION).toString();
    if (!compatVersion.isEmpty() && !isValidVersion(compatVersion)) {
        reader.raiseError(msgInvalidFormat(PLUGIN_COMPATVERSION));
        return;
    } else if (compatVersion.isEmpty()) {
        compatVersion = version;
    }
614
615
616
617
618
619
620
    QString experimentalString = reader.attributes().value(PLUGIN_EXPERIMENTAL).toString();
    experimental = (experimentalString.compare(QLatin1String("true"), Qt::CaseInsensitive) == 0);
    if (!experimentalString.isEmpty() && !experimental
            && experimentalString.compare(QLatin1String("false"), Qt::CaseInsensitive) != 0) {
        reader.raiseError(msgInvalidFormat(PLUGIN_EXPERIMENTAL));
        return;
    }
621
    disabledByDefault = experimental;
622
    enabled = !experimental;
con's avatar
con committed
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
    while (!reader.atEnd()) {
        reader.readNext();
        switch (reader.tokenType()) {
        case QXmlStreamReader::StartElement:
            element = reader.name().toString();
            if (element == VENDOR)
                vendor = reader.readElementText().trimmed();
            else if (element == COPYRIGHT)
                copyright = reader.readElementText().trimmed();
            else if (element == LICENSE)
                license = reader.readElementText().trimmed();
            else if (element == DESCRIPTION)
                description = reader.readElementText().trimmed();
            else if (element == URL)
                url = reader.readElementText().trimmed();
638
639
            else if (element == CATEGORY)
                category = reader.readElementText().trimmed();
con's avatar
con committed
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
            else if (element == DEPENDENCYLIST)
                readDependencies(reader);
            else if (element == ARGUMENTLIST)
                readArgumentDescriptions(reader);
            else
                reader.raiseError(msgInvalidElement(name));
            break;
        case QXmlStreamReader::EndDocument:
        case QXmlStreamReader::Comment:
        case QXmlStreamReader::EndElement:
        case QXmlStreamReader::Characters:
            break;
        default:
            reader.raiseError(msgUnexpectedToken());
            break;
        }
    }
}

/*!
    \fn void PluginSpecPrivate::readArgumentDescriptions(QXmlStreamReader &reader)
    \internal
*/

void PluginSpecPrivate::readArgumentDescriptions(QXmlStreamReader &reader)
{
    QString element;
    while (!reader.atEnd()) {
        reader.readNext();
        switch (reader.tokenType()) {
        case QXmlStreamReader::StartElement:
            element = reader.name().toString();
            if (element == ARGUMENT) {
                readArgumentDescription(reader);
            } else {
                reader.raiseError(msgInvalidElement(name));
            }
            break;
        case QXmlStreamReader::Comment:
        case QXmlStreamReader::Characters:
            break;
        case QXmlStreamReader::EndElement:
            element = reader.name().toString();
            if (element == ARGUMENTLIST)
                return;
            reader.raiseError(msgUnexpectedClosing(element));
            break;
        default:
            reader.raiseError(msgUnexpectedToken());
            break;
        }
    }
}

/*!
    \fn void PluginSpecPrivate::readArgumentDescription(QXmlStreamReader &reader)
    \internal
*/
void PluginSpecPrivate::readArgumentDescription(QXmlStreamReader &reader)
{
    PluginArgumentDescription arg;
    arg.name = reader.attributes().value(ARGUMENT_NAME).toString();
    if (arg.name.isEmpty()) {
        reader.raiseError(msgAttributeMissing(ARGUMENT, ARGUMENT_NAME));
        return;
    }
    arg.parameter = reader.attributes().value(ARGUMENT_PARAMETER).toString();
    arg.description = reader.readElementText();
    if (reader.tokenType() != QXmlStreamReader::EndElement)
        reader.raiseError(msgUnexpectedToken());
    argumentDescriptions.push_back(arg);
}

/*!
    \fn void PluginSpecPrivate::readDependencies(QXmlStreamReader &reader)
    \internal
*/
void PluginSpecPrivate::readDependencies(QXmlStreamReader &reader)
{
    QString element;
    while (!reader.atEnd()) {
        reader.readNext();
        switch (reader.tokenType()) {
        case QXmlStreamReader::StartElement:
            element = reader.name().toString();
            if (element == DEPENDENCY) {
                readDependencyEntry(reader);
            } else {
                reader.raiseError(msgInvalidElement(name));
            }
            break;
        case QXmlStreamReader::Comment:
        case QXmlStreamReader::Characters:
            break;
        case QXmlStreamReader::EndElement:
            element = reader.name().toString();
            if (element == DEPENDENCYLIST)
                return;
            reader.raiseError(msgUnexpectedClosing(element));
            break;
        default:
            reader.raiseError(msgUnexpectedToken());
            break;
        }
    }
}

/*!
    \fn void PluginSpecPrivate::readDependencyEntry(QXmlStreamReader &reader)
    \internal
*/
void PluginSpecPrivate::readDependencyEntry(QXmlStreamReader &reader)
{
    PluginDependency dep;
    dep.name = reader.attributes().value(DEPENDENCY_NAME).toString();
    if (dep.name.isEmpty()) {
        reader.raiseError(msgAttributeMissing(DEPENDENCY, DEPENDENCY_NAME));
        return;
    }
    dep.version = reader.attributes().value(DEPENDENCY_VERSION).toString();
    if (!dep.version.isEmpty() && !isValidVersion(dep.version)) {
        reader.raiseError(msgInvalidFormat(DEPENDENCY_VERSION));
        return;
    }
con's avatar
con committed
764
765
766
767
768
769
770
771
772
773
774
775
    dep.type = PluginDependency::Required;
    if (reader.attributes().hasAttribute(DEPENDENCY_TYPE)) {
        QString typeValue = reader.attributes().value(DEPENDENCY_TYPE).toString();
        if (typeValue == QLatin1String(DEPENDENCY_TYPE_HARD)) {
            dep.type = PluginDependency::Required;
        } else if (typeValue == QLatin1String(DEPENDENCY_TYPE_SOFT)) {
            dep.type = PluginDependency::Optional;
        } else {
            reader.raiseError(msgInvalidFormat(DEPENDENCY_TYPE));
            return;
        }
    }
con's avatar
con committed
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
    dependencies.append(dep);
    reader.readNext();
    if (reader.tokenType() != QXmlStreamReader::EndElement)
        reader.raiseError(msgUnexpectedToken());
}

/*!
    \fn bool PluginSpecPrivate::provides(const QString &pluginName, const QString &pluginVersion) const
    \internal
*/
bool PluginSpecPrivate::provides(const QString &pluginName, const QString &pluginVersion) const
{
    if (QString::compare(pluginName, name, Qt::CaseInsensitive) != 0)
        return false;
    return (versionCompare(version, pluginVersion) >= 0) && (versionCompare(compatVersion, pluginVersion) <= 0);
}

/*!
    \fn QRegExp &PluginSpecPrivate::versionRegExp()
    \internal
*/
QRegExp &PluginSpecPrivate::versionRegExp()
{
    static QRegExp reg("([0-9]+)(?:[.]([0-9]+))?(?:[.]([0-9]+))?(?:_([0-9]+))?");
    return reg;
}
/*!
    \fn bool PluginSpecPrivate::isValidVersion(const QString &version)
    \internal
*/
bool PluginSpecPrivate::isValidVersion(const QString &version)
{
    return versionRegExp().exactMatch(version);
}

/*!
    \fn int PluginSpecPrivate::versionCompare(const QString &version1, const QString &version2)
    \internal
*/
int PluginSpecPrivate::versionCompare(const QString &version1, const QString &version2)
{
    QRegExp reg1 = versionRegExp();
    QRegExp reg2 = versionRegExp();
    if (!reg1.exactMatch(version1))
        return 0;
    if (!reg2.exactMatch(version2))
        return 0;
    int number1;
    int number2;
    for (int i = 0; i < 4; ++i) {
        number1 = reg1.cap(i+1).toInt();
        number2 = reg2.cap(i+1).toInt();
        if (number1 < number2)
            return -1;
        if (number1 > number2)
            return 1;
    }
    return 0;
}

/*!
837
    \fn bool PluginSpecPrivate::resolveDependencies(const QList<PluginSpec *> &specs)
con's avatar
con committed
838
839
    \internal
*/
840
bool PluginSpecPrivate::resolveDependencies(const QList<PluginSpec *> &specs)
con's avatar
con committed
841
842
843
844
845
846
847
848
849
850
{
    if (hasError)
        return false;
    if (state == PluginSpec::Resolved)
        state = PluginSpec::Read; // Go back, so we just re-resolve the dependencies.
    if (state != PluginSpec::Read) {
        errorString = QCoreApplication::translate("PluginSpec", "Resolving dependencies failed because state != Read");
        hasError = true;
        return false;
    }
con's avatar
con committed
851
    QHash<PluginDependency, PluginSpec *> resolvedDependencies;
con's avatar
con committed
852
853
    foreach (const PluginDependency &dependency, dependencies) {
        PluginSpec *found = 0;
854

con's avatar
con committed
855
856
857
858
859
860
861
        foreach (PluginSpec *spec, specs) {
            if (spec->provides(dependency.name, dependency.version)) {
                found = spec;
                break;
            }
        }
        if (!found) {
con's avatar
con committed
862
863
864
865
866
867
868
            if (dependency.type == PluginDependency::Required) {
                hasError = true;
                if (!errorString.isEmpty())
                    errorString.append(QLatin1Char('\n'));
                errorString.append(QCoreApplication::translate("PluginSpec", "Could not resolve dependency '%1(%2)'")
                    .arg(dependency.name).arg(dependency.version));
            }
con's avatar
con committed
869
870
            continue;
        }
con's avatar
con committed
871
        resolvedDependencies.insert(dependency, found);
con's avatar
con committed
872
873
874
    }
    if (hasError)
        return false;
875

con's avatar
con committed
876
    dependencySpecs = resolvedDependencies;
877

878
    state = PluginSpec::Resolved;
879

con's avatar
con committed
880
881
882
    return true;
}

883
884
885
886
887
void PluginSpecPrivate::disableIndirectlyIfDependencyDisabled()
{
    if (!enabled)
        return;

888
889
890
    if (disabledIndirectly)
        return;

con's avatar
con committed
891
892
893
894
895
896
    QHashIterator<PluginDependency, PluginSpec *> it(dependencySpecs);
    while (it.hasNext()) {
        it.next();
        if (it.key().type == PluginDependency::Optional)
            continue;
        PluginSpec *dependencySpec = it.value();
897
898
899
900
901
902
903
        if (dependencySpec->isDisabledIndirectly() || !dependencySpec->isEnabled()) {
            disabledIndirectly = true;
            break;
        }
    }
}

con's avatar
con committed
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
/*!
    \fn bool PluginSpecPrivate::loadLibrary()
    \internal
*/
bool PluginSpecPrivate::loadLibrary()
{
    if (hasError)
        return false;
    if (state != PluginSpec::Resolved) {
        if (state == PluginSpec::Loaded)
            return true;
        errorString = QCoreApplication::translate("PluginSpec", "Loading the library failed because state != Resolved");
        hasError = true;
        return false;
    }
#ifdef QT_NO_DEBUG

#ifdef Q_OS_WIN
    QString libName = QString("%1/%2.dll").arg(location).arg(name);
#elif defined(Q_OS_MAC)
    QString libName = QString("%1/lib%2.dylib").arg(location).arg(name);
#else
    QString libName = QString("%1/lib%2.so").arg(location).arg(name);
#endif

#else //Q_NO_DEBUG

#ifdef Q_OS_WIN
    QString libName = QString("%1/%2d.dll").arg(location).arg(name);
#elif defined(Q_OS_MAC)
    QString libName = QString("%1/lib%2_debug.dylib").arg(location).arg(name);
#else
    QString libName = QString("%1/lib%2.so").arg(location).arg(name);
#endif

#endif

941
    PluginLoader loader(libName);
con's avatar
con committed
942
943
    if (!loader.load()) {
        hasError = true;
Alessandro Portale's avatar
Alessandro Portale committed
944
945
        errorString = QDir::toNativeSeparators(libName)
            + QString::fromLatin1(": ") + loader.errorString();
con's avatar
con committed
946
947
948
949
950
        return false;
    }
    IPlugin *pluginObject = qobject_cast<IPlugin*>(loader.instance());
    if (!pluginObject) {
        hasError = true;
951
        errorString = QCoreApplication::translate("PluginSpec", "Plugin is not valid (does not derive from IPlugin)");
con's avatar
con committed
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
        loader.unload();
        return false;
    }
    state = PluginSpec::Loaded;
    plugin = pluginObject;
    plugin->d->pluginSpec = q;
    return true;
}

/*!
    \fn bool PluginSpecPrivate::initializePlugin()
    \internal
*/
bool PluginSpecPrivate::initializePlugin()
{
    if (hasError)
        return false;
    if (state != PluginSpec::Loaded) {
        if (state == PluginSpec::Initialized)
            return true;
        errorString = QCoreApplication::translate("PluginSpec", "Initializing the plugin failed because state != Loaded");
        hasError = true;
        return false;
    }
    if (!plugin) {
        errorString = QCoreApplication::translate("PluginSpec", "Internal error: have no plugin instance to initialize");
        hasError = true;
        return false;
    }
    QString err;
    if (!plugin->initialize(arguments, &err)) {
        errorString = QCoreApplication::translate("PluginSpec", "Plugin initialization failed: %1").arg(err);
        hasError = true;
        return false;
    }
    state = PluginSpec::Initialized;
    return true;
}

/*!
    \fn bool PluginSpecPrivate::initializeExtensions()
    \internal
*/
bool PluginSpecPrivate::initializeExtensions()
{
    if (hasError)
        return false;
    if (state != PluginSpec::Initialized) {
        if (state == PluginSpec::Running)
            return true;
        errorString = QCoreApplication::translate("PluginSpec", "Cannot perform extensionsInitialized because state != Initialized");
        hasError = true;
        return false;
    }
    if (!plugin) {
        errorString = QCoreApplication::translate("PluginSpec", "Internal error: have no plugin instance to perform extensionsInitialized");
        hasError = true;
        return false;
    }
    plugin->extensionsInitialized();
    state = PluginSpec::Running;
    return true;
}

/*!
    \fn bool PluginSpecPrivate::stop()
    \internal
*/
1020
IPlugin::ShutdownFlag PluginSpecPrivate::stop()
con's avatar
con committed
1021
1022
{
    if (!plugin)
1023
        return IPlugin::SynchronousShutdown;
con's avatar
con committed
1024
    state = PluginSpec::Stopped;
1025
    return plugin->aboutToShutdown();
con's avatar
con committed
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
}

/*!
    \fn bool PluginSpecPrivate::kill()
    \internal
*/
void PluginSpecPrivate::kill()
{
    if (!plugin)
        return;
    delete plugin;
    plugin = 0;
    state = PluginSpec::Deleted;
}