3
* Copyright (C) 2015 Christian Hergert <christian@hergert.me>
5
* This program is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation, either version 3 of the License, or
8
* (at your option) any later version.
10
* This program 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
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19
#define G_LOG_DOMAIN "ide-progress"
21
#include <glib/gi18n.h>
23
#include "ide-progress.h"
27
GObject parent_instance;
34
G_DEFINE_TYPE (IdeProgress, ide_progress, G_TYPE_OBJECT)
44
static GParamSpec *gParamSpecs [LAST_PROP];
47
ide_progress_get_completed (IdeProgress *self)
49
g_return_val_if_fail (IDE_IS_PROGRESS (self), FALSE);
51
return self->completed;
55
ide_progress_set_completed (IdeProgress *self,
58
g_return_if_fail (IDE_IS_PROGRESS (self));
60
if (self->completed != completed)
62
self->completed = completed;
63
g_object_notify_by_pspec (G_OBJECT (self), gParamSpecs [PROP_COMPLETED]);
68
ide_progress_get_fraction (IdeProgress *self)
70
g_return_val_if_fail (IDE_IS_PROGRESS (self), 0.0);
72
return self->fraction;
76
ide_progress_set_fraction (IdeProgress *self,
79
g_return_if_fail (IDE_IS_PROGRESS (self));
80
g_return_if_fail (fraction >= 0.0);
81
g_return_if_fail (fraction <= 1.0);
83
if (self->fraction != fraction)
85
self->fraction = fraction;
87
ide_progress_set_completed (self, TRUE);
88
g_object_notify_by_pspec (G_OBJECT (self), gParamSpecs [PROP_FRACTION]);
93
ide_progress_get_message (IdeProgress *self)
95
g_return_val_if_fail (IDE_IS_PROGRESS (self), NULL);
101
ide_progress_set_message (IdeProgress *self,
102
const gchar *message)
104
g_return_if_fail (IDE_IS_PROGRESS (self));
106
if (self->message != message)
108
g_free (self->message);
109
self->message = g_strdup (message);
110
g_object_notify_by_pspec (G_OBJECT (self), gParamSpecs [PROP_MESSAGE]);
115
* ide_progress_file_progress_callback:
117
* This function is a #GFileProgressCallback helper that will update the
118
* #IdeProgress:fraction property. @user_data must be an #IdeProgress.
120
* Remember to make sure to unref the #IdeProgress instance with
121
* g_object_unref() during the #GDestroyNotify.
124
ide_progress_file_progress_callback (goffset current_num_bytes,
125
goffset total_num_bytes,
128
IdeProgress *self = user_data;
129
gdouble fraction = 0.0;
131
g_return_if_fail (IDE_IS_PROGRESS (self));
134
fraction = (gdouble)current_num_bytes / (gdouble)total_num_bytes;
136
ide_progress_set_fraction (self, fraction);
140
ide_progress_finalize (GObject *object)
142
IdeProgress *self = (IdeProgress *)object;
144
g_clear_pointer (&self->message, g_free);
146
G_OBJECT_CLASS (ide_progress_parent_class)->finalize (object);
150
ide_progress_get_property (GObject *object,
155
IdeProgress *self = IDE_PROGRESS (object);
160
g_value_set_boolean (value, ide_progress_get_completed (self));
164
g_value_set_double (value, ide_progress_get_fraction (self));
168
g_value_set_string (value, ide_progress_get_message (self));
172
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
177
ide_progress_set_property (GObject *object,
182
IdeProgress *self = IDE_PROGRESS (object);
187
ide_progress_set_fraction (self, g_value_get_double (value));
191
ide_progress_set_message (self, g_value_get_string (value));
195
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
200
ide_progress_class_init (IdeProgressClass *klass)
202
GObjectClass *object_class = G_OBJECT_CLASS (klass);
204
object_class->finalize = ide_progress_finalize;
205
object_class->get_property = ide_progress_get_property;
206
object_class->set_property = ide_progress_set_property;
208
gParamSpecs [PROP_COMPLETED] =
209
g_param_spec_boolean ("completed",
211
"If the progress has completed.",
213
(G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
215
gParamSpecs [PROP_FRACTION] =
216
g_param_spec_double ("fraction",
218
"The fraction of the progress.",
222
(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
224
gParamSpecs [PROP_MESSAGE] =
225
g_param_spec_string ("message",
227
"A short message for the progress.",
229
(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
231
g_object_class_install_properties (object_class, LAST_PROP, gParamSpecs);
235
ide_progress_init (IdeProgress *self)
240
ide_progress_new (void)
242
return g_object_new (IDE_TYPE_PROGRESS, NULL);