Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add QR Code for Quick Setup #525

Merged
merged 1 commit into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
<PackageVersion Include="Microsoft.Win32.Registry" Version="5.0.0" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<PackageVersion Include="QRCoder" Version="1.5.1" />
<PackageVersion Include="sqlite-net-pcl" Version="1.8.116" />
<PackageVersion Include="System.Collections" Version="4.3.0" />
<PackageVersion Include="System.Drawing.Common" Version="8.0.4" />
Expand Down
20 changes: 20 additions & 0 deletions MacroDeck/DataTypes/QrCode/QuickConnectQrCodeData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace SuchByte.MacroDeck.DataTypes.QrCode;

public class QuickConnectQrCodeData
{
public string InstanceName { get; set; }

public List<string> NetworkInterfaces { get; set; }

public int Port { get; set; }

public bool Ssl { get; set; }

public QuickConnectQrCodeData(string instanceName, List<string> networkInterfaces, int port, bool ssl)
{
InstanceName = instanceName;
NetworkInterfaces = networkInterfaces;
Port = port;
Ssl = ssl;
}
}
207 changes: 82 additions & 125 deletions MacroDeck/GUI/MainWindow.Designer.cs

Large diffs are not rendered by default.

32 changes: 3 additions & 29 deletions MacroDeck/GUI/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ private void UpdateAvailable(object? sender, UpdateApiVersionInfo e)

private void UpdateTranslation()
{
lblIpAddressHostname.Text = LanguageManager.Strings.IpAddressHostNamePort;
}

private void LanguageChanged(object? sender, EventArgs e)
Expand Down Expand Up @@ -115,7 +114,6 @@ private void MainWindowShown(object? sender, EventArgs e)
{
Application.DoEvents();
RefreshPluginsLabels();
LoadHosts();

if (MacroDeck.SafeMode)
{
Expand All @@ -138,6 +136,8 @@ private void MainWindowShown(object? sender, EventArgs e)
using var updateAvailableDialog = new UpdateAvailableDialog(updateApiVersionInfo);
updateAvailableDialog.ShowDialog();
}

this.qrCodeBox.BackgroundImage = QrCodeService.Instance.GetQuickSetupQrCode();
}

private void MainWindow_Load(object? sender, EventArgs e)
Expand All @@ -158,26 +158,6 @@ private void MainWindow_Load(object? sender, EventArgs e)
CenterToScreen();
}

private void LoadHosts()
{
hosts.SelectedIndexChanged -= Hosts_SelectedIndexChanged;
foreach (var networkInterface in NetworkInterface.GetAllNetworkInterfaces())
{
var ipAddress = networkInterface
.GetIPProperties()
.UnicastAddresses
.FirstOrDefault(x => x.Address.AddressFamily == AddressFamily.InterNetwork)
?.Address
.ToString();
if (!string.IsNullOrWhiteSpace(ipAddress))
{
hosts.Items.Add(ipAddress);
}
}
hosts.Text = MacroDeck.Configuration.HostAddress;
hosts.SelectedIndexChanged += Hosts_SelectedIndexChanged;
}

private void NotificationsChanged(object? sender, EventArgs e)
{
btnNotifications.NotificationCount = NotificationManager.Notifications.Count;
Expand Down Expand Up @@ -254,7 +234,7 @@ private void BtnNotifications_Click(object? sender, EventArgs e)
{
_notificationsList = new NotificationsList
{
Location = btnNotifications.Location with { Y = btnNotifications.Location.Y + btnNotifications.Height }
Location = btnNotifications.Location with { Y = btnNotifications.Location.Y + btnNotifications.Height, X = btnNotifications.Location.X + btnNotifications.Size.Width + 20 }
};
_notificationsList.OnCloseRequested += (_, _) =>
{
Expand All @@ -272,10 +252,4 @@ private void BtnNotifications_Click(object? sender, EventArgs e)
_notificationsList.BringToFront();
}
}

private void Hosts_SelectedIndexChanged(object? sender, EventArgs e)
{
MacroDeck.Configuration.HostAddress = hosts.Text;
MacroDeck.Configuration.Save(ApplicationPaths.MainConfigFilePath);
}
}
62 changes: 61 additions & 1 deletion MacroDeck/GUI/MainWindow.resx
Original file line number Diff line number Diff line change
@@ -1,4 +1,64 @@
<root>
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema

Version 2.0

The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.

Example:

... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>

There are any number of "resheader" rows that contain simple
name/value pairs.

Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.

The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:

Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.

mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
Expand Down
1 change: 1 addition & 0 deletions MacroDeck/MacroDeck.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
<PackageReference Include="Microsoft.Win32.Registry" />
<PackageReference Include="Newtonsoft.Json" />
<PackageReference Include="QRCoder" />
<PackageReference Include="sqlite-net-pcl" />
<PackageReference Include="System.Collections" />
<PackageReference Include="System.Drawing.Common" />
Expand Down
60 changes: 60 additions & 0 deletions MacroDeck/Services/QrCodeService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System.IO;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text.Json;
using QRCoder;
using SuchByte.MacroDeck.DataTypes.QrCode;
using SuchByte.MacroDeck.Logging;

namespace SuchByte.MacroDeck.Services;

public class QrCodeService
{
public static readonly QrCodeService Instance = new();

private byte[]? _quickSetupQrCode;

public Image GetQuickSetupQrCode()
{
if (_quickSetupQrCode is not null)
{
return FromSavedBytes();
}

var networkInterfaces = new List<string>();
try
{
networkInterfaces.AddRange(NetworkInterface.GetAllNetworkInterfaces()
.Select(adapter => adapter.GetIPProperties()
.UnicastAddresses.FirstOrDefault(x => x.Address.AddressFamily == AddressFamily.InterNetwork)
?.Address.ToString())
.Where(address => !string.IsNullOrWhiteSpace(address) && address != "127.0.0.1"));
}
catch (Exception ex)
{
MacroDeckLogger.Warning($"Error while searching for network interfaces\n{ex.Message}");
}

var data = new QuickConnectQrCodeData(Environment.MachineName,
networkInterfaces,
MacroDeck.Configuration.HostPort,
MacroDeck.Configuration.EnableSsl);

var dataJson = JsonSerializer.Serialize(data);
var dataBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(dataJson));
var qrCodeLink = $"https://macro-deck.app/quick-setup/{dataBase64}";

using var qrGenerator = new QRCodeGenerator();
using var qrCodeData = qrGenerator.CreateQrCode(qrCodeLink, QRCodeGenerator.ECCLevel.L);
using var qrCode = new BitmapByteQRCode(qrCodeData);

_quickSetupQrCode = qrCode.GetGraphic(20);
return FromSavedBytes();

Image FromSavedBytes()
{
using var ms = new MemoryStream(_quickSetupQrCode);
return Image.FromStream(ms);
}
}
}
Loading