private void AttachWatcherToUI(ThreadWatcher watcher) { if (watcher.Tag != null) { throw new Exception("Watcher's tag is already set."); } watcher.FoundNewImage += ThreadWatcher_FoundNewImage; watcher.DownloadStatus += ThreadWatcher_DownloadStatus; watcher.WaitStatus += ThreadWatcher_WaitStatus; watcher.StopStatus += ThreadWatcher_StopStatus; watcher.ThreadDownloadDirectoryRename += ThreadWatcher_ThreadDownloadDirectoryRename; watcher.DownloadStart += ThreadWatcher_DownloadStart; watcher.DownloadProgress += ThreadWatcher_DownloadProgress; watcher.DownloadEnd += ThreadWatcher_DownloadEnd; ListViewItem newListViewItem = new ListViewItem(""); for (int i = 1; i < lvThreads.Columns.Count; i++) { newListViewItem.SubItems.Add(""); } newListViewItem.Tag = watcher; lvThreads.Items.Add(newListViewItem); watcher.Tag = newListViewItem; }
public void PostprocessFiles(ThreadWatcher watcher, ProgressReporter onProgress) { string indexPath = Path.Combine(watcher.ThreadDownloadDirectory, _indexFileName); IndexEntry[] files = ReadIndexFile(indexPath); if (files.Length == 0) { return; } string imageDir = watcher.ImageDownloadDirectory; bool useOriginalName = watcher.UseOriginalFileNames; int fileNameLengthLimit = ThreadWatcher.GetFileNameLengthLimit(imageDir); HashSet <string> processedFileNames = new HashSet <string>(StringComparer.OrdinalIgnoreCase); using (FileStream dst = File.Create(Path.Combine(imageDir, "stream.ts"))) { for (int iFile = 0; iFile < files.Length; iFile++) { onProgress((double)iFile / files.Length); IndexEntry file = files[iFile]; string fileName = ThreadWatcher.GetUniqueFileName(ImageInfo.GetEffectiveFileName( file.FileName, file.OriginalFileName, useOriginalName, fileNameLengthLimit), processedFileNames); using (FileStream src = File.OpenRead(Path.Combine(imageDir, fileName))) { src.CopyTo(dst); } } } foreach (string fileName in processedFileNames) { string path = Path.Combine(imageDir, fileName); try { File.Delete(path); } catch { } } }
private void ThreadWatcher_FoundNewImage(ThreadWatcher watcher, EventArgs args) { this.BeginInvoke(() => { DisplayLastImageOn(watcher); _saveThreadList = true; }); }
private void SetStopStatus(ThreadWatcher watcher) { string status = "Stopped: "; switch (watcher.StopReason) { case StopReason.UserRequest: status += "User requested"; break; case StopReason.Exiting: status += "Exiting"; break; case StopReason.PageNotFound: status += "Page not found"; break; case StopReason.DownloadComplete: status += "Download complete"; break; case StopReason.IOError: status += "Error writing to disk"; break; default: status += "Unknown error"; break; } DisplayStatus(watcher, status); }
private void SetDownloadStatus(ThreadWatcher watcher, DownloadType downloadType, int completeCount, int totalCount) { string type; bool hideDetail = false; switch (downloadType) { case DownloadType.Page: type = totalCount == 1 ? "page" : "pages"; hideDetail = totalCount == 1; break; case DownloadType.Image: type = "images"; break; case DownloadType.Thumbnail: type = "thumbnails"; break; default: return; } string status = hideDetail ? $"Downloading {type}" : $"Downloading {type}: {completeCount} of {totalCount} completed"; DisplayStatus(watcher, status); }
private void ThreadWatcher_WaitStatus(ThreadWatcher watcher, EventArgs args) { this.BeginInvoke(() => { SetWaitStatus(watcher); SetupWaitTimer(); }); }
private void ThreadWatcher_DownloadStatus(ThreadWatcher watcher, DownloadStatusEventArgs args) { this.BeginInvoke(() => { SetDownloadStatus(watcher, args.DownloadType, args.CompleteCount, args.TotalCount); SetupWaitTimer(); }); }
private void SetSubItemText(ThreadWatcher watcher, ColumnIndex columnIndex, string text) { ListViewItem item = (ListViewItem)watcher.Tag; var subItem = item.SubItems[(int)columnIndex]; if (subItem.Text != text) { subItem.Text = text; } }
private void ThreadWatcher_StopStatus(ThreadWatcher watcher, EventArgs args) { this.BeginInvoke(() => { SetStopStatus(watcher); SetupWaitTimer(); if (watcher.StopReason != StopReason.UserRequest && watcher.StopReason != StopReason.Exiting) { _saveThreadList = true; } }); }
private void ThreadWatcher_DownloadProgress(ThreadWatcher watcher, DownloadProgressEventArgs args) { lock (_downloadProgresses) { DownloadProgressInfo info; if (!_downloadProgresses.TryGetValue(args.DownloadID, out info)) { return; } info.DownloadedSize = args.DownloadedSize; _downloadProgresses[args.DownloadID] = info; } }
private void ThreadWatcher_DownloadStart(ThreadWatcher watcher, DownloadStartEventArgs args) { DownloadProgressInfo info = new DownloadProgressInfo(); info.DownloadID = args.DownloadID; info.Url = args.Url; info.TryNumber = args.TryNumber; info.StartTicks = TickCount.Now; info.TotalSize = args.TotalSize; lock (_downloadProgresses) { _downloadProgresses[args.DownloadID] = info; } }
private void ThreadWatcher_DownloadEnd(ThreadWatcher watcher, DownloadEndEventArgs args) { lock (_downloadProgresses) { DownloadProgressInfo info; if (!_downloadProgresses.TryGetValue(args.DownloadID, out info)) { return; } info.EndTicks = TickCount.Now; info.DownloadedSize = args.DownloadedSize; info.TotalSize = args.DownloadedSize; _downloadProgresses[args.DownloadID] = info; } }
private bool AddThread(string pageUrl, string pageAuth, string imageAuth, int checkIntervalSeconds, bool oneTimeDownload, string description, UrlTransformResult urlTransformResult) { if (urlTransformResult != null) { pageUrl = urlTransformResult.TransformedUrl; } SiteHelper siteHelper = SiteHelper.CreateByUrl(pageUrl); string globalThreadID = siteHelper.GetGlobalThreadID(); ThreadWatcher watcher = ThreadList.Items.FirstOrDefault(w => w.GlobalThreadID.Equals(globalThreadID, StringComparison.OrdinalIgnoreCase)); if (watcher == null) { description ??= urlTransformResult?.DefaultDescription ?? siteHelper.GetDefaultDescription(); watcher = ThreadWatcher.Create(siteHelper, pageUrl, pageAuth, imageAuth, oneTimeDownload, checkIntervalSeconds, description); AttachWatcherToUI(watcher); DisplayDescription(watcher); DisplayAddedOn(watcher); ThreadList.Add(watcher); } else { if (watcher.IsRunning) { return(false); } watcher.PageAuth = pageAuth; watcher.ImageAuth = imageAuth; watcher.CheckIntervalSeconds = checkIntervalSeconds; watcher.OneTimeDownload = oneTimeDownload; if (description != null) { watcher.Description = description; DisplayDescription(watcher); } } watcher.Start(); _saveThreadList = true; return(true); }
public static void Load(Action <ThreadWatcher> onThreadLoad) { if (_watchers != null) { throw new Exception("Threads have already been loaded."); } _watchers = new List <ThreadWatcher>(); string path = Path.Combine(Settings.GetSettingsDirectory(), Settings.ThreadsFileName); if (!File.Exists(path)) { return; } ThreadListData data = JsonConvert.DeserializeObject <ThreadListData>(File.ReadAllText(path)); if (data.FileVersion > _currentFileVersion) { throw new Exception("Threads file was created with a newer version of this program."); } else if (data.FileVersion <= 1) { // Skip loading if not backwards compatible return; } foreach (ThreadWatcherConfig config in data.Threads) { ThreadWatcher watcher = ThreadWatcher.Create(config); onThreadLoad(watcher); if (!watcher.IsStopping) { watcher.Start(); } _watchers.Add(watcher); } }
private void RemoveThreads(bool removeCompleted, bool removeSelected, Action <ThreadWatcher> preRemoveAction = null) { int i = 0; while (i < lvThreads.Items.Count) { ThreadWatcher watcher = (ThreadWatcher)lvThreads.Items[i].Tag; if ((removeCompleted || (removeSelected && lvThreads.Items[i].Selected)) && !watcher.IsRunning) { try { preRemoveAction?.Invoke(watcher); } catch { } lvThreads.Items.RemoveAt(i); ThreadList.Remove(watcher); } else { i++; } } _saveThreadList = true; }
public frmThreadEdit(ThreadWatcher watcher, Dictionary <string, int> categories) { InitializeComponent(); GUI.SetFontAndScaling(this); foreach (string key in categories.Keys) { cboCategory.Items.Add(key); } cboCheckEvery.ValueMember = "Value"; cboCheckEvery.DisplayMember = "Text"; cboCheckEvery.DataSource = new[] { new ListItemInt32(0, "1 or <"), new ListItemInt32(2, "2"), new ListItemInt32(3, "3"), new ListItemInt32(5, "5"), new ListItemInt32(10, "10"), new ListItemInt32(60, "60") }; txtDescription.Text = watcher.Description; cboCategory.Text = watcher.Category; chkPageAuth.Checked = !String.IsNullOrEmpty(watcher.PageAuth); txtPageAuth.Text = watcher.PageAuth; chkPageAuth.Checked = !String.IsNullOrEmpty(watcher.ImageAuth); txtImageAuth.Text = watcher.ImageAuth; chkOneTime.Checked = watcher.OneTimeDownload; chkAutoFollow.Checked = watcher.AutoFollow; switch (watcher.CheckIntervalSeconds / 60) { case 0: cboCheckEvery.SelectedIndex = 0; break; case 1: cboCheckEvery.SelectedIndex = 0; break; case 2: cboCheckEvery.SelectedIndex = 1; break; case 3: cboCheckEvery.SelectedIndex = 2; break; case 5: cboCheckEvery.SelectedIndex = 3; break; case 10: cboCheckEvery.SelectedIndex = 4; break; case 60: cboCheckEvery.SelectedIndex = 5; break; default: txtCheckEvery.Text = (watcher.CheckIntervalSeconds / 60).ToString(CultureInfo.InvariantCulture); cboCheckEvery.SelectedIndex = -1; cboCheckEvery.Enabled = false; break; } if (watcher.IsRunning) { chkPageAuth.Enabled = false; txtPageAuth.Enabled = false; chkImageAuth.Enabled = false; txtImageAuth.Enabled = false; chkOneTime.Enabled = false; chkAutoFollow.Enabled = false; pnlCheckEvery.Enabled = false; } cboCheckEvery.SelectedIndexChanged += cboCheckEvery_SelectedIndexChanged; txtCheckEvery.TextChanged += txtCheckEvery_TextChanged; }
public static void Remove(ThreadWatcher watcher) { EnsureLoaded(); _watchers.Remove(watcher); }
private void SetWaitStatus(ThreadWatcher watcher) { int remainingSeconds = (watcher.MillisecondsUntilNextCheck + 999) / 1000; DisplayStatus(watcher, $"Waiting {remainingSeconds} seconds"); }
private void ThreadWatcher_ThreadDownloadDirectoryRename(ThreadWatcher watcher, EventArgs args) { this.BeginInvoke(() => { _saveThreadList = true; }); }
public static void Add(ThreadWatcher watcher) { EnsureLoaded(); _watchers.Add(watcher); }
private void DisplayLastImageOn(ThreadWatcher watcher) { SetSubItemText(watcher, ColumnIndex.LastImageOn, watcher.LastImageOn?.ToLocalTime().ToString(_uiDateTimeFormat)); }
private void DisplayAddedOn(ThreadWatcher watcher) { SetSubItemText(watcher, ColumnIndex.AddedOn, watcher.AddedOn.ToLocalTime().ToString(_uiDateTimeFormat)); }
private void DisplayStatus(ThreadWatcher watcher, string status) { SetSubItemText(watcher, ColumnIndex.Status, status); }
private void DisplayDescription(ThreadWatcher watcher) { SetSubItemText(watcher, ColumnIndex.Description, watcher.Description); }