4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "bcprogressbox.h"
24 #include "mainprogress.h"
26 #include "mwindowgui.h"
27 #include "statusbar.h"
32 MainProgressBar::MainProgressBar(MWindow *mwindow, MainProgress *mainprogress)
36 this->mwindow = mwindow;
37 this->mainprogress = mainprogress;
38 eta_timer = new Timer;
41 MainProgressBar::~MainProgressBar()
43 if(progress_box) delete progress_box;
45 if(mainprogress->mwindow_progress == this)
47 mainprogress->mwindow_progress = 0;
51 mainprogress->progress_bars.remove(this);
56 void MainProgressBar::start()
60 progress_box->start();
66 void MainProgressBar::stop_progress()
70 progress_box->stop_progress();
75 mwindow->gui->lock_window("MainProgressBar::stop_progress");
76 progress_bar->update(0);
77 mwindow->gui->statusbar->default_message();
78 mwindow->gui->unlock_window();
82 int MainProgressBar::is_cancelled()
86 //printf("MainProgressBar::is_cancelled 1 %d\n", progress_box->is_cancelled());
87 return progress_box->is_cancelled();
92 return mainprogress->cancelled;
98 void MainProgressBar::update_title(char *string, int default_)
100 if(default_) strcpy(default_title, string);
101 //printf("MainProgressBar::update_title %p %p %s\n", progress_bar, progress_box, string);
105 progress_box->update_title(string, 1);
110 char text[BCTEXTLEN], *cp = text, *ep = cp+sizeof(text)-1;
111 for( const unsigned char *bp = (const unsigned char *)string; *bp && cp<ep; ++bp )
112 *cp++ = *bp >= ' ' ? *bp : ' ';
114 mwindow->gui->lock_window("MainProgressBar::update_title");
115 mwindow->gui->show_message(text);
116 mwindow->gui->unlock_window();
120 void MainProgressBar::update_length(int64_t length)
122 this->length = length;
123 //printf("MainProgressBar::update_length %d\n", length);
126 progress_box->update_length(length, 1);
131 mwindow->gui->lock_window("MainProgressBar::update_length");
132 progress_bar->update_length(length);
133 mwindow->gui->unlock_window();
137 int MainProgressBar::update(int64_t value)
139 // Print ETA on title
140 double current_eta = (double)eta_timer->get_scaled_difference(1000);
142 //printf("MainProgressBar::update %f %f %f\n", current_eta, last_eta, current_eta - last_eta);
143 if(current_eta - last_eta > 1000)
145 char string[BCTEXTLEN];
146 char time_string[BCTEXTLEN];
150 eta = (double)current_eta /
159 //printf("MainProgressBar::update %f %d %d %f\n", current_eta, length, value, eta);
160 Units::totext(time_string,
163 // sprintf(time_string,
165 // (int64_t)eta / 3600,
166 // ((int64_t)eta / 60) % 60,
167 // (int64_t)eta % 60);
169 sprintf(string, _("%s ETA: %s"),
172 update_title(string, 0);
174 last_eta = (int64_t)current_eta;
179 progress_box->update(value, 1);
184 mwindow->gui->lock_window("MainProgressBar::update");
185 progress_bar->update(value);
186 mwindow->gui->unlock_window();
189 return is_cancelled();
192 void MainProgressBar::get_time(char *text)
194 double current_time = (double)eta_timer->get_scaled_difference(1);
195 //printf("MainProgressBar::get_time %f\n", current_time);
201 double MainProgressBar::get_time()
203 return (double)eta_timer->get_scaled_difference(1);
216 MainProgress::MainProgress(MWindow *mwindow, MWindowGUI *gui)
218 this->mwindow = mwindow;
220 mwindow_progress = 0;
225 MainProgress::~MainProgress()
229 MainProgressBar* MainProgress::start_progress(char *text,
230 int64_t total_length,
233 MainProgressBar *result = 0;
235 // Default to main window
236 if(!mwindow_progress && !use_window)
238 mwindow_progress = new MainProgressBar(mwindow, this);
239 mwindow_progress->progress_bar = gui->statusbar->main_progress;
240 mwindow_progress->progress_bar->update_length(total_length);
241 mwindow_progress->update_title(text);
242 result = mwindow_progress;
248 result = new MainProgressBar(mwindow, this);
249 progress_bars.append(result);
250 result->progress_box = new BC_ProgressBox(gui->get_abs_cursor_x(1),
251 gui->get_abs_cursor_y(1),
256 result->length = total_length;
257 strcpy(result->default_title, text);
263 void MainProgress::end_progress(MainProgressBar* progress_bar)
265 if(mwindow_progress == progress_bar)
267 delete mwindow_progress;
268 mwindow_progress = 0;
273 progress_bars.remove(progress_bar);