-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathESoft.Launcher.Clipboard.pas
254 lines (213 loc) · 5.79 KB
/
ESoft.Launcher.Clipboard.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
Unit ESoft.Launcher.Clipboard;
Interface
Uses
Winapi.Windows,
System.Classes,
{$IFDEF VER230}
DBXJSON,
{$ELSE}
JSON,
{$ENDIF}
IniFiles,
System.SysUtils,
ShellApi,
Vcl.Graphics,
Generics.Collections,
ESoft.Utils,
Clipbrd;
Type
TEClipboardItem = Class(TPersistent)
Strict Private
FName: String;
FData: WideString;
Public
Constructor Create(Const aName: String = '');
Function ToJSONString: TJSONObject;
Procedure FromJSON(Const aJSONObj: TJSONObject);
Procedure LoadFromClipboard;
Procedure SaveToClipboard;
Procedure Rename(Const aName: String);
Published
Property Name: String Read FName;
Property Data: WideString Read FData Write FData;
End;
TEClipboardItems = Class(TObject)
Strict Private
FItems: TObjectList<TEClipboardItem>;
Function GetItemByName(aName: String): TEClipboardItem;
Private
Function GetItem(aIndex: Integer): TEClipboardItem;
Public
Constructor Create;
Destructor Destroy; Override;
Function Contains(Const aName: String): Boolean;
Function IndexOf(Const aName: String): Integer;
Function AddItem(Const aName: String): TEClipboardItem;
Procedure Save;
Procedure Load;
Function Count: Integer;
Procedure DeleteByItemName(aName: String);
Property Item[aIndex: Integer]: TEClipboardItem Read GetItem; Default;
Property ItemByName[aName: String]: TEClipboardItem Read GetItemByName;
End;
Implementation
{ TEClipboard }
Uses
UnitMDIMain;
Const
cJSON_NAME = 'CPL_NAME';
cJSON_DATA = 'CPL_DATA';
{ TEClipboardItems }
Function TEClipboardItems.AddItem(Const aName: String): TEClipboardItem;
Var
iIndex: Integer;
Begin
Result := Nil;
iIndex := IndexOf(aName);
If iIndex <> -1 Then
Exit(FItems[iIndex]);
Result := TEClipboardItem.Create(aName);
FItems.Add(Result);
End;
Function TEClipboardItems.Contains(Const aName: String): Boolean;
Begin
Result := IndexOf(aName) <> -1;
End;
Function TEClipboardItems.Count: Integer;
Begin
Result := FItems.Count;
End;
Constructor TEClipboardItems.Create;
Begin
Inherited;
FItems := TObjectList<TEClipboardItem>.Create(True);
End;
Procedure TEClipboardItems.DeleteByItemName(aName: String);
Var
varClpBrdItem: TEClipboardItem;
Begin
varClpBrdItem := ItemByName[aName];
If Assigned(varClpBrdItem) Then
FItems.Remove(varClpBrdItem);
End;
Destructor TEClipboardItems.Destroy;
Begin
EFreeAndNil(FItems);
Inherited;
End;
Function TEClipboardItems.GetItem(aIndex: Integer): TEClipboardItem;
Begin
Result := FItems[aIndex];
End;
Function TEClipboardItems.GetItemByName(aName: String): TEClipboardItem;
Var
iIndex: Integer;
Begin
Result := Nil;
iIndex := IndexOf(aName);
If iIndex <> -1 Then
Exit(FItems[iIndex]);
End;
Function TEClipboardItems.IndexOf(Const aName: String): Integer;
Var
iCntr: Integer;
Begin
Result := -1;
For iCntr := 0 To Pred(FItems.Count) Do
Begin
If SameText(FItems[iCntr].Name, aName) Then
Exit(iCntr);
End;
End;
Procedure TEClipboardItems.Load;
Var
varClpItem: TEClipboardItem;
varList: TStringList;
iCntr: Integer;
varJSONArray: TJSONArray;
Begin
If Not FileExists(FormMDIMain.ParentFolder + cClipbord_Data) Then
Exit;
varList := TStringList.Create;
try
varList.LoadFromFile(FormMDIMain.ParentFolder + cClipbord_Data);
varJSONArray := TJSONObject.ParseJSONValue(varList.Text) as TJSONArray;
Try
FItems.Clear; // Clear all items { Ajmal }
If not Assigned(varJSONArray) Then
Exit;
For iCntr := 0 To Pred(varJSONArray.Count) Do
Begin
varClpItem := TEClipboardItem.Create;
varClpItem.FromJSON(varJSONArray.Items[iCntr] as TJSONObject);
// Don't use FItems.IndexOf(), we need to compare incasesensitive { Ajmal }
If Not Contains(varClpItem.Name) Then
FItems.Add(varClpItem);
End;
Finally
varJSONArray.Free;
End;
finally
varList.Free;
end;
End;
Procedure TEClipboardItems.Save;
Var
varClpItem: TEClipboardItem;
varList: TStringList;
varJSONArray: TJSONArray;
Begin
varJSONArray := TJSONArray.Create;
varList := TStringList.Create;
Try
For varClpItem In FItems Do
varJSONArray.Add(varClpItem.ToJSONString);
varList.Text := varJSONArray.ToString;
Try
varList.SaveToFile(FormMDIMain.ParentFolder + cClipbord_Data);
Except
// Do nothing here { Ajmal }
End;
Finally
varJSONArray.Free;
varList.Free;
End;
End;
{ TEClipboardItem }
Constructor TEClipboardItem.Create(Const aName: String);
Begin
FName := aName;
End;
Procedure TEClipboardItem.FromJSON(Const aJSONObj: TJSONObject);
Begin
FName := aJSONObj.Get(cJSON_NAME).JsonValue.Value;
try
FData := aJSONObj.Get(cJSON_DATA).JsonValue.Value;
except
FData := '';
end;
End;
Procedure TEClipboardItem.LoadFromClipboard;
Begin
FData := Clipboard.AsText;
End;
Procedure TEClipboardItem.Rename(Const aName: String);
Begin
FName := aName;
End;
Procedure TEClipboardItem.SaveToClipboard;
Begin
Clipboard.AsText := FData;
End;
Function TEClipboardItem.ToJSONString: TJSONObject;
Begin
Result := TJSONObject.Create;
Try
Result.AddPair(cJSON_NAME, FName);
Result.AddPair(cJSON_DATA, FData);
Except
// Free if there is some exception. On the otherhand it's caller's responsiblity to free { Ajmal }
FreeAndNil(Result);
End;
End;
End.