VERSION 5.00
Object = "{DC29082A-7B00-474E-8CE6-3A76BC3E33A9}#1.0#0"; "PCR300x.ocx"
Object = "{CFB094A6-8FF0-4EF7-A644-ED122CC38E57}#1.0#0"; "TaskIcon.ocx"
Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
Object = "{0ECD9B60-23AA-11D0-B351-00A0C9055D8E}#6.0#0"; "MSHFLXGD.OCX"
Begin VB.Form frmMain 
   BackColor       =   &H80000004&
   BorderStyle     =   1  'Fixed Single
   Caption         =   "WinLoq (WinSecurity Express)"
   ClientHeight    =   3180
   ClientLeft      =   45
   ClientTop       =   330
   ClientWidth     =   5295
   Icon            =   "frmMain.frx":0000
   LinkTopic       =   "Form1"
   LockControls    =   -1  'True
   MaxButton       =   0   'False
   NegotiateMenus  =   0   'False
   ScaleHeight     =   212
   ScaleMode       =   3  'Pixel
   ScaleWidth      =   353
   ShowInTaskbar   =   0   'False
   StartUpPosition =   1  'CenterOwner
   Visible         =   0   'False
   Begin VB.Timer Timer 
      Interval        =   3000
      Left            =   4620
      Top             =   2790
   End
   Begin VB.ComboBox cmbLanguage 
      Height          =   315
      Left            =   5430
      TabIndex        =   7
      Text            =   "Language"
      Top             =   3060
      Width           =   1365
   End
   Begin MSHierarchicalFlexGridLib.MSHFlexGrid fgUserList 
      Height          =   1515
      Left            =   30
      TabIndex        =   5
      Top             =   1200
      Width           =   5220
      _ExtentX        =   9208
      _ExtentY        =   2672
      _Version        =   393216
      Cols            =   4
      ScrollBars      =   2
      SelectionMode   =   1
      AllowUserResizing=   1
      PictureType     =   1
      RowSizingMode   =   1
      _NumberOfBands  =   1
      _Band(0).Cols   =   4
   End
   Begin MSComDlg.CommonDialog dlgFile 
      Left            =   6600
      Top             =   1080
      _ExtentX        =   847
      _ExtentY        =   847
      _Version        =   393216
   End
   Begin TASKICONLib.TaskIcon TaskIcon 
      Left            =   7440
      Top             =   1200
      _Version        =   65536
      _ExtentX        =   847
      _ExtentY        =   847
      _StockProps     =   4
      BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
         Name            =   "Arial"
         Size            =   9.75
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   -1  'True
         Strikethrough   =   0   'False
      EndProperty
      ToolTipText     =   ""
      TitleText       =   ""
      TitleBackColor  =   12582912
      BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
         Name            =   "Arial"
         Size            =   9.75
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   -1  'True
         Strikethrough   =   0   'False
      EndProperty
   End
   Begin VB.CommandButton cmdUsers 
      Caption         =   "Add"
      Height          =   315
      Index           =   0
      Left            =   60
      TabIndex        =   4
      Tag             =   "3001"
      Top             =   2790
      Width           =   1065
   End
   Begin VB.CommandButton cmdUsers 
      Caption         =   "Modify"
      Height          =   315
      Index           =   1
      Left            =   1140
      TabIndex        =   3
      Tag             =   "3002"
      Top             =   2790
      Width           =   1065
   End
   Begin VB.CommandButton cmdUsers 
      Caption         =   "Remove"
      Height          =   315
      Index           =   2
      Left            =   2220
      TabIndex        =   2
      Tag             =   "3003"
      Top             =   2790
      Width           =   1065
   End
   Begin VB.CommandButton cmdUsers 
      Caption         =   "Records"
      Height          =   315
      Index           =   3
      Left            =   3270
      TabIndex        =   1
      Tag             =   "3004"
      Top             =   2790
      Width           =   1065
   End
   Begin VB.Image imgLock 
      Height          =   480
      Index           =   2
      Left            =   5640
      Picture         =   "frmMain.frx":0442
      Top             =   1800
      Width           =   480
   End
   Begin VB.Label Label1 
      Alignment       =   2  'Center
      BackStyle       =   0  'Transparent
      Caption         =   "ftp.gigatms.com.tw"
      BeginProperty Font 
         Name            =   "Arial"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   -1  'True
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H00FFC0C0&
      Height          =   255
      Left            =   3030
      MouseIcon       =   "frmMain.frx":0884
      MousePointer    =   99  'Custom
      TabIndex        =   8
      Top             =   120
      Width           =   2115
   End
   Begin PCR300XLib.PCR300x PCR300x 
      Height          =   525
      Left            =   6480
      TabIndex        =   0
      Top             =   360
      Width           =   555
      _Version        =   65536
      _ExtentX        =   979
      _ExtentY        =   926
      _StockProps     =   0
      CommPort        =   5
   End
   Begin VB.Label labVersion 
      BackStyle       =   0  'Transparent
      Caption         =   "Label1"
      ForeColor       =   &H00FFFFFF&
      Height          =   225
      Left            =   4290
      TabIndex        =   6
      Top             =   630
      Width           =   795
   End
   Begin VB.Image Image1 
      BorderStyle     =   1  'Fixed Single
      Height          =   1140
      Left            =   30
      Picture         =   "frmMain.frx":0B8E
      Top             =   30
      Width           =   5220
   End
   Begin VB.Image imgLock 
      Height          =   480
      Index           =   0
      Left            =   5640
      Picture         =   "frmMain.frx":7090
      Top             =   360
      Width           =   480
   End
   Begin VB.Image imgLock 
      Height          =   480
      Index           =   1
      Left            =   5640
      Picture         =   "frmMain.frx":74D2
      Top             =   960
      Width           =   480
   End
End
Attribute VB_Name = "frmMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Public InpuptPSWD As String
Public bInputCancel As Boolean

Public dirList As String

Dim bIsLock As Boolean

'Public bPCR300 As Boolean

Dim NowUserName As String
Dim NowPassword As String
Dim NowLevel As Integer
Dim NowCardID As String
Dim AdminCardID As String

Dim RecordPath As String
Dim nRetry As Integer   ' Card Remove retry count

Dim FunKeyMode As Integer
Public txtFocus As Integer
Dim bLoad As Boolean
Dim bExit As Boolean
Dim bMsgBox As Boolean

Dim bShutDown As Boolean

Private m_bIsHideTaskICON As Boolean

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Sub LockUninstall()
    Dim f As String, I As Integer
    
    On Error Resume Next
    f = Dir(App.Path + "\*.isu")
    Do While f <> ""
        'WinLock.AddLockPathFile App.Path + "\" + f, True
        I = I + 10
        Open App.Path + "\" + f For Input Access Read Lock Read As I
        f = Dir
    Loop
End Sub

Sub SetIsAdministrator(bFlag As Boolean)
    'NowPassword = ""
    If HaveAdministrator(False) Then
        If AdminCardID <> "" Then
            TaskIcon.EnableMenuItem(1) = bFlag
            TaskIcon.EnableMenuItem(2) = bFlag
            Exit Sub
        End If
    End If
    TaskIcon.EnableMenuItem(1) = True
    TaskIcon.EnableMenuItem(2) = True
End Sub

Sub UpdateLanguageList()
    Dim file As String
    
    file = Dir(App.Path + "\Language\*.txt")
    Do While file <> ""
        cmbLanguage.AddItem file
        file = Dir()
    Loop
End Sub

Sub UpdateTaskIconContents()
    TaskIcon.AddMenuItem MF_STRING, 1, LoadResStringX(301)
    TaskIcon.AddMenuItem MF_SEPARATOR, 0, ""
    TaskIcon.AddMenuItem MF_STRING, 2, LoadResStringX(302)
    TaskIcon.AddMenuItem MF_SEPARATOR, 0, ""
    'TaskIcon.AddMenuItem MF_STRING, 10, LoadResStringX(310)
    'TaskIcon.AddMenuItem MF_STRING, 11, LoadResStringX(311)
    TaskIcon.AddMenuItem MF_STRING, 20, LoadResStringX(320)
    
    'TaskIcon.EnableMenuItem(10) = False
    
    TaskIcon.TitleText = LoadResStringX(300)
    TaskIcon.ToolTipText = LoadResStringX(300)
End Sub

Sub UpdateUserList()
    Dim I As Long, txt As String
    
    ' update fgUserList
    With fgUserList
        .Clear
        .Rows = 2
        .FixedRows = 1
        .FixedCols = 1
        '.Rows = 1
        
        .ColWidth(0) = 32 * 15
        .ColWidth(1) = 8 * 2 * 15
        .ColWidth(2) = 12 * 8 * 15
        .ColWidth(3) = 16 * 8 * 15
        .ColAlignment(0) = flexAlignLeftCenter
        .ColAlignment(1) = flexAlignLeftCenter
        .ColAlignment(2) = flexAlignLeftCenter
        .ColAlignment(3) = flexAlignLeftCenter
       
        .Row = 0
        .Col = 1
        .Text = "#"
        .Col = 2
        .Text = LoadResStringX(3011)
        .Col = 3
        .Text = LoadResStringX(3013)
        
        For I = 0 To UBound(UserDB)
            If UserDB(I).Enable Then
                .Rows = .Rows + 1
                .Row = .Rows - 2
                .Col = 1
                .Text = CStr(I)
                .Col = 2
                .Text = UserDB(I).CardID
                .Col = 3
                .Text = UserDB(I).Name
                .Col = 0
                If UserDB(I).Level = 0 Then
                    .Text = "MAN"
                Else
                    .Text = "USR"
                End If
            End If
        Next I
    End With

End Sub

Sub LockComputer(bFlag As Boolean, Optional bMsg As Boolean = False)
    
    If bMsgBox Then Exit Sub
    
    If Me.Visible = False And HaveAdministrator(bMsg) Then
    
        If bIsLock = bFlag Then Exit Sub
        
        If bFlag Then
        
            PCR300x.LockInput True
            
            TaskIcon.Icon = imgLock(1).Picture
            
            If PCR300x.GetScreenSaverActive Then
                PCR300x.ScreenSaver = True
            Else
                frmLock.Show        ' show locked screen
            End If
            
        Else
        
            PCR300x.ScreenSaver = False
            Unload frmLock      ' remove locked screen
            TaskIcon.Icon = imgLock(0).Picture
            PCR300x.LockInput False
            
        End If
        bIsLock = bFlag
    End If
End Sub

Private Sub cmbLanguage_Click()
    SetLanguageProfile App.Path + "\Language\" + cmbLanguage.Text
    LoadResStrings Me
    UpdateUserList
    
    SaveSetting App.Title, "App", "Language", cmbLanguage.Text
End Sub

Private Sub cmdUsers_Click(index As Integer)
    Dim I As Integer
    
    On Error GoTo RecordError
    Select Case index
        Case 0
            I = GetFreeUserIndex()
            If I >= 0 Then
                frmAddUser.UserIndex = I
                Load frmAddUser
                frmAddUser.Show 1, Me
                UpdateUserList
            End If
        Case 1
            If fgUserList.RowSel > 0 Then
            
                fgUserList.Row = fgUserList.RowSel
                fgUserList.Col = 1
                I = CInt(fgUserList.Text)
            
                If I >= 0 Then
                    frmAddUser.UserIndex = I
                    Load frmAddUser
                    frmAddUser.Show 1, Me
                    UpdateUserList
                End If
            End If
        Case 2
            If fgUserList.RowSel > 0 Then
            
                fgUserList.Row = fgUserList.RowSel
                fgUserList.Col = 1
                I = CInt(fgUserList.Text)
            
                If I >= 0 Then
                    UserDB(I).Enable = False
                    UserDB(I).CardID = ""
                    UserDB(I).Password = ""
                    UserDB(I).Level = 0
                    UserDB(I).Name = ""
                    
                    UpdateUserList
                    'lstUsers.RemoveItem lstUsers.ListIndex
                    SaveUserDB
                End If
            End If
        Case 3
            dlgFile.CancelError = True
            dlgFile.Flags = cdlOFNFileMustExist Or cdlOFNPathMustExist
            dlgFile.FileName = Format(Now, "YYMM") & ".txt"
            dlgFile.Filter = "Text (*.txt)|*.txt"
            dlgFile.InitDir = App.Path & "\log"
            dlgFile.ShowOpen
            
            If dlgFile.FileName <> "" Then
                Shell "Notepad.exe " & dlgFile.FileName, vbNormalFocus
            End If

    End Select
RecordError:
End Sub

Sub SaveLogFile(Status As Integer)
    Dim FileName As String
    
    On Error GoTo ExitSaveLogFile
    If NowCardID <> "" And NowUserName <> "" Then
        If Dir(RecordPath, vbDirectory) = "" Then
            MkDir RecordPath
        End If
    
        FileName = RecordPath + "\" + Format$(Now, "YYYYMM") + ".txt"
        Open FileName For Append Access Write Shared As #1
            Print #1, NowCardID + " " + Format(Status, "00 ") + Format(Now, "YYYY/MM/DD HH:MM:SS") + " '" + NowUserName + "'"
        Close #1
    End If
    
ExitSaveLogFile:
End Sub

Private Sub Form_Load()
    Dim I As Integer
    
    If App.PrevInstance Then
        End
    End If
    
    App.TaskVisible = False
    PCR300x.HideInTaskList True
               
    'PCR300x.RegDriver   ' Register WinDriver for Keyboard Lock
    bShutDown = False   ' 2003/06/20
       
    UpdateLanguageList
    
    LoadResStrings Me
    
    ' Register My ActiveX
    TaskIcon.Licence "Jason", "5F5D7C787A71787D7375747370"
    
    LoadUserDB
    UpdateUserList

    TaskIcon.Icon = imgLock(0).Picture
    UpdateTaskIconContents
    TaskIcon.TitleText = "WinLoq"
    TaskIcon.ShowIcon
    
    ' 2002/11/7 Jason Liao for Record File Path
    RecordPath = LoadResStringX(5, App.Path + "\Log")
    
    If HaveAdministrator() Then
        LockComputer True
    End If
    
    If PCR300x.AutoScan > 0 Then
        PCR300x.Request
    Else
        m_bIsHideTaskICON = True
        TaskIcon.Hidden = True
    End If
    
    
    LockUninstall
    labVersion.Caption = "V" & App.Major & "." & App.Minor & "R" & App.Revision
    
End Sub


' 檢查使用者資料庫中,是否有管理者
Function HaveAdministrator(Optional bMsg As Boolean = True) As Boolean
    Static bShowMsg As Boolean
    Dim I As Integer, f As Integer
    
    I = Administrator()
    If I >= 0 Then
        AdminCardID = UserDB(I).CardID
        If AdminCardID = "" Then
            NowPassword = DeSpace(UserDB(I).Password)
        End If
        HaveAdministrator = True
        Exit Function
    End If
    
    If bMsg And bShowMsg = False Then
        bShowMsg = True
        MsgBox LoadResStringX(206), vbOKOnly Or vbInformation, LoadResStringX(0)
        bShowMsg = False
    End If
End Function

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    If UnloadMode = vbFormControlMenu Then
        Cancel = True
        Me.Visible = False
        NowCardID = ""
        PCR300x.Request
    End If
End Sub


Private Sub Form_Resize()
    If Me.WindowState = vbMinimized Then
        Me.Visible = False
        Me.WindowState = vbNormal
        PCR300x.Request
    End If
End Sub

Private Sub Form_Unload(Cancel As Integer)

    PCR300x.HideInTaskList False
    LockComputer False
    If HaveAdministrator() And bShutDown = False Then
        PCR300x.InStartup App.Title, App.Path + "\" + App.Title + ".exe", True
    Else
        PCR300x.InStartup App.Title, App.Path + "\" + App.Title + ".exe", False
    End If
    'WinLock.RemoveAllLockFile
    If PCR300x.PortOpen Then
        PCR300x.PortOpen = False
    End If
    SaveUserDB
End Sub

Private Sub Label1_Click()
        On Error Resume Next
        Shell "explorer.exe ftp://ftp.gigatms.com.tw/software/WinLoq", vbNormalFocus
End Sub

Private Sub PCR300x_FunctionKey(ByVal key As Integer)
    If NowCardID <> "" Then
        If bIsLock Then
            LockComputer False
            PCR300x.Request
        Else
            LockComputer True, True
        End If
    Else
        PCR300x.Request
    End If
End Sub

Private Sub PCR300x_OnIdChange(ByVal Code As String)
    Dim I As Integer, bUserIdExist As Boolean
    Dim sFind As String

    On Error GoTo OnIDError

    If m_bIsHideTaskICON Then
        TaskIcon.Hidden = False
        m_bIsHideTaskICON = False
    End If

    ' Input ID Code
    If Me.Visible = True And txtFocus > 0 Then
        frmAddUser.txtUserID = Code
        txtFocus = 0
        Exit Sub
    End If
    
    nRetry = 0                          ' 2002/11/10 Jason Liao (card Remove retry count reset)
    If NowCardID = Code Then Exit Sub   ' 2002/11/8 Jason Liao
    
    ' Check Code is Administrator
    If Code <> "" And Me.Visible = False Then
        
        I = CheckCardID(Code)
        If I >= 0 Then
            NowPassword = DeSpace(UserDB(I).Password)
            NowUserName = UserDB(I).Name
            NowLevel = UserDB(I).Level
            Call LockComputer(False)
            
            Timer.Enabled = False
            
            If NowLevel = 0 Then
                SetIsAdministrator True
            Else
                SetIsAdministrator False
            End If
        Else
            Call LockComputer(True)
        End If
        
        ' Save Log File
        If bIsLock = False Then
            If Code <> NowCardID Then
                NowCardID = Code
                SaveLogFile 1
            End If
        Else
            PCR300x.Alarm
        End If
    Else
        NowCardID = ""
        NowUserName = ""
        NowLevel = 1
    End If
OnIDError:

End Sub

Private Sub PCR300x_StatusChange(ByVal Status As Integer)
    
    If Me.Visible Then Exit Sub
    
    Select Case Status
        Case 0
            If m_bIsHideTaskICON Then
                TaskIcon.Hidden = False
                m_bIsHideTaskICON = False
            End If
            If Me.Visible = False Then ' Card Not Exist
                If NowCardID <> "" Then
                    If nRetry > 3 Then
                        SaveLogFile 0
                        Unload frmInput
                        
                        Call LockComputer(True)
                        NowCardID = ""
                        NowUserName = ""
                        NowLevel = 1
                        nRetry = 0
                    Else
                        nRetry = nRetry + 1 ' 防止PCR300卡片剛放入時誤動作
                        PCR300x.Request
                    End If
                Else    ' 2002/11/15 for [OK] button
                    Call LockComputer(True)
                    NowCardID = ""
                    NowUserName = ""
                    NowLevel = 1
                    nRetry = 0
                End If
            End If
        Case 1      ' Card In Reader
            
        
        Case 2      ' Inster PCR300 (USB)
            'ScanPCR300
            'TaskIcon.Hidden = False
            PCR300x.Buzzer
            Sleep 1000
            PCR300x.Request
            
        Case 3      ' remove PCR300 (USB)
            Call LockComputer(True)
            'TaskIcon.Hidden = True
            NowCardID = ""
            NowUserName = ""
            NowLevel = 1
            
        Case 4      ' Power in Suspen
            NowCardID = ""
            LockComputer False
            
        Case 5      ' Power Resume
            PCR300x.Buzzer
            bIsLock = False
            LockComputer True
            Sleep 1000
            PCR300x.Request
            
    End Select
End Sub

Private Sub TaskIcon_OnIconEvent(ByVal EventMsg As Long)
    'ShowCursor 1
    Me.MousePointer = vbDefault
End Sub

Private Sub TaskIcon_OnMenuEvent(ByVal id As Integer)
    On Error Resume Next
    
    If id = 1 Or id = 2 Then
        If Me.Visible = False Then
            If NowPassword <> "" Then
                ' Ask User Administrator Password
                
                    frmInput.Show 1, Me
                    If InpuptPSWD <> NowPassword Then
                        If bInputCancel = False And InpuptPSWD <> "" Then
                            bMsgBox = True
                            MsgBox LoadResStringX(201), vbExclamation Or vbOKOnly, App.Title
                            bMsgBox = False
                            PCR300x.Request
                        End If
                        Exit Sub
                    End If
                    
            End If
        End If
    End If
    
    Select Case id
        Case 1  ' Properties
            NowPassword = ""
            Me.Visible = True
        Case 2  ' Shutdown WinSecurity
            bShutDown = True
            Unload Me
            End
        Case 10 ' Help
           ' help = LoadResStringX(6, App.path + "\WinSecurity.pdf")
           ' help = Replace(help, "<App>", App.path)
           ' Shell "explorer.exe " + help, vbNormalFocus
        Case 11 ' Revision Notes
           ' Shell "explorer.exe " + App.path + "\notes.pdf", vbNormalFocus
        Case 20 ' About
            frmAbout.Show
    End Select
End Sub

Private Sub Timer_Timer()
    If PCR300x.PortOpen = False Then
        If PCR300x.AutoScan > 0 Then
            PCR300x.Buzzer
            Sleep 2000
            PCR300x.Request
            'Timer.Enabled = False
        End If
    End If
End Sub
