3 /// Thread class for device manipulation
7 Copyright (C) 2007-2010, Net Direct Inc. (http://www.netdirect.ca/)
8 Copyright (C) 2009, Ryan Li (ryan@ryanium.com)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 See the GNU General Public License in the COPYING file at the
20 root directory of this project for more details.
27 void Thread::SetStatus(std::string mode
)
33 Thread::Thread(Device dev
, Glib::Dispatcher
*update_signal
)
34 : ConfigFile(dev
.GetPIN())
37 , m_update(update_signal
)
43 , m_thread_state(THREAD_STATE_IDLE
)
45 m_signal_progress
.connect(
46 sigc::mem_fun(*this, &Thread::on_thread_progress
));
47 m_signal_error
.connect(
48 sigc::mem_fun(*this, &Thread::on_thread_error
));
49 m_signal_done
.connect(
50 sigc::mem_fun(*this, &Thread::on_thread_done
));
51 m_signal_erase_db
.connect(
52 sigc::mem_fun(*this, &Thread::on_thread_erase_db
));
53 m_signal_restored_db
.connect(
54 sigc::mem_fun(*this, &Thread::on_thread_restored_db
));
56 SetStatus(_("Ready"));
59 void Thread::LoadConfig()
63 Enlighten(m_interface
.GetDBDB());
67 bool Thread::CheckFinishedMarker()
69 if( !m_finished_marker
)
71 m_finished_marker
= false;
75 std::string
Thread::GetFullname()
77 std::string ret
= GetPIN().str() + " (" + GetDeviceName() + ")";
81 bool Thread::Connect()
83 password_required
= false;
86 if( !m_interface
.Connect() )
87 return (m_connected
= false);
89 catch( Barry::BadPassword
&bp
) {
90 password_out_of_tries
= bp
.out_of_tries();
91 password_remaining_tries
= bp
.remaining_tries();
92 password_required
= true;
93 return (m_connected
= false);
95 catch( Barry::BadSize
&bs
) {
96 bad_size_error
= std::string("Barry::BadSize caught in Connect: ") + bs
.what();
98 return (m_connected
= false);
100 SetStatus(_("Connected"));
101 return (m_connected
= true);
104 bool Thread::Connect(const std::string
&password
)
107 m_interface
.Password(password
.c_str());
109 catch( Barry::BadPassword
&bp
) {
110 password_out_of_tries
= bp
.out_of_tries();
111 password_remaining_tries
= bp
.remaining_tries();
112 bad_password_error
= bp
.what();
113 return (m_connected
= false);
115 SetStatus(_("Connected"));
116 return (m_connected
= true);
119 void Thread::Disconnect()
123 m_interface
.Disconnect();
124 SetStatus(_("Ready"));
129 void Thread::UnsetActive()
136 bool Thread::Backup(std::string label
)
138 // only start a backup if currently idle
142 m_recordTotal
= m_interface
.GetRecordTotal(GetBackupList());
143 m_recordFinished
= 0;
145 bool started
= m_interface
.StartBackup(
146 DeviceInterface::AppComm(&m_signal_progress
,
150 &m_signal_restored_db
),
151 GetBackupList(), GetPath(), label
);
153 m_thread_state
= THREAD_STATE_BACKUP
;
154 SetStatus(_("Backup..."));
159 bool Thread::Restore(std::string filename
)
161 // only start a restore if currently idle
165 m_recordTotal
= m_interface
.GetRecordTotal(GetRestoreList(), filename
);
166 m_recordFinished
= 0;
168 bool started
= m_interface
.StartRestore(
169 DeviceInterface::AppComm(&m_signal_progress
,
173 &m_signal_restored_db
),
174 GetRestoreList(), filename
);
176 m_thread_state
= THREAD_STATE_RESTORE
;
177 SetStatus(_("Restore..."));
182 bool Thread::RestoreAndBackup(std::string filename
)
184 // only start a restore if currently idle
192 m_working = m_interface.StartRestoreAndBackup(
193 DeviceInterface::AppComm(&m_signal_progress,
197 GetRestoreList(), filename,
200 SetStatus("Restore & Backup");
205 void Thread::on_thread_progress()
211 void Thread::on_thread_error()
214 m_thread_state
|= THREAD_STATE_IDLE
;
216 Gtk::MessageDialog
msg(m_status
+ _(" error: ") + m_interface
.get_last_thread_error());
220 void Thread::on_thread_done()
223 SetStatus(_("Connected"));
226 m_thread_state
|= THREAD_STATE_IDLE
;
227 m_finished_marker
= true;
230 void Thread::on_thread_erase_db()
232 m_erasing_db_name
= m_interface
.GetThreadDBName();
233 std::cerr
<< _("Erasing database: ") << m_erasing_db_name
<< std::endl
;
236 void Thread::on_thread_restored_db()
238 if( m_erasing_db_name
.size() ) {
239 std::cerr
<< _("Restored database: ") << m_erasing_db_name
<< std::endl
;
240 m_erasing_db_name
.clear();