localhelpmanager.cpp 4.61 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
kh1's avatar
kh1 committed
2
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
kh1's avatar
kh1 committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
kh1's avatar
kh1 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.
kh1's avatar
kh1 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
con's avatar
con committed
26
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
kh1's avatar
kh1 committed
29

30
#include "localhelpmanager.h"
31

32
33
#include "bookmarkmanager.h"
#include "helpconstants.h"
kh1's avatar
kh1 committed
34

35
#include <app/app_version.h>
kh1's avatar
kh1 committed
36
#include <coreplugin/helpmanager.h>
37

38
#include <QMutexLocker>
39

40
#include <QHelpEngine>
kh1's avatar
kh1 committed
41

kh1's avatar
kh1 committed
42
using namespace Help::Internal;
kh1's avatar
kh1 committed
43

44
45
static LocalHelpManager *m_instance = 0;

kh1's avatar
kh1 committed
46
47
QMutex LocalHelpManager::m_guiMutex;
QHelpEngine* LocalHelpManager::m_guiEngine = 0;
48

kh1's avatar
kh1 committed
49
50
QMutex LocalHelpManager::m_bkmarkMutex;
BookmarkManager* LocalHelpManager::m_bookmarkManager = 0;
51

kh1's avatar
kh1 committed
52
LocalHelpManager::LocalHelpManager(QObject *parent)
53
    : QObject(parent)
kh1's avatar
kh1 committed
54
55
    , m_guiNeedsSetup(true)
    , m_needsCollectionFile(true)
kh1's avatar
kh1 committed
56
{
57
    m_instance = this;
kh1's avatar
kh1 committed
58
59
}

kh1's avatar
kh1 committed
60
LocalHelpManager::~LocalHelpManager()
61
{
62
63
64
65
66
    if (m_bookmarkManager) {
        m_bookmarkManager->saveBookmarks();
        delete m_bookmarkManager;
        m_bookmarkManager = 0;
    }
67
68
69

    delete m_guiEngine;
    m_guiEngine = 0;
70
71
}

72
73
74
75
76
LocalHelpManager *LocalHelpManager::instance()
{
    return m_instance;
}

kh1's avatar
kh1 committed
77
void LocalHelpManager::setupGuiHelpEngine()
78
79
80
{
    if (m_needsCollectionFile) {
        m_needsCollectionFile = false;
kh1's avatar
kh1 committed
81
        helpEngine().setCollectionFile(Core::HelpManager::collectionFilePath());
82
83
84
85
    }

    if (m_guiNeedsSetup) {
        m_guiNeedsSetup = false;
kh1's avatar
kh1 committed
86
        helpEngine().setupData();
87
88
89
    }
}

kh1's avatar
kh1 committed
90
void LocalHelpManager::setEngineNeedsUpdate()
kh1's avatar
kh1 committed
91
{
kh1's avatar
kh1 committed
92
    m_guiNeedsSetup = true;
93
94
}

kh1's avatar
kh1 committed
95
QHelpEngine &LocalHelpManager::helpEngine()
kh1's avatar
kh1 committed
96
{
97
98
99
    if (!m_guiEngine) {
        QMutexLocker _(&m_guiMutex);
        if (!m_guiEngine) {
100
            m_guiEngine = new QHelpEngine(QString());
101
102
            m_guiEngine->setAutoSaveFilter(false);
        }
kh1's avatar
kh1 committed
103
    }
104
    return *m_guiEngine;
kh1's avatar
kh1 committed
105
}
106

kh1's avatar
kh1 committed
107
BookmarkManager& LocalHelpManager::bookmarkManager()
108
109
{
    if (!m_bookmarkManager) {
kh1's avatar
kh1 committed
110
111
112
113
        QMutexLocker _(&m_bkmarkMutex);
        if (!m_bookmarkManager) {
            m_bookmarkManager = new BookmarkManager;
            m_bookmarkManager->setupBookmarkModels();
114
            const QString &url = QString::fromLatin1("qthelp://org.qt-project.qtcreator."
kh1's avatar
kh1 committed
115
116
117
118
                "%1%2%3/doc/index.html").arg(IDE_VERSION_MAJOR).arg(IDE_VERSION_MINOR)
                .arg(IDE_VERSION_RELEASE);
            helpEngine().setCustomValue(QLatin1String("DefaultHomePage"), url);
        }
119
120
121
    }
    return *m_bookmarkManager;
}
122
123
124
125
126
127

QVariant LocalHelpManager::engineFontSettings()
{
    return helpEngine().customValue(Constants::FontKey, QVariant());
}

128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*!
 * Checks if the string does contain a scheme, and if that scheme is a "sensible" scheme for
 * opening in a internal or external browser (qthelp, about, file, http, https).
 * This is necessary to avoid trying to open e.g. "Foo::bar" in a external browser.
 */
bool LocalHelpManager::isValidUrl(const QString &link)
{
    QUrl url(link);
    if (!url.isValid())
        return false;
    const QString scheme = url.scheme();
    return (scheme == QLatin1String("qthelp")
            || scheme == QLatin1String("about")
            || scheme == QLatin1String("file")
            || scheme == QLatin1String("http")
            || scheme == QLatin1String("https"));
}

146
147
148
149
150
151
152
QByteArray LocalHelpManager::helpData(const QUrl &url)
{
    const QHelpEngineCore &engine = helpEngine();

    return engine.findFile(url).isValid() ? engine.fileData(url)
            : tr("Could not load \"%1\".").arg(url.toString()).toUtf8();
}