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