Skip to content

Commit

Permalink
HaikuDepot: Implemented filtering by category
Browse files Browse the repository at this point in the history
  • Loading branch information
stippi committed Aug 10, 2013
1 parent 4abd2b7 commit 4247995
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 22 deletions.
25 changes: 16 additions & 9 deletions src/apps/haiku-depot/FilterView.cpp
Expand Up @@ -23,20 +23,14 @@
#define B_TRANSLATION_CONTEXT "FilterView"


enum {
MSG_CATEGORY_SELECTED = 'ctsl',
MSG_REPOSITORY_SELECTED = 'rpsl',
MSG_SEARCH_TERMS_MODIFIED = 'stmd',
};


FilterView::FilterView(const Model& model)
:
BGroupView("filter view")
{
// Contruct category popup
BPopUpMenu* categoryMenu = new BPopUpMenu(B_TRANSLATE("Show"));
categoryMenu->AddItem(new BMenuItem(B_TRANSLATE("All packages"), NULL));
categoryMenu->AddItem(new BMenuItem(B_TRANSLATE("All packages"),
new BMessage(MSG_CATEGORY_SELECTED)));
categoryMenu->AddItem(new BSeparatorItem());

const CategoryList& categories = model.Categories();
Expand Down Expand Up @@ -68,8 +62,21 @@ FilterView::FilterView(const Model& model)

// Construct repository popup
BPopUpMenu* repositoryMenu = new BPopUpMenu(B_TRANSLATE("Depot"));
repositoryMenu->AddItem(new BMenuItem(B_TRANSLATE("All depots"), NULL));
repositoryMenu->AddItem(new BMenuItem(B_TRANSLATE("All depots"),
new BMessage(MSG_DEPOT_SELECTED)));
repositoryMenu->ItemAt(0)->SetMarked(true);

repositoryMenu->AddItem(new BSeparatorItem());

const DepotList& depots = model.Depots();
for (int i = 0; i < depots.CountItems(); i++) {
const DepotInfo& depot = depots.ItemAtFast(i);
BMessage* message = new BMessage(MSG_DEPOT_SELECTED);
message->AddString("name", depot.Name());
BMenuItem* item = new BMenuItem(depot.Name(), message);
repositoryMenu->AddItem(item);
}

fRepositoryField = new BMenuField("repository", B_TRANSLATE("Depot:"),
repositoryMenu);

Expand Down
7 changes: 7 additions & 0 deletions src/apps/haiku-depot/FilterView.h
Expand Up @@ -13,6 +13,13 @@ class BTextControl;
class Model;


enum {
MSG_CATEGORY_SELECTED = 'ctsl',
MSG_DEPOT_SELECTED = 'dpsl',
MSG_SEARCH_TERMS_MODIFIED = 'stmd',
};


class FilterView : public BGroupView {
public:
FilterView(const Model& model);
Expand Down
13 changes: 12 additions & 1 deletion src/apps/haiku-depot/MainWindow.cpp
Expand Up @@ -33,6 +33,8 @@ MainWindow::MainWindow(BRect frame)
B_DOCUMENT_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL,
B_ASYNCHRONOUS_CONTROLS | B_AUTO_UPDATE_SIZE_LIMITS)
{
_InitDummyModel();

BMenuBar* menuBar = new BMenuBar(B_TRANSLATE("Main Menu"));
_BuildMenu(menuBar);

Expand All @@ -59,7 +61,6 @@ MainWindow::MainWindow(BRect frame)
fSplitView->SetCollapsible(0, false);
fSplitView->SetCollapsible(1, false);

_InitDummyModel();
_AdoptModel();
}

Expand Down Expand Up @@ -100,6 +101,16 @@ MainWindow::MessageReceived(BMessage* message)
break;
}

case MSG_CATEGORY_SELECTED:
{
BString name;
if (message->FindString("name", &name) != B_OK)
name = "";
fModel.SetCategory(name);
_AdoptModel();
break;
}

default:
BWindow::MessageReceived(message);
break;
Expand Down
36 changes: 36 additions & 0 deletions src/apps/haiku-depot/Model.cpp
Expand Up @@ -60,6 +60,32 @@ class DepotFilter : public PackageFilter {
};


class CategoryFilter : public PackageFilter {
public:
CategoryFilter(const BString& category)
:
fCategory(category)
{
}

virtual bool AcceptsPackage(const PackageInfo& package) const
{
const CategoryList& categories = package.Categories();
for (int i = categories.CountItems() - 1; i >= 0; i--) {
const CategoryRef& category = categories.ItemAtFast(i);
if (category.Get() == NULL)
continue;
if (category->Name() == fCategory)
return true;
}
return false;
}

private:
BString fCategory;
};


// #pragma mark - Model


Expand Down Expand Up @@ -137,3 +163,13 @@ Model::AddDepot(const DepotInfo& depot)
return fDepots.Add(depot);
}


void
Model::SetCategory(const BString& category)
{
if (category.Length() == 0)
fCategoryFilter.SetTo(new AnyFilter(), true);
else
fCategoryFilter.SetTo(new CategoryFilter(category), true);
}

7 changes: 6 additions & 1 deletion src/apps/haiku-depot/Model.h
Expand Up @@ -28,6 +28,8 @@ class Model {
PackageInfoList CreatePackageList() const;

bool AddDepot(const DepotInfo& depot);
const DepotList& Depots() const
{ return fDepots; }

// Access to global categories
const CategoryRef& CategoryAudio() const
Expand All @@ -48,10 +50,13 @@ class Model {
const CategoryList& Categories() const
{ return fCategories; }

// Configure PackageFilters
void SetCategory(const BString& category);

private:
BString fSearchTerms;

DepotInfoList fDepots;
DepotList fDepots;

CategoryRef fCategoryAudio;
CategoryRef fCategoryVideo;
Expand Down
12 changes: 6 additions & 6 deletions src/apps/haiku-depot/PackageInfo.cpp
Expand Up @@ -584,23 +584,23 @@ PackageInfo::AddScreenshot(const BitmapRef& screenshot)

DepotInfo::DepotInfo()
:
fTitle(),
fName(),
fPackages()
{
}


DepotInfo::DepotInfo(const BString& title)
DepotInfo::DepotInfo(const BString& name)
:
fTitle(title),
fName(name),
fPackages()
{
}


DepotInfo::DepotInfo(const DepotInfo& other)
:
fTitle(other.fTitle),
fName(other.fName),
fPackages(other.fPackages)
{
}
Expand All @@ -609,7 +609,7 @@ DepotInfo::DepotInfo(const DepotInfo& other)
DepotInfo&
DepotInfo::operator=(const DepotInfo& other)
{
fTitle = other.fTitle;
fName = other.fName;
fPackages = other.fPackages;
return *this;
}
Expand All @@ -618,7 +618,7 @@ DepotInfo::operator=(const DepotInfo& other)
bool
DepotInfo::operator==(const DepotInfo& other) const
{
return fTitle == other.fTitle
return fName == other.fName
&& fPackages == other.fPackages;
}

Expand Down
10 changes: 5 additions & 5 deletions src/apps/haiku-depot/PackageInfo.h
Expand Up @@ -238,28 +238,28 @@ typedef List<PackageInfo, false> PackageInfoList;
class DepotInfo {
public:
DepotInfo();
DepotInfo(const BString& title);
DepotInfo(const BString& name);
DepotInfo(const DepotInfo& other);

DepotInfo& operator=(const DepotInfo& other);
bool operator==(const DepotInfo& other) const;
bool operator!=(const DepotInfo& other) const;

const BString& Title() const
{ return fTitle; }
const BString& Name() const
{ return fName; }

const PackageInfoList& PackageList() const
{ return fPackages; }

bool AddPackage(const PackageInfo& package);

private:
BString fTitle;
BString fName;
PackageInfoList fPackages;
};


typedef List<DepotInfo, false> DepotInfoList;
typedef List<DepotInfo, false> DepotList;


#endif // PACKAGE_INFO_H

0 comments on commit 4247995

Please sign in to comment.