UPD: MacCloud/DropBox step-38: improve exception handling and eliminate duplicate code

This commit is contained in:
rich2014 2025-04-10 22:18:08 +08:00
commit 59bc178c81
4 changed files with 44 additions and 33 deletions

View file

@ -207,7 +207,6 @@ type
_listFolderSession: TDropBoxListFolderSession;
public
class function driverName: String; override;
class function isMatched(const name: String): Boolean; override;
class function createInstance: TCloudDriver; override;
public
constructor Create( const config: TDropBoxConfig );
@ -1175,11 +1174,6 @@ begin
Result:= 'DropBox';
end;
class function TDropBoxClient.isMatched(const name: String): Boolean;
begin
Result:= name='DropBox';
end;
class function TDropBoxClient.createInstance: TCloudDriver;
begin
Result:= TDropBoxClient.Create( dropBoxConfig );

View file

@ -59,12 +59,26 @@ begin
macCloudDriverConfigManager.loadFromSecurity;
end;
procedure saveConfig( const path: String );
procedure saveConfig( const path: String = '' );
var
configPath: String;
begin
macCloudDriverConfigManager.saveToCommon( path );
if path = EmptyStr then
configPath:= macCloudPlugin.configPath
else
configPath:= path;
macCloudDriverConfigManager.saveToCommon( configPath );
macCloudDriverConfigManager.saveToSecurity;
end;
function getDriver( const parser: TCloudPathParser ): TCloudDriver;
begin
if parser.driverPath = EmptyStr then
Result:= TCloudRootDriver.Create
else
Result:= parser.driver;
end;
procedure ExtensionInitialize(StartupInfo: PExtensionStartupInfo); cdecl;
var
configPath: String;
@ -305,16 +319,14 @@ var
procedure doCreateFolder;
var
driver: TCloudDriver;
folderName: String;
begin
parser:= TCloudPathParser.Create( TStringUtil.widecharsToString(RemoteDir) );
if parser.driverPath = EmptyStr then begin
driver:= TCloudRootDriver.Create;
driver.createFolder( parser.connectionName );
end else begin
driver:= parser.driver;
driver.createFolder( parser.driverPath );
end;
if parser.driverPath = EmptyStr then
folderName:= parser.connectionName
else
folderName:= parser.driverPath;
getDriver(parser).createFolder( folderName );
end;
begin
@ -340,17 +352,14 @@ var
procedure doDelete;
var
utf8Path: String;
driver: TCloudDriver;
begin
utf8Path:= TStringUtil.widecharsToString(RemoteName);
parser:= TCloudPathParser.Create( utf8Path );
if parser.driverPath = EmptyStr then begin
driver:= TCloudRootDriver.Create;
driver.delete( parser.connectionName );
end else begin
driver:= parser.driver;
driver.delete( parser.driverPath );
end;
if parser.driverPath = EmptyStr then
utf8Path:= parser.connectionName
else
utf8Path:= parser.driverPath;
getDriver(parser).delete( utf8Path );
end;
begin
@ -392,15 +401,14 @@ var
if ret then begin
parserOld:= TCloudPathParser.Create( TStringUtil.widecharsToString(OldName) );
parserNew:= TCloudPathParser.Create( TStringUtil.widecharsToString(NewName) );
driver:= getDriver( parserOld );
if parserOld.driverPath = EmptyStr then begin
if parserNew.driverPath <> EmptyStr then
raise ENotSupportedException.Create( 'Connection not support copying' );
driver:= TCloudRootDriver.Create;
driver.copyOrMove( parserOld.connectionName, parserNew.connectionName, True );
end else begin
if parserOld.connection <> parserNew.connection then
raise ENotSupportedException.Create( 'Internal copy/move functions cannot be used between different accounts' );
driver:= parserNew.driver;
driver.copyOrMove( parserOld.driverPath, parserNew.driverPath, Move );
end;
macCloudPlugin.progress( oldName, newName, 100 );
@ -446,13 +454,13 @@ var
if utf8Verb = 'open' then begin
if parser.connectionName = CONST_ADD_NEW_CONNECTION then begin
TCloudOptionsUtil.addAndShow;
saveConfig( macCloudPlugin.configPath );
saveConfig;
end else begin
Exit( FS_EXEC_SYMLINK );
end;
end else if utf8Verb = 'properties' then begin
TCloudOptionsUtil.show( parser.connectionName );
saveConfig( macCloudPlugin.configPath );
saveConfig;
end;
end;
@ -537,7 +545,8 @@ end;
procedure TCloudRootDriver.createFolder(const path: String);
begin
TCloudOptionsUtil.addAndShow( path );
saveConfig( macCloudPlugin.configPath );
saveConfig;
self.Free;
end;
procedure TCloudRootDriver.delete(const path: String);
@ -546,7 +555,8 @@ var
begin
TLogUtil.logInformation( 'Connection Deleted: ' + connectionName );
cloudConnectionManager.delete( connectionName );
saveConfig( macCloudPlugin.configPath );
saveConfig;
self.Free;
end;
procedure TCloudRootDriver.copyOrMove(const fromPath: String;
@ -561,7 +571,8 @@ begin
TLogUtil.logInformation( 'Connection Rename: ' + connectionOldName + ' --> ' + connectionNewName );
connection:= cloudConnectionManager.get( connectionOldName );
connection.name:= connectionNewName;
saveConfig( macCloudPlugin.configPath );
saveConfig;
self.Free;
end;
end.

View file

@ -53,7 +53,6 @@ type
TCloudDriver = class
public
class function driverName: String; virtual; abstract;
class function isMatched( const name: String ): Boolean; virtual; abstract;
class function createInstance: TCloudDriver; virtual; abstract;
public
function clone: TCloudDriver; virtual; abstract;
@ -202,7 +201,7 @@ var
begin
for i:= 0 to _classes.Count - 1 do begin
cloudDriverClass:= TCloudDriverClass( _classes[i] );
if NOT cloudDriverClass.isMatched(name) then
if cloudDriverClass.driverName <> name then
continue;
Exit( cloudDriverClass );
end;

View file

@ -361,6 +361,13 @@ begin
driver.unauthorize
else
driver.authorize;
except
on e: Exception do begin
TLogUtil.logError( 'in TCloudOptionsWindow: ' + e.Message );
end;
end;
try
self.propertyView.updateConnectStatus;
except
on e: Exception do begin