HaikuDepot: Reset package state on failure

* Save the package state before attempting to install/uninstall the package.
* In the case of failure set the package state back to what it was.
* Use the synchronous version of BAlert so that the package state won't
  change until the user clicks the Close button on the dialog.

Fixes #10838
This commit is contained in:
John Scipione
2015-10-22 10:44:27 -07:00
parent ae557b7951
commit c06aefa0ee
+10 -3
View File
@@ -125,6 +125,7 @@ public:
| BPackageManager::B_ADD_REMOTE_REPOSITORIES | BPackageManager::B_ADD_REMOTE_REPOSITORIES
| BPackageManager::B_REFRESH_REPOSITORIES); | BPackageManager::B_REFRESH_REPOSITORIES);
PackageInfoRef ref(Package()); PackageInfoRef ref(Package());
PackageState state = ref->State();
ref->SetState(PENDING); ref->SetState(PENDING);
fPackageManager->SetCurrentActionPackage(ref, true); fPackageManager->SetCurrentActionPackage(ref, true);
@@ -140,7 +141,6 @@ public:
try { try {
fPackageManager->Install(&packageNameString, 1); fPackageManager->Install(&packageNameString, 1);
} catch (BFatalErrorException ex) { } catch (BFatalErrorException ex) {
_SetDownloadedPackagesState(NONE);
BString errorString; BString errorString;
errorString.SetToFormat( errorString.SetToFormat(
"Fatal error occurred while installing package %s: " "Fatal error occurred while installing package %s: "
@@ -150,13 +150,16 @@ public:
errorString, B_TRANSLATE("Close"), NULL, NULL, errorString, B_TRANSLATE("Close"), NULL, NULL,
B_WIDTH_AS_USUAL, B_STOP_ALERT)); B_WIDTH_AS_USUAL, B_STOP_ALERT));
if (alert != NULL) if (alert != NULL)
alert->Go(NULL); alert->Go();
_SetDownloadedPackagesState(NONE);
ref->SetState(state);
return ex.Error(); return ex.Error();
} catch (BAbortedByUserException ex) { } catch (BAbortedByUserException ex) {
fprintf(stderr, "Installation of package " fprintf(stderr, "Installation of package "
"%s aborted by user: %s\n", packageNameString, "%s aborted by user: %s\n", packageNameString,
ex.Message().String()); ex.Message().String());
_SetDownloadedPackagesState(NONE); _SetDownloadedPackagesState(NONE);
ref->SetState(state);
return B_OK; return B_OK;
} catch (BNothingToDoException ex) { } catch (BNothingToDoException ex) {
fprintf(stderr, "Nothing to do while installing package " fprintf(stderr, "Nothing to do while installing package "
@@ -166,6 +169,7 @@ public:
fprintf(stderr, "Exception occurred while installing package " fprintf(stderr, "Exception occurred while installing package "
"%s: %s\n", packageNameString, ex.Message().String()); "%s: %s\n", packageNameString, ex.Message().String());
_SetDownloadedPackagesState(NONE); _SetDownloadedPackagesState(NONE);
ref->SetState(state);
return B_ERROR; return B_ERROR;
} }
@@ -241,6 +245,7 @@ public:
{ {
fPackageManager->Init(BPackageManager::B_ADD_INSTALLED_REPOSITORIES); fPackageManager->Init(BPackageManager::B_ADD_INSTALLED_REPOSITORIES);
PackageInfoRef ref(Package()); PackageInfoRef ref(Package());
PackageState state = ref->State();
fPackageManager->SetCurrentActionPackage(ref, false); fPackageManager->SetCurrentActionPackage(ref, false);
fPackageManager->AddProgressListener(this); fPackageManager->AddProgressListener(this);
const char* packageName = ref->Name().String(); const char* packageName = ref->Name().String();
@@ -256,7 +261,8 @@ public:
errorString, B_TRANSLATE("Close"), NULL, NULL, errorString, B_TRANSLATE("Close"), NULL, NULL,
B_WIDTH_AS_USUAL, B_STOP_ALERT)); B_WIDTH_AS_USUAL, B_STOP_ALERT));
if (alert != NULL) if (alert != NULL)
alert->Go(NULL); alert->Go();
ref->SetState(state);
return ex.Error(); return ex.Error();
} catch (BAbortedByUserException ex) { } catch (BAbortedByUserException ex) {
return B_OK; return B_OK;
@@ -265,6 +271,7 @@ public:
} catch (BException ex) { } catch (BException ex) {
fprintf(stderr, "Exception occurred while uninstalling package " fprintf(stderr, "Exception occurred while uninstalling package "
"%s: %s\n", packageName, ex.Message().String()); "%s: %s\n", packageName, ex.Message().String());
ref->SetState(state);
return B_ERROR; return B_ERROR;
} }