MimeTypeList.cpp revision a51764c5
1/*
2Open Tracker License
3
4Terms and Conditions
5
6Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
7
8Permission is hereby granted, free of charge, to any person obtaining a copy of
9this software and associated documentation files (the "Software"), to deal in
10the Software without restriction, including without limitation the rights to
11use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12of the Software, and to permit persons to whom the Software is furnished to do
13so, subject to the following conditions:
14
15The above copyright notice and this permission notice applies to all licensees
16and shall be included in all copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
20FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
23WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25Except as contained in this notice, the name of Be Incorporated shall not be
26used in advertising or otherwise to promote the sale, use or other dealings in
27this Software without prior written authorization from Be Incorporated.
28
29Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
30of Be Incorporated in the United States and other countries. Other brand product
31names are registered trademarks or trademarks of their respective holders.
32All rights reserved.
33*/
34
35#include <Mime.h>
36
37#include "AutoLock.h"
38#include "MimeTypeList.h"
39#include "Thread.h"
40
41
42ShortMimeInfo::ShortMimeInfo(const BMimeType &mimeType)
43	:	fCommonMimeType(true)
44{
45	fPrivateName = mimeType.Type();
46
47	char buffer[B_MIME_TYPE_LENGTH];
48
49	// weed out apps - their preferred handler is themselves
50	if (mimeType.GetPreferredApp(buffer) == B_OK
51		&& fPrivateName.ICompare(buffer) == 0)
52		fCommonMimeType = false;
53
54	// weed out metamimes without a short description
55	if (mimeType.GetShortDescription(buffer) != B_OK || buffer[0] == 0)
56		fCommonMimeType = false;
57	else
58		fShortDescription = buffer;
59}
60
61
62ShortMimeInfo::ShortMimeInfo(const char* shortDescription)
63	:	fShortDescription(shortDescription)
64{
65}
66
67const char*
68ShortMimeInfo::InternalName() const
69{
70	return fPrivateName.String();
71}
72
73const char*
74ShortMimeInfo::ShortDescription() const
75{
76	return fShortDescription.String();
77}
78
79int
80ShortMimeInfo::CompareShortDescription(const ShortMimeInfo* a,
81	const ShortMimeInfo* b)
82{
83	return a->fShortDescription.ICompare(b->fShortDescription);
84}
85
86bool
87ShortMimeInfo::IsCommonMimeType() const
88{
89	return fCommonMimeType;
90}
91
92
93//	#pragma mark -
94
95
96MimeTypeList::MimeTypeList()
97	:	fMimeList(100, true),
98		fCommonMimeList(30, false),
99		fLock("mimeListLock")
100{
101	fLock.Lock();
102	Thread::Launch(NewMemberFunctionObject(&MimeTypeList::Build, this),
103		B_NORMAL_PRIORITY);
104}
105
106static int
107MatchOneShortDescription(const ShortMimeInfo* a, const ShortMimeInfo* b)
108{
109	return strcasecmp(a->ShortDescription(), b->ShortDescription());
110}
111
112const ShortMimeInfo*
113MimeTypeList::FindMimeType(const char* shortDescription) const
114{
115	ShortMimeInfo tmp(shortDescription);
116	const ShortMimeInfo* result = fCommonMimeList.BinarySearch(tmp,
117		&MatchOneShortDescription);
118
119	return result;
120}
121
122const ShortMimeInfo*
123MimeTypeList::EachCommonType(bool (*func)(const ShortMimeInfo*, void*),
124	void* state) const
125{
126	AutoLock<Benaphore> locker(fLock);
127	int32 count = fCommonMimeList.CountItems();
128	for (int32 index = 0; index < count; index++) {
129		if ((func)(fCommonMimeList.ItemAt(index), state))
130			return fCommonMimeList.ItemAt(index);
131	}
132	return NULL;
133}
134
135void
136MimeTypeList::Build()
137{
138	ASSERT(fLock.IsLocked());
139
140	BMessage message;
141	BMimeType::GetInstalledTypes(&message);
142
143	int32 count;
144	uint32 type;
145	message.GetInfo("types", &type, &count);
146
147	for (int32 index = 0; index < count; index++) {
148		const char* str;
149		if (message.FindString("types", index, &str) != B_OK)
150			continue;
151
152		BMimeType mimetype(str);
153		if (mimetype.InitCheck() != B_OK)
154			continue;
155
156		ShortMimeInfo* mimeInfo = new ShortMimeInfo(mimetype);
157		fMimeList.AddItem(mimeInfo);
158		if (mimeInfo->IsCommonMimeType())
159			fCommonMimeList.AddItem(mimeInfo);
160	}
161	fCommonMimeList.SortItems(&ShortMimeInfo::CompareShortDescription);
162	fLock.Unlock();
163}
164