home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Softwarová Záchrana 3
/
Softwarova-zachrana-3.bin
/
StartRight
/
source.zip
/
UnitItemManager.pas
< prev
next >
Wrap
Pascal/Delphi Source File
|
2004-10-03
|
19KB
|
680 lines
unit UnitItemManager;
{
Purpose:
Create one centralized location where items
are Excluded/Disabled/Included. PPP
Updates:
Fix: AutoExcludeRunkeyItems resorted startup items
instead of runkey items.
Items that are excluded will be removed from disabled lists
(They can be both excluded and disabled because of the AutoExcluding)
Notes:
When an Item is removed from a sorted list, the sorted list
must be recreated to keep it contiguous.
}
interface
uses UnitMyRegistry;
type TItemManager = class(TObject)
private
r : TMyRegistry;
procedure AddToRunkeySortItems(value : string);
procedure AddToStartupfolderItem(filename : string);
procedure AddItem(key : string; itemdata : string);
procedure DeleteStartupSortItem(filename : string);
procedure MoveFile(FullName, ToPath : string);
public
constructor Create;
destructor Destroy; override;
procedure AutoExcludeRunkeyItems;
procedure AutoExcludeStartupItems;
procedure DisableRunkeyItem(value : string);
procedure DisableStartupItem(filename : string; noResort : boolean = false);
procedure ExcludeRunkeyItem(value : string);
procedure ExcludeStartupItem(shortcutname : string);
procedure IncludeRunkeyItem(value : string; currentuser : boolean = false);
function IncludeStartupItem(filename : string) : string;
procedure EnableRunkeyItem(value : string);
procedure EnableStartupItem(filename : string);
procedure CorrectExcludedShortcutItem(value : string);
procedure ResortRunkeySortItems;
procedure ResortStartupSortItems;
end;
var ItemManager : TItemManager;
{////////////////////}
{//}implementation{//}
{////////////////////}
uses Classes, Windows, UnitMyKeys, StrUtils, SysUtils, ShellAPI, ShlObj,
UnitFrmDummyRunner, Forms, UnitSpecialPaths;
constructor TItemManager.Create;
begin
r := TMyRegistry.Create;
end;
destructor TItemManager.Destroy;
begin
r.Free;
inherited Destroy;
end;
procedure TItemManager.CorrectExcludedShortcutItem(value : string);
var s, cs, als : string;
begin
s := IncludeTrailingPathDelimiter( SpecialPaths.GetStartupPath);
cs := IncludeTrailingPathDelimiter( SpecialPaths.GetCommonStartupPath);
als := IncludeTrailingPathDelimiter( SpecialPaths.GetAltStartupPath);
if FileExists(s + value) then begin
r.SetDataString(SR_STARTUPEXCLUDE_KEY, value, s + value);
end else if FileExists(cs + value) then begin
r.SetDataString(SR_STARTUPEXCLUDE_KEY, value, cs + value);
end else if FileExists(als + value) then begin
r.SetDataString(SR_STARTUPEXCLUDE_KEY, value, als + value);
end;
end;
procedure TItemManager.DisableStartupItem(filename : string; noResort : boolean = false);
var j, index : longint;
value : string;
begin
// write the value to the disabled items
// find the sorted item and remove it
r.SetDataString(SR_STARTUPDISABLE_KEY,ExtractFilename(filename),filename);
try
index := UnitMyKeys.GetStartupSortCount;
for j := 0 to (index - 1) do begin
value := r.GetDataString(SR_STARTUPSORT_KEY, IntToStr(j));
if lowercase(value) = lowercase(filename) then begin
r.DeleteDataString(SR_STARTUPSORT_KEY, IntToStr(j));
end;
end;
except
end;
self.ResortStartupSortItems;
end;
procedure TItemManager.AutoExcludeStartupItems;
procedure ScanForFiles(path : string; files : TStringList);
var rec : TSearchRec;
r : integer;
begin
if (trim(path) <> '') then begin
path := IncludeTrailingPathDelimiter(path);
r := findfirst(path + '*.*', faHidden , rec);
while r = 0 do begin
files.Add(rec.Name);
r := findnext(rec);
end;
end;
end;
var
Startup, CommonStartup, AltStartup : string;
newExcludes, files : TStringList;
i, cnt : longint;
s : string;
begin
files := TStringList.Create;
newExcludes := TStringList.Create;
//
// get all existing startup folder files
//
Startup := SpecialPaths.GetStartupPath;
CommonStartup := SpecialPaths.GetCommonStartupPath;
AltStartup := SpecialPaths.GetAltStartupPath;
ScanForFiles(Startup, files);
ScanForFiles(CommonStartup, files);
ScanForFiles(AltStartup, files);
//
// search for my startup files for matches in the startupsort key
// exclude all matches (remove the startup sort item and add it to excluded list)
//
cnt := UnitMyKeys.GetStartupSortCount;
for i := 0 to (cnt - 1) do begin
s := ExtractFilename(r.GetDataString(SR_STARTUPSORT_KEY, IntToStr(i)));
if (files.IndexOf(s) <> - 1) then begin
r.DeleteDataString(SR_STARTUPSORT_KEY, IntToStr(i));
newExcludes.Add(s);
end;
end;
for i := 0 to (newExcludes.Count - 1) do begin
ExcludeStartupItem(newExcludes.Strings[i]);
end;
files.Free;
newExcludes.Free;
end;
procedure TItemManager.ExcludeStartupItem(shortcutname : string);
var src, startup : string;
begin
//
// move file back to system's startup folder
// write to excluded list
// remove from disabled list
// (autoexcluded items that are also disabled don't work anyways)
startup := SpecialPaths.GetCommonStartupPath;
src := SpecialPaths.GetStartRightStartup;
MoveFile(src + ExtractFilename(shortcutname), Startup);
r.SetDataString(
SR_STARTUPEXCLUDE_KEY,
ExtractFilename(ShortcutName),
IncludeTrailingPathDelimiter(Startup) + ExtractFilename(Shortcutname)
);
r.DeleteDataString(SR_STARTUPDISABLE_KEY, ExtractFilename(shortcutname));
{
r.RootKey := HKEY_LOCAL_MACHINE;
if (r.OpenKey(SR_STARTUPEXCLUDE_KEY, true)) then begin
r.WriteString(ExtractFilename(ShortcutName),
IncludeTrailingPathDelimiter(Startup) + ExtractFilename(Shortcutname));
r.CloseKey;
end;
r.RootKey := HKEY_LOCAL_MACHINE;
if (r.OpenKey(SR_STARTUPDISABLE_KEY, false)) then begin
r.DeleteValue(ExtractFilename(shortcutname));
r.closekey;
end;
}
self.DeleteStartupSortItem(shortcutname);
end;
procedure TItemManager.AutoExcludeRunkeyItems;
var winrun, myrun, exclude : TStringList;
i : longint;
s : string;
begin
winrun := TStringList.Create;
myrun := TStringList.Create;
exclude := TStringList.Create;
//
// get both run keys and compare for matches
// on a match, delete from my run key and add to excluded items
//
r.GetValues(WINDOWS_RUN_KEY, winrun);
r.GetValues(SR_RUN_KEY, myrun);
for i := 0 to (myrun.Count - 1) do begin
s := myrun.Strings[i];
if (winrun.IndexOf(s) <> -1) then begin
exclude.Add(s);
r.DeleteDataString(SR_RUN_KEY, s);
end;
end;
self.ResortRunkeySortItems;
for i := 0 to (exclude.Count - 1) do begin
r.SetDataString(SR_RUNEXCLUDE_KEY, exclude.Strings[i], SR_EXCLUDE_DATA);
end;
{
r.RootKey := HKEY_LOCAL_MACHINE;
if (r.OpenKey(SR_RUNEXCLUDE_KEY, false)) then begin
for i := 0 to (exclude.Count - 1) do begin
r.WriteString(exclude.Strings[i], SR_EXCLUDE_DATA);
end;
r.CloseKey;
end;
}
winrun.free;
myrun.Free;
exclude.free;
end;
procedure TItemManager.ExcludeRunkeyItem(value : string);
var data : string;
begin
// add the items to the registry excluded list
// read the data from SR's run key and delete the value
// if was Disabled - it didn't work so remove it from disabled list
// add the value/data back to the Windows' Run key
r.SetDataString(SR_RUNEXCLUDE_KEY, value, SR_EXCLUDE_DATA);
data := r.DeleteDataString(SR_RUN_KEY, value);
if (data <> '') then begin
self.ResortRunkeySortItems;
r.DeleteDataString(SR_RUNDISABLED_KEY, value);
r.SetDataString(WINDOWS_RUN_KEY, value, data);
end;
{
r.RootKey := HKEY_LOCAL_MACHINE;
if (r.OpenKey(SR_RUNEXCLUDE_KEY, true)) then begin
r.WriteString(value, SR_EXCLUDE_DATA);
r.CloseKey;
end;
data := '';
r.RootKey := HKEY_LOCAL_MACHINE;
if (r.OpenKey(SR_RUN_KEY, false)) then begin
if r.ValueExists(value) then begin
data := r.ReadString(value);
r.DeleteValue(value);
end;
r.CloseKey;
end;
}
{
if (data <> '') then begin
self.ResortRunkeySortItems;
r.RootKey := HKEY_LOCAL_MACHINE;
if r.OpenKey(SR_RUNDISABLED_KEY, false) then begin
r.DeleteValue(value);
r.CloseKey;
end;
r.RootKey := HKEY_LOCAL_MACHINE;
if (r.OpenKey(WINDOWS_RUN_KEY, false)) then begin
r.WriteString(value, data);
r.CloseKey;
end;
end;
}
end;
procedure TItemManager.DisableRunkeyItem(value : string);
var i : longint;
s, rundata : string;
begin
// If disabled item exists in included items sortlist, remove/resort
rundata := UnitMyKeys.GetRunSortData(value);
if (rundata <> '') then begin
for i := 0 to (UnitMyKeys.GetRunSortCount - 1) do begin
s := UnitMyKeys.GetRunSortValue(i);
if s <> value then CONTINUE;
RunData := UnitMyKeys.GetRunSortData(i);
r.DeleteDataString(SR_RUNSORT_KEY, IntToStr(i));
{
r.RootKey := HKEY_LOCAL_MACHINE;
if (r.OpenKey(SR_RUNSORT_KEY, false)) then begin
r.DeleteValue(IntToStr(i));
r.CloseKey;
end;
}
end;
// Add the entry into the RunDisabled key, so that it
// may be enabled at a future date
r.SetDataString(SR_RUNDISABLED_KEY, value, rundata);
{
r.RootKey := HKEY_LOCAL_MACHINE;
if (r.OpenKey(SR_RUNDISABLED_KEY, true)) then begin
r.WriteString(value, rundata);
r.CloseKey;
end;
}
end;
// remove from system's run key
r.DeleteDataString(WINDOWS_RUN_KEY, value);
{
r.RootKey := HKEY_LOCAL_MACHINE;
if (r.OpenKey(WINDOWS_RUN_KEY, false)) then begin
r.DeleteValue(value);
r.CloseKey;
end;
}
// resort since we just screwed up the contiguos numbers
// in the sort key
self.ResortRunkeySortItems;
end;
procedure TItemManager.IncludeRunkeyItem(value : string; currentuser : boolean = false);
var rundata : string;
rkey : HKEY;
begin
// Find the item in windows' run key and delete
if (currentuser) then begin
rKey := HKEY_CURRENT_USER;
end else begin
rKey := HKEY_LOCAL_MACHINE;
end;
rundata := r.DeleteDataString(rkey, WINDOWS_RUN_KEY, value);
// Ignore if item doesn't exist
// write into my runkey
if (rundata <> '') then begin
r.SetDataString(HKEY_LOCAL_MACHINE, SR_RUN_KEY, value, rundata);
self.AddToRunkeySortItems(value);
self.ResortRunkeySortItems; {just to be sure}
end;
// remove from my excluded list
// and from run key
r.DeleteDataString(rkey, SR_RUNEXCLUDE_KEY, value);
end;
function TItemManager.IncludeStartupItem(filename : string) : string;
var dest : string;
begin
result := '';
// move the file back to the SR's startup folder
// remove the item from the exclusion list
dest := SpecialPaths.GetStartRightStartup;
if FileExists(filename) then begin
MoveFile(FileName, dest);
result := IncludeTrailingPathDelimiter(dest) + ExtractFilename(filename);
end;
r.DeleteDataString(SR_STARTUPEXCLUDE_KEY, ExtractFileName(filename));
{
r.RootKey := HKEY_LOCAL_MACHINE;
if r.OpenKey(SR_STARTUPEXCLUDE_KEY, false) then begin
r.DeleteValue(ExtractFileName(filename));
r.CloseKey;
end;
}
self.AddToStartupfolderItem(IncludeTrailingPathDelimiter(dest)
+ ExtractFilename(filename));
end;
procedure TItemManager.EnableRunkeyItem(value : string);
var rundata : string;
begin
// Find the data in my disabled list
// and remove it
rundata := r.DeleteDataString(SR_RUNDISABLED_KEY, value);
{
r.RootKey := HKEY_LOCAL_MACHINE;
if r.OpenKey(SR_RUNDISABLED_KEY, false) then begin
rundata := r.ReadString(value);
r.DeleteValue(value);
r.CloseKey;
end;
}
// Ignore if item doesn't exist
// write into my runkey
if (rundata <> '') then begin
r.SetDataString(SR_RUN_KEY, value, rundata);
{
r.RootKey := HKEY_LOCAL_MACHINE;
if r.OpenKey(SR_RUN_KEY, true) then begin
r.WriteString(value, rundata);
r.CloseKey;
end;
}
self.AddToRunkeySortItems(value);
self.ResortRunkeySortItems; {just to be sure}
end;
end;
procedure TItemManager.EnableStartupItem(filename : string);
begin
// remove the disable key
r.DeleteDataString(SR_STARTUPDISABLE_KEY,ExtractFilename(filename));
{
r.RootKey := HKEY_LOCAL_MACHINE;
if (r.OpenKey(SR_STARTUPDISABLE_KEY, false)) then begin
r.DeleteValue(ExtractFilename(filename));
r.closekey;
end;
}
self.AddToStartupfolderItem(filename);
end;
//====================================
// Util methods
//====================================
procedure TItemManager.ResortRunKeySortItems;
var {runvalues, values,} data : TStringList;
s : string;
i : longint;
begin
data := TStringList.Create;
data.Sorted := false;
// generate an ordered list of valid RunSort data
for i := 0 to (UnitMyKeys.GetRunSortCount - 1) do begin
s := UnitMyKeys.GetRunSortData(i);
if (s <> '') then begin
data.Add(UnitMyKeys.GetRunSortValue(i));
end;
end;
// recreate the sort list using the verified ordered list
r.EraseKey(SR_HOME_KEY, SR_SUB_RUNSORT);
for i := 0 to (data.Count - 1) do begin
r.SetDataString(SR_RUNSORT_KEY,IntToStr(i), data.Strings[i]);
end;
r.SetDataInteger(SR_RUNSORT_KEY, SR_SORTINDEX_VALUE, data.count);
{
r.RootKey := HKEY_LOCAL_MACHINE;
if (r.OpenKey(SR_HOME_KEY, false)) then begin
r.DeleteKey(SR_SUB_RUNSORT);
r.CreateKey(SR_SUB_RUNSORT);
r.CloseKey;
end;
r.RootKey := HKEY_LOCAL_MACHINE;
if (r.OpenKey(SR_RUNSORT_KEY, false)) then begin
for i := 0 to (data.Count - 1) do begin
r.WriteString(IntToStr(i), data.Strings[i]);
end;
r.WriteInteger(SR_SORTINDEX_VALUE, data.count);
r.CloseKey;
end;
}
data.Free;
end;
procedure TItemManager.ResortStartupSortItems;
begin
self.DeleteStartupSortItem('thisitemdoesnotexistever');
end;
//================================
// Private Implemenation
//================================
procedure TItemManager.AddItem(key : string; itemdata : string);
var sortIndex : integer;
s : string;
begin
if (not r.ValueExistsInteger(key, SR_SORTINDEX_VALUE, sortIndex)) then begin
sortIndex := 0;
end;
if (not r.ValueExistsString(key, IntToStr(sortIndex), s)) then begin
r.SetDataString(key, IntToStr(sortIndex), itemdata);
r.SetDataInteger(key, SR_SORTINDEX_VALUE, sortIndex + 1);
end;
{
r.RootKey := HKEY_LOCAL_MACHINE;
if (r.OpenKey(KEY, true)) then begin
// read the sort index (default if doesn't exist)
// write new entry at end of list
// increment the sort index
if (not r.ValueExists(SR_SORTINDEX_VALUE)) then begin
sortIndex := 0;
end else begin
sortIndex := r.ReadInteger(SR_SORTINDEX_VALUE);
end;
//Add only if doesn't already exist
if (data.IndexOf(itemdata) = -1) then begin
r.WriteString(IntToStr(sortIndex), itemdata);
r.WriteInteger(SR_SORTINDEX_VALUE, sortIndex + 1);
end;
r.CloseKey;
end;
}
end;
procedure TItemManager.AddToStartupfolderItem(filename : string);
var s1, s2 : string;
i : integer;
begin
// ignore duplicates
s1 := lowercase(ExtractFileName(filename));
for i := 0 to UnitMyKeys.GetStartupSortCount - 1 do begin
s2 := r.GetDataString(SR_STARTUPSORT_KEY, IntToStr(i));
s2 := lowercase(ExtractFileName(s2));
if s1 = s2 then EXIT;
end;
self.AddItem(SR_STARTUPSORT_KEY, filename);
end;
procedure TItemManager.AddToRunkeySortItems(value : string);
begin
self.AddItem(SR_RUNSORT_KEY, value);
end;
procedure TItemManager.DeleteStartupSortItem(filename : string);
var values, data : TStringList;
i : integer;
s : string;
begin
//
values := TStringList.Create();
data := TStringList.Create();
//
// extract the existing values/data
// [minus the sort index]
// [delete any entries matching filename]
r.GetValues(SR_STARTUPSORT_KEY, values);
values.Delete(values.IndexOf(SR_SORTINDEX_VALUE));
for i := (values.Count - 1) downto 0 do begin
s := r.GetDataString(SR_STARTUPSORT_KEY, values[i]);
if (filename <> s) or (filename = '') then begin
data.Add(s);
end else begin
values.Delete(i);
end;
end;
//
// delete existing and recreate
r.EraseKey(SR_HOME_KEY, SR_SUB_STARTUPSORT);
for i := 0 to (values.Count - 1) do begin
r.SetDataString(SR_STARTUPSORT_KEY, IntToStr(i), data[i]);
end;
r.SetDataInteger(SR_STARTUPSORT_KEY, SR_SORTINDEX_VALUE, values.count);
{
r.RootKey := HKEY_LOCAL_MACHINE;
if (r.OpenKey(SR_HOME_KEY, false)) then begin
r.DeleteKey(SR_SUB_STARTUPSORT);
r.CreateKey(SR_SUB_STARTUPSORT);
r.CloseKey;
end;
r.RootKey := HKEY_LOCAL_MACHINE;
if (r.OpenKey(SR_STARTUPSORT_KEY, false)) then begin
for i := 0 to (values.Count - 1) do begin
r.WriteString(IntToStr(i), data[i]);
end;
r.WriteInteger(SR_SORTINDEX_VALUE, values.count);
r.CloseKey;
end;
}
values.free;
data.Free;
end;
procedure TItemManager.MoveFile(FullName, ToPath : string);
var exeName : string;
dest : string;
begin
exeName := ExtractFileName(FullName);
dest := IncludeTrailingPathDelimiter(ToPath) + exeName;
if (lowercase(dest) <> lowercase(fullname)) then begin
if FileExists( dest ) then begin
DeleteFile( dest );
end;
if CopyFile(PChar(FullName), PChar(Dest), true) then begin
DeleteFile(FullName);
end;
end;
end;
initialization
ItemManager := TItemManager.Create();
end.