-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDesktopShortcuts.Settings.pas
More file actions
127 lines (106 loc) · 3.49 KB
/
Copy pathDesktopShortcuts.Settings.pas
File metadata and controls
127 lines (106 loc) · 3.49 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
unit DesktopShortcuts.Settings;
interface
uses
System.Classes,
System.IniFiles,
System.IOUtils,
System.SysUtils,
Vcl.Menus;
type
TDesktopShortcutItem = record
DesktopName: string;
Shortcut: TShortCut;
end;
TDesktopShortcutItems = array of TDesktopShortcutItem;
TDesktopShortcutSettings = class
private const
CFileName = 'DesktopShortcuts.ini';
CSection = 'Shortcuts';
private
class function SameDesktopName(const AFirstName: string;
const ASecondName: string): Boolean; static;
public
class function FileName: string; static;
class procedure Load(out AItems: TDesktopShortcutItems); static;
class procedure Save(const AItems: TDesktopShortcutItems); static;
class procedure Synchronize(ADesktopNames: TStrings;
var AItems: TDesktopShortcutItems); static;
end;
implementation
class function TDesktopShortcutSettings.FileName: string;
begin
var LBaseFolder := GetEnvironmentVariable('APPDATA');
if LBaseFolder.IsEmpty then
LBaseFolder := TPath.GetHomePath;
Result := TPath.Combine(TPath.Combine(LBaseFolder, 'Code4D\DesktopShortcuts'), CFileName);
end;
class procedure TDesktopShortcutSettings.Load(out AItems: TDesktopShortcutItems);
begin
SetLength(AItems, 0);
if not FileExists(TDesktopShortcutSettings.FileName) then
Exit;
var LIniFile := TIniFile.Create(TDesktopShortcutSettings.FileName);
try
var LCount := LIniFile.ReadInteger(CSection, 'Count', 0);
if LCount < 0 then
LCount := 0;
SetLength(AItems, LCount);
for var i := 0 to Pred(LCount) do
begin
var LKeySuffix := Succ(i).ToString;
AItems[i].DesktopName := LIniFile.ReadString(CSection, 'Desktop' + LKeySuffix,
AItems[i].DesktopName);
AItems[i].Shortcut := TShortCut(LIniFile.ReadInteger(CSection, 'Shortcut' + LKeySuffix,
AItems[i].Shortcut));
end;
finally
LIniFile.Free;
end;
end;
class function TDesktopShortcutSettings.SameDesktopName(const AFirstName: string;
const ASecondName: string): Boolean;
begin
Result := AFirstName.ToUpper = ASecondName.ToUpper;
end;
class procedure TDesktopShortcutSettings.Save(const AItems: TDesktopShortcutItems);
begin
ForceDirectories(ExtractFilePath(TDesktopShortcutSettings.FileName));
var LIniFile := TIniFile.Create(TDesktopShortcutSettings.FileName);
try
LIniFile.EraseSection(CSection);
LIniFile.WriteInteger(CSection, 'Count', Length(AItems));
for var i := 0 to High(AItems) do
begin
var LKeySuffix := Succ(i).ToString;
LIniFile.WriteString(CSection, 'Desktop' + LKeySuffix, AItems[i].DesktopName);
LIniFile.WriteInteger(CSection, 'Shortcut' + LKeySuffix, AItems[i].Shortcut);
end;
LIniFile.UpdateFile;
finally
LIniFile.Free;
end;
end;
class procedure TDesktopShortcutSettings.Synchronize(ADesktopNames: TStrings;
var AItems: TDesktopShortcutItems);
begin
if not Assigned(ADesktopNames) or (ADesktopNames.Count = 0) then
Exit;
var LSavedItems: TDesktopShortcutItems;
SetLength(LSavedItems, Length(AItems));
for var i := 0 to High(AItems) do
LSavedItems[i] := AItems[i];
SetLength(AItems, ADesktopNames.Count);
for var i := 0 to Pred(ADesktopNames.Count) do
begin
AItems[i].DesktopName := ADesktopNames[i];
AItems[i].Shortcut := 0;
for var j := 0 to High(LSavedItems) do
if TDesktopShortcutSettings.SameDesktopName(ADesktopNames[i],
LSavedItems[j].DesktopName) then
begin
AItems[i].Shortcut := LSavedItems[j].Shortcut;
Break;
end;
end;
end;
end.