3 /// Thread class for device manipulation
7 Copyright (C) 2007-2013, 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::vector
<std::string
> Thread::CompareTotals() const
77 if( AutoSelectAll() ) {
78 // compare against full DBDB
79 return m_interface
.CompareTotals(m_interface
.GetDBDB());
82 // compare against current saved backup list
83 return m_interface
.CompareTotals(GetBackupList());
87 std::string
Thread::GetFullname()
89 std::string ret
= GetPIN().Str() + " (" + GetDeviceName() + ")";
93 bool Thread::Connect()
95 password_required
= false;
98 if( !m_interface
.Connect() )
99 return (m_connected
= false);
101 catch( Barry::BadPassword
&bp
) {
102 password_out_of_tries
= bp
.out_of_tries();
103 password_remaining_tries
= bp
.remaining_tries();
104 password_required
= true;
105 return (m_connected
= false);
107 catch( Barry::BadSize
&bs
) {
108 bad_size_error
= std::string("Barry::BadSize caught in Connect: ") + bs
.what();
110 return (m_connected
= false);
112 SetStatus(_("Connected"));
113 return (m_connected
= true);
116 bool Thread::Connect(const std::string
&password
)
119 if( !m_interface
.Password(password
.c_str()) ) {
120 // low level error, not a password failure
121 return (m_connected
= false);
124 catch( Barry::BadPassword
&bp
) {
125 password_out_of_tries
= bp
.out_of_tries();
126 password_remaining_tries
= bp
.remaining_tries();
127 bad_password_error
= bp
.what();
128 return (m_connected
= false);
130 SetStatus(_("Connected"));
131 return (m_connected
= true);
134 void Thread::Disconnect()
138 m_interface
.Disconnect();
139 SetStatus(_("Ready"));
144 void Thread::UnsetActive()
151 bool Thread::Backup(std::string label
)
153 // only start a backup if currently idle
157 // grab copy of backup list
158 Barry::ConfigFile::DBListType list
;
159 if( AutoSelectAll() ) {
160 // set list to full DBDB
161 list
= m_interface
.GetDBDB();
164 // copy our current saved list
165 list
= GetBackupList();
168 m_recordTotal
= m_interface
.GetRecordTotal(list
);
169 m_recordFinished
= 0;
171 bool started
= m_interface
.StartBackup(
172 DeviceInterface::AppComm(&m_signal_progress
,
176 &m_signal_restored_db
),
177 list
, GetPath(), label
);
179 m_thread_state
= THREAD_STATE_BACKUP
;
180 SetStatus(_("Backup..."));
185 bool Thread::Restore(std::string filename
)
187 // only start a restore if currently idle
191 m_recordTotal
= m_interface
.GetRecordTotal(GetRestoreList(), filename
);
192 m_recordFinished
= 0;
194 bool started
= m_interface
.StartRestore(
195 DeviceInterface::AppComm(&m_signal_progress
,
199 &m_signal_restored_db
),
200 GetRestoreList(), filename
);
202 m_thread_state
= THREAD_STATE_RESTORE
;
203 SetStatus(_("Restore..."));
208 void Thread::on_thread_progress()
214 void Thread::on_thread_error()
217 // Do not change state, since backup/restores continue processing
218 // on error... wait for on_thread_done() to change state.
219 //m_thread_state |= THREAD_STATE_IDLE;
221 Gtk::MessageDialog
msg(m_status
+ _(" error: ") + m_interface
.get_last_thread_error());
225 void Thread::on_thread_done()
228 SetStatus(_("Connected"));
231 m_thread_state
|= THREAD_STATE_IDLE
;
232 m_finished_marker
= true;
235 void Thread::on_thread_erase_db()
237 m_erasing_db_name
= m_interface
.GetThreadDBName();
238 std::cerr
<< _("Erasing database: ") << m_erasing_db_name
<< std::endl
;
241 void Thread::on_thread_restored_db()
243 if( m_erasing_db_name
.size() ) {
244 std::cerr
<< _("Restored database: ") << m_erasing_db_name
<< std::endl
;
245 m_erasing_db_name
.clear();