-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclsCFun.vb
548 lines (429 loc) · 19.6 KB
/
clsCFun.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
Imports System.Math
Imports System.IO
Imports System.Threading
Imports System.Data.SQLite
Imports System.Drawing
Imports System.Drawing.Imaging
Public Class cfun
Public Enum RecDateTimeType
RecDate
RecTime
End Enum
Dim threadSound As Thread
Shared Sub CheckGazDB()
'Check that there are records in the gazetteer DB
Dim db As clsDB = New clsDB
Dim com As SQLiteCommand
com = New SQLiteCommand("Select Count(*) from os50kgaz", db.conGazetteer)
If com.ExecuteScalar() = 0 Then
MessageBox.Show("You haven't set up your gazetteer yet, so grid references cannot be matched to location names. See how to set up the gazetteer on the quick-start guide in Help.")
End If
com.Dispose()
db.Dispose()
End Sub
Shared Function UTC2LocalTime(ByVal utcDateTime As DateTime, ByVal dtType As RecDateTimeType) As String
'use cst.StandardName to populate timezone in records table
Dim localDateTime As DateTime = utcDateTime.ToLocalTime()
If dtType = RecDateTimeType.RecDate Then
Return (Format(localDateTime, "dd/MM/yyyy"))
Else
Return (Format(localDateTime, "HH:mm"))
End If
End Function
Shared Function GetGridRef(ByVal dblLon As Double, ByVal dblLat As Double, ByVal intRes As Integer)
Dim dblNorthing As Double
Dim dblEasting As Double
Dim strGR As String = ""
Dim objGridRef As GridRef = New GridRef
objGridRef.MakePrefixArrays()
dblNorthing = objGridRef.LatWGS842Northing(dblLat, dblLon, 100)
dblEasting = objGridRef.LongWGS842Easting(dblLat, dblLon, 100)
Select Case intRes
Case 10000
strGR = objGridRef.EN2Hectad(dblEasting, dblNorthing)
Case 2000
strGR = objGridRef.EN2Tetrad(dblEasting, dblNorthing)
Case 1000
strGR = objGridRef.EN2Monad(dblEasting, dblNorthing)
Case 100
strGR = objGridRef.EN26fig(dblEasting, dblNorthing)
Case 10
strGR = objGridRef.EN28fig(dblEasting, dblNorthing)
Case 1
strGR = objGridRef.EN210fig(dblEasting, dblNorthing)
End Select
Return strGR
End Function
Shared Function GetByteArrayFromWav(ByVal strWavFile As String) As Byte()
strWavFile = strWavFile.Replace(Chr(0), "")
Dim arr() As Byte
'Resize array so that it can accomodate the file
ReDim arr(FileLen(strWavFile) - 1)
FileOpen(1, strWavFile, OpenMode.Binary, OpenAccess.Read, OpenShare.Shared)
FileGet(1, arr)
FileClose(1)
Return arr
End Function
Shared Sub WriteWAVfromByteArray(ByVal strFile As String, ByVal data() As Byte)
Dim fStream As New FileStream(strFile, FileMode.Create)
Dim bw As New BinaryWriter(fStream)
bw.Write(data)
bw.Close()
fStream.Close()
End Sub
Shared Function GetByteArraySoundResource(ByVal strm As IO.Stream) As Byte()
Dim arr(CType(strm.Length, Integer)) As Byte
strm.Read(arr, 0, Int(CType(strm.Length, Integer)))
Return arr
End Function
Shared Function getVoiceFolder(ByVal bCreate As Boolean) As String
If File.Exists(frmOptions.txtDB.Text) Then
Dim strDBFolder As String = Path.GetDirectoryName(frmOptions.txtDB.Text)
Dim strDBName As String = Path.GetFileName(frmOptions.txtDB.Text)
Dim strVoiceFolderName As String = strDBName.Substring(0, strDBName.Length - 3) & "voice"
Dim strVoiceFolder = Path.Combine(strDBFolder, strVoiceFolderName)
'If the root folder doesn't exist, then create it
If Not Directory.Exists(strVoiceFolder) And bCreate Then
Directory.CreateDirectory(strVoiceFolder)
End If
Return strVoiceFolder
Else
Return ""
End If
End Function
Shared Function getSoundFolder(ByVal bCreate As Boolean) As String
If File.Exists(frmOptions.txtDB.Text) Then
Dim strDBFolder As String = Path.GetDirectoryName(frmOptions.txtDB.Text)
Dim strDBName As String = Path.GetFileName(frmOptions.txtDB.Text)
Dim strSoundFolderName As String = strDBName.Substring(0, strDBName.Length - 3) & "sound"
Dim strSoundFolder = Path.Combine(strDBFolder, strSoundFolderName)
'If the root folder doesn't exist, then create it
If Not Directory.Exists(strSoundFolder) And bCreate Then
Directory.CreateDirectory(strSoundFolder)
End If
Return strSoundFolder
Else
Return ""
End If
End Function
Shared Function getWavFolder(ByVal intID As Integer, ByVal bCreate As Boolean) As String
Dim strVoiceFolder As String = getVoiceFolder(bCreate)
Dim strVoiceSubFolder As String
If intID < 0 Then
strVoiceSubFolder = Path.Combine(strVoiceFolder, "TempVoice")
If Not Directory.Exists(strVoiceSubFolder) And bCreate Then
Directory.CreateDirectory(strVoiceSubFolder)
End If
Else
'Get the relevant 1k folder
Dim intOneThousand As Integer = (intID \ 1000) * 1000
Dim strOneThousandFolder As String = intOneThousand.ToString() & "-" & (intOneThousand + 999).ToString()
strVoiceSubFolder = Path.Combine(strVoiceFolder, strOneThousandFolder)
If Not Directory.Exists(strVoiceSubFolder) And bCreate Then
Directory.CreateDirectory(strVoiceSubFolder)
End If
'Get the relevant 100 folder
Dim intOneHundred As Integer = (intID \ 100) * 100
Dim strOneHundredFolder As String = intOneHundred.ToString() & "-" & (intOneHundred + 99).ToString()
strVoiceSubFolder = Path.Combine(strVoiceSubFolder, strOneHundredFolder)
If Not Directory.Exists(strVoiceSubFolder) And bCreate Then
Directory.CreateDirectory(strVoiceSubFolder)
End If
End If
Return strVoiceSubFolder
End Function
Shared Function getWavFolderV2(ByVal intID As Integer, ByVal bCreate As Boolean) As String
Dim strSoundFolder As String = getSoundFolder(bCreate)
Dim strSoundSubFolder As String
If intID < 0 Then
strSoundSubFolder = Path.Combine(strSoundFolder, "TempSound")
If Not Directory.Exists(strSoundSubFolder) And bCreate Then
Directory.CreateDirectory(strSoundSubFolder)
End If
Else
'Get the relevant 1k folder
Dim intOneThousand As Integer = (intID \ 1000) * 1000
Dim strOneThousandFolder As String = intOneThousand.ToString() & "-" & (intOneThousand + 999).ToString()
strSoundSubFolder = Path.Combine(strSoundFolder, strOneThousandFolder)
If Not Directory.Exists(strSoundSubFolder) And bCreate Then
Directory.CreateDirectory(strSoundSubFolder)
End If
'Get the relevant 100 folder
Dim intOneHundred As Integer = (intID \ 100) * 100
Dim strOneHundredFolder As String = intOneHundred.ToString() & "-" & (intOneHundred + 99).ToString()
strSoundSubFolder = Path.Combine(strSoundSubFolder, strOneHundredFolder)
If Not Directory.Exists(strSoundSubFolder) And bCreate Then
Directory.CreateDirectory(strSoundSubFolder)
End If
End If
Return strSoundSubFolder
End Function
Shared Sub SaveRecordWav(ByRef arr() As Byte, ByVal intID As Integer, ByVal intSeq As Integer)
Dim strVoiceSubFolder = getWavFolder(intID, True)
WriteWAVfromByteArray(Path.Combine(strVoiceSubFolder, intID.ToString() & "-" & intSeq.ToString() & ".wav"), arr)
End Sub
Shared Sub SaveRecordWavFile(ByRef strWav As String, ByVal intID As Integer, ByVal intSeq As Integer)
Dim strVoiceSubFolder = getWavFolder(intID, True)
File.Copy(strWav, Path.Combine(strVoiceSubFolder, intID.ToString() & "-" & intSeq.ToString() & ".wav"), True)
End Sub
Shared Sub CopyWavFiles(ByVal intFromID As Integer, ByVal intToID As Integer)
Dim strFromVoiceSubFolder As String = getWavFolder(intFromID, False)
If Not Directory.Exists(strFromVoiceSubFolder) Then
Exit Sub
End If
Dim wavCopyFiles() As String = Directory.GetFiles(strFromVoiceSubFolder, intFromID.ToString() & "-*.wav")
If wavCopyFiles.Length = 0 Then
Exit Sub
End If
Dim strToVoiceSubFolder As String = getWavFolder(intToID, True)
Dim wavCurrentFiles() As String = Directory.GetFiles(strToVoiceSubFolder, intToID.ToString() & "-*.wav")
Dim intSuffix = wavCurrentFiles.Length
Dim strWav As String
For Each strWav In wavCopyFiles
intSuffix = intSuffix + 1
File.Copy(strWav, Path.Combine(strToVoiceSubFolder, intToID.ToString() & "-" & intSuffix.ToString & ".wav"))
Next
End Sub
'Shared Sub PlayWavForID(ByVal intID As Integer)
' Dim strVoiceFolder As String = getVoiceFolder(True)
' Dim strVoiceSubFolder As String = getWavFolder(intID, False)
' Dim wavFiles() As String
' If Not Directory.Exists(strVoiceSubFolder) Then
' Exit Sub
' End If
' wavFiles = Directory.GetFiles(strVoiceSubFolder, intID.ToString() & "-*.wav")
' If wavFiles.Length = 0 Then
' 'Does 'no voice' sound file exist in root voice folder? If so return it,
' 'otherwise create it.
' If Not File.Exists(Path.Combine(strVoiceFolder, "no-voice.wav")) Then
' WriteWAVfromByteArray(Path.Combine(strVoiceFolder, "no-voice.wav"), cfun.GetByteArraySoundResource(My.Resources.ChangeMetadata))
' End If
' My.Computer.Audio.Play(Path.Combine(strVoiceFolder, "no-voice.wav"), AudioPlayMode.Background)
' Else
' Dim snd As clsSound = New clsSound()
' snd.SetWavFiles(wavFiles)
' Dim t As Thread
' t = New Thread(AddressOf snd.PlayWavs)
' t.IsBackground = True
' t.Start()
' End If
'End Sub
Public Sub PlayWavForIDV2(ByVal intID As Integer)
Dim db As clsDB = New clsDB
'Get all the voice WAVs associated with this record
Dim strSQL As String = "select [order], sound, OriginalFilename from RecordSounds, Sounds where RecordID = " & intID.ToString & " and RecordSounds.SoundID = Sounds.SoundID order by [order]"
Dim daSounds As SQLiteDataAdapter
Dim dtSounds As DataTable = New DataTable()
daSounds = New SQLiteDataAdapter(strSQL, db.conVoice)
Try
daSounds.Fill(dtSounds)
Catch ex As Exception
MessageBox.Show("Record selection query failed: " & ex.Message)
Exit Sub
End Try
Dim dtRow As DataRow
Dim arr() As Byte
Dim strFilename As String
Dim strTmpFile As String
Dim wavFiles(dtSounds.Rows.Count - 1) As String
Dim iWav As Integer = 0
For Each dtRow In dtSounds.Rows
arr = dtRow("sound")
strFilename = dtRow("OriginalFilename")
'Playing sound directly from a Byte array - Audio.Play(arr, AudioPlayMode.Background)
'can cause unpredictable weird sounds and crashes (happened both when data stored
'in Access DB and SQLite). So we write out temp files and play them instead.
strTmpFile = Path.GetTempFileName()
strTmpFile = strTmpFile.Substring(0, Len(strTmpFile) - 4) & Path.GetExtension(strFilename)
cfun.WriteWAVfromByteArray(strTmpFile, arr)
wavFiles(iWav) = strTmpFile
iWav += 1
Next
'If there is already an active thread playing sound files, then abort it.
If Not threadSound Is Nothing Then
If threadSound.IsAlive Then
threadSound.Abort()
End If
End If
'The temp wav files are actually played by a background process so that control can
'return to the application.
Dim snd As clsSound = New clsSound()
snd.SetWavFiles(wavFiles)
threadSound = New Thread(AddressOf snd.PlayWavs)
threadSound.SetApartmentState(ApartmentState.STA)
threadSound.IsBackground = True
threadSound.Start()
'var t = new Thread(MyThreadStartMethod);
't.SetApartmentState(ApartmentState.STA);
't.Start();
End Sub
Shared Sub DeleteWavsForID(ByVal intID As Integer)
Dim strVoiceSubFolder As String = getWavFolder(intID, False)
If Not Directory.Exists(strVoiceSubFolder) Then
Exit Sub
End If
Dim wavFiles() As String = Directory.GetFiles(strVoiceSubFolder, intID.ToString() & "-*.wav")
Dim wavFile As String
For Each wavFile In wavFiles
File.Delete(wavFile)
Next
End Sub
Shared Sub DeleteTmpWavs()
Dim strVoiceFolder As String = getVoiceFolder(True)
Dim strVoiceSubFolder As String
strVoiceSubFolder = Path.Combine(strVoiceFolder, "TempVoice")
If Not Directory.Exists(strVoiceSubFolder) Then
Exit Sub
End If
Dim wavFiles() As String = Directory.GetFiles(strVoiceSubFolder, "*.wav")
Dim wavFile As String
For Each wavFile In wavFiles
File.Delete(wavFile)
Next
End Sub
Shared Sub DeleteTmpWavsV2()
Dim strSoundFolder As String = getSoundFolder(True)
Dim strSoundSubFolder As String
'Empty the TmpRecordSounds datatable
'frmMain.dtTmpRecordSounds.Rows.Clear()
'Delete the temporary WAV files
strSoundSubFolder = Path.Combine(strSoundFolder, "TempVoice")
If Not Directory.Exists(strSoundSubFolder) Then
Exit Sub
End If
Dim wavFiles() As String = Directory.GetFiles(strSoundSubFolder, "*.wav")
Dim wavFile As String
For Each wavFile In wavFiles
File.Delete(wavFile)
Next
End Sub
Shared Function GetByteArrayFromImg(ByVal img) As Byte()
Dim imgStream As MemoryStream = New MemoryStream()
img.Save(imgStream, Imaging.ImageFormat.Png)
imgStream.Close()
Dim arr As Byte() = imgStream.ToArray()
imgStream.Dispose()
Return arr
End Function
Shared Function GetImageFromByteArray(ByVal arr As Byte()) As Image
Dim ms As MemoryStream = New MemoryStream(arr)
Dim img As Image = Drawing.Image.FromStream(ms)
Return img
End Function
Shared Function ThumbFromImage(ByVal img As Image, ByVal iMax As Integer) As Image
Dim tw As Integer, th As Integer, tx As Integer, ty As Integer
Dim w As Integer = img.Width
Dim h As Integer = img.Height
Dim whRatio As Double = CDbl(w) / h
If img.Width >= img.Height Then
tw = iMax
th = CInt(Math.Truncate(tw / whRatio))
Else
th = iMax
tw = CInt(Math.Truncate(th * whRatio))
End If
tx = (iMax - tw) \ 2
ty = (iMax - th) \ 2
Dim thumb As New Bitmap(iMax, iMax, PixelFormat.Format24bppRgb)
Dim g As Graphics = Graphics.FromImage(thumb)
g.Clear(Color.White)
g.InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor
g.DrawImage(img, New Rectangle(tx, ty, tw, th), New Rectangle(0, 0, w, h), GraphicsUnit.Pixel)
Return thumb
End Function
Shared Function GetRelativePath(ByVal strPathName As String) As String
Dim strDB As String = frmOptions.txtDBFolder.Text.ToLower()
Dim strRelPath As String = ""
If strPathName.ToLower.StartsWith(strDB) Then
strRelPath = strPathName.Substring(strDB.Length)
If strRelPath.StartsWith("\") Then
strRelPath = strRelPath.Substring(1)
End If
Else
strRelPath = ""
End If
Return strRelPath
End Function
Shared Function GetHeadlineFromText(ByVal strText As String) As String
Dim strHeadline As String = ""
Dim intHeadlineLength As Integer = 50
If strText = "" Then
Return ""
End If
'If there is a full stop or exclamation mark in string, then this is the endpoint
Dim intStop As Integer = strText.IndexOf(".")
Dim intExclamation As Integer = strText.IndexOf("!")
Dim intCut As Integer = -1
If intStop > -1 And intExclamation > -1 Then
intCut = Math.Min(intStop, intExclamation)
ElseIf intStop > -1 Then
intCut = intStop
ElseIf intExclamation > -1 Then
intCut = intExclamation
End If
If intCut > -1 Then
strHeadline = strText.Substring(0, intCut)
Else
strHeadline = strText
End If
If strHeadline.Length > intHeadlineLength Then
strHeadline = strHeadline.Substring(0, intHeadlineLength)
End If
Return strHeadline
End Function
Shared Sub AddMediaFile(ByVal strFilename As String, ByVal intRecID As Integer, ByVal intOrder As Integer)
Dim img As Image = Nothing
Try
img = Image.FromFile(strFilename)
Catch ex As Exception
End Try
If img Is Nothing Then
Exit Sub
End If
Dim info As FileInfo = New FileInfo(strFilename)
'Add the image to the media table
'(Should probably check here that it's not already there)
Dim db As clsDB = New clsDB
Dim comMedia As SQLiteCommand = New SQLiteCommand(db.conMedia)
comMedia.CommandText = "insert into Media(FileName, FullPath, RelativePath, DateCreated, Comment, FileType, GenericFileType, Thumbnail) values(?,?,?,?,?,?,?,?)"
comMedia.Parameters.AddWithValue("FileName", strFilename)
comMedia.Parameters.AddWithValue("FullPath", strFilename)
comMedia.Parameters.AddWithValue("RelativePath", cfun.GetRelativePath(strFilename))
comMedia.Parameters.AddWithValue("DateCreated", info.CreationTime)
comMedia.Parameters.AddWithValue("Comment", "")
comMedia.Parameters.AddWithValue("FileType", strFilename.Substring(strFilename.Length - 3))
comMedia.Parameters.AddWithValue("GenericFileType", "image")
comMedia.Parameters.AddWithValue("Thumbnail", cfun.GetByteArrayFromImg(cfun.ThumbFromImage(img, 120)))
comMedia.ExecuteNonQuery()
'Add a reference in the RecordsMedia table
comMedia.CommandText = "Select last_insert_rowid()"
Dim intMediaID As Integer = comMedia.ExecuteScalar()
comMedia.Parameters.Clear()
comMedia.CommandText = "insert into RecordMedia (RecordID, FileID, [Order]) values(?,?,?);"
comMedia.Parameters.AddWithValue("RecordID", intRecID)
comMedia.Parameters.AddWithValue("FileID", intMediaID)
comMedia.Parameters.AddWithValue("Order", intOrder)
comMedia.ExecuteNonQuery()
End Sub
Shared Function HasNoValue(ByVal obj As Object) As Boolean
If IsDBNull(obj) Then
Return True
ElseIf obj Is Nothing Then
Return True
ElseIf obj.ToString().ToLower = "null" Then
'This may have been used early on
Return True
ElseIf obj.ToString.Trim = "" Then
Return True
Else
Return False
End If
End Function
Shared Function NullToEmpty(ByVal objValue As Object) As Object
If HasNoValue(objValue) Then
Return ""
Else
Return objValue
End If
End Function
End Class