1 /****************************************************************************
2 **
3 ** Copyright (C) 2021 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #include "dockerconstants.h"
27 #include "dockersettings.h"
28 
29 #include <coreplugin/icore.h>
30 
31 #include <projectexplorer/projectexplorerconstants.h>
32 
33 #include <utils/layoutbuilder.h>
34 #include <utils/qtcprocess.h>
35 #include <utils/qtcsettings.h>
36 
37 using namespace Utils;
38 
39 namespace Docker {
40 namespace Internal {
41 
42 // DockerSettings
43 
44 const char SETTINGS_KEY[] = "Docker";
45 
46 static DockerSettings *theSettings = nullptr;
47 
DockerSettings()48 DockerSettings::DockerSettings()
49 {
50     theSettings = this;
51     setAutoApply(false);
52     readSettings(Core::ICore::settings());
53 
54     imageListFilter.setSettingsKey("DockerListFilter");
55     imageListFilter.setPlaceHolderText(tr("<filter>"));
56     imageListFilter.setDisplayStyle(StringAspect::LineEditDisplay);
57     imageListFilter.setLabelText(tr("Filter:"));
58 
59     imageList.setDisplayStyle(StringAspect::TextEditDisplay);
60     imageList.setLabelText(tr("Images:"));
61 
62     connect(&imageListFilter, &BaseAspect::changed, this, &DockerSettings::updateImageList);
63 }
64 
instance()65 DockerSettings *DockerSettings::instance()
66 {
67     return theSettings;
68 }
69 
writeSettings(QSettings * settings) const70 void DockerSettings::writeSettings(QSettings *settings) const
71 {
72     settings->remove(SETTINGS_KEY);
73     settings->beginGroup(SETTINGS_KEY);
74     forEachAspect([settings](BaseAspect *aspect) {
75         QtcSettings::setValueWithDefault(settings, aspect->settingsKey(),
76                                          aspect->value(), aspect->defaultValue());
77     });
78     settings->endGroup();
79 }
80 
updateImageList()81 void DockerSettings::updateImageList()
82 {
83     QtcProcess process;
84     process.setCommand({"docker", {"search", imageListFilter.value()}});
85 
86     connect(&process, &QtcProcess::finished, this, [&process, this] {
87         const QString data = QString::fromUtf8(process.readAllStandardOutput());
88         imageList.setValue(data);
89     });
90 
91     process.start();
92     process.waitForFinished();
93 }
94 
readSettings(const QSettings * settings)95 void DockerSettings::readSettings(const QSettings *settings)
96 {
97     const QString keyRoot = QString(SETTINGS_KEY) + '/';
98     forEachAspect([settings, keyRoot](BaseAspect *aspect) {
99         QString key = aspect->settingsKey();
100         const QVariant value = settings->value(keyRoot + key, aspect->defaultValue());
101         aspect->setValue(value);
102     });
103 }
104 
105 // DockerOptionsPage
106 
DockerOptionsPage(DockerSettings * settings)107 DockerOptionsPage::DockerOptionsPage(DockerSettings *settings)
108 {
109     setId(Constants::DOCKER_SETTINGS_ID);
110     setDisplayName(DockerSettings::tr("Docker"));
111     setCategory(ProjectExplorer::Constants::DEVICE_SETTINGS_CATEGORY);
112     setDisplayCategory(QCoreApplication::translate("ProjectExplorer", "Devices"));
113     setCategoryIconPath(":/projectexplorer/images/settingscategory_devices.png");
114     setSettings(settings);
115 
116     setLayouter([settings](QWidget *widget) {
117         using namespace Layouting;
118         DockerSettings &s = *settings;
119 
120         Column {
121             Group {
122                 Title(DockerSettings::tr("Search Images on Docker Hub")),
123                 Form {
124                     s.imageListFilter,
125                     s.imageList
126                 },
127             },
128             Stretch()
129         }.attachTo(widget);
130     });
131 }
132 
133 } // Internal
134 } // Docker
135