forked from KurtisLiggett/Simple-IP-Config
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.au3
More file actions
1666 lines (1508 loc) · 60.4 KB
/
functions.au3
File metadata and controls
1666 lines (1508 loc) · 60.4 KB
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; -----------------------------------------------------------------------------
; This file is part of Simple IP Config.
;
; Simple IP Config is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
;
; Simple IP Config is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with Simple IP Config. If not, see <http://www.gnu.org/licenses/>.
; -----------------------------------------------------------------------------
;==============================================================================
; Filename: functions.au3
; Description: - most functions related to doing something in the program
;==============================================================================
Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")
Global $suppressComError = 0
Func MyErrFunc($oError)
If Not $suppressComError Then
SetError(1)
; Do anything here.
MsgBox(1, "COM Error", "Simple IP Config COM Error!" & @CRLF & "Error Number: " & Hex($oError.number))
EndIf
EndFunc ;==>MyErrFunc
;------------------------------------------------------------------------------
; Title...........: RunCallback
; Description.....: Callback function that runs after a command is finished
; *commands set IP address info through a hidden command
; prompt. They run without blocking the program, so
; we need a callback function to decide how to proceed
; when the command is done running.
;
; Parameters......: $sDescription -Command description
; $sNextDescription -Next command description
; $sStdOut -STD output string from command prompt
; Return value....:
;------------------------------------------------------------------------------
Func RunCallback($sDescription, $sNextDescription, $sStdOut)
If $sStdOut = "Command timeout" Then
_setStatus("Action timed out! Command Aborted.", 1)
If asyncRun_isIdle() Then
_GUICtrlToolbar_EnableButton($hToolbar, $tb_apply, True)
Else
_GUICtrlToolbar_EnableButton($hToolbar, $tb_apply, False)
EndIf
Else
If StringInStr($sStdOut, "failed") Then
_setStatus(StringReplace($sStdOut, @CRLF, " "), 1)
If asyncRun_isIdle() Then
_GUICtrlToolbar_EnableButton($hToolbar, $tb_apply, True)
EndIf
ElseIf StringInStr($sStdOut, "exists") Then
_setStatus(StringReplace($sStdOut, @CRLF, " "), 1)
_GUICtrlToolbar_EnableButton($hToolbar, $tb_apply, True)
_asyncRun_Clear()
Else
If $sDescription = $sNextDescription Then
If Not $showWarning Then _setStatus($sNextDescription)
_GUICtrlToolbar_EnableButton($hToolbar, $tb_apply, False)
ElseIf asyncRun_isIdle() Then
_GUICtrlToolbar_EnableButton($hToolbar, $tb_apply, True)
If Not $showWarning Then _setStatus("Ready")
_GUICtrlToolbar_EnableButton($hToolbar, $tb_apply, True)
Else
If Not $showWarning Then _setStatus($sNextDescription)
_GUICtrlToolbar_EnableButton($hToolbar, $tb_apply, False)
EndIf
EndIf
EndIf
_updateCurrent()
EndFunc ;==>RunCallback
;------------------------------------------------------------------------------
; Title...........: _ExitChild
; Description.....: Destroy the child window and enable the main GUI
;
; Parameters......: $childwin -child window handle
; Return value....:
;------------------------------------------------------------------------------
Func _ExitChild($childwin)
$guiState = WinGetState($hgui)
GUISetState(@SW_ENABLE, $hGUI)
Local $a_ret = DllCall("user32.dll", "int", "DestroyWindow", "hwnd", $childwin)
;GUIDelete( $childwin )
If Not (BitAND($guiState, $WIN_STATE_VISIBLE)) Or BitAND($guiState, $WIN_STATE_MINIMIZED) Then Return
If BitAND($guiState, $WIN_STATE_EXISTS) And Not (BitAND($guiState, $WIN_STATE_ACTIVE)) Then
_maximize()
EndIf
EndFunc ;==>_ExitChild
Func _CreateLink()
$profileName = StringReplace(GUICtrlRead(GUICtrlRead($list_profiles)), "|", "")
$iniName = StringReplace($profileName, "[", "{lb}")
$iniName = StringReplace($iniName, "]", "{rb}")
$dir = FileSaveDialog("Choose a filename", @ScriptDir, "Shortcuts (*.lnk)", 0, "Simple IP Config - " & $profileName)
If @error Then Return
$res = FileCreateShortcut(@ScriptFullPath, $dir, @ScriptDir, '/set-config "' & $iniName & '"', "desc", @ScriptFullPath)
If $res = -1 Then _setStatus("Could not save to the selected location!", 1)
EndFunc ;==>_CreateLink
;------------------------------------------------------------------------------
; Title...........: _checksSICUpdate
; Description.....: Check for updates/ask to download
;
; Parameters......: $manualCheck -manually run check from menu item
; Return value....:
;------------------------------------------------------------------------------
Func _checksSICUpdate($manualCheck = 0)
; This function checks if there are new releases on github and request the user to download it
$github_releases = "https://api.github.com/repos/KurtisLiggett/Simple-IP-Config/releases/latest"
Local $oHTTP = ObjCreate("WinHttp.WinHttpRequest.5.1")
$oHTTP.Open("GET", $github_releases, False)
If (@error) Then
SetError(1001, 0, 0)
If $manualCheck Then
MsgBox(16, "Automatic Update Error", "An error was encountered while retrieving the update." & @CRLF & "Please check your internet connection." & @CRLF & "Error code: " & @error)
EndIf
Return
EndIf
$oHTTP.Send()
If (@error) Then
SetError(1002, 0, 0)
If $manualCheck Then
MsgBox(16, "Automatic Update Error", "An error was encountered while retrieving the update." & @CRLF & "Please check your internet connection." & @CRLF & "Error code: " & @error)
EndIf
Return
EndIf
If ($oHTTP.Status <> 200) Then
SetError(1003, 0, 0)
If $manualCheck Then
MsgBox(16, "Automatic Update Error", "An error was encountered while retrieving the update." & @CRLF & "Please check your internet connection." & @CRLF & "Error code: " & @error)
EndIf
Return
EndIf
Local $cleanedJSON = StringReplace($oHTTP.ResponseText, '"', "")
Local $JSONinfo = StringRegExp($cleanedJSON, '(?:tag_name:)([^\{,}]+)', 3)
Local $currentVersion = $JSONinfo[0]
Local $currentVersiontokens = StringSplit($currentVersion, ".")
If (@error) Then
If $manualCheck Then
SetError(1004, 0, 0)
MsgBox(16, "Automatic Update Error", "An error was encountered while retrieving the update." & @CRLF & "Invalid result string." & @CRLF & "Error code: " & @error)
EndIf
Return
EndIf
Local $thisVersion = $winVersion
Local $thisVersiontokens = StringSplit($thisVersion, ".")
If (@error) Then
If $manualCheck Then
SetError(1004, 0, 0)
MsgBox(16, "Automatic Update Error", "An error was encountered while retrieving the update." & @CRLF & "Invalid result string." & @CRLF & "Error code: " & @error)
EndIf
Return
EndIf
;compare current version to running version -> -1 = current < running, 0 = same, 1 = current > running
Local $result = 0
If $currentVersiontokens[0] > 1 And $thisVersiontokens[0] > 1 Then
If Int($currentVersiontokens[1]) > Int($thisVersiontokens[1]) Then ;newer major version
$result = 1
ElseIf Int($currentVersiontokens[1]) == Int($thisVersiontokens[1]) Then ;same major version
If Int($currentVersiontokens[2]) > Int($thisVersiontokens[2]) Then ;newer minor version
$result = 1
ElseIf Int($currentVersiontokens[2]) == Int($thisVersiontokens[2]) Then ;same minor
If $currentVersiontokens[0] > 2 And $thisVersiontokens[0] > 2 Then
If StringLeft( $thisVersiontokens[3], 1 ) == 'b' Then
$result = 1
Else
If Int($currentVersiontokens[3]) > Int($thisVersiontokens[3]) Then ;newer revision
$result = 1
Else ;same revision
$result = 0
EndIf
EndIf
ElseIf $currentVersiontokens[0] > 2 And $thisVersiontokens[0] == 2 Then
$result = 1
ElseIf $currentVersiontokens[0] == 2 And $thisVersiontokens[0] > 2 Then
If StringLeft( $thisVersiontokens[3], 1 ) == 'b' Then
$result = 1
Else
$result = -1
EndIf
Else
$result = -1
EndIf
Else
$result = -1
EndIf
Else
$result = -1
EndIf
Else
If $manualCheck Then
SetError(1004, 0, 0)
MsgBox(16, "Automatic Update Error", "An error was encountered while retrieving the update." & @CRLF & "Invalid result string." & @CRLF & "Error code: " & @error)
EndIf
EndIf
Local $isNew = 0
If $result == 1 Then
$isNew = 1
$dateNow = _NowCalcDate()
$dateLastCheck = Options_GetValue($options, $OPTIONS_LastUpdateCheck)
$updateText = "Your version is: " & $thisVersion & @CRLF & _
"Latest version is: " & $currentVersion & @CRLF & @CRLF & _
"A newer version is available"
If $manualCheck Or _DateDiff('D', $dateLastCheck, $dateNow) >= 7 Or $dateLastCheck='' Then
_ShowUpdateDialog($thisVersion, $currentVersion, $isNew)
Options_SetValue($options, $OPTIONS_LastUpdateCheck, $dateNow)
Local $LastUpdateCheckName = Options_GetName($options, $OPTIONS_LastUpdateCheck)
IniWrite(@ScriptDir & "/profiles.ini", "options", $LastUpdateCheckName, $dateNow)
EndIf
Else
$updateText = "Your version is: " & $thisVersion & @CRLF & _
"Latest version is: " & $currentVersion & @CRLF & @CRLF & _
"You have the latest version."
If $manualCheck Then _ShowUpdateDialog($thisVersion, $currentVersion, $isNew)
EndIf
EndFunc ;==>_checksSICUpdate
Func _DoUpdate($newFilename)
$fileStr = '@echo off' & @CRLF & _
'taskkill /pid ' & WinGetProcess($hgui) & @CRLF & _ ; kill running instance
'del /Q "' & @ScriptFullPath & '"' & @CRLF & _ ; delete old version
'start "" "' & $newFilename & '"' ; start new version
$filename = 'simple_ip_config_updater.cmd'
$file = FileOpen($filename, 2) ; open/overwrite
FileWrite($file, $fileStr)
FileClose($file)
$iPID = Run(@ComSpec & " /k " & $filename, "", @SW_HIDE, $STDIN_CHILD + $STDERR_MERGED)
EndFunc ;==>_DoUpdate
;------------------------------------------------------------------------------
; Title...........: _updateCombo
; Description.....: UpdateUpdate the adapters list
;
; Parameters......:
; Return value....:
;------------------------------------------------------------------------------
Func _updateCombo()
_setStatus("Updating Adapter List...")
_loadAdapters()
Local $adapterNames = Adapter_GetNames($adapters)
_GUICtrlComboBox_ResetContent(GUICtrlGetHandle($combo_adapters))
If Not IsArray($adapters) Then
MsgBox(16, "Error", "There was a problem retrieving the adapters.")
Else
Adapter_Sort($adapters) ; connections sort ascending
$defaultitem = $adapterNames[0]
$sStartupAdapter = Options_GetValue($options, $OPTIONS_StartupAdapter)
$index = _ArraySearch($adapters, $sStartupAdapter, 1)
If ($index <> -1) Then
$defaultitem = $sStartupAdapter
EndIf
$sBlacklist = Options_GetValue($options, $OPTIONS_AdapterBlacklist)
$aBlacklist = StringSplit($sBlacklist, "|")
For $i = 0 To UBound($adapterNames) - 1
$indexBlacklist = _ArraySearch($aBlacklist, $adapterNames[$i], 1)
If $indexBlacklist <> -1 Then ContinueLoop
GUICtrlSetData($combo_adapters, $adapterNames[$i], $defaultitem)
Next
EndIf
ControlSend($hgui, "", $combo_adapters, "{END}")
_setStatus("Ready")
EndFunc ;==>_updateCombo
;------------------------------------------------------------------------------
; Title...........: _blacklistAdd
; Description.....: Add selected adapter to the edit box in the
; hide adapters window
;
; Parameters......:
; Return value....:
;------------------------------------------------------------------------------
;~ Func _blacklistAdd()
;~ $selected_adapter = GUICtrlRead($combo_adapters)
;~ $list = GUICtrlRead($blacklistEdit)
;~ If $list = "" OR StringRight($list, 1) = @CR OR StringRight($list, 1) = @LF Then
;~ $newString = $selected_adapter
;~ Else
;~ $newString = @CRLF&$selected_adapter
;~ EndIf
;~ $iEnd = StringLen($list & $newString)
;~ _GUICtrlEdit_SetSel($blacklistEdit, $iEnd, $iEnd)
;~ _GUICtrlEdit_Scroll($blacklistEdit, 4)
;~ GUICtrlSetData($blacklistEdit, $newString, 1)
;~ EndFunc
;------------------------------------------------------------------------------
; Title...........: _arrange
; Description.....: Arrange profiles in asc/desc order
;
; Parameters......: $desc - 0=ascending order; 1=descending order
; Return value....: 0 -success
; 1 -could not open file
; 3 -error writing to file
;------------------------------------------------------------------------------
Func _arrange($desc = 0)
_loadProfiles()
Local $newfile = ""
$newfile &= "[Options]" & @CRLF
$newfile &= Options_GetName($options, $OPTIONS_Version) & "=" & Options_GetValue($options, $OPTIONS_Version) & @CRLF
$newfile &= Options_GetName($options, $OPTIONS_MinToTray) & "=" & Options_GetValue($options, $OPTIONS_MinToTray) & @CRLF
$newfile &= Options_GetName($options, $OPTIONS_StartupMode) & "=" & Options_GetValue($options, $OPTIONS_StartupMode) & @CRLF
$newfile &= Options_GetName($options, $OPTIONS_Language) & "=" & Options_GetValue($options, $OPTIONS_Language) & @CRLF
$newfile &= Options_GetName($options, $OPTIONS_StartupAdapter) & "=" & Options_GetValue($options, $OPTIONS_StartupAdapter) & @CRLF
$newfile &= Options_GetName($options, $OPTIONS_Theme) & "=" & Options_GetValue($options, $OPTIONS_Theme) & @CRLF
$newfile &= Options_GetName($options, $OPTIONS_SaveAdapterToProfile) & "=" & Options_GetValue($options, $OPTIONS_SaveAdapterToProfile) & @CRLF
$newfile &= Options_GetName($options, $OPTIONS_AdapterBlacklist) & "=" & Options_GetValue($options, $OPTIONS_AdapterBlacklist) & @CRLF
$newfile &= Options_GetName($options, $OPTIONS_PositionX) & "=" & Options_GetValue($options, $OPTIONS_PositionX) & @CRLF
$newfile &= Options_GetName($options, $OPTIONS_PositionY) & "=" & Options_GetValue($options, $OPTIONS_PositionY) & @CRLF
$newfile &= Options_GetName($options, $OPTIONS_AutoUpdate) & "=" & Options_GetValue($options, $OPTIONS_AutoUpdate) & @CRLF
Profiles_Sort($profiles, $desc)
For $i = 0 To Profiles_GetSize($profiles) - 1
$sProfileName = Profiles_GetValueByIndex($profiles, $i, $PROFILES_Name)
$iniName = iniNameEncode($sProfileName)
$newfile &= "[" & $iniName & "]" & @CRLF
$newfile &= Profiles_GetKeyName($profiles, $PROFILES_IpAuto) & "=" & Profiles_GetValueByIndex($profiles, $i, $PROFILES_IpAuto) & @CRLF
$newfile &= Profiles_GetKeyName($profiles, $PROFILES_IpAddress) & "=" & Profiles_GetValueByIndex($profiles, $i, $PROFILES_IpAddress) & @CRLF
$newfile &= Profiles_GetKeyName($profiles, $PROFILES_IpSubnet) & "=" & Profiles_GetValueByIndex($profiles, $i, $PROFILES_IpSubnet) & @CRLF
$newfile &= Profiles_GetKeyName($profiles, $PROFILES_IpGateway) & "=" & Profiles_GetValueByIndex($profiles, $i, $PROFILES_IpGateway) & @CRLF
$newfile &= Profiles_GetKeyName($profiles, $PROFILES_DnsAuto) & "=" & Profiles_GetValueByIndex($profiles, $i, $PROFILES_DnsAuto) & @CRLF
$newfile &= Profiles_GetKeyName($profiles, $PROFILES_DnsPref) & "=" & Profiles_GetValueByIndex($profiles, $i, $PROFILES_DnsPref) & @CRLF
$newfile &= Profiles_GetKeyName($profiles, $PROFILES_DnsAlt) & "=" & Profiles_GetValueByIndex($profiles, $i, $PROFILES_DnsAlt) & @CRLF
$newfile &= Profiles_GetKeyName($profiles, $PROFILES_RegisterDns) & "=" & Profiles_GetValueByIndex($profiles, $i, $PROFILES_RegisterDns) & @CRLF
$newfile &= Profiles_GetKeyName($profiles, $PROFILES_AdapterName) & "=" & Profiles_GetValueByIndex($profiles, $i, $PROFILES_AdapterName) & @CRLF
Next
Local $hFileOpen = FileOpen(@ScriptDir & "/profiles.ini", 2)
If $hFileOpen = -1 Then
Return 1
EndIf
Local $sFileWrite = FileWrite($hFileOpen, $newfile)
If @error <> 0 Then
FileClose($hFileOpen)
Return 3
EndIf
FileClose($hFileOpen)
_updateProfileList()
Return 0
EndFunc ;==>_arrange
;------------------------------------------------------------------------------
; Title...........: _checkMouse
; Description.....: Check if mouse is over a particular control
;
; Parameters......: $Id -controlID
; Return value....: 0 -mouse is NOT over the control
; 1 -mouse IS over the control
;------------------------------------------------------------------------------
Func _checkMouse($Id)
$idPos = ControlGetPos($hgui, "", $Id)
$mPos = MouseGetPos()
If $mPos[0] > $idPos[0] And $mPos[0] < $idPos[0] + $idPos[2] And $mPos[1] > $idPos[1] And $mPos[1] < $idPos[1] + $idPos[3] Then
Return 1
Else
Return 0
EndIf
EndFunc ;==>_checkMouse
;------------------------------------------------------------------------------
; Title...........: _clickDn
; Description.....: Check if started dragging a listview item
;
; Parameters......:
; Return value....:
;------------------------------------------------------------------------------
Func _clickDn()
Static $dragItemPrev
$dragitem = ControlListView($hgui, "", $list_profiles, "GetSelected")
If _checkMouse($list_profiles) And $dragitem <> "" Then
; -- check for double click --
$mdblTimerDiff = TimerDiff($mdblTimerInit)
If $mdblTimerDiff <= $mDblClickTime Then
If $dragItemPrev == $dragitem Then
$mdblClick = 1
EndIf
Else
$mdblClick = 0
EndIf
$mdblTimerInit = TimerInit()
; -- check for dragging --
If $dragitem <> "" And $mdblClick <> 1 Then
$dragging = True
Else
$dragging = False
EndIf
Else
$dragging = False
$mdblClick = 0
EndIf
$dragItemPrev = $dragitem
EndFunc ;==>_clickDn
;------------------------------------------------------------------------------
; Title...........: _clickUp
; Description.....: On mouse click release:
; if double-clicked, apply the selected profile.
; if item was dragged, rearrange the items in the listview
; and profiles.ini file.
;
; Parameters......:
; Return value....:
;------------------------------------------------------------------------------
Func _clickUp()
If _checkMouse($list_profiles) And _ctrlHasFocus($list_profiles) Then
MouseClick($MOUSE_CLICK_PRIMARY)
If $mdblClick Then
_apply_GUI()
$mdblClick = 0
Else
If $dragging Then
$newitem = ControlListView($hgui, "", $list_profiles, "GetSelected")
$dragtext = ControlListView($hgui, "", $list_profiles, "GetText", $dragitem)
$newtext = ControlListView($hgui, "", $list_profiles, "GetText", $newitem)
If $newitem <> "" And $dragtext <> $newtext Then
$ret = _iniMove(@ScriptDir & "/profiles.ini", $dragtext, $newtext)
If Not $ret Then
$dragProfile = Profiles_GetProfile($profiles, $dragtext)
Profiles_Delete($profiles, $dragtext)
Profiles_InsertProfile($profiles, $newitem, $dragtext, $dragProfile)
_updateProfileList()
EndIf
EndIf
EndIf
EndIf
EndIf
$dragging = False
EndFunc ;==>_clickUp
;------------------------------------------------------------------------------
; Title...........: _iniMove
; Description.....:
;
; Parameters......: $file -filename
; $fromName -source profile name
; $toName -destination profile name
; Return value....: 0 -success
; 1 -file read error or string error
; 2 -string error
; 3 -file write error
;
; Notes...........: need to make these return errors more descriptive
;------------------------------------------------------------------------------
Func _iniMove($file, $fromName, $toName)
Local $sNewFile = ""
Local $sLeft = ""
Local $sSection = ""
Local $sMid = ""
Local $sRight = ""
Local $sAfterTo = ""
$inifromName = iniNameEncode($fromName)
$initoName = iniNameEncode($toName)
Local $sFileRead = FileRead($file)
If @error <> 0 Then
Return 1
EndIf
$strToPos = StringInStr($sFileRead, "[" & $initoName & "]")
If $strToPos = 0 Then
Return 2
EndIf
$strFromPos = StringInStr($sFileRead, "[" & $inifromName & "]")
If $strFromPos = 0 Then
Return 2
EndIf
If $strFromPos > $strToPos Then
$aToSplit = StringSplit($sFileRead, "[" & $initoName & "]", 1)
If @error Then
Return 1
EndIf
$sLeft = $aToSplit[1]
$aFromSplit = StringSplit($aToSplit[2], "[" & $inifromName & "]", 1)
If @error Then
Return 1
EndIf
$sMid = "[" & $initoName & "]" & $aFromSplit[1]
$strNextPos = StringInStr($aFromSplit[2], "[")
If $strNextPos = 0 Then
$sSection = "[" & $inifromName & "]" & $aFromSplit[2]
$sRight = ""
Else
$sSection = "[" & $inifromName & "]" & StringLeft($aFromSplit[2], $strNextPos - 1)
$sRight = StringRight($aFromSplit[2], StringLen($aFromSplit[2]) - $strNextPos + 1)
EndIf
$sNewFile = $sLeft & $sSection & $sMid & $sRight
ElseIf $strFromPos < $strToPos Then
$aFromSplit = StringSplit($sFileRead, "[" & $inifromName & "]", 1)
If @error Then
Return 1
EndIf
$sBeforeFrom = $aFromSplit[1]
$strNextPos = StringInStr($aFromSplit[2], "[")
$sSection = "[" & $inifromName & "]" & StringLeft($aFromSplit[2], $strNextPos - 1)
$sAfterFrom = StringRight($aFromSplit[2], StringLen($aFromSplit[2]) - $strNextPos + 1)
$strToPos = StringInStr($sAfterFrom, "[" & $initoName & "]")
$strNextPos = StringInStr($sAfterFrom, "[", 0, 1, $strToPos + 1)
If $strNextPos = 0 Then
$sMid = $sAfterFrom
$sAfterTo = ""
Else
$sMid = StringLeft($sAfterFrom, $strNextPos - 1)
$sAfterTo = StringRight($sAfterFrom, StringLen($sAfterFrom) - $strNextPos + 1)
EndIf
$sNewFile = $sBeforeFrom & $sMid & $sSection & $sAfterTo
Else
Return 0
EndIf
Local $hFileOpen = FileOpen($file, 2)
If $hFileOpen = -1 Then
Return 1
EndIf
Local $sFileWrite = FileWrite($hFileOpen, $sNewFile)
If @error <> 0 Then
FileClose($hFileOpen)
Return 3
EndIf
FileClose($hFileOpen)
Return 0
EndFunc ;==>_iniMove
;------------------------------------------------------------------------------
; Title...........: _radios
; Description.....: Check/set radio button interlocks
;
; Parameters......:
; Return value....:
;------------------------------------------------------------------------------
Func _radios()
If GUICtrlRead($radio_IpAuto) = $GUI_CHECKED Then
GUICtrlSetState($radio_DnsAuto, $GUI_ENABLE)
WinSetState($ip_Ip, "", @SW_DISABLE)
WinSetState($ip_Subnet, "", @SW_DISABLE)
WinSetState($ip_Gateway, "", @SW_DISABLE)
GUICtrlSetState($label_ip, $GUI_DISABLE)
GUICtrlSetState($label_subnet, $GUI_DISABLE)
GUICtrlSetState($label_gateway, $GUI_DISABLE)
Else
GUICtrlSetState($radio_DnsMan, $GUI_CHECKED)
GUICtrlSetState($radio_DnsAuto, $GUI_DISABLE)
WinSetState($ip_Ip, "", @SW_ENABLE)
WinSetState($ip_Subnet, "", @SW_ENABLE)
WinSetState($ip_Gateway, "", @SW_ENABLE)
GUICtrlSetState($label_ip, $GUI_ENABLE)
GUICtrlSetState($label_subnet, $GUI_ENABLE)
GUICtrlSetState($label_gateway, $GUI_ENABLE)
EndIf
If GUICtrlRead($radio_DnsAuto) = $GUI_CHECKED Then
WinSetState($ip_DnsPri, "", @SW_DISABLE)
WinSetState($ip_DnsAlt, "", @SW_DISABLE)
GUICtrlSetState($label_dnsPri, $GUI_DISABLE)
GUICtrlSetState($label_dnsAlt, $GUI_DISABLE)
GUICtrlSetState($ck_dnsReg, $GUI_DISABLE)
Else
WinSetState($ip_DnsPri, "", @SW_ENABLE)
WinSetState($ip_DnsAlt, "", @SW_ENABLE)
GUICtrlSetState($label_dnsPri, $GUI_ENABLE)
GUICtrlSetState($label_dnsAlt, $GUI_ENABLE)
GUICtrlSetState($ck_dnsReg, $GUI_ENABLE)
EndIf
EndFunc ;==>_radios
Func _apply_GUI()
$dhcp = (GUICtrlRead($radio_IpAuto) = $GUI_CHECKED) ? "true" : "false"
$ip = _ctrlGetIP($ip_Ip)
$subnet = _ctrlGetIP($ip_Subnet)
$gateway = _ctrlGetIP($ip_Gateway)
$dnsDhcp = (GUICtrlRead($radio_DnsAuto) = $GUI_CHECKED) ? "true" : "false"
$dnsp = _ctrlGetIP($ip_DnsPri)
$dnsa = _ctrlGetIP($ip_DnsAlt)
$dnsreg = (BitAND(GUICtrlRead($ck_dnsReg), $GUI_CHECKED) = $GUI_CHECKED) ? "true" : "false"
$adapter = GUICtrlRead($combo_adapters)
_apply($dhcp, $ip, $subnet, $gateway, $dnsDhcp, $dnsp, $dnsa, $dnsreg, $adapter, RunCallback)
EndFunc ;==>_apply_GUI
;------------------------------------------------------------------------------
; Title...........: _apply
; Description.....: Apply the selected profile to the selected adapter
;
; Parameters......:
; Return value....: 0 -success
; 1 -no adapter is selected
;------------------------------------------------------------------------------
; MUST BE TESTED VERY CAREFULLY
Func _apply($dhcp, $ip, $subnet, $gateway, $dnsDhcp, $dnsp, $dnsa, $dnsreg, $adapter, $Callback)
If $adapter = "" Then
_setStatus("Please select an adapter and try again", 1)
Return 1
EndIf
$cmd1 = 'netsh interface ip set address '
$cmd2 = '"' & $adapter & '"'
$cmd3 = ""
$message = ""
If ($dhcp = "true") Then
$cmd3 = " dhcp"
$message = "Setting DHCP..."
Else
If $ip = "" Then
_setStatus("Please enter an IP address", 1)
Return 1
ElseIf $subnet = "" Then
_setStatus("Please enter a subnet mask", 1)
Return 1
Else
If $gateway = "" Then
$cmd3 = " static " & $ip & " " & $subnet & " none"
Else
$cmd3 = " static " & $ip & " " & $subnet & " " & $gateway & " 1"
EndIf
$message = "Setting static IP address..."
EndIf
EndIf
;_asyncNewCmd($cmd1&$cmd2&$cmd3, $message)
;(cmd, callback, description)
asyncRun($cmd1 & $cmd2 & $cmd3, $Callback, $message)
$cmd1 = ''
$cmd1_1 = 'netsh interface ip set dns '
$cmd1_2 = 'netsh interface ip add dns '
$cmd1_3 = 'netsh interface ip delete dns '
$cmd2 = '"' & $adapter & '"'
$cmd3 = ""
$cmdend = ""
$message = ""
$cmdReg = ""
If ($dnsDhcp = "true") Then
$cmd1 = $cmd1_1
$cmd3 = " dhcp"
$message = "Setting DNS DHCP..."
;_asyncNewCmd($cmd1&$cmd2&$cmd3, $message, 1)
;(cmd, callback, description)
asyncRun($cmd1 & $cmd2 & $cmd3, $Callback, $message)
Else
If $dnsreg = "true" Then
$cmdReg = "both"
Else
$cmdReg = "none"
EndIf
If $dnsp <> "" Then
$cmd1 = $cmd1_1
$cmd3 = " static " & $dnsp
$message = "Setting preferred DNS server..."
$cmdend = (_OSVersion() >= 6) ? " " & $cmdReg & " no" : "$cmdReg"
;_asyncNewCmd($cmd1&$cmd2&$cmd3&$cmdend, $message, 1)
;(cmd, callback, description)
asyncRun($cmd1 & $cmd2 & $cmd3 & $cmdend, $Callback, $message)
If $dnsa <> "" Then
$cmd1 = $cmd1_2
$cmd3 = " " & $dnsa
$message = "Setting alternate DNS server..."
$cmdend = (_OSVersion() >= 6) ? " 2 no" : ""
;_asyncNewCmd($cmd1&$cmd2&$cmd3&$cmdend, $message, 1)
;(cmd, callback, description)
asyncRun($cmd1 & $cmd2 & $cmd3 & $cmdend, $Callback, $message)
EndIf
ElseIf $dnsa <> "" Then
$cmd1 = $cmd1_1
$cmd3 = " static " & $dnsp
$message = "Setting preferred DNS server..."
$cmdend = (_OSVersion() >= 6) ? " " & $cmdReg & " no" : "$cmdReg"
;_asyncNewCmd($cmd1&$cmd2&$cmd3&$cmdend, $message, 1)
;(cmd, callback, description)
asyncRun($cmd1 & $cmd2 & $cmd3 & $cmdend, $Callback, $message)
Else
$cmd1 = $cmd1_3
$cmd3 = " all"
$message = "Deleting DNS servers..."
;_asyncNewCmd($cmd1&$cmd2&$cmd3, $message, 1)
;(cmd, callback, description)
asyncRun($cmd1 & $cmd2 & $cmd3, $Callback, $message)
EndIf
EndIf
EndFunc ;==>_apply
;Func _OLDapply()
; $selected_adapter = GUICtrlRead($combo_adapters)
; If $selected_adapter = "" Then
; _setStatus("Please select an adapter and try again", 1)
; Return 1
; Endif
;
; $ip = ""
; $subnet = ""
; $gateway = ""
;
; $dhcp = (GUICtrlRead($radio_IpAuto) = $GUI_CHECKED)?1:0
; $cmd1 = 'netsh interface ip set address '
; $cmd2 = '"' & $selected_adapter & '"'
; $cmd3 = ""
; $message = ""
; if $dhcp Then
; $cmd3 = " dhcp"
; $message = "Setting DHCP..."
; Else
; $ip = _ctrlGetIP( $ip_Ip )
; $subnet = _ctrlGetIP( $ip_Subnet )
; $gateway = _ctrlGetIP( $ip_Gateway )
; If $ip = "" Then
; _setStatus("Please enter an IP address", 1)
; Return 1
; ElseIf $subnet = "" Then
; _setStatus("Please enter a subnet mask", 1)
; Return 1
; Else
; If $gateway = "" Then
; $cmd3 = " static " & $ip & " " & $subnet & " none"
; Else
; $cmd3 = " static " & $ip & " " & $subnet & " " & $gateway & " 1"
; EndIf
; $message = "Setting static IP address..."
; EndIf
; EndIf
; _asyncNewCmd($cmd1&$cmd2&$cmd3, $message)
;
; $dnsp = ""
; $dnsa = ""
;
; $dnsDhcp = (GUICtrlRead($radio_DnsAuto) = $GUI_CHECKED)?1:0
; $cmd1 = ''
; $cmd1_1 = 'netsh interface ip set dns '
; $cmd1_2 = 'netsh interface ip add dns '
; $cmd1_3 = 'netsh interface ip delete dns '
; $cmd2 = '"' & $selected_adapter & '"'
; $cmd3 = ""
; $cmdend = ""
; $message = ""
; $cmdReg = ""
; if $dnsDhcp Then
; $cmd1 = $cmd1_1
; $cmd3 = " dhcp"
; $message = "Setting DNS DHCP..."
; _asyncNewCmd($cmd1&$cmd2&$cmd3, $message, 1)
; Else
; $dnsp = _ctrlGetIP( $ip_DnsPri )
; $dnsa = _ctrlGetIP( $ip_DnsAlt )
; If BitAND(GUICtrlRead($ck_dnsReg), $GUI_CHECKED) = $GUI_CHECKED Then
; $cmdReg = "both"
; Else
; $cmdReg = "none"
; EndIf
; If $dnsp <> "" Then
; $cmd1 = $cmd1_1
; $cmd3 = " static " & $dnsp
; $message = "Setting preferred DNS server..."
; $cmdend = (_OSVersion() >= 6)?" " & $cmdReg & " no":"$cmdReg"
; _asyncNewCmd($cmd1&$cmd2&$cmd3&$cmdend, $message, 1)
; If $dnsa <> "" Then
; $cmd1 = $cmd1_2
; $cmd3 = " " & $dnsa
; $message = "Setting alternate DNS server..."
; $cmdend = (_OSVersion() >= 6)?" 2 no":""
; _asyncNewCmd($cmd1&$cmd2&$cmd3&$cmdend, $message, 1)
; EndIf
; ElseIf $dnsa <> "" Then
; $cmd1 = $cmd1_1
; $cmd3 = " static " & $dnsp
; $message = "Setting preferred DNS server..."
; $cmdend = (_OSVersion() >= 6)?" " & $cmdReg & " no":"$cmdReg"
; _asyncNewCmd($cmd1&$cmd2&$cmd3&$cmdend, $message, 1)
; Else
; $cmd1 = $cmd1_3
; $cmd3 = " all"
; $message = "Deleting DNS servers..."
; _asyncNewCmd($cmd1&$cmd2&$cmd3, $message, 1)
; EndIf
; EndIf
;EndFunc
;------------------------------------------------------------------------------
; Title...........: _OSVersion
; Description.....: Get the OS version number from the registry
;
; Parameters......:
; Return value....: Version number
;------------------------------------------------------------------------------
Func _OSVersion()
Return RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\", "CurrentVersion")
EndFunc ;==>_OSVersion
;------------------------------------------------------------------------------
; Title...........: _pull
; Description.....: Get current IP information from adapter and update display
;
; Parameters......:
; Return value....:
;------------------------------------------------------------------------------
Func _pull()
GUICtrlSetState($radio_IpMan, $GUI_CHECKED)
_ctrlSetIP($ip_Ip, GUICtrlRead($label_CurrentIp))
_ctrlSetIP($ip_Subnet, GUICtrlRead($label_CurrentSubnet))
_ctrlSetIP($ip_Gateway, GUICtrlRead($label_CurrentGateway))
GUICtrlSetState($radio_DnsMan, $GUI_CHECKED)
_ctrlSetIP($ip_DnsPri, GUICtrlRead($label_CurrentDnsPri))
_ctrlSetIP($ip_DnsAlt, GUICtrlRead($label_CurrentDnsAlt))
_radios()
EndFunc ;==>_pull
;------------------------------------------------------------------------------
; Title...........: _ctrlHasFocus
; Description.....: Check if control currently has focus
;
; Parameters......: $id -controlID
; Return value....: 0 -control does NOT have focus
; 1 -control has focus
;------------------------------------------------------------------------------
Func _ctrlHasFocus($Id)
$idFocus = ControlGetFocus($hgui)
$hFocus = ControlGetHandle($hgui, "", $idFocus)
$hCtrl = ControlGetHandle($hgui, "", $Id)
If $hFocus = $hCtrl Then
Return 1
Else
Return 0
EndIf
EndFunc ;==>_ctrlHasFocus
;------------------------------------------------------------------------------
; Title...........: _disable
; Description.....: Disable/enable selected adapter based on current state
;
; Parameters......:
; Return value....:
;------------------------------------------------------------------------------
Func _disable()
$selected_adapter = GUICtrlRead($combo_adapters)
If _GUICtrlMenu_GetItemText(GUICtrlGetHandle($toolsmenu), $disableitem, 0) = "Disable adapter" Then
_AdapterMod($selected_adapter, 0)
GUICtrlSetData($disableitem, "En&able adapter")
Else
_AdapterMod($selected_adapter, 1)
GUICtrlSetData($disableitem, "Dis&able adapter")
_setStatus("Updating Adapter List...")
_loadAdapters()
_setStatus("Ready")
EndIf
EndFunc ;==>_disable
;------------------------------------------------------------------------------
; Title...........: _onLvDoneEdit
; Description.....: When done editing listview item, rename the profile
;
; Parameters......:
; Return value....:
;------------------------------------------------------------------------------
Func _onLvDoneEdit()
$lv_newName = ControlListView($hgui, "", $list_profiles, "GetText", $lv_editIndex)
;ConsoleWrite( $lv_oldName & " " & $lv_newName )
$lv_doneEditing = 0
If $lv_aboutEditing Then
Return
EndIf
If $lv_newItem = 0 Then
_rename()
Else
_new()
EndIf
EndFunc ;==>_onLvDoneEdit
;------------------------------------------------------------------------------
; Title...........: _clear
; Description.....: Clears the IP address entry fields
;
; Parameters......:
; Return value....:
;------------------------------------------------------------------------------
Func _clear()
_ctrlSetIP($ip_Ip, "")
_ctrlSetIP($ip_Subnet, "")
_ctrlSetIP($ip_Gateway, "")
_ctrlSetIP($ip_DnsPri, "")
_ctrlSetIP($ip_DnsAlt, "")
EndFunc ;==>_clear
;------------------------------------------------------------------------------
; Title...........: _checkChangelog
; Description.....: Check if new version and change log needs to be displayed.
; Then write new version to profiles.ini file.
;
; Parameters......:
; Return value....:
;------------------------------------------------------------------------------
Func _checkChangelog()
$sVersion = Options_GetValue($options, $OPTIONS_Version)
If $sVersion <> $winVersion Then
_changeLog()
$sVersion = $winVersion
Options_SetValue($options, $OPTIONS_Version, $sVersion)
$sVersionName = Options_GetName($options, $OPTIONS_Version)
IniWrite(@ScriptDir & "/profiles.ini", "options", $sVersionName, $sVersion)
EndIf
EndFunc ;==>_checkChangelog
;------------------------------------------------------------------------------
; Title...........: _rename
; Description.....: Call the ini file rename function and modify the listview
;
; Parameters......:
; Return value....:
;------------------------------------------------------------------------------
Func _rename()
If $lv_oldName = $lv_newName Then
Return
EndIf
$ret = _iniRename(@ScriptDir & "/profiles.ini", $lv_oldName, $lv_newName)
If $ret = 2 Then
MsgBox($MB_ICONWARNING, "Warning!", "The profile name already exists!")
_GUICtrlListView_SetItemText($list_profiles, $lv_editIndex, $lv_oldName)
ElseIf $ret = 1 Or $ret = 3 Then
_setStatus("An error occurred while saving the profile name", 1)
Else
Profiles_SetValue($profiles, $lv_oldName, $PROFILES_Name, $lv_newName)
EndIf
EndFunc ;==>_rename
;------------------------------------------------------------------------------
; Title...........: _delete
; Description.....: Delete a profile
;
; Parameters......:
; Return value....:
;------------------------------------------------------------------------------
Func _delete($name = "")
;check to make sure a profile is selected
If ControlListView($hgui, "", $list_profiles, "GetSelectedCount") = 0 Then
_setStatus("No profile is selected!", 1)
Return 1
EndIf
$selIndex = ControlListView($hgui, "", $list_profiles, "GetSelected")
If Not $lv_editing Then
$profileName = StringReplace(GUICtrlRead(GUICtrlRead($list_profiles)), "|", "")
$iniName = iniNameEncode($profileName)
$ret = IniDelete(@ScriptDir & "/profiles.ini", $iniName)
If $ret = 0 Then
_setStatus("An error occurred while deleting the profile", 1)
EndIf
Profiles_Delete($profiles, $profileName)
Else
_GUICtrlListView_CancelEditLabel(ControlGetHandle($hgui, "", $list_profiles))
EndIf