Added tray icon support
This commit is contained in:
@@ -0,0 +1,136 @@
|
||||
using System.Text.Json;
|
||||
using PrintServiceTray.Models;
|
||||
|
||||
namespace PrintServiceTray.Services;
|
||||
|
||||
public class ConfigurationService
|
||||
{
|
||||
private readonly string _globalConfigPath;
|
||||
private readonly string _localConfigPath;
|
||||
|
||||
public ConfigurationService()
|
||||
{
|
||||
// Global config: next to CLI executable (C:\LAAPC or wherever installed)
|
||||
var cliPath = Path.Combine(
|
||||
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),
|
||||
"LAAPC"
|
||||
);
|
||||
_globalConfigPath = Path.Combine(cliPath, "printer-config.json");
|
||||
|
||||
// Local config: user's AppData
|
||||
var appDataPath = Path.Combine(
|
||||
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
|
||||
"LAAPC"
|
||||
);
|
||||
Directory.CreateDirectory(appDataPath);
|
||||
_localConfigPath = Path.Combine(appDataPath, "printer-config.json");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Load configuration (local overrides global)
|
||||
/// </summary>
|
||||
public PrinterConfiguration Load()
|
||||
{
|
||||
var config = new PrinterConfiguration();
|
||||
|
||||
// Load global first
|
||||
if (File.Exists(_globalConfigPath))
|
||||
{
|
||||
try
|
||||
{
|
||||
var json = File.ReadAllText(_globalConfigPath);
|
||||
var globalConfig = JsonSerializer.Deserialize<PrinterConfiguration>(json);
|
||||
if (globalConfig != null)
|
||||
{
|
||||
config = globalConfig;
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
// Load local and merge/override
|
||||
if (File.Exists(_localConfigPath))
|
||||
{
|
||||
try
|
||||
{
|
||||
var json = File.ReadAllText(_localConfigPath);
|
||||
var localConfig = JsonSerializer.Deserialize<PrinterConfiguration>(json);
|
||||
if (localConfig != null)
|
||||
{
|
||||
// Merge: local overrides global for matching document types
|
||||
foreach (var kvp in localConfig.DocumentTypes)
|
||||
{
|
||||
config.DocumentTypes[kvp.Key] = kvp.Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Save configuration to global or local
|
||||
/// </summary>
|
||||
public void Save(PrinterConfiguration config, bool saveGlobal)
|
||||
{
|
||||
var path = saveGlobal ? _globalConfigPath : _localConfigPath;
|
||||
|
||||
// Ensure directory exists
|
||||
var directory = Path.GetDirectoryName(path);
|
||||
if (!string.IsNullOrEmpty(directory))
|
||||
{
|
||||
Directory.CreateDirectory(directory);
|
||||
}
|
||||
|
||||
var options = new JsonSerializerOptions
|
||||
{
|
||||
WriteIndented = true
|
||||
};
|
||||
var json = JsonSerializer.Serialize(config, options);
|
||||
File.WriteAllText(path, json);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get all installed printers
|
||||
/// </summary>
|
||||
public List<string> GetInstalledPrinters()
|
||||
{
|
||||
var printers = new List<string>();
|
||||
try
|
||||
{
|
||||
foreach (string printerName in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
|
||||
{
|
||||
printers.Add(printerName);
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
return printers;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get available trays for a printer
|
||||
/// </summary>
|
||||
public List<(int Number, string Name)> GetPrinterTrays(string printerName)
|
||||
{
|
||||
var trays = new List<(int, string)>();
|
||||
try
|
||||
{
|
||||
var printerSettings = new System.Drawing.Printing.PrinterSettings
|
||||
{
|
||||
PrinterName = printerName
|
||||
};
|
||||
|
||||
foreach (System.Drawing.Printing.PaperSource source in printerSettings.PaperSources)
|
||||
{
|
||||
trays.Add((source.RawKind, source.SourceName));
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
return trays;
|
||||
}
|
||||
|
||||
public string GlobalConfigPath => _globalConfigPath;
|
||||
public string LocalConfigPath => _localConfigPath;
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
using System.IO.Pipes;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using PrintServiceTray.Models;
|
||||
|
||||
namespace PrintServiceTray.Services;
|
||||
|
||||
public class ServiceClient
|
||||
{
|
||||
private readonly string _pipeName;
|
||||
private readonly int _timeout;
|
||||
|
||||
public ServiceClient(string pipeName = "PrintServicePipe", int timeoutMs = 5000)
|
||||
{
|
||||
_pipeName = pipeName;
|
||||
_timeout = timeoutMs;
|
||||
}
|
||||
|
||||
public async Task<QueueStatusResponse> GetQueueStatusAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
using var pipe = new NamedPipeClientStream(".", _pipeName, PipeDirection.InOut);
|
||||
await pipe.ConnectAsync(_timeout);
|
||||
|
||||
var request = new QueueStatusRequest();
|
||||
var requestJson = JsonSerializer.Serialize(request);
|
||||
var requestBytes = Encoding.UTF8.GetBytes(requestJson);
|
||||
|
||||
await pipe.WriteAsync(requestBytes);
|
||||
await pipe.FlushAsync();
|
||||
|
||||
var buffer = new byte[4096];
|
||||
var bytesRead = await pipe.ReadAsync(buffer);
|
||||
var responseJson = Encoding.UTF8.GetString(buffer, 0, bytesRead);
|
||||
|
||||
var response = JsonSerializer.Deserialize<QueueStatusResponse>(responseJson);
|
||||
return response ?? new QueueStatusResponse { Success = false, Message = "Invalid response" };
|
||||
}
|
||||
catch (TimeoutException)
|
||||
{
|
||||
return new QueueStatusResponse { Success = false, Message = "Service not responding" };
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return new QueueStatusResponse { Success = false, Message = $"Error: {ex.Message}" };
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> IsServiceRunningAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = await GetQueueStatusAsync();
|
||||
return response.Success;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user