HaikuDepot: Try to use correct architecture and preferred language...

... when retrieving package info. Added listener support for setting
the summary and description. The list view updates on summary changes.
Those happen (in theory) if the web app provides a translated summary.
This commit is contained in:
Stephan Aßmus
2014-08-31 01:31:16 +02:00
parent 27dd7f6d71
commit 6e48c81bad
9 changed files with 157 additions and 38 deletions
+6 -2
View File
@@ -475,7 +475,10 @@ MainWindow::_RefreshPackageList()
| BSolver::B_FIND_IN_PROVIDES, | BSolver::B_FIND_IN_PROVIDES,
packages); packages);
if (result != B_OK) { if (result != B_OK) {
// TODO: notify user BString message(B_TRANSLATE("An error occurred while "
"obtaining the package list: %message%"));
message.ReplaceFirst("%message%", strerror(result));
_NotifyUser("Error", message.String());
return; return;
} }
@@ -532,7 +535,8 @@ MainWindow::_RefreshPackageList()
PublisherInfo(BitmapRef(), publisherName, PublisherInfo(BitmapRef(), publisherName,
"", publisherURL), repoPackageInfo.Summary(), "", publisherURL), repoPackageInfo.Summary(),
repoPackageInfo.Description(), repoPackageInfo.Description(),
repoPackageInfo.Flags()), repoPackageInfo.Flags(),
repoPackageInfo.ArchitectureName()),
true); true);
if (modelInfo.Get() == NULL) if (modelInfo.Get() == NULL)
+70 -17
View File
@@ -17,6 +17,7 @@
#include <Entry.h> #include <Entry.h>
#include <FindDirectory.h> #include <FindDirectory.h>
#include <File.h> #include <File.h>
#include <LocaleRoster.h>
#include <Message.h> #include <Message.h>
#include <Path.h> #include <Path.h>
@@ -588,6 +589,18 @@ int32
Model::_PopulateAllPackagesEntry(void* cookie) Model::_PopulateAllPackagesEntry(void* cookie)
{ {
Model* model = static_cast<Model*>(cookie); Model* model = static_cast<Model*>(cookie);
model->fPreferredLanguage = "en";
BLocaleRoster* localeRoster = BLocaleRoster::Default();
if (localeRoster != NULL) {
BMessage preferredLanguages;
if (localeRoster->GetPreferredLanguages(&preferredLanguages) == B_OK) {
BString language;
if (preferredLanguages.FindString("language", 0, &language) == B_OK)
language.CopyInto(model->fPreferredLanguage, 0, 2);
}
}
model->_PopulateAllPackagesThread(true); model->_PopulateAllPackagesThread(true);
model->_PopulateAllPackagesThread(false); model->_PopulateAllPackagesThread(false);
return 0; return 0;
@@ -641,7 +654,7 @@ Model::_PopulateAllPackagesThread(bool fromCacheOnly)
// list view, so without the user clicking the package. // list view, so without the user clicking the package.
} }
if (!fStopPopulatingAllPackages && bulkPackageList.CountItems() > 0) { if (bulkPackageList.CountItems() > 0) {
_PopulatePackageInfos(bulkPackageList, fromCacheOnly, _PopulatePackageInfos(bulkPackageList, fromCacheOnly,
packagesWithIconsList); packagesWithIconsList);
} }
@@ -663,11 +676,15 @@ void
Model::_PopulatePackageInfos(PackageList& packages, bool fromCacheOnly, Model::_PopulatePackageInfos(PackageList& packages, bool fromCacheOnly,
PackageList& packagesWithIcons) PackageList& packagesWithIcons)
{ {
if (fStopPopulatingAllPackages)
return;
if (fromCacheOnly) if (fromCacheOnly)
return; return;
// Retrieve info from web-app // Retrieve info from web-app
WebAppInterface interface; WebAppInterface interface;
interface.SetPreferredLanguage(fPreferredLanguage);
BMessage info; BMessage info;
StringList packageNames; StringList packageNames;
@@ -686,6 +703,8 @@ Model::_PopulatePackageInfos(PackageList& packages, bool fromCacheOnly,
&& result.FindMessage("pkgs", &pkgs) == B_OK) { && result.FindMessage("pkgs", &pkgs) == B_OK) {
int32 index = 0; int32 index = 0;
while (true) { while (true) {
if (fStopPopulatingAllPackages)
return;
BString name; BString name;
name << index++; name << index++;
BMessage pkgInfo; BMessage pkgInfo;
@@ -753,9 +772,11 @@ Model::_PopulatePackageInfo(const PackageInfoRef& package, bool fromCacheOnly)
// Retrieve info from web-app // Retrieve info from web-app
WebAppInterface interface; WebAppInterface interface;
interface.SetPreferredLanguage(fPreferredLanguage);
BMessage info; BMessage info;
status_t status = interface.RetrievePackageInfo(package->Title(), info); status_t status = interface.RetrievePackageInfo(package->Title(),
package->Architecture(), info);
if (status == B_OK) { if (status == B_OK) {
// Parse message // Parse message
// info.PrintToStream(); // info.PrintToStream();
@@ -766,15 +787,46 @@ Model::_PopulatePackageInfo(const PackageInfoRef& package, bool fromCacheOnly)
} }
void static void
Model::_PopulatePackageInfo(const PackageInfoRef& package, append_word_list(BString& words, const char* word)
const BMessage& data)
{ {
const char* categoriesDebug = ""; if (words.Length() > 0)
const char* ratingDebug = ""; words << ", ";
words << word;
}
void
Model::_PopulatePackageInfo(const PackageInfoRef& package, const BMessage& data)
{
BAutolock locker(&fLock);
BString foundInfo;
BMessage versions;
BMessage version;
if (data.FindMessage("versions", &versions) == B_OK
&& versions.FindMessage("0", &version)) {
BString languageCode;
if (version.FindString("naturalLanguageCode", &languageCode) == B_OK) {
if (languageCode == fPreferredLanguage) {
BString summary;
if (version.FindString("summary", &summary) == B_OK) {
package->SetShortDescription(summary);
append_word_list(foundInfo, "summary");
}
BString description;
if (version.FindString("description", &description) == B_OK) {
package->SetFullDescription(description);
append_word_list(foundInfo, "description");
}
}
}
}
BMessage categories; BMessage categories;
if (data.FindMessage("pkgCategoryCodes", &categories) == B_OK) { if (data.FindMessage("pkgCategoryCodes", &categories) == B_OK) {
bool foundCategory = false;
int32 index = 0; int32 index = 0;
while (true) { while (true) {
BString name; BString name;
@@ -810,8 +862,10 @@ Model::_PopulatePackageInfo(const PackageInfoRef& package,
// This should then be used instead of hard-coded // This should then be used instead of hard-coded
// categories and translations in the app. // categories and translations in the app.
categoriesDebug = "categories"; foundCategory = true;
} }
if (foundCategory)
append_word_list(foundInfo, "categories");
} }
double derivedRating; double derivedRating;
double derivedRatingSampleSize; double derivedRatingSampleSize;
@@ -824,14 +878,13 @@ Model::_PopulatePackageInfo(const PackageInfoRef& package,
summary.ratingCount = (int)derivedRatingSampleSize; summary.ratingCount = (int)derivedRatingSampleSize;
package->SetRatingSummary(summary); package->SetRatingSummary(summary);
if (strlen(categoriesDebug) > 0) append_word_list(foundInfo, "rating");
ratingDebug = ", rating";
else
ratingDebug = "rating";
} }
} }
printf("Populated package info for %s: %s%s\n", if (foundInfo.Length() > 0) {
package->Title().String(), categoriesDebug, ratingDebug); printf("Populated package info for %s: %s\n",
package->Title().String(), foundInfo.String());
}
} }
@@ -862,6 +915,7 @@ Model::_PopulatePackageIcon(const PackageInfoRef& package, bool fromCacheOnly)
if (fromCacheOnly || now - modifiedTime < 60 * 60) { if (fromCacheOnly || now - modifiedTime < 60 * 60) {
// Cache file is recent enough, just use it and return. // Cache file is recent enough, just use it and return.
BitmapRef bitmapRef(new(std::nothrow)SharedBitmap(iconFile), true); BitmapRef bitmapRef(new(std::nothrow)SharedBitmap(iconFile), true);
BAutolock locker(&fLock);
package->SetIcon(bitmapRef); package->SetIcon(bitmapRef);
return; return;
} }
@@ -877,7 +931,9 @@ Model::_PopulatePackageIcon(const PackageInfoRef& package, bool fromCacheOnly)
status_t status = interface.RetrievePackageIcon(package->Title(), &buffer); status_t status = interface.RetrievePackageIcon(package->Title(), &buffer);
if (status == B_OK) { if (status == B_OK) {
BitmapRef bitmapRef(new(std::nothrow)SharedBitmap(buffer), true); BitmapRef bitmapRef(new(std::nothrow)SharedBitmap(buffer), true);
BAutolock locker(&fLock);
package->SetIcon(bitmapRef); package->SetIcon(bitmapRef);
locker.Unlock();
if (iconFile.SetTo(iconCachePath.Path(), if (iconFile.SetTo(iconCachePath.Path(),
B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE) == B_OK) { B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE) == B_OK) {
iconFile.Write(buffer.Buffer(), buffer.BufferLength()); iconFile.Write(buffer.Buffer(), buffer.BufferLength());
@@ -893,9 +949,6 @@ Model::_HasNativeIcon(const BMessage& message) const
if (message.FindMessage("pkgIcons", &pkgIcons) != B_OK) if (message.FindMessage("pkgIcons", &pkgIcons) != B_OK)
return false; return false;
if (!pkgIcons.IsEmpty())
pkgIcons.PrintToStream();
int32 index = 0; int32 index = 0;
while (true) { while (true) {
BString name; BString name;
+1
View File
@@ -156,6 +156,7 @@ private:
thread_id fPopulateAllPackagesThread; thread_id fPopulateAllPackagesThread;
volatile bool fStopPopulatingAllPackages; volatile bool fStopPopulatingAllPackages;
BString fPreferredLanguage;
}; };
+30 -5
View File
@@ -569,7 +569,8 @@ PackageInfo::PackageInfo()
fState(NONE), fState(NONE),
fDownloadProgress(0.0), fDownloadProgress(0.0),
fFlags(0), fFlags(0),
fSystemDependency(false) fSystemDependency(false),
fArchitecture()
{ {
} }
@@ -577,7 +578,7 @@ PackageInfo::PackageInfo()
PackageInfo::PackageInfo(const BString& title, PackageInfo::PackageInfo(const BString& title,
const BString& version, const PublisherInfo& publisher, const BString& version, const PublisherInfo& publisher,
const BString& shortDescription, const BString& fullDescription, const BString& shortDescription, const BString& fullDescription,
int32 flags) int32 flags, const char* architecture)
: :
fIcon(), fIcon(),
fTitle(title), fTitle(title),
@@ -593,7 +594,8 @@ PackageInfo::PackageInfo(const BString& title,
fState(NONE), fState(NONE),
fDownloadProgress(0.0), fDownloadProgress(0.0),
fFlags(flags), fFlags(flags),
fSystemDependency(false) fSystemDependency(false),
fArchitecture(architecture)
{ {
} }
@@ -615,7 +617,8 @@ PackageInfo::PackageInfo(const PackageInfo& other)
fInstallationLocations(other.fInstallationLocations), fInstallationLocations(other.fInstallationLocations),
fDownloadProgress(other.fDownloadProgress), fDownloadProgress(other.fDownloadProgress),
fFlags(other.fFlags), fFlags(other.fFlags),
fSystemDependency(other.fSystemDependency) fSystemDependency(other.fSystemDependency),
fArchitecture(other.fArchitecture)
{ {
} }
@@ -639,6 +642,7 @@ PackageInfo::operator=(const PackageInfo& other)
fDownloadProgress = other.fDownloadProgress; fDownloadProgress = other.fDownloadProgress;
fFlags = other.fFlags; fFlags = other.fFlags;
fSystemDependency = other.fSystemDependency; fSystemDependency = other.fSystemDependency;
fArchitecture = other.fArchitecture;
return *this; return *this;
} }
@@ -660,7 +664,8 @@ PackageInfo::operator==(const PackageInfo& other) const
&& fState == other.fState && fState == other.fState
&& fFlags == other.fFlags && fFlags == other.fFlags
&& fDownloadProgress == other.fDownloadProgress && fDownloadProgress == other.fDownloadProgress
&& fSystemDependency == other.fSystemDependency; && fSystemDependency == other.fSystemDependency
&& fArchitecture == other.fArchitecture;
} }
@@ -671,6 +676,26 @@ PackageInfo::operator!=(const PackageInfo& other) const
} }
void
PackageInfo::SetShortDescription(const BString& description)
{
if (fShortDescription != description) {
fShortDescription = description;
_NotifyListeners(PKG_CHANGED_SUMMARY);
}
}
void
PackageInfo::SetFullDescription(const BString& description)
{
if (fFullDescription != description) {
fFullDescription = description;
_NotifyListeners(PKG_CHANGED_DESCRIPTION);
}
}
void void
PackageInfo::SetIcon(const BitmapRef& icon) PackageInfo::SetIcon(const BitmapRef& icon)
{ {
+8 -1
View File
@@ -224,7 +224,8 @@ public:
const PublisherInfo& publisher, const PublisherInfo& publisher,
const BString& shortDescription, const BString& shortDescription,
const BString& fullDescription, const BString& fullDescription,
int32 packageFlags); int32 packageFlags,
const char* architecture);
PackageInfo(const PackageInfo& other); PackageInfo(const PackageInfo& other);
PackageInfo& operator=(const PackageInfo& other); PackageInfo& operator=(const PackageInfo& other);
@@ -235,8 +236,10 @@ public:
{ return fTitle; } { return fTitle; }
const BString& Version() const const BString& Version() const
{ return fVersion; } { return fVersion; }
void SetShortDescription(const BString& description);
const BString& ShortDescription() const const BString& ShortDescription() const
{ return fShortDescription; } { return fShortDescription; }
void SetFullDescription(const BString& description);
const BString& FullDescription() const const BString& FullDescription() const
{ return fFullDescription; } { return fFullDescription; }
const PublisherInfo& Publisher() const const PublisherInfo& Publisher() const
@@ -257,6 +260,9 @@ public:
{ return fSystemDependency; } { return fSystemDependency; }
void SetSystemDependency(bool isDependency); void SetSystemDependency(bool isDependency);
const BString Architecture() const
{ return fArchitecture; }
PackageState State() const PackageState State() const
{ return fState; } { return fState; }
void SetState(PackageState state); void SetState(PackageState state);
@@ -311,6 +317,7 @@ private:
PackageListenerList fListeners; PackageListenerList fListeners;
int32 fFlags; int32 fFlags;
bool fSystemDependency; bool fSystemDependency;
BString fArchitecture;
}; };
+8 -7
View File
@@ -10,13 +10,14 @@
enum { enum {
PKG_CHANGED_DESCRIPTION = 1 << 0, PKG_CHANGED_SUMMARY = 1 << 0,
PKG_CHANGED_RATINGS = 1 << 1, PKG_CHANGED_DESCRIPTION = 1 << 1,
PKG_CHANGED_SCREENSHOTS = 1 << 2, PKG_CHANGED_RATINGS = 1 << 2,
PKG_CHANGED_STATE = 1 << 3, PKG_CHANGED_SCREENSHOTS = 1 << 3,
PKG_CHANGED_ICON = 1 << 4, PKG_CHANGED_STATE = 1 << 4,
PKG_CHANGED_CHANGELOG = 1 << 5, PKG_CHANGED_ICON = 1 << 5,
PKG_CHANGED_CATEGORIES = 1 << 6 PKG_CHANGED_CHANGELOG = 1 << 6,
PKG_CHANGED_CATEGORIES = 1 << 7
// ... // ...
}; };
+16 -2
View File
@@ -124,6 +124,7 @@ public:
{ return fPackage; } { return fPackage; }
void UpdateTitle(); void UpdateTitle();
void UpdateSummary();
void UpdateState(); void UpdateState();
void UpdateRating(); void UpdateRating();
@@ -472,8 +473,8 @@ PackageRow::PackageRow(const PackageInfoRef& packageRef,
// Rating // Rating
UpdateRating(); UpdateRating();
// Description // Summary
SetField(new BStringField(package.ShortDescription()), kDescriptionColumn); UpdateSummary();
// Size // Size
// TODO: Store package size // TODO: Store package size
@@ -518,6 +519,17 @@ PackageRow::UpdateState()
} }
void
PackageRow::UpdateSummary()
{
if (fPackage.Get() == NULL)
return;
SetField(new BStringField(fPackage->ShortDescription()),
kDescriptionColumn);
}
void void
PackageRow::UpdateRating() PackageRow::UpdateRating()
{ {
@@ -676,6 +688,8 @@ PackageListView::MessageReceived(BMessage* message)
BAutolock _(fModelLock); BAutolock _(fModelLock);
PackageRow* row = _FindRow(title); PackageRow* row = _FindRow(title);
if (row != NULL) { if (row != NULL) {
if ((changes & PKG_CHANGED_SUMMARY) != 0)
row->UpdateSummary();
if ((changes & PKG_CHANGED_RATINGS) != 0) if ((changes & PKG_CHANGED_RATINGS) != 0)
row->UpdateRating(); row->UpdateRating();
if ((changes & PKG_CHANGED_STATE) != 0) if ((changes & PKG_CHANGED_STATE) != 0)
+13 -4
View File
@@ -232,6 +232,8 @@ WebAppInterface::fRequestIndex = 0;
WebAppInterface::WebAppInterface() WebAppInterface::WebAppInterface()
:
fLanguage("en")
{ {
} }
@@ -250,9 +252,16 @@ WebAppInterface::SetAuthorization(const BString& username,
} }
void
WebAppInterface::SetPreferredLanguage(const BString& language)
{
fLanguage = language;
}
status_t status_t
WebAppInterface::RetrievePackageInfo(const BString& packageName, WebAppInterface::RetrievePackageInfo(const BString& packageName,
BMessage& message) const BString& architecture, BMessage& message)
{ {
BString jsonString = JsonBuilder() BString jsonString = JsonBuilder()
.AddValue("jsonrpc", "2.0") .AddValue("jsonrpc", "2.0")
@@ -261,8 +270,8 @@ WebAppInterface::RetrievePackageInfo(const BString& packageName,
.AddArray("params") .AddArray("params")
.AddObject() .AddObject()
.AddValue("name", packageName) .AddValue("name", packageName)
.AddValue("architectureCode", "x86_gcc2") .AddValue("architectureCode", architecture)
.AddValue("naturalLanguageCode", "en") .AddValue("naturalLanguageCode", fLanguage)
.AddValue("versionType", "NONE") .AddValue("versionType", "NONE")
.EndObject() .EndObject()
.EndArray() .EndArray()
@@ -288,7 +297,7 @@ WebAppInterface::RetrieveBulkPackageInfo(const StringList& packageNames,
.AddStrings(packageNames) .AddStrings(packageNames)
.EndArray() .EndArray()
.AddValue("architectureCode", "x86_gcc2") .AddValue("architectureCode", "x86_gcc2")
.AddValue("naturalLanguageCode", "en") .AddValue("naturalLanguageCode", fLanguage)
.AddValue("versionType", "LATEST") .AddValue("versionType", "LATEST")
.AddArray("filter") .AddArray("filter")
.AddItem("PKGCATEGORIES") .AddItem("PKGCATEGORIES")
+5
View File
@@ -25,9 +25,12 @@ public:
void SetAuthorization(const BString& username, void SetAuthorization(const BString& username,
const BString& password); const BString& password);
void SetPreferredLanguage(const BString& language);
void SetArchitecture(const BString& architecture);
status_t RetrievePackageInfo( status_t RetrievePackageInfo(
const BString& packageName, const BString& packageName,
const BString& architecture,
BMessage& message); BMessage& message);
status_t RetrieveBulkPackageInfo( status_t RetrieveBulkPackageInfo(
@@ -45,6 +48,8 @@ private:
private: private:
BString fUsername; BString fUsername;
BString fPassword; BString fPassword;
BString fLanguage;
BString fArchitecture;
static int fRequestIndex; static int fRequestIndex;
}; };