Imports System.Drawing.Drawing2D

Imports Stax
Imports Stax.UI
Imports Stax.Win32
Imports Stax.AppFramework

Public Class ExternalApplicationsForm
    Inherits ExtendedForm

#Region " Windows Form Designer generated code "
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents bClose As System.Windows.Forms.Button
    Friend WithEvents tv As ExtendedTreeView
    Friend WithEvents StatusControl As Stax.UI.StatusControl
    Friend WithEvents SmartTagProvider As Stax.UI.Design.SmartTagProvider
    Friend WithEvents TipProvider As Stax.UI.TipProvider
    Friend WithEvents ToolStrip As System.Windows.Forms.ToolStrip
    Friend WithEvents tsbSearch As System.Windows.Forms.ToolStripButton
    Friend WithEvents tsbDownload As System.Windows.Forms.ToolStripButton
    Friend WithEvents tsbHelp As System.Windows.Forms.ToolStripButton
    Friend WithEvents ToolStripItemExtender As Stax.UI.Design.ToolStripItemExtender
    Friend WithEvents tsbCheck As System.Windows.Forms.ToolStripButton
    Friend WithEvents SelectionToolStripMenuItem As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents ShowWarningMenuItem As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents ToolStripSeparator1 As System.Windows.Forms.ToolStripSeparator
    Friend WithEvents DownloadWithBrowserMenuItem As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents OpenWebsiteInBrowserMenuItem As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents OpenDirectoryOfApplicationMenuItem As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents LaunchTheApplicationMenuItem As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents CopyDownloadURLMenuItem As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents CopyWebsiteURLMenuItem As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents ChangeDownloadDirectoryToolStripMenuItem As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents tsbBrowse As System.Windows.Forms.ToolStripButton
    Friend WithEvents ToolsToolStripDropDownButton As System.Windows.Forms.ToolStripDropDownButton
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Me.tv = New Stax.UI.ExtendedTreeView
        Me.StatusControl = New Stax.UI.StatusControl
        Me.bClose = New System.Windows.Forms.Button
        Me.SmartTagProvider = New Stax.UI.Design.SmartTagProvider
        Me.TipProvider = New Stax.UI.TipProvider(Me.components)
        Me.ToolStrip = New System.Windows.Forms.ToolStrip
        Me.tsbBrowse = New System.Windows.Forms.ToolStripButton
        Me.tsbSearch = New System.Windows.Forms.ToolStripButton
        Me.tsbDownload = New System.Windows.Forms.ToolStripButton
        Me.tsbCheck = New System.Windows.Forms.ToolStripButton
        Me.ToolsToolStripDropDownButton = New System.Windows.Forms.ToolStripDropDownButton
        Me.SelectionToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem
        Me.ShowWarningMenuItem = New System.Windows.Forms.ToolStripMenuItem
        Me.ToolStripSeparator1 = New System.Windows.Forms.ToolStripSeparator
        Me.DownloadWithBrowserMenuItem = New System.Windows.Forms.ToolStripMenuItem
        Me.OpenWebsiteInBrowserMenuItem = New System.Windows.Forms.ToolStripMenuItem
        Me.OpenDirectoryOfApplicationMenuItem = New System.Windows.Forms.ToolStripMenuItem
        Me.LaunchTheApplicationMenuItem = New System.Windows.Forms.ToolStripMenuItem
        Me.CopyDownloadURLMenuItem = New System.Windows.Forms.ToolStripMenuItem
        Me.CopyWebsiteURLMenuItem = New System.Windows.Forms.ToolStripMenuItem
        Me.ChangeDownloadDirectoryToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem
        Me.tsbHelp = New System.Windows.Forms.ToolStripButton
        Me.ToolStripItemExtender = New Stax.UI.Design.ToolStripItemExtender
        CType(Me.TipProvider, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.ToolStrip.SuspendLayout()
        CType(Me.ToolStripItemExtender, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'tv
        '
        Me.tv.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
        Me.tv.AutoCollaps = True
        Me.tv.AutoExpand = True
        Me.tv.HideSelection = False
        Me.tv.Location = New System.Drawing.Point(12, 36)
        Me.tv.Name = "tv"
        Me.tv.Scrollable = False
        Me.tv.SelectOnMouseDown = True
        Me.tv.ShowLines = False
        Me.tv.ShowPlusMinus = False
        Me.tv.Size = New System.Drawing.Size(152, 327)
        Me.tv.Sorted = True
        Me.tv.TabIndex = 66
        '
        'StatusControl
        '
        Me.StatusControl.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.StatusControl.AutoScroll = True
        Me.StatusControl.BackColor = System.Drawing.Color.White
        Me.StatusControl.Location = New System.Drawing.Point(171, 36)
        Me.StatusControl.Name = "StatusControl"
        Me.StatusControl.Size = New System.Drawing.Size(378, 327)
        Me.StatusControl.TabIndex = 64
        '
        'bClose
        '
        Me.bClose.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.bClose.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.bClose.Location = New System.Drawing.Point(474, 370)
        Me.bClose.Name = "bClose"
        Me.bClose.Size = New System.Drawing.Size(75, 24)
        Me.bClose.TabIndex = 1
        Me.bClose.Text = "Close"
        '
        'ToolStrip
        '
        Me.ToolStrip.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.tsbBrowse, Me.tsbSearch, Me.tsbDownload, Me.tsbCheck, Me.ToolsToolStripDropDownButton, Me.tsbHelp})
        Me.ToolStrip.Location = New System.Drawing.Point(0, 0)
        Me.ToolStrip.Name = "ToolStrip"
        Me.ToolStrip.Size = New System.Drawing.Size(561, 25)
        Me.ToolStrip.TabIndex = 2
        Me.ToolStrip.Text = "ToolStrip1"
        '
        'tsbBrowse
        '
        Me.tsbBrowse.Image = Global.StaxRip.My.Resources.Resources.OpenFile16
        Me.tsbBrowse.ImageTransparentColor = System.Drawing.Color.Magenta
        Me.tsbBrowse.Name = "tsbBrowse"
        Me.tsbBrowse.Size = New System.Drawing.Size(62, 22)
        Me.tsbBrowse.Text = "Browse"
        Me.ToolStripItemExtender.SetTipText(Me.tsbBrowse, "Browse for the location.")
        '
        'tsbSearch
        '
        Me.tsbSearch.Image = Global.StaxRip.My.Resources.Resources.Find16
        Me.tsbSearch.ImageTransparentColor = System.Drawing.Color.Magenta
        Me.ToolStripItemExtender.SetKeyData(Me.tsbSearch, System.Windows.Forms.Keys.F3)
        Me.tsbSearch.Name = "tsbSearch"
        Me.tsbSearch.Size = New System.Drawing.Size(60, 22)
        Me.tsbSearch.Text = "Search"
        Me.ToolStripItemExtender.SetTipText(Me.tsbSearch, "Searches a already installed application.")
        '
        'tsbDownload
        '
        Me.tsbDownload.AutoToolTip = False
        Me.tsbDownload.Image = Global.StaxRip.My.Resources.Resources.Internet16
        Me.tsbDownload.ImageTransparentColor = System.Drawing.Color.Magenta
        Me.tsbDownload.Name = "tsbDownload"
        Me.tsbDownload.Size = New System.Drawing.Size(74, 22)
        Me.tsbDownload.Text = "Download"
        Me.ToolStripItemExtender.SetTipText(Me.tsbDownload, "Downloads and installes the selected application.")
        '
        'tsbCheck
        '
        Me.tsbCheck.AutoToolTip = False
        Me.tsbCheck.Image = Global.StaxRip.My.Resources.Resources.TrafficLight16
        Me.tsbCheck.ImageTransparentColor = System.Drawing.Color.Magenta
        Me.tsbCheck.Name = "tsbCheck"
        Me.tsbCheck.Size = New System.Drawing.Size(56, 22)
        Me.tsbCheck.Text = "Check"
        Me.ToolStripItemExtender.SetTipText(Me.tsbCheck, "Checks if all required applications are installed properly.")
        '
        'ToolsToolStripDropDownButton
        '
        Me.ToolsToolStripDropDownButton.AutoToolTip = False
        Me.ToolsToolStripDropDownButton.DropDownItems.AddRange(New System.Windows.Forms.ToolStripItem() {Me.SelectionToolStripMenuItem, Me.ChangeDownloadDirectoryToolStripMenuItem})
        Me.ToolsToolStripDropDownButton.Image = Global.StaxRip.My.Resources.Resources.Config16
        Me.ToolsToolStripDropDownButton.ImageTransparentColor = System.Drawing.Color.Magenta
        Me.ToolsToolStripDropDownButton.Name = "ToolsToolStripDropDownButton"
        Me.ToolsToolStripDropDownButton.Size = New System.Drawing.Size(61, 22)
        Me.ToolsToolStripDropDownButton.Text = "Tools"
        '
        'SelectionToolStripMenuItem
        '
        Me.SelectionToolStripMenuItem.DropDownItems.AddRange(New System.Windows.Forms.ToolStripItem() {Me.ShowWarningMenuItem, Me.ToolStripSeparator1, Me.DownloadWithBrowserMenuItem, Me.OpenWebsiteInBrowserMenuItem, Me.OpenDirectoryOfApplicationMenuItem, Me.LaunchTheApplicationMenuItem, Me.CopyDownloadURLMenuItem, Me.CopyWebsiteURLMenuItem})
        Me.SelectionToolStripMenuItem.Name = "SelectionToolStripMenuItem"
        Me.SelectionToolStripMenuItem.Size = New System.Drawing.Size(231, 22)
        Me.SelectionToolStripMenuItem.Text = "Selection"
        '
        'ShowWarningMenuItem
        '
        Me.ShowWarningMenuItem.CheckOnClick = True
        Me.ShowWarningMenuItem.Name = "ShowWarningMenuItem"
        Me.ShowWarningMenuItem.Size = New System.Drawing.Size(289, 22)
        Me.ShowWarningMenuItem.Text = "Show Warning About Unsupported Version"
        Me.ToolStripItemExtender.SetTipText(Me.ShowWarningMenuItem, "Shows a warning message in case a unsupported version is used. It's recommended t" & _
                "o leave this always enabled.")
        '
        'ToolStripSeparator1
        '
        Me.ToolStripSeparator1.Name = "ToolStripSeparator1"
        Me.ToolStripSeparator1.Size = New System.Drawing.Size(286, 6)
        '
        'DownloadWithBrowserMenuItem
        '
        Me.DownloadWithBrowserMenuItem.Image = Global.StaxRip.My.Resources.Resources.Internet16
        Me.DownloadWithBrowserMenuItem.Name = "DownloadWithBrowserMenuItem"
        Me.DownloadWithBrowserMenuItem.Size = New System.Drawing.Size(289, 22)
        Me.DownloadWithBrowserMenuItem.Text = "Download with Browser"
        Me.ToolStripItemExtender.SetTipText(Me.DownloadWithBrowserMenuItem, "Downloads the application with the browser.")
        '
        'OpenWebsiteInBrowserMenuItem
        '
        Me.OpenWebsiteInBrowserMenuItem.Image = Global.StaxRip.My.Resources.Resources.Internet16
        Me.OpenWebsiteInBrowserMenuItem.Name = "OpenWebsiteInBrowserMenuItem"
        Me.OpenWebsiteInBrowserMenuItem.Size = New System.Drawing.Size(289, 22)
        Me.OpenWebsiteInBrowserMenuItem.Text = "Open Website in Browser"
        Me.ToolStripItemExtender.SetTipText(Me.OpenWebsiteInBrowserMenuItem, "Opens the application's website in the browser.")
        '
        'OpenDirectoryOfApplicationMenuItem
        '
        Me.OpenDirectoryOfApplicationMenuItem.Image = Global.StaxRip.My.Resources.Resources.Folder16_8Bit
        Me.OpenDirectoryOfApplicationMenuItem.Name = "OpenDirectoryOfApplicationMenuItem"
        Me.OpenDirectoryOfApplicationMenuItem.Size = New System.Drawing.Size(289, 22)
        Me.OpenDirectoryOfApplicationMenuItem.Text = "Open Directory of Application"
        Me.ToolStripItemExtender.SetTipText(Me.OpenDirectoryOfApplicationMenuItem, "Opens the directory of the application with the windows explorer.")
        '
        'LaunchTheApplicationMenuItem
        '
        Me.LaunchTheApplicationMenuItem.Image = Global.StaxRip.My.Resources.Resources.Run16
        Me.LaunchTheApplicationMenuItem.Name = "LaunchTheApplicationMenuItem"
        Me.LaunchTheApplicationMenuItem.Size = New System.Drawing.Size(289, 22)
        Me.LaunchTheApplicationMenuItem.Text = "Launch the Application"
        Me.ToolStripItemExtender.SetTipText(Me.LaunchTheApplicationMenuItem, "Launches the Application.")
        '
        'CopyDownloadURLMenuItem
        '
        Me.CopyDownloadURLMenuItem.Image = Global.StaxRip.My.Resources.Resources.Copy16
        Me.CopyDownloadURLMenuItem.Name = "CopyDownloadURLMenuItem"
        Me.CopyDownloadURLMenuItem.Size = New System.Drawing.Size(289, 22)
        Me.CopyDownloadURLMenuItem.Text = "Copy Download URL"
        Me.ToolStripItemExtender.SetTipText(Me.CopyDownloadURLMenuItem, "Copies the download URL to the clipboard.")
        '
        'CopyWebsiteURLMenuItem
        '
        Me.CopyWebsiteURLMenuItem.Image = Global.StaxRip.My.Resources.Resources.Copy16
        Me.CopyWebsiteURLMenuItem.Name = "CopyWebsiteURLMenuItem"
        Me.CopyWebsiteURLMenuItem.Size = New System.Drawing.Size(289, 22)
        Me.CopyWebsiteURLMenuItem.Text = "Copy Website URL"
        Me.ToolStripItemExtender.SetTipText(Me.CopyWebsiteURLMenuItem, "Copies the website URL to the clipboard.")
        '
        'ChangeDownloadDirectoryToolStripMenuItem
        '
        Me.ChangeDownloadDirectoryToolStripMenuItem.Name = "ChangeDownloadDirectoryToolStripMenuItem"
        Me.ChangeDownloadDirectoryToolStripMenuItem.Size = New System.Drawing.Size(231, 22)
        Me.ChangeDownloadDirectoryToolStripMenuItem.Text = "Change Download Directory..."
        '
        'tsbHelp
        '
        Me.tsbHelp.AutoToolTip = False
        Me.tsbHelp.Image = Global.StaxRip.My.Resources.Resources.Help16
        Me.tsbHelp.ImageTransparentColor = System.Drawing.Color.Magenta
        Me.ToolStripItemExtender.SetKeyData(Me.tsbHelp, System.Windows.Forms.Keys.F1)
        Me.tsbHelp.Name = "tsbHelp"
        Me.tsbHelp.Size = New System.Drawing.Size(48, 22)
        Me.tsbHelp.Text = "Help"
        '
        'ToolStripItemExtender
        '
        Me.ToolStripItemExtender.Owner = Me
        '
        'ExternalApplicationsForm
        '
        Me.CancelButton = Me.bClose
        Me.ClientSize = New System.Drawing.Size(561, 406)
        Me.Controls.Add(Me.StatusControl)
        Me.Controls.Add(Me.tv)
        Me.Controls.Add(Me.bClose)
        Me.Controls.Add(Me.ToolStrip)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
        Me.KeyPreview = True
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "ExternalApplicationsForm"
        Me.ShowInTaskbar = False
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "External Applications"
        CType(Me.TipProvider, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ToolStrip.ResumeLayout(False)
        Me.ToolStrip.PerformLayout()
        CType(Me.ToolStripItemExtender, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub

#End Region

    Public ActivePackage As Package

    Private LargeIcons As New Hashtable
    Private Directories As New Hashtable
    Private Icons As New Dictionary(Of String, Icon)
    Private Nodes As New List(Of TreeNode)
    Private WebsiteNotAvailableMessage As String = "The website is not available for this application." + vbCrLf + "Please read the description for further information."
    Private ShowCheckMessage As Boolean

    Public Sub New()
        MyBase.New()
        InitializeComponent()

        Dim il As New ImageList

        il.Images.Add("folder", My.Resources.Folder16_8Bit)
        il.Images.Add("folderopen", My.Resources.FolderOpen16_8Bit)
        il.Images.Add("right", My.Resources.Right16_8Bit)
        il.Images.Add("transparent", My.Resources.Transparent16_8Bit)

        tv.ImageList = il

        tv.ImageKey = "transparent"
        tv.SelectedImageKey = "right"

        For Each i As Package In Paths.Packages.Values
            Dim n As TreeNode = TreeViewHelp.AddNode(i.TreeFolder + "/" + i.Name, tv)
            Nodes.Add(n)
            n.Tag = i
        Next

        For Each i As TreeNode In tv.Nodes
            i.ImageIndex = 0
            i.SelectedImageIndex = 0
        Next

#If DEBUG Then
        Dim mi As New ToolStripMenuItem
        mi.Text = "Debug: Copy Date Code"
        AddHandler mi.Click, AddressOf DebugCopyDateCodeClick
        ToolsToolStripDropDownButton.DropDownItems.Add(mi)
#End If
    End Sub

    Private Sub DebugCopyDateCodeClick(ByVal sender As Object, ByVal e As EventArgs)
        Dim dt As DateTime = File.GetLastWriteTimeUtc(ActivePackage.GetPath(False))
        Clipboard.SetDataObject("New DateTime(" + dt.Year.ToString + ", " + dt.Month.ToString + ", " + dt.Day.ToString + ")", True)
    End Sub

    Public Sub ShowPackage(ByVal package As Package)
        For Each i As TreeNode In TreeViewHelp.GetNodes(tv.Nodes)
            If package Is i.Tag Then
                tv.SelectedNode = i
            End If
        Next
    End Sub

    Private Sub ShowPackage(ByVal tn As TreeNode)
        If Not tn Is Nothing AndAlso Not tn.Tag Is Nothing Then
            Dim newPackage As Package = DirectCast(tn.Tag, Package)

            If Not newPackage Is ActivePackage Then
                ActivePackage = newPackage
                ShowActivePackage()
            End If
        End If
    End Sub

    Public Sub ShowActivePackage()
        ShowWarningMenuItem.Checked = ActivePackage.ShowWrongVersionWarning
        SelectionToolStripMenuItem.Text = ActivePackage.Name
        LaunchTheApplicationMenuItem.Visible = ActivePackage.Filename.ToUpper.EndsWith(".EXE")

        s.StringDictionary("RecentExternalApplicationControl") = ActivePackage.Name
        StatusControl.Items.Clear()

        'does GetPath(True) and FindPath if path is empty
        Dim status As String = ActivePackage.GetErrorStatus
        Dim path As String = ActivePackage.GetPath(False)

        If Not path Is Nothing AndAlso Not Icons.ContainsKey(path) Then
            If File.Exists(path) Then
                Try 'crashed by a user for unknown reason
                    Icons(path) = Win32Help.GetLargeIcon(path)
                Catch
                    Icons(path) = Nothing
                End Try
            Else
                Icons(path) = Nothing
            End If
        End If

        If path Is Nothing Then
            StatusControl.AddIconTitle(ActivePackage.Name, Nothing)
        Else
            StatusControl.AddIconTitle(ActivePackage.Name, Icons(path))
        End If

        If status = "" Then
            If ActivePackage.IsRequired Then
                If ActivePackage.Version <> "" AndAlso Not ActivePackage.IsCorrectVersion() Then
                    StatusControl.AddSection("Status", "Unknown version, please click on 'Download' to install the required version.")
                Else
                    StatusControl.AddSection("Status", "OK")
                End If
            Else
                If ActivePackage.Version <> "" AndAlso Not ActivePackage.IsCorrectVersion() Then
                    StatusControl.AddSection("Status", "Unknown version though the current project might not require this application. Please click on 'Download' if you want to install the application anyway.")
                Else
                    StatusControl.AddSection("Status", "OK though the current project might not require this application.")
                End If
            End If
        Else
            If ActivePackage.IsRequired Then
                StatusControl.AddSection("Status", status, Color.Red)
            Else
                StatusControl.AddSection("Status", status)
            End If
        End If

        If path <> "" Then
            StatusControl.AddSection("Path", path)
        End If

        If ActivePackage.Version <> "" Then
            StatusControl.AddSection("Version", ActivePackage.Version)
        End If

        If ActivePackage.Website <> "" Then
            StatusControl.AddSection("Web", ActivePackage.Website)
        End If

        StatusControl.AddSection("Description", ActivePackage.Description)
        StatusControl.Refresh()
    End Sub

    Private Sub DownloadWithBrowserMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles DownloadWithBrowserMenuItem.Click
        If Not ActivePackage.DownLoadURL Is Nothing Then
            Process.Start(ActivePackage.DownLoadURL)
        Else
            ShowManualDownLoadMessage()
        End If
    End Sub

    Private Sub OpenWebsiteInBrowserMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles OpenWebsiteInBrowserMenuItem.Click
        If ActivePackage.Website Is Nothing Then
            Msg.Show(WebsiteNotAvailableMessage, "Not Available", MsgIcon.Warning)
        Else
            Process.Start(ActivePackage.Website)
        End If
    End Sub

    Private Sub LaunchTheApplicationMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles LaunchTheApplicationMenuItem.Click
        Try
            Process.Start(ActivePackage.GetPath(False))
        Catch ex As Exception
            Msg.Show("The application could not be launched.", MsgIcon.Error)
        End Try
    End Sub

    Private Sub OpenDirectoryOfApplicationMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles OpenDirectoryOfApplicationMenuItem.Click
        Try
            Process.Start(Filepath.GetDir(ActivePackage.GetPath(False)))
        Catch ex As Exception
            Msg.Show("The directory could not be opened.", MsgIcon.Error)
        End Try
    End Sub

    Private Sub PathsForm_Activated(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Activated
        ShowActivePackage()

        If ShowCheckMessage Then
            tsbCheck.PerformClick()
        End If
    End Sub

    Private Sub CopyDownloadURLMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles CopyDownloadURLMenuItem.Click
        If Not ActivePackage.DownLoadURL Is Nothing Then
            Clipboard.SetDataObject(ActivePackage.DownLoadURL, True)
        Else
            ShowManualDownLoadMessage()
        End If
    End Sub

    Private Sub CopyWebsiteURLMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles CopyWebsiteURLMenuItem.Click
        If Not ActivePackage.Website Is Nothing Then
            Clipboard.SetText(ActivePackage.Website, TextDataFormat.Text)
        Else
            Msg.Show(WebsiteNotAvailableMessage, "Not Available", MsgIcon.Warning)
        End If
    End Sub

    Private Sub ExternalApplicationsForm_ActivatedFirstTime() Handles Me.ActivatedFirstTime
        If Not s.StringList.Contains(Me.GetType.FullName) Then
            tsbHelp.PerformClick()
            s.StringList.Add(Me.GetType.FullName)
        End If
    End Sub

    Private Sub ShowWarningMenuItem_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ShowWarningMenuItem.CheckedChanged
        ActivePackage.ShowWrongVersionWarning = ShowWarningMenuItem.Checked
    End Sub

    Private Sub tsbSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsbSearch.Click
        Dim f As New SearchPackageForm(ActivePackage)
        f.ShowDialog()
        f.Dispose()
    End Sub

    Private Sub tsbDownload_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsbDownload.Click
        If Not ActivePackage.DownLoadURL Is Nothing Then
            Using f As New DownloadForm(ActivePackage)
                ShowCheckMessage = True
                f.ShowDialog()
            End Using
        Else
            ShowManualDownLoadMessage()
        End If
    End Sub

    Private Sub ShowManualDownLoadMessage()
        Dim m As New Msg
        m.Title = "Not Available"
        m.Text = "Please download and install this application manually."
        m.Icon = MsgIcon.Warning
        m.AddButton("Website", Windows.Forms.DialogResult.OK)
        m.AddButton("Cancel", Windows.Forms.DialogResult.Cancel)

        If m.Show = Windows.Forms.DialogResult.OK Then
            OpenWebsiteInBrowserMenuItem.PerformClick()
        End If
    End Sub

    Private Sub tsbHelp_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsbHelp.Click
        Dim f As New HelpForm()
        f.Document.WriteStart(Text)
        f.Document.WriteElement("p", "For the most tasks StaxRip requires external applications. The External Applications dialog provides features to manage the external applications. The **Download** button downloads and installs the selected application.")
        f.Document.WriteElement("p", "Applications already installed manually can be integrated using the **Search** option (StaxRip needs to know the location of the application). What applications are required depends on the used settings.")
        f.Document.WriteElement("p", "The **Check** button checks if all required applications are installed properly. StaxRip verifies that the proper version of a application is used. Please **do not** use any other versions. Applications not required for the current settings don't have to be installed.")
        f.Document.WriteTips(ToolStripItemExtender.GetTips)
        f.Document.WriteTable("Shortcut Keys", ToolStripItemExtender.GetKeys, False)
        f.Show()
    End Sub

    Private Sub tv_AfterCollapse(ByVal sender As Object, ByVal e As TreeViewEventArgs) Handles tv.AfterCollapse
        e.Node.ImageKey = "folder"
        e.Node.SelectedImageKey = "folder"
    End Sub

    Private Sub tv_AfterExpand(ByVal sender As Object, ByVal e As TreeViewEventArgs) Handles tv.AfterExpand
        e.Node.ImageKey = "folderopen"
        e.Node.SelectedImageKey = "folderopen"
    End Sub

    Private Sub tv_AfterSelect(ByVal sender As Object, ByVal e As TreeViewEventArgs) Handles tv.AfterSelect
        If e.Node.Parent Is Nothing Then
            tv.SelectedNode = e.Node.Nodes(0)
        Else
            If Not e.Node.Tag Is Nothing Then
                ShowPackage(e.Node)
            End If
        End If
    End Sub

    Private Sub tsbCheck_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsbCheck.Click
        ShowCheckMessage = False

        Dim dic As New Dictionary(Of Package, String)

        For Each i As Package In Paths.Packages.Values
            Dim status As String = i.GetErrorStatus

            If status = "" Then
                If i.IsRequired Then
                    If i.Version <> "" AndAlso Not i.IsCorrectVersion() Then
                        dic(i) = "Unknown version"
                    End If
                End If
            Else
                If i.IsRequired Then
                    dic(i) = status
                End If
            End If
        Next

        If dic.Count > 0 Then
            Dim m As New Msg
            m.Icon = MsgIcon.Warning

            Dim message As String = ""

            For Each i As Package In dic.Keys
                If message = "" Then
                    message = i.Name + ": " + dic(i)
                Else
                    message += vbCrLf + vbCrLf + i.Name + ": " + dic(i)
                End If

                If m.ButtonList.Count < 6 Then
                    m.AddButton(i.Name, Windows.Forms.DialogResult.Ignore).Tag = i
                End If
            Next

            m.Text = dic.Count.ToString + " problematic applications detected:" + vbCrLf + vbCrLf + message
            m.Show()

            If Not m.PressedButton Is Nothing Then
                ShowPackage(DirectCast(m.PressedButton.Tag, Package))
            End If
        Else
            Msg.Show("All required applications are installed properly.", MsgIcon.Info)
        End If
    End Sub

    Private Sub ChangeDownloadDirectoryToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ChangeDownloadDirectoryToolStripMenuItem.Click
        g.MainForm.OpenSettingsDialog("External Applications")
    End Sub

    Private Sub tsbBrowse_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsbBrowse.Click
        Dim f As New OpenFileDialog
        f.Filter = ActivePackage.Filename + "|" + ActivePackage.Filename

        If f.ShowDialog = Windows.Forms.DialogResult.OK Then
            ActivePackage.SetPath(f.FileName)
            ShowActivePackage()
        End If
    End Sub
End Class