412 lines
16 KiB
C#
412 lines
16 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace BokBonCheck
|
|
{
|
|
public partial class Form1 : Form
|
|
{
|
|
private readonly BookSearchService _searchService;
|
|
private bool _isSearching = false;
|
|
private bool _isDriverReady = false;
|
|
|
|
public Form1()
|
|
{
|
|
InitializeComponent();
|
|
_searchService = new BookSearchService();
|
|
InitializeUI();
|
|
InitializeChromeDriver();
|
|
}
|
|
|
|
private async void InitializeChromeDriver()
|
|
{
|
|
var lblStatus = (Label)this.Controls["lblStatus"];
|
|
lblStatus.Text = "Chrome 드라이버 확인 중...";
|
|
lblStatus.ForeColor = Color.Blue;
|
|
|
|
try
|
|
{
|
|
// Chrome 설치 확인
|
|
if (!ChromeDriverManager.IsChromeInstalled())
|
|
{
|
|
MessageBox.Show("Google Chrome이 설치되어 있지 않습니다. Chrome을 설치한 후 프로그램을 다시 실행해주세요.",
|
|
"Chrome 필요", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
lblStatus.Text = "Chrome이 설치되지 않음";
|
|
lblStatus.ForeColor = Color.Red;
|
|
return;
|
|
}
|
|
|
|
// 기존 드라이버가 준비되어 있는지 확인
|
|
if (ChromeDriverManager.IsDriverReady())
|
|
{
|
|
lblStatus.Text = "Chrome 드라이버 준비 완료 - 검색할 준비가 되었습니다.";
|
|
lblStatus.ForeColor = Color.Green;
|
|
|
|
var btnSearch = (Button)this.Controls["btnSearch"];
|
|
btnSearch.Enabled = true;
|
|
_isDriverReady = true;
|
|
return;
|
|
}
|
|
|
|
// 드라이버가 없거나 작동하지 않으면 다운로드 진행 창 표시
|
|
using (var progressForm = new DownloadProgressForm())
|
|
{
|
|
progressForm.Show();
|
|
|
|
try
|
|
{
|
|
// ChromeDriver 설정
|
|
await ChromeDriverManager.SetupChromeDriverAsync(progressForm);
|
|
|
|
// 드라이버 테스트
|
|
var isWorking = await ChromeDriverManager.TestChromeDriverAsync(progressForm);
|
|
|
|
if (isWorking)
|
|
{
|
|
_isDriverReady = true;
|
|
lblStatus.Text = "Chrome 드라이버 준비 완료 - 검색할 준비가 되었습니다.";
|
|
lblStatus.ForeColor = Color.Green;
|
|
|
|
var btnSearch = (Button)this.Controls["btnSearch"];
|
|
btnSearch.Enabled = true;
|
|
}
|
|
else
|
|
{
|
|
lblStatus.Text = "Chrome 드라이버 테스트 실패";
|
|
lblStatus.ForeColor = Color.Red;
|
|
MessageBox.Show("Chrome 드라이버 테스트에 실패했습니다. 인터넷 연결을 확인하고 프로그램을 다시 실행해주세요.",
|
|
"드라이버 오류", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
catch (OperationCanceledException)
|
|
{
|
|
lblStatus.Text = "드라이버 다운로드가 취소되었습니다.";
|
|
lblStatus.ForeColor = Color.Orange;
|
|
return;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
lblStatus.Text = "Chrome 드라이버 설정 실패";
|
|
lblStatus.ForeColor = Color.Red;
|
|
MessageBox.Show($"Chrome 드라이버 설정 중 오류가 발생했습니다: {ex.Message}",
|
|
"설정 오류", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
lblStatus.Text = "Chrome 드라이버 설정 실패";
|
|
lblStatus.ForeColor = Color.Red;
|
|
MessageBox.Show($"Chrome 드라이버 설정 중 오류가 발생했습니다: {ex.Message}",
|
|
"설정 오류", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
|
|
private void InitializeUI()
|
|
{
|
|
// 폼 설정
|
|
this.Text = "도서 검색 프로그램";
|
|
this.Size = new Size(820, 600);
|
|
this.StartPosition = FormStartPosition.CenterScreen;
|
|
|
|
// 검색 입력 영역
|
|
var lblSearch = new Label
|
|
{
|
|
Text = "도서명을 입력하세요:",
|
|
Location = new Point(20, 20),
|
|
Size = new Size(150, 25),
|
|
Font = new Font("맑은 고딕", 10)
|
|
};
|
|
|
|
var txtSearch = new TextBox
|
|
{
|
|
Name = "txtSearch",
|
|
Location = new Point(20, 50),
|
|
Size = new Size(300, 25),
|
|
Font = new Font("맑은 고딕", 10)
|
|
};
|
|
|
|
var btnSearch = new Button
|
|
{
|
|
Name = "btnSearch",
|
|
Text = "검색",
|
|
Location = new Point(340, 50),
|
|
Size = new Size(80, 25),
|
|
Font = new Font("맑은 고딕", 10),
|
|
BackColor = Color.LightBlue,
|
|
Enabled = false // 초기에는 비활성화
|
|
};
|
|
|
|
var btnClear = new Button
|
|
{
|
|
Name = "btnClear",
|
|
Text = "초기화",
|
|
Location = new Point(430, 50),
|
|
Size = new Size(80, 25),
|
|
Font = new Font("맑은 고딕", 10),
|
|
BackColor = Color.LightGray
|
|
};
|
|
|
|
var btnResetDriver = new Button
|
|
{
|
|
Name = "btnResetDriver",
|
|
Text = "드라이버 재설정",
|
|
Location = new Point(520, 50),
|
|
Size = new Size(100, 25),
|
|
Font = new Font("맑은 고딕", 9),
|
|
BackColor = Color.LightYellow
|
|
};
|
|
|
|
// 진행 상황 표시
|
|
var progressBar = new ProgressBar
|
|
{
|
|
Name = "progressBar",
|
|
Location = new Point(20, 90),
|
|
Size = new Size(600, 20),
|
|
Visible = false
|
|
};
|
|
|
|
var lblStatus = new Label
|
|
{
|
|
Name = "lblStatus",
|
|
Text = "Chrome 드라이버 설정 중...",
|
|
Location = new Point(20, 120),
|
|
Size = new Size(490, 25),
|
|
Font = new Font("맑은 고딕", 9),
|
|
ForeColor = Color.Blue
|
|
};
|
|
|
|
// 결과 표시 영역
|
|
var lblResults = new Label
|
|
{
|
|
Text = "검색 결과:",
|
|
Location = new Point(20, 160),
|
|
Size = new Size(150, 25),
|
|
Font = new Font("맑은 고딕", 10, FontStyle.Bold)
|
|
};
|
|
|
|
var dataGridView = new DataGridView
|
|
{
|
|
Name = "dataGridView",
|
|
Location = new Point(20, 190),
|
|
Size = new Size(760, 350),
|
|
AllowUserToAddRows = false,
|
|
AllowUserToDeleteRows = false,
|
|
ReadOnly = true,
|
|
SelectionMode = DataGridViewSelectionMode.FullRowSelect,
|
|
AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill,
|
|
BackgroundColor = Color.White
|
|
};
|
|
|
|
// 컨트롤 추가
|
|
this.Controls.AddRange(new Control[]
|
|
{
|
|
lblSearch, txtSearch, btnSearch, btnClear, btnResetDriver,
|
|
progressBar, lblStatus, lblResults, dataGridView
|
|
});
|
|
|
|
// 이벤트 핸들러 연결
|
|
btnSearch.Click += BtnSearch_Click;
|
|
btnClear.Click += BtnClear_Click;
|
|
btnResetDriver.Click += BtnResetDriver_Click;
|
|
txtSearch.KeyPress += TxtSearch_KeyPress;
|
|
|
|
// DataGridView 설정
|
|
SetupDataGridView();
|
|
}
|
|
|
|
private void SetupDataGridView()
|
|
{
|
|
var dataGridView = (DataGridView)this.Controls["dataGridView"];
|
|
|
|
dataGridView.Columns.Clear();
|
|
dataGridView.Columns.Add("SiteName", "도서관");
|
|
dataGridView.Columns.Add("BookCount", "도서 수");
|
|
dataGridView.Columns.Add("SearchTime", "검색 시간");
|
|
dataGridView.Columns.Add("Status", "상태");
|
|
|
|
// 컬럼 너비 설정
|
|
dataGridView.Columns["SiteName"].Width = 150;
|
|
dataGridView.Columns["BookCount"].Width = 100;
|
|
dataGridView.Columns["SearchTime"].Width = 150;
|
|
dataGridView.Columns["Status"].Width = 100;
|
|
}
|
|
|
|
private async void BtnSearch_Click(object sender, EventArgs e)
|
|
{
|
|
if (_isSearching || !_isDriverReady) return;
|
|
|
|
var txtSearch = (TextBox)this.Controls["txtSearch"];
|
|
var btnSearch = (Button)this.Controls["btnSearch"];
|
|
var progressBar = (ProgressBar)this.Controls["progressBar"];
|
|
var lblStatus = (Label)this.Controls["lblStatus"];
|
|
var dataGridView = (DataGridView)this.Controls["dataGridView"];
|
|
|
|
if (string.IsNullOrWhiteSpace(txtSearch.Text))
|
|
{
|
|
MessageBox.Show("도서명을 입력해주세요.", "알림", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
return;
|
|
}
|
|
|
|
_isSearching = true;
|
|
btnSearch.Enabled = false;
|
|
progressBar.Visible = true;
|
|
progressBar.Style = ProgressBarStyle.Marquee;
|
|
lblStatus.Text = "검색 중...";
|
|
lblStatus.ForeColor = Color.Blue;
|
|
|
|
try
|
|
{
|
|
// 기존 결과 초기화
|
|
dataGridView.Rows.Clear();
|
|
|
|
// 검색 실행
|
|
var results = await _searchService.SearchBooksAsync(txtSearch.Text.Trim());
|
|
|
|
// 결과 표시
|
|
foreach (var result in results)
|
|
{
|
|
var row = new object[]
|
|
{
|
|
result.SiteName,
|
|
result.IsSuccess ? result.BookCount.ToString() : "오류",
|
|
result.SearchTime.ToString("yyyy-MM-dd HH:mm:ss"),
|
|
result.IsSuccess ? "성공" : "실패"
|
|
};
|
|
|
|
dataGridView.Rows.Add(row);
|
|
|
|
// 실패한 경우 오류 메시지 표시
|
|
if (!result.IsSuccess)
|
|
{
|
|
MessageBox.Show($"{result.SiteName}: {result.ErrorMessage}", "검색 오류", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
|
|
lblStatus.Text = $"검색 완료 - {results.Count}개 사이트 검색됨";
|
|
lblStatus.ForeColor = Color.Green;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
lblStatus.Text = "검색 중 오류가 발생했습니다.";
|
|
lblStatus.ForeColor = Color.Red;
|
|
MessageBox.Show($"검색 중 오류가 발생했습니다: {ex.Message}", "오류", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
finally
|
|
{
|
|
_isSearching = false;
|
|
btnSearch.Enabled = _isDriverReady;
|
|
progressBar.Visible = false;
|
|
}
|
|
}
|
|
|
|
private void BtnClear_Click(object sender, EventArgs e)
|
|
{
|
|
var txtSearch = (TextBox)this.Controls["txtSearch"];
|
|
var dataGridView = (DataGridView)this.Controls["dataGridView"];
|
|
var lblStatus = (Label)this.Controls["lblStatus"];
|
|
|
|
txtSearch.Clear();
|
|
dataGridView.Rows.Clear();
|
|
|
|
if (_isDriverReady)
|
|
{
|
|
lblStatus.Text = "검색할 준비가 되었습니다.";
|
|
lblStatus.ForeColor = Color.Green;
|
|
}
|
|
else
|
|
{
|
|
lblStatus.Text = "Chrome 드라이버 설정 실패";
|
|
lblStatus.ForeColor = Color.Red;
|
|
}
|
|
}
|
|
|
|
private async void BtnResetDriver_Click(object sender, EventArgs e)
|
|
{
|
|
var btnResetDriver = (Button)sender;
|
|
var lblStatus = (Label)this.Controls["lblStatus"];
|
|
|
|
btnResetDriver.Enabled = false;
|
|
lblStatus.Text = "드라이버 재설정 중...";
|
|
lblStatus.ForeColor = Color.Blue;
|
|
|
|
try
|
|
{
|
|
// 강제로 캐시 정리 (재설정이므로)
|
|
ChromeDriverManager.ClearDriverCache();
|
|
|
|
// 다운로드 진행 창 표시
|
|
using (var progressForm = new DownloadProgressForm())
|
|
{
|
|
progressForm.Text = "Chrome 드라이버 재설정";
|
|
progressForm.Show();
|
|
|
|
try
|
|
{
|
|
// 드라이버 재설정
|
|
await ChromeDriverManager.SetupChromeDriverAsync(progressForm);
|
|
|
|
// 테스트
|
|
var isWorking = await ChromeDriverManager.TestChromeDriverAsync(progressForm);
|
|
|
|
if (isWorking)
|
|
{
|
|
_isDriverReady = true;
|
|
lblStatus.Text = "드라이버 재설정 완료 - 검색할 준비가 되었습니다.";
|
|
lblStatus.ForeColor = Color.Green;
|
|
|
|
var btnSearch = (Button)this.Controls["btnSearch"];
|
|
btnSearch.Enabled = true;
|
|
}
|
|
else
|
|
{
|
|
lblStatus.Text = "드라이버 재설정 실패";
|
|
lblStatus.ForeColor = Color.Red;
|
|
MessageBox.Show("드라이버 재설정에 실패했습니다.", "재설정 실패", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
catch (OperationCanceledException)
|
|
{
|
|
lblStatus.Text = "드라이버 재설정이 취소되었습니다.";
|
|
lblStatus.ForeColor = Color.Orange;
|
|
return;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
lblStatus.Text = "드라이버 재설정 중 오류 발생";
|
|
lblStatus.ForeColor = Color.Red;
|
|
MessageBox.Show($"드라이버 재설정 중 오류가 발생했습니다: {ex.Message}", "오류", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
lblStatus.Text = "드라이버 재설정 중 오류 발생";
|
|
lblStatus.ForeColor = Color.Red;
|
|
MessageBox.Show($"드라이버 재설정 중 오류가 발생했습니다: {ex.Message}", "오류", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
finally
|
|
{
|
|
btnResetDriver.Enabled = true;
|
|
}
|
|
}
|
|
|
|
private void TxtSearch_KeyPress(object sender, KeyPressEventArgs e)
|
|
{
|
|
if (e.KeyChar == (char)Keys.Enter)
|
|
{
|
|
e.Handled = true;
|
|
BtnSearch_Click(sender, e);
|
|
}
|
|
}
|
|
}
|
|
}
|