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)
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 $
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
32 ephy_download_class_init (EphyDownloadClass
*klass
);
34 ephy_download_init (EphyDownload
*dv
);
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 };
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
),
63 NULL
, /* base_finalize */
64 (GClassInitFunc
) ephy_download_class_init
,
65 NULL
, /* class_finalize */
66 NULL
, /* class_data */
67 sizeof (EphyDownload
),
69 (GInstanceInitFunc
) ephy_download_init
72 type
= g_type_register_static (G_TYPE_OBJECT
,
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",
91 G_STRUCT_OFFSET (EphyDownloadClass
, changed
),
93 g_cclosure_marshal_VOID__VOID
,
97 g_type_class_add_private (object_class
, sizeof(EphyDownloadPrivate
));
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;
110 ephy_download_new (void)
112 return EPHY_DOWNLOAD (g_object_new (EPHY_TYPE_DOWNLOAD
, NULL
));
116 ephy_download_get_name (EphyDownload
*download
)
122 target
= ephy_download_get_target (download
);
124 uri
= gnome_vfs_uri_new (target
);
127 result
= gnome_vfs_uri_extract_short_name (uri
);
128 gnome_vfs_uri_unref (uri
);
132 result
= g_strdup ("Unknown");
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
);
153 per_byte_time
= (float)elapsed_time
/ (float)cur
;
154 download
->priv
->remaining_time
= per_byte_time
* (total
- cur
);
159 ephy_download_get_remaining_time (EphyDownload
*download
)
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
;
175 ephy_download_get_source (EphyDownload
*download
)
177 EphyDownloadClass
*klass
= EPHY_DOWNLOAD_GET_CLASS (download
);
178 return klass
->get_source (download
);
182 ephy_download_get_target (EphyDownload
*download
)
184 EphyDownloadClass
*klass
= EPHY_DOWNLOAD_GET_CLASS (download
);
185 return klass
->get_target (download
);
189 ephy_download_get_current_progress (EphyDownload
*download
)
191 EphyDownloadClass
*klass
= EPHY_DOWNLOAD_GET_CLASS (download
);
192 return klass
->get_current_progress (download
);
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
);
210 ephy_download_get_elapsed_time (EphyDownload
*download
)
212 EphyDownloadClass
*klass
= EPHY_DOWNLOAD_GET_CLASS (download
);
213 return klass
->get_elapsed_time (download
);
217 ephy_download_get_state (EphyDownload
*download
)
219 EphyDownloadClass
*klass
= EPHY_DOWNLOAD_GET_CLASS (download
);
220 return klass
->get_state (download
);
224 ephy_download_get_mime (EphyDownload
*download
)
226 EphyDownloadClass
*klass
= EPHY_DOWNLOAD_GET_CLASS (download
);
227 return klass
->get_mime (download
);
231 ephy_download_cancel (EphyDownload
*download
)
233 EphyDownloadClass
*klass
= EPHY_DOWNLOAD_GET_CLASS (download
);
234 klass
->cancel (download
);
238 ephy_download_pause (EphyDownload
*download
)
240 EphyDownloadClass
*klass
= EPHY_DOWNLOAD_GET_CLASS (download
);
241 klass
->pause (download
);
245 ephy_download_resume (EphyDownload
*download
)
247 EphyDownloadClass
*klass
= EPHY_DOWNLOAD_GET_CLASS (download
);
248 klass
->resume (download
);