Statistics
| Branch: | Tag: | Revision:

root / lib / filter-manager.c @ aebfd4cc

History | View | Annotate | Download (2.85 KB)

1
/* SylFilter - a message filter
2
 *
3
 * Copyright (C) 2011 Hiroyuki Yamamoto
4
 * Copyright (C) 2011 Sylpheed Development Team
5
 */
6

    
7
#include <glib.h>
8

    
9
#include "filter.h"
10
#include "filter-manager.h"
11
#include "filter-private.h"
12

    
13

    
14
XFilterManager *xfilter_manager_new(void)
15
{
16
        XFilterManager *mgr;
17

    
18
        mgr = g_new0(XFilterManager, 1);
19
        return mgr;
20
}
21

    
22
void xfilter_manager_free(XFilterManager *mgr)
23
{
24
        XFilter *list;
25
        XFilter *next;
26

    
27
        if (!mgr)
28
                return;
29

    
30
        for (list = mgr->filter_list; list != NULL; list = next) {
31
                next = list->next;
32
                xfilter_free(list);
33
        }
34
        g_free(mgr->original_encoding);
35

    
36
        g_free(mgr);
37
}
38

    
39
void xfilter_manager_filter_add(XFilterManager *mgr, XFilter *filter)
40
{
41
        XFilter *list;
42

    
43
        g_return_if_fail(mgr != NULL);
44
        g_return_if_fail(filter != NULL);
45

    
46
        list = mgr->filter_list;
47
        if (list) {
48
                while (list->next) {
49
                        list = list->next;
50
                }
51
                list->next = filter;
52
        } else {
53
                mgr->filter_list = filter;
54
        }
55

    
56
        filter->manager = mgr;
57
}
58

    
59
void xfilter_manager_filter_remove(XFilterManager *mgr, XFilter *filter)
60
{
61
        XFilter *list;
62
        XFilter *prev = NULL;
63

    
64
        g_return_if_fail(mgr != NULL);
65
        g_return_if_fail(filter != NULL);
66

    
67
        for (list = mgr->filter_list; list != NULL; list = list->next) {
68
                if (list == filter) {
69
                        if (prev)
70
                                prev->next = list->next;
71
                        list->next = NULL;
72
                        filter->manager = NULL;
73
                        break;
74
                }
75
                prev = list;
76
        }
77
}
78

    
79
int xfilter_manager_add_filters(XFilterManager *mgr, XFilterConstructorFunc ctors[])
80
{
81
        XFilter *filter;
82
        int i;
83

    
84
        for (i = 0; ctors[i] != NULL; i++) {
85
                filter = ctors[i]();
86
                if (!filter)
87
                        return -1;
88
                xfilter_manager_filter_add(mgr, filter);
89
        }
90

    
91
        return 0;
92
}
93

    
94
void xfilter_manager_done(XFilterManager *mgr)
95
{
96
        g_return_if_fail(mgr != NULL);
97

    
98
        g_free(mgr->original_encoding);
99
        mgr->original_encoding = NULL;
100
}
101

    
102
XFilterResult *xfilter_manager_run(XFilterManager *mgr, XMessageData *msgdata)
103
{
104
        XFilter *list;
105
        const XMessageData *curdata;
106
        XFilterResult *res;
107
        XFilterStatus status;
108

    
109
        g_return_val_if_fail(mgr != NULL, NULL);
110

    
111
        xfilter_debug_print("%s: %s: run filter chain\n", __FILE__, G_STRFUNC);
112

    
113
        res = xfilter_result_new();
114

    
115
        curdata = msgdata;
116

    
117
        for (list = mgr->filter_list; list != NULL; list = list->next) {
118
                xfilter_debug_print("exec filter: %s [%s] in data type: %s\n", xfilter_get_name(list), xfilter_get_type(list) == XF_CONTENT ? "content filter" : "test filter", xfilter_message_data_get_mime_type(curdata));
119

    
120
                status = xfilter_exec(list, curdata, res);
121
                if (res->msgdata)
122
                        curdata = res->msgdata;
123

    
124
                xfilter_debug_print("exec filter: %s: status %d: out data type: %s\n", xfilter_get_name(list), status, xfilter_message_data_get_mime_type(curdata));
125

    
126
                if (status == XF_JUNK) {
127
                        xfilter_debug_print("filter returned XF_JUNK, end filter chain\n");
128
                        break;
129
                }
130
                if (status == XF_UNSUPPORTED_TYPE || status == XF_ERROR) {
131
                        xfilter_debug_print("filter returned error, end filter chain\n");
132
                        break;
133
                }
134
        }
135

    
136
        xfilter_manager_done(mgr);
137

    
138
        return res;
139
}