home *** CD-ROM | disk | FTP | other *** search
/ Softwarová Záchrana 3 / Softwarova-zachrana-3.bin / StartRight / source.zip / UnitMyKeys.pas < prev    next >
Pascal/Delphi Source File  |  2004-10-03  |  13KB  |  459 lines

  1. unit UnitMyKeys;
  2. {
  3.     Purpose:
  4.         define all registry keys used by ME
  5.  
  6.     Notes:
  7.         There are some util functions that really don't belong here
  8.         or anywhere else
  9. }
  10.  
  11. interface
  12. uses SysUtils, StrUtils, Registry, Windows;
  13.  
  14.  
  15.  
  16.  
  17. const SR_SUB_RUN = 'Run';
  18. const SR_SUB_RUNSORT = 'RunSort';
  19. const SR_SUB_STARTUPSORT = 'StartupSort';
  20. const SR_SUB_RUNNEWITEMS = 'RunNewItems';
  21. const SR_SUB_STARTUPNEW = 'StartupNew';
  22. const SR_SUB_EXECMETHOD = 'ExecMethod';
  23. const SR_SUB_CUSTOMDELAYS = 'CustomDelays';
  24. const SR_SUB_RUN_LOCATION = 'RunLocation';
  25. const SR_SUB_STARTUP_LOCATION = 'StartupLocation';
  26.  
  27. const SR_STARTRIGHT_VALUE = 'STARTRIGHT';
  28. const SR_EXCLUDE_DATA = 'EXCLUDE';
  29. const SR_SORTINDEX_VALUE  = 'SortIndex';
  30. const SR_RUNONCE_VALUE = 'RUNONCE';
  31. const SR_NEWRUNITEMS_VALUE = 'NewRunItems';
  32. const SR_NEWSTARTUPITEMS_VALUE = 'NewStartupItems';
  33. const SR_ISINSTALLED_VALUE = 'IsInstalled';
  34. const SR_LOWCPU_STAT = 'LowCPUStat';
  35. const SR_HIGHCPU_STAT = 'HighCPUStat';
  36.  
  37.  
  38. const SR_HOME_KEY = '\SOFTWARE\JackassArswareOrg\StartRight';
  39. const SR_RUN_KEY = SR_HOME_KEY + '\' + SR_SUB_RUN;
  40. const SR_RUNEXCLUDE_KEY =  SR_HOME_KEY +'\RunExclude';
  41. const SR_RUNSORT_KEY = SR_HOME_KEY + '\' + SR_SUB_RUNSORT;
  42. const SR_RUNNEWITEMS_KEY = SR_HOME_KEY + '\' + SR_SUB_RUNNEWITEMS;
  43.  
  44. const SR_STARTUPSORT_KEY = SR_HOME_KEY + '\' + SR_SUB_STARTUPSORT;
  45. const SR_STARTUPEXCLUDE_KEY = SR_HOME_KEY +'\StartupExclude';
  46. const SR_STARTUPDISABLE_KEY = SR_HOME_KEY +'\StartupDisable';
  47. const SR_STARTUPNEW_KEY = SR_HOME_KEY + '\' + SR_SUB_STARTUPNEW;
  48.  
  49. const SR_EXECMETHOD_KEY = SR_HOME_KEY + '\' + SR_SUB_EXECMETHOD;
  50. const SR_CUSTOMDELAYS_KEY = SR_HOME_KEY + '\' + SR_SUB_CUSTOMDELAYS;
  51.  
  52. const SR_RUNDISABLED_KEY = SR_HOME_KEY + '\RunDisabled';
  53. const SR_AUTOTUNE_KEY = SR_HOME_KEY + '\AutoTune';
  54. const SR_STARTUP_LOCATION = SR_HOME_KEY + '\' +  SR_SUB_STARTUP_LOCATION;
  55. const SR_RUN_LOCATIOn = SR_HOME_KEY + '\' + SR_SUB_RUN_LOCATION;
  56.  
  57. const WINDOWS_CURRENT_VERSION = '\SOFTWARE\Microsoft\Windows\CurrentVersion';
  58. const WINDOWS_APPSPATH_KEY = WINDOWS_CURRENT_VERSION + '\App Paths';
  59. const WINDOWS_RUN_KEY = WINDOWS_CURRENT_VERSION + '\Run';
  60. const WINDOWS_RUNONCE_KEY = WINDOWS_RUN_KEY + 'Once';
  61.  
  62.  
  63. type SR_STATUS = (
  64.     SR_STATUS_NORMAL,
  65.     SR_STATUS_EXCLUDED,
  66.     SR_STATUS_DISABLED,
  67.     SR_STATUS_INCLUDED
  68. );
  69.  
  70. function GetRunkeyStatus(value : string) : SR_STATUS;
  71. function GetStartupStatus(value : string) : SR_STATUS;
  72. function GetIsNewRunkey(value : string) : boolean;
  73. function GetIsNewStartup(value : string) : boolean;
  74.  
  75. function GetRunSortCount : integer;
  76. function GetRunSortValue(index : integer) : string;
  77. function GetRunSortData(index : integer) : string; overload;
  78. function GetRunSortData(value : string) : string; overload;
  79.  
  80. function GetStartupSortCount : integer;
  81.  
  82. function GetIsExcludedRunkey(value : string) : boolean;
  83. function GetIsExcludedStartup(value : string) : boolean;
  84.  
  85. function GetEXEPathFromRunValue(value : string) : string;
  86. function FindExecutableFromRunValue(value : string) : string;
  87.  
  88. function GetCustomDelay(value : string) : integer;
  89. procedure SetCustomDelay(value : string; delay : integer);
  90. function GetTunnedCustomDelay(value : string) : integer;
  91. procedure SetTunnedCustomDelay(value : string; delay : integer);
  92. function ReadStat(name : string; default : integer = 0) : integer;
  93. procedure UpdateStat(name : string; value : integer);
  94. function ReadPreviousStat(name : string; default : integer = 0) : integer;
  95. procedure UpdatePreviousStat(name : string; value : integer);
  96.  
  97.  
  98. procedure SetStartupLocation(value : string; location : string);
  99. procedure SetRunLocation(value : string; location: string);
  100. function GetStartupLocation(value : string) : string;
  101. function GetRunLocation(value : string) : string;
  102. procedure SetRunLocationRootkey(value : string; HKEYVal : HKEY);
  103. function GetRunLocationRootkey(value : string) : HKEY;
  104.  
  105. //procedure SetCurrentUsername(name : string);
  106. //function GetCurrentUsername : string;
  107.  
  108. function GetLowCPUStat : integer;
  109. function GetHighCPUStat : integer;
  110. procedure SaveLowHighCPUStats;
  111.  
  112. {////////////////////}
  113. {//}implementation{//}
  114. {////////////////////}
  115.  
  116. uses UnitMyRegistry,  Dialogs;
  117. var r : TMyRegistry;
  118. var LowCPUStat : integer;
  119. var HighCPUStat : integer;
  120. const HKEY_STRING = '_HKEY';
  121.  
  122. //-----------------------------------------------------
  123. // util functions
  124. // - getting and setting a string
  125. // - EXE stuff
  126. //-----------------------------------------------------
  127.  
  128.  
  129. function FindExecutableFromRunValue(value : string) : string;
  130. begin
  131.     // extract EXE fullname from 'value'
  132.     // if it doesn't exists, look it up in the registry under "AppsPath"
  133.     // if it still doesn't exist, return an empty string
  134.  
  135.     result := GetEXEPathFromRunValue(value);
  136.     if (result <> '') and
  137.         (not FileExists(result)) then begin
  138.  
  139.         result := r.GetDataString(WINDOWS_APPSPATH_KEY + '\' + result, '');
  140.         if (not FileExists(result)) then begin
  141.             result := '';
  142.         end;
  143.     end;
  144. end;
  145.  
  146.  
  147. function GetEXEPathFromRunValue(value : string) : string;
  148. var i : integer;
  149. begin
  150.     i := pos('.exe', lowercase(value));
  151.     result := '';
  152.  
  153.     if (i > 0) then begin
  154.         result := LeftStr(value, i + 3);
  155.  
  156.         if leftstr(result,1) ='"' then
  157.             result := RightStr(result, length(result) - 1);
  158.     end;
  159. end;
  160.  
  161.  
  162.  
  163.  
  164. //-----------------------------------------------------
  165. // end of util functions
  166. //-----------------------------------------------------
  167.  
  168.  
  169. procedure SetCurrentUsername(name : string);
  170. begin
  171.     r.SetDataString(HKEY_CURRENT_USER,SR_HOME_KEY, 'username', name);
  172. end;
  173. function GetCurrentUsername : string;
  174. begin
  175.     result := r.GetDataString(HKEY_CURRENT_USER,SR_HOME_KEY, 'username');
  176. end;
  177.  
  178.  
  179. procedure SetStartupLocation(value : string; location : string);
  180. begin
  181.     r.SetDataString(SR_STARTUP_LOCATION, value, location);
  182. end;
  183. procedure SetRunLocation(value : string; location: string);
  184. begin
  185.     r.SetDataString(SR_RUN_LOCATION, value, location);
  186. end;
  187. procedure SetRunLocationRootkey(value : string; HKEYVal : HKEY);
  188. begin
  189.     r.SetDataInteger(SR_RUN_LOCATION, value + HKEY_STRING, HKEYVal);
  190. end;
  191. function GetRunLocationRootkey(value : string) : HKEY;
  192. begin
  193.     result := r.GetDataInteger(SR_RUN_LOCATION, value + HKEY_STRING);
  194. end;
  195.  
  196.  
  197. function GetStartupLocation(value : string) : string;
  198. begin
  199.     result := r.GetDataString(SR_STARTUP_LOCATION,value);
  200. end;
  201. function GetRunLocation(value : string) : string;
  202. begin
  203.     result := r.GetDataString(SR_RUN_LOCATION, value);
  204. end;
  205.  
  206.  
  207. //-----------------------------------------------------
  208. // stat functions
  209. //-----------------------------------------------------
  210.  
  211. function GetLowCPUStat : integer;
  212. begin
  213.     if not r.ValueExistsInteger(SR_AUTOTUNE_KEY, SR_LOWCPU_STAT, result) then begin
  214.         result := 0;
  215.     end;
  216. end;
  217. function GetHighCPUStat : integer;
  218. begin
  219.     if not r.ValueExistsInteger(SR_AUTOTUNE_KEY, SR_HIGHCPU_STAT, result) then begin
  220.         result := 0;
  221.     end;
  222. end;
  223.  
  224. procedure SaveLowHighCPUStats;
  225. begin
  226.     r.SetDataInteger(SR_AUTOTUNE_KEY, SR_LOWCPU_STAT , LowCPUStat);
  227.     r.SetDataInteger(SR_AUTOTUNE_KEY, SR_HIGHCPU_STAT, HighCPUStat);
  228. end;
  229.  
  230.  
  231. function ReadStat(name : string; default : integer = 0) : integer;
  232. begin
  233.     result := 0;
  234.     r.RootKey := HKEY_LOCAL_MACHINE;
  235.     if (r.OpenKey(SR_AUTOTUNE_KEY, true)) then begin
  236.         if (r.ValueExists(name)) then begin
  237.             result := r.ReadInteger(name, default);
  238.         end;
  239.         r.CloseKey;
  240.     end;
  241. end;
  242. procedure UpdateStat(name : string; value : integer);
  243. var x : integer;
  244. begin
  245.  
  246.     x := ReadStat(name, 10);
  247.  
  248.     // quick and dirty exponential average (%new_value + %history)
  249.     x := trunc((value * 0.60) + (x * 0.40));
  250.  
  251.     if (x < lowCPUStat) then lowCPUStat := x;
  252.     if (x > highCPUStat) then HighCPUStat := x;
  253.  
  254.  
  255.     r.SetDataInteger(SR_AUTOTUNE_KEY, name, x);
  256. end;
  257.  
  258. procedure UpdatePreviousStat(name : string; value : integer);
  259. begin
  260.     r.SetDataInteger(SR_AUTOTUNE_KEY, name +   '_PREVIOUS', value);
  261. end;
  262.  
  263. function ReadPreviousStat(name : string; default : integer = 0) : integer;
  264. begin
  265.     result := ReadStat(name + '_PREVIOUS', default);
  266. end;
  267.  
  268.  
  269. function GetTunnedCustomDelay(value : string) : integer;
  270. begin
  271.     result := 0;
  272.     value := value +  '_tunned';
  273.     r.RootKey := HKEY_LOCAL_MACHINE;
  274.     if r.OpenKeyReadOnly(SR_CUSTOMDELAYS_KEY) then begin
  275.         if r.ValueExists(value) then begin
  276.             result := r.ReadInteger(value);
  277.         end;
  278.  
  279.         r.CloseKey;
  280.     end;
  281. end;
  282.  
  283. procedure SetTunnedCustomDelay(value : string; delay : integer);
  284. begin
  285.     value := value +  '_tunned';
  286.     r.SetDataInteger(SR_CUSTOMDELAYS_KEY, value, delay);
  287. end;
  288.  
  289.  
  290. function GetCustomDelay(value : string) : integer;
  291. begin
  292.     result := 0;
  293.     r.RootKey := HKEY_LOCAL_MACHINE;
  294.     if r.OpenKeyReadOnly(SR_CUSTOMDELAYS_KEY) then begin
  295.         if r.ValueExists(value) then begin
  296.             result := r.ReadInteger(value);
  297.         end;
  298.  
  299.         r.CloseKey;
  300.     end;
  301. end;
  302.  
  303. procedure SetCustomDelay(value : string; delay : integer);
  304. begin
  305.     r.SetDataInteger(SR_CUSTOMDELAYS_KEY, value, delay);
  306. end;
  307.  
  308.  
  309. //-----------------------------------------------------
  310. // Status function
  311. //-----------------------------------------------------
  312.  
  313. function GetRunkeyStatus(value : string) : SR_STATUS;
  314. var i : integer;
  315. begin
  316.     result := SR_STATUS_NORMAL;
  317.  
  318.     if GetIsExcludedRunkey(value) then begin
  319.         result := SR_STATUS_EXCLUDED;
  320.     end;
  321.  
  322.     r.RootKey := HKEY_LOCAL_MACHINE;
  323.     if (r.OpenKeyReadOnly(SR_RUNDISABLED_KEY)) then begin
  324.         if r.ValueExists(value) then begin
  325.             result := SR_STATUS_DISABLED;
  326.         end;
  327.         r.CloseKey;
  328.     end;
  329.  
  330.     for i := 0 to GetRunSortCount - 1 do begin
  331.          if (lowercase(GetRunSortValue(i)) = lowercase(value)) then begin
  332.             result := SR_STATUS_INCLUDED;
  333.             BREAK;
  334.          end;
  335.     end;
  336. end;
  337.  
  338.  
  339. function GetIsExcludedRunkey(value : string) : boolean;
  340. begin
  341.     result := (value = SR_STARTRIGHT_VALUE);
  342.     if result then EXIT;
  343.  
  344.     r.RootKey := HKEY_LOCAL_MACHINE;
  345.     if (not r.OpenKeyReadOnly(SR_RUNEXCLUDE_KEY)) then EXIT;
  346.     result :=  r.ValueExists(value);
  347.     r.CloseKey;
  348. end;
  349.  
  350. function GetIsExcludedStartup(value : string) : boolean;
  351. begin
  352.     result := false;
  353.  
  354.     r.RootKey := HKEY_LOCAL_MACHINE;
  355.     if (not r.OpenKeyReadOnly(SR_STARTUPEXCLUDE_KEY)) then EXIT;
  356.     result := r.ValueExists(value);
  357.     r.CloseKey;
  358. end;
  359.  
  360.  
  361. function GetStartupStatus(value : string) : SR_STATUS;
  362. var i, count : integer;
  363.     s : string;
  364.     filename : string;
  365. begin
  366.     result := SR_STATUS_NORMAL;
  367.  
  368.     if GetIsExcludedStartup(value) then begin
  369.         result := SR_STATUS_EXCLUDED;
  370.     end;
  371.  
  372.     count := GetStartupSortCount;
  373.     for i := 0 to (count - 1) do begin
  374.         s := r.GetDataString(SR_STARTUPSORT_KEY, IntToStr(i));
  375.         filename := Lowercase(ExtractFileName(s));
  376.  
  377.         if (filename = Lowercase(ExtractFilename(value))) then begin
  378.             result := SR_STATUS_INCLUDED;
  379.         end;
  380.     end;
  381.  
  382.     if (r.ValueExistsString(SR_STARTUPDISABLE_KEY, value, s)) then begin
  383.         result := SR_STATUS_DISABLED;
  384.     end;
  385. end;
  386.  
  387.  
  388.  
  389. function GetIsNewRunkey(value : string) : boolean;
  390. var data : string;
  391. begin
  392.     result := r.ValueExistsString(SR_RUNNEWITEMS_KEY, value, data);
  393. end;
  394. function GetIsNewStartup(value : string) : boolean;
  395. var data : string;
  396. begin
  397.     result := r.ValueExistsString(SR_STARTUPNEW_KEY, value, data);
  398. end;
  399.  
  400.  
  401.  
  402. function GetCount(Key : string) : integer;
  403. var i : integer;
  404. begin
  405.     result := 0; // default to failure
  406.  
  407.     if r.ValueExistsInteger(key, SR_SORTINDEX_VALUE, i) then begin
  408.         result := i;
  409.     end;
  410. end;
  411.  
  412.  
  413. function GetRunSortCount : integer;
  414. begin
  415.     result := GetCount(SR_RUNSORT_KEY);
  416. end;
  417.  
  418. function GetStartupSortCount : integer;
  419. begin
  420.     result := GetCount(SR_STARTUPSORT_KEY);
  421. end;
  422.  
  423. function GetRunSortValue(index : integer) : string;
  424. begin
  425.     result := r.GetDataString(SR_RUNSORT_KEY, IntToStr(index));
  426. end;
  427.  
  428. function GetRunSortData(index : integer) : string;
  429. var s : string;
  430. begin
  431.     result := ''; // default to failure
  432.  
  433.     s := GetRunSortValue(index);
  434.     if (s = '') then EXIT;
  435.  
  436.     result := GetRunSortData(s);
  437. end;
  438.  
  439.  
  440. function GetRunSortData(value : string) : string;
  441. begin
  442.     result := r.GetDataString(SR_RUN_KEY, value);
  443. end;
  444.  
  445.  
  446.  
  447. initialization
  448. begin
  449.     r := TMyRegistry.Create;
  450.     LowCPUStat := 100;
  451.     HighCPUStat := 0;
  452. end
  453.  
  454. finalization
  455. begin
  456.     r.Free;
  457. end;
  458. end.
  459.