forked from ibv/LDAP-Admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBookmarks.pas
317 lines (283 loc) · 8.46 KB
/
Bookmarks.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
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
{ LDAPAdmin - Bookmarks.pas
* Copyright (C) 2015-2016 Tihomir Karlovic
*
* Author: Tihomir Karlovic
*
*
* This file 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 2 of the License, or
* (at your option) any later version.
*
* This file 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
}
unit Bookmarks;
{$IFDEF FPC}
{$MODE Delphi}
{$ENDIF}
interface
uses
{$IFnDEF FPC}
Windows,
{$ELSE}
LCLIntf, LCLType, LMessages,
{$ENDIF}
SysUtils, Classes, Graphics, Forms, Controls, StdCtrls,
Buttons, ExtCtrls, ComCtrls, Menus, LdapClasses, Sorter;
type
TBookmarks = class
private
FSession: TLdapSession; // have to use Session here to avoid cicular references
FMenu: TPopupMenu;
FBookmarks: TStringList;
procedure MenuPopup(Sender: TObject);
procedure BookmarkClick(Sender: TObject);
function GetItems: TStringList;
procedure InitMenu;
function GetImageIndex(Index: Integer): Integer;
public
constructor Create(Session: TLdapSession);
destructor Destroy; override;
property Items: TStringList read GetItems;
property Menu: TPopupMenu read FMenu;
property Images[Index: Integer]: Integer read GetImageIndex;
end;
TBookmarkDlg = class(TForm)
btnCancel: TButton;
BookmarkList: TListView;
btnRemove: TBitBtn;
btnAdd: TBitBtn;
btnOk: TButton;
btnGoto: TBitBtn;
procedure BookmarkListSelectItem(Sender: TObject; Item: TListItem;
Selected: Boolean);
procedure btnAddClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure btnRemoveClick(Sender: TObject);
procedure BookmarkListClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure btnGotoClick(Sender: TObject);
procedure BookmarkListData(Sender: TObject; Item: TListItem);
procedure BookmarkListDblClick(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure BookmarkListCustomDrawItem(Sender: TCustomListView;
Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
private
FBookmarks: TBookmarks;
FModified: Boolean;
FSorter: TListViewSorter;
procedure SetModified(Value: Boolean);
procedure ListViewSort(SortColumn: TListColumn; SortAsc: boolean);
public
constructor Create(AOwner: TComponent; Bookmarks: TBookmarks); reintroduce;
property Modified: Boolean read fModified write SetModified;
end;
var
BookmarkDlg: TBookmarkDlg;
implementation
{$R *.dfm}
uses Main, Constant, Dialogs, Connection, ObjectInfo;
procedure TBookmarks.MenuPopup(Sender: TObject);
var
i: Integer;
mi: TMenuItem;
begin
if FMenu.Items.Count = 0 then
begin
FBookmarks.Sorted := true;
for i := 0 to FBookmarks.Count - 1 do
begin
mi := TMenuItem.Create(FMenu);
mi.OnClick := BookmarkClick;
mi.Caption := FBookmarks[i];
mi.ImageIndex := Images[i];
if mi.ImageIndex = -1 then
mi.Enabled := false;
FMenu.Items.Add(mi);
end;
end;
end;
procedure TBookmarks.BookmarkClick(Sender: TObject);
begin
MainFrm.LocateEntry((Sender as TMenuItem).Caption, true);
end;
function TBookmarks.GetItems: TStringList;
begin
Result := FBookmarks;
end;
procedure TBookmarks.InitMenu;
begin
if FBookmarks.Count > 0 then
begin
if not Assigned(FMenu) then
begin
FMenu := TPopupMenu.Create(nil);
///FMenu.AutoHotkeys:= maManual;
FMenu.OnPopup := MenuPopup;
FMenu.Images := MainFrm.ImageList;
end
else
FMenu.Items.Clear;
end
else
FreeAndNil(FMenu);
end;
function TBookmarks.GetImageIndex(Index: Integer): Integer;
var
Entry: TLdapEntry;
oi: TObjectInfo;
begin
if FBookmarks.Objects[Index] = nil then
begin
Entry := TLdapEntry.Create(FSession, FBookmarks[Index]);
try
Entry.Read;
oi := TObjectInfo.Create(Entry, false);
try
FBookmarks.Objects[Index] := Pointer(oi.ImageIndex);
finally
oi.Free;
end;
except;
FBookmarks.Objects[Index] := Pointer(-1);
end;
Entry.Free;
end;
Result := Integer(FBookmarks.Objects[Index]);
end;
constructor TBookmarks.Create(Session: TLdapSession);
begin
FSession := Session;
FBookmarks := TStringList.Create;
FBookmarks.CommaText := (Session as TConnection).Account.ReadString(rBookmarks);
InitMenu;
end;
destructor TBookmarks.Destroy;
begin
(FSession as TConnection).Account.WriteString(rBookmarks, FBookmarks.CommaText);
FBookmarks.Free;
FMenu.Free;
inherited;
end;
procedure TBookmarkDlg.SetModified(Value: Boolean);
begin
if Value and not fModified then
begin
btnOk.Visible := true;
btnCancel.Caption := cCancel;
end;
fModified := Value;
BookmarkListClick(nil);
end;
constructor TBookmarkDlg.Create(AOwner: TComponent; Bookmarks: TBookmarks);
var
ls: TStringList;
i: Integer;
begin
inherited Create(AOwner);
FBookmarks := Bookmarks;
BookmarkList.Items.Count := FBookmarks.Items.Count;
BookmarkList.SmallImages := MainFrm.ImageList;
FSorter := TListViewSorter.Create;
FSorter.ListView := BookmarkList;
FSorter.OnSort := ListViewSort;
end;
procedure TBookmarkDlg.BookmarkListClick(Sender: TObject);
begin
btnRemove.Enabled := BookmarkList.Selected <> nil;
btnGoto.Enabled := BookmarkList.ItemFocused <> nil;
end;
procedure TBookmarkDlg.BookmarkListCustomDrawItem(Sender: TCustomListView;
Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
if Item.ImageIndex = -1 then
begin
Sender.Canvas.Font.Color := clGrayText;
Sender.Canvas.Font.Style := Sender.Canvas.Font.Style + [fsItalic];
end;
end;
procedure TBookmarkDlg.BookmarkListData(Sender: TObject; Item: TListItem);
begin
Item.Caption := FBookmarks.Items[Item.Index];
Item.ImageIndex := FBookmarks.Images[Item.Index];
end;
procedure TBookmarkDlg.BookmarkListDblClick(Sender: TObject);
begin
btnGotoClick(nil);
end;
procedure TBookmarkDlg.BookmarkListSelectItem(Sender: TObject; Item: TListItem; Selected: Boolean);
begin
btnRemove.Enabled := true;
end;
procedure TBookmarkDlg.FormClose(Sender: TObject; var Action: TCloseAction);
var
i: Integer;
s: string;
begin
if fModified then
begin
if ModalResult <> mrOk then
FBookmarks.Items.CommaText := (FBookmarks.FSession as TConnection).Account.ReadString(rBookmarks); // reset
FBookmarks.InitMenu;
end;
end;
procedure TBookmarkDlg.FormDestroy(Sender: TObject);
begin
FSorter.Free;
end;
procedure TBookmarkDlg.FormShow(Sender: TObject);
begin
if BookmarkList.Items.Count > 0 then
begin
BookmarkList.Items[0].Selected := true;
BookmarkList.ItemFocused := BookmarkList.Items[0];
end;
BookmarkListClick(nil);
end;
procedure TBookmarkDlg.btnAddClick(Sender: TObject);
var
s: string;
begin
s := MainFrm.PickEntry(cSearchBase);
if s <> '' then
begin
//BookmarkList.Items.Add.Caption := s;
FBookmarks.Items.Add(s);
BookmarkList.Items.Count := FBookmarks.Items.Count;
Modified := true;
end;
end;
procedure TBookmarkDlg.btnGotoClick(Sender: TObject);
begin
MainFrm.LocateEntry(BookmarkList.ItemFocused.Caption, true);
end;
procedure TBookmarkDlg.btnRemoveClick(Sender: TObject);
begin
//BookmarkList.Selected.Delete;
FBookmarks.Items.Delete(BookmarkList.ItemIndex);
BookmarkList.Items.Count := FBookmarks.Items.Count;
Modified := true;
end;
var
_gAscending: Boolean;
function DoSort(List: TStringList; Index1, Index2: Integer): Integer;
begin
Result := AnsiCompareText(List[Index1], List[Index2]);
if not _gAscending then
Result := -Result;
end;
procedure TBookmarkDlg.ListViewSort(SortColumn: TListColumn; SortAsc: boolean);
begin
_gAscending := SortAsc;
FBookmarks.Items.Sorted := false;
FBookmarks.Items.CustomSort(DoSort);
BookmarkList.Repaint;
end;
end.