3 /// Thread class for device manipulation
7 Copyright (C) 2007-2012, 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 if( !m_interface
.Password(password
.c_str()) ) {
108 // low level error, not a password failure
109 return (m_connected
= false);
112 catch( Barry::BadPassword
&bp
) {
113 password_out_of_tries
= bp
.out_of_tries();
114 password_remaining_tries
= bp
.remaining_tries();
115 bad_password_error
= bp
.what();
116 return (m_connected
= false);
118 SetStatus(_("Connected"));
119 return (m_connected
= true);
122 void Thread::Disconnect()
126 m_interface
.Disconnect();
127 SetStatus(_("Ready"));
132 void Thread::UnsetActive()
139 bool Thread::Backup(std::string label
)
141 // only start a backup if currently idle
145 m_recordTotal
= m_interface
.GetRecordTotal(GetBackupList());
146 m_recordFinished
= 0;
148 bool started
= m_interface
.StartBackup(
149 DeviceInterface::AppComm(&m_signal_progress
,
153 &m_signal_restored_db
),
154 GetBackupList(), GetPath(), label
);
156 m_thread_state
= THREAD_STATE_BACKUP
;
157 SetStatus(_("Backup..."));
162 bool Thread::Restore(std::string filename
)
164 // only start a restore if currently idle
168 m_recordTotal
= m_interface
.GetRecordTotal(GetRestoreList(), filename
);
169 m_recordFinished
= 0;
171 bool started
= m_interface
.StartRestore(
172 DeviceInterface::AppComm(&m_signal_progress
,
176 &m_signal_restored_db
),
177 GetRestoreList(), filename
);
179 m_thread_state
= THREAD_STATE_RESTORE
;
180 SetStatus(_("Restore..."));
185 void Thread::on_thread_progress()
191 void Thread::on_thread_error()
194 m_thread_state
|= THREAD_STATE_IDLE
;
196 Gtk::MessageDialog
msg(m_status
+ _(" error: ") + m_interface
.get_last_thread_error());
200 void Thread::on_thread_done()
203 SetStatus(_("Connected"));
206 m_thread_state
|= THREAD_STATE_IDLE
;
207 m_finished_marker
= true;
210 void Thread::on_thread_erase_db()
212 m_erasing_db_name
= m_interface
.GetThreadDBName();
213 std::cerr
<< _("Erasing database: ") << m_erasing_db_name
<< std::endl
;
216 void Thread::on_thread_restored_db()
218 if( m_erasing_db_name
.size() ) {
219 std::cerr
<< _("Restored database: ") << m_erasing_db_name
<< std::endl
;
220 m_erasing_db_name
.clear();