Initial import of ephy (rev# 7126) from svn
[ephy-soc.git] / embed / ephy-download.c
blob0bdb58cc235b67ad7973551bc5456ae5a5e6c9e9
1 /*
2 * Copyright © 2000, 2001, 2002 Marco Pesenti Gritti
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
7 * any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * $Id: ephy-download.c 6952 2007-03-11 19:42:02Z chpe $
21 #include "config.h"
23 #include "ephy-download.h"
25 #include <libgnomevfs/gnome-vfs-uri.h>
27 #define EPHY_DOWNLOAD_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EPHY_TYPE_DOWNLOAD, EphyDownloadPrivate))
29 #define REMAINING_TIME_UPDATE_SECS 2
31 static void
32 ephy_download_class_init (EphyDownloadClass *klass);
33 static void
34 ephy_download_init (EphyDownload *dv);
36 enum
38 CHANGED,
39 LAST_SIGNAL
42 struct _EphyDownloadPrivate
44 gint64 remaining_time_last_update;
45 gint64 remaining_time;
48 static GObjectClass *parent_class = NULL;
50 static guint ephy_download_signals[LAST_SIGNAL] = { 0 };
52 GType
53 ephy_download_get_type (void)
55 static GType type = 0;
57 if (G_UNLIKELY (type == 0))
59 const GTypeInfo our_info =
61 sizeof (EphyDownloadClass),
62 NULL, /* base_init */
63 NULL, /* base_finalize */
64 (GClassInitFunc) ephy_download_class_init,
65 NULL, /* class_finalize */
66 NULL, /* class_data */
67 sizeof (EphyDownload),
68 0, /* n_preallocs */
69 (GInstanceInitFunc) ephy_download_init
72 type = g_type_register_static (G_TYPE_OBJECT,
73 "EphyDownload",
74 &our_info, 0);
77 return type;
80 static void
81 ephy_download_class_init (EphyDownloadClass *klass)
83 GObjectClass *object_class = G_OBJECT_CLASS (klass);
85 parent_class = g_type_class_peek_parent (klass);
87 ephy_download_signals[CHANGED] =
88 g_signal_new ("changed",
89 EPHY_TYPE_DOWNLOAD,
90 G_SIGNAL_RUN_FIRST,
91 G_STRUCT_OFFSET (EphyDownloadClass, changed),
92 NULL, NULL,
93 g_cclosure_marshal_VOID__VOID,
94 G_TYPE_NONE,
95 0);
97 g_type_class_add_private (object_class, sizeof(EphyDownloadPrivate));
100 static void
101 ephy_download_init (EphyDownload *download)
103 download->priv = EPHY_DOWNLOAD_GET_PRIVATE (download);
105 download->priv->remaining_time = 0;
106 download->priv->remaining_time_last_update = 0;
109 EphyDownload *
110 ephy_download_new (void)
112 return EPHY_DOWNLOAD (g_object_new (EPHY_TYPE_DOWNLOAD, NULL));
115 char *
116 ephy_download_get_name (EphyDownload *download)
118 GnomeVFSURI *uri;
119 char *target;
120 char *result;
122 target = ephy_download_get_target (download);
124 uri = gnome_vfs_uri_new (target);
125 if (uri)
127 result = gnome_vfs_uri_extract_short_name (uri);
128 gnome_vfs_uri_unref (uri);
130 else
132 result = g_strdup ("Unknown");
135 g_free (target);
137 return result;
140 static void
141 update_remaining_time (EphyDownload *download)
143 gint64 elapsed_time, total, cur;
145 total = ephy_download_get_total_progress (download);
146 cur = ephy_download_get_current_progress (download);
147 elapsed_time = ephy_download_get_elapsed_time (download);
149 if (cur > 0)
151 float per_byte_time;
153 per_byte_time = (float)elapsed_time / (float)cur;
154 download->priv->remaining_time = per_byte_time * (total - cur);
158 gint64
159 ephy_download_get_remaining_time (EphyDownload *download)
161 gint64 elapsed_time;
163 elapsed_time = ephy_download_get_elapsed_time (download);
164 if (elapsed_time - download->priv->remaining_time_last_update >=
165 REMAINING_TIME_UPDATE_SECS)
167 update_remaining_time (download);
168 download->priv->remaining_time_last_update = elapsed_time;
171 return download->priv->remaining_time;
174 char *
175 ephy_download_get_source (EphyDownload *download)
177 EphyDownloadClass *klass = EPHY_DOWNLOAD_GET_CLASS (download);
178 return klass->get_source (download);
181 char *
182 ephy_download_get_target (EphyDownload *download)
184 EphyDownloadClass *klass = EPHY_DOWNLOAD_GET_CLASS (download);
185 return klass->get_target (download);
188 gint64
189 ephy_download_get_current_progress (EphyDownload *download)
191 EphyDownloadClass *klass = EPHY_DOWNLOAD_GET_CLASS (download);
192 return klass->get_current_progress (download);
195 gint64
196 ephy_download_get_total_progress (EphyDownload *download)
198 EphyDownloadClass *klass = EPHY_DOWNLOAD_GET_CLASS (download);
199 return klass->get_total_progress (download);
203 ephy_download_get_percent (EphyDownload *download)
205 EphyDownloadClass *klass = EPHY_DOWNLOAD_GET_CLASS (download);
206 return klass->get_percent (download);
209 gint64
210 ephy_download_get_elapsed_time (EphyDownload *download)
212 EphyDownloadClass *klass = EPHY_DOWNLOAD_GET_CLASS (download);
213 return klass->get_elapsed_time (download);
216 EphyDownloadState
217 ephy_download_get_state (EphyDownload *download)
219 EphyDownloadClass *klass = EPHY_DOWNLOAD_GET_CLASS (download);
220 return klass->get_state (download);
223 char *
224 ephy_download_get_mime (EphyDownload *download)
226 EphyDownloadClass *klass = EPHY_DOWNLOAD_GET_CLASS (download);
227 return klass->get_mime (download);
230 void
231 ephy_download_cancel (EphyDownload *download)
233 EphyDownloadClass *klass = EPHY_DOWNLOAD_GET_CLASS (download);
234 klass->cancel (download);
237 void
238 ephy_download_pause (EphyDownload *download)
240 EphyDownloadClass *klass = EPHY_DOWNLOAD_GET_CLASS (download);
241 klass->pause (download);
244 void
245 ephy_download_resume (EphyDownload *download)
247 EphyDownloadClass *klass = EPHY_DOWNLOAD_GET_CLASS (download);
248 klass->resume (download);