2 /// \file DeviceIface.h
3 /// Interface class for device backup and restore
7 Copyright (C) 2007-2009, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #ifndef __BARRYBACKUP_DEVICEIFACE_H__
23 #define __BARRYBACKUP_DEVICEIFACE_H__
25 #include <barry/barry.h>
29 #include "ConfigFile.h"
32 #define DI_THREAD_DONE 100
33 #define DI_THREAD_PROGRESS 101
42 Barry::ProbeResult result
;
46 Device(Barry::ProbeResult
);
48 Barry::Pin
GetPIN() const { return Barry::Pin(result
.m_pin
); };
50 friend class DeviceInterface
;
53 class DeviceInterface
:
58 struct AppComm
// app communication
60 Glib::Dispatcher
*m_erase_db
; // to notify the app about the
61 // db erase stage of restore
62 Glib::Dispatcher
*m_progress
;
63 Glib::Dispatcher
*m_error
;
64 Glib::Dispatcher
*m_done
;
72 AppComm(Glib::Dispatcher
*progress
,
73 Glib::Dispatcher
*error
,
74 Glib::Dispatcher
*done
,
75 Glib::Dispatcher
*erase_db
) :
82 { return m_erase_db
&& m_progress
&& m_error
&& m_done
; }
84 { m_erase_db
= m_progress
= m_error
= m_done
= 0; }
87 class Quit
// quit exception to break out of upload/download
93 Barry::Controller
*m_con
;
94 Barry::Mode::Desktop
*m_desktop
;
95 std::string m_last_error
;
96 std::string m_last_thread_error
;
99 std::auto_ptr
<reuse::TarFile
> m_tar
, m_tarback
;
101 // parser and builder data (only one side uses these at a time)
102 ConfigFile::DBListType m_dbList
;
103 mutable Glib::Mutex
*m_dbnameMutex
;
104 std::string m_current_dbname_not_thread_safe
;
105 std::string m_current_dbname
;
107 uint32_t m_unique_id
;
108 std::string m_tar_id_text
;
109 std::string m_record_data
;
111 bool m_tar_record_loaded
;
113 // thread quit flag... not locked, only a byte
114 volatile bool m_thread_quit
;
117 bool False(const std::string
&msg
);
121 void RestoreThread();
122 void RestoreAndBackupThread();
125 std::string
MakeFilename(const std::string
&label
= "") const;
126 int CountFiles(reuse::TarFile
&tar
, const ConfigFile::DBListType
&restoreList
) const;
127 bool SplitTarPath(const std::string
&tarpath
, std::string
&dbname
,
128 std::string
&dbid_text
, uint8_t &dbrectype
, uint32_t &dbid
) const;
130 // Sets the name of the database the thread is currently working on
131 void SetThreadDBName(const std::string
&dbname
);
134 DeviceInterface(Device
*dev
= 0);
137 const std::string
& get_last_error() const { return m_last_error
; }
138 const std::string
& get_last_thread_error() const { return m_last_thread_error
; }
142 bool Password(const char *password
);
145 const Barry::DatabaseDatabase
& GetDBDB() const { return m_desktop
->GetDBDB(); }
146 unsigned int GetRecordTotal(const ConfigFile::DBListType
&backupList
) const;
147 unsigned int GetRecordTotal(const ConfigFile::DBListType
&restoreList
, const std::string
&filename
) const;
149 void QuitThread() { m_thread_quit
= true; }
151 /// returns name of database the thread is currently working on
152 std::string
GetThreadDBName() const;
154 bool StartBackup(AppComm comm
,
155 const ConfigFile::DBListType
&backupList
,
156 const std::string
&directory
, const std::string
&backupLabel
);
157 bool StartRestore(AppComm comm
,
158 const ConfigFile::DBListType
&restoreList
,
159 const std::string
&tarfilename
);
160 // this is for debugging... starts a restore, and then does an
161 // immediate backup of the same DB before moving on to the next
162 bool StartRestoreAndBackup(AppComm comm
,
163 const ConfigFile::DBListType
&restoreAndBackupList
,
164 const std::string
&tarfilename
,
165 const std::string
&directory
);
167 // Barry::Parser overrides
168 virtual void Clear();
169 virtual void SetIds(uint8_t RecType
, uint32_t UniqueId
);
170 virtual void ParseHeader(const Barry::Data
&data
, size_t &offset
);
171 virtual void ParseFields(const Barry::Data
&data
, size_t &offset
,
172 const Barry::IConverter
*ic
);
173 virtual void Store();
175 // Barry::Builder overrides
176 virtual bool Retrieve(unsigned int dbId
);
177 virtual uint8_t GetRecType() const;
178 virtual uint32_t GetUniqueId() const;
179 virtual void BuildHeader(Barry::Data
&data
, size_t &offset
);
180 virtual void BuildFields(Barry::Data
&data
, size_t &offset
, const Barry::IConverter
*ic
);
181 void SkipCurrentDB() throw(); // helper function for halding restore errors