diff --git a/Form1.Designer.cs b/Form1.Designer.cs index ac1c829..7b898f5 100644 --- a/Form1.Designer.cs +++ b/Form1.Designer.cs @@ -34,7 +34,7 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(463, 310); + this.ClientSize = new System.Drawing.Size(784, 561); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); diff --git a/Form1.cs b/Form1.cs index bacc5e1..19af638 100644 --- a/Form1.cs +++ b/Form1.cs @@ -3,6 +3,7 @@ using System.Windows.Forms; using Microsoft.Web.WebView2.WinForms; using Microsoft.Owin.Hosting; using VNCServerList.Web; +using Microsoft.Web.WebView2.Core; namespace VNCServerList { @@ -28,6 +29,10 @@ namespace VNCServerList try { await webView.EnsureCoreWebView2Async(null); + + // JavaScript에서 C# 메서드를 호출할 수 있도록 설정 + webView.CoreWebView2.WebMessageReceived += CoreWebView2_WebMessageReceived; + webView.CoreWebView2.Navigate("http://localhost:8080"); } catch (Exception ex) @@ -36,6 +41,45 @@ namespace VNCServerList } } + private async void CoreWebView2_WebMessageReceived(object sender, CoreWebView2WebMessageReceivedEventArgs e) + { + try + { + var message = e.TryGetWebMessageAsString(); + + if (message == "OPEN_FILE_DIALOG") + { + var filePath = ShowOpenFileDialog(); + if (!string.IsNullOrEmpty(filePath)) + { + // JavaScript로 결과 전송 (ExecuteScriptAsync 사용) + var script = $"window.receiveFilePath('{filePath.Replace("\\", "\\\\")}');"; + await webView.CoreWebView2.ExecuteScriptAsync(script); + } + } + } + catch (Exception ex) + { + System.Diagnostics.Debug.WriteLine($"WebView2 메시지 처리 오류: {ex.Message}"); + } + } + + private string ShowOpenFileDialog() + { + using (var openFileDialog = new OpenFileDialog()) + { + openFileDialog.Filter = "실행 파일 (*.exe)|*.exe|모든 파일 (*.*)|*.*"; + openFileDialog.Title = "VNC Viewer 실행 파일 선택"; + openFileDialog.InitialDirectory = @"C:\Program Files"; + + if (openFileDialog.ShowDialog() == DialogResult.OK) + { + return openFileDialog.FileName; + } + } + return null; + } + private void StartWebServer() { try diff --git a/Models/AppSettings.cs b/Models/AppSettings.cs new file mode 100644 index 0000000..206bb4b --- /dev/null +++ b/Models/AppSettings.cs @@ -0,0 +1,10 @@ +using System; + +namespace VNCServerList.Models +{ + public class AppSettings + { + public string VNCViewerPath { get; set; } = @"C:\Program Files\TightVNC\tvnviewer.exe"; + public int WebServerPort { get; set; } = 8080; + } +} \ No newline at end of file diff --git a/Services/DatabaseService.cs b/Services/DatabaseService.cs index fd8e64e..05bcbfa 100644 --- a/Services/DatabaseService.cs +++ b/Services/DatabaseService.cs @@ -10,9 +10,11 @@ namespace VNCServerList.Services public class DatabaseService { private readonly string _connectionString; + private readonly SettingsService _settingsService; - public DatabaseService() + public DatabaseService(SettingsService settingsService = null) { + _settingsService = settingsService ?? new SettingsService(); _connectionString = Properties.Settings.Default.VNCServerDB; InitializeDatabase(); } diff --git a/Services/SettingsService.cs b/Services/SettingsService.cs new file mode 100644 index 0000000..636f18f --- /dev/null +++ b/Services/SettingsService.cs @@ -0,0 +1,88 @@ +using System; +using System.IO; +using Newtonsoft.Json; +using VNCServerList.Models; + +namespace VNCServerList.Services +{ + public class SettingsService + { + private readonly string _settingsFilePath; + private AppSettings _settings; + + public SettingsService() + { + _settingsFilePath = Path.Combine( + AppDomain.CurrentDomain.BaseDirectory, + "VNCServerList", + "settings.json" + ); + LoadSettings(); + } + + public AppSettings GetSettings() + { + return _settings; + } + + public void SaveSettings(AppSettings settings) + { + _settings = settings; + + // 디버깅용 로그 + System.Diagnostics.Debug.WriteLine($"설정 저장 시작: 파일 경로={_settingsFilePath}"); + System.Diagnostics.Debug.WriteLine($"저장할 설정: VNCViewerPath={settings.VNCViewerPath}, WebServerPort={settings.WebServerPort}"); + + // 디렉토리가 없으면 생성 + var directory = Path.GetDirectoryName(_settingsFilePath); + if (!Directory.Exists(directory)) + { + Directory.CreateDirectory(directory); + System.Diagnostics.Debug.WriteLine($"디렉토리 생성: {directory}"); + } + + // JSON으로 직렬화하여 저장 + var json = JsonConvert.SerializeObject(settings, Formatting.Indented); + System.Diagnostics.Debug.WriteLine($"JSON 데이터: {json}"); + + File.WriteAllText(_settingsFilePath, json); + System.Diagnostics.Debug.WriteLine("설정 파일 저장 완료"); + } + + private void LoadSettings() + { + try + { + System.Diagnostics.Debug.WriteLine($"설정 로드 시작: 파일 경로={_settingsFilePath}"); + + if (File.Exists(_settingsFilePath)) + { + var json = File.ReadAllText(_settingsFilePath); + System.Diagnostics.Debug.WriteLine($"읽은 JSON: {json}"); + _settings = JsonConvert.DeserializeObject(json); + System.Diagnostics.Debug.WriteLine($"로드된 설정: VNCViewerPath={_settings.VNCViewerPath}, WebServerPort={_settings.WebServerPort}"); + } + else + { + System.Diagnostics.Debug.WriteLine("설정 파일이 없어서 기본값으로 초기화"); + _settings = new AppSettings(); + SaveSettings(_settings); + } + } + catch (Exception ex) + { + // 설정 파일 로드 실패 시 기본값 사용 + System.Diagnostics.Debug.WriteLine($"설정 로드 오류: {ex.Message}"); + _settings = new AppSettings(); + } + } + + public void UpdateVNCViewerPath(string path) + { + _settings.VNCViewerPath = path; + SaveSettings(_settings); + } + + + } +} \ No newline at end of file diff --git a/Services/VNCService.cs b/Services/VNCService.cs index 3fbfc4a..530969a 100644 --- a/Services/VNCService.cs +++ b/Services/VNCService.cs @@ -7,12 +7,12 @@ namespace VNCServerList.Services { public class VNCService { - private readonly string _vncViewerPath; + private readonly SettingsService _settingsService; private readonly DatabaseService _databaseService; - public VNCService(DatabaseService databaseService) + public VNCService(DatabaseService databaseService, SettingsService settingsService = null) { - _vncViewerPath = @"C:\Program Files\TightVNC\tvnviewer.exe"; + _settingsService = settingsService ?? new SettingsService(); _databaseService = databaseService; } @@ -20,15 +20,17 @@ namespace VNCServerList.Services { try { - if (!File.Exists(_vncViewerPath)) + var vncViewerPath = _settingsService.GetSettings().VNCViewerPath; + + if (!File.Exists(vncViewerPath)) { - throw new FileNotFoundException($"VNC Viewer를 찾을 수 없습니다: {_vncViewerPath}"); + throw new FileNotFoundException($"VNC Viewer를 찾을 수 없습니다: {vncViewerPath}"); } // VNC Viewer 실행 var startInfo = new ProcessStartInfo { - FileName = _vncViewerPath, + FileName = vncViewerPath, Arguments = $"-host={server.IP} {server.Argument}", UseShellExecute = true }; @@ -58,12 +60,21 @@ namespace VNCServerList.Services public bool IsVNCViewerInstalled() { - return File.Exists(_vncViewerPath); + var vncViewerPath = _settingsService.GetSettings().VNCViewerPath; + System.Diagnostics.Debug.WriteLine($"VNC 설치 확인: 경로='{vncViewerPath}', 존재={File.Exists(vncViewerPath)}"); + return File.Exists(vncViewerPath); } public string GetVNCViewerPath() { - return _vncViewerPath; + var path = _settingsService.GetSettings().VNCViewerPath; + System.Diagnostics.Debug.WriteLine($"VNC 경로 조회: '{path}'"); + return path; + } + + public void SetVNCViewerPath(string path) + { + _settingsService.UpdateVNCViewerPath(path); } } } \ No newline at end of file diff --git a/VNCServerList.csproj b/VNCServerList.csproj index 4e5db45..27fdcb7 100644 --- a/VNCServerList.csproj +++ b/VNCServerList.csproj @@ -67,8 +67,10 @@ + + diff --git a/Web/Controllers/VNCServerController.cs b/Web/Controllers/VNCServerController.cs index 4e9cfed..691a380 100644 --- a/Web/Controllers/VNCServerController.cs +++ b/Web/Controllers/VNCServerController.cs @@ -11,11 +11,13 @@ namespace VNCServerList.Web.Controllers { private readonly DatabaseService _databaseService; private readonly VNCService _vncService; + private readonly SettingsService _settingsService; public VNCServerController() { + _settingsService = new SettingsService(); _databaseService = new DatabaseService(); - _vncService = new VNCService(_databaseService); + _vncService = new VNCService(_databaseService, _settingsService); } [HttpGet] @@ -169,5 +171,86 @@ namespace VNCServerList.Web.Controllers return InternalServerError(ex); } } + + [HttpPost] + [Route("set-vnc-path")] + public IHttpActionResult SetVNCPath([FromBody] dynamic data) + { + try + { + string path = data.path; + if (string.IsNullOrEmpty(path)) + { + return BadRequest("VNC Viewer 경로가 제공되지 않았습니다."); + } + + _vncService.SetVNCViewerPath(path); + return Ok(new { Message = "VNC Viewer 경로가 설정되었습니다." }); + } + catch (Exception ex) + { + return InternalServerError(ex); + } + } + + [HttpGet] + [Route("settings")] + public IHttpActionResult GetSettings() + { + try + { + System.Diagnostics.Debug.WriteLine("=== 설정 조회 시작 ==="); + + var settings = _settingsService.GetSettings(); + + // 디버깅용 로그 + System.Diagnostics.Debug.WriteLine($"조회된 설정: VNCViewerPath='{settings.VNCViewerPath}', WebServerPort={settings.WebServerPort}"); + System.Diagnostics.Debug.WriteLine("=== 설정 조회 완료 ==="); + + return Ok(settings); + } + catch (Exception ex) + { + System.Diagnostics.Debug.WriteLine($"설정 조회 오류: {ex.Message}"); + System.Diagnostics.Debug.WriteLine($"스택 트레이스: {ex.StackTrace}"); + return InternalServerError(ex); + } + } + + [HttpPost] + [Route("settings")] + public IHttpActionResult UpdateSettings([FromBody] AppSettings settings) + { + try + { + System.Diagnostics.Debug.WriteLine("=== 설정 저장 요청 시작 ==="); + + if (settings == null) + { + System.Diagnostics.Debug.WriteLine("설정 객체가 null입니다."); + return BadRequest("설정 정보가 없습니다."); + } + + // 디버깅용 로그 + System.Diagnostics.Debug.WriteLine($"받은 설정 데이터: VNCViewerPath='{settings.VNCViewerPath}', WebServerPort={settings.WebServerPort}"); + System.Diagnostics.Debug.WriteLine($"VNCViewerPath 타입: {settings.VNCViewerPath?.GetType()}"); + System.Diagnostics.Debug.WriteLine($"WebServerPort 타입: {settings.WebServerPort.GetType()}"); + + _settingsService.SaveSettings(settings); + + // 저장된 설정 확인 + var savedSettings = _settingsService.GetSettings(); + System.Diagnostics.Debug.WriteLine($"저장된 설정: VNCViewerPath='{savedSettings.VNCViewerPath}', WebServerPort={savedSettings.WebServerPort}"); + System.Diagnostics.Debug.WriteLine("=== 설정 저장 완료 ==="); + + return Ok(new { Message = "설정이 저장되었습니다." }); + } + catch (Exception ex) + { + System.Diagnostics.Debug.WriteLine($"설정 저장 오류: {ex.Message}"); + System.Diagnostics.Debug.WriteLine($"스택 트레이스: {ex.StackTrace}"); + return InternalServerError(ex); + } + } } } \ No newline at end of file diff --git a/Web/wwwroot/index.html b/Web/wwwroot/index.html index 2b352f2..420e25c 100644 --- a/Web/wwwroot/index.html +++ b/Web/wwwroot/index.html @@ -32,14 +32,21 @@
- -
+ +
+
@@ -94,6 +101,36 @@
+ + +