-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimagelist.pas
183 lines (160 loc) · 4.33 KB
/
imagelist.pas
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
unit ImageList;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, contnrs;
type
TFilenameGroup = class
public
Count: integer;
Name: string;
Filenames: TStringList;
constructor Create;
destructor Destroy; override;
end;
TCount = class
public
Count: integer;
constructor Create;
end;
TIndex = class
public
Index: integer;
constructor Create(index_: integer);
end;
TImageList = class
public
FilenameGroups: TFPObjectHashTable;
constructor Create;
destructor Destroy; override;
function Load(Folder: string): boolean;
procedure UpdateWallpaper;
private
RootFolder: string;
SelectedGroups: TFPObjectList;
MinCount: integer;
procedure MinFinder(Item: TObject; const {%H-}Key: string; var Continue: Boolean);
end;
implementation
uses LazFileUtils, StrUtils, Windows, Dialogs;
constructor TFilenameGroup.Create;
begin
Count := 0;
Name := '';
Filenames := TStringList.Create;
end;
constructor TCount.Create;
begin
Count := 0;
end;
constructor TIndex.Create(index_: integer);
begin
Index := index_;
end;
destructor TFilenameGroup.Destroy;
begin
FreeAndNil(Filenames);
end;
constructor TImageList.Create;
begin
FilenameGroups := TFPObjectHashTable.Create;
RootFolder := '';
Randomize;
end;
destructor TImageList.Destroy;
begin
FreeAndNil(FilenameGroups);
end;
procedure AddImage(filename: string; FilenameGroups: TFPObjectHashTable);
var
c: char;
prefix: string;
group: TFilenameGroup;
begin
prefix := '';
for c in filename do begin
if (c <= ' ') or (c = '.') then break;
prefix := prefix + c;
end;
if prefix = '' then raise Exception.Create('Cannot extract prefix for filename: ' + filename);
group := FilenameGroups[prefix] as TFilenameGroup;
if group = nil then begin
group := TFilenameGroup.Create;
group.name := prefix;
FilenameGroups[prefix] := group;
end;
group.Filenames.AddObject(filename, TCount.Create);
end;
function TImageList.Load(Folder: string): boolean;
var
sr: TSearchRec;
begin
Result := false;
RootFolder := Folder;
FilenameGroups.Clear;
if FindFirstUTF8(IncludeTrailingPathDelimiter(Folder) + '*', faReadOnly + faArchive, sr) <> 0 then Exit;
try
repeat
if AnsiEndsText('.jpg', sr.Name) or AnsiEndsText('.jpeg', sr.Name) then AddImage(sr.Name, FilenameGroups);
until FindNextUTF8(sr) <> 0;
finally
FindCloseUTF8(sr);
end;
Result := FilenameGroups.Count > 0;
end;
procedure TImageList.MinFinder(Item: TObject; const {%H-}Key: string; var Continue: Boolean);
var
Count: integer;
begin
Count := (Item as TFilenameGroup).Count;
if Count < MinCount then begin
MinCount := Count;
SelectedGroups.Clear;
end;
if Count = MinCount then
SelectedGroups.Add(Item);
Continue := true;
end;
procedure TImageList.UpdateWallpaper;
var
index: integer;
group: TFilenameGroup;
selectedFilenames: TFPObjectList;
Count: integer;
ImagePath: UnicodeString;
begin
SelectedGroups := TFPObjectList.Create(false);
MinCount := MaxInt;
try
FilenameGroups.Iterate(@MinFinder);
index := random(SelectedGroups.Count);
group := SelectedGroups[index] as TFilenameGroup;
Inc(group.Count);
selectedFilenames := TFPObjectList.Create;
MinCount := MaxInt;
try
for index := 0 to group.Filenames.Count - 1 do begin
Count := (group.Filenames.Objects[index] as TCount).Count;
if Count < MinCount then begin
MinCount := Count;
selectedFilenames.Clear;
end;
if Count = MinCount then
selectedFilenames.Add(TIndex.Create(index));
end;
index := random(selectedFilenames.Count);
index := (selectedFilenames.Items[index] as TIndex).Index;
Inc((group.Filenames.Objects[index] as TCount).Count);
ImagePath := UnicodeString(IncludeTrailingPathDelimiter(RootFolder) + group.Filenames.Strings[index]);
// ShowMessage(ImagePath);
if not SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, pChar(ImagePath), SPIF_SENDCHANGE) then begin
raise Exception.Create(IntToStr(GetLastError()));
end
finally
selectedFilenames.Free;
end;
finally
SelectedGroups.Free;
end;
end;
end.