- minor wording change in backup GUI prompt: "Backup working..." to
[barry.git] / gui / src / BackupWindow.cc
blobed31c4f88f189f6b1edefbe97584f8317714f39c
1 ///
2 /// \file BackupWindow.cc
3 /// GUI window class
4 ///
6 /*
7 Copyright (C) 2007, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #include "BackupWindow.h"
23 #include "DeviceSelectDlg.h"
24 #include "PasswordDlg.h"
25 #include "ConfigDlg.h"
26 #include "aboutdialog.h"
27 #include "util.h"
28 #include <iostream>
29 #include <iomanip>
30 #include <sstream>
31 #include <unistd.h>
33 BackupWindow::BackupWindow(BaseObjectType *cobject,
34 const Glib::RefPtr<Gnome::Glade::Xml> &xml)
35 : Gtk::Window(cobject)
36 , m_xml(xml)
37 , m_recordTotal(0)
38 , m_finishedRecords(0)
39 , m_pProgressBar(0)
40 , m_pStatusBar(0)
41 , m_pBackupButton(0)
42 , m_pRestoreButton(0)
43 , m_scanned(false)
44 , m_working(false)
46 // setup menu signals
47 Gtk::MenuItem *pItem = 0;
48 m_xml->get_widget("menu_file_quit", pItem);
49 pItem->signal_activate().connect(
50 sigc::mem_fun(*this, &BackupWindow::on_file_quit));
52 m_xml->get_widget("menu_edit_config", pItem);
53 pItem->signal_activate().connect(
54 sigc::mem_fun(*this, &BackupWindow::on_edit_config));
56 m_xml->get_widget("menu_help_about", pItem);
57 pItem->signal_activate().connect(
58 sigc::mem_fun(*this, &BackupWindow::on_help_about));
60 // get various widget pointers we will use later
61 m_xml->get_widget("BackupButton", m_pBackupButton);
62 m_xml->get_widget("RestoreButton", m_pRestoreButton);
63 m_xml->get_widget("progressbar1", m_pProgressBar);
64 m_xml->get_widget("statusbar1", m_pStatusBar);
65 m_xml->get_widget("entry1", m_pPINEntry);
66 m_xml->get_widget("entry2", m_pDatabaseEntry);
68 // setup widget signals
69 m_pBackupButton->signal_clicked().connect(
70 sigc::mem_fun(*this, &BackupWindow::on_backup));
71 m_pRestoreButton->signal_clicked().connect(
72 sigc::mem_fun(*this, &BackupWindow::on_restore));
74 // setup thread dispatcher signals
75 m_signal_progress.connect(
76 sigc::mem_fun(*this, &BackupWindow::on_thread_progress));
77 m_signal_error.connect(
78 sigc::mem_fun(*this, &BackupWindow::on_thread_error));
79 m_signal_done.connect(
80 sigc::mem_fun(*this, &BackupWindow::on_thread_done));
81 m_signal_erase_db.connect(
82 sigc::mem_fun(*this, &BackupWindow::on_thread_erase_db));
84 // setup startup device scan
85 Glib::signal_timeout().connect(
86 sigc::mem_fun(*this, &BackupWindow::on_startup), 500);
88 m_pStatusBar->push("Ready");
89 m_pProgressBar->set_fraction(0.00);
91 // do this last so that any exceptions in the constructor
92 // won't cause a connected signal handler to a non-object
93 // (i.e. ~BackupWindow() won't get called if constructor throws)
94 m_signal_handler_connection = Glib::add_exception_handler(
95 sigc::mem_fun(*this, &BackupWindow::signal_exception_handler) );
98 BackupWindow::~BackupWindow()
100 // disconnect the signal, as we're going out of business
101 m_signal_handler_connection.disconnect();
104 void BackupWindow::ScanAndConnect()
106 m_pStatusBar->push("Scanning for devices...");
107 m_pStatusBar->show_now();
109 Barry::Probe probe;
110 uint32_t pin = 0;
111 int nSelection = -1;
113 if( probe.GetCount() > 1 ) {
114 DeviceSelectDlg dlg(probe);
115 if( dlg.run() == Gtk::RESPONSE_OK ) {
116 pin = dlg.GetPIN();
117 nSelection = probe.FindActive(pin);
119 else {
120 // no selection, exit
121 hide();
122 return;
125 else if( probe.GetCount() == 1 ) {
126 // default to first
127 pin = probe.Get(0).m_pin;
128 nSelection = 0;
130 else {
131 Gtk::MessageDialog msg("No BlackBerry devices found.");
132 msg.run();
133 hide();
134 return;
137 if( nSelection == -1 ) {
138 Gtk::MessageDialog msg("Internal error: unable to find pin.");
139 msg.run();
140 hide();
141 return;
144 bool out_of_tries = false, password_required = false;
145 int remaining_tries = 0;
146 try {
147 if( !m_dev.Connect(probe.Get(nSelection)) ) {
148 Gtk::MessageDialog msg(m_dev.get_last_error());
149 msg.run();
150 hide();
151 return;
154 catch( Barry::BadPassword &bp ) {
155 out_of_tries = bp.out_of_tries();
156 remaining_tries = bp.remaining_tries();
157 password_required = true;
160 if( password_required ) {
161 // try password repeatedly until out of tries or
162 // the user cancels... or success :-)
164 bool connected = false;
165 while( !connected && !out_of_tries ) try {
166 PasswordDlg dlg(remaining_tries);
167 if( dlg.run() == Gtk::RESPONSE_OK ) {
168 connected = m_dev.Password(dlg.GetPassword());
169 if( !connected ) {
170 Gtk::MessageDialog msg(m_dev.get_last_error());
171 msg.run();
172 hide();
173 return;
176 else {
177 // user cancelled
178 hide();
179 return;
182 catch( Barry::BadPassword &bp ) {
183 out_of_tries = bp.out_of_tries();
184 remaining_tries = bp.remaining_tries();
185 if( out_of_tries ) {
186 Gtk::MessageDialog msg(bp.what());
187 msg.run();
188 hide();
189 return;
193 if( !connected ) {
194 hide();
195 return;
199 std::ostringstream oss;
200 oss << std::hex << pin;
201 m_pPINEntry->set_text(oss.str());
203 // open configuration now that we know which device we're talking to
204 m_pConfig.reset( new ConfigFile(oss.str(), m_dev.GetDBDB()) );
206 m_pStatusBar->pop();
209 void BackupWindow::SetWorkingMode(const std::string &taskname)
211 m_working = true;
212 m_thread_error = false;
213 m_pBackupButton->set_sensitive(false);
214 m_pRestoreButton->set_sensitive(false);
215 m_pStatusBar->push(taskname + " in progress...");
216 m_pProgressBar->set_fraction(0.00);
219 void BackupWindow::ClearWorkingMode()
221 m_working = false;
222 m_pBackupButton->set_sensitive(true);
223 m_pRestoreButton->set_sensitive(true);
224 m_pStatusBar->pop();
225 if( m_finishedRecords >= m_recordTotal ) {
226 // only reset the progress bar on success
227 m_pProgressBar->set_fraction(0.00);
230 std::ostringstream oss;
231 oss << m_finishedRecords << " total records processed.";
232 m_pDatabaseEntry->set_text(oss.str());
235 void BackupWindow::UpdateProgress()
237 double done = (double)m_finishedRecords / m_recordTotal;
238 // never say 100% unless really done
239 if( done >= 1.0 && m_finishedRecords < m_recordTotal ) {
240 done = 0.99;
242 m_pProgressBar->set_fraction(done);
244 m_pDatabaseEntry->set_text(m_dev.GetThreadDBName());
249 void BackupWindow::signal_exception_handler()
251 try {
252 throw;
254 catch( Glib::Exception &e ) {
255 // This usually just means a missing .glade file,
256 // so we try to carry on.
257 std::cerr << "Glib::Exception caught in main: " << std::endl;
258 std::cerr << e.what() << std::endl;
259 Gtk::MessageDialog msg(e.what());
260 msg.run();
262 catch( ... ) {
263 // anything else, terminate window and pass on to next handler
264 // (which should be in main.cc)
265 hide();
266 throw;
271 //////////////////////////////////////////////////////////////////////////////
272 // signal handlers
274 void BackupWindow::on_backup()
276 // already working?
277 if( m_working ) {
278 Gtk::MessageDialog msg("Thread already in progress.");
279 msg.run();
280 return;
283 // make sure our target directory exists
284 if( !::CheckPath(m_pConfig->GetPath()) ) {
285 Gtk::MessageDialog msg("Could not create directory: " + m_pConfig->GetPath());
286 msg.run();
287 return;
290 // prepare for the progress bar
291 m_recordTotal = m_dev.GetDeviceRecordTotal(m_pConfig->GetBackupList());
292 m_finishedRecords = 0;
293 m_modeName = "Backup";
295 // anything to do?
296 if( m_recordTotal == 0 ) {
297 Gtk::MessageDialog msg("No databases selected in configuration.");
298 msg.run();
299 return;
302 // start the thread
303 m_working = m_dev.StartBackup(
304 DeviceInterface::AppComm(&m_signal_progress,
305 &m_signal_error,
306 &m_signal_done,
307 &m_signal_erase_db),
308 m_pConfig->GetBackupList(), m_pConfig->GetPath(),
309 m_pConfig->GetPIN());
310 if( !m_working ) {
311 Gtk::MessageDialog msg("Error starting backup thread: " +
312 m_dev.get_last_error());
313 msg.run();
316 // update the GUI
317 SetWorkingMode("Backup");
320 bool BackupWindow::PromptForRestoreTarball(std::string &restoreFilename,
321 const std::string &start_path)
323 char buffer[PATH_MAX];
324 char *buf = getcwd(buffer, PATH_MAX);
326 // start at the base path given... if it fails, just open
327 // the dialog where we are
328 chdir(start_path.c_str());
330 Gtk::FileChooserDialog dlg(*this, "Select backup to restore from");
331 dlg.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
332 dlg.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
333 int result = dlg.run();
335 if( buf )
336 chdir(buf);
338 if( result != Gtk::RESPONSE_OK )
339 return false;
341 restoreFilename = dlg.get_filename();
342 return true;
345 void BackupWindow::on_restore()
347 // already working?
348 if( m_working ) {
349 Gtk::MessageDialog msg("Thread already in progress.");
350 msg.run();
351 return;
354 std::string restoreFilename;
355 if( !PromptForRestoreTarball(restoreFilename, m_pConfig->GetPath()) )
356 return; // nothing to do
358 // prepare for the progress bar
359 m_finishedRecords = 0;
360 m_modeName = "Restore";
362 // start the thread
363 m_working = m_dev.StartRestore(
364 DeviceInterface::AppComm(&m_signal_progress,
365 &m_signal_error,
366 &m_signal_done,
367 &m_signal_erase_db),
368 m_pConfig->GetRestoreList(), restoreFilename, &m_recordTotal);
369 // m_working = m_dev.StartRestoreAndBackup(
370 // DeviceInterface::AppComm(&m_signal_progress,
371 // &m_signal_error,
372 // &m_signal_done,
373 // &m_signal_erase_db),
374 // m_pConfig->GetRestoreList(), restoreFilename,
375 // m_pConfig->GetPath(), m_pConfig->GetPIN(),
376 // &m_recordTotal);
377 if( !m_working ) {
378 Gtk::MessageDialog msg("Error starting restore thread: " +
379 m_dev.get_last_error());
380 msg.run();
383 std::cerr << "m_recordTotal for restore: " << m_recordTotal << std::endl;
385 // update the GUI
386 SetWorkingMode("Restore");
389 void BackupWindow::on_file_quit()
391 m_dev.Disconnect();
392 hide();
395 void BackupWindow::on_edit_config()
397 ConfigDlg dlg(m_dev.GetDBDB(), *m_pConfig);
398 if( dlg.run() == Gtk::RESPONSE_OK ) {
399 m_pConfig->SetBackupList(dlg.GetBackupList());
400 m_pConfig->SetRestoreList(dlg.GetRestoreList());
401 if( !m_pConfig->Save() ) {
402 Gtk::MessageDialog msg("Error saving config: " +
403 m_pConfig->get_last_error());
404 msg.run();
409 void BackupWindow::on_help_about()
411 Gtk::AboutDialog dlg;
412 dlg.set_copyright("Copyright (C) 2007, Net Direct Inc.");
413 dlg.set_license(
414 " This program is free software; you can redistribute it and/or modify\n"
415 " it under the terms of the GNU General Public License as published by\n"
416 " the Free Software Foundation; either version 2 of the License, or\n"
417 " (at your option) any later version.\n"
418 "\n"
419 " This program is distributed in the hope that it will be useful,\n"
420 " but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
421 " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
422 "\n"
423 " See the GNU General Public License in the COPYING file at the\n"
424 " root directory of this project for more details.\n");
426 std::vector<std::string> authors;
427 authors.push_back("Chris Frey <cdfrey@foursquare.net>");
429 dlg.set_authors(authors);
431 int major, minor;
432 const char *BarryVersion = Barry::Version(major, minor);
433 dlg.set_name("Barry Backup");
434 dlg.set_version("0.8");
435 dlg.set_comments(std::string("Using library: ") + BarryVersion);
436 dlg.set_website("http://www.netdirect.ca/software/packages/barry/");
437 dlg.run();
440 bool BackupWindow::on_startup()
442 if( !m_scanned ) {
443 ScanAndConnect();
444 m_scanned = true;
446 return false;
449 void BackupWindow::on_thread_progress()
451 m_finishedRecords++;
452 UpdateProgress();
455 void BackupWindow::on_thread_error()
457 m_thread_error = true;
459 Gtk::MessageDialog msg(m_modeName + " error: " + m_dev.get_last_thread_error());
460 msg.run();
463 void BackupWindow::on_thread_done()
465 if( !m_thread_error ) {
466 Gtk::MessageDialog msg(m_modeName + " complete!");
467 msg.run();
470 // done!
471 ClearWorkingMode();
472 m_working = false;
475 void BackupWindow::on_thread_erase_db()
477 std::string name = m_dev.GetThreadDBName();
478 m_pDatabaseEntry->set_text("Erasing database: " + name);
484 void on_showtext()
486 Glib::ustring text = pEntry->get_text();
487 Gtk::MessageDialog dialog("This is the text entered: " + text);
488 // dialog.set_secondary_text(text);
489 dialog.run();
492 void on_close()
494 // response(Gtk::RESPONSE_CLOSE);
495 // signal_delete_event().emit();
496 // Gtk::Main::quit();
497 hide();