more section docs
[platform/upstream/gstreamer.git] / gst / gsterror.c
1 /* GStreamer
2  * Copyright (C) <2003> David A. Schleef <ds@schleef.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /**
21  * SECTION:gsterror
22  * @short_description: Categorized error messages
23  *
24  * This module manages localizable error messages. Developers can use
25  * gst_error_get_message() to get a localized message from a error-code and
26  * -domain.
27  */
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include "gst_private.h"
34 #include <gst/gst.h>
35 #include "gst-i18n-lib.h"
36
37 #define TABLE(t, d, a, b) t[GST_ ## d ## _ERROR_ ## a] = g_strdup (b)
38 #define QUARK_FUNC(string)                                              \
39 GQuark gst_ ## string ## _error_quark (void) {                          \
40   static GQuark quark;                                                  \
41   if (!quark)                                                           \
42     quark = g_quark_from_static_string ("gst-" # string "-error-quark"); \
43   return quark; }
44
45 GType
46 gst_g_error_get_type (void)
47 {
48   static GType type = 0;
49
50   if (!type)
51     type = g_boxed_type_register_static ("GstGError",
52         (GBoxedCopyFunc) g_error_copy, (GBoxedFreeFunc) g_error_free);
53   return type;
54 }
55
56 #define FILE_A_BUG "  Please file a bug at "\
57     "http://bugzilla.gnome.org/enter_bug.cgi?product=GStreamer."
58
59 /* initialize the dynamic table of translated core errors */
60 static gchar **
61 _gst_core_errors_init (void)
62 {
63   gchar **t = NULL;
64
65   t = g_new0 (gchar *, GST_CORE_ERROR_NUM_ERRORS);
66
67   TABLE (t, CORE, FAILED,
68       N_("GStreamer encountered a general core library error."));
69   TABLE (t, CORE, TOO_LAZY,
70       N_("GStreamer developers were too lazy to assign an error code "
71           "to this error." FILE_A_BUG));
72   TABLE (t, CORE, NOT_IMPLEMENTED,
73       N_("Internal GStreamer error: code not implemented." FILE_A_BUG));
74   TABLE (t, CORE, STATE_CHANGE,
75       N_("Internal GStreamer error: state change failed." FILE_A_BUG));
76   TABLE (t, CORE, PAD, N_("Internal GStreamer error: pad problem." FILE_A_BUG));
77   TABLE (t, CORE, THREAD,
78       N_("Internal GStreamer error: thread problem." FILE_A_BUG));
79   TABLE (t, CORE, NEGOTIATION,
80       N_("Internal GStreamer error: negotiation problem." FILE_A_BUG));
81   TABLE (t, CORE, EVENT,
82       N_("Internal GStreamer error: event problem." FILE_A_BUG));
83   TABLE (t, CORE, SEEK,
84       N_("Internal GStreamer error: seek problem." FILE_A_BUG));
85   TABLE (t, CORE, CAPS,
86       N_("Internal GStreamer error: caps problem." FILE_A_BUG));
87   TABLE (t, CORE, TAG, N_("Internal GStreamer error: tag problem." FILE_A_BUG));
88
89   return t;
90 }
91
92 /* initialize the dynamic table of translated library errors */
93 static gchar **
94 _gst_library_errors_init (void)
95 {
96   gchar **t = NULL;
97
98   t = g_new0 (gchar *, GST_LIBRARY_ERROR_NUM_ERRORS);
99
100   TABLE (t, LIBRARY, FAILED,
101       N_("GStreamer encountered a general supporting library error."));
102   TABLE (t, LIBRARY, TOO_LAZY,
103       N_("GStreamer developers were too lazy to assign an error code "
104           "to this error." FILE_A_BUG));
105   TABLE (t, LIBRARY, INIT, N_("Could not initialize supporting library."));
106   TABLE (t, LIBRARY, SHUTDOWN, N_("Could not close supporting library."));
107   TABLE (t, LIBRARY, SETTINGS, N_("Could not close supporting library."));
108
109   return t;
110 }
111
112 /* initialize the dynamic table of translated resource errors */
113 static gchar **
114 _gst_resource_errors_init (void)
115 {
116   gchar **t = NULL;
117
118   t = g_new0 (gchar *, GST_RESOURCE_ERROR_NUM_ERRORS);
119
120   TABLE (t, RESOURCE, FAILED,
121       N_("GStreamer encountered a general resource error."));
122   TABLE (t, RESOURCE, TOO_LAZY,
123       N_("GStreamer developers were too lazy to assign an error code "
124           "to this error." FILE_A_BUG));
125   TABLE (t, RESOURCE, NOT_FOUND, N_("Resource not found."));
126   TABLE (t, RESOURCE, BUSY, N_("Resource busy or not available."));
127   TABLE (t, RESOURCE, OPEN_READ, N_("Could not open resource for reading."));
128   TABLE (t, RESOURCE, OPEN_WRITE, N_("Could not open resource for writing."));
129   TABLE (t, RESOURCE, OPEN_READ_WRITE,
130       N_("Could not open resource for reading and writing."));
131   TABLE (t, RESOURCE, CLOSE, N_("Could not close resource."));
132   TABLE (t, RESOURCE, READ, N_("Could not read from resource."));
133   TABLE (t, RESOURCE, WRITE, N_("Could not write to resource."));
134   TABLE (t, RESOURCE, SEEK, N_("Could not perform seek on resource."));
135   TABLE (t, RESOURCE, SYNC, N_("Could not synchronize on resource."));
136   TABLE (t, RESOURCE, SETTINGS,
137       N_("Could not get/set settings from/on resource."));
138
139   return t;
140 }
141
142 /* initialize the dynamic table of translated stream errors */
143 static gchar **
144 _gst_stream_errors_init (void)
145 {
146   gchar **t = NULL;
147
148   t = g_new0 (gchar *, GST_STREAM_ERROR_NUM_ERRORS);
149
150   TABLE (t, STREAM, FAILED,
151       N_("GStreamer encountered a general stream error."));
152   TABLE (t, STREAM, TOO_LAZY,
153       N_("GStreamer developers were too lazy to assign an error code "
154           "to this error." FILE_A_BUG));
155   TABLE (t, STREAM, NOT_IMPLEMENTED,
156       N_("Element doesn't implement handling of this stream. "
157           "Please file a bug."));
158   TABLE (t, STREAM, TYPE_NOT_FOUND, N_("Could not determine type of stream."));
159   TABLE (t, STREAM, WRONG_TYPE,
160       N_("The stream is of a different type than handled by this element."));
161   TABLE (t, STREAM, CODEC_NOT_FOUND,
162       N_("There is no codec present that can handle the stream's type."));
163   TABLE (t, STREAM, DECODE, N_("Could not decode stream."));
164   TABLE (t, STREAM, ENCODE, N_("Could not encode stream."));
165   TABLE (t, STREAM, DEMUX, N_("Could not demultiplex stream."));
166   TABLE (t, STREAM, MUX, N_("Could not multiplex stream."));
167
168   return t;
169 }
170
171 QUARK_FUNC (core);
172 QUARK_FUNC (library);
173 QUARK_FUNC (resource);
174 QUARK_FUNC (stream);
175
176 /**
177  * gst_error_get_message:
178  * @domain: the GStreamer error domain this error belongs to.
179  * @code: the error code belonging to the domain.
180  *
181  * Get a string describing the error message in the current locale.
182  *
183  * Returns: a newly allocated string describing the error message in the
184  * current locale.
185  */
186 gchar *
187 gst_error_get_message (GQuark domain, gint code)
188 {
189   static gchar **gst_core_errors = NULL;
190   static gchar **gst_library_errors = NULL;
191   static gchar **gst_resource_errors = NULL;
192   static gchar **gst_stream_errors = NULL;
193
194   gchar *message = NULL;
195
196   /* initialize error message tables if necessary */
197   if (gst_core_errors == NULL)
198     gst_core_errors = _gst_core_errors_init ();
199   if (gst_library_errors == NULL)
200     gst_library_errors = _gst_library_errors_init ();
201   if (gst_resource_errors == NULL)
202     gst_resource_errors = _gst_resource_errors_init ();
203   if (gst_stream_errors == NULL)
204     gst_stream_errors = _gst_stream_errors_init ();
205
206
207   if (domain == GST_CORE_ERROR)
208     message = gst_core_errors[code];
209   else if (domain == GST_LIBRARY_ERROR)
210     message = gst_library_errors[code];
211   else if (domain == GST_RESOURCE_ERROR)
212     message = gst_resource_errors[code];
213   else if (domain == GST_STREAM_ERROR)
214     message = gst_stream_errors[code];
215   else {
216     g_warning ("No error messages for domain %s", g_quark_to_string (domain));
217     return g_strdup_printf (_("No error message for domain %s."),
218         g_quark_to_string (domain));
219   }
220   if (message)
221     return g_strdup (_(message));
222   else
223     return
224         g_strdup_printf (_
225         ("No standard error message for domain %s and code %d."),
226         g_quark_to_string (domain), code);
227 }