2 /// \file BackupWindow.cc
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"
33 BackupWindow::BackupWindow(BaseObjectType
*cobject
,
34 const Glib::RefPtr
<Gnome::Glade::Xml
> &xml
)
35 : Gtk::Window(cobject
)
38 , m_finishedRecords(0)
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();
113 if( probe
.GetCount() > 1 ) {
114 DeviceSelectDlg
dlg(probe
);
115 if( dlg
.run() == Gtk::RESPONSE_OK
) {
117 nSelection
= probe
.FindActive(pin
);
120 // no selection, exit
125 else if( probe
.GetCount() == 1 ) {
127 pin
= probe
.Get(0).m_pin
;
131 Gtk::MessageDialog
msg("No BlackBerry devices found.");
137 if( nSelection
== -1 ) {
138 Gtk::MessageDialog
msg("Internal error: unable to find pin.");
144 bool out_of_tries
= false, password_required
= false;
145 int remaining_tries
= 0;
147 if( !m_dev
.Connect(probe
.Get(nSelection
)) ) {
148 Gtk::MessageDialog
msg(m_dev
.get_last_error());
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());
170 Gtk::MessageDialog
msg(m_dev
.get_last_error());
182 catch( Barry::BadPassword
&bp
) {
183 out_of_tries
= bp
.out_of_tries();
184 remaining_tries
= bp
.remaining_tries();
186 Gtk::MessageDialog
msg(bp
.what());
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()) );
209 void BackupWindow::SetWorkingMode(const std::string
&taskname
)
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()
222 m_pBackupButton
->set_sensitive(true);
223 m_pRestoreButton
->set_sensitive(true);
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
) {
242 m_pProgressBar
->set_fraction(done
);
244 m_pDatabaseEntry
->set_text(m_dev
.GetThreadDBName());
249 void BackupWindow::signal_exception_handler()
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());
263 // anything else, terminate window and pass on to next handler
264 // (which should be in main.cc)
271 //////////////////////////////////////////////////////////////////////////////
274 void BackupWindow::on_backup()
278 Gtk::MessageDialog
msg("Thread already in progress.");
283 // make sure our target directory exists
284 if( !::CheckPath(m_pConfig
->GetPath()) ) {
285 Gtk::MessageDialog
msg("Could not create directory: " + m_pConfig
->GetPath());
290 // prepare for the progress bar
291 m_recordTotal
= m_dev
.GetDeviceRecordTotal(m_pConfig
->GetBackupList());
292 m_finishedRecords
= 0;
293 m_modeName
= "Backup";
296 if( m_recordTotal
== 0 ) {
297 Gtk::MessageDialog
msg("No databases selected in configuration.");
303 m_working
= m_dev
.StartBackup(
304 DeviceInterface::AppComm(&m_signal_progress
,
308 m_pConfig
->GetBackupList(), m_pConfig
->GetPath(),
309 m_pConfig
->GetPIN());
311 Gtk::MessageDialog
msg("Error starting backup thread: " +
312 m_dev
.get_last_error());
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();
338 if( result
!= Gtk::RESPONSE_OK
)
341 restoreFilename
= dlg
.get_filename();
345 void BackupWindow::on_restore()
349 Gtk::MessageDialog
msg("Thread already in progress.");
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";
363 m_working
= m_dev
.StartRestore(
364 DeviceInterface::AppComm(&m_signal_progress
,
368 m_pConfig
->GetRestoreList(), restoreFilename
, &m_recordTotal
);
369 // m_working = m_dev.StartRestoreAndBackup(
370 // DeviceInterface::AppComm(&m_signal_progress,
373 // &m_signal_erase_db),
374 // m_pConfig->GetRestoreList(), restoreFilename,
375 // m_pConfig->GetPath(), m_pConfig->GetPIN(),
378 Gtk::MessageDialog
msg("Error starting restore thread: " +
379 m_dev
.get_last_error());
383 std::cerr
<< "m_recordTotal for restore: " << m_recordTotal
<< std::endl
;
386 SetWorkingMode("Restore");
389 void BackupWindow::on_file_quit()
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());
409 void BackupWindow::on_help_about()
411 Gtk::AboutDialog dlg
;
412 dlg
.set_copyright("Copyright (C) 2007, Net Direct Inc.");
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"
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"
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
);
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/");
440 bool BackupWindow::on_startup()
449 void BackupWindow::on_thread_progress()
455 void BackupWindow::on_thread_error()
457 m_thread_error
= true;
459 Gtk::MessageDialog
msg(m_modeName
+ " error: " + m_dev
.get_last_thread_error());
463 void BackupWindow::on_thread_done()
465 if( !m_thread_error
) {
466 Gtk::MessageDialog
msg(m_modeName
+ " complete!");
475 void BackupWindow::on_thread_erase_db()
477 std::string name
= m_dev
.GetThreadDBName();
478 m_pDatabaseEntry
->set_text("Erasing database: " + name
);
486 Glib::ustring text = pEntry->get_text();
487 Gtk::MessageDialog dialog("This is the text entered: " + text);
488 // dialog.set_secondary_text(text);
494 // response(Gtk::RESPONSE_CLOSE);
495 // signal_delete_event().emit();
496 // Gtk::Main::quit();