1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4 * Copyright (C) Johannes Schmid 2011 <jhs@Obelix>
6 * anjuta is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * anjuta is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
21 * SECTION: anjuta-pkg-scanner
22 * @short_description: Scan files contained by a library using pkg-config
23 * @see_also: #AnjutaAsyncCommand
24 * @include libanjuta/anjuta-pkg-scanner.h
26 * #AnjutaPkgScanner is an async command that can be used to query the list
27 * of files and the version of a package.
31 #include <libanjuta/anjuta-pkg-scanner.h>
32 #include <libanjuta/anjuta-utils.h>
33 #include <libanjuta/anjuta-pkg-config.h>
43 struct _AnjutaPkgScannerPrivate
50 G_DEFINE_TYPE (AnjutaPkgScanner
, anjuta_pkg_scanner
, ANJUTA_TYPE_ASYNC_COMMAND
);
55 anjuta_pkg_scanner_init (AnjutaPkgScanner
*object
)
57 object
->priv
= G_TYPE_INSTANCE_GET_PRIVATE (object
,
58 ANJUTA_TYPE_PKG_SCANNER
,
59 AnjutaPkgScannerPrivate
);
60 object
->priv
->files
= NULL
;
64 anjuta_pkg_scanner_finalize (GObject
*object
)
66 AnjutaPkgScanner
* scanner
= ANJUTA_PKG_SCANNER (object
);
67 g_free (scanner
->priv
->package
);
68 g_free (scanner
->priv
->version
);
69 anjuta_util_glist_strings_free (scanner
->priv
->files
);
71 G_OBJECT_CLASS (anjuta_pkg_scanner_parent_class
)->finalize (object
);
75 anjuta_pkg_scanner_set_property (GObject
*object
, guint prop_id
, const GValue
*value
, GParamSpec
*pspec
)
77 AnjutaPkgScanner
* scanner
;
79 g_return_if_fail (ANJUTA_IS_PKG_SCANNER (object
));
81 scanner
= ANJUTA_PKG_SCANNER(object
);
86 scanner
->priv
->package
= g_value_dup_string (value
);
89 scanner
->priv
->version
= g_value_dup_string (value
);
92 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
98 anjuta_pkg_scanner_get_property (GObject
*object
, guint prop_id
, GValue
*value
, GParamSpec
*pspec
)
100 AnjutaPkgScanner
* scanner
;
102 g_return_if_fail (ANJUTA_IS_PKG_SCANNER (object
));
104 scanner
= ANJUTA_PKG_SCANNER(object
);
109 g_value_set_string (value
, scanner
->priv
->package
);
112 g_value_set_string (value
, scanner
->priv
->version
);
115 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
121 anjuta_pkg_scanner_list_files (GList
**children
, GFile
*dir
)
123 GFileEnumerator
*list
;
125 list
= g_file_enumerate_children (dir
,
126 G_FILE_ATTRIBUTE_STANDARD_NAME
,
127 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
,
135 while ((info
= g_file_enumerator_next_file (list
, NULL
, NULL
)) != NULL
)
140 name
= g_file_info_get_name (info
);
141 file
= g_file_get_child (dir
, name
);
142 g_object_unref (info
);
144 if (g_file_query_file_type (file
, G_FILE_QUERY_INFO_NONE
, NULL
) == G_FILE_TYPE_DIRECTORY
)
146 anjuta_pkg_scanner_list_files (children
, file
);
150 gchar
* filename
= g_file_get_path (file
);
151 *children
= g_list_prepend (*children
, filename
);
153 g_object_unref (file
);
155 g_file_enumerator_close (list
, NULL
, NULL
);
156 g_object_unref (list
);
161 anjuta_pkg_scanner_run (AnjutaCommand
* command
)
163 AnjutaPkgScanner
* scanner
= ANJUTA_PKG_SCANNER (command
);
164 GList
* dirs
= anjuta_pkg_config_get_directories (scanner
->priv
->package
, TRUE
, NULL
);
167 for (dir
= dirs
; dir
!= NULL
; dir
= g_list_next (dir
))
169 GFile
* file
= g_file_new_for_path (dir
->data
);
170 anjuta_pkg_scanner_list_files (&scanner
->priv
->files
, file
);
172 anjuta_util_glist_strings_free (dirs
);
177 anjuta_pkg_scanner_class_init (AnjutaPkgScannerClass
*klass
)
179 GObjectClass
* object_class
= G_OBJECT_CLASS (klass
);
180 AnjutaCommandClass
* command_class
= ANJUTA_COMMAND_CLASS (klass
);
182 object_class
->finalize
= anjuta_pkg_scanner_finalize
;
183 object_class
->set_property
= anjuta_pkg_scanner_set_property
;
184 object_class
->get_property
= anjuta_pkg_scanner_get_property
;
186 g_object_class_install_property (object_class
,
188 g_param_spec_string ("package",
190 "Name of the package",
192 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT
));
194 g_object_class_install_property (object_class
,
196 g_param_spec_string ("version",
198 "Version of the package",
200 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT
));
202 command_class
->run
= anjuta_pkg_scanner_run
;
204 g_type_class_add_private (klass
, sizeof (AnjutaPkgScannerPrivate
));
208 * anjuta_pkg_scanner_new:
209 * @package: Name of the package to scan
210 * @version: Version of the package
212 * Returns: a new #AnjutaCommand to scan for files.
215 anjuta_pkg_scanner_new (const gchar
* package
, const gchar
* version
)
218 g_object_new (ANJUTA_TYPE_PKG_SCANNER
,
223 return ANJUTA_COMMAND (object
);
227 anjuta_pkg_scanner_get_package (AnjutaPkgScanner
* scanner
)
229 return scanner
->priv
->package
;
232 anjuta_pkg_scanner_get_version (AnjutaPkgScanner
* scanner
)
234 return scanner
->priv
->version
;
238 anjuta_pkg_scanner_get_files (AnjutaPkgScanner
* scanner
)
240 return scanner
->priv
->files
;