1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4; coding: utf-8 -*- */
2 /* am-source.c
3  *
4  * Copyright (C) 2009  Sébastien Granjoux
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public
17  * License along with this program; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include "amp-source.h"
28 
29 #include "amp-node.h"
30 #include "am-scanner.h"
31 #include "am-properties.h"
32 #include "am-writer.h"
33 
34 
35 #include <libanjuta/interfaces/ianjuta-project.h>
36 
37 #include <libanjuta/anjuta-debug.h>
38 
39 #include <glib/gi18n.h>
40 
41 #include <memory.h>
42 #include <string.h>
43 #include <ctype.h>
44 
45 /* Types
46  *---------------------------------------------------------------------------*/
47 
48 struct _AmpSourceNode {
49 	AnjutaProjectNode base;
50 	AnjutaToken* token;
51 };
52 
53 
54 
55 
56 /* Source object
57  *---------------------------------------------------------------------------*/
58 
59 AnjutaToken *
amp_source_node_get_token(AmpSourceNode * node)60 amp_source_node_get_token (AmpSourceNode *node)
61 {
62 	return node->token;
63 }
64 
65 void
amp_source_node_add_token(AmpSourceNode * node,AnjutaToken * token)66 amp_source_node_add_token (AmpSourceNode *node, AnjutaToken *token)
67 {
68 	node->token = token;
69 }
70 
71 void
amp_source_node_update_node(AmpSourceNode * node,AmpSourceNode * new_node)72 amp_source_node_update_node (AmpSourceNode *node, AmpSourceNode *new_node)
73 {
74 	node->token = new_node->token;
75 }
76 
77 AnjutaProjectNode*
amp_source_node_new(GFile * file,AnjutaProjectNodeType type)78 amp_source_node_new (GFile *file, AnjutaProjectNodeType type)
79 {
80 	AmpSourceNode *node = NULL;
81 
82 	node = g_object_new (AMP_TYPE_SOURCE_NODE, NULL);
83 	node->base.file = g_object_ref (file);
84 	node->base.type = ANJUTA_PROJECT_SOURCE | type;
85 
86 	return ANJUTA_PROJECT_NODE (node);
87 }
88 
89 AnjutaProjectNode*
amp_source_node_new_valid(GFile * file,AnjutaProjectNodeType type,GError ** error)90 amp_source_node_new_valid (GFile *file, AnjutaProjectNodeType type, GError **error)
91 {
92 	/* Validate source name */
93 
94 	if (g_file_query_file_type (file, G_FILE_QUERY_INFO_NONE, NULL) == G_FILE_TYPE_DIRECTORY)
95 	{
96 		amp_set_error (error, IANJUTA_PROJECT_ERROR_VALIDATION_FAILED,
97 		               _("Source file must be a regular file, not a directory"));
98 
99 		return NULL;
100 	}
101 
102 	return amp_source_node_new (file, type);
103 }
104 
105 gboolean
amp_source_node_set_file(AmpSourceNode * source,GFile * new_file)106 amp_source_node_set_file (AmpSourceNode *source, GFile *new_file)
107 {
108 	g_object_unref (source->base.file);
109 	source->base.file = g_object_ref (new_file);
110 
111 	return TRUE;
112 }
113 
114 void
amp_source_node_free(AmpSourceNode * node)115 amp_source_node_free (AmpSourceNode *node)
116 {
117 	g_object_unref (G_OBJECT (node));
118 }
119 
120 /* AmpNode implementation
121  *---------------------------------------------------------------------------*/
122 
123 static gboolean
amp_source_node_update(AmpNode * node,AmpNode * new_node)124 amp_source_node_update (AmpNode *node, AmpNode *new_node)
125 {
126 	amp_source_node_update_node (AMP_SOURCE_NODE (node), AMP_SOURCE_NODE (new_node));
127 
128 	return TRUE;
129 }
130 
131 static gboolean
amp_source_node_write(AmpNode * node,AmpNode * parent,AmpProject * project,GError ** error)132 amp_source_node_write (AmpNode *node, AmpNode *parent, AmpProject *project, GError **error)
133 {
134 	return amp_source_node_create_token (project, AMP_SOURCE_NODE (node), error);
135 }
136 
137 static gboolean
amp_source_node_erase(AmpNode * node,AmpNode * parent,AmpProject * project,GError ** error)138 amp_source_node_erase (AmpNode *node, AmpNode *parent, AmpProject *project, GError **error)
139 {
140 	return amp_source_node_delete_token (project, AMP_SOURCE_NODE (node), error);
141 }
142 
143 
144 
145 
146 /* GObjet implementation
147  *---------------------------------------------------------------------------*/
148 
149 typedef struct _AmpSourceNodeClass AmpSourceNodeClass;
150 
151 struct _AmpSourceNodeClass {
152 	AmpNodeClass parent_class;
153 };
154 
155 G_DEFINE_DYNAMIC_TYPE (AmpSourceNode, amp_source_node, AMP_TYPE_NODE);
156 
157 static void
amp_source_node_init(AmpSourceNode * node)158 amp_source_node_init (AmpSourceNode *node)
159 {
160 	node->base.type = ANJUTA_PROJECT_SOURCE;
161 	node->base.properties_info = amp_get_source_property_list();
162 	node->base.state = ANJUTA_PROJECT_CAN_REMOVE;
163 	node->token = NULL;
164 }
165 
166 static void
amp_source_node_finalize(GObject * object)167 amp_source_node_finalize (GObject *object)
168 {
169 	AmpSourceNode *node = AMP_SOURCE_NODE (object);
170 
171 	G_OBJECT_CLASS (amp_source_node_parent_class)->finalize (object);
172 }
173 
174 static void
amp_source_node_class_init(AmpSourceNodeClass * klass)175 amp_source_node_class_init (AmpSourceNodeClass *klass)
176 {
177 	GObjectClass* object_class = G_OBJECT_CLASS (klass);
178 	AmpNodeClass* node_class;
179 
180 	object_class->finalize = amp_source_node_finalize;
181 
182 	node_class = AMP_NODE_CLASS (klass);
183 	node_class->update = amp_source_node_update;
184 	node_class->write = amp_source_node_write;
185 	node_class->erase = amp_source_node_erase;
186 }
187 
188 static void
amp_source_node_class_finalize(AmpSourceNodeClass * klass)189 amp_source_node_class_finalize (AmpSourceNodeClass *klass)
190 {
191 }
192 
193 void
amp_source_node_register(GTypeModule * module)194 amp_source_node_register (GTypeModule *module)
195 {
196 	amp_source_node_register_type (module);
197 }
198 
199