using System; using System.Diagnostics; using System.IO; using VNCServerList.Models; namespace VNCServerList.Services { public class VNCService { private readonly SettingsService _settingsService; private readonly DatabaseService _databaseService; public VNCService(DatabaseService databaseService, SettingsService settingsService = null) { _settingsService = settingsService ?? new SettingsService(); _databaseService = databaseService; } public bool ConnectToServer(VNCServer server) { try { var settings = _settingsService.GetSettings(); var vncViewerPath = settings.VNCViewerPath; if (!File.Exists(vncViewerPath)) { throw new FileNotFoundException($"VNC Viewer를 찾을 수 없습니다: {vncViewerPath}"); } // 서버의 argument가 없으면 설정의 기본 argument 사용 var arguments = $"-host={server.IP}"; if (!string.IsNullOrEmpty(server.Argument)) { arguments += $" {server.Argument}"; } else if (!string.IsNullOrEmpty(settings.Argument)) { arguments += $" {settings.Argument}"; } // 비밀번호가 있으면 추가 if (!string.IsNullOrEmpty(server.Password)) { arguments += $" -password={server.Password}"; } System.Diagnostics.Debug.WriteLine($"VNC 실행: 경로='{vncViewerPath}', 인수='{arguments}'"); // VNC Viewer 실행 var startInfo = new ProcessStartInfo { FileName = vncViewerPath, Arguments = arguments, UseShellExecute = true }; Process.Start(startInfo); return true; } catch (Exception ex) { throw new Exception($"VNC 연결 중 오류가 발생했습니다: {ex.Message}", ex); } } public bool ConnectToServer(string user, string ip) { var server = _databaseService.GetServerByUserAndIP(user, ip); if (server == null) { throw new ArgumentException($"서버 {user}@{ip}를 찾을 수 없습니다."); } return ConnectToServer(server); } public bool IsVNCViewerInstalled() { var vncViewerPath = _settingsService.GetSettings().VNCViewerPath; System.Diagnostics.Debug.WriteLine($"VNC 설치 확인: 경로='{vncViewerPath}', 존재={File.Exists(vncViewerPath)}"); return File.Exists(vncViewerPath); } public string GetVNCViewerPath() { var path = _settingsService.GetSettings().VNCViewerPath; System.Diagnostics.Debug.WriteLine($"VNC 경로 조회: '{path}'"); return path; } public void SetVNCViewerPath(string path) { _settingsService.UpdateVNCViewerPath(path); } } }