File: tny-status.c

package info (click to toggle)
libtinymail 0.0.9-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 18,148 kB
  • ctags: 19,175
  • sloc: ansic: 151,565; xml: 20,145; sh: 9,245; makefile: 2,394; cs: 243; cpp: 141; python: 93; perl: 71
file content (342 lines) | stat: -rw-r--r-- 8,314 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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* libtinymail - The Tiny Mail base library
 * Copyright (C) 2006-2007 Philip Van Hoof <pvanhoof@gnome.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 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 self library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include <glib.h>

#include <tny-status.h>

/**
 * TnyStatus:
 *
 * A progress status
 *
 * free-function: tny_status_free
 **/

/**
 * tny_status_set_fraction:
 * @status: a #TnyStatus
 * @fraction: the fraction to set
 * 
 * Set the fraction of @status. The of_total member of @status will be set to 100
 * by this method.
 *
 * since: 1.0
 * audience: application-developer
 **/
void 
tny_status_set_fraction (TnyStatus *status, gdouble fraction)
{
	status->of_total = 100;
	status->position = fraction * 100;
}

/**
 * tny_status_get_fraction:
 * @status: a #TnyStatus
 * 
 * Get the fraction of @status
 *
 * returns: the fraction of @status
 * since: 1.0
 * audience: application-developer
 **/
gdouble 
tny_status_get_fraction (TnyStatus *status)
{
	if (status->of_total == 0)
		return 1;

	return (gdouble) ( ((gdouble) status->position) / ((gdouble)status->of_total) );
}


static TnyStatus* 
tny_status_new_valist (GQuark domain, int code, guint position, guint of_total, const gchar *format, va_list args)
{
	TnyStatus *status;

	status = g_slice_new (TnyStatus);

	status->position = position;
	status->of_total = of_total;
	status->domain = domain;
	status->code = code;
	status->message = g_strdup_vprintf (format, args);

	return status;
}

/**
 * tny_status_new:
 * @domain: status domain 
 * @code: status code
 * @position: the position
 * @of_total: the total amount of events to happen
 * @format: printf()-style format for status message
 * @Varargs: parameters for message format
 * 
 * Creates a new #TnyStatus with the given @domain and @code,
 * and a message formatted with @format.
 * 
 * returns: (caller-owns): a new #TnyStatus
 **/
TnyStatus* 
tny_status_new (GQuark domain, gint code, guint position, guint of_total, const gchar *format, ...)
{
	TnyStatus* status;
	va_list args;

	g_return_val_if_fail (format != NULL, NULL);
	g_return_val_if_fail (domain != 0, NULL);

	va_start (args, format);
	status = tny_status_new_valist (domain, code, position, of_total, format, args);
	va_end (args);

	return status;
}

/**
 * tny_status_new_literal:
 * @domain: status domain
 * @code: status code
 * @position: the position
 * @of_total: the total amount of events to happen
 * @message: status message
 * 
 * Creates a new #TnyStatus; unlike tny_status_new(), @message is not
 * a printf()-style format string. Use this 
 * function if @message contains text you don't have control over, 
 * that could include printf() escape sequences.
 * 
 * returns: (caller-owns): a new #TnyStatus
 **/
TnyStatus* 
tny_status_new_literal (GQuark domain, gint code, guint position, guint of_total, const gchar *message)
{
	TnyStatus* status;

	g_return_val_if_fail (message != NULL, NULL);
	g_return_val_if_fail (domain != 0, NULL);

	status = g_slice_new (TnyStatus);

	status->position = position;
	status->of_total = of_total;
	status->domain = domain;
	status->code = code;
	status->message = g_strdup (message);

	return status;
}

/**
 * tny_status_free:
 * @status: a #TnyStatus
 *
 * Destroys a #TnyStatus and associated resources.
 *
 * since: 1.0
 * audience: application-developer
 **/
void 
tny_status_free (TnyStatus *status)
{
	g_return_if_fail (status != NULL);  

	g_free (status->message);

	g_slice_free (TnyStatus, status);
}

/**
 * tny_status_copy:
 * @status: a #TnyStatus
 * 
 * Makes a full copy of @status (not just a shallow copy).
 * 
 * returns: (caller-owns): a new #TnyStatus
 * since: 1.0
 * audience: application-developer
 **/
TnyStatus* 
tny_status_copy (const TnyStatus *status)
{
	TnyStatus *copy;

	g_return_val_if_fail (status != NULL, NULL);

	copy = g_slice_new (TnyStatus);

	*copy = *status;

	copy->message = g_strdup (status->message);

	return copy;
}

/**
 * tny_status_matches:
 * @status: a #TnyStatus
 * @domain: a status domain
 * @code: a status code
 * 
 * Returns %TRUE if @status matches @domain and @code, %FALSE
 * otherwise.
 * 
 * returns: TRUE if @status has @domain and @code, else FALSE
 * since: 1.0
 * audience: application-developer
 **/
gboolean 
tny_status_matches (const TnyStatus *status, GQuark domain, gint code)
{
	return status &&
		status->domain == domain &&
		status->code == code;
}

/**
 * tny_set_status:
 * @status: a return location for a #TnyStatus, or %NULL
 * @domain: status domain
 * @code: status code 
 * @position: the position
 * @of_total: the total amount of events to happen
 * @format: printf()-style format
 * @Varargs: args for @format 
 * 
 * Does nothing if @status is %NULL; if @status is non-%NULL, then *@status must
 * be %NULL. A new #TnyStatus is created and assigned to *@status.
 *
 * since: 1.0
 * audience: application-developer
 **/
void 
tny_set_status (TnyStatus **status, GQuark domain, gint code, guint position, guint of_total, gchar *format, ...)
{
	TnyStatus *new;

	va_list args;

	if (status == NULL)
		return;

	va_start (args, format);
	new = tny_status_new_valist (domain, code, position, of_total, format, args);
	va_end (args);

	if (*status == NULL)
		*status = new;
}


/**
 * tny_clear_status:
 * @status: a #TnyStatus return location
 * 
 * If @status is %NULL, does nothing. If @status is non-%NULL,
 * calls tny_status_free() on *@status and sets *@status to %NULL.
 *
 * since: 1.0
 * audience: application-developer
 **/
void
tny_clear_status (TnyStatus **status)
{
	if (status && *status)
	{
		tny_status_free (*status);
		*status = NULL;
	}
}

/**
 * tny_status_get_message:
 * @status: a #TnyStatus 
 * 
 * returns: (null-ok): the message of @status (as a const gchar*)
 **/

/**
 * tny_status_get_domain:
 * @status: a #TnyStatus 
 * 
 * returns: the domain of @status (as a TnyStatusDomain)
 **/

/**
 * tny_status_get_code:
 * @status: a #TnyStatus 
 * 
 * returns: the code of @status (as a TnyStatusCode)
 **/


/**
 * tny_status_domain_get_type:
 *
 * GType system helper function
 *
 * returns: a #GType
 **/
GType
tny_status_domain_get_type (void)
{
  static GType etype = 0;
  if (etype == 0) {
    static const GEnumValue values[] = {
      { TNY_FOLDER_STATUS, "TNY_FOLDER_STATUS", "folder_status" },
      { TNY_GET_MSG_QUEUE_STATUS, "TNY_GET_MSG_QUEUE_STATUS", "get_msg_queue_status" },
      { TNY_GET_SUPPORTED_SECURE_AUTH_STATUS, "TNY_GET_SUPPORTED_SECURE_AUTH_STATUS", "secured-auth-status" },
      { 0, NULL, NULL }
    };
    etype = g_enum_register_static ("TnyStatusDomain", values);
  }
  return etype;
}


/**
 * tny_status_code_get_type:
 *
 * GType system helper function
 *
 * returns: a #GType
 **/
GType
tny_status_code_get_type (void)
{
  static GType etype = 0;
  if (etype == 0) {
    static const GEnumValue values[] = {
      { TNY_FOLDER_STATUS_CODE_REFRESH, "TNY_FOLDER_STATUS_CODE_REFRESH", "folder_status_code_refresh" },
      { TNY_FOLDER_STATUS_CODE_GET_MSG, "TNY_FOLDER_STATUS_CODE_GET_MSG", "folder_status_code_get_msg" },
      { TNY_GET_MSG_QUEUE_STATUS_GET_MSG, "TNY_GET_MSG_QUEUE_STATUS_GET_MSG", "get_msg_queue_status_get_msg" },
      { TNY_FOLDER_STATUS_CODE_XFER_MSGS, "TNY_FOLDER_STATUS_CODE_XFER_MSGS", "xfer-msgs" },
      { TNY_FOLDER_STATUS_CODE_COPY_FOLDER, "TNY_FOLDER_STATUS_CODE_COPY_FOLDER", "copy-folder" },
      { TNY_GET_SUPPORTED_SECURE_AUTH_STATUS_GET_SECURE_AUTH, "TNY_GET_SUPPORTED_SECURE_AUTH_STATUS_GET_SECURE_AUTH", "get-secure-auth" },
      { TNY_FOLDER_STATUS_CODE_SYNC, "TNY_FOLDER_STATUS_CODE_SYNC", "code-sync" },
      { 0, NULL, NULL }
    };
    etype = g_enum_register_static ("TnyStatusCode", values);
  }
  return etype;
}