-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathServiceCollection.pas
More file actions
402 lines (365 loc) · 13.3 KB
/
ServiceCollection.pas
File metadata and controls
402 lines (365 loc) · 13.3 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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
unit ServiceCollection;
interface
uses Classes, SysUtils, Generics.Collections, NativeXML, TypInfo,Dialogs,
stdctrls,ComCtrls;
type
TSendMethod = (GET, POST);
TServiceAccount = class;
TServiceInfo = class;
TAccountBase = class;
TServiceBase = class;
TServiceInfo = class
private
FDomain: string; // äîìåí ñåðâèñà - is.gd, bit.ly è ò.ä.
FTemplate: string; // øàáëîí URL
FMethod: TSendMethod; // ìåòîä îòïðàâêè çàïðîñà
FParams: TStringList; // ïàðàìåòðû çàïðîñà
FDoEncode: boolean; // êîäèðîâàòü ëè èñõîäíûé URL
FNeedRegistry: boolean; // ñåðâèñ òðåáóåò ðåãèñòðàöèè äëÿ äîñòóïà ê API
FPreviewChar: Char; // ñèìâîë, êîòîðûé íåîáõîäèìî âñòàâèòü â URL äëÿ ïðåäïðîñìîòðà
public
constructor Create;
destructor Destroy;
property Domain: string read FDomain write FDomain;
property Template: string read FTemplate write FTemplate;
property Method: TSendMethod read FMethod write FMethod;
property Params: TStringList read FParams write FParams;
property DoEncode: boolean read FDoEncode write FDoEncode;
property PreviewChar: Char read FPreviewChar write FPreviewChar;
property NeedRegistry: boolean read FNeedRegistry write FNeedRegistry;
end;
TServiceBase = class(TList<TServiceInfo>)
public
constructor Create;
procedure LoadFromFile(const FileName: string);
procedure SaveToFile(const FileName: string);
procedure ShowTo(Broker:TComponent);
function Service(i:integer):TServiceInfo;overload;
function Service(DomainName:string):TServiceInfo;overload;
end;
TServiceAccount = class
private
FServiceDomain:string;
FUserName:string;
FUserPass:string;
FAPIKey: string;
public
property ServiceDomain:string read FServiceDomain write FServiceDomain;
property UserName:string read FUserName write FUserName;
property UserPass:string read FUserPass write FUserPass;
property APIKey: string read FAPIKey write FAPIKey;
end;
TAccountBase = class(TList<TServiceAccount>)
strict private
class var
FListComponent: TComponent;
class property ListComponent: TComponent read FListComponent write FListComponent;
class procedure OnNotifyEvent (Sender: TObject; const Item: TServiceAccount;
Action: TCollectionNotification); overload;
public
constructor Create(const ListObject: TComponent);
procedure LoadFromFile(const FileName: string);
procedure SaveToFile(const FileName: string);
procedure AssignListComponent(aListComponent: TComponent);
procedure Delete(i: integer);overload;
function Account(i:integer):TServiceAccount;overload;
function Account(Domain:string):TServiceAccount;overload;
end;
var
ServiceBase: TServiceBase;
AccountBase: TAccountBase;
ServiceInfo: array of TServiceInfo;
Acc: array of TServiceAccount;
implementation
constructor TServiceBase.Create;
begin
inherited Create;
end;
procedure TServiceBase.LoadFromFile(const FileName: string);
var i:integer;
XML:TNativeXml;
Ch: Char;
begin
try
XML:=TNativeXml.Create;
XML.LoadFromFile(FileName);
for I:=0 to XML.Root.NodeCount-1 do
begin
SetLength(ServiceInfo,length(ServiceInfo)+1);
ServiceInfo[length(ServiceInfo)-1]:=TServiceInfo.Create;
ServiceInfo[length(ServiceInfo)-1].FDomain:=XML.Root.Nodes[i].Name;
ServiceInfo[length(ServiceInfo)-1].FTemplate:=XML.Root.Nodes[i].ReadString('Template','');
ServiceInfo[length(ServiceInfo)-1].FMethod:=TSendMethod(GetEnumValue(TypeInfo(TSendMethod),XML.Root.Nodes[i].ReadString('Method','')));
ServiceInfo[length(ServiceInfo)-1].FParams.Text:=XML.Root.Nodes[i].ReadString('Params','');
ServiceInfo[length(ServiceInfo)-1].FDoEncode:=XML.Root.Nodes[i].ReadBool('DoEncode',true);
ServiceInfo[length(ServiceInfo)-1].FNeedRegistry:=XML.Root.Nodes[i].ReadBool('NeedRegistry',false);
Ch:=Char(XML.Root.Nodes[i].ReadString('PreviewChar','')[1]);
ServiceInfo[length(ServiceInfo)-1].FPreviewChar:=Ch;
end;
ServiceBase.AddRange(ServiceInfo);
ShowMessage(IntToStr(ServiceBase.Count));
finally
FreeAndNil(XML);
end;
end;
procedure TServiceBase.SaveToFile(const FileName: string);
var
i: integer;
XML: TNativeXml;
Node: TXmlNode;
begin
try
XML := TNativeXml.Create;
XML.CreateName('Services');
for i := 0 to Self.Count - 1 do
begin
Node := XML.Root.NodeNew(Self[i].FDomain);
Node.WriteString('Template', Self[i].FTemplate);
Node.WriteString('Method', GetEnumName(TypeInfo(TSendMethod), ord
((Self[i].FMethod))));
Node.WriteString('Params', Self[i].FParams.Text);
Node.WriteBool('DoEncode', Self[i].FDoEncode);
Node.WriteBool('NeedRegistry', Self[i].FNeedRegistry);
Node.WriteString('PreviewChar', Self[i].FPreviewChar);
Node := nil;
end;
XML.SaveToFile(FileName);
finally
XML.Free;
end;
end;
function TServiceBase.Service(i: integer): TServiceInfo;
begin
if i<=Self.Count-1 then
begin
Result:=TServiceInfo.Create;
Result:=Self.Items[i];
end;
end;
function TServiceBase.Service(DomainName: string): TServiceInfo;
var i:integer;
begin
for I := 0 to Self.Count - 1 do
begin
if lowercase(Trim(DomainName))=lowercase(self.Items[i].Domain) then
begin
Result:=TServiceInfo.Create;
Result:=Service(i);
break;
end;
end;
end;
procedure TServiceBase.ShowTo(Broker: TComponent);
var i:integer;
TN,R,N:TTreeNode;
begin
if (Broker is TListBox) then
begin
(Broker as TListBox).Items.Clear;
for I := 0 to self.Count-1 do
(Broker as TListBox).Items.Add(Self.Items[i].Domain)
end
else
if (Broker is TTreeView) then
begin
(Broker as TTreeView).Items.BeginUpdate;
R:=TTreeNode.Create((Broker as TTreeView).Items);
N:=TTreeNode.Create((Broker as TTreeView).Items);
(Broker as TTreeView).Items.Clear;
R:=(Broker as TTreeView).Items.AddFirst(R,'Ñ ðåãèñòðàöèåé');
N:=(Broker as TTreeView).Items.AddFirst(N,'Áåç ðåãèñòðàöèè');
for I := 0 to self.Count-1 do
begin
if Self.Items[i].NeedRegistry then
(Broker as TTreeView).Items.AddChildFirst(R,Self.Items[i].Domain)
else
(Broker as TTreeView).Items.AddChild(N,Self.Items[i].Domain)
end;
(Broker as TTreeView).Items.EndUpdate;
end
else
raise Exception.Create('Íåäîïóñòèìûé îáúåêò');
end;
{ TServiceInfo }
constructor TServiceInfo.Create;
begin
inherited Create;
FParams := TStringList.Create;
end;
destructor TServiceInfo.Destroy;
begin
FreeAndNil(FParams);
inherited Free;
end;
{ TAccountBase }
function TAccountBase.Account(i: integer): TServiceAccount;
begin
if i<=Self.Count-1 then
begin
Result:=TServiceAccount.Create;
Result:=Self.Items[i];
end;
end;
function TAccountBase.Account(Domain: string): TServiceAccount;
var i:integer;
begin
for I := 0 to Self.Count - 1 do
begin
if LowerCase(Trim(Domain))=LowerCase(Self.Items[i].ServiceDomain) then
begin
Result:=Account(i);
break;
end;
end;
end;
procedure TAccountBase.AssignListComponent(aListComponent: TComponent);
begin
FListComponent:=aListComponent;
end;
constructor TAccountBase.Create(const ListObject: TComponent);
begin
inherited Create;
FListComponent:=ListObject;
Self.OnNotify:=OnNotifyEvent;
end;
procedure TAccountBase.Delete(i: integer);
begin
inherited DeleteRange(i,1);
if ListComponent<>nil then
begin
if (ListComponent is TListBox) then
(ListComponent as TListBox).Items.Delete(i);
end;
end;
procedure TAccountBase.LoadFromFile(const FileName: string);
var i:integer;
XML: TNativeXml;
begin
try
XML:=TNativeXml.Create;
XML.LoadFromFile(FileName);
for I:=0 to XML.Root.NodeCount-1 do
begin
SetLength(Acc,length(Acc)+1);
Acc[length(Acc)-1]:=TServiceAccount.Create;
Acc[length(Acc)-1].FServiceDomain:=XML.Root.Nodes[i].Name;
Acc[length(Acc)-1].FUserName:=XML.Root.Nodes[i].ReadString('UserName','');
Acc[length(Acc)-1].FUserPass:=XML.Root.Nodes[i].ReadString('UserPass','');
Acc[length(Acc)-1].FAPIKey:=XML.Root.Nodes[i].ReadString('APIKey','');
end;
AccountBase.AddRange(Acc);
ShowMessage(IntToStr(AccountBase.Count));
finally
FreeAndNil(XML);
end;
end;
class procedure TAccountBase.OnNotifyEvent(Sender: TObject;
const Item: TServiceAccount; Action: TCollectionNotification);
begin
if ListComponent<>nil then
begin
if Action=cnAdded then
begin
if (ListComponent is TListBox) then
(ListComponent as TListBox).Items.Add(Item.FServiceDomain);
end
end;
end;
procedure TAccountBase.SaveToFile(const FileName: string);
var
i: integer;
XML: TNativeXml;
Node: TXmlNode;
begin
try
XML := TNativeXml.Create;
XML.CreateName('Accounts');
for i := 0 to Self.Count - 1 do
begin
Node := XML.Root.NodeNew(Self[i].ServiceDomain);
Node.WriteString('UserName', Self[i].UserName);
Node.WriteString('UserPass', Self[i].UserPass);
Node.WriteString('APIKey', Self[i].APIKey);
Node := nil;
end;
XML.SaveToFile(FileName);
finally
XML.Free;
end;
end;
initialization
begin
ServiceBase := TServiceBase.Create;
if not FileExists('ServBase.xml') then
begin
SetLength(ServiceInfo, length(ServiceInfo) + 1);
ServiceInfo[length(ServiceInfo) - 1] := TServiceInfo.Create;
ServiceInfo[length(ServiceInfo) - 1].Domain := 'is.gd';
ServiceInfo[length(ServiceInfo) - 1].FTemplate :=
'http://is.gd/api.php?longurl=[long_url]';
ServiceInfo[length(ServiceInfo) - 1].FMethod := GET;
ServiceInfo[length(ServiceInfo) - 1].FDoEncode := true;
ServiceInfo[length(ServiceInfo) - 1].FNeedRegistry := false;
ServiceInfo[length(ServiceInfo) - 1].FPreviewChar := '=';
SetLength(ServiceInfo, length(ServiceInfo) + 1);
ServiceInfo[length(ServiceInfo) - 1] := TServiceInfo.Create;
ServiceInfo[length(ServiceInfo) - 1].Domain := 'sokrati.ru';
ServiceInfo[length(ServiceInfo) - 1].FTemplate := 'http://sokrati.ru/do/[long_url]';
ServiceInfo[length(ServiceInfo) - 1].FMethod := GET;
ServiceInfo[length(ServiceInfo) - 1].FDoEncode := false;
ServiceInfo[length(ServiceInfo) - 1].FNeedRegistry := false;
ServiceInfo[length(ServiceInfo) - 1].FPreviewChar := '1';
SetLength(ServiceInfo, length(ServiceInfo) + 1);
ServiceInfo[length(ServiceInfo) - 1] := TServiceInfo.Create;
ServiceInfo[length(ServiceInfo) - 1].Domain := 'clck.ru';
ServiceInfo[length(ServiceInfo) - 1].FTemplate :=
'http://clck.ru/--?url=[long_url]';
ServiceInfo[length(ServiceInfo) - 1].FMethod := GET;
ServiceInfo[length(ServiceInfo) - 1].FDoEncode := true;
ServiceInfo[length(ServiceInfo) - 1].FNeedRegistry := false;
ServiceInfo[length(ServiceInfo) - 1].FPreviewChar := '1';
SetLength(ServiceInfo, length(ServiceInfo) + 1);
ServiceInfo[length(ServiceInfo) - 1] := TServiceInfo.Create;
ServiceInfo[length(ServiceInfo) - 1].Domain := 'zi.ma';
ServiceInfo[length(ServiceInfo) - 1].FTemplate :=
'http://zi.ma/?module=ShortURL&file=Add&mode=API&url=[long_url]';
ServiceInfo[length(ServiceInfo) - 1].FMethod := GET;
ServiceInfo[length(ServiceInfo) - 1].FDoEncode := true;
ServiceInfo[length(ServiceInfo) - 1].FNeedRegistry := false;
ServiceInfo[length(ServiceInfo) - 1].FPreviewChar := '1';
SetLength(ServiceInfo, length(ServiceInfo) + 1);
ServiceInfo[length(ServiceInfo) - 1] := TServiceInfo.Create;
ServiceInfo[length(ServiceInfo) - 1].Domain := 'u.nu';
ServiceInfo[length(ServiceInfo) - 1].FTemplate :=
'http://u.nu/unu-api-simple?url=[long_url]';
ServiceInfo[length(ServiceInfo) - 1].FMethod := GET;
ServiceInfo[length(ServiceInfo) - 1].FDoEncode := true;
ServiceInfo[length(ServiceInfo) - 1].FNeedRegistry := false;
ServiceInfo[length(ServiceInfo) - 1].FPreviewChar := '?';
SetLength(ServiceInfo, length(ServiceInfo) + 1);
ServiceInfo[length(ServiceInfo) - 1] := TServiceInfo.Create;
ServiceInfo[length(ServiceInfo) - 1].Domain := 'nn.nf';
ServiceInfo[length(ServiceInfo) - 1].FTemplate := 'http://nn.nf/api.php';
ServiceInfo[length(ServiceInfo) - 1].FMethod := POST;
ServiceInfo[length(ServiceInfo) - 1].FDoEncode := true;
ServiceInfo[length(ServiceInfo) - 1].FParams.Add('url=[long_url]');
ServiceInfo[length(ServiceInfo) - 1].FNeedRegistry := false;
ServiceInfo[length(ServiceInfo) - 1].FPreviewChar := '=';
SetLength(ServiceInfo, length(ServiceInfo) + 1);
ServiceInfo[length(ServiceInfo) - 1] := TServiceInfo.Create;
ServiceInfo[length(ServiceInfo) - 1].Domain := 'wc.ru';
ServiceInfo[length(ServiceInfo) - 1].FTemplate := 'http://0wc.ru/text/[long_url]';
ServiceInfo[length(ServiceInfo) - 1].FMethod := GET;
ServiceInfo[length(ServiceInfo) - 1].FDoEncode := false;
ServiceInfo[length(ServiceInfo) - 1].FNeedRegistry := false;
ServiceInfo[length(ServiceInfo) - 1].FPreviewChar := '1';
ServiceBase.AddRange(ServiceInfo);
ServiceBase.SaveToFile('ServBase.xml');
end
else
begin
ServiceInfo:=nil;
ServiceBase.LoadFromFile('ServBase.xml');
end;
end;
end.