/*============================================================== * 取消激活窗口时 *==============================================================*/ private void frm_Mainform_Deactivate(object sender, EventArgs e) { for (int i = 1; i < (int)COMMON.e_HotKeyID.MAX; ++i) { COMMON.UnregisterHotKey(this.Handle, i); } }
/*============================================================== * 异步下载进度 *==============================================================*/ private void Wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) { if (m_is_stopping) { WebClient wc = (WebClient)e.UserState; wc.CancelAsync(); return; } label_Log.Text = $"{e.BytesReceived}/{e.TotalBytesToReceive}({e.ProgressPercentage}%)"; COMMON.SetProgressValue(e.ProgressPercentage * 100); }
/*============================================================== * 激活窗口时 *==============================================================*/ private void frm_Mainform_Activated(object sender, EventArgs e) { // Alt + Enter COMMON.RegHotKey(this.Handle, (int)COMMON.e_HotKeyID.OpenDir, COMMON.e_KeyModifiers.Alt, Keys.Enter); // F1 COMMON.RegHotKey(this.Handle, (int)COMMON.e_HotKeyID.OpenURL, COMMON.e_KeyModifiers.None, Keys.F1); // F3 COMMON.RegHotKey(this.Handle, (int)COMMON.e_HotKeyID.Search, COMMON.e_KeyModifiers.None, Keys.F3); // F5 COMMON.RegHotKey(this.Handle, (int)COMMON.e_HotKeyID.UpdateDB, COMMON.e_KeyModifiers.None, Keys.F5); }
/*============================================================== * 执行更新数据库的线程 *==============================================================*/ void update_db_thread() { if (!Directory.Exists(CONFIG.m_s_subs_path)) { FORMS.invoke(() => { append_log($"{LANGUAGES.txt(56)} {CONFIG.m_s_subs_path}", // 找不到本地字幕路径 Color.Red); lock_controls(true); }); m_is_updating_database = false; return; } m_sw.Restart(); long can_refresh_UI_tick = 0; DATA.m_s_lock.EnterWriteLock(); DATA.m_s_all_subs.Clear(); SortedSet <string> source_list = new SortedSet <string>(); FORMS.invoke(() => { frm_Mainform.m_s_mainform.m_Search.comboBox_Type.Items.Clear(); frm_Mainform.m_s_mainform.m_Search.comboBox_Type.Items.Add(""); frm_Mainform.m_s_mainform.m_Search.comboBox_Source.Items.Clear(); COMMON.SetProgressValue(0); }); string[] dirs_type = Directory.GetDirectories(CONFIG.m_s_subs_path, "*.*", SearchOption.TopDirectoryOnly); foreach (string dir_type in dirs_type) { FORMS.invoke(() => { frm_Mainform.m_s_mainform.m_Search.comboBox_Type.Items.Add(Path.GetFileName(dir_type)); }); string[] dirs_year = Directory.GetDirectories(dir_type, "*.*", SearchOption.TopDirectoryOnly); for (int i_year = 0; i_year < dirs_year.Length; ++i_year) { string dir_year = dirs_year[i_year]; string[] dirs_video = Directory.GetDirectories(dir_year, "*.*", SearchOption.TopDirectoryOnly); for (int i_video = 0; i_video < dirs_video.Length; ++i_video) { string dir_video = dirs_video[i_video]; // 路径 string path = dir_video.Substring(CONFIG.m_s_subs_path.Length + 1).Replace("\\", "/"); FORMS.invoke(() => { label_Log.Text = dir_video; }); // 日期 string name_tmp = Path.GetFileName(dir_video); int idx = name_tmp.IndexOf(")"); string[] date_vals = name_tmp.Substring(1, idx - 1).Split('.'); int year = int.Parse(date_vals[0]); int month = (date_vals.Length >= 2) ? int.Parse(date_vals[1]) : 1; int day = (date_vals.Length >= 3) ? int.Parse(date_vals[2]) : 1; DateTime date = new DateTime(year, month, day); // 番名 //string name_chs = name_tmp.Substring(idx + 1); string name_chs = ""; string name_cht = ""; string name_jp = ""; string name_en = ""; string name_rome = ""; // 读取 info.txt string info_filename = Path.Combine(dir_video, "info.txt"); if (File.Exists(info_filename)) { string[] lines = File.ReadAllLines(info_filename, Encoding.UTF8); foreach (string line in lines) { if (line.Length < 2) { continue; } if (line[0] == '/' && line[1] == '/') { continue; } idx = line.IndexOf(":"); if (idx < 0) { continue; } string w1 = line.Substring(0, idx).Trim().ToLower(); string w2 = line.Substring(idx + 1).Trim(); if (w1 == "chs") { name_chs = w2.Trim(); continue; } if (w1 == "cht") { name_cht = w2.Trim(); continue; } if (w1 == "jp") { name_jp = w2.Trim(); continue; } if (w1 == "en") { name_en = w2.Trim(); continue; } if (w1 == "rome") { name_rome = w2.Trim(); continue; } } // for } string[] dirs_source = Directory.GetDirectories(dir_video, "*.*", SearchOption.TopDirectoryOnly); foreach (string dir_source in dirs_source) { source_list.Add(Path.GetFileName(dir_source)); string[] dirs_sub_name = Directory.GetDirectories(dir_source, "*.*", SearchOption.TopDirectoryOnly); foreach (string dir_sub_name in dirs_sub_name) { if (m_is_stopping) { return; } string providers = ""; // 字幕组/提供者 string desc = ""; // 字幕说明 // 后缀名 SortedSet <string> extension_list = new SortedSet <string>(); string[] files = Directory.GetFiles(dir_sub_name, "*.*", SearchOption.TopDirectoryOnly); foreach (string file in files) { string extension = Path.GetExtension(file).ToLower(); extension_list.Add(extension); } // for files // xxx.txt string desc_filename = dir_sub_name + ".txt"; if (File.Exists(desc_filename)) { string file_txt = File.ReadAllText(desc_filename, Encoding.UTF8); idx = file_txt.IndexOf("desc:", StringComparison.CurrentCultureIgnoreCase); if (idx > 0) { desc = file_txt.Substring(idx + 5).Trim(); } string[] lines = File.ReadAllLines(desc_filename, Encoding.UTF8); foreach (string line in lines) { if (line.Length < 2) { continue; } if (line[0] == '/' && line[1] == '/') { continue; } idx = line.IndexOf(":"); if (idx < 0) { continue; } string w1 = line.Substring(0, idx).Trim().ToLower(); string w2 = line.Substring(idx + 1).Trim(); if (w1 == "providers") { providers = w2.Trim(); break; } } // for } DATA.c_SubInfo sub_info = new DATA.c_SubInfo(); DATA.m_s_all_subs.Add(sub_info); sub_info.m_name_chs = name_chs; sub_info.m_name_cht = name_cht; sub_info.m_name_jp = name_jp; sub_info.m_name_en = name_en; sub_info.m_name_rome = name_rome; sub_info.m_time = date; sub_info.m_type = Path.GetFileName(dir_type); sub_info.m_source = Path.GetFileName(dir_source); sub_info.m_sub_name = Path.GetFileName(dir_sub_name); foreach (string extension in extension_list) { if (sub_info.m_extension.Length > 0) { sub_info.m_extension += ";"; } sub_info.m_extension += extension; } sub_info.m_providers = providers; sub_info.m_desc = desc; sub_info.m_path = path; } // for dirs_sub_name } // for dirs_source // 进度条 int progress_value = COMMON.m_k_MAX_PROGRESS_VALUE * i_year / dirs_year.Length + COMMON.m_k_MAX_PROGRESS_VALUE * (i_video + 1) / (dirs_year.Length * dirs_video.Length); COMMON.SetProgressValue(progress_value); } // for dirs_video if (can_refresh_UI_tick <= m_sw.ElapsedMilliseconds) { can_refresh_UI_tick = m_sw.ElapsedMilliseconds + 50; // 50ms 刷新一次界面 Application.DoEvents(); } } // for dirs_year } // for dirs_type FORMS.invoke(() => { foreach (string source in source_list) { frm_Mainform.m_s_mainform.m_Search.comboBox_Source.Items.Add(source); } COMMON.SetProgressValue(0); }); DATA.m_s_lock.ExitWriteLock(); FORMS.invoke(() => { m_sw.Stop(); CONFIG.m_s_last_updata_db_time = DateTime.Now; CONFIG.write_config(); lock_controls(true); DATA.data2dt(); append_log(string.Format(LANGUAGES.txt(61), // 正在把数据写入到文件 {0:s} ... PATH.m_k_DB_FILENAME)); DATA.m_s_dt.WriteXml(PATH.m_k_DB_FILENAME); if (m_is_stopping) { append_log(string.Format(LANGUAGES.txt(57) + LANGUAGES.txt(60), // 用户停止更新数据库(合计 {0:d} 条数据,耗时 {1:s}) DATA.m_s_all_subs.Count, m_sw.Elapsed.ToString()), Color.Blue); } else { append_log(string.Format(LANGUAGES.txt(58) + LANGUAGES.txt(60), // 更新数据库完成(合计 {0:d} 条数据,耗时 {1:s}) DATA.m_s_all_subs.Count, m_sw.Elapsed.ToString()), Color.Green); } frm_Mainform.m_s_mainform.m_DataGridView_event_enable = false; frm_Mainform.m_s_mainform.dataGridView_Main.DataSource = DATA.m_s_dt; frm_Mainform.m_s_mainform.update_columns_style(); frm_Mainform.m_s_mainform.m_DataGridView_event_enable = true; frm_Mainform.m_s_mainform.update_status(); m_is_stopping = false; }); m_is_updating_database = false; }