aboutsummaryrefslogtreecommitdiffstats
path: root/QtVsTools.Core/MsBuild/MsBuildProject.Messages.cs
blob: 24ec0251b1fe4c769c9fb0bd60fe04c99bf22b4d (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
// 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 Microsoft.VisualStudio;
using Microsoft.VisualStudio.Imaging;
using Microsoft.VisualStudio.Imaging.Interop;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;

using Task = System.Threading.Tasks.Task;

namespace QtVsTools.Core.MsBuild
{
    using Common;
    using Options;
    using VisualStudio;

    public partial class MsBuildProject
    {
        private class UpdateQtInstallation : InfoBarMessage
        {
            private UpdateQtInstallation(MsBuildProject project = null) => Project = project;

            private static UpdateQtInstallation MessageImpl =>
                StaticLazy.Get(() => MessageImpl, () => null);

            public static UpdateQtInstallation Message(MsBuildProject project = null) =>
                StaticLazy.Get(() => MessageImpl, () => new UpdateQtInstallation(project));

            protected override ImageMoniker Icon => KnownMonikers.StatusWarning;

            protected override TextSpan[] Text => new TextSpan[]
            {
                new() { Bold = true, Text = "Qt Visual Studio Tools" },
                new TextSpacer(2),
                Utils.EmDash,
                new TextSpacer(2),
                "The project's 'Qt Installation' property is not set correctly. Please specify a "
                    + "valid Qt version or path."
            };

            protected override Hyperlink[] Hyperlinks => new Hyperlink[]
            {
                    new()
                    {
                        Text = "Open project properties",
                        CloseInfoBar = true,
                        OnClicked = () => {
                            if (!Project.SelectInSolutionExplorer())
                                return;
                            if (VsServiceProvider.GetService<SVsUIShell, IVsUIShell>() is not {} shell)
                                return;

                            var guid = typeof(VSConstants.VSStd97CmdID).GUID;
                            const uint id = (uint)VSConstants.VSStd97CmdID.ProjectProperties;
                            ThreadHelper.ThrowIfNotOnUIThread();
                            shell.PostExecCommand(guid, id, 0, null);
                        }
                    },
                    new()
                    {
                        Text = "Don't show again",
                        CloseInfoBar = true,
                        OnClicked = () =>
                        {
                            QtOptionsPage.UpdateQtInstallation = false;
                            QtOptionsPage.SaveSettingsToStorageStatic();
                        }
                    }
            };

            private MsBuildProject Project { get; }
        }

        public static void ShowUpdateQtInstallationMessage(MsBuildProject project)
        {
            ThreadHelper.JoinableTaskFactory.Run(() =>
                ShowUpdateQtInstallationMessageAsync(project));
        }

        public static async Task ShowUpdateQtInstallationMessageAsync(MsBuildProject project)
        {
            if (QtOptionsPage.UpdateQtInstallation)
                await VsShell.UiThreadAsync(UpdateQtInstallation.Message(project).Show);
        }

        private class UpdateProjectFormat : InfoBarMessage
        {
            public static UpdateProjectFormat Message =>
                StaticLazy.Get(() => Message, () => new UpdateProjectFormat());

            protected override ImageMoniker Icon => KnownMonikers.StatusWarning;

            protected override TextSpan[] Text => new TextSpan[]
            {
                new() { Bold = true, Text = "Qt Visual Studio Tools" },
                new TextSpacer(2),
                Utils.EmDash,
                new TextSpacer(2),
                "You are using a legacy Qt project with Qt Visual Studio Tools. The project format"
                    + " is no longer supported, please update your project to our current version."
            };

            protected override Hyperlink[] Hyperlinks => new Hyperlink[]
            {
                    new()
                    {
                        Text = "Update",
                        CloseInfoBar = true,
                        OnClicked = () => {
                            ThreadHelper.ThrowIfNotOnUIThread();
                            MsBuildProjectConverter.SolutionToQtMsBuild();
                        }
                    },
                    new()
                    {
                        Text = "Don't show again",
                        CloseInfoBar = true,
                        OnClicked = () =>
                        {
                            QtOptionsPage.UpdateProjectFormat = false;
                            QtOptionsPage.SaveSettingsToStorageStatic();
                        }
                    }
            };
        }

        public static void ShowUpdateFormatMessage()
        {
            ThreadHelper.JoinableTaskFactory.Run(ShowUpdateFormatMessageAsync);
        }

        public static async Task ShowUpdateFormatMessageAsync()
        {
            if (!QtOptionsPage.UpdateProjectFormat)
                return;
            await VsShell.UiThreadAsync(UpdateProjectFormat.Message.Show);
        }

        public static void CloseUpdateFormatMessage()
        {
            ThreadHelper.JoinableTaskFactory.Run(CloseUpdateFormatMessageAsync);
        }

        public static async Task CloseUpdateFormatMessageAsync()
        {
            await VsShell.UiThreadAsync(UpdateProjectFormat.Message.Close);
        }

        private class ProjectFormatUpdated : InfoBarMessage
        {
            public static ProjectFormatUpdated Message =>
                StaticLazy.Get(() => Message, () => new ProjectFormatUpdated());

            public string ReportPath { get; set; }

            protected override ImageMoniker Icon => KnownMonikers.StatusInformation;

            protected override TextSpan[] Text => new TextSpan[]
            {
                new() { Bold = true, Text = "Qt Visual Studio Tools" },
                new TextSpacer(2),
                Utils.EmDash,
                new TextSpacer(2),
                "Project format conversion complete."
            };

            protected override Hyperlink[] Hyperlinks => new Hyperlink[]
            {
                    new()
                    {
                        Text = "View report",
                        CloseInfoBar = true,
                        OnClicked = () => {
                            ThreadHelper.ThrowIfNotOnUIThread();
                            VsEditor.Open(ReportPath);
                        }
                    }
            };
        }

        public static void ShowProjectFormatUpdated(string reportPath)
        {
            ThreadHelper.JoinableTaskFactory.Run(() => ShowProjectFormatUpdatedAsync(reportPath));
        }

        public static async Task ShowProjectFormatUpdatedAsync(string reportPath)
        {
            ProjectFormatUpdated.Message.ReportPath = reportPath;
            await VsShell.UiThreadAsync(ProjectFormatUpdated.Message.Show);
        }

        public static void CloseProjectFormatUpdated()
        {
            ThreadHelper.JoinableTaskFactory.Run(CloseProjectFormatUpdatedAsync);
        }

        public static async Task CloseProjectFormatUpdatedAsync()
        {
            await VsShell.UiThreadAsync(ProjectFormatUpdated.Message.Close);
        }
    }
}