librarydetailscontroller.cpp 40.3 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Tobias Hunger's avatar
Tobias Hunger committed
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
Tobias Hunger's avatar
Tobias Hunger committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
Tobias Hunger's avatar
Tobias Hunger committed
7
**
hjk's avatar
hjk committed
8
9
10
11
12
13
14
** Commercial License Usage
** Licensees holding valid commercial Qt 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 Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
Tobias Hunger's avatar
Tobias Hunger committed
15
16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
18
19
20
21
22
23
24
25
** Alternatively, 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
Tobias Hunger's avatar
Tobias Hunger committed
26
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
Tobias Hunger's avatar
Tobias Hunger committed
29

30
31
32
33
#include "librarydetailscontroller.h"
#include "ui_librarydetailswidget.h"
#include "findqt4profiles.h"
#include "qt4nodes.h"
34
#include "qt4buildconfiguration.h"
35
36
37

#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/session.h>
38
#include <projectexplorer/project.h>
Tobias Hunger's avatar
Tobias Hunger committed
39
#include <projectexplorer/kitinformation.h>
40
#include <projectexplorer/target.h>
41
#include <projectexplorer/toolchain.h>
42
#include <utils/hostosinfo.h>
43

44
45
46
#include <QFileInfo>
#include <QDir>
#include <QTextStream>
47
48
49
50
51

using namespace Qt4ProjectManager;
using namespace Qt4ProjectManager::Internal;

LibraryDetailsController::LibraryDetailsController(
52
53
        Ui::LibraryDetailsWidget *libraryDetails,
        const QString &proFile, QObject *parent) :
54
55
56
    QObject(parent),
    m_platforms(AddLibraryWizard::LinuxPlatform
                | AddLibraryWizard::MacPlatform
57
                | AddLibraryWizard::WindowsPlatform),
58
59
    m_linkageType(AddLibraryWizard::NoLinkage),
    m_macLibraryType(AddLibraryWizard::NoLibraryType),
60
    m_proFile(proFile),
61
62
63
64
65
66
67
68
    m_ignoreGuiSignals(false),
    m_includePathChanged(false),
    m_linkageRadiosVisible(true),
    m_macLibraryRadiosVisible(true),
    m_includePathVisible(true),
    m_windowsGroupVisible(true),
    m_libraryDetailsWidget(libraryDetails)
{
69
70
71
72
73
    switch (Utils::HostOsInfo::hostOs()) {
    case Utils::HostOsInfo::HostOsMac:
        m_creatorPlatform = CreatorMac;
        break;
    case Utils::HostOsInfo::HostOsLinux:
74
        m_creatorPlatform = CreatorLinux;
75
76
77
78
79
80
81
82
83
84
85
86
87
        break;
    case Utils::HostOsInfo::HostOsWindows:
        m_creatorPlatform = CreatorWindows;
        break;
    default:
        break;
    }

    if (!Utils::HostOsInfo::isLinuxHost()) {
        // project for which we are going to insert the snippet
        const ProjectExplorer::Project *project =
                ProjectExplorer::ProjectExplorerPlugin::instance()->session()->projectForFile(proFile);
        // if its tool chain is maemo behave the same as we would be on linux
88
        ProjectExplorer::ToolChain *tc = ProjectExplorer::ToolChainKitInformation::toolChain(project->activeTarget()->kit());
89
90
91
92
93
        if (tc
                && (tc->targetAbi().osFlavor() == ProjectExplorer::Abi::HarmattanLinuxFlavor
                    || tc->targetAbi().osFlavor() == ProjectExplorer::Abi::MaemoLinuxFlavor))
            m_creatorPlatform = CreatorLinux;
    }
94

95
96
97
98
99
    setPlatformsVisible(true);
    setLinkageGroupVisible(true);
    setMacLibraryGroupVisible(true);
    setPackageLineEditVisible(false);

100
101
102
103
104
105
    if (creatorPlatform() == CreatorMac)
        setMacLibraryRadiosVisible(false);

    if (creatorPlatform() != CreatorWindows)
        setLinkageRadiosVisible(false);

106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
    connect(m_libraryDetailsWidget->includePathChooser, SIGNAL(changed(QString)),
            this, SLOT(slotIncludePathChanged()));
    connect(m_libraryDetailsWidget->frameworkRadio, SIGNAL(clicked(bool)),
            this, SLOT(slotMacLibraryTypeChanged()));
    connect(m_libraryDetailsWidget->libraryRadio, SIGNAL(clicked(bool)),
            this, SLOT(slotMacLibraryTypeChanged()));
    connect(m_libraryDetailsWidget->useSubfoldersCheckBox, SIGNAL(toggled(bool)),
            this, SLOT(slotUseSubfoldersChanged(bool)));
    connect(m_libraryDetailsWidget->addSuffixCheckBox, SIGNAL(toggled(bool)),
            this, SLOT(slotAddSuffixChanged(bool)));
    connect(m_libraryDetailsWidget->linCheckBox, SIGNAL(clicked(bool)),
            this, SLOT(slotPlatformChanged()));
    connect(m_libraryDetailsWidget->macCheckBox, SIGNAL(clicked(bool)),
            this, SLOT(slotPlatformChanged()));
    connect(m_libraryDetailsWidget->winCheckBox, SIGNAL(clicked(bool)),
            this, SLOT(slotPlatformChanged()));
}

124
LibraryDetailsController::CreatorPlatform LibraryDetailsController::creatorPlatform() const
125
{
126
    return m_creatorPlatform;
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
}

Ui::LibraryDetailsWidget *LibraryDetailsController::libraryDetailsWidget() const
{
    return m_libraryDetailsWidget;
}

AddLibraryWizard::Platforms LibraryDetailsController::platforms() const
{
    return m_platforms;
}

AddLibraryWizard::LinkageType LibraryDetailsController::linkageType() const
{
    return m_linkageType;
}

AddLibraryWizard::MacLibraryType LibraryDetailsController::macLibraryType() const
{
    return m_macLibraryType;
}

void LibraryDetailsController::updateGui()
{
    // read values from gui
    m_platforms = 0;
    if (libraryDetailsWidget()->linCheckBox->isChecked())
        m_platforms |= AddLibraryWizard::LinuxPlatform;
    if (libraryDetailsWidget()->macCheckBox->isChecked())
        m_platforms |= AddLibraryWizard::MacPlatform;
    if (libraryDetailsWidget()->winCheckBox->isChecked())
        m_platforms |= AddLibraryWizard::WindowsPlatform;

    bool macLibraryTypeUpdated = false;
    if (!m_linkageRadiosVisible) {
        m_linkageType = suggestedLinkageType();
        if (m_linkageType == AddLibraryWizard::StaticLinkage) {
            m_macLibraryType = AddLibraryWizard::LibraryType;
            macLibraryTypeUpdated = true;
        }
    } else {
        m_linkageType = AddLibraryWizard::DynamicLinkage; // the default
        if (libraryDetailsWidget()->staticRadio->isChecked())
            m_linkageType = AddLibraryWizard::StaticLinkage;
    }

    if (!macLibraryTypeUpdated) {
        if (!m_macLibraryRadiosVisible) {
            m_macLibraryType = suggestedMacLibraryType();
        } else {
            m_macLibraryType = AddLibraryWizard::LibraryType; // the default
            if (libraryDetailsWidget()->frameworkRadio->isChecked())
                m_macLibraryType = AddLibraryWizard::FrameworkType;
        }
    }

    // enable or disable some parts of gui
    libraryDetailsWidget()->macGroupBox->setEnabled(platforms()
                                & AddLibraryWizard::MacPlatform);
    updateWindowsOptionsEnablement();
    const bool macRadiosEnabled = m_linkageRadiosVisible ||
            linkageType() != AddLibraryWizard::StaticLinkage;
    libraryDetailsWidget()->libraryRadio->setEnabled(macRadiosEnabled);
    libraryDetailsWidget()->frameworkRadio->setEnabled(macRadiosEnabled);

    // update values in gui
    setIgnoreGuiSignals(true);

    showLinkageType(linkageType());
    showMacLibraryType(macLibraryType());
    if (!m_includePathChanged)
        libraryDetailsWidget()->includePathChooser->setPath(suggestedIncludePath());

    setIgnoreGuiSignals(false);
}

QString LibraryDetailsController::proFile() const
{
    return m_proFile;
}

bool LibraryDetailsController::isIncludePathChanged() const
{
    return m_includePathChanged;
}

void LibraryDetailsController::setIgnoreGuiSignals(bool ignore)
{
    m_ignoreGuiSignals = ignore;
}

bool LibraryDetailsController::guiSignalsIgnored() const
{
    return m_ignoreGuiSignals;
}

void LibraryDetailsController::showLinkageType(
        AddLibraryWizard::LinkageType linkageType)
{
    const QString linkage(tr("Linkage:"));
    QString linkageTitle;
    switch (linkageType) {
    case AddLibraryWizard::DynamicLinkage:
        libraryDetailsWidget()->dynamicRadio->setChecked(true);
        linkageTitle = tr("%1 Dynamic").arg(linkage);
        break;
    case AddLibraryWizard::StaticLinkage:
        libraryDetailsWidget()->staticRadio->setChecked(true);
        linkageTitle = tr("%1 Static").arg(linkage);
        break;
    default:
        libraryDetailsWidget()->dynamicRadio->setChecked(false);
        libraryDetailsWidget()->staticRadio->setChecked(false);
        linkageTitle = linkage;
        break;
        }
    libraryDetailsWidget()->linkageGroupBox->setTitle(linkageTitle);
}

void LibraryDetailsController::showMacLibraryType(
        AddLibraryWizard::MacLibraryType libType)
{
    const QString libraryType(tr("Mac:"));
    QString libraryTypeTitle;
    switch (libType) {
    case AddLibraryWizard::FrameworkType:
        libraryDetailsWidget()->frameworkRadio->setChecked(true);
        libraryTypeTitle = tr("%1 Framework").arg(libraryType);
        break;
    case AddLibraryWizard::LibraryType:
        libraryDetailsWidget()->libraryRadio->setChecked(true);
        libraryTypeTitle = tr("%1 Library").arg(libraryType);
        break;
    default:
        libraryDetailsWidget()->frameworkRadio->setChecked(false);
        libraryDetailsWidget()->libraryRadio->setChecked(false);
        libraryTypeTitle = libraryType;
        break;
    }
    libraryDetailsWidget()->macGroupBox->setTitle(libraryTypeTitle);
}

269
270
271
272
273
void LibraryDetailsController::setPlatformsVisible(bool ena)
{
    libraryDetailsWidget()->platformGroupBox->setVisible(ena);
}

274
275
276
277
278
279
280
void LibraryDetailsController::setLinkageRadiosVisible(bool ena)
{
    m_linkageRadiosVisible = ena;
    libraryDetailsWidget()->staticRadio->setVisible(ena);
    libraryDetailsWidget()->dynamicRadio->setVisible(ena);
}

281
282
283
284
285
286
void LibraryDetailsController::setLinkageGroupVisible(bool ena)
{
    setLinkageRadiosVisible(ena);
    libraryDetailsWidget()->linkageGroupBox->setVisible(ena);
}

287
288
289
290
291
292
293
void LibraryDetailsController::setMacLibraryRadiosVisible(bool ena)
{
    m_macLibraryRadiosVisible = ena;
    libraryDetailsWidget()->frameworkRadio->setVisible(ena);
    libraryDetailsWidget()->libraryRadio->setVisible(ena);
}

294
295
296
297
298
299
void LibraryDetailsController::setMacLibraryGroupVisible(bool ena)
{
    setMacLibraryRadiosVisible(ena);
    libraryDetailsWidget()->macGroupBox->setVisible(ena);
}

300
301
302
303
304
305
306
307
308
309
310
311
void LibraryDetailsController::setLibraryPathChooserVisible(bool ena)
{
    libraryDetailsWidget()->libraryPathChooser->setVisible(ena);
    libraryDetailsWidget()->libraryFileLabel->setVisible(ena);
}

void LibraryDetailsController::setLibraryComboBoxVisible(bool ena)
{
    libraryDetailsWidget()->libraryComboBox->setVisible(ena);
    libraryDetailsWidget()->libraryLabel->setVisible(ena);
}

312
313
314
315
316
317
void LibraryDetailsController::setPackageLineEditVisible(bool ena)
{
    libraryDetailsWidget()->packageLineEdit->setVisible(ena);
    libraryDetailsWidget()->packageLabel->setVisible(ena);
}

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
void LibraryDetailsController::setIncludePathVisible(bool ena)
{
    m_includePathVisible = ena;
    libraryDetailsWidget()->includeLabel->setVisible(ena);
    libraryDetailsWidget()->includePathChooser->setVisible(ena);
}

void LibraryDetailsController::setWindowsGroupVisible(bool ena)
{
    m_windowsGroupVisible = ena;
    libraryDetailsWidget()->winGroupBox->setVisible(ena);
}

void LibraryDetailsController::setRemoveSuffixVisible(bool ena)
{
    libraryDetailsWidget()->removeSuffixCheckBox->setVisible(ena);
}

bool LibraryDetailsController::isMacLibraryRadiosVisible() const
{
    return m_macLibraryRadiosVisible;
}

bool LibraryDetailsController::isIncludePathVisible() const
{
    return m_includePathVisible;
}

bool LibraryDetailsController::isWindowsGroupVisible() const
{
    return m_windowsGroupVisible;
}

void LibraryDetailsController::slotIncludePathChanged()
{
    if (m_ignoreGuiSignals)
        return;
    m_includePathChanged = true;
}

void LibraryDetailsController::slotPlatformChanged()
{
    updateGui();
    emit completeChanged();
}

void LibraryDetailsController::slotMacLibraryTypeChanged()
{
    if (guiSignalsIgnored())
        return;

    if (m_linkageRadiosVisible
            && libraryDetailsWidget()->frameworkRadio->isChecked()) {
        setIgnoreGuiSignals(true);
        libraryDetailsWidget()->dynamicRadio->setChecked(true);
        setIgnoreGuiSignals(true);
    }

    updateGui();
}

void LibraryDetailsController::slotUseSubfoldersChanged(bool ena)
{
    if (ena) {
        libraryDetailsWidget()->addSuffixCheckBox->setChecked(false);
        libraryDetailsWidget()->removeSuffixCheckBox->setChecked(false);
    }
}

void LibraryDetailsController::slotAddSuffixChanged(bool ena)
{
    if (ena) {
        libraryDetailsWidget()->useSubfoldersCheckBox->setChecked(false);
        libraryDetailsWidget()->removeSuffixCheckBox->setChecked(false);
    }
}

static QString appendSpaceIfNotEmpty(const QString &aString)
{
    if (aString.isEmpty())
        return aString;
    return aString + QLatin1Char(' ');
}

static QString appendSeparator(const QString &aString)
{
    if (aString.isEmpty())
        return aString;
    if (aString.at(aString.size() - 1) == QLatin1Char('/'))
        return aString;
    return aString + QLatin1Char('/');
}

static QString commonScopes(AddLibraryWizard::Platforms scopes,
                            AddLibraryWizard::Platforms excludedScopes)
{
    QString scopesString;
    QTextStream str(&scopesString);
    AddLibraryWizard::Platforms common = scopes | excludedScopes;
    bool unixLikeScopes = false;
    if (scopes & ~QFlags<AddLibraryWizard::Platform>(AddLibraryWizard::WindowsPlatform)) {
        unixLikeScopes = true;
        if (common & AddLibraryWizard::LinuxPlatform) {
            str << "unix";
            if (!(common & AddLibraryWizard::MacPlatform))
                str << ":!macx";
        } else {
            if (scopes & AddLibraryWizard::MacPlatform)
                str << "macx";
        }
    }
    if (scopes & AddLibraryWizard::WindowsPlatform) {
        if (unixLikeScopes)
            str << "|";
        str << "win32";
    }
    return scopesString;
}

static QString generateLibsSnippet(AddLibraryWizard::Platforms platforms,
                     AddLibraryWizard::MacLibraryType macLibraryType,
                     const QString &libName,
                     const QString &targetRelativePath, const QString &pwd,
                     bool useSubfolders, bool addSuffix, bool generateLibPath)
{
    // if needed it contains: $$[pwd]/_PATH_
    const QString libraryPathSnippet = generateLibPath ?
                QLatin1String("$$") + pwd + QLatin1Char('/') +
                targetRelativePath : QString();

    // if needed it contains: -L$$[pwd]/_PATH_
    const QString simpleLibraryPathSnippet = generateLibPath ?
                QLatin1String("-L") + libraryPathSnippet : QString();
    // if needed it contains: -F$$[pwd]/_PATH_
    const QString macLibraryPathSnippet = generateLibPath ?
                QLatin1String("-F") + libraryPathSnippet : QString();

    AddLibraryWizard::Platforms commonPlatforms = platforms;
    if (macLibraryType == AddLibraryWizard::FrameworkType) // we will generate a separate -F -framework line
        commonPlatforms &= ~QFlags<AddLibraryWizard::Platform>(AddLibraryWizard::MacPlatform);
    if (useSubfolders || addSuffix) // we will generate a separate debug/release conditions
        commonPlatforms &= ~QFlags<AddLibraryWizard::Platform>(AddLibraryWizard::WindowsPlatform);

    AddLibraryWizard::Platforms diffPlatforms = platforms ^ commonPlatforms;
    AddLibraryWizard::Platforms generatedPlatforms = 0;

    QString snippetMessage;
    QTextStream str(&snippetMessage);

    if (diffPlatforms & AddLibraryWizard::WindowsPlatform) {
        str << "win32:CONFIG(release, debug|release): LIBS += ";
        if (useSubfolders)
            str << simpleLibraryPathSnippet << "release/ " << "-l" << libName << "\n";
        else if (addSuffix)
            str << appendSpaceIfNotEmpty(simpleLibraryPathSnippet) << "-l" << libName << "\n";

        str << "else:win32:CONFIG(debug, debug|release): LIBS += ";
        if (useSubfolders)
            str << simpleLibraryPathSnippet << "debug/ " << "-l" << libName << "\n";
        else if (addSuffix)
            str << appendSpaceIfNotEmpty(simpleLibraryPathSnippet) << "-l" << libName << "d\n";
        generatedPlatforms |= AddLibraryWizard::WindowsPlatform;
    }
    if (diffPlatforms & AddLibraryWizard::MacPlatform) {
        if (generatedPlatforms)
            str << "else:";
        str << "mac: LIBS += " << appendSpaceIfNotEmpty(macLibraryPathSnippet)
                    << "-framework " << libName << "\n";
        generatedPlatforms |= AddLibraryWizard::MacPlatform;
    }

    if (commonPlatforms) {
        if (generatedPlatforms)
            str << "else:";
        str << commonScopes(commonPlatforms, generatedPlatforms) << ": LIBS += "
                << appendSpaceIfNotEmpty(simpleLibraryPathSnippet) << "-l" << libName << "\n";
    }
    return snippetMessage;
}

static QString generateIncludePathSnippet(const QString &includeRelativePath)
{
    return QLatin1String("\nINCLUDEPATH += $$PWD/")
            + includeRelativePath + QLatin1Char('\n')
            + QLatin1String("DEPENDPATH += $$PWD/")
            + includeRelativePath + QLatin1Char('\n');
}

static QString generatePreTargetDepsSnippet(AddLibraryWizard::Platforms platforms,
                                            AddLibraryWizard::LinkageType linkageType,
                                            const QString &libName,
                                            const QString &targetRelativePath, const QString &pwd,
                                            bool useSubfolders, bool addSuffix)
{
    if (linkageType != AddLibraryWizard::StaticLinkage)
        return QString();

    // if needed it contains: PRE_TARGETDEPS += $$[pwd]/_PATH_TO_LIB_WITHOUT_LIB_NAME_
    const QString preTargetDepsSnippet = QLatin1String("PRE_TARGETDEPS += $$") +
            pwd + QLatin1Char('/') + targetRelativePath;

    QString snippetMessage;
    QTextStream str(&snippetMessage);
    str << "\n";
    AddLibraryWizard::Platforms generatedPlatforms = 0;
    if (platforms & AddLibraryWizard::WindowsPlatform) {
        if (useSubfolders || addSuffix) {
            str << "win32:CONFIG(release, debug|release): "
                << preTargetDepsSnippet;
            if (useSubfolders)
                str << "release/" << libName << ".lib\n";
            else if (addSuffix)
                str << libName << ".lib\n";

            str << "else:win32:CONFIG(debug, debug|release): "
                << preTargetDepsSnippet;
            if (useSubfolders)
                str << "debug/" << libName << ".lib\n";
            else if (addSuffix)
                str << libName << "d.lib\n";
        } else {
            str << "win32: " << preTargetDepsSnippet << libName << ".lib\n";
        }
        generatedPlatforms |= AddLibraryWizard::WindowsPlatform;
    }
    AddLibraryWizard::Platforms commonPlatforms = platforms;
    commonPlatforms &= ~QFlags<AddLibraryWizard::Platform>(AddLibraryWizard::WindowsPlatform);
    if (commonPlatforms) {
        if (generatedPlatforms)
            str << "else:";
        str << commonScopes(commonPlatforms, generatedPlatforms) << ": "
            << preTargetDepsSnippet << "lib" << libName << ".a\n";
    }
    return snippetMessage;
}

NonInternalLibraryDetailsController::NonInternalLibraryDetailsController(
555
556
557
        Ui::LibraryDetailsWidget *libraryDetails,
        const QString &proFile, QObject *parent) :
    LibraryDetailsController(libraryDetails, proFile, parent)
558
559
560
561
{
    setLibraryComboBoxVisible(false);
    setLibraryPathChooserVisible(true);

562
563
564
565
566
567
568
569
570
    if (creatorPlatform() == CreatorWindows) {
        libraryDetailsWidget()->libraryPathChooser->setPromptDialogFilter(
                QLatin1String("Library file (*.lib)"));
        setLinkageRadiosVisible(true);
        setRemoveSuffixVisible(true);
    } else {
        setLinkageRadiosVisible(false);
        setRemoveSuffixVisible(false);
    }
571

572
573
574
    if (creatorPlatform() == CreatorLinux)
        libraryDetailsWidget()->libraryPathChooser->setPromptDialogFilter(
                QLatin1String("Library file (lib*.so lib*.a)"));
575

576
577
578
    if (creatorPlatform() == CreatorMac) {
        libraryDetailsWidget()->libraryPathChooser->setPromptDialogFilter(
                QLatin1String("Library file (*.dylib *.a *.framework)"));
579
           // QLatin1String("Library file (lib*.dylib lib*.a *.framework)"));
580
581
582
583
        libraryDetailsWidget()->libraryPathChooser->setExpectedKind(Utils::PathChooser::Any);
    } else {
        libraryDetailsWidget()->libraryPathChooser->setExpectedKind(Utils::PathChooser::File);
    }
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599

    connect(libraryDetailsWidget()->libraryPathChooser, SIGNAL(validChanged()),
            this, SIGNAL(completeChanged()));
    connect(libraryDetailsWidget()->libraryPathChooser, SIGNAL(changed(QString)),
            this, SLOT(slotLibraryPathChanged()));
    connect(libraryDetailsWidget()->removeSuffixCheckBox, SIGNAL(toggled(bool)),
            this, SLOT(slotRemoveSuffixChanged(bool)));
    connect(libraryDetailsWidget()->dynamicRadio, SIGNAL(clicked(bool)),
            this, SLOT(slotLinkageTypeChanged()));
    connect(libraryDetailsWidget()->staticRadio, SIGNAL(clicked(bool)),
            this, SLOT(slotLinkageTypeChanged()));
}

AddLibraryWizard::LinkageType NonInternalLibraryDetailsController::suggestedLinkageType() const
{
    AddLibraryWizard::LinkageType type = AddLibraryWizard::NoLinkage;
600
601
602
603
604
605
606
607
    if (creatorPlatform() != CreatorWindows) {
        if (libraryDetailsWidget()->libraryPathChooser->isValid()) {
            QFileInfo fi(libraryDetailsWidget()->libraryPathChooser->path());
            if (fi.suffix() == QLatin1String("a"))
                type = AddLibraryWizard::StaticLinkage;
            else
                type = AddLibraryWizard::DynamicLinkage;
        }
608
609
610
611
612
613
614
    }
    return type;
}

AddLibraryWizard::MacLibraryType NonInternalLibraryDetailsController::suggestedMacLibraryType() const
{
    AddLibraryWizard::MacLibraryType type = AddLibraryWizard::NoLibraryType;
615
616
617
618
619
620
621
622
    if (creatorPlatform() == CreatorMac) {
        if (libraryDetailsWidget()->libraryPathChooser->isValid()) {
            QFileInfo fi(libraryDetailsWidget()->libraryPathChooser->path());
            if (fi.suffix() == QLatin1String("framework"))
                type = AddLibraryWizard::FrameworkType;
            else
                type = AddLibraryWizard::LibraryType;
        }
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
    }
    return type;
}

QString NonInternalLibraryDetailsController::suggestedIncludePath() const
{
    QString includePath;
    if (libraryDetailsWidget()->libraryPathChooser->isValid()) {
        QFileInfo fi(libraryDetailsWidget()->libraryPathChooser->path());
        includePath = fi.absolutePath();
        QFileInfo dfi(includePath);
        // TODO: Win: remove debug or release folder first if appropriate
        if (dfi.fileName() == QLatin1String("lib")) {
            QDir dir = dfi.absoluteDir();
            includePath = dir.absolutePath();
            QDir includeDir(dir.absoluteFilePath(QLatin1String("include")));
            if (includeDir.exists())
                includePath = includeDir.absolutePath();
        }
    }
    return includePath;
}

void NonInternalLibraryDetailsController::updateWindowsOptionsEnablement()
{
    bool ena = platforms() & AddLibraryWizard::WindowsPlatform;
649
650
651
652
    if (creatorPlatform() == CreatorWindows) {
        libraryDetailsWidget()->addSuffixCheckBox->setEnabled(ena);
        ena = true;
    }
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
    libraryDetailsWidget()->winGroupBox->setEnabled(ena);
}

void NonInternalLibraryDetailsController::slotLinkageTypeChanged()
{
    if (guiSignalsIgnored())
        return;

    if (isMacLibraryRadiosVisible()
            && libraryDetailsWidget()->staticRadio->isChecked()) {
        setIgnoreGuiSignals(true);
        libraryDetailsWidget()->libraryRadio->setChecked(true);
        setIgnoreGuiSignals(true);
    }

    updateGui();
}

void NonInternalLibraryDetailsController::slotRemoveSuffixChanged(bool ena)
{
    if (ena) {
        libraryDetailsWidget()->useSubfoldersCheckBox->setChecked(false);
        libraryDetailsWidget()->addSuffixCheckBox->setChecked(false);
    }
}

void NonInternalLibraryDetailsController::slotLibraryPathChanged()
{
681
682
683
684
685
686
687
688
    if (creatorPlatform() == CreatorWindows) {
        bool subfoldersEnabled = true;
        bool removeSuffixEnabled = true;
        if (libraryDetailsWidget()->libraryPathChooser->isValid()) {
            QFileInfo fi(libraryDetailsWidget()->libraryPathChooser->path());
            QFileInfo dfi(fi.absolutePath());
            const QString parentFolderName = dfi.fileName().toLower();
            if (parentFolderName != QLatin1String("debug") &&
689
                parentFolderName != QLatin1String("release"))
690
691
692
693
694
695
696
697
698
699
700
701
702
                subfoldersEnabled = false;
            const QString baseName = fi.baseName();

            if (baseName.isEmpty() || baseName.at(baseName.size() - 1).toLower() != QLatin1Char('d'))
                removeSuffixEnabled = false;

            if (subfoldersEnabled)
                libraryDetailsWidget()->useSubfoldersCheckBox->setChecked(true);
            else if (removeSuffixEnabled)
                libraryDetailsWidget()->removeSuffixCheckBox->setChecked(true);
            else
                libraryDetailsWidget()->addSuffixCheckBox->setChecked(true);
        }
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
    }

    updateGui();

    emit completeChanged();
}

bool NonInternalLibraryDetailsController::isComplete() const
{
    return libraryDetailsWidget()->libraryPathChooser->isValid() &&
           platforms();
}

QString NonInternalLibraryDetailsController::snippet() const
{
    QString libPath = libraryDetailsWidget()->libraryPathChooser->path();
    QFileInfo fi(libPath);
    QString libName;
    const bool removeSuffix = isWindowsGroupVisible()
            && libraryDetailsWidget()->removeSuffixCheckBox->isChecked();
723
    if (creatorPlatform() == CreatorWindows) {
724
        libName = fi.baseName();
725
726
727
728
729
730
731
732
        if (removeSuffix && !libName.isEmpty()) // remove last letter which needs to be "d"
            libName = libName.left(libName.size() - 1);
    } else if (creatorPlatform() == CreatorMac) {
        if (macLibraryType() == AddLibraryWizard::FrameworkType)
            libName = fi.baseName();
        else
            libName = fi.baseName().mid(3); // cut the "lib" prefix
    } else {
733
        libName = fi.baseName().mid(3); // cut the "lib" prefix
734
735
    }

736
737
738
739
740
    QString targetRelativePath;
    QString includeRelativePath;
    bool useSubfolders = false;
    bool addSuffix = false;
    if (isWindowsGroupVisible()) {
741
742
743
744
        // when we are on Win but we don't generate the code for Win
        // we still need to remove "debug" or "release" subfolder
        const bool useSubfoldersCondition = (creatorPlatform() == CreatorWindows)
                                            ? true : platforms() & AddLibraryWizard::WindowsPlatform;
745
746
747
748
749
750
751
752
753
        if (useSubfoldersCondition)
            useSubfolders = libraryDetailsWidget()->useSubfoldersCheckBox->isChecked();
        if (platforms() & AddLibraryWizard::WindowsPlatform)
            addSuffix = libraryDetailsWidget()->addSuffixCheckBox->isChecked() || removeSuffix;
    }
    if (isIncludePathVisible()) { // generate also the path to lib
        QFileInfo pfi(proFile());
        QDir pdir = pfi.absoluteDir();
        QString absoluteLibraryPath = fi.absolutePath();
754
        if (creatorPlatform() == CreatorWindows && useSubfolders) { // drop last subfolder which needs to be "debug" or "release"
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
            QFileInfo libfi(absoluteLibraryPath);
            absoluteLibraryPath = libfi.absolutePath();
        }
        targetRelativePath = appendSeparator(pdir.relativeFilePath(absoluteLibraryPath));

        const QString includePath = libraryDetailsWidget()->includePathChooser->path();
        if (!includePath.isEmpty())
            includeRelativePath = pdir.relativeFilePath(includePath);
    }

    QString snippetMessage;
    QTextStream str(&snippetMessage);
    str << "\n";
    str << generateLibsSnippet(platforms(), macLibraryType(), libName,
                               targetRelativePath, QLatin1String("PWD"),
                               useSubfolders, addSuffix, isIncludePathVisible());
    if (isIncludePathVisible()) {
        str << generateIncludePathSnippet(includeRelativePath);
        str << generatePreTargetDepsSnippet(platforms(), linkageType(), libName,
                               targetRelativePath, QLatin1String("PWD"),
                               useSubfolders, addSuffix);
    }
    return snippetMessage;
}

/////////////

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
PackageLibraryDetailsController::PackageLibraryDetailsController(
    Ui::LibraryDetailsWidget *libraryDetails,
    const QString &proFile, QObject *parent)
    : NonInternalLibraryDetailsController(libraryDetails, proFile, parent)
{
    setPlatformsVisible(false);
    setIncludePathVisible(false);
    setWindowsGroupVisible(false);
    setLinkageGroupVisible(false);
    setMacLibraryGroupVisible(false);
    setLibraryPathChooserVisible(false);
    setPackageLineEditVisible(true);

    connect(libraryDetailsWidget()->packageLineEdit, SIGNAL(textChanged(QString)),
            this, SIGNAL(completeChanged()));

    updateGui();
}

bool PackageLibraryDetailsController::isComplete() const
{
    return !libraryDetailsWidget()->packageLineEdit->text().isEmpty();
}

QString PackageLibraryDetailsController::snippet() const
{
    QString snippetMessage;
    QTextStream str(&snippetMessage);
    str << "\n";
    if (!isLinkPackageGenerated())
        str << "unix: CONFIG += link_pkgconfig\n";
    str << "unix: PKGCONFIG += " << libraryDetailsWidget()->packageLineEdit->text() << "\n";
    return snippetMessage;
}

bool PackageLibraryDetailsController::isLinkPackageGenerated() const
{
    const ProjectExplorer::Project *project =
            ProjectExplorer::ProjectExplorerPlugin::instance()->session()->projectForFile(proFile());
    if (!project)
        return false;

    const Qt4ProFileNode *rootProject = qobject_cast<const Qt4ProFileNode *>(project->rootProjectNode());
    if (!rootProject)
        return false;

    const Qt4ProFileNode *currentProject = rootProject->findProFileFor(proFile());
    if (!currentProject)
        return false;

    const QStringList configVar = currentProject->variableValue(ConfigVar);
    if (configVar.contains(QLatin1String("link_pkgconfig")))
        return true;
835

836
837
838
839
840
    return false;
}

/////////////

841
SystemLibraryDetailsController::SystemLibraryDetailsController(
842
843
844
    Ui::LibraryDetailsWidget *libraryDetails,
    const QString &proFile, QObject *parent)
    : NonInternalLibraryDetailsController(libraryDetails, proFile, parent)
845
846
847
{
    setIncludePathVisible(false);
    setWindowsGroupVisible(false);
848
849

    updateGui();
850
851
852
853
854
}

/////////////

ExternalLibraryDetailsController::ExternalLibraryDetailsController(
855
856
857
    Ui::LibraryDetailsWidget *libraryDetails,
    const QString &proFile, QObject *parent)
    : NonInternalLibraryDetailsController(libraryDetails, proFile, parent)
858
859
860
{
    setIncludePathVisible(true);
    setWindowsGroupVisible(true);
861
862

    updateGui();
863
864
865
866
867
}

void ExternalLibraryDetailsController::updateWindowsOptionsEnablement()
{
    NonInternalLibraryDetailsController::updateWindowsOptionsEnablement();
868
869
870
871

    if (creatorPlatform() != CreatorWindows)
        return;

872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
    bool subfoldersEnabled = true;
    bool removeSuffixEnabled = true;
    if (libraryDetailsWidget()->libraryPathChooser->isValid()) {
        QFileInfo fi(libraryDetailsWidget()->libraryPathChooser->path());
        QFileInfo dfi(fi.absolutePath());
        const QString parentFolderName = dfi.fileName().toLower();
        if (parentFolderName != QLatin1String("debug") &&
                parentFolderName != QLatin1String("release"))
            subfoldersEnabled = false;
        const QString baseName = fi.baseName();

        if (baseName.isEmpty() || baseName.at(baseName.size() - 1).toLower() != QLatin1Char('d'))
            removeSuffixEnabled = false;

    }
    libraryDetailsWidget()->useSubfoldersCheckBox->setEnabled(subfoldersEnabled);
    libraryDetailsWidget()->removeSuffixCheckBox->setEnabled(removeSuffixEnabled);
}

/////////////

InternalLibraryDetailsController::InternalLibraryDetailsController(
894
895
896
        Ui::LibraryDetailsWidget *libraryDetails,
        const QString &proFile, QObject *parent)
    : LibraryDetailsController(libraryDetails, proFile, parent)
897
898
899
900
901
902
903
904
{
    setLinkageRadiosVisible(false);
    setLibraryPathChooserVisible(false);
    setLibraryComboBoxVisible(true);
    setIncludePathVisible(true);
    setWindowsGroupVisible(true);
    setRemoveSuffixVisible(false);

905
906
    if (creatorPlatform() == CreatorWindows)
        libraryDetailsWidget()->useSubfoldersCheckBox->setEnabled(true);
907
908
909

    connect(libraryDetailsWidget()->libraryComboBox, SIGNAL(currentIndexChanged(int)),
            this, SLOT(slotCurrentLibraryChanged()));
910
911
912

    updateProFile();
    updateGui();
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
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
}

AddLibraryWizard::LinkageType InternalLibraryDetailsController::suggestedLinkageType() const
{
    const int currentIndex = libraryDetailsWidget()->libraryComboBox->currentIndex();
    AddLibraryWizard::LinkageType type = AddLibraryWizard::NoLinkage;
    if (currentIndex >= 0) {
        Qt4ProFileNode *proFileNode = m_proFileNodes.at(currentIndex);
        const QStringList configVar = proFileNode->variableValue(ConfigVar);
        if (configVar.contains(QLatin1String("staticlib"))
                || configVar.contains(QLatin1String("static")))
            type = AddLibraryWizard::StaticLinkage;
        else
            type = AddLibraryWizard::DynamicLinkage;
    }
    return type;
}

AddLibraryWizard::MacLibraryType InternalLibraryDetailsController::suggestedMacLibraryType() const
{
    const int currentIndex = libraryDetailsWidget()->libraryComboBox->currentIndex();
    AddLibraryWizard::MacLibraryType type = AddLibraryWizard::NoLibraryType;
    if (currentIndex >= 0) {
        Qt4ProFileNode *proFileNode = m_proFileNodes.at(currentIndex);
        const QStringList configVar = proFileNode->variableValue(ConfigVar);
        if (configVar.contains(QLatin1String("lib_bundle")))
            type = AddLibraryWizard::FrameworkType;
        else
            type = AddLibraryWizard::LibraryType;
    }
    return type;
}

QString InternalLibraryDetailsController::suggestedIncludePath() const
{
    const int currentIndex = libraryDetailsWidget()->libraryComboBox->currentIndex();
    QString includePath;
    if (currentIndex >= 0) {
        Qt4ProFileNode *proFileNode = m_proFileNodes.at(currentIndex);
        QFileInfo fi(proFileNode->path());
        includePath = fi.absolutePath();
    }
    return includePath;
}

void InternalLibraryDetailsController::updateWindowsOptionsEnablement()
{
960
961
    if (creatorPlatform() == CreatorWindows)
        libraryDetailsWidget()->addSuffixCheckBox->setEnabled(true);
962
963
964
965
    libraryDetailsWidget()->winGroupBox->setEnabled(platforms()
                                & AddLibraryWizard::WindowsPlatform);
}

966
void InternalLibraryDetailsController::updateProFile()
967
{
968
    m_rootProjectPath.clear();
969
970
971
972
973
974
975
976
977
978
979
980
    m_proFileNodes.clear();
    libraryDetailsWidget()->libraryComboBox->clear();

    const ProjectExplorer::Project *project =
            ProjectExplorer::ProjectExplorerPlugin::instance()->session()->projectForFile(proFile());
    if (!project)
        return;

    setIgnoreGuiSignals(true);

    ProjectExplorer::ProjectNode *rootProject = project->rootProjectNode();
    QFileInfo fi(rootProject->path());
981
982
    m_rootProjectPath = fi.absolutePath();
    QDir rootDir(m_rootProjectPath);
983
984
985
986
    FindQt4ProFiles findQt4ProFiles;
    QList<Qt4ProFileNode *> proFiles = findQt4ProFiles(rootProject);
    foreach (Qt4ProFileNode *proFileNode, proFiles) {
        const QString proFilePath = proFileNode->path();
987
        if (proFileNode->projectType() == LibraryTemplate) {
988
989
990
991
            const QStringList configVar = proFileNode->variableValue(ConfigVar);
            if (!configVar.contains(QLatin1String("plugin"))) {
                const QString relProFilePath = rootDir.relativeFilePath(proFilePath);
                TargetInformation targetInfo = proFileNode->targetInformation();
992
                const QString itemToolTip = QString::fromLatin1("%1 (%2)").arg(targetInfo.target).arg(relProFilePath);
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
                m_proFileNodes.append(proFileNode);
                libraryDetailsWidget()->libraryComboBox->addItem(targetInfo.target);
                libraryDetailsWidget()->libraryComboBox->setItemData(
                            libraryDetailsWidget()->libraryComboBox->count() - 1,
                            itemToolTip, Qt::ToolTipRole);
            }
        }
    }

    setIgnoreGuiSignals(false);
}

void InternalLibraryDetailsController::slotCurrentLibraryChanged()
{
    const int currentIndex = libraryDetailsWidget()->libraryComboBox->currentIndex();
    if (currentIndex >= 0) {
        libraryDetailsWidget()->libraryComboBox->setToolTip(
                    libraryDetailsWidget()->libraryComboBox->itemData(
                        currentIndex, Qt::ToolTipRole).toString());
        Qt4ProFileNode *proFileNode = m_proFileNodes.at(currentIndex);
        const QStringList configVar = proFileNode->variableValue(ConfigVar);
1014
1015
1016
        if (creatorPlatform() == CreatorWindows) {
            bool useSubfolders = false;
            if (configVar.contains(QLatin1String("debug_and_release"))
1017
                && configVar.contains(QLatin1String("debug_and_release_target")))
1018
1019
1020
1021
                useSubfolders = true;
            libraryDetailsWidget()->useSubfoldersCheckBox->setChecked(useSubfolders);
            libraryDetailsWidget()->addSuffixCheckBox->setChecked(!useSubfolders);
        }
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
    }

    if (guiSignalsIgnored())
        return;

    updateGui();

    emit completeChanged();
}

bool InternalLibraryDetailsController::isComplete() const
{
    return libraryDetailsWidget()->libraryComboBox->count() && platforms();
}

QString InternalLibraryDetailsController::snippet() const
{
    const int currentIndex = libraryDetailsWidget()->libraryComboBox->currentIndex();
1040

1041
1042
1043
    if (currentIndex < 0)
        return QString();

1044
    if (m_rootProjectPath.isEmpty())
1045
1046
1047
        return QString();


1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
    // dir of the root project
    QDir rootDir(m_rootProjectPath);

    // relative path for the project for which we insert the snippet,
    // it's relative to the root project
    const QString proRelavitePath = rootDir.relativeFilePath(proFile());

    // project for which we insert the snippet
    const ProjectExplorer::Project *project =
            ProjectExplorer::ProjectExplorerPlugin::instance()->session()->projectForFile(proFile());

    // the build directory of the active build configuration
1060
1061
1062
1063
    QDir rootBuildDir = rootDir; // If the project is unconfigured use the project dir
    if (ProjectExplorer::Target *t = project->activeTarget())
        if (ProjectExplorer::BuildConfiguration *bc = t->activeBuildConfiguration())
            rootBuildDir = bc->buildDirectory();
1064
1065
1066
1067
1068
1069
1070

    // the project for which we insert the snippet inside build tree
    QFileInfo pfi(rootBuildDir.filePath(proRelavitePath));
    // the project dir for which we insert the snippet inside build tree
    QDir projectBuildDir(pfi.absolutePath());

    // current project node from combobox
1071
1072
    QFileInfo fi(proFile());
    QDir projectSrcDir(fi.absolutePath());
1073
1074
1075

    // project node which we want to link against
    Qt4ProFileNode *proFileNode = m_proFileNodes.at(currentIndex);
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
    TargetInformation targetInfo = proFileNode->targetInformation();

    const QString targetRelativePath = appendSeparator(projectBuildDir.relativeFilePath(targetInfo.buildDir));
    const QString includeRelativePath = projectSrcDir.relativeFilePath(libraryDetailsWidget()->includePathChooser->path());

    const bool useSubfolders = libraryDetailsWidget()->useSubfoldersCheckBox->isChecked();
    const bool addSuffix = libraryDetailsWidget()->addSuffixCheckBox->isChecked();

    QString snippetMessage;
    QTextStream str(&snippetMessage);
    str << "\n";
1087
1088
1089
1090

    // replace below to "PRI_OUT_PWD" when task QTBUG-13057 is done
    // (end enable adding libraries into .pri files as well).
    const QString outPwd = QLatin1String("OUT_PWD");
1091
    str << generateLibsSnippet(platforms(), macLibraryType(), targetInfo.target,
1092
                               targetRelativePath, outPwd,
1093
1094
1095
                               useSubfolders, addSuffix, true);
    str << generateIncludePathSnippet(includeRelativePath);
    str << generatePreTargetDepsSnippet(platforms(), linkageType(), targetInfo.target,
1096
                               targetRelativePath, outPwd,
1097
1098
1099
                               useSubfolders, addSuffix);
    return snippetMessage;
}