' Copyright (C) 2025  P4pillon.org
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU Affero 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; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU Affero General Public License for more details.
'
' You should have received a copy of the GNU Affero General Public License
' along with this program.  If not, see <https://www.gnu.org/licenses/>.
Imports System.Xml
Public Class Nettoyage_Fichiers


  
    Public Sub Suppression_fichiers_affichage()
        Suppression_fichiers_DataGridView.Rows.Clear()
        For r = 1 To Main.config.Nettoyage_Fichiers_Items.Count
            Suppression_fichiers_DataGridView.Rows.Add()
        Next
        For r = 0 To Main.config.Nettoyage_Fichiers_Items.Count - 1
            Suppression_fichiers_DataGridView.Item(Suppression_fichiers_Repertoire.Index, r).Value = Main.config.Nettoyage_Fichiers_Items(r).Rep
            Suppression_fichiers_DataGridView.Item(Suppression_fichiers_Fichiers.Index, r).Value = Main.config.Nettoyage_Fichiers_Items(r).Fichiers
            Suppression_fichiers_DataGridView.Item(Suppression_fichiers_Jours.Index, r).Value = Main.config.Nettoyage_Fichiers_Items(r).Jours
        Next
    End Sub
  


    Private Sub Suppression_fichiers_Cancel_Button_Click(sender As Object, e As EventArgs)
        Suppression_fichiers_affichage()
    End Sub

    Public Sub Suppression_fichiers_Action()
        For r = 0 To Main.config.Nettoyage_Fichiers_Items.Count - 1
            Dim Rep = Main.config.Nettoyage_Fichiers_Items(r).Rep
            If Mid(Rep, Rep.Length, "1") <> "\" Then
                Rep = Rep & "\"
            End If
            Dim Fichiers = Main.config.Nettoyage_Fichiers_Items(r).Fichiers
            Dim Jours = CDbl(Main.config.Nettoyage_Fichiers_Items(r).Jours)


            Dim Fichiers_remove_list As New List(Of String)
            Dim fichier = Dir(Rep & Fichiers)
            While fichier <> ""
                Fichiers_remove_list.Add(Rep & fichier)
                fichier = Dir()
            End While
            For Each fichier_ As String In Fichiers_remove_list
                Try
                    Dim fichier_info = My.Computer.FileSystem.GetFileInfo(fichier_)
                    If DateAdd(DateInterval.Day, Jours, fichier_info.CreationTime) < Now Then
                        Label1.Text = "Suppression: " & fichier_
                        Application.DoEvents()
                        My.Computer.FileSystem.DeleteFile(fichier_)
                    End If
                Catch ex As Exception
                End Try
            Next
        Next
        Label1.Text = ""
    End Sub

    Private Sub Suppression_fichiers_Go_Button_Click(sender As Object, e As EventArgs) Handles Suppression_fichiers_Go_Button.Click
        Suppression_fichiers_Action()
    End Sub
End Class
