[go: up one dir, main page]

Menu

[r301]: / trunk / src / Plugins.pas  Maximize  Restore  History

Download this file

555 lines (434 with data), 16.1 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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
{-----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/MPL-1.1.html
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License for
the specific language governing rights and limitations under the License.
The Original Code is: Plugins.PAS, released on 2003-05-13.
The Initial Developer of the Original Code is Olivier Sannier
[obones@meloo.com]
Portions created by Olivier Sannier are Copyright (C) 2003 Olivier Sannier.
All Rights Reserved.
Contributor(s): none to date.
You may retrieve the latest version of this file at the Connection Manager
home page, located at http://cnxmanager.sourceforge.net
Known Issues: none to date.
-----------------------------------------------------------------------------}
// $Id$
unit Plugins;
interface
uses
Contnrs, WIndows,
ConnectionAction,
Graphics, JvSimpleXml;
type
TCMPlugin = class;
TPluginType = class;
TPluginTypeList = class;
TPluginInternalActionList = class;
TCMPlugin = class
protected
// true if the plugin is valid
// a plugin is valid if it respects the specifications
// it claims it is respecting
fValid : boolean;
// The list of types proposed by the plugin
fTypes : TPluginTypeList;
// The list of actions this plugins owns
fActions : TPluginInternalActionList;
// the id of this plugin
fId : Integer;
// the icon associated to the plugin
fIcon : TBitmap;
// returns the plugin's icon
// the default behaviour coded in this class returns the bitmap
// stored in fIcon which was filled with a generic icon upon
// construction of the objet.
// If you want to use your own icon, override the constructor
// and load your own icon
function getIcon : TBitmap; virtual;
// returns the title of the plugin
function getTitle : String; virtual; abstract;
// returns the description of the plugin
function getDescription : String; virtual; abstract;
// returns the version of the plugin
function getVersion : String; virtual; abstract;
// returns the name of the plugin's author
function getAuthorName : String; virtual; abstract;
// returns, if specified, the email of the plugin's author
function getAuthorEmail : String; virtual; abstract;
// returns the specification version used by the plugin
function getSpecVersion : Integer; virtual; abstract;
// creates an action of the given type ID and initializes the action object
procedure createAction(pluginType : TPluginType; var action : TConnectionAction); virtual;
// creates a new action of the given type ID
function createNewAction(pluginType : TPluginType) : TConnectionAction; virtual; abstract;
// fills the given bitmap with an icon representing the type of
// that given ID.
// the default behaviour coded here fills the given icon with
// a copy of the one returned by getIcon
procedure getTypeIcon(typeId : integer; icon : TBitmap); virtual;
public
// creates the type list, the action list and the default icon
constructor Create(pluginId : integer); virtual;
// Destroys the types list
destructor Destroy; override;
// destroys the given action in the plugin
procedure DestroyAction(actionId : integer); virtual; abstract;
// returns the default name for actions of the given type
// the default behaviour coded here returns the type's title
function GetDefaultActionName(typeId : integer) : string; virtual;
// Returns the title of the given type
function GetTypeTitle(typeId : Integer) : string; virtual; abstract;
// Returns the description of the given type
function GetTypeDescription(typeId : Integer) : string; virtual; abstract;
// returns the hint for the given action
// the default behaviour coded here returns an empty string
function GetActionHint(actionId : integer) : string; virtual;
// fills the given bitmap with an icon representing the action of
// that given ID
// the default behaviour coded here fills the given icon with
// a copy of the one returned by getIcon
procedure GetActionIcon(actionId : integer; icon : TBitmap); virtual;
// asks the plugin to ask the given action to load its specific
// values from the given XML Node
function LoadActionFromXML(actionId : integer; node : TJvSimpleXmlElem) : boolean; virtual; abstract;
// asks the plugin to ask the given action to save its specific
// values into the given XML Node
procedure SaveActionToXML(actionId : integer; node : TJvSimpleXmlElem); virtual; abstract;
// will return if the given action will finish by itself
function WillActionFinish(actionId : integer) : boolean; virtual; abstract;
// set the internal state of the given action to the given state
procedure SetActionActive(actionId : integer; nActive : boolean); virtual; abstract;
// get the internal state of the given action
function GetActionActive(actionId : integer) : boolean; virtual; abstract;
// asks the given action to display its properties in the given control
// and the given area
function DisplayActionProperties(actionId : integer; parent : HWND; area : PRect) : HWND; virtual; abstract;
// asks the plugin to use the given language, if supported
procedure UseLanguage(code : string); virtual; abstract;
// the ID of this plugin
property ID : integer read fId;
// The list of types proposed by the plugin
property Types : TPluginTypeList read fTypes;
// returns the title of the plugin
property Title : String read getTitle;
// returns the icon associated with this plugin
property Icon : TBitmap read getIcon;
// returns the description of the plugin
property Description : String read getDescription;
// returns the version of the plugin
property Version : String read getVersion;
// returns the name of the plugin's author
property AuthorName : String read getAuthorName;
// returns, if specified, the email of the plugin's author
property AuthorEmail : String read getAuthorEmail;
// returns the specification version used by this plugin
property SpecVersion : Integer read getSpecVersion;
end;
TPluginType = class
protected
// the ID of this plugin type
fID : Integer;
// the plugin this type is linked to
linkedPlugin : TCMPlugin;
// returns the default name of actions of that type
// will ask the plugin to get this value
function getDefaultName : string;
function GetDescription: String;
function GetTitle: String;
public
// creates the plugin type and links it to the given plugin
constructor Create (plugin : TCMPlugin;
iD : integer;
title : string;
description : string);
// creates and initialises an action with that type
procedure CreateAction(action : TConnectionAction);
// creates and return a new action of that type
function CreateNewAction : TConnectionAction;
// fills the given graphic with an icon representing the action type
procedure GetIcon (icon : TBitmap);
// the ID of this plugin type
property ID : Integer read FID;
// The title of this plugin type
property Title : String read GetTitle;
// The title for that plugin type
property Description : String read GetDescription;
// the default name for actions of that type
property DefaultName : string read getDefaultName;
end;
TPluginInternalAction = class
protected
// the action Id
fId : integer;
// the type of the action
fPluginType : TPluginType;
public
constructor Create(id : integer; pluginType : TPluginType);
property Id : integer read fId;
property PluginType : TPluginType read fPluginType;
end;
TCMPluginList = class(TObjectList)
protected
// returns the asked member casted to a TCMPlugin
function getItem (index : Integer) : TCMPlugin;
// sets the given TCMPlugin at the given index
procedure setItem
(index : Integer;
nCMPlugin : TCMPlugin);
public
// adds a plugin type in the list
function Add(nCMPlugin : TCMPlugin) : integer;
// refreshes the content of the list
procedure Refresh(path : string);
// asks all the contained plugins to use the given language
procedure UseLanguage(code : string);
// the items of the list
property Items [Index : Integer] : TCMPlugin read getItem write setItem; default;
end;
TPluginTypeList = class(TObjectList)
protected
// returns the asked member casted to a TPluginType
function getItem (index : Integer) : TPluginType;
// sets the given TPluginType at the given index
procedure setItem
(index : Integer;
nPluginType : TPluginType);
public
// adds a plugin type in the list
function Add(nPluginType : TPluginType) : integer;
// the items of the list
property Items [Index : Integer] : TPluginType read getItem write setItem; default;
end;
TPluginInternalActionList = class(TObjectList)
protected
// returns the asked member casted to a TPluginType
function getItem (index : Integer) : TPluginInternalAction;
// sets the given TPluginType at the given index
procedure setItem
(index : Integer;
nInternalAction : TPluginInternalAction);
public
// adds a plugin type in the list
function Add(nInternalAction : TPluginInternalAction) : integer;
// the items of the list
property Items [Index : Integer] : TPluginInternalAction read getItem write setItem; default;
end;
var PluginList : TCMPluginList;
implementation
uses SysUtils, DLLPlugin, PluginAction;
{ TPluginTypeList }
function TPluginTypeList.Add(nPluginType : TPluginType) : integer;
begin
Result := TObjectList(Self).Add(nPluginType);
end;
function TPluginTypeList.getItem (index : Integer) : TPluginType;
begin
Result := TPluginType(TObjectList(Self).Items[index]);
end;
procedure TPluginTypeList.setItem(index : Integer; nPluginType : TPluginType);
begin
TObjectList(Self).Items[index] := nPluginType;
end;
{ TCMPluginList }
function TCMPluginList.Add(nCMPlugin : TCMPlugin) : integer;
begin
Result := TObjectList(Self).Add(nCMPlugin);
end;
function TCMPluginList.getItem (index : Integer) : TCMPlugin;
begin
Result := TCMPlugin(TObjectList(Self).Items[index]);
end;
procedure TCMPluginList.Refresh(path : string);
var
searchRec : TSearchRec;
DLLPlugin : TCMDLLPlugin;
i : Integer;
Found: Boolean;
begin
// Don't Clear, it would call FreeLibrary and close the forms
// Clear;
// find all DLL plugins in subdir Plugins
if FindFirst(path + '\*.dll',
0,
searchRec) = 0 then
begin
repeat
Found := False;
for i := 0 to Count-1 do
if Items[i] is TCMDLLPlugin then
if TCMDLLPlugin(Items[i]).Filename = searchRec.name then
Found := True;
if not Found then
begin
// create a dll plugin
DLLPlugin := TCMDLLPlugin.Create(Count, path+'\'+searchRec.name);
// if it is valid
if DLLPlugin.Valid then
begin
// then we add it to the list
Add(DLLPlugin);
end
else
begin
// else we free it
DLLPlugin.Free;
end;
end;
until FindNext(searchRec) <> 0;
end;
// close search
FindClose(searchRec);
end;
procedure TCMPluginList.setItem(index : Integer; nCMPlugin : TCMPlugin);
begin
TObjectList(Self).Items[index] := nCMPlugin;
end;
procedure TCMPluginList.UseLanguage(code: string);
var
i : Integer;
begin
for i := 0 to Count-1 do
Items[i].UseLanguage(code);
end;
{ TCMPlugin }
constructor TCMPlugin.Create(pluginId : integer);
begin
inherited Create;
fId := pluginId;
fTypes := TPluginTypeList.Create;
fActions := TPluginInternalActionList.Create;
fIcon := TBitmap.Create;
fIcon.LoadFromResourceName(hInstance, 'Plugin');
end;
procedure TCMPlugin.createAction(
pluginType: TPluginType; var action : TConnectionAction);
begin
fActions.Add(TPluginInternalAction.Create(TPluginAction(action).Id, pluginType));
end;
destructor TCMPlugin.Destroy;
var currActionIndex : integer;
begin
for currActionIndex:= 0 to fActions.Count -1 do
begin
// stop the action
SetActionActive(fActions[currActionIndex].Id, false);
// then destroy it
DestroyAction(fActions[currActionIndex].Id);
end;
fIcon.free;
fActions.Free;
fTypes.free;
inherited;
end;
function TCMPlugin.GetActionHint(actionId: integer): string;
begin
Result := '';
end;
procedure TCMPlugin.getActionIcon(actionId: integer; icon: TBitmap);
var actionIndex : integer;
begin
// explore all available actions in the plugin to find the given one
for actionIndex := 0 to fActions.Count-1 do
begin
// if we found it
if fActions[actionIndex].Id = actionId then
begin
// then we ask to get the icon for its type
fActions[actionIndex].PluginType.GetIcon(icon);
end;
end;
end;
function TCMPlugin.GetDefaultActionName(typeId: integer): string;
var currTypeNo : integer;
begin
for currTypeNo := 0 to fTypes.Count - 1 do
begin
if fTypes[currTypeNo].ID = typeID then
begin
result := fTypes[currTypeNo].DefaultName;
exit;
end;
end;
end;
function TCMPlugin.getIcon : TBitmap;
begin
Result := fIcon;
end;
procedure TCMPlugin.getTypeIcon(typeId: integer; icon: TBitmap);
begin
// by default, assign the plugin icon
icon.Assign(getIcon);
end;
{ TPluginType }
constructor TPluginType.Create(plugin : TCMPlugin;
iD : integer;
title : string;
description : string);
begin
inherited Create;
linkedPlugin := plugin;
fID := id;
// fTitle := title;
// fDescription := description;
end;
{function TPluginType.CreateAction : TConnectionAction;
var dummy : integer;
begin
result := linkedPlugin.createAction(self, dummy);
end;}
procedure TPluginType.CreateAction(action: TConnectionAction);
begin
linkedPlugin.createAction(self, action);
end;
function TPluginType.CreateNewAction: TConnectionAction;
begin
Result := linkedPlugin.CreateNewAction(self);
end;
function TPluginType.getDefaultName: string;
begin
result := linkedPlugin.GetDefaultActionName(fId);
end;
function TPluginType.GetDescription: String;
begin
Result := linkedPlugin.GetTypeDescription(fId);
end;
procedure TPluginType.GetIcon (icon : TBitmap);
begin
linkedPlugin.getTypeIcon(id, icon);
end;
function TPluginType.GetTitle: String;
begin
Result := linkedPlugin.getTypeTitle(fId);
end;
{ TPluginInternalAction }
constructor TPluginInternalAction.Create(id: integer;
pluginType: TPluginType);
begin
fId := id;
fPluginType := pluginType;
end;
{ TPluginInternalActionList }
function TPluginInternalActionList.Add(
nInternalAction: TPluginInternalAction): integer;
begin
Result := TObjectList(Self).Add(nInternalAction);
end;
function TPluginInternalActionList.getItem(
index: Integer): TPluginInternalAction;
begin
Result := TPluginInternalAction(TObjectList(Self).Items[index]);
end;
procedure TPluginInternalActionList.setItem(index: Integer;
nInternalAction: TPluginInternalAction);
begin
TObjectList(self).Items[index] := nInternalAction;
end;
initialization
PluginList := TCMPluginList.Create;
finalization
PluginList.Free;
end.