2 /// \file DeviceIface.h
3 /// Interface class for device backup and restore
7 Copyright (C) 2007-2010, 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>
31 #define DI_THREAD_DONE 100
32 #define DI_THREAD_PROGRESS 101
41 Barry::ProbeResult result
;
45 Device(Barry::ProbeResult
);
47 Barry::Pin
GetPIN() const { return Barry::Pin(result
.m_pin
); };
49 friend class DeviceInterface
;
52 class DeviceInterface
:
57 struct AppComm
// app communication
59 Glib::Dispatcher
*m_erase_db
; // to notify the app about the
60 // db erase stage of restore
61 Glib::Dispatcher
*m_restored_db
;// to notify the app that the
62 // previous erase_db was
63 // restored... do not rely
64 // on the current db name ==
65 // the name found at erase time
66 Glib::Dispatcher
*m_progress
;
67 Glib::Dispatcher
*m_error
;
68 Glib::Dispatcher
*m_done
;
77 AppComm(Glib::Dispatcher
*progress
,
78 Glib::Dispatcher
*error
,
79 Glib::Dispatcher
*done
,
80 Glib::Dispatcher
*erase_db
,
81 Glib::Dispatcher
*restored_db
) :
83 m_restored_db(restored_db
),
89 { return m_erase_db
&& m_restored_db
&& m_progress
&& m_error
&& m_done
; }
91 { m_erase_db
= m_restored_db
= m_progress
= m_error
= m_done
= 0; }
94 class Quit
// quit exception to break out of upload/download
100 Barry::Controller
*m_con
;
101 Barry::Mode::Desktop
*m_desktop
;
102 std::string m_last_error
;
103 std::string m_last_thread_error
;
106 std::auto_ptr
<reuse::TarFile
> m_tar
, m_tarback
;
108 // parser and builder data (only one side uses these at a time)
109 Barry::ConfigFile::DBListType m_dbList
;
110 mutable Glib::Mutex
*m_dbnameMutex
;
111 std::string m_current_dbname_not_thread_safe
;
112 std::string m_current_dbname
;
114 uint32_t m_unique_id
;
115 std::string m_tar_id_text
;
116 std::string m_record_data
;
118 bool m_tar_record_loaded
;
120 // thread quit flag... not locked, only a byte
121 volatile bool m_thread_quit
;
124 bool False(const std::string
&msg
);
128 void RestoreThread();
129 void RestoreAndBackupThread();
132 std::string
MakeFilename(const std::string
&label
= "") const;
133 int CountFiles(reuse::TarFile
&tar
, const Barry::ConfigFile::DBListType
&restoreList
) const;
134 bool SplitTarPath(const std::string
&tarpath
, std::string
&dbname
,
135 std::string
&dbid_text
, uint8_t &dbrectype
, uint32_t &dbid
) const;
137 // Sets the name of the database the thread is currently working on
138 void SetThreadDBName(const std::string
&dbname
);
141 DeviceInterface(Device
*dev
= 0);
144 const std::string
& get_last_error() const { return m_last_error
; }
145 const std::string
& get_last_thread_error() const { return m_last_thread_error
; }
149 bool Password(const char *password
);
152 const Barry::DatabaseDatabase
& GetDBDB() const { return m_desktop
->GetDBDB(); }
153 unsigned int GetRecordTotal(const Barry::ConfigFile::DBListType
&backupList
) const;
154 unsigned int GetRecordTotal(const Barry::ConfigFile::DBListType
&restoreList
, const std::string
&filename
) const;
156 void QuitThread() { m_thread_quit
= true; }
158 /// returns name of database the thread is currently working on
159 std::string
GetThreadDBName() const;
161 bool StartBackup(AppComm comm
,
162 const Barry::ConfigFile::DBListType
&backupList
,
163 const std::string
&directory
, const std::string
&backupLabel
);
164 bool StartRestore(AppComm comm
,
165 const Barry::ConfigFile::DBListType
&restoreList
,
166 const std::string
&tarfilename
);
167 // this is for debugging... starts a restore, and then does an
168 // immediate backup of the same DB before moving on to the next
169 bool StartRestoreAndBackup(AppComm comm
,
170 const Barry::ConfigFile::DBListType
&restoreAndBackupList
,
171 const std::string
&tarfilename
,
172 const std::string
&directory
);
174 // Barry::Parser overrides
175 virtual void Clear();
176 virtual void SetIds(uint8_t RecType
, uint32_t UniqueId
);
177 virtual void ParseHeader(const Barry::Data
&data
, size_t &offset
);
178 virtual void ParseFields(const Barry::Data
&data
, size_t &offset
,
179 const Barry::IConverter
*ic
);
180 virtual void Store();
182 // Barry::Builder overrides
183 virtual bool Retrieve(unsigned int dbId
);
184 virtual uint8_t GetRecType() const;
185 virtual uint32_t GetUniqueId() const;
186 virtual void BuildHeader(Barry::Data
&data
, size_t &offset
);
187 virtual void BuildFields(Barry::Data
&data
, size_t &offset
, const Barry::IConverter
*ic
);
188 void SkipCurrentDB() throw(); // helper function for halding restore errors