Ejemplo n.º 1
0
void
RatePackageWindow::_SendRatingThread()
{
	if (!Lock()) {
		fprintf(stderr, "upload rating: Failed to lock window\n");
		return;
	}

	BString package = fPackage->Name();
	BString architecture = fPackage->Architecture();
	BString repositoryCode;
	int rating = (int)fRating;
	BString stability = fStability;
	BString comment = fRatingText->Text();
	BString languageCode = fCommentLanguage;
	BString ratingID = fRatingID;
	bool active = fRatingActive;

	const DepotInfo* depot = fModel.DepotForName(fPackage->DepotName());

	if (depot != NULL)
		repositoryCode = depot->WebAppRepositoryCode();

	WebAppInterface interface = fModel.GetWebAppInterface();

	Unlock();

	if (repositoryCode.Length() == 0) {
		printf("unable to find the web app repository code for the local "
			"depot %s\n",
			fPackage->DepotName().String());
		return;
	}

	if (stability == "unspecified")
		stability = "";

	status_t status;
	BMessage info;
	if (ratingID.Length() > 0) {
		status = interface.UpdateUserRating(ratingID,
		languageCode, comment, stability, rating, active, info);
	} else {
		status = interface.CreateUserRating(package, architecture,
			repositoryCode, languageCode, comment, stability, rating, info);
	}

	BString error = B_TRANSLATE(
		"There was a puzzling response from the web service.");

	BMessage result;
	if (status == B_OK) {
		if (info.FindMessage("result", &result) == B_OK) {
			error = "";
		} else if (info.FindMessage("error", &result) == B_OK) {
			result.PrintToStream();
			BString message;
			if (result.FindString("message", &message) == B_OK) {
				if (message == "objectnotfound") {
					error = B_TRANSLATE("The package was not found by the "
						"web service. This probably means that it comes "
						"from a depot which is not tracked there. Rating "
						"such packages is unfortunately not supported.");
				} else {
					error << B_TRANSLATE(" It responded with: ");
					error << message;
				}
			}
		}
	} else {
		error = B_TRANSLATE(
			"It was not possible to contact the web service.");
	}

	if (!error.IsEmpty()) {
		BString failedTitle;
		if (ratingID.Length() > 0)
			failedTitle = B_TRANSLATE("Failed to update rating");
		else
			failedTitle = B_TRANSLATE("Failed to rate package");

		BAlert* alert = new(std::nothrow) BAlert(
			failedTitle,
			error,
			B_TRANSLATE("Close"), NULL, NULL,
			B_WIDTH_AS_USUAL, B_WARNING_ALERT);

		if (alert != NULL)
			alert->Go();

		fprintf(stderr,
			B_TRANSLATE("Failed to create or update rating: %s\n"),
			error.String());
		if (!info.IsEmpty())
			info.PrintToStream();

		_SetWorkerThread(-1);
	} else {
		_SetWorkerThread(-1);

		fModel.PopulatePackage(fPackage,
			Model::POPULATE_FORCE | Model::POPULATE_USER_RATINGS);

		BMessenger(this).SendMessage(B_QUIT_REQUESTED);

		BString message;
		if (ratingID.Length() > 0) {
			message = B_TRANSLATE("Your rating was updated successfully.");
		} else {
			message = B_TRANSLATE("Your rating was uploaded successfully. "
				"You can update or remove it at any time by rating the "
				"package again.");
		}

		BAlert* alert = new(std::nothrow) BAlert(
			B_TRANSLATE("Success"),
			message,
			B_TRANSLATE("Close"));

		if (alert != NULL)
			alert->Go();
	}
}