summaryrefslogtreecommitdiffstats
path: root/debian/opensync/opensync-0.22/osengine/osengine_flags.c
blob: 77c7f5df3892b6aad4eb81da4638f80480a1f232 (plain)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
 * libosengine - A synchronization engine for the opensync framework
 * Copyright (C) 2004-2005  Armin Bauer <armin.bauer@opensync.org>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 * 
 */
 
#include "engine.h"
#include "engine_internals.h"

OSyncFlag *osync_flag_new(OSyncFlag *parent)
{
	OSyncFlag *flag = g_malloc0(sizeof(OSyncFlag));
	flag->is_set = FALSE;
	if (parent) {
		flag->comb_flag = parent;
		parent->num_not_set++;
		osync_flag_calculate_comb(parent);
	}
	return flag;
}

void osync_flag_free(OSyncFlag *flag)
{
	g_free(flag);
}

OSyncFlag *osync_comb_flag_new(osync_bool any, osync_bool default_val)
{
	OSyncFlag *flag = osync_flag_new(NULL);
	flag->is_comb = TRUE;
	flag->is_any = any;
	flag->default_val = default_val;
	flag->is_set = default_val;
	return flag;
}

void osync_flag_attach(OSyncFlag *flag, OSyncFlag *target)
{
	if (flag->comb_flag)
		return;
	g_assert(target->is_comb);
	flag->comb_flag = target;
	if (flag->is_set) {
		target->num_set++;
	} else {
		target->num_not_set++;
	}
	osync_flag_calculate_comb(target);
}

osync_bool osync_flag_is_attached(OSyncFlag *flag)
{
	if (flag->comb_flag)
		return TRUE;
	return FALSE;
}

void osync_flag_detach(OSyncFlag *flag)
{
	OSyncFlag *target = flag->comb_flag;
	if (!target)
		return;
	if (flag->is_set) {
		target->num_set--;
	} else {
		target->num_not_set--;
	}
	flag->comb_flag = NULL;
	osync_flag_calculate_comb(target);
}

void osync_flag_set_pos_trigger(OSyncFlag *flag, OSyncFlagTriggerFunc func, void *data1, void *data2)
{
	flag->pos_trigger_func = func;
	flag->pos_user_data1 = data1;
	flag->pos_user_data2 = data2;
}

void osync_flag_set_neg_trigger(OSyncFlag *flag, OSyncFlagTriggerFunc func, void *data1, void *data2)
{
	flag->neg_trigger_func = func;
	flag->neg_user_data1 = data1;
	flag->neg_user_data2 = data2;
}

void osync_flag_calculate_comb(OSyncFlag *flag)
{
	if (!flag->is_comb)
		return;
	
	if (!flag->num_not_set && !flag->num_set) {
		if (flag->default_val)
			osync_flag_set(flag);
		else
			osync_flag_unset(flag);
		return;
	}
	
	if (!flag->is_any) {
		if (!flag->num_not_set && flag->num_set) {
			osync_flag_set(flag);
		} else {
			osync_flag_unset(flag);
		}
	} else {
		if (flag->num_set) {
			osync_flag_set(flag);
		} else {
			osync_flag_unset(flag);
		}
	}
}

osync_bool osync_flag_is_set(OSyncFlag *flag)
{
	if (flag->is_set == TRUE && flag->is_changing == FALSE)
		return TRUE;
	return FALSE;
}

osync_bool osync_flag_is_not_set(OSyncFlag *flag)
{
	if (flag->is_set == FALSE && flag->is_changing == FALSE)
		return TRUE;
	return FALSE;
}

void osync_comb_flag_update(OSyncFlag *combflag, OSyncFlag *flag, osync_bool prev_state)
{
	if (prev_state == flag->is_set)
		return;
	if (flag->is_set) {
		combflag->num_not_set--;
		combflag->num_set++;
	} else {
		combflag->num_not_set++;
		combflag->num_set--;
	}
}

void osync_flag_changing(OSyncFlag *flag)
{
	flag->is_changing = TRUE;
}

void osync_flag_cancel(OSyncFlag *flag)
{
	flag->is_changing = FALSE;
}

void osync_flag_unset(OSyncFlag *flag)
{
	osync_bool oldstate = flag->is_set;
	flag->is_set = FALSE;
	flag->is_changing = FALSE;
	osync_flag_calc_trigger(flag, oldstate);
	if (flag->comb_flag) {
		osync_comb_flag_update(flag->comb_flag, flag, oldstate);
		osync_flag_calculate_comb(flag->comb_flag);
	}
}

void osync_flag_set(OSyncFlag *flag)
{
	osync_bool oldstate = flag->is_set;
	flag->is_set = TRUE;
	flag->is_changing = FALSE;
	osync_flag_calc_trigger(flag, oldstate);
	if (flag->comb_flag) {
		osync_comb_flag_update(flag->comb_flag, flag, oldstate);
		osync_flag_calculate_comb(flag->comb_flag);
	}
}

void osync_flag_set_state(OSyncFlag *flag, osync_bool state)
{
	osync_bool oldstate = flag->is_set;
	flag->is_set = state;
	flag->is_changing = FALSE;
	if (flag->comb_flag) {
		osync_comb_flag_update(flag->comb_flag, flag, oldstate);
	}
	if (flag->is_comb) {
		//flag->num_not_set = 0;
		//flag->num_set = 0;
	}
}

osync_bool osync_flag_get_state(OSyncFlag *flag)
{
	return flag->is_set;
}

void osync_flag_calc_trigger(OSyncFlag *flag, osync_bool oldstate)
{
	if (flag->is_set != oldstate) {
		if (flag->is_set == TRUE) {
			if (flag->pos_trigger_func) {
				flag->pos_trigger_func(flag->pos_user_data1, flag->pos_user_data2);
			}
		} else {
			if (flag->neg_trigger_func) {
				flag->neg_trigger_func(flag->neg_user_data1, flag->neg_user_data2);
			}
		}
	}
}