1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 
3 /*
4  *  File-Roller
5  *
6  *  Copyright (C) 2003 Free Software Foundation, Inc.
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
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 General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 
23 #include <config.h>
24 #include "fr-error.h"
25 
26 
27 GQuark
fr_error_quark(void)28 fr_error_quark (void)
29 {
30 	static GQuark quark;
31 
32         if (!quark)
33                 quark = g_quark_from_static_string ("FrError");
34 
35         return quark;
36 }
37 
38 
39 G_DEFINE_BOXED_TYPE (FrError,
40 		     fr_error,
41 		     fr_error_copy,
42 		     fr_error_free);
43 
44 
45 FrError *
fr_error_new(FrErrorType type,int status,GError * gerror)46 fr_error_new (FrErrorType  type,
47 	      int          status,
48 	      GError      *gerror)
49 {
50 	FrError *error;
51 
52 	error = g_new0 (FrError, 1);
53 	fr_error_set (error, type, status, gerror);
54 
55 	return error;
56 }
57 
58 
59 FrError *
fr_error_copy(FrError * error)60 fr_error_copy (FrError *error)
61 {
62 	if (error != NULL)
63 		return fr_error_new (error->type, error->status, error->gerror);
64 	else
65 		return NULL;
66 }
67 
68 
69 void
fr_error_free(FrError * error)70 fr_error_free (FrError *error)
71 {
72 	if (error == NULL)
73 		return;
74 	g_clear_error (&error->gerror);
75 	g_free (error);
76 }
77 
78 
79 void
fr_error_set(FrError * error,FrErrorType type,int status,GError * gerror)80 fr_error_set (FrError     *error,
81 	      FrErrorType  type,
82 	      int          status,
83 	      GError      *gerror)
84 {
85 	error->type = type;
86 	error->status = status;
87 	if (gerror != error->gerror) {
88 		g_clear_error (&error->gerror);
89 		if (gerror != NULL)
90 			error->gerror = g_error_copy (gerror);
91 	}
92 }
93 
94 
95 void
fr_error_take_gerror(FrError * error,GError * gerror)96 fr_error_take_gerror (FrError *error,
97 		      GError  *gerror)
98 {
99 	if (gerror != error->gerror) {
100 		g_clear_error (&error->gerror);
101 		error->gerror = gerror;
102 	}
103 }
104 
105 
106 void
fr_error_clear_gerror(FrError * error)107 fr_error_clear_gerror (FrError *error)
108 {
109 	g_clear_error (&error->gerror);
110 }
111 
112 
113 void
fr_clear_error(FrError ** error)114 fr_clear_error (FrError **error)
115 {
116 	if ((error == NULL) || (*error == NULL))
117 		return;
118 
119 	fr_error_free (*error);
120 	*error = NULL;
121 }
122