HaikuDepot: Fleshed out the code to support the virtual categories

This commit is contained in:
Stephan Aßmus
2013-08-10 20:37:11 +02:00
parent 706edd805f
commit 3a6ccc8579
4 changed files with 112 additions and 22 deletions
+16 -20
View File
@@ -23,6 +23,19 @@
#define B_TRANSLATION_CONTEXT "FilterView"
static void
add_categories_to_menu(const CategoryList& categories, BMenu* menu)
{
for (int i = 0; i < categories.CountItems(); i++) {
const CategoryRef& category = categories.ItemAtFast(i);
BMessage* message = new BMessage(MSG_CATEGORY_SELECTED);
message->AddString("name", category->Name());
BMenuItem* item = new BMenuItem(category->Label(), message);
menu->AddItem(item);
}
}
FilterView::FilterView(const Model& model)
:
BGroupView("filter view")
@@ -32,29 +45,12 @@ FilterView::FilterView(const Model& model)
categoryMenu->AddItem(new BMenuItem(B_TRANSLATE("All packages"),
new BMessage(MSG_CATEGORY_SELECTED)));
categoryMenu->AddItem(new BSeparatorItem());
const CategoryList& categories = model.Categories();
for (int i = 0; i < categories.CountItems(); i++) {
const CategoryRef& category = categories.ItemAtFast(i);
BMessage* message = new BMessage(MSG_CATEGORY_SELECTED);
message->AddString("name", category->Name());
BMenuItem* item = new BMenuItem(category->Label(), message);
categoryMenu->AddItem(item);
}
add_categories_to_menu(model.Categories(), categoryMenu);
categoryMenu->AddItem(new BSeparatorItem());
categoryMenu->AddItem(new BMenuItem(B_TRANSLATE("Installed packages"),
NULL));
categoryMenu->AddItem(new BMenuItem(B_TRANSLATE("Uninstalled packages"),
NULL));
categoryMenu->AddItem(new BMenuItem(B_TRANSLATE("User selected packages"),
NULL));
add_categories_to_menu(model.UserCategories(), categoryMenu);
categoryMenu->AddItem(new BSeparatorItem());
add_categories_to_menu(model.ProgressCategories(), categoryMenu);
categoryMenu->AddItem(new BMenuItem(B_TRANSLATE("Downloading"), NULL));
categoryMenu->AddItem(new BMenuItem(B_TRANSLATE("Update available"), NULL));
categoryMenu->ItemAt(0)->SetMarked(true);
fShowField = new BMenuField("category", B_TRANSLATE("Show:"),
+3
View File
@@ -228,4 +228,7 @@ MainWindow::_InitDummyModel()
depot.AddPackage(paladin);
fModel.AddDepot(depot);
fModel.SetPackageState(wonderbrush, UNINSTALLED);
fModel.SetPackageState(paladin, ACTIVATED);
}
+85 -2
View File
@@ -86,6 +86,46 @@ private:
};
class ContainedInFilter : public PackageFilter {
public:
ContainedInFilter(const PackageList& packageList)
:
fPackageList(packageList)
{
}
virtual bool AcceptsPackage(const PackageInfo& package) const
{
return fPackageList.Contains(package);
}
private:
const PackageList& fPackageList;
};
class ContainedInEitherFilter : public PackageFilter {
public:
ContainedInEitherFilter(const PackageList& packageListA,
const PackageList& packageListB)
:
fPackageListA(packageListA),
fPackageListB(packageListB)
{
}
virtual bool AcceptsPackage(const PackageInfo& package) const
{
return fPackageListA.Contains(package)
|| fPackageListB.Contains(package);
}
private:
const PackageList& fPackageListA;
const PackageList& fPackageListB;
};
// #pragma mark - Model
@@ -128,6 +168,34 @@ Model::Model()
fCategories.Add(fCategoryDevelopment);
fCategories.Add(fCategoryCommandLine);
fCategories.Add(fCategoryGames);
// A category for packages that the user installed.
fUserCategories.Add(CategoryRef(new PackageCategory(
BitmapRef(),
B_TRANSLATE("Installed packages"), "installed"), true));
// A category for packages that the user specifically uninstalled.
// For example, a user may have removed packages from a default
// Haiku installation
fUserCategories.Add(CategoryRef(new PackageCategory(
BitmapRef(),
B_TRANSLATE("Uninstalled packages"), "uninstalled"), true));
// A category for all packages that the user has installed or uninstalled.
// Those packages resemble what makes their system different from a
// fresh Haiku installation.
fUserCategories.Add(CategoryRef(new PackageCategory(
BitmapRef(),
B_TRANSLATE("User modified packages"), "modified"), true));
// Two categories to see just the packages which are downloading or
// have updates available
fProgressCategories.Add(CategoryRef(new PackageCategory(
BitmapRef(),
B_TRANSLATE("Downloading"), "downloading"), true));
fProgressCategories.Add(CategoryRef(new PackageCategory(
BitmapRef(),
B_TRANSLATE("Update available"), "updates"), true));
}
@@ -198,9 +266,24 @@ Model::SetPackageState(const PackageInfo& package, PackageState state)
void
Model::SetCategory(const BString& category)
{
PackageFilter* filter;
if (category.Length() == 0)
fCategoryFilter.SetTo(new AnyFilter(), true);
filter = new AnyFilter();
else if (category == "installed")
filter = new ContainedInFilter(fInstalledPackages);
else if (category == "uninstalled")
filter = new ContainedInFilter(fUninstalledPackages);
else if (category == "modified") {
filter = new ContainedInEitherFilter(fInstalledPackages,
fUninstalledPackages);
} else if (category == "downloading")
filter = new ContainedInFilter(fDownloadingPackages);
else if (category == "updates")
filter = new ContainedInFilter(fUpdateablePackages);
else
fCategoryFilter.SetTo(new CategoryFilter(category), true);
filter = new CategoryFilter(category);
fCategoryFilter.SetTo(filter, true);
}
+8
View File
@@ -49,6 +49,10 @@ public:
const CategoryList& Categories() const
{ return fCategories; }
const CategoryList& UserCategories() const
{ return fUserCategories; }
const CategoryList& ProgressCategories() const
{ return fProgressCategories; }
void SetPackageState(
const PackageInfo& package,
@@ -72,10 +76,14 @@ private:
// TODO: More categories
CategoryList fCategories;
CategoryList fUserCategories;
CategoryList fProgressCategories;
PackageList fInstalledPackages;
PackageList fActivatedPackages;
PackageList fUninstalledPackages;
PackageList fDownloadingPackages;
PackageList fUpdateablePackages;
PackageFilterRef fCategoryFilter;
PackageFilterRef fDepotFilter;