File: filter-file.c

package info (click to toggle)
balsa 2.6.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 23,576 kB
  • sloc: ansic: 99,871; xml: 4,934; makefile: 769; sh: 185; awk: 60; python: 34
file content (191 lines) | stat: -rw-r--r-- 5,908 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* -*-mode:c; c-style:k&r; c-basic-offset:4; -*- */
/* Balsa E-Mail Client
 *
 * Copyright (C) 1997-2016 Stuart Parmenter and others,
 *                         See the file AUTHORS for a list.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option) 
 * any later version.
 *  
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  
 * GNU General Public License for more details.
 *  
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
 */
/*

 * filter-file.c
 *
 * File functions of the mail filter portion of balsa.
 *
 */

#if defined(HAVE_CONFIG_H) && HAVE_CONFIG_H
# include "config.h"
#endif                          /* HAVE_CONFIG_H */
#include "filter-file.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>

#include "filter-private.h"
#include "filter-funcs.h"
#include "libbalsa-conf.h"
#include "mailbox-filter.h"
#include <glib/gi18n.h>

/* Load the header of a filter filter (you have to separately load the
 * associated conditions) Filter is marked as invalid Position
 * filter_errno.
 */

LibBalsaFilter*
libbalsa_filter_new_from_config(void)
{
    LibBalsaFilter * newf = libbalsa_filter_new();
    gchar *str, *p;
    /* First we load the fixed part of the filter */
    newf->name          = libbalsa_conf_get_string("Name");
    p = str             = libbalsa_conf_get_string("Condition");
    newf->sound         = libbalsa_conf_get_string("Sound");
    newf->popup_text    = libbalsa_conf_get_string("Popup-text");
    newf->action        = libbalsa_conf_get_int("Action-type");
    newf->action_string = libbalsa_conf_get_string("Action-string");
    newf->condition     = libbalsa_condition_new_from_string(&p);
    g_free(str);
    if (newf->sound[0]=='\0') {
	g_free(newf->sound);
	newf->sound=NULL;
    }
    if (newf->popup_text[0]=='\0') {
	g_free(newf->popup_text);
	newf->popup_text=NULL;
    }

    return newf;
}

/*
 * libbalsa_conditions_save_config saves a list of conditions using
 * prefix and filter_section_name to create the name of the section We
 * clean all preceding saved conditions to keep the config file clean
 * and coherent. */

/*
 * void libbalsa_filter_save_config(filter * f)
 *
 * Saves the filter into the config file.
 *
 * Arguments:
 *    filter * filter - the filter to save
 *
 */

void
libbalsa_filter_save_config(LibBalsaFilter * fil)
{
    gchar *str = libbalsa_condition_to_string(fil->condition);
    libbalsa_conf_set_string("Name",          fil->name);
    libbalsa_conf_set_string("Condition",     str);
    libbalsa_conf_set_string("Sound",         fil->sound);
    libbalsa_conf_set_string("Popup-text",    fil->popup_text);
    libbalsa_conf_set_int("Action-type",      fil->action);
    libbalsa_conf_set_string("Action-string", fil->action_string);
    g_free(str);
}

/* Loads the filters associated to the mailbox
 * Note : does not clean current filters list (normally the caller did it)
 * Position filter_errno
 */

void
libbalsa_mailbox_filters_load_config(LibBalsaMailbox* mbox)
{
    gint i, nb_filters;
    gchar **filters_names = NULL;
    gboolean def;
    GSList *lst;
    GSList *filters;

    filters = libbalsa_mailbox_get_filters(mbox);

    /* We load the associated filters */
    libbalsa_conf_get_vector_with_default(MAILBOX_FILTERS_KEY,
                                          &nb_filters, &filters_names, &def);
    if (!def) {
	for(i = 0; i < nb_filters; i++) {
            LibBalsaFilter *fil;

	    fil = libbalsa_filter_get_by_name(filters_names[i]);
	    if (fil != NULL) {
		LibBalsaMailboxFilter *mf = g_new(LibBalsaMailboxFilter, 1);

		mf->actual_filter = fil;
                mf->when = FILTER_WHEN_NEVER; /* 0 */
		filters = g_slist_prepend(filters, mf);
	    }
	    else
		libbalsa_information(LIBBALSA_INFORMATION_WARNING,
				     _("Invalid filters %s for mailbox %s"),
                                     filters_names[i], libbalsa_mailbox_get_name(mbox));
	}
	filters = g_slist_reverse(filters);
    }
    g_strfreev(filters_names);

    if (!def) {
	libbalsa_conf_get_vector_with_default(MAILBOX_FILTERS_WHEN_KEY,
                                              &nb_filters, &filters_names, &def);
	if (def) {
	    for(lst = filters; lst != NULL; lst = lst->next)
		FILTER_WHEN_SETFLAG((LibBalsaMailboxFilter*)lst->data,
				    FILTER_WHEN_NEVER);
        } else {
	    lst = filters;
	    for (i = 0; i < nb_filters && lst != NULL; i++) {
		LibBalsaMailboxFilter *mf = lst->data;

                mf->when = atoi(filters_names[i]);
		lst = lst->next;
	    }
	}
	g_strfreev(filters_names);
	libbalsa_mailbox_set_filters(mbox, filters);
    }
    filter_errno=FILTER_NOERR;
}

/* Saves the filters associated to the mailbox
 */

void
libbalsa_mailbox_filters_save_config(LibBalsaMailbox * mbox)
{
    GSList *filters = libbalsa_mailbox_get_filters(mbox);
    GPtrArray *names;
    GSList *fil;

    names = g_ptr_array_new();

    for (fil = filters; fil != NULL; fil = fil->next)
        g_ptr_array_add(names, ((LibBalsaMailboxFilter*)fil->data)->actual_filter->name);
    libbalsa_conf_set_vector(MAILBOX_FILTERS_KEY, names->len, (const char **) names->pdata);

    g_ptr_array_set_size(names, 0);
    g_ptr_array_set_free_func(names, g_free);

    for (fil = filters; fil != NULL; fil = fil->next)
        g_ptr_array_add(names, g_strdup_printf("%d", ((LibBalsaMailboxFilter*)fil->data)->when));
    libbalsa_conf_set_vector(MAILBOX_FILTERS_WHEN_KEY, names->len, (const char **) names->pdata);

    g_ptr_array_free(names, TRUE);
}