PageRenderTime 18ms CodeModel.GetById 11ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/Source Code/PowerSong/GlobalContext.vb

#
Visual Basic | 124 lines | 84 code | 33 blank | 7 comment | 0 complexity | 125299c293b4d7780316a10deee52e19 MD5 | raw file
  1Imports System.IO
  2Imports PowerSong.SongDatabase
  3Imports PowerSong.SongDatabase.Bibles
  4Imports PowerSong.SongDatabase.Items
  5
  6Public Module GlobalContext
  7
  8    Private FMainForm As frmMain = Nothing
  9
 10    Public Property MainForm() As frmMain
 11        Get
 12            Return FMainForm
 13        End Get
 14        Friend Set(ByVal value As frmMain)
 15            FMainForm = value
 16        End Set
 17    End Property
 18
 19    Private FBibleIndex As BibleIndex = Nothing
 20
 21    Public ReadOnly Property BibleIndex() As BibleIndex
 22        Get
 23            Return FBibleIndex
 24        End Get
 25    End Property
 26
 27    Private FDatabaseLocation As String = String.Empty
 28
 29    Public Property DatabaseLocation() As String
 30        Get
 31            Return FDatabaseLocation
 32        End Get
 33        Set(ByVal value As String)
 34            FDatabaseLocation = value
 35        End Set
 36    End Property
 37
 38    Private FDatabase As Database = Nothing
 39
 40    Friend Property Database() As Database
 41        Get
 42            Return FDatabase
 43        End Get
 44        Set(ByVal value As Database)
 45            FDatabase = value
 46        End Set
 47    End Property
 48
 49    Public Sub LoadBible(ByVal translation As String)
 50
 51        Try
 52
 53            ' Close old index if one is already open
 54            If FBibleIndex IsNot Nothing Then FBibleIndex.Close()
 55
 56            ' Load new index
 57            Dim BibleFileName As String = Database.InitialContentFolder + "\" + translation + ".Bible"
 58            Dim BibleIndexFileName As String = Database.InitialContentFolder + "\" + translation + ".Index"
 59            FBibleIndex = BibleIndex.Load(BibleFileName, BibleIndexFileName)
 60
 61        Catch ex As Exception
 62            MsgBox("Bible translation '" + translation + "' could not be loaded." + Environment.NewLine + _
 63                   "Bible functionality will not be available until you select a different translation." + Environment.NewLine + Environment.NewLine + _
 64                   "Reason: " + ex.Message, MsgBoxStyle.Exclamation)
 65            FBibleIndex = Nothing
 66        End Try
 67
 68    End Sub
 69
 70    Public Sub LoadExternalFile(ByVal fileName As String)
 71
 72        Select Case Path.GetExtension(fileName).ToUpper
 73
 74            Case ".POWERSONG"
 75                Call New frmImportDatabase(Database, fileName).ShowDialog()
 76
 77            Case ".PLAYLIST"
 78                LoadPlayList(fileName)
 79
 80            Case ".PSDB"
 81                ' TODO: Close and open new database
 82                MsgBox("PowerSong is already running. Please close PowerSong before attempting to open the referenced database.", MsgBoxStyle.Exclamation)
 83
 84            Case Else
 85                MsgBox("PowerSong cannot open the file '" + Path.GetFileName(fileName) + "' because it is not recognised.")
 86
 87        End Select
 88
 89    End Sub
 90
 91    Public Sub LoadPlayList(ByVal fileName As String)
 92
 93        Try
 94
 95            If FDatabase Is Nothing Then Throw New Exception("A database has not been loaded.")
 96
 97            ' Ask the user if they want to append songs to the current list
 98            Dim RemoveSongsFirst As Boolean = False
 99            If MainForm.lstPlaylist.ItemCount > 0 Then
100                RemoveSongsFirst = Not MsgBox("Do you wish to append these songs to the list?", MsgBoxStyle.Question + MsgBoxStyle.YesNo) = MsgBoxResult.Yes
101            End If
102
103            ' Load the play list
104            Dim AllSongsFound As Boolean = True
105            Dim List As PlayList = PlayList.Load(Database, fileName, AllSongsFound)
106
107            ' Show a friendly message box if some or all of the songs are not present
108            If Not AllSongsFound Then
109                MsgBox("Some or all of the referenced songs in the file do not exist in the database.", MsgBoxStyle.Information)
110            End If
111
112            ' Insert items into the play list
113            If RemoveSongsFirst Then MainForm.lstPlaylist.Clear()
114            For Each Item As PlayListItem In List
115                MainForm.lstPlaylist.AddItem(New SongsListItem(Item.Item, Item.Style, Item.OverrideBackground))
116            Next
117
118        Catch ex As Exception
119            MsgBox("Could not load the play list:" + Environment.NewLine + ex.Message, MsgBoxStyle.Exclamation)
120        End Try
121
122    End Sub
123
124End Module