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();
	}
}
Ejemplo n.º 2
0
void
RatePackageWindow::_QueryRatingThread()
{
	if (!Lock()) {
		fprintf(stderr, "rating query: Failed to lock window\n");
		return;
	}

	PackageInfoRef package(fPackage);

	Unlock();

	BAutolock locker(fModel.Lock());
	BString username = fModel.Username();
	locker.Unlock();

	if (package.Get() == NULL) {
		fprintf(stderr, "rating query: No package\n");
		_SetWorkerThread(-1);
		return;
	}

	WebAppInterface interface;
	BMessage info;
	const DepotInfo* depot = fModel.DepotForName(package->DepotName());
	BString repositoryCode;

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

	if (repositoryCode.Length() == 0) {
		printf("unable to obtain the repository code for depot; %s\n",
			package->DepotName().String());
	} else {
		status_t status = interface.RetrieveUserRating(
			package->Name(), package->Version(), package->Architecture(),
			repositoryCode, username, info);

	//	info.PrintToStream();

		BMessage result;
		if (status == B_OK && info.FindMessage("result", &result) == B_OK
			&& Lock()) {

			result.FindString("code", &fRatingID);
			result.FindBool("active", &fRatingActive);
			BString comment;
			if (result.FindString("comment", &comment) == B_OK) {
				MarkupParser parser;
				fRatingText = parser.CreateDocumentFromMarkup(comment);
				fTextView->SetTextDocument(fRatingText);
			}
			if (result.FindString("userRatingStabilityCode",
				&fStability) == B_OK) {
				int32 index = 0;
				for (int32 i = fStabilityCodes.CountItems() - 1; i >= 0; i--) {
					const StabilityRating& stability
						= fStabilityCodes.ItemAtFast(i);
					if (stability.Name() == fStability) {
						index = i;
						break;
					}
				}
				BMenuItem* item = fStabilityField->Menu()->ItemAt(index);
				if (item != NULL)
					item->SetMarked(true);
			}
			if (result.FindString("naturalLanguageCode",
				&fCommentLanguage) == B_OK) {
				BMenuItem* item = fCommentLanguageField->Menu()->ItemAt(
					fModel.SupportedLanguages().IndexOf(fCommentLanguage));
				if (item != NULL)
					item->SetMarked(true);
			}
			double rating;
			if (result.FindDouble("rating", &rating) == B_OK) {
				fRating = (float)rating;
				fSetRatingView->SetPermanentRating(fRating);
			}

			fRatingActiveCheckBox->SetValue(fRatingActive);
			fRatingActiveCheckBox->Show();

			fSendButton->SetLabel(B_TRANSLATE("Update"));

			Unlock();
		} else {
			fprintf(stderr, "rating query: Failed response: %s\n",
				strerror(status));
			if (!info.IsEmpty())
				info.PrintToStream();
		}
	}

	_SetWorkerThread(-1);
}