1/*
2 * Copyright 2013, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Ingo Weinhold <ingo_weinhold@gmx.de>
7 */
8
9
10#include <mime/DatabaseDirectory.h>
11
12#include <fs_attr.h>
13#include <Node.h>
14#include <StringList.h>
15
16#include <mime/database_support.h>
17#include <mime/DatabaseLocation.h>
18
19
20namespace BPrivate {
21namespace Storage {
22namespace Mime {
23
24
25DatabaseDirectory::DatabaseDirectory()
26	:
27	BMergedDirectory(B_COMPARE)
28{
29}
30
31
32DatabaseDirectory::~DatabaseDirectory()
33{
34}
35
36
37status_t
38DatabaseDirectory::Init(DatabaseLocation* databaseLocation,
39	const char* superType)
40{
41	status_t error = BMergedDirectory::Init();
42	if (error != B_OK)
43		return error;
44
45	const BStringList& directories = databaseLocation->Directories();
46	int32 count = directories.CountStrings();
47	for (int32 i = 0; i < count; i++) {
48		BString directory = directories.StringAt(i);
49		if (superType != NULL)
50			directory << '/' << superType;
51
52		AddDirectory(directory);
53	}
54
55	return B_OK;
56}
57
58
59bool
60DatabaseDirectory::ShallPreferFirstEntry(const entry_ref& entry1, int32 index1,
61	const entry_ref& entry2, int32 index2)
62{
63	return _IsValidMimeTypeEntry(entry1) || !_IsValidMimeTypeEntry(entry2);
64}
65
66
67bool
68DatabaseDirectory::_IsValidMimeTypeEntry(const entry_ref& entry)
69{
70	// check whether the MIME:TYPE attribute exists
71	BNode node;
72	attr_info info;
73	return node.SetTo(&entry) == B_OK
74		&& node.GetAttrInfo(BPrivate::Storage::Mime::kTypeAttr, &info) == B_OK;
75}
76
77
78} // namespace Mime
79} // namespace Storage
80} // namespace BPrivate
81