Files
SCrawler/SCrawler/Download/MissingPostsForm.vb
Andy c28c0e1ba3 2022.9.10.0
Fixed: missed posts are not saved
Fixed memory leaking because of the video
2022-09-10 12:28:40 +03:00

296 lines
17 KiB
VB.net

' Copyright (C) 2022 Andy
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY
Imports System.ComponentModel
Imports PersonalUtilities.Functions.Messaging
Imports PersonalUtilities.Forms
Imports PersonalUtilities.Forms.Toolbars
Imports SCrawler.API.Base
Namespace DownloadObjects
Friend Class MissingPostsForm
#Region "Declarations"
Private WithEvents MyDefs As DefaultFormOptions
Private ReadOnly MUsers As List(Of IUserData)
Private WithEvents BTT_DOWN_ALL As ToolStripButton
Private WithEvents BTT_INFO As ToolStripButton
#End Region
#Region "Initializer"
Friend Sub New()
InitializeComponent()
MUsers = New List(Of IUserData)
MyDefs = New DefaultFormOptions(Me, Settings.Design)
BTT_DOWN_ALL = New ToolStripButton With {
.Text = "Download ALL",
.ToolTipText = String.Empty,
.AutoToolTip = False,
.Image = My.Resources.StartPic_01_Green_16,
.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText
}
BTT_INFO = New ToolStripButton With {
.Text = "Info",
.ToolTipText = "Show information about the missing post (F1)",
.AutoToolTip = True,
.Image = My.Resources.InfoPic_32,
.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText
}
End Sub
#End Region
#Region "Form handlers"
Private Sub MissingPostsForm_Load(sender As Object, e As EventArgs) Handles Me.Load
With MyDefs
.MyViewInitialize()
.AddEditToolbarPlus({EditToolbar.ControlItem.Separator, BTT_DOWN_ALL, BTT_INFO})
.EndLoaderOperations(False)
End With
RefillList()
End Sub
Private Sub MissingPostsForm_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
e.Cancel = True
Hide()
End Sub
Private Sub MissingPostsForm_Disposed(sender As Object, e As EventArgs) Handles Me.Disposed
MUsers.Clear()
End Sub
Private Sub MissingPostsForm_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.F1 Then ShowPostInformation() : e.Handled = True
End Sub
#End Region
#Region "RefillList"
Private Overloads Sub RefillList() Handles MyDefs.ButtonUpdateClick
RefillList(True)
End Sub
Friend Overloads Sub RefillList(ByVal User As IUserData)
If MUsers.Count = 0 OrElse Not MUsers.Contains(User) Then MUsers.Add(User) : RefillList(False)
End Sub
Friend Overloads Sub RefillList(ByVal Reload As Boolean)
Try
If Reload Then MUsers.Clear()
LIST_DATA.Items.Clear()
LIST_DATA.Groups.Clear()
If Reload And Settings.Users.Count > 0 Then
MUsers.ListAddList(Settings.Users.SelectMany(Function(ByVal user As IUserData) As IEnumerable(Of IUserData)
DirectCast(user, UserDataBase).LoadContentInformation()
If user.IsCollection Then
With DirectCast(user, API.UserDataBind)
If .Count > 0 Then Return .Where(Function(u) DirectCast(u, UserDataBase).ContentMissingExists)
End With
ElseIf DirectCast(user, UserDataBase).ContentMissingExists Then
Return {user}
End If
Return New IUserData() {}
End Function), LAP.IgnoreICopier)
End If
If MUsers.Count > 0 Then
Dim gName$ = String.Empty
Dim g As ListViewGroup = Nothing
Dim i% = -1
Dim cm As List(Of UserMedia)
For Each uu As UserDataBase In MUsers
i += 1
cm = uu.ContentMissing
If cm.Count > 0 Then
gName = String.Empty
If uu.IncludedInCollection Then gName = $"{uu.CollectionName} - "
gName &= $"{uu.User.Name} ({uu.Site})"
ControlInvoke(LIST_DATA, Sub()
LIST_DATA.Groups.Add(New ListViewGroup(gName) With {.Tag = uu.LVIKey})
g = LIST_DATA.Groups(LIST_DATA.Groups.Count - 1)
End Sub)
For i% = 0 To cm.Count - 1 : ControlInvoke(LIST_DATA, Sub() LIST_DATA.Items.Add(New ListViewItem(cm(i).Post.ID, g))) : Next
End If
Next
End If
Catch ex As Exception
ErrorsDescriber.Execute(EDP.SendInLog, ex, "[DownloadObjects.MissingPostsForm.RefillList]")
End Try
End Sub
#End Region
#Region "Post actions"
Private Sub BTT_DOWN_Click(sender As Object, e As EventArgs) Handles BTT_DOWN.Click
'Try
' If LIST_DATA.SelectedItems.Count > 0 Then
' Dim users As List(Of IUserData) = LIST_DATA.SelectedItems.ToObjectsList.ListCast(Of ListViewItem)().
' Select(Function(d) Settings.GetUser(CStr(d.Group.Tag))).ListWithRemove(Function(d) d Is Nothing)
' If users.ListExists Then
' If MsgBoxE({"The following users will be added to the download queue:" & vbCr & vbCr &
' users.Select(Function(u) u.ToString).ListToString(vbNewLine), "Download users"},,,, {"Process", "Cancel"}) = 0 Then
' users.ForEach(Sub(u) u.DownloadMissingOnly = True)
' Downloader.AddRange(users)
' users.Clear()
' End If
' End If
' Else
' MsgBoxE("No selected posts")
' End If
'Catch ex As Exception
' ErrorsDescriber.Execute(EDP.SendInLog, ex, "[DownloadObjects.MissingPostsForm.Download]")
'End Try
End Sub
Private Sub BTT_OPEN_POST_Click(sender As Object, e As EventArgs) Handles BTT_OPEN_POST.Click
Try
If LIST_DATA.SelectedItems.Count > 0 Then
If LIST_DATA.SelectedItems.Count = 1 OrElse
MsgBoxE({$"Are you sure you want to open {LIST_DATA.SelectedItems.Count} posts?", "Open multiple posts"}, vbExclamation + vbYesNo) = vbYes Then
Dim data As List(Of ListViewItem) = LIST_DATA.SelectedItems.ToObjectsList.ListCast(Of ListViewItem)
If data.ListExists Then
Dim uKey$, url$
Dim u As IUserData = Nothing
Dim i%
Dim cm As List(Of UserMedia)
For Each _d In data
uKey = _d.Group.Tag
If u Is Nothing OrElse Not u.Key = uKey Then u = Settings.GetUser(uKey)
If Not u Is Nothing Then
i = -1
With DirectCast(u, UserDataBase)
cm = .ContentMissing
If cm.Count > 0 Then i = cm.FindIndex(Function(c) c.Post.ID = _d.Text)
If i >= 0 Then
url = UserDataBase.GetPostUrl(u, cm(i))
If Not url.IsEmptyString Then
Try : Process.Start(url) : Catch : End Try
End If
End If
End With
End If
Next
End If
End If
End If
Catch ex As Exception
ErrorsDescriber.Execute(EDP.SendInLog, ex, "[DownloadObjects.MissingPostsForm.OpenPost]")
End Try
End Sub
Private Sub BTT_OPEN_USER_Click(sender As Object, e As EventArgs) Handles BTT_OPEN_USER.Click
Try
If LIST_DATA.SelectedItems.Count > 0 Then
Dim users As List(Of IUserData) = LIST_DATA.SelectedItems.ToObjectsList.ListCast(Of ListViewItem)().
Select(Function(d) Settings.GetUser(CStr(d.Group.Tag))).ListWithRemove(Function(d) d Is Nothing)
If users.ListExists Then users.ForEach(Sub(u) u.OpenFolder())
Else
MsgBoxE("No selected posts")
End If
Catch ex As Exception
ErrorsDescriber.Execute(EDP.SendInLog, ex, $"[DownloadObjects.MissingPostsForm.OpenUser]")
End Try
End Sub
Private Sub ShowPostInformation() Handles BTT_INFO.Click, BTT_CONTEXT_SHOW_POST_INFO.Click, LIST_DATA.DoubleClick
Try
If LIST_DATA.SelectedItems.Count > 0 Then
Dim data As ListViewItem = LIST_DATA.SelectedItems.ToObjectsList.ListCast(Of ListViewItem)().First
Dim uKey$, url$
Dim u As IUserData = Nothing
Dim i%
Dim cm As List(Of UserMedia)
Dim m As UserMedia
uKey = data.Group.Tag
If Not uKey.IsEmptyString Then u = Settings.GetUser(uKey)
If Not u Is Nothing Then
i = -1
With DirectCast(u, UserDataBase)
cm = .ContentMissing
If cm.Count > 0 Then i = cm.FindIndex(Function(c) c.Post.ID = data.Text)
If i >= 0 Then
m = cm(i)
url = UserDataBase.GetPostUrl(u, m)
Dim msg As New MMessage("", "Post information") With {.Editable = True}
Dim b As New List(Of MsgBoxButton)
If Not url.IsEmptyString Then b.Add(New MsgBoxButton("Open") With {.IsDialogResultButton = False,
.ToolTip = "Open post in browser",
.KeyCode = Keys.F1,
.CallBack = Sub(result, message, button)
Try : Process.Start(url) : Catch : End Try
End Sub})
b.Add(New MsgBoxButton("OK"))
With msg
.Buttons = b
.DefaultButton = If(b.Count = 2, 1, 0)
.CancelButton = .DefaultButton
.Text = $"Type: {m.Type}"
.Text.StringAppendLine($"Address: {url}")
If m.Post.Date.HasValue Then .Text.StringAppendLine($"Date: {m.Post.Date.Value.ToStringDate(ADateTime.Formats.BaseDateTime)}")
.Text &= vbNewLine.StringDup(2)
If u.IncludedInCollection Then .Text.StringAppendLine($"User collection: {u.CollectionName}")
.Text.StringAppendLine($"User site: {u.Site}")
.Text.StringAppendLine($"User name: {IIf(Not u.FriendlyName.IsEmptyString And Not u.IncludedInCollection, u.FriendlyName, u.Name)}")
End With
MsgBoxE(msg)
b.Clear()
cm.Clear()
End If
End With
End If
Else
MsgBoxE("No selected posts")
End If
Catch ex As Exception
ErrorsDescriber.Execute(EDP.SendInLog, ex, "[DownloadObjects.MissingPostsForm.ShowPostInformation]")
End Try
End Sub
Private Sub BTT_FIND_USER_Click(sender As Object, e As EventArgs) Handles BTT_FIND_USER.Click
Try
If LIST_DATA.SelectedItems.Count > 0 Then
Dim user As IUserData = LIST_DATA.SelectedItems.ToObjectsList.ListCast(Of ListViewItem)().
Select(Function(d) Settings.GetUser(CStr(d.Group.Tag))).ListWithRemove(Function(d) d Is Nothing).DefaultIfEmpty(Nothing).First
If Not user Is Nothing Then MainFrameObj.FocusUser(user.Key, True)
Else
MsgBoxE("No selected posts")
End If
Catch ex As Exception
ErrorsDescriber.Execute(EDP.SendInLog, ex, $"[DownloadObjects.MissingPostsForm.FindUser]")
End Try
End Sub
Private Sub DeletePost() Handles MyDefs.ButtonDeleteClickE, BTT_DELETE.Click
Const MsgTitle$ = "Remove missing posts"
Dim UsersToUpdate As New List(Of UserDataBase)
Try
Dim data As List(Of ListViewItem) = LIST_DATA.SelectedItems.ToObjectsList.ListCast(Of ListViewItem)
If data.ListExists Then
Dim lp As New ListAddParams(LAP.NotContainsOnly)
Dim usersCount% = ListAddList(Nothing, data.Select(Function(d) d.Group.Name), LAP.NotContainsOnly).ListIfNothing.Count
If MsgBoxE({"Are you sure you want to delete the selected missing posts?" & vbCr &
$"Number of affected users: {usersCount}." & vbCr &
$"Number of posts to be deleted: {data.Count}.", MsgTitle}, vbExclamation,,, {"Process", "Cancel"}) = 0 Then
Dim uKey$
Dim u As UserDataBase = Nothing
Dim cm As List(Of UserMedia)
Dim i%
For Each _d In data
uKey = _d.Group.Tag
If u Is Nothing OrElse Not u.LVIKey = uKey Then u = Settings.GetUser(uKey)
If Not u Is Nothing Then
i = -1
cm = u.ContentMissing
If cm.Count > 0 Then i = cm.FindIndex(Function(c) c.Post.ID = _d.Text)
If i >= 0 Then u.RemoveMedia(cm(i), UserMedia.States.Missing) : UsersToUpdate.ListAddValue(u, lp)
End If
Next
MsgBoxE({"The selected posts have been successfully deleted", MsgTitle})
Else
MsgBoxE({"Operation canceled", MsgTitle})
End If
Else
MsgBoxE({"No selected posts", MsgTitle})
End If
Catch ex As Exception
ErrorsDescriber.Execute(EDP.SendInLog, ex, "[DownloadObjects.MissingPostsForm.DeletePost]")
Finally
UpdateUsers(UsersToUpdate)
UsersToUpdate.Clear()
End Try
End Sub
Private Sub UpdateUsers(ByVal UserList As List(Of UserDataBase))
Try
If UserList.ListExists Then UserList.ForEach(Sub(u) u.UpdateContentInformation())
Catch ex As Exception
ErrorsDescriber.Execute(EDP.SendInLog, ex, "[DownloadObjects.MissingPostsForm.UpdateUsers]")
End Try
End Sub
#End Region
End Class
End Namespace