2019-01-24 16:43:03 +08:00
|
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
2018-10-24 04:03:00 +08:00
|
|
|
|
|
2022-08-24 16:39:22 +08:00
|
|
|
|
using System;
|
2018-10-24 04:03:00 +08:00
|
|
|
|
using osu.Framework.Allocation;
|
2019-02-21 18:04:31 +08:00
|
|
|
|
using osu.Framework.Bindables;
|
2025-01-06 16:51:04 +08:00
|
|
|
|
using osu.Framework.Extensions;
|
2018-10-24 04:03:00 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
2022-08-24 16:39:22 +08:00
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
2018-10-24 04:03:00 +08:00
|
|
|
|
using osu.Framework.Platform;
|
|
|
|
|
using osu.Game.Configuration;
|
2024-05-31 10:44:53 +08:00
|
|
|
|
using osu.Game.Localisation;
|
2025-01-06 16:51:04 +08:00
|
|
|
|
using osu.Game.Online.API;
|
2018-10-24 04:03:00 +08:00
|
|
|
|
using osu.Game.Overlays;
|
2022-08-24 16:39:22 +08:00
|
|
|
|
using osu.Game.Overlays.Dialog;
|
2025-01-06 16:51:04 +08:00
|
|
|
|
using osu.Game.Overlays.Notifications;
|
2024-11-04 20:44:25 +08:00
|
|
|
|
using WebCommonStrings = osu.Game.Resources.Localisation.Web.CommonStrings;
|
2018-10-24 04:03:00 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Online.Chat
|
|
|
|
|
{
|
|
|
|
|
public partial class ExternalLinkOpener : Component
|
|
|
|
|
{
|
2020-02-14 23:02:10 +08:00
|
|
|
|
[Resolved]
|
2022-08-24 16:39:22 +08:00
|
|
|
|
private GameHost host { get; set; } = null!;
|
2020-02-14 21:14:00 +08:00
|
|
|
|
|
2023-07-11 17:42:31 +08:00
|
|
|
|
[Resolved]
|
|
|
|
|
private Clipboard clipboard { get; set; } = null!;
|
|
|
|
|
|
2025-01-06 16:51:04 +08:00
|
|
|
|
[Resolved]
|
2022-08-24 16:39:22 +08:00
|
|
|
|
private IDialogOverlay? dialogOverlay { get; set; }
|
2020-02-14 21:14:00 +08:00
|
|
|
|
|
2025-01-06 16:51:04 +08:00
|
|
|
|
[Resolved]
|
|
|
|
|
private INotificationOverlay? notificationOverlay { get; set; }
|
|
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
|
private IAPIProvider api { get; set; } = null!;
|
|
|
|
|
|
2022-08-24 16:39:22 +08:00
|
|
|
|
private Bindable<bool> externalLinkWarning = null!;
|
2018-10-24 04:03:00 +08:00
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader(true)]
|
2020-02-14 21:14:00 +08:00
|
|
|
|
private void load(OsuConfigManager config)
|
2018-10-24 04:03:00 +08:00
|
|
|
|
{
|
2018-11-02 04:52:07 +08:00
|
|
|
|
externalLinkWarning = config.GetBindable<bool>(OsuSetting.ExternalLinkWarning);
|
2018-10-24 04:03:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-01-06 16:51:04 +08:00
|
|
|
|
public void OpenUrlExternally(string url, LinkWarnMode warnMode = LinkWarnMode.Default)
|
2018-10-24 04:03:00 +08:00
|
|
|
|
{
|
2025-01-06 16:51:04 +08:00
|
|
|
|
bool isTrustedDomain;
|
|
|
|
|
|
|
|
|
|
if (url.StartsWith('/'))
|
|
|
|
|
{
|
|
|
|
|
url = $"{api.WebsiteRootUrl}{url}";
|
|
|
|
|
isTrustedDomain = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
isTrustedDomain = url.StartsWith(api.WebsiteRootUrl, StringComparison.Ordinal);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!url.CheckIsValidUrl())
|
|
|
|
|
{
|
|
|
|
|
notificationOverlay?.Post(new SimpleErrorNotification
|
|
|
|
|
{
|
|
|
|
|
Text = NotificationsStrings.UnsupportedOrDangerousUrlProtocol(url),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool shouldWarn;
|
|
|
|
|
|
|
|
|
|
switch (warnMode)
|
|
|
|
|
{
|
|
|
|
|
case LinkWarnMode.Default:
|
|
|
|
|
shouldWarn = externalLinkWarning.Value && !isTrustedDomain;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case LinkWarnMode.AlwaysWarn:
|
|
|
|
|
shouldWarn = true;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case LinkWarnMode.NeverWarn:
|
|
|
|
|
shouldWarn = false;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(warnMode), warnMode, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (dialogOverlay != null && shouldWarn)
|
2023-07-11 17:42:31 +08:00
|
|
|
|
dialogOverlay.Push(new ExternalLinkDialog(url, () => host.OpenUrlExternally(url), () => clipboard.SetText(url)));
|
2018-10-24 04:03:00 +08:00
|
|
|
|
else
|
2018-11-02 04:52:07 +08:00
|
|
|
|
host.OpenUrlExternally(url);
|
2018-10-24 04:03:00 +08:00
|
|
|
|
}
|
2022-08-24 16:39:22 +08:00
|
|
|
|
|
|
|
|
|
public partial class ExternalLinkDialog : PopupDialog
|
|
|
|
|
{
|
|
|
|
|
public ExternalLinkDialog(string url, Action openExternalLinkAction, Action copyExternalLinkAction)
|
|
|
|
|
{
|
2024-10-07 15:44:10 +08:00
|
|
|
|
HeaderText = DialogStrings.CautionHeaderText;
|
2024-05-31 09:46:30 +08:00
|
|
|
|
BodyText = $"Are you sure you want to open the following link in a web browser?\n\n{url}";
|
2022-08-24 16:39:22 +08:00
|
|
|
|
|
|
|
|
|
Icon = FontAwesome.Solid.ExclamationTriangle;
|
|
|
|
|
|
|
|
|
|
Buttons = new PopupDialogButton[]
|
|
|
|
|
{
|
|
|
|
|
new PopupDialogOkButton
|
|
|
|
|
{
|
2024-05-30 15:34:52 +08:00
|
|
|
|
Text = @"Open in browser",
|
2022-08-24 16:39:22 +08:00
|
|
|
|
Action = openExternalLinkAction
|
|
|
|
|
},
|
|
|
|
|
new PopupDialogCancelButton
|
|
|
|
|
{
|
2024-11-04 20:44:25 +08:00
|
|
|
|
Text = CommonStrings.CopyLink,
|
2022-08-24 16:39:22 +08:00
|
|
|
|
Action = copyExternalLinkAction
|
|
|
|
|
},
|
|
|
|
|
new PopupDialogCancelButton
|
|
|
|
|
{
|
2024-11-04 20:44:25 +08:00
|
|
|
|
Text = WebCommonStrings.ButtonsCancel,
|
2022-08-24 16:39:22 +08:00
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-24 04:03:00 +08:00
|
|
|
|
}
|
|
|
|
|
}
|