1/*
2 * Copyright 2011, Haiku, Inc. All rights reserved.
3 * Copyright 2011, Clemens Zeidler <haiku@clemens-zeidler.de>
4 * Distributed under the terms of the MIT License.
5 */
6
7
8#include "FilterAddonList.h"
9
10#include <Directory.h>
11#include <FindDirectory.h>
12#include <Path.h>
13
14
15FilterAddonList::FilterAddonList(direction dir, bool loadOnStart)
16	:
17	fDirection(dir)
18{
19	if (loadOnStart)
20		Reload();
21}
22
23
24FilterAddonList::~FilterAddonList()
25{
26	_MakeEmpty();
27}
28
29
30void
31FilterAddonList::Reload()
32{
33	_MakeEmpty();
34
35	BPath path;
36	status_t status = find_directory(B_SYSTEM_ADDONS_DIRECTORY, &path);
37	if (status != B_OK)
38		return;
39	path.Append("mail_daemon");
40	if (fDirection == kIncomming)
41		path.Append("inbound_filters");
42	else
43		path.Append("outbound_filters");
44
45	BDirectory dir(path.Path());
46	if (dir.InitCheck() != B_OK)
47		return;
48	BEntry entry;
49	while (dir.GetNextEntry(&entry) != B_ENTRY_NOT_FOUND)
50		_LoadAddon(entry);
51}
52
53
54int32
55FilterAddonList::CountFilterAddons()
56{
57	return fFilterAddonList.size();
58}
59
60
61FilterAddonInfo&
62FilterAddonList::FilterAddonAt(int32 index)
63{
64	return fFilterAddonList[index];
65}
66
67
68bool
69FilterAddonList::GetDescriptiveName(int32 index, BString& name)
70{
71	if (index < 0)
72		return false;
73
74	FilterAddonInfo& info = FilterAddonAt(index);
75
76	BString (*descriptive_name)();
77	if (get_image_symbol(info.image, "descriptive_name", B_SYMBOL_TYPE_TEXT,
78		(void **)&descriptive_name) == B_OK) {
79		name = (*descriptive_name)();
80	} else
81		name = info.ref.name;
82	return true;
83}
84
85
86bool
87FilterAddonList::GetDescriptiveName(const entry_ref& ref, BString& name)
88{
89	int32 index = FindInfo(ref);
90	return GetDescriptiveName(index, name);
91}
92
93
94BView*
95FilterAddonList::CreateConfigView(AddonSettings& settings)
96{
97	const entry_ref& ref = settings.AddonRef();
98	int32 index = FindInfo(ref);
99	if (index < 0)
100		return NULL;
101	FilterAddonInfo& info = FilterAddonAt(index);
102
103	BView* (*instantiate_filter_config_panel)(AddonSettings&);
104	if (get_image_symbol(info.image, "instantiate_filter_config_panel",
105		B_SYMBOL_TYPE_TEXT, (void **)&instantiate_filter_config_panel) != B_OK)
106		return NULL;
107	return (*instantiate_filter_config_panel)(settings);
108}
109
110
111void
112FilterAddonList::_MakeEmpty()
113{
114	for (unsigned int i = 0; i < fFilterAddonList.size(); i++) {
115		FilterAddonInfo& info = fFilterAddonList[i];
116		unload_add_on(info.image);
117	}
118	fFilterAddonList.clear();
119}
120
121
122int32
123FilterAddonList::FindInfo(const entry_ref& ref)
124{
125	for (unsigned int i = 0; i < fFilterAddonList.size(); i++) {
126		FilterAddonInfo& currentInfo = fFilterAddonList[i];
127		if (currentInfo.ref == ref)
128			return i;
129	}
130	return -1;
131}
132
133
134void
135FilterAddonList::_LoadAddon(BEntry& entry)
136{
137	FilterAddonInfo info;
138
139	BPath path(&entry);
140	info.image = load_add_on(path.Path());
141	if (info.image < 0)
142		return;
143
144	BView* (*instantiate_filter_config_panel)(MailAddonSettings&);
145	if (get_image_symbol(info.image, "instantiate_filter_config_panel",
146		B_SYMBOL_TYPE_TEXT, (void **)&instantiate_filter_config_panel)
147			!= B_OK) {
148		unload_add_on(info.image);
149		return;
150	}
151
152	entry.GetRef(&info.ref);
153
154	fFilterAddonList.push_back(info);
155}
156