CollabVMDesktopClient/CollabVMClient/Utilities.cs
2023-08-29 08:52:35 -04:00

59 lines
1.8 KiB
C#

using CollabVMClient.ViewModels;
using CollabVMSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace CollabVMClient
{
public static class Utilities
{
/// <summary>
/// Return view models for each node a cvm server offers
/// </summary>
public static async Task<CVMNodeViewModel[]> GetNodes(string url)
{
var cvm = new CollabVMSharp.CollabVMClient(url);
await cvm.Connect();
var nodes = await cvm.ListVMs();
await cvm.Disconnect();
var nodelist = new List<CVMNodeViewModel>();
foreach (var node in nodes)
{
using var ms = new MemoryStream(node.Thumbnail);
nodelist.Add(new CVMNodeViewModel(url, node.ID, node.Name, new Avalonia.Media.Imaging.Bitmap(ms)));
}
return nodelist.ToArray();
}
public static void WriteDefaultConfig()
{
var cj = JsonSerializer.Serialize(Constants.DefaultConfig);
File.WriteAllText("config.json", cj);
}
public static IConfig LoadConfig()
{
try
{
var cr = File.ReadAllText("config.json");
var c = JsonSerializer.Deserialize<IConfig>(cr);
if (c == null)
{
Utilities.WriteDefaultConfig();
return Constants.DefaultConfig;
}
else return c;
}
catch (FileNotFoundException)
{
Utilities.WriteDefaultConfig();
return Constants.DefaultConfig;
}
}
}
}