~codygarver/+junk/gnome-builder

« back to all changes in this revision

Viewing changes to libide/ide-progress.c

  • Committer: Cody Garver
  • Date: 2015-11-21 00:50:38 UTC
  • Revision ID: cody@elementary.io-20151121005038-8wygis63zt0ljqlz
Import https://github.com/chergert/gnome-builder 06e3158922a02a27f4abca250d70aa7b2970e06a

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ide-progress.c
 
2
 *
 
3
 * Copyright (C) 2015 Christian Hergert <christian@hergert.me>
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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/>.
 
17
 */
 
18
 
 
19
#define G_LOG_DOMAIN "ide-progress"
 
20
 
 
21
#include <glib/gi18n.h>
 
22
 
 
23
#include "ide-progress.h"
 
24
 
 
25
struct _IdeProgress
 
26
{
 
27
  GObject  parent_instance;
 
28
 
 
29
  gchar   *message;
 
30
  gdouble  fraction;
 
31
  guint    completed : 1;
 
32
};
 
33
 
 
34
G_DEFINE_TYPE (IdeProgress, ide_progress, G_TYPE_OBJECT)
 
35
 
 
36
enum {
 
37
  PROP_0,
 
38
  PROP_COMPLETED,
 
39
  PROP_FRACTION,
 
40
  PROP_MESSAGE,
 
41
  LAST_PROP
 
42
};
 
43
 
 
44
static GParamSpec *gParamSpecs [LAST_PROP];
 
45
 
 
46
gboolean
 
47
ide_progress_get_completed (IdeProgress *self)
 
48
{
 
49
  g_return_val_if_fail (IDE_IS_PROGRESS (self), FALSE);
 
50
 
 
51
  return self->completed;
 
52
}
 
53
 
 
54
void
 
55
ide_progress_set_completed (IdeProgress *self,
 
56
                            gboolean     completed)
 
57
{
 
58
  g_return_if_fail (IDE_IS_PROGRESS (self));
 
59
 
 
60
  if (self->completed != completed)
 
61
    {
 
62
      self->completed = completed;
 
63
      g_object_notify_by_pspec (G_OBJECT (self), gParamSpecs [PROP_COMPLETED]);
 
64
    }
 
65
}
 
66
 
 
67
gdouble
 
68
ide_progress_get_fraction (IdeProgress *self)
 
69
{
 
70
  g_return_val_if_fail (IDE_IS_PROGRESS (self), 0.0);
 
71
 
 
72
  return self->fraction;
 
73
}
 
74
 
 
75
void
 
76
ide_progress_set_fraction (IdeProgress *self,
 
77
                           gdouble      fraction)
 
78
{
 
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);
 
82
 
 
83
  if (self->fraction != fraction)
 
84
    {
 
85
      self->fraction = fraction;
 
86
      if (fraction == 1.0)
 
87
        ide_progress_set_completed (self, TRUE);
 
88
      g_object_notify_by_pspec (G_OBJECT (self), gParamSpecs [PROP_FRACTION]);
 
89
    }
 
90
}
 
91
 
 
92
const gchar *
 
93
ide_progress_get_message (IdeProgress *self)
 
94
{
 
95
  g_return_val_if_fail (IDE_IS_PROGRESS (self), NULL);
 
96
 
 
97
  return self->message;
 
98
}
 
99
 
 
100
void
 
101
ide_progress_set_message (IdeProgress *self,
 
102
                          const gchar *message)
 
103
{
 
104
  g_return_if_fail (IDE_IS_PROGRESS (self));
 
105
 
 
106
  if (self->message != message)
 
107
    {
 
108
      g_free (self->message);
 
109
      self->message = g_strdup (message);
 
110
      g_object_notify_by_pspec (G_OBJECT (self), gParamSpecs [PROP_MESSAGE]);
 
111
    }
 
112
}
 
113
 
 
114
/**
 
115
 * ide_progress_file_progress_callback:
 
116
 *
 
117
 * This function is a #GFileProgressCallback helper that will update the
 
118
 * #IdeProgress:fraction property. @user_data must be an #IdeProgress.
 
119
 *
 
120
 * Remember to make sure to unref the #IdeProgress instance with
 
121
 * g_object_unref() during the #GDestroyNotify.
 
122
 */
 
123
void
 
124
ide_progress_file_progress_callback (goffset  current_num_bytes,
 
125
                                     goffset  total_num_bytes,
 
126
                                     gpointer user_data)
 
127
{
 
128
  IdeProgress *self = user_data;
 
129
  gdouble fraction = 0.0;
 
130
 
 
131
  g_return_if_fail (IDE_IS_PROGRESS (self));
 
132
 
 
133
  if (total_num_bytes)
 
134
    fraction = (gdouble)current_num_bytes / (gdouble)total_num_bytes;
 
135
 
 
136
  ide_progress_set_fraction (self, fraction);
 
137
}
 
138
 
 
139
static void
 
140
ide_progress_finalize (GObject *object)
 
141
{
 
142
  IdeProgress *self = (IdeProgress *)object;
 
143
 
 
144
  g_clear_pointer (&self->message, g_free);
 
145
 
 
146
  G_OBJECT_CLASS (ide_progress_parent_class)->finalize (object);
 
147
}
 
148
 
 
149
static void
 
150
ide_progress_get_property (GObject    *object,
 
151
                           guint       prop_id,
 
152
                           GValue     *value,
 
153
                           GParamSpec *pspec)
 
154
{
 
155
  IdeProgress *self = IDE_PROGRESS (object);
 
156
 
 
157
  switch (prop_id)
 
158
    {
 
159
    case PROP_COMPLETED:
 
160
      g_value_set_boolean (value, ide_progress_get_completed (self));
 
161
      break;
 
162
 
 
163
    case PROP_FRACTION:
 
164
      g_value_set_double (value, ide_progress_get_fraction (self));
 
165
      break;
 
166
 
 
167
    case PROP_MESSAGE:
 
168
      g_value_set_string (value, ide_progress_get_message (self));
 
169
      break;
 
170
 
 
171
    default:
 
172
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
173
    }
 
174
}
 
175
 
 
176
static void
 
177
ide_progress_set_property (GObject      *object,
 
178
                           guint         prop_id,
 
179
                           const GValue *value,
 
180
                           GParamSpec   *pspec)
 
181
{
 
182
  IdeProgress *self = IDE_PROGRESS (object);
 
183
 
 
184
  switch (prop_id)
 
185
    {
 
186
    case PROP_FRACTION:
 
187
      ide_progress_set_fraction (self, g_value_get_double (value));
 
188
      break;
 
189
 
 
190
    case PROP_MESSAGE:
 
191
      ide_progress_set_message (self, g_value_get_string (value));
 
192
      break;
 
193
 
 
194
    default:
 
195
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
196
    }
 
197
}
 
198
 
 
199
static void
 
200
ide_progress_class_init (IdeProgressClass *klass)
 
201
{
 
202
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
203
 
 
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;
 
207
 
 
208
  gParamSpecs [PROP_COMPLETED] =
 
209
    g_param_spec_boolean ("completed",
 
210
                          "Completed",
 
211
                          "If the progress has completed.",
 
212
                          FALSE,
 
213
                          (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
 
214
 
 
215
  gParamSpecs [PROP_FRACTION] =
 
216
    g_param_spec_double ("fraction",
 
217
                         "Fraction",
 
218
                         "The fraction of the progress.",
 
219
                         0.0,
 
220
                         1.0,
 
221
                         0.0,
 
222
                         (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
223
 
 
224
  gParamSpecs [PROP_MESSAGE] =
 
225
    g_param_spec_string ("message",
 
226
                         "Message",
 
227
                         "A short message for the progress.",
 
228
                         NULL,
 
229
                         (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
230
 
 
231
  g_object_class_install_properties (object_class, LAST_PROP, gParamSpecs);
 
232
}
 
233
 
 
234
static void
 
235
ide_progress_init (IdeProgress *self)
 
236
{
 
237
}
 
238
 
 
239
IdeProgress *
 
240
ide_progress_new (void)
 
241
{
 
242
  return g_object_new (IDE_TYPE_PROGRESS, NULL);
 
243
}