From: Nando D. <na...@us...> - 2005-06-26 14:28:29
|
Update of /cvsroot/instantobjects/Source/Core In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv22308/Core Modified Files: InstantConnectionManager.pas InstantConsts.pas Log Message: Added ability to override loading and saving connectiondefs without having to duplicate all the base logic; also simplified the code (now it does correct stream handling). Index: InstantConsts.pas =================================================================== RCS file: /cvsroot/instantobjects/Source/Core/InstantConsts.pas,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** InstantConsts.pas 18 Jun 2005 09:42:39 -0000 1.10 --- InstantConsts.pas 26 Jun 2005 14:28:20 -0000 1.11 *************** *** 91,94 **** --- 91,95 ---- SDisposeConflict = 'Object %s(''%s'') was disposed by another session'; SErrorDisposingObject = 'Error disposing object %s(''%s''): "%s"'; + SErrorLoadingConnectionDefs = 'Error loading connection definitions from %s: %s'; SErrorRefreshingObject = 'Error refreshing object %s(''%s''): "%s"'; SErrorRetrievingObject = 'Error retrieving object %s(''%s''): "%s"'; Index: InstantConnectionManager.pas =================================================================== RCS file: /cvsroot/instantobjects/Source/Core/InstantConnectionManager.pas,v retrieving revision 1.14 retrieving revision 1.15 diff -C2 -d -r1.14 -r1.15 *** InstantConnectionManager.pas 20 Jun 2005 07:30:55 -0000 1.14 --- InstantConnectionManager.pas 26 Jun 2005 14:28:20 -0000 1.15 *************** *** 95,105 **** procedure SetCaption(const Value: string); function GetDefsFileName: string; - property DefsFileName: string read GetDefsFileName; protected public constructor Create(AOwner: TComponent); override; destructor Destroy; override; ! procedure LoadConnectionDefs; virtual; ! procedure SaveConnectionDefs; virtual; procedure ConnectByName(const ConnectionDefName: string); procedure Execute; --- 95,123 ---- procedure SetCaption(const Value: string); function GetDefsFileName: string; protected + property DefsFileName: string read GetDefsFileName; + // Creates and returns a stream to read the connectiondefs data from. + // If there is nothing to read, it should return nil. + // The caller is responsible for freeing the stream after using it. + // The default implementation just creates a TFileStream that opens + // DefsFileName for reading. If DefsFileName does not exist, then it returns + // nil. + // An overridden implementation might get the data from an encrypted file or + // a completely different source. + function CreateConnectionDefsInputStream(): TStream; virtual; + // Creates and returns a stream to write the connectiondefs data to. + // If there is nothing to write to, it should return nil. + // The caller is responsible for freeing the stream after using it. + // The default implementation just creates a TFileStream that opens + // DefsFileName for writing. If DefsFileName is not specified, then it + // returns nil. + // An overridden implementation might write the data to an encrypted file + // or a completely different destination. + function CreateConnectionDefsOutputStream(): TStream; virtual; public constructor Create(AOwner: TComponent); override; destructor Destroy; override; ! procedure LoadConnectionDefs; ! procedure SaveConnectionDefs; procedure ConnectByName(const ConnectionDefName: string); procedure Execute; *************** *** 259,293 **** procedure TInstantConnectionManager.LoadConnectionDefs; var ! FileStream: TFileStream; ! MemoryStream: TMemoryStream; begin ! if FileExists(DefsFileName) then ! begin try ! if FileFormat = sfBinary then ! begin ! FileStream := TFileStream.Create(DefsFileName, fmOpenRead); ! try ! InstantReadObjectFromStream(FileStream, ConnectionDefs); ! finally ! FileStream.Free; ! end; ! end ! else ! begin ! MemoryStream := TMemoryStream.Create; ! try ! MemoryStream.LoadFromFile(DefsFileName); ! InstantReadObject(MemoryStream, sfXML, ConnectionDefs); ! finally ! MemoryStream.Free; ! end; ! end; ! except ! on E: Exception do ! raise EInstantError.CreateFmt( ! 'Error loading connection definitions from %s: %s', ! [DefsFileName, E.Message]); end; end; end; --- 277,294 ---- procedure TInstantConnectionManager.LoadConnectionDefs; var ! InputStream: TStream; begin ! try ! InputStream := CreateConnectionDefsInputStream(); try ! if Assigned(InputStream) then ! InstantReadObject(InputStream, FileFormat, ConnectionDefs); ! finally ! InputStream.Free; end; + except + on E: Exception do + raise EInstantError.CreateFmt(SErrorLoadingConnectionDefs, + [DefsFileName, E.Message]); end; end; *************** *** 295,320 **** procedure TInstantConnectionManager.SaveConnectionDefs; var ! FileStream: TFileStream; ! MemoryStream: TMemoryStream; begin ! if DefsFileName = '' then ! Exit; ! FileStream := TFileStream.Create(DefsFileName, fmCreate); try ! if FileFormat = sfBinary then ! InstantWriteObjectToStream(FileStream, ConnectionDefs) ! else ! begin ! MemoryStream := TMemoryStream.Create; ! try ! InstantWriteObjectToStream(MemoryStream, ConnectionDefs); ! MemoryStream.Position := 0; ! InstantObjectBinaryToText(MemoryStream, FileStream); ! finally ! MemoryStream.Free; ! end; ! end; finally ! FileStream.Free; end; end; --- 296,307 ---- procedure TInstantConnectionManager.SaveConnectionDefs; var ! OutputStream: TStream; begin ! OutputStream := CreateConnectionDefsOutputStream(); try ! if Assigned(OutputStream) then ! InstantWriteObject(OutputStream, FileFormat, ConnectionDefs); finally ! OutputStream.Free; end; end; *************** *** 352,356 **** end; ! function TInstantConnectionManager.isConnected: boolean; var i: Integer; --- 339,343 ---- end; ! function TInstantConnectionManager.IsConnected: Boolean; var i: Integer; *************** *** 360,364 **** if not Assigned(OnIsConnected) then Exit; ! for i := 0 to ConnectionDefs.Count - 1 do begin ConnectionDef := ConnectionDefs.Items[i]; --- 347,351 ---- if not Assigned(OnIsConnected) then Exit; ! for i := 0 to Pred(ConnectionDefs.Count) do begin ConnectionDef := ConnectionDefs.Items[i]; *************** *** 369,371 **** --- 356,374 ---- end; + function TInstantConnectionManager.CreateConnectionDefsInputStream: TStream; + begin + if FileExists(DefsFileName) then + Result := TFileStream.Create(DefsFileName, fmOpenRead) + else + Result := nil; + end; + + function TInstantConnectionManager.CreateConnectionDefsOutputStream: TStream; + begin + if DefsFileName <> '' then + Result := TFileStream.Create(DefsFileName, fmCreate) + else + Result := nil; + end; + end. |