Imported Upstream version 3.3.3
[debian/amanda] / xfer-src / xmsg.c
1 /*
2  * Copyright (c) 2008-2012 Zmanda, Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17  *
18  * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
19  * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
20  */
21
22 #include "amanda.h"
23 #include "amxfer.h"
24
25 /* TODO: use glib chunk allocator */
26
27 /*
28  * Methods
29  */
30
31 XMsg *
32 xmsg_new(
33     XferElement *elt,
34     xmsg_type type,
35     int version)
36 {
37     XMsg *msg = g_new0(XMsg, 1);
38     msg->elt = elt;
39     msg->type = type;
40     msg->version = version;
41
42     /* messages hold a reference to the XferElement, to avoid dangling
43      * pointers. */
44     g_object_ref((GObject *)elt);
45
46     return msg;
47 }
48
49 void
50 xmsg_free(
51     XMsg *msg)
52 {
53     /* unreference the source */
54     g_object_unref((GObject *)msg->elt);
55
56     /* and free any allocated attributes */
57     if (msg->repr) g_free(msg->repr);
58     if (msg->message) g_free(msg->message);
59
60     /* then free the XMsg itself */
61     g_free(msg);
62 }
63
64 char *
65 xmsg_repr(
66     XMsg *msg)
67 {
68     if (!msg) return "(nil)"; /* better safe than sorry */
69
70     /* this just shows the "header" fields for now */
71     if (!msg->repr) {
72         char *typ = NULL;
73         switch (msg->type) {
74             case XMSG_INFO: typ = "INFO"; break;
75             case XMSG_ERROR: typ = "ERROR"; break;
76             case XMSG_DONE: typ = "DONE"; break;
77             case XMSG_CANCEL: typ = "CANCEL"; break;
78             case XMSG_PART_DONE: typ = "PART_DONE"; break;
79             case XMSG_READY: typ = "READY"; break;
80             default: typ = "**UNKNOWN**"; break;
81         }
82
83         msg->repr = vstrallocf("<XMsg@%p type=XMSG_%s elt=%s version=%d>",
84             msg, typ, xfer_element_repr(msg->elt), msg->version);
85     }
86
87     return msg->repr;
88 }