Skip to content

Commit c588dfb

Browse files
committed
Add saving and loading XML files for contacts and settings.
1 parent f69b1da commit c588dfb

File tree

7 files changed

+104
-12
lines changed

7 files changed

+104
-12
lines changed

C-ALM-VOIP/C-ALM-VOIP/AddrBook/AddressableBase.vb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Public MustInherit Class AddressableBase
4242
_name = value
4343
End Set
4444
End Property
45-
45+
<Xml.Serialization.XmlIgnore>
4646
Public Overridable Property targetAddress As String
4747
Get
4848
Return _targaddress
@@ -51,7 +51,7 @@ Public MustInherit Class AddressableBase
5151
_targaddress = value
5252
End Set
5353
End Property
54-
54+
<Xml.Serialization.XmlIgnore>
5555
Public Overridable Property targetPort As Integer
5656
Get
5757
Return _targport
@@ -98,7 +98,7 @@ Public MustInherit Class AddressableBase
9898
_advport = value
9999
End Set
100100
End Property
101-
101+
<Xml.Serialization.XmlIgnore>
102102
Public Overridable Property type As AddressableType
103103
Get
104104
Return _type
@@ -107,7 +107,7 @@ Public MustInherit Class AddressableBase
107107
_type = value
108108
End Set
109109
End Property
110-
110+
<Xml.Serialization.XmlIgnore>
111111
Public Overridable Property targetIPVersion As IPVersion
112112
Get
113113
Return _targver

C-ALM-VOIP/C-ALM-VOIP/AddrBook/Contact.vb

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22
Public Class Contact
33
Inherits AddressableBase
44

5+
Public Sub New()
6+
MyBase.New("", 0, IPVersion.None, AddressableType.None)
7+
End Sub
8+
59
Public Sub New(other As AddressableBase)
610
MyBase.New(other)
711
End Sub
812

913
Public Sub New(targAddress As String, targPort As Integer, targVer As IPVersion, aType As AddressableType)
1014
MyBase.New(targAddress, targPort, targVer, aType)
1115
End Sub
12-
16+
<Xml.Serialization.XmlElement(ElementName:="targetAddress")>
1317
Public Overloads Property targetAddress As String
1418
Get
1519
Return MyBase.targetAddress
@@ -18,7 +22,7 @@ Public Class Contact
1822
MyBase.targetAddress = value
1923
End Set
2024
End Property
21-
25+
<Xml.Serialization.XmlElement(ElementName:="targetPort")>
2226
Public Overloads Property targetPort As Integer
2327
Get
2428
Return MyBase.targetPort
@@ -27,7 +31,7 @@ Public Class Contact
2731
MyBase.targetPort = value
2832
End Set
2933
End Property
30-
34+
<Xml.Serialization.XmlElement(ElementName:="type")>
3135
Public Overloads Property type As AddressableType
3236
Get
3337
Return MyBase.type
@@ -36,7 +40,7 @@ Public Class Contact
3640
MyBase.type = value
3741
End Set
3842
End Property
39-
43+
<Xml.Serialization.XmlElement(ElementName:="targetIPVersion")>
4044
Public Overloads Property targetIPVersion As IPVersion
4145
Get
4246
Return MyBase.targetIPVersion
@@ -67,12 +71,17 @@ Public Class Contact
6771
Public Overrides Function duplicateToNew() As AddressableBase
6872
Return New Contact(Me)
6973
End Function
74+
End Class
75+
76+
<Serializable, Xml.Serialization.XmlInclude(GetType(Contact))>
77+
Public Class Contacts
78+
Public contacts As Contact() = New Contact() {}
7079

71-
Public Shared Function load(strIn As String) As Contact
72-
Return sserializer.deSerialize(Of Contact)(strIn)
80+
Public Shared Function load(strIn As String) As Contacts
81+
Return sserializer.deSerialize(Of Contacts)(strIn)
7382
End Function
7483

7584
Public Function save() As String
76-
Return sserializer.serialize(Of Contact)(Me)
85+
Return sserializer.serialize(Of Contacts)(Me)
7786
End Function
7887
End Class

C-ALM-VOIP/C-ALM-VOIP/Main.vb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@ Module Main
1414
Public Const quote As Char = """c"
1515
Public programAssembly As Assembly = Assembly.GetEntryAssembly()
1616
Public programPath As String = programAssembly.Location
17+
Public programName As String = Path.GetFileNameWithoutExtension(programPath)
1718
Public execdir As String = Path.GetDirectoryName(programPath)
1819
Public worker As WorkerPump = Nothing
1920
Public forms As New List(Of Form)
2021
Public parsers As New List(Of IEventParser)
22+
Public contactStoreLoc As String = execdir & "\" + programName & ".contacts"
23+
Public settingsStoreLoc As String = execdir & "\" + programName & ".settings"
2124

2225
Public Sub main()
2326
Try
@@ -57,6 +60,22 @@ Module Main
5760
Catch ex As IOException
5861
End Try
5962

63+
Dim args As String() = Environment.GetCommandLineArgs()
64+
If args.Length > 1 Then
65+
For i As Integer = 1 To args.Length - 1 Step 1
66+
Try
67+
If File.Exists(args(i)) Then
68+
If Path.GetExtension(args(i)) = ".contacts" Then
69+
contactStoreLoc = args(i)
70+
ElseIf Path.GetExtension(args(i)) = ".settings" Then
71+
settingsStoreLoc = args(i)
72+
End If
73+
End If
74+
Catch ex As IOException
75+
End Try
76+
Next
77+
End If
78+
6079
worker = New WorkerPump()
6180
AddHandler worker.OnPumpException, AddressOf ope
6281
forms.Add(New AboutBx(worker))

C-ALM-VOIP/C-ALM-VOIP/Utils/Global.vb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Imports captainalm.CALMNetMarshal
33
Imports System.Net.Sockets
44
Imports captainalm.Serialize
5+
Imports System.IO
56

67
Public Module [Global]
78
'Global Information
@@ -54,6 +55,43 @@ Public Module [Global]
5455
Public Function returnFirstItemOrNothing(Of t)(input As t()) As t
5556
If input Is Nothing OrElse input.Length < 1 Then Return Nothing Else Return input(0)
5657
End Function
58+
59+
Public Function loadSettings() As GlobalSettings
60+
Dim toret As New GlobalSettings()
61+
Try
62+
toret.load(File.ReadAllText(settingsStoreLoc))
63+
Catch ex As IOException
64+
toret = New GlobalSettings()
65+
End Try
66+
Return toret
67+
End Function
68+
69+
Public Sub saveSettings(setIn As GlobalSettings)
70+
If setIn Is Nothing Then Return
71+
Try
72+
File.WriteAllText(settingsStoreLoc, setIn.save())
73+
Catch ex As IOException
74+
End Try
75+
End Sub
76+
77+
Public Function loadContacts() As Contacts
78+
Dim toret As New Contacts()
79+
Try
80+
toret = Contacts.load(File.ReadAllText(contactStoreLoc))
81+
If toret Is Nothing Then toret = New Contacts()
82+
Catch ex As IOException
83+
toret = New Contacts()
84+
End Try
85+
Return toret
86+
End Function
87+
88+
Public Sub saveContacts(csIn As Contacts)
89+
If csIn Is Nothing Then Return
90+
Try
91+
File.WriteAllText(contactStoreLoc, csIn.save())
92+
Catch ex As IOException
93+
End Try
94+
End Sub
5795
End Module
5896

5997
Public Enum EditorMode As Integer

C-ALM-VOIP/C-ALM-VOIP/Utils/GlobalSettings.vb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,12 @@ Public Class GlobalSettings
6969
Public Sub load(data As String)
7070
setupGS()
7171
Dim setting As GlobalSettings = settings.gserializer.deSerialize(Of GlobalSettings)(data)
72-
takeACopy(setting)
72+
If Not setting Is Nothing Then takeACopy(setting)
7373
setting = Nothing
7474
End Sub
7575

7676
Public Sub load(toDuplicate As GlobalSettings)
77+
If toDuplicate Is Nothing Then Return
7778
takeACopy(toDuplicate)
7879
End Sub
7980

C-ALM-VOIP/C-ALM-VOIP/Utils/ListViewedRegistry.vb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,10 @@
103103
Next
104104
End SyncLock
105105
End Sub
106+
107+
Public ReadOnly Property internalList As SyncLockedList(Of t)
108+
Get
109+
Return backedList
110+
End Get
111+
End Property
106112
End Class

C-ALM-VOIP/C-ALM-VOIP/Views/MainProgram.vb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,20 @@ Public NotInheritable Class MainProgram
6262
clientreg = New ListViewedRegistry(Of Client)(ListViewcl)
6363
contactreg = New ListViewedRegistry(Of Contact)(ListViewcl2)
6464
streamreg = New ListViewedRegistry(Of Streamer)(ListViewsc)
65+
settings = loadSettings()
66+
lContacts()
67+
End Sub
68+
69+
Private Sub lContacts()
70+
Dim cs As Contact() = loadContacts().contacts
71+
contactreg.clear()
72+
For Each c As Contact In cs
73+
addContact(c)
74+
Next
75+
End Sub
76+
77+
Private Sub sContacts()
78+
saveContacts(New Contacts() With {.contacts = contactreg.internalList.toArray()})
6579
End Sub
6680

6781
Private Sub MainProgram_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
@@ -103,6 +117,7 @@ Public NotInheritable Class MainProgram
103117
While Not configfin
104118
Threading.Thread.Sleep(125)
105119
End While
120+
saveSettings(settings)
106121
End If
107122
wp.addEvent(New WorkerEvent(butrconf, New Object() {Me}, ETs.Click, e))
108123
End If
@@ -311,6 +326,8 @@ Public NotInheritable Class MainProgram
311326
'Marshal Management
312327

313328
Private Sub engage()
329+
saveSettings(settings)
330+
lContacts()
314331
micVOIP = New VOIPSender() With {.samplebuffersize = settings.samplerate * (settings.buffmdmsecs / 1000) * 2}
315332
spkVOIP = New VOIPReceiver()
316333
If Not settings.selected_interfaceIPv4.Equals(IPAddress.None) Then
@@ -379,6 +396,8 @@ Public NotInheritable Class MainProgram
379396
micVOIP = Nothing
380397
spkVOIP.Dispose()
381398
spkVOIP = Nothing
399+
settings = loadSettings()
400+
sContacts()
382401
End Sub
383402

384403
Private Function terminateMarshal(marshalIn As NetMarshalBase) As NetMarshalBase

0 commit comments

Comments
 (0)