lib: moved GUI's Pin class into the library
[barry.git] / gui / src / DeviceIface.h
blob723e9bca3bba0edd1e81c7ef29b46f694f32cb0a
1 ///
2 /// \file DeviceIface.h
3 /// Interface class for device backup and restore
4 ///
6 /*
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>
26 #include <string>
27 #include <memory>
28 #include <stdint.h>
29 #include "ConfigFile.h"
30 #include "tarfile.h"
32 #define DI_THREAD_DONE 100
33 #define DI_THREAD_PROGRESS 101
35 namespace Glib {
36 class Dispatcher;
37 class Mutex;
40 class Device
42 Barry::ProbeResult result;
44 public:
45 Device();
46 Device(Barry::ProbeResult);
48 Barry::Pin GetPIN() const { return Barry::Pin(result.m_pin); };
50 friend class DeviceInterface;
53 class DeviceInterface :
54 public Barry::Parser,
55 public Barry::Builder
57 public:
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;
66 AppComm() :
67 m_erase_db(0),
68 m_progress(0),
69 m_error(0),
70 m_done(0)
72 AppComm(Glib::Dispatcher *progress,
73 Glib::Dispatcher *error,
74 Glib::Dispatcher *done,
75 Glib::Dispatcher *erase_db) :
76 m_erase_db(erase_db),
77 m_progress(progress),
78 m_error(error),
79 m_done(done)
81 bool IsValid() const
82 { return m_erase_db && m_progress && m_error && m_done; }
83 void Invalidate()
84 { m_erase_db = m_progress = m_error = m_done = 0; }
87 class Quit // quit exception to break out of upload/download
91 private:
92 Device *m_dev;
93 Barry::Controller *m_con;
94 Barry::Mode::Desktop *m_desktop;
95 std::string m_last_error;
96 std::string m_last_thread_error;
98 AppComm m_AppComm;
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;
106 uint8_t m_rec_type;
107 uint32_t m_unique_id;
108 std::string m_tar_id_text;
109 std::string m_record_data;
110 bool m_end_of_tar;
111 bool m_tar_record_loaded;
113 // thread quit flag... not locked, only a byte
114 volatile bool m_thread_quit;
116 protected:
117 bool False(const std::string &msg);
119 // threads
120 void BackupThread();
121 void RestoreThread();
122 void RestoreAndBackupThread();
124 // helpers
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);
133 public:
134 DeviceInterface(Device *dev = 0);
135 ~DeviceInterface();
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; }
140 void Reset();
141 bool Connect();
142 bool Password(const char *password);
143 void Disconnect();
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
184 #endif