code-analyzer: Fixed bgo#667903 - Code Analyzer Crashes
[anjuta.git] / plugins / symbol-db / symbol-db-engine-utils.c
blob3d0e2c02936e7ac9d36a0ee75c46edf151fa2207
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 * anjuta
4 * Copyright (C) Massimo Cora' 2007-2008 <maxcvs@email.it>
5 *
6 * anjuta is free software.
7 *
8 * You may redistribute it and/or modify it under the terms of the
9 * GNU General Public License, as published by the Free Software
10 * Foundation; either version 2 of the License, or (at your option)
11 * any later version.
13 * anjuta 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.
16 * See the GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with anjuta. If not, write to:
20 * The Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02110-1301, USA.
25 #include <libanjuta/anjuta-debug.h>
27 #include <libanjuta/resources.h>
28 #include "symbol-db-engine-utils.h"
29 #include "symbol-db-engine-priv.h"
31 static GHashTable *pixbufs_hash = NULL;
34 * extern declarations
36 extern GNUC_INLINE const GdaStatement *
37 sdb_engine_get_statement_by_query_id (SymbolDBEngine * dbe, static_query_type query_id);
39 extern GNUC_INLINE const GdaSet *
40 sdb_engine_get_query_parameters_list (SymbolDBEngine *dbe, static_query_type query_id);
43 * implementation starts here
46 gint
47 symbol_db_glist_compare_func (gconstpointer a, gconstpointer b)
49 return g_strcmp0 ((const gchar*)a, (const gchar*)b);
52 gint
53 symbol_db_gtree_compare_func (gconstpointer a, gconstpointer b, gpointer user_data)
55 return GPOINTER_TO_INT(a) - GPOINTER_TO_INT(b);
58 gchar*
59 symbol_db_util_get_full_local_path (SymbolDBEngine *dbe, const gchar* file)
61 SymbolDBEnginePriv *priv;
62 g_return_val_if_fail (dbe != NULL, NULL);
64 priv = dbe->priv;
65 return g_build_filename (priv->project_directory, file, NULL);
68 const gchar*
69 symbol_db_util_get_file_db_path (SymbolDBEngine *dbe, const gchar* full_local_file_path)
71 SymbolDBEnginePriv *priv;
72 g_return_val_if_fail (dbe != NULL, NULL);
73 g_return_val_if_fail (full_local_file_path != NULL, NULL);
75 priv = dbe->priv;
77 g_return_val_if_fail (priv->project_directory != NULL, NULL);
79 if (priv->db_directory == NULL ||
80 strlen (priv->project_directory) >= strlen (full_local_file_path))
82 return NULL;
85 return full_local_file_path + strlen (priv->project_directory);
88 GPtrArray *
89 symbol_db_util_get_files_with_zero_symbols (SymbolDBEngine *dbe)
91 SymbolDBEnginePriv *priv;
92 GdaDataModel *data_model;
93 GPtrArray *files_to_scan;
94 const GdaStatement *stmt;
95 gint i, num_rows = 0;
97 g_return_val_if_fail (dbe != NULL, NULL);
98 priv = dbe->priv;
100 if (priv->mutex)
101 g_mutex_lock (priv->mutex);
103 if ((stmt = sdb_engine_get_statement_by_query_id (dbe,
104 PREP_QUERY_GET_ALL_FROM_FILE_WHERE_NOT_IN_SYMBOLS))
105 == NULL)
107 if (priv->mutex)
108 g_mutex_unlock (priv->mutex);
109 return NULL;
112 data_model = gda_connection_statement_execute_select (priv->db_connection,
113 (GdaStatement*)stmt,
114 NULL, NULL);
116 if (!GDA_IS_DATA_MODEL (data_model) ||
117 (num_rows = gda_data_model_get_n_rows (GDA_DATA_MODEL (data_model))) <= 0)
119 if (data_model != NULL)
120 g_object_unref (data_model);
121 if (priv->mutex)
122 g_mutex_unlock (priv->mutex);
123 return NULL;
126 /* initialize the array */
127 files_to_scan = g_ptr_array_new_with_free_func (g_free);
129 /* we can now scan each filename entry to check the last modification time. */
130 for (i = 0; i < num_rows; i++)
132 const GValue *value;
133 const gchar *file_name;
134 gchar *file_abs_path = NULL;
136 if ((value =
137 gda_data_model_get_value_at (data_model,
138 gda_data_model_get_column_index(data_model, "db_file_path"),
139 i, NULL)) == NULL)
141 continue;
144 /* build abs path. */
145 file_name = g_value_get_string (value);
146 file_abs_path = symbol_db_util_get_full_local_path (dbe, file_name);
147 g_ptr_array_add (files_to_scan, file_abs_path);
150 g_object_unref (data_model);
152 if (priv->mutex)
153 g_mutex_unlock (priv->mutex);
155 return files_to_scan;
158 #define CREATE_SYM_ICON(N, F) \
159 pix_file = anjuta_res_get_pixmap_file (F); \
160 g_hash_table_insert (pixbufs_hash, \
161 N, \
162 gdk_pixbuf_new_from_file (pix_file, NULL)); \
163 g_free (pix_file);
165 static void
166 sdb_util_load_symbol_pixbufs ()
168 gchar *pix_file;
170 if (pixbufs_hash != NULL)
172 /* we already have loaded it */
173 return;
176 pixbufs_hash = g_hash_table_new (g_str_hash, g_str_equal);
178 CREATE_SYM_ICON ("class", "element-class-16.png");
179 CREATE_SYM_ICON ("enum", "element-enumeration-16.png");
180 CREATE_SYM_ICON ("enumerator", "element-enumeration-16.png");
181 CREATE_SYM_ICON ("function", "element-method-16.png");
182 CREATE_SYM_ICON ("method", "element-method-16.png");
183 CREATE_SYM_ICON ("interface", "element-interface-16.png");
184 CREATE_SYM_ICON ("macro", "element-event-16.png");
185 CREATE_SYM_ICON ("namespace", "element-namespace-16.png");
186 CREATE_SYM_ICON ("none", "element-literal-16.png");
187 CREATE_SYM_ICON ("struct", "element-structure-16.png");
188 CREATE_SYM_ICON ("typedef", "element-literal-16.png");
189 CREATE_SYM_ICON ("union", "element-structure-16.png");
190 CREATE_SYM_ICON ("variable", "element-literal-16.png");
191 CREATE_SYM_ICON ("prototype", "element-interface-16.png");
193 CREATE_SYM_ICON ("privateclass", "element-class-16.png");
194 CREATE_SYM_ICON ("privateenum", "element-enumeration-16.png");
195 CREATE_SYM_ICON ("privatefield", "element-event-16.png");
196 CREATE_SYM_ICON ("privatefunction", "element-method-private-16.png");
197 CREATE_SYM_ICON ("privateinterface", "element-interface-private-16.png");
198 CREATE_SYM_ICON ("privatemember", "element-property-private-16.png");
199 CREATE_SYM_ICON ("privatemethod", "element-method-private-16.png");
200 CREATE_SYM_ICON ("privateproperty", "element-property-private-16.png");
201 CREATE_SYM_ICON ("privatestruct", "element-structure-16.png");
202 CREATE_SYM_ICON ("privateprototype", "element-interface-private-16.png");
204 CREATE_SYM_ICON ("protectedclass", "element-class-16.png");
205 CREATE_SYM_ICON ("protectedenum", "element-enumeration-16.png");
206 CREATE_SYM_ICON ("protectedfield", "element-event-16.png");
207 CREATE_SYM_ICON ("protectedfunction", "element-method-protected-16.png");
208 CREATE_SYM_ICON ("protectedmember", "element-property-protected-16.png");
209 CREATE_SYM_ICON ("protectedmethod", "element-method-protected-16.png");
210 CREATE_SYM_ICON ("protectedproperty", "element-property-protected-16.png");
211 CREATE_SYM_ICON ("protectedprototype","element-interface-protected-16.png");
213 CREATE_SYM_ICON ("publicclass", "element-class-16.png");
214 CREATE_SYM_ICON ("publicenum", "element-enumeration-16.png");
215 CREATE_SYM_ICON ("publicfunction", "element-method-public-16.png");
216 CREATE_SYM_ICON ("publicmember", "element-property-public-16.png");
217 CREATE_SYM_ICON ("publicmethod", "element-method-public-16.png");
218 CREATE_SYM_ICON ("publicproperty", "element-property-public-16.png");
219 CREATE_SYM_ICON ("publicstruct", "element-structure-16.png");
220 CREATE_SYM_ICON ("publicprototype", "element-interface-public-16.png");
222 /* special icon */
223 CREATE_SYM_ICON ("othersvars", "element-event-16.png");
224 CREATE_SYM_ICON ("globalglobal", "element-event-16.png");
227 const GdkPixbuf*
228 symbol_db_util_get_pixbuf (const gchar *node_type, const gchar *node_access)
230 gchar *search_node;
231 GdkPixbuf *pix;
232 if (!pixbufs_hash)
234 /* lazy loading */
235 sdb_util_load_symbol_pixbufs ();
238 /*DEBUG_PRINT ("symbol_db_view_get_pixbuf: node_type %s node_access %s",
239 node_type, node_access);*/
241 /* is there a better/quicker method to retrieve pixbufs? */
242 if (node_type != NULL && node_access != NULL)
244 search_node = g_strdup_printf ("%s%s", node_access, node_type);
246 else if (node_type != NULL)
248 /* we will not free search_node gchar, so casting here is ok. */
249 search_node = (gchar*)node_type;
251 else {
252 search_node = "othersvars";
254 pix = GDK_PIXBUF (g_hash_table_lookup (pixbufs_hash, search_node));
256 if (node_type != NULL && node_access != NULL)
258 g_free (search_node);
261 if (pix == NULL)
263 DEBUG_PRINT ("symbol_db_view_get_pixbuf (): no pixbuf for %s %s",
264 node_type, node_access);
267 return pix;