~ubuntu-branches/ubuntu/karmic/libtinymail/karmic

« back to all changes in this revision

Viewing changes to libtinymail-camel/camel-lite/camel/camel-exception.c

  • Committer: Bazaar Package Importer
  • Author(s): Steve Kowalik
  • Date: 2007-10-12 11:21:12 UTC
  • Revision ID: james.westby@ubuntu.com-20071012112112-fod9fs7yrooxjr7i
Tags: upstream-0.0.2
ImportĀ upstreamĀ versionĀ 0.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
 
2
/* 
 
3
 *
 
4
 * Author : 
 
5
 *  Bertrand Guiheneuf <bertrand@helixcode.com>
 
6
 *
 
7
 * Copyright 1999-2003 Ximian, Inc. (www.ximian.com)
 
8
 *
 
9
 * This program is free software; you can redistribute it and/or 
 
10
 * modify it under the terms of version 2 of the GNU Lesser General Public 
 
11
 * License as published by the Free Software Foundation.
 
12
 *
 
13
 * This program is distributed in the hope that it will be useful,
 
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
 * GNU Lesser General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU Lesser General Public License
 
19
 * along with this program; if not, write to the Free Software
 
20
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 
21
 * USA
 
22
 */
 
23
 
 
24
#ifdef HAVE_CONFIG_H
 
25
#include <config.h>
 
26
#endif
 
27
 
 
28
#include <stdio.h>
 
29
#include <pthread.h>
 
30
 
 
31
#include <glib.h>
 
32
#include <glib/gi18n-lib.h>
 
33
 
 
34
#include <libedataserver/e-memory.h>
 
35
 
 
36
#include "camel-debug.h"
 
37
#include "camel-exception.h"
 
38
 
 
39
/* dont turn this off */
 
40
#define w(x) x
 
41
 
 
42
 
 
43
/**
 
44
 * camel_exception_new: allocate a new exception object. 
 
45
 * 
 
46
 * Create and returns a new exception object.
 
47
 * 
 
48
 * Returns the newly allocated exception object
 
49
 **/
 
50
CamelException *
 
51
camel_exception_new (void)
 
52
{
 
53
        CamelException *ex;
 
54
 
 
55
        ex = g_slice_new (CamelException);
 
56
        ex->desc = NULL;
 
57
 
 
58
        /* set the Exception Id to NULL */
 
59
        ex->id = CAMEL_EXCEPTION_NONE;
 
60
 
 
61
        return ex;
 
62
}
 
63
 
 
64
/**
 
65
 * camel_exception_init:
 
66
 * @ex: a #CamelException
 
67
 * 
 
68
 * Init an exception. This routine is mainly useful when using a
 
69
 * statically allocated exception.
 
70
 **/
 
71
void
 
72
camel_exception_init (CamelException *ex)
 
73
{
 
74
        ex->desc = NULL;
 
75
 
 
76
        /* set the Exception Id to NULL */
 
77
        ex->id = CAMEL_EXCEPTION_NONE;
 
78
}
 
79
 
 
80
 
 
81
/**
 
82
 * camel_exception_clear:
 
83
 * @ex: a #CamelException
 
84
 * 
 
85
 * Clear an exception, that is, set the exception ID to
 
86
 * #CAMEL_EXCEPTION_NONE and free the description text.  If the
 
87
 * exception is %NULL, this funtion just returns.
 
88
 **/
 
89
void 
 
90
camel_exception_clear (CamelException *exception)
 
91
{
 
92
        if (!exception)
 
93
                return;
 
94
 
 
95
        if (exception->desc)
 
96
                g_free (exception->desc);
 
97
        exception->desc = NULL;
 
98
        exception->id = CAMEL_EXCEPTION_NONE;
 
99
}
 
100
 
 
101
/**
 
102
 * camel_exception_free:
 
103
 * @ex: a #CamelException
 
104
 * 
 
105
 * Free an exception object. If the exception is %NULL, nothing is
 
106
 * done, the routine simply returns.
 
107
 **/
 
108
void 
 
109
camel_exception_free (CamelException *exception)
 
110
{
 
111
        if (!exception)
 
112
                return;
 
113
        
 
114
        if (exception->desc)
 
115
                g_free (exception->desc);
 
116
 
 
117
        g_slice_free (CamelException, exception);
 
118
}
 
119
 
 
120
/**
 
121
 * camel_exception_set: set an exception 
 
122
 * @ex: a #CamelException
 
123
 * @id: exception id 
 
124
 * @desc: textual description of the exception
 
125
 * 
 
126
 * Set the value of an exception. The exception id is 
 
127
 * a unique number representing the exception. The 
 
128
 * textual description is a small text explaining 
 
129
 * what happened and provoked the exception.
 
130
 *
 
131
 * When @ex is %NULL, nothing is done, this routine
 
132
 * simply returns.
 
133
 **/
 
134
void
 
135
camel_exception_set (CamelException *ex, ExceptionId id, const char *desc)
 
136
{
 
137
        if (camel_debug("exception"))
 
138
                printf("CamelException.set(%p, %u, '%s')\n", ex, id, desc);
 
139
        if (!ex)
 
140
                return;
 
141
        ex->id = id;
 
142
        if (desc != ex->desc) {
 
143
                g_free (ex->desc);
 
144
                ex->desc = g_strdup (desc);
 
145
        }
 
146
}
 
147
 
 
148
/**
 
149
 * camel_exception_setv: set an exception 
 
150
 * @ex: a #CamelException
 
151
 * @id: exception id 
 
152
 * @format: format of the description string. The format string is
 
153
 * used as in printf().
 
154
 * 
 
155
 * Set the value of an exception. The exception id is 
 
156
 * a unique number representing the exception. The 
 
157
 * textual description is a small text explaining 
 
158
 * what happened and provoked the exception. 
 
159
 * In this version, the string is created from the format 
 
160
 * string and the variable argument list.
 
161
 *
 
162
 * It is safe to say:
 
163
 *   camel_exception_setv (ex, ..., camel_exception_get_description (ex), ...);
 
164
 *
 
165
 * When @ex is %NULL, nothing is done, this routine
 
166
 * simply returns.
 
167
 **/
 
168
void
 
169
camel_exception_setv (CamelException *ex, ExceptionId id, const char *format, ...)
 
170
{
 
171
        va_list args;
 
172
        char *desc;
 
173
 
 
174
        va_start(args, format);
 
175
        desc = g_strdup_vprintf (format, args);
 
176
        va_end (args);
 
177
 
 
178
        if (camel_debug("exception"))
 
179
                printf("CamelException.setv(%p, %u, '%s')\n", ex, id, desc);
 
180
        
 
181
        if (!ex) {
 
182
                g_free(desc);
 
183
                return;
 
184
        }
 
185
 
 
186
        g_free(ex->desc);
 
187
        ex->desc = desc;
 
188
        ex->id = id;
 
189
}
 
190
 
 
191
/**
 
192
 * camel_exception_xfer:
 
193
 * @ex_dst: Destination exception object 
 
194
 * @ex_src: Source exception object
 
195
 * 
 
196
 * Transfer the content of an exception from an exception object to
 
197
 * another.  The destination exception receives the id and the
 
198
 * description text of the source exception.
 
199
 **/
 
200
void 
 
201
camel_exception_xfer (CamelException *ex_dst,
 
202
                      CamelException *ex_src)
 
203
{
 
204
        if (ex_src == NULL) {
 
205
                w(g_warning ("camel_exception_xfer: trying to transfer NULL exception to %p\n", ex_dst));
 
206
                return;
 
207
        }
 
208
 
 
209
        if (ex_dst == NULL) {
 
210
                /* must have same side-effects */
 
211
                camel_exception_clear (ex_src);
 
212
                return;
 
213
        }
 
214
 
 
215
        if (ex_dst->desc)
 
216
                g_free (ex_dst->desc);
 
217
 
 
218
        ex_dst->id = ex_src->id;
 
219
        ex_dst->desc = ex_src->desc;
 
220
 
 
221
        ex_src->desc = NULL;
 
222
        ex_src->id = CAMEL_EXCEPTION_NONE;
 
223
}
 
224
 
 
225
/**
 
226
 * camel_exception_get_id:
 
227
 * @ex: a #CamelException
 
228
 * 
 
229
 * Get the id of an exception.
 
230
 * 
 
231
 * Returns the exception id (#CAMEL_EXCEPTION_NONE will be returned if
 
232
 * @ex is %NULL or unset)
 
233
 **/
 
234
ExceptionId
 
235
camel_exception_get_id (CamelException *ex)
 
236
{
 
237
        if (ex)
 
238
                return ex->id;
 
239
        
 
240
        w(g_warning ("camel_exception_get_id called with NULL parameter."));
 
241
        
 
242
        return CAMEL_EXCEPTION_NONE;
 
243
}
 
244
 
 
245
/**
 
246
 * camel_exception_get_description:
 
247
 * @ex: a #CamelException
 
248
 * 
 
249
 * Get the exception description text.
 
250
 * 
 
251
 * Returns the exception description text (%NULL will be returned if
 
252
 * @ex is %NULL or unset)
 
253
 **/
 
254
const gchar *
 
255
camel_exception_get_description (CamelException *ex)
 
256
{
 
257
        char *ret = NULL;
 
258
 
 
259
        if (ex)
 
260
                ret = ex->desc;
 
261
        else
 
262
                w(g_warning ("camel_exception_get_description called with NULL parameter."));
 
263
 
 
264
        return ret ? ret : (_("No description available"));
 
265
}