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
|
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Windows;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
namespace QtVsTools.Core.Options
{
using Core;
using QtVsTools.Common;
using VisualStudio;
using static Common.Utils;
using static HelperFunctions;
public class QtVersionsPage : UIElementDialogPage
{
private static LazyFactory Lazy { get; } = new();
private static QtVersionsTable VersionsTable => Lazy.Get(() =>
VersionsTable, () => new QtVersionsTable());
protected override UIElement Child => VersionsTable;
public override void LoadSettingsFromStorage()
{
var versions = new ObservableCollection<QtVersion>();
var defaultVersion = QtVersionManager.GetDefaultVersionName();
foreach (var versionName in QtVersionManager.GetVersions()) {
var versionPath = QtVersionManager.GetInstallPath(versionName);
if (string.IsNullOrEmpty(versionPath))
continue;
var host = BuildHost.Windows;
var compiler = "msvc";
if (versionPath.StartsWith("SSH:") || versionPath.StartsWith("WSL:")) {
var linuxPaths = versionPath.Split(':');
versionPath = linuxPaths[1];
host = linuxPaths[0] == "SSH" ? BuildHost.LinuxSSH : BuildHost.LinuxWSL;
compiler = "g++";
if (linuxPaths.Length > 2 && !string.IsNullOrEmpty(linuxPaths[2]))
compiler = linuxPaths[2];
}
var isDefault = versionName == defaultVersion;
versions.Insert(isDefault ? 0 : versions.Count, new QtVersion
{
IsDefault = isDefault,
InitialIsDefault = versionName == defaultVersion,
Name = versionName,
InitialName = versionName,
Path = versionPath,
InitialPath = FromNativeSeparators(NormalizePath(versionPath) ?? ""),
Host = host,
InitialHost = host,
Compiler = compiler,
InitialCompiler = compiler
});
}
VersionsTable.QtVersions = versions;
}
public override void SaveSettingsToStorage()
{
static void RemoveVersion(string versionName)
{
try {
if (QtVersionManager.HasVersion(versionName))
QtVersionManager.RemoveVersion(versionName);
} catch (Exception exception) {
exception.Log();
}
}
foreach (var qtVersion in VersionsTable.RemovedQtVersions)
RemoveVersion(qtVersion.InitialName);
VersionsTable.RemovedQtVersions.Clear();
foreach (var version in VersionsTable.QtVersions) {
try {
if (version.State.HasFlag(State.HostModified) || version.State.HasFlag(State.CompilerModified)) {
if (version.Host != BuildHost.Windows) {
var access = version.Host == BuildHost.LinuxSSH ? "SSH" : "WSL";
var compiler = version.Compiler;
if (compiler == "g++")
compiler = string.Empty;
QtVersionManager.SaveVersion(version.Name,
$"{access}:{version.Path}:{compiler}", checkPath: false);
} else {
QtVersionManager.SaveVersion(version.Name, version.Path);
}
RemoveVersion(version.InitialName);
continue;
}
if (version.State.HasFlag(State.PathModified))
QtVersionManager.SaveVersion(version.Name, version.Path);
if (!version.State.HasFlag(State.NameModified))
continue;
try {
QtVersionManager.SaveVersion(version.Name, version.Path);
} catch (Exception exception) {
exception.Log();
}
RemoveVersion(version.InitialName);
} catch (Exception exception) {
exception.Log();
RemoveVersion(version.Name);
}
}
try {
var defaultVersion =
VersionsTable.QtVersions.FirstOrDefault(v => v is { IsDefault: true });
QtVersionManager.SaveDefaultVersion(defaultVersion?.Name ?? "");
} catch (Exception exception) {
exception.Log();
}
if (!Notifications.NoQtVersion.IsOpen || QtVersionManager.GetVersions()?.Any() != true)
return;
ThreadHelper.ThrowIfNotOnUIThread();
Notifications.NoQtVersion.Close();
}
protected override void OnApply(PageApplyEventArgs e)
{
var errorMessages = VersionsTable.GetErrorMessages().ToList();
try {
var versions = VersionsTable.QtVersions;
foreach (var version in versions) {
if (!version.State.HasFlag(State.PathModified) || version.Host != BuildHost.Windows)
continue;
var versionPath = version.Path;
if (string.IsNullOrEmpty(versionPath))
continue;
if (string.Equals(Path.GetFileName(versionPath), "qmake.exe", IgnoreCase))
versionPath = Path.GetDirectoryName(versionPath);
if (string.Equals(Path.GetFileName(versionPath ?? ""), "bin", IgnoreCase))
versionPath = Path.GetDirectoryName(versionPath);
var versionInfo = VersionInformation.GetOrAddByPath(versionPath);
var generator = versionInfo?.GetQMakeConfEntry("MAKEFILE_GENERATOR");
if (generator is "MSVC.NET" or "MSBUILD")
continue;
var message = string.Empty;
if (!string.IsNullOrEmpty(version.Name))
message += $"{version.Name} - ";
message += "Incompatible makefile generator";
if (!string.IsNullOrEmpty(generator))
message += $": {generator}";
errorMessages.Add(message);
version.ErrorMessage = message;
}
} catch (Exception exception) {
errorMessages.Add(exception.Message);
}
if (errorMessages.Any()) {
VersionsTable.Focus();
var distinctErrorMessages = errorMessages
.SelectMany(errMsg => errMsg.Split(new[] { Environment.NewLine },
StringSplitOptions.RemoveEmptyEntries))
.Distinct()
.Select(message => " * " + message);
var errorMessage = "Invalid Qt versions:" + Environment.NewLine
+ Environment.NewLine + string.Join(Environment.NewLine, distinctErrorMessages);
VsShellUtilities.ShowMessageBox(
VsServiceProvider.Instance as IServiceProvider,
errorMessage, null,
OLEMSGICON.OLEMSGICON_WARNING,
OLEMSGBUTTON.OLEMSGBUTTON_OK,
OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
e.ApplyBehavior = ApplyKind.CancelNoNavigate;
} else {
base.OnApply(e);
Instances.CMake.ActiveProject?.CheckQtStatus();
}
}
}
}
|