aboutsummaryrefslogtreecommitdiffstats
path: root/src/usagestatisticplugin.cpp
blob: 5f04486ad967803258bd36b2ec55fc1f450168b1 (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
/****************************************************************************
**
** Copyright (C) 2019 The Qt Company
** Contact: https://www.qt.io/licensing/
**
** This file is part of UsageStatistic plugin for Qt Creator.
**
** 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 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 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** 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.
**
****************************************************************************/
#include "usagestatisticplugin.h"
#include "usagestatisticconstants.h"

#include <coreplugin/icore.h>
#include <coreplugin/icontext.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/command.h>
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/coreconstants.h>

#include <KUserFeedback/Provider>
#include <KUserFeedback/ApplicationVersionSource>
#include <KUserFeedback/CompilerInfoSource>
#include <KUserFeedback/CpuInfoSource>
#include <KUserFeedback/LocaleInfoSource>
#include <KUserFeedback/OpenGLInfoSource>
#include <KUserFeedback/PlatformInfoSource>
#include <KUserFeedback/QPAInfoSource>
#include <KUserFeedback/QtVersionSource>
#include <KUserFeedback/ScreenInfoSource>
#include <KUserFeedback/StartCountSource>
#include <KUserFeedback/UsageTimeSource>
#include <KUserFeedback/StyleInfoSource>

#include "datasources/qtclicensesource.h"
#include "datasources/buildcountsource.h"
#include "datasources/buildsystemsource.h"
#include "datasources/modeusagetimesource.h"
#include "datasources/examplesdatasource.h"
#include "datasources/kitsource.h"
#include "datasources/qmldesignerusagetimesource.h"
#include "datasources/servicesource.h"

#include "services/datasubmitter.h"

#include "ui/usagestatisticpage.h"
#include "ui/outputpane.h"

#include "common/utils.h"

namespace UsageStatistic {
namespace Internal {

UsageStatisticPlugin::UsageStatisticPlugin() = default;

UsageStatisticPlugin::~UsageStatisticPlugin() = default;

bool UsageStatisticPlugin::initialize(const QStringList &arguments, QString *errorString)
{
    Q_UNUSED(arguments)
    Q_UNUSED(errorString)

    // We have to create a pane here because of OutputPaneManager internal initialization order
    createOutputPane();

    return true;
}

void UsageStatisticPlugin::extensionsInitialized()
{
}

static void addDefaultDataSources(KUserFeedback::Provider &provider)
{
    provider.addDataSource(new KUserFeedback::ApplicationVersionSource);
    provider.addDataSource(new KUserFeedback::CompilerInfoSource);
    provider.addDataSource(new KUserFeedback::CpuInfoSource);
    provider.addDataSource(new KUserFeedback::LocaleInfoSource);
    provider.addDataSource(new KUserFeedback::OpenGLInfoSource);
    provider.addDataSource(new KUserFeedback::PlatformInfoSource);
    provider.addDataSource(new KUserFeedback::QPAInfoSource);
    provider.addDataSource(new KUserFeedback::QtVersionSource);
    provider.addDataSource(new KUserFeedback::ScreenInfoSource);
    provider.addDataSource(new KUserFeedback::StartCountSource);
    provider.addDataSource(new KUserFeedback::UsageTimeSource);
    provider.addDataSource(new KUserFeedback::StyleInfoSource);
}

static void addQtCreatorDataSources(KUserFeedback::Provider &provider)
{
    provider.addDataSource(new QtcLicenseSource);
    provider.addDataSource(new BuildCountSource);
    provider.addDataSource(new BuildSystemSource);
    provider.addDataSource(new ModeUsageTimeSource);
    provider.addDataSource(new ExamplesDataSource);
    provider.addDataSource(new KitSource);
    provider.addDataSource(new QmlDesignerUsageTimeSource);
}

static void addServiceDataSource(const std::shared_ptr<KUserFeedback::Provider> &provider)
{
    if (provider) {
        provider->addDataSource(new ServiceSource(provider));
    }
}

bool UsageStatisticPlugin::delayedInitialize()
{
    // We should create the provider only after everything else
    // is initialised (e.g., setting organization name)
    createProvider();

    addDefaultDataSources(*m_provider);
    addQtCreatorDataSources(*m_provider);
    addServiceDataSource(m_provider);

    createUsageStatisticPage();

    restoreSettings();

    configureOutputPane();

    return true;
}

ExtensionSystem::IPlugin::ShutdownFlag UsageStatisticPlugin::aboutToShutdown()
{
    storeSettings();

    return SynchronousShutdown;
}

void UsageStatisticPlugin::createUsageStatisticPage()
{
    m_usageStatisticPage = std::make_unique<UsageStatisticPage>(m_provider);

    connect(m_usageStatisticPage.get(), &UsageStatisticPage::settingsChanged,
            this, &UsageStatisticPlugin::storeSettings);
}

void UsageStatisticPlugin::createOutputPane()
{
    m_outputPane = std::make_unique<OutputPane>();
}

void UsageStatisticPlugin::configureOutputPane()
{
    Q_ASSERT(m_outputPane);

    m_outputPane->setProvider(m_provider);

    connect(m_provider.get(), &KUserFeedback::Provider::showEncouragementMessage,
            m_outputPane.get(), &OutputPane::flash);
}

void UsageStatisticPlugin::storeSettings()
{
    if (m_provider) {
        m_provider->store();
    }
}

void UsageStatisticPlugin::restoreSettings()
{
    if (m_provider) {
        m_provider->load();
    }
}

static constexpr int encouragementTimeSec() { return 1800; }
static constexpr int encouragementIntervalDays() { return 1; }
static constexpr int submissionIntervalDays() { return 10; }

void UsageStatisticPlugin::createProvider()
{
    Q_ASSERT(!m_provider);
    m_provider = std::make_shared<KUserFeedback::Provider>();

    m_provider->setFeedbackServer(QString::fromUtf8(Utils::serverUrl()));
    m_provider->setDataSubmitter(new DataSubmitter);

    m_provider->setApplicationUsageTimeUntilEncouragement(encouragementTimeSec());
    m_provider->setEncouragementDelay(encouragementTimeSec());
    m_provider->setEncouragementInterval(encouragementIntervalDays());

    m_provider->setSubmissionInterval(submissionIntervalDays());
}

} // namespace Internal
} // namespace UsageStatistic