Ejemplo n.º 1
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);
}