diff --git a/SCrawler/Download/Automation/Scheduler.vb b/SCrawler/Download/Automation/Scheduler.vb index 01931d4..a5426cc 100644 --- a/SCrawler/Download/Automation/Scheduler.vb +++ b/SCrawler/Download/Automation/Scheduler.vb @@ -19,7 +19,9 @@ Namespace DownloadObjects RaiseEvent PauseDisabled() End Sub Private ReadOnly Plans As List(Of AutoDownloader) - Private ReadOnly File As SFile = $"Settings\AutoDownload.xml" + Friend Const FileNameDefault As String = "AutoDownload" + Friend ReadOnly FileDefault As SFile = $"{SettingsFolderName}\{FileNameDefault}.xml" + Friend File As SFile = Nothing Private ReadOnly PlanWorking As Predicate(Of AutoDownloader) = Function(Plan) Plan.Working Private ReadOnly PlanDownloading As Predicate(Of AutoDownloader) = Function(Plan) Plan.Downloading Private ReadOnly PlansWaiter As Action(Of Predicate(Of AutoDownloader)) = Sub(ByVal Predicate As Predicate(Of AutoDownloader)) @@ -27,20 +29,9 @@ Namespace DownloadObjects End Sub Friend Sub New() Plans = New List(Of AutoDownloader) - If File.Exists Then - Using x As New XmlFile(File,, False) With {.AllowSameNames = True} - x.LoadData() - If x.Contains(Name_Plan) Then - For Each e In x : Plans.Add(New AutoDownloader(e)) : Next - Else - Plans.Add(New AutoDownloader(x)) - End If - End Using - End If - If Plans.Count > 0 Then Plans.ForEach(Sub(p) - p.Source = Me - AddHandler p.PauseDisabled, AddressOf OnPauseDisabled - End Sub) : Plans.ListReindex + File = Settings.AutomationFile.Value.IfNullOrEmpty(FileDefault) + If Not File.Exists Then File = FileDefault + Reset(File) End Sub Default Friend ReadOnly Property Item(ByVal Index As Integer) As AutoDownloader Implements IMyEnumerator(Of AutoDownloader).MyEnumeratorObject Get @@ -96,6 +87,40 @@ Namespace DownloadObjects Catch End Try End Sub + Friend Function Reset(ByVal f As SFile) As Boolean + Dim __pause As PauseModes = Pause + If Plans.Count > 0 Then + If Not Plans.Exists(PlanWorking) Then + Pause = PauseModes.Unlimited + If Plans.Exists(PlanWorking) Then + MsgBoxE({$"Some plans are already being worked.{vbCr}Wait for the plans to complete their work and try again.", + "Change scheduler"}, vbCritical) + If __pause = PauseModes.Until Then __pause = PauseModes.Unlimited + Pause = __pause + Return False + End If + End If + [Stop]() + If _UpdateRequired Then Update() + Plans.ListClearDispose(,, EDP.LogMessageValue) + End If + If f.Exists Then + File = f + Using x As New XmlFile(File,, False) With {.AllowSameNames = True} + x.LoadData() + If x.Contains(Name_Plan) Then + For Each e In x : Plans.Add(New AutoDownloader(e)) : Next + Else + Plans.Add(New AutoDownloader(x)) + End If + End Using + If Plans.Count > 0 Then Plans.ForEach(Sub(ByVal p As AutoDownloader) + p.Source = Me + AddHandler p.PauseDisabled, AddressOf OnPauseDisabled + End Sub) : Plans.ListReindex + End If + Return True + End Function #Region "Groups Support" Friend Sub GROUPS_Updated(ByVal Sender As DownloadGroup) If Count > 0 Then Plans.ForEach(Sub(p) p.GROUPS_Updated(Sender)) diff --git a/SCrawler/Download/Automation/SchedulerEditorForm.vb b/SCrawler/Download/Automation/SchedulerEditorForm.vb index be47944..4edd83f 100644 --- a/SCrawler/Download/Automation/SchedulerEditorForm.vb +++ b/SCrawler/Download/Automation/SchedulerEditorForm.vb @@ -9,10 +9,12 @@ Imports PersonalUtilities.Forms Imports PersonalUtilities.Forms.Toolbars Imports ECI = PersonalUtilities.Forms.Toolbars.EditToolbar.ControlItem +Imports ADB = PersonalUtilities.Forms.Controls.Base.ActionButton.DefaultButtons Namespace DownloadObjects Friend Class SchedulerEditorForm #Region "Declarations" Private WithEvents MyDefs As DefaultFormOptions + Private WithEvents BTT_SETTINGS As ToolStripButton Private WithEvents BTT_CLONE As ToolStripButton Private ReadOnly MENU_SKIP As ToolStripDropDownButton Private WithEvents BTT_SKIP As ToolStripMenuItem @@ -28,6 +30,13 @@ Namespace DownloadObjects Friend Sub New() InitializeComponent() MyDefs = New DefaultFormOptions(Me, Settings.Design) + BTT_SETTINGS = New ToolStripButton With { + .Text = String.Empty, + .AutoToolTip = True, + .ToolTipText = "Change scheduler", + .DisplayStyle = ToolStripItemDisplayStyle.Image, + .Image = My.Resources.ScriptPic_32 + } BTT_CLONE = New ToolStripButton With { .Text = "Clone", .DisplayStyle = ToolStripItemDisplayStyle.ImageAndText, @@ -95,7 +104,8 @@ Namespace DownloadObjects Private Sub SchedulerEditorForm_Load(sender As Object, e As EventArgs) Handles Me.Load With MyDefs .MyViewInitialize() - .AddEditToolbar({ECI.Add, BTT_CLONE, ECI.Edit, ECI.Delete, ECI.Update, ECI.Separator, BTT_START, BTT_START_FORCE, MENU_SKIP, BTT_PAUSE}) + .AddEditToolbar({BTT_SETTINGS, ECI.Separator, ECI.Add, BTT_CLONE, ECI.Edit, ECI.Delete, ECI.Update, ECI.Separator, + BTT_START, BTT_START_FORCE, MENU_SKIP, BTT_PAUSE}) PauseArr.AddButtons(BTT_PAUSE, .MyEditToolbar.ToolStrip) Refill() .EndLoaderOperations(False) @@ -186,7 +196,78 @@ Namespace DownloadObjects Edit() End Sub #End Region -#Region "Start, Skip, Pause" +#Region "Settings, Start, Skip, Pause" + Private Sub BTT_SETTINGS_Click(sender As Object, e As EventArgs) Handles BTT_SETTINGS.Click + Const msgTitle$ = "Change scheduler" + Try + Const defName$ = "Default" + Dim l As New Dictionary(Of SFile, String) + With SFile.GetFiles(SettingsFolderName.CSFileP, $"{Scheduler.FileNameDefault}*.xml",, EDP.ReturnValue) + If .ListExists Then .ForEach(Sub(ff) l.Add(ff, ff.Name.Replace(Scheduler.FileNameDefault, String.Empty).StringTrimStart("_").IfNullOrEmpty(defName))) + End With + If l.Count > 0 Then + Using chooser As New SimpleListForm(Of String)(l.Values.Cast(Of String), Settings.Design) With { + .DesignXMLNodeName = "SchedulerChooserForm", + .Icon = PersonalUtilities.Tools.ImageRenderer.GetIcon(My.Resources.ScriptPic_32, EDP.ReturnValue), + .FormText = "Schedulers", + .Mode = SimpleListFormModes.SelectedItems, + .MultiSelect = False + } + With chooser + Dim i% + Dim f As SFile + Dim selectedName$ + Dim addedObj$ = String.Empty + .ClearButtons() + .Buttons = {ADB.Add, ADB.Delete} + AddHandler .AddClick, Sub(ByVal obj As Object, ByVal args As SimpleListFormEventArgs) + If addedObj.IsEmptyString Then + addedObj = InputBoxE("Enter a new scheduler name:", msgTitle) + args.Result = Not addedObj.IsEmptyString + If args.Result Then args.Item = addedObj + Else + MsgBoxE({"You can only create one scheduler at a time", "Create a new scheduler"}, vbCritical) + End If + End Sub + If Settings.Automation.File.Name = Scheduler.FileNameDefault Then + .DataSelectedIndexes.Add(0) + Else + i = l.Keys.ListIndexOf(Function(ff) ff = Settings.Automation.File) + If i >= 0 Then .DataSelectedIndexes.Add(i) + End If + If .ShowDialog() = DialogResult.OK Then + selectedName = .DataResult.FirstOrDefault + If Not selectedName.IsEmptyString Then + If selectedName = defName Then + f = Settings.Automation.FileDefault + Else + f = $"{SettingsFolderName}\{Scheduler.FileNameDefault}_{selectedName.StringRemoveWinForbiddenSymbols}.xml" + End If + If Not Settings.Automation.File = f AndAlso Settings.Automation.Reset(f) Then + Settings.Automation.File = f + If selectedName = defName Then + Settings.AutomationFile.Value = Nothing + Else + Settings.AutomationFile.Value = f + End If + Refill() + If Not .DataSource.Count = l.Count Then + For i = l.Count - 1 To 0 Step -1 + If Not .DataSource.Contains(l(l.Keys(i))) Then l.Keys(i).Delete(, SFODelete.DeleteToRecycleBin, EDP.SendToLog) + Next + End If + End If + End If + End If + End With + End Using + Else + MsgBoxE({"There are no plans created", msgTitle}, vbExclamation) + End If + Catch ex As Exception + ErrorsDescriber.Execute(EDP.LogMessageValue, ex, msgTitle) + End Try + End Sub Private Sub BTT_START_Click(sender As Object, e As EventArgs) Handles BTT_START.Click If _LatestSelected.ValueBetween(0, LIST_PLANS.Items.Count - 1) Then With Settings.Automation(_LatestSelected) : .Start(.IsNewPlan) : End With diff --git a/SCrawler/SettingsCLS.vb b/SCrawler/SettingsCLS.vb index a6798ce..1aa7b44 100644 --- a/SCrawler/SettingsCLS.vb +++ b/SCrawler/SettingsCLS.vb @@ -130,6 +130,7 @@ Friend Class SettingsCLS : Implements IDownloaderSettings, IDisposable Friend ReadOnly Property DownloadLocations As STDownloader.DownloadLocationsCollection Friend ReadOnly Property GlobalLocations As STDownloader.DownloadLocationsCollection Friend Property Automation As Scheduler + Friend ReadOnly Property AutomationFile As XMLValue(Of SFile) Friend ReadOnly Property BlackList As List(Of UserBan) Private ReadOnly BlackListFile As SFile = $"{SettingsFolderName}\BlackList.txt" Private ReadOnly UsersSettingsFile As SFile = $"{SettingsFolderName}\Users.xml" @@ -170,6 +171,7 @@ Friend Class SettingsCLS : Implements IDownloaderSettings, IDisposable SeparateVideoFolder = New XMLValue(Of Boolean)("SeparateVideoFolder", True, MyXML) CollectionsPath = New XMLValue(Of String)("CollectionsPath", CollectionsFolderName, MyXML) + AutomationFile = New XMLValue(Of SFile)("AutomationFile",, MyXML) UserAgent = New XMLValue(Of String)("UserAgent",, MyXML) If Not UserAgent.IsEmptyString Then DefaultUserAgent = UserAgent