search: Implemented file search based on mime-type and some memory fixes.
[anjuta.git] / plugins / document-manager / search-filter-file-command.c
blob09f57f5a9dd2cb27917b3f597710b14bd0f51f26
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 * anjuta
4 * Copyright (C) Johannes Schmid 2012 <jhs@Obelix>
5 *
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/>.
20 #include "search-filter-file-command.h"
21 #include <libanjuta/anjuta-debug.h>
23 struct _SearchFilterFileCommandPrivate
25 GFile* file;
26 gchar* mime_types;
29 enum
31 PROP_0,
33 PROP_FILE,
34 PROP_MIME_TYPES
39 static guint
40 search_filter_file_command_run (AnjutaCommand* anjuta_cmd)
42 SearchFilterFileCommand* cmd = SEARCH_FILTER_FILE_COMMAND (anjuta_cmd);
44 gchar** mime_types = g_strsplit (cmd->priv->mime_types, ",", -1);
45 gchar** mime_type;
46 guint retval = 1;
48 GFileInfo* file_info;
49 GError* error = NULL;
51 file_info = g_file_query_info (cmd->priv->file,
52 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
53 G_FILE_QUERY_INFO_NONE,
54 NULL, &error);
56 if (file_info)
58 for (mime_type = mime_types; mime_type && *mime_type; mime_type++)
60 gchar* content_type = g_content_type_from_mime_type (*mime_type);
61 if (g_content_type_is_a (g_file_info_get_content_type (file_info),
62 content_type))
64 retval = 0;
65 g_free (content_type);
66 break;
69 g_object_unref (file_info);
71 else
73 int code = error->code;
74 DEBUG_PRINT ("Couldn't query mime-type: %s", error->message);
75 g_error_free (error);
76 return code;
78 g_strfreev(mime_types);
80 return retval;
83 G_DEFINE_TYPE (SearchFilterFileCommand, search_filter_file_command, ANJUTA_TYPE_ASYNC_COMMAND);
85 static void
86 search_filter_file_command_init (SearchFilterFileCommand *cmd)
88 cmd->priv = G_TYPE_INSTANCE_GET_PRIVATE (cmd,
89 SEARCH_TYPE_FILTER_FILE_COMMAND,
90 SearchFilterFileCommandPrivate);
93 static void
94 search_filter_file_command_finalize (GObject *object)
96 SearchFilterFileCommand* cmd = SEARCH_FILTER_FILE_COMMAND(object);
98 if (cmd->priv->file)
99 g_object_unref (cmd->priv->file);
100 g_free (cmd->priv->mime_types);
102 G_OBJECT_CLASS (search_filter_file_command_parent_class)->finalize (object);
105 static void
106 search_filter_file_command_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
108 SearchFilterFileCommand* cmd;
110 g_return_if_fail (SEARCH_IS_FILTER_FILE_COMMAND (object));
112 cmd = SEARCH_FILTER_FILE_COMMAND(object);
114 switch (prop_id)
116 case PROP_FILE:
117 if (cmd->priv->file)
118 g_object_unref (cmd->priv->file);
119 cmd->priv->file = G_FILE(g_value_dup_object (value));
120 break;
121 case PROP_MIME_TYPES:
122 g_free (cmd->priv->mime_types);
123 cmd->priv->mime_types = g_value_dup_string (value);
124 break;
125 default:
126 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
127 break;
131 static void
132 search_filter_file_command_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
134 SearchFilterFileCommand* cmd;
136 g_return_if_fail (SEARCH_IS_FILTER_FILE_COMMAND (object));
138 cmd = SEARCH_FILTER_FILE_COMMAND(object);
140 switch (prop_id)
142 case PROP_FILE:
143 g_value_set_object (value, cmd->priv->file);
144 break;
145 case PROP_MIME_TYPES:
146 g_value_set_string (value, cmd->priv->mime_types);
147 break;
148 default:
149 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
150 break;
154 static void
155 search_filter_file_command_class_init (SearchFilterFileCommandClass *klass)
157 GObjectClass* object_class = G_OBJECT_CLASS (klass);
158 AnjutaCommandClass* cmd_class = ANJUTA_COMMAND_CLASS (klass);
160 object_class->finalize = search_filter_file_command_finalize;
161 object_class->set_property = search_filter_file_command_set_property;
162 object_class->get_property = search_filter_file_command_get_property;
164 g_object_class_install_property (object_class,
165 PROP_FILE,
166 g_param_spec_object ("file",
169 G_TYPE_FILE,
170 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
172 g_object_class_install_property (object_class,
173 PROP_MIME_TYPES,
174 g_param_spec_string ("mime-types",
177 NULL,
178 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
180 cmd_class->run = search_filter_file_command_run;
182 g_type_class_add_private (klass, sizeof (SearchFilterFileCommandPrivate));
186 SearchFilterFileCommand*
187 search_filter_file_command_new (GFile* file, const gchar* mime_types)
189 SearchFilterFileCommand* cmd;
191 cmd = SEARCH_FILTER_FILE_COMMAND (g_object_new (SEARCH_TYPE_FILTER_FILE_COMMAND,
192 "file", file,
193 "mime-types", mime_types,
194 NULL));
195 return cmd;