private void LoadThreadList() { try { string path = Path.Combine(Settings.GetSettingsDirectory(), Settings.ThreadsFileName); if (!File.Exists(path)) return; string[] lines = File.ReadAllLines(path); if (lines.Length < 1) return; int fileVersion = Int32.Parse(lines[0]); int linesPerThread; switch (fileVersion) { case 1: linesPerThread = 6; break; case 2: linesPerThread = 7; break; case 3: linesPerThread = 10; break; default: return; } if (lines.Length < (1 + linesPerThread)) return; int i = 1; while (i <= lines.Length - linesPerThread) { string pageURL = lines[i++]; string pageAuth = lines[i++]; string imageAuth = lines[i++]; int checkIntervalSeconds = Int32.Parse(lines[i++]); bool oneTimeDownload = lines[i++] == "1"; string saveDir = lines[i++]; saveDir = saveDir.Length != 0 ? General.GetAbsoluteDirectoryPath(saveDir, Settings.AbsoluteDownloadDirectory) : null; string description; StopReason? stopReason = null; WatcherExtraData extraData = new WatcherExtraData(); if (fileVersion >= 2) { string stopReasonLine = lines[i++]; if (stopReasonLine.Length != 0) { stopReason = (StopReason)Int32.Parse(stopReasonLine); } } if (fileVersion >= 3) { description = lines[i++]; extraData.AddedOn = new DateTime(Int64.Parse(lines[i++]), DateTimeKind.Utc).ToLocalTime(); string lastImageOn = lines[i++]; if (lastImageOn.Length != 0) { extraData.LastImageOn = new DateTime(Int64.Parse(lastImageOn), DateTimeKind.Utc).ToLocalTime(); } } else { description = String.Empty; extraData.AddedOn = DateTime.Now; } AddThread(pageURL, pageAuth, imageAuth, checkIntervalSeconds, oneTimeDownload, saveDir, description, stopReason, extraData); } } catch { } }
private bool AddThread(string pageURL, string pageAuth, string imageAuth, int checkInterval, bool oneTime, string saveDir, string description, StopReason? stopReason, WatcherExtraData extraData) { ThreadWatcher watcher = null; ListViewItem newListViewItem = null; foreach (ThreadWatcher existingWatcher in ThreadWatchers) { if (String.Equals(existingWatcher.PageURL, pageURL, StringComparison.OrdinalIgnoreCase)) { if (existingWatcher.IsRunning) return false; watcher = existingWatcher; break; } } if (watcher == null) { watcher = new ThreadWatcher(pageURL); watcher.ThreadDownloadDirectory = saveDir; watcher.Description = description; 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; newListViewItem = new ListViewItem(String.Empty); for (int i = 1; i < lvThreads.Columns.Count; i++) { newListViewItem.SubItems.Add(String.Empty); } newListViewItem.Tag = watcher; lvThreads.Items.Add(newListViewItem); } watcher.PageAuth = pageAuth; watcher.ImageAuth = imageAuth; watcher.CheckIntervalSeconds = checkInterval; watcher.OneTimeDownload = oneTime; if (extraData == null) { extraData = watcher.Tag as WatcherExtraData; if (extraData == null) { extraData = new WatcherExtraData { AddedOn = DateTime.Now }; } } if (newListViewItem != null) { extraData.ListViewItem = newListViewItem; } watcher.Tag = extraData; DisplayDescription(watcher); DisplayAddedOn(watcher); DisplayLastImageOn(watcher); if (stopReason == null) { watcher.Start(); } else { watcher.Stop(stopReason.Value); } return true; }