Updated buildgen.sh's clean option to remove gui/config.rpath
[barry.git] / gui / src / Thread.cc
blob793249859ffbf62764ae5ad956abdee7c5c809d3
1 ///
2 /// \file Thread.cc
3 /// Thread class for device manipulation
4 ///
6 /*
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.
23 #include "Thread.h"
24 #include "util.h"
25 #include "i18n.h"
27 void Thread::SetStatus(std::string mode)
29 m_status = mode;
30 m_update->emit();
33 Thread::Thread(Device dev, Glib::Dispatcher *update_signal)
34 : ConfigFile(dev.GetPIN())
35 , m_dev(dev)
36 , m_interface(&m_dev)
37 , m_update(update_signal)
38 , m_status("")
39 , m_recordFinished(0)
40 , m_recordTotal(0)
41 , m_connected(false)
42 , m_error(false)
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()
61 ConfigFile::Load();
62 if( m_connected )
63 Enlighten(m_interface.GetDBDB());
64 m_update->emit();
67 bool Thread::CheckFinishedMarker()
69 if( !m_finished_marker )
70 return false;
71 m_finished_marker = false;
72 return true;
75 std::string Thread::GetFullname()
77 std::string ret = GetPIN().str() + " (" + GetDeviceName() + ")";
78 return ret;
81 bool Thread::Connect()
83 password_required = false;
84 bad_size = false;
85 try {
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();
97 bad_size = true;
98 return (m_connected = false);
100 SetStatus(_("Connected"));
101 return (m_connected = true);
104 bool Thread::Connect(const std::string &password)
106 try {
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()
121 if( m_connected )
123 m_interface.Disconnect();
124 SetStatus(_("Ready"));
125 m_connected = false;
129 void Thread::UnsetActive()
131 m_active = false;
132 if( !Working() )
133 Disconnect();
136 bool Thread::Backup(std::string label)
138 // only start a backup if currently idle
139 if( Working() )
140 return false;
142 m_recordTotal = m_interface.GetRecordTotal(GetBackupList());
143 m_recordFinished = 0;
145 bool started = m_interface.StartBackup(
146 DeviceInterface::AppComm(&m_signal_progress,
147 &m_signal_error,
148 &m_signal_done,
149 &m_signal_erase_db,
150 &m_signal_restored_db),
151 GetBackupList(), GetPath(), label);
152 if( started ) {
153 m_thread_state = THREAD_STATE_BACKUP;
154 SetStatus(_("Backup..."));
156 return started;
159 bool Thread::Restore(std::string filename)
161 // only start a restore if currently idle
162 if( Working() )
163 return false;
165 m_recordTotal = m_interface.GetRecordTotal(GetRestoreList(), filename);
166 m_recordFinished = 0;
168 bool started = m_interface.StartRestore(
169 DeviceInterface::AppComm(&m_signal_progress,
170 &m_signal_error,
171 &m_signal_done,
172 &m_signal_erase_db,
173 &m_signal_restored_db),
174 GetRestoreList(), filename);
175 if( started ) {
176 m_thread_state = THREAD_STATE_RESTORE;
177 SetStatus(_("Restore..."));
179 return started;
182 bool Thread::RestoreAndBackup(std::string filename)
184 // only start a restore if currently idle
185 if( Working() )
186 return false;
188 // not supported
189 return false;
192 m_working = m_interface.StartRestoreAndBackup(
193 DeviceInterface::AppComm(&m_signal_progress,
194 &m_signal_error,
195 &m_signal_done,
196 &m_signal_erase_db),
197 GetRestoreList(), filename,
198 GetPath());
199 if( m_working )
200 SetStatus("Restore & Backup");
201 return m_working;
205 void Thread::on_thread_progress()
207 ++m_recordFinished;
208 m_update->emit();
211 void Thread::on_thread_error()
213 m_error = true;
214 m_thread_state |= THREAD_STATE_IDLE;
216 Gtk::MessageDialog msg(m_status + _(" error: ") + m_interface.get_last_thread_error());
217 msg.run();
220 void Thread::on_thread_done()
222 if( m_active )
223 SetStatus(_("Connected"));
224 else
225 Disconnect();
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();