Bumped copyright dates to 2012
[barry/progweb.git] / gui / src / Thread.cc
blobf2ce583dc59e2f79af685202aba918df6d1d9feb
1 ///
2 /// \file Thread.cc
3 /// Thread class for device manipulation
4 ///
6 /*
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.
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 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()
124 if( m_connected )
126 m_interface.Disconnect();
127 SetStatus(_("Ready"));
128 m_connected = false;
132 void Thread::UnsetActive()
134 m_active = false;
135 if( !Working() )
136 Disconnect();
139 bool Thread::Backup(std::string label)
141 // only start a backup if currently idle
142 if( Working() )
143 return false;
145 m_recordTotal = m_interface.GetRecordTotal(GetBackupList());
146 m_recordFinished = 0;
148 bool started = m_interface.StartBackup(
149 DeviceInterface::AppComm(&m_signal_progress,
150 &m_signal_error,
151 &m_signal_done,
152 &m_signal_erase_db,
153 &m_signal_restored_db),
154 GetBackupList(), GetPath(), label);
155 if( started ) {
156 m_thread_state = THREAD_STATE_BACKUP;
157 SetStatus(_("Backup..."));
159 return started;
162 bool Thread::Restore(std::string filename)
164 // only start a restore if currently idle
165 if( Working() )
166 return false;
168 m_recordTotal = m_interface.GetRecordTotal(GetRestoreList(), filename);
169 m_recordFinished = 0;
171 bool started = m_interface.StartRestore(
172 DeviceInterface::AppComm(&m_signal_progress,
173 &m_signal_error,
174 &m_signal_done,
175 &m_signal_erase_db,
176 &m_signal_restored_db),
177 GetRestoreList(), filename);
178 if( started ) {
179 m_thread_state = THREAD_STATE_RESTORE;
180 SetStatus(_("Restore..."));
182 return started;
185 void Thread::on_thread_progress()
187 ++m_recordFinished;
188 m_update->emit();
191 void Thread::on_thread_error()
193 m_error = true;
194 m_thread_state |= THREAD_STATE_IDLE;
196 Gtk::MessageDialog msg(m_status + _(" error: ") + m_interface.get_last_thread_error());
197 msg.run();
200 void Thread::on_thread_done()
202 if( m_active )
203 SetStatus(_("Connected"));
204 else
205 Disconnect();
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();