Debian Codegraph @

src/libqmi-glib/qmi-file.c

raw
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * libqmi-glib -- GLib/GIO based library to control QMI devices
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301 USA.
19 *
20 * Copyright (C) 2012 Lanedo GmbH
21 * Copyright (C) 2012-2017 Aleksander Morgado <[email protected]>
22 * Copyright (C) 2019 Eric Caruso <[email protected]>
23 */
24
25 #include "qmi-file.h"
26
27 #include "qmi-error-types.h"
28 #include "qmi-errors.h"
29
30 G_DEFINE_TYPE (QmiFile, qmi_file, G_TYPE_OBJECT)
31
32 struct _QmiFilePrivate {
33 GFile *file;
34 gboolean is_uri;
35 gchar *path;
36 gchar *path_display;
37 };
38
39 /*****************************************************************************/
40
41 GFile *
42 qmi_file_get_file (QmiFile *self)
43 {
44 return g_object_ref (self->priv->file);
45 }
46
47 GFile *
48 qmi_file_peek_file (QmiFile *self)
49 {
50 return self->priv->file;
51 }
52
53 const gchar *
54 qmi_file_get_path (QmiFile *self)
55 {
56 return self->priv->path;
57 }
58
59 const gchar *
60 qmi_file_get_path_display (QmiFile *self)
61 {
62 return self->priv->path_display;
63 }
64
65 /*****************************************************************************/
66
67 gboolean
68 qmi_file_check_type_finish (QmiFile *self,
69 GAsyncResult *res,
70 GError **error)
71 {
72 return g_task_propagate_boolean (G_TASK (res), error);
73 }
74
75 static void
76 query_info_async_ready (GFile *file,
77 GAsyncResult *res,
78 GTask *task)
79 {
80 GError *error = NULL;
81 GFileInfo *info;
82 GFileType file_type;
83
84 info = g_file_query_info_finish (file, res, &error);
85 if (!info) {
86 g_prefix_error (&error,
87 "Couldn't query file info: ");
88 g_task_return_error (task, error);
89 g_object_unref (task);
90 return;
91 }
92
93 file_type = g_file_info_get_file_type (info);
94 g_object_unref (info);
95
96 if (file_type != G_FILE_TYPE_SPECIAL) {
97 g_task_return_new_error (task,
98 QMI_CORE_ERROR,
99 QMI_CORE_ERROR_FAILED,
100 "Wrong file type");
101 g_object_unref (task);
102 return;
103 }
104
105 g_task_return_boolean (task, TRUE);
106 g_object_unref (task);
107 }
108
109 void
110 qmi_file_check_type_async (QmiFile *self,
111 GCancellable *cancellable,
112 GAsyncReadyCallback callback,
113 gpointer user_data)
114 {
115 GTask *task;
116
117 task = g_task_new (self, cancellable, callback, user_data);
118
119 /* If this file only has a URI and no local path, then it refers to some
120 * non-filesystem resource, such as something on a device bus. We can't
121 * figure out if it's the right type in this case, so we just try to see
122 * if it's OK later. */
123 if (self->priv->is_uri) {
124 g_task_return_boolean (task, TRUE);
125 g_object_unref (task);
126 return;
127 }
128
129 /* Check the file type. Note that this is just a quick check to avoid
130 * creating QmiDevices pointing to a location already known not to be a QMI
131 * device. */
132 g_file_query_info_async (self->priv->file,
133 G_FILE_ATTRIBUTE_STANDARD_TYPE,
134 G_FILE_QUERY_INFO_NONE,
135 G_PRIORITY_DEFAULT,
136 cancellable,
137 (GAsyncReadyCallback)query_info_async_ready,
138 task);
139 }
140
141 /*****************************************************************************/
142
143 QmiFile *
144 qmi_file_new (GFile *file)
145 {
146 QmiFile *self;
147 gchar *path;
148
149 g_assert (G_IS_FILE (file));
150
151 self = g_object_new (QMI_TYPE_FILE, NULL);
152 self->priv->file = g_object_ref (file);
153
154 path = g_file_get_path (self->priv->file);
155 if (path) {
156 self->priv->is_uri = FALSE;
157 self->priv->path = path;
158 self->priv->path_display = g_filename_display_name (self->priv->path);
159 } else {
160 self->priv->is_uri = TRUE;
161 self->priv->path = g_file_get_uri (self->priv->file);
162 self->priv->path_display = g_strdup (self->priv->path);
163 }
164
165 return self;
166 }
167
168 /*****************************************************************************/
169
170 static void
171 qmi_file_init (QmiFile *self)
172 {
173 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, QMI_TYPE_FILE, QmiFilePrivate);
174 }
175
176 static void
177 dispose (GObject *object)
178 {
179 QmiFile *self = QMI_FILE (object);
180
181 g_clear_object (&self->priv->file);
182 g_clear_pointer (&self->priv->path, g_free);
183 g_clear_pointer (&self->priv->path_display, g_free);
184
185 G_OBJECT_CLASS (qmi_file_parent_class)->dispose (object);
186 }
187
188 static void
189 qmi_file_class_init (QmiFileClass *klass)
190 {
191 GObjectClass *object_class = G_OBJECT_CLASS (klass);
192
193 g_type_class_add_private (object_class, sizeof (QmiFilePrivate));
194
195 object_class->dispose = dispose;
196 }