lib: set ProbeResult's constructor to private
[barry.git] / gui / src / DeviceIface.h
blob4cd39cc16021cfde8ddbb38671d3ebdaf4e98880
1 ///
2 /// \file DeviceIface.h
3 /// Interface class for device backup and restore
4 ///
6 /*
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>
26 #include <string>
27 #include <memory>
28 #include <stdint.h>
29 #include "tarfile.h"
31 #define DI_THREAD_DONE 100
32 #define DI_THREAD_PROGRESS 101
34 namespace Glib {
35 class Dispatcher;
36 class Mutex;
39 class Device
41 Barry::ProbeResult result;
43 public:
44 Device(const Barry::ProbeResult &result);
46 Barry::Pin GetPIN() const { return Barry::Pin(result.m_pin); };
48 friend class DeviceInterface;
51 class DeviceInterface :
52 public Barry::Parser,
53 public Barry::Builder
55 public:
56 struct AppComm // app communication
58 Glib::Dispatcher *m_erase_db; // to notify the app about the
59 // db erase stage of restore
60 Glib::Dispatcher *m_restored_db;// to notify the app that the
61 // previous erase_db was
62 // restored... do not rely
63 // on the current db name ==
64 // the name found at erase time
65 Glib::Dispatcher *m_progress;
66 Glib::Dispatcher *m_error;
67 Glib::Dispatcher *m_done;
69 AppComm() :
70 m_erase_db(0),
71 m_restored_db(0),
72 m_progress(0),
73 m_error(0),
74 m_done(0)
76 AppComm(Glib::Dispatcher *progress,
77 Glib::Dispatcher *error,
78 Glib::Dispatcher *done,
79 Glib::Dispatcher *erase_db,
80 Glib::Dispatcher *restored_db) :
81 m_erase_db(erase_db),
82 m_restored_db(restored_db),
83 m_progress(progress),
84 m_error(error),
85 m_done(done)
87 bool IsValid() const
88 { return m_erase_db && m_restored_db && m_progress && m_error && m_done; }
89 void Invalidate()
90 { m_erase_db = m_restored_db = m_progress = m_error = m_done = 0; }
93 class Quit // quit exception to break out of upload/download
97 private:
98 Device *m_dev;
99 Barry::Controller *m_con;
100 Barry::Mode::Desktop *m_desktop;
101 std::string m_last_error;
102 std::string m_last_thread_error;
104 AppComm m_AppComm;
105 std::auto_ptr<reuse::TarFile> m_tar, m_tarback;
107 // parser and builder data (only one side uses these at a time)
108 Barry::ConfigFile::DBListType m_dbList;
109 mutable Glib::Mutex *m_dbnameMutex;
110 std::string m_current_dbname_not_thread_safe;
111 std::string m_current_dbname;
112 uint8_t m_rec_type;
113 uint32_t m_unique_id;
114 std::string m_tar_id_text;
115 std::string m_record_data;
116 bool m_end_of_tar;
117 bool m_tar_record_loaded;
119 // thread quit flag... not locked, only a byte
120 volatile bool m_thread_quit;
122 protected:
123 bool False(const std::string &msg);
125 // threads
126 void BackupThread();
127 void RestoreThread();
128 void RestoreAndBackupThread();
130 // helpers
131 std::string MakeFilename(const std::string &label = "") const;
132 int CountFiles(reuse::TarFile &tar, const Barry::ConfigFile::DBListType &restoreList) const;
133 bool SplitTarPath(const std::string &tarpath, std::string &dbname,
134 std::string &dbid_text, uint8_t &dbrectype, uint32_t &dbid) const;
136 // Sets the name of the database the thread is currently working on
137 void SetThreadDBName(const std::string &dbname);
139 public:
140 DeviceInterface(Device *dev = 0);
141 ~DeviceInterface();
143 const std::string& get_last_error() const { return m_last_error; }
144 const std::string& get_last_thread_error() const { return m_last_thread_error; }
146 void Reset();
147 bool Connect();
148 bool Password(const char *password);
149 void Disconnect();
151 const Barry::DatabaseDatabase& GetDBDB() const { return m_desktop->GetDBDB(); }
152 unsigned int GetRecordTotal(const Barry::ConfigFile::DBListType &backupList) const;
153 unsigned int GetRecordTotal(const Barry::ConfigFile::DBListType &restoreList, const std::string &filename) const;
155 void QuitThread() { m_thread_quit = true; }
157 /// returns name of database the thread is currently working on
158 std::string GetThreadDBName() const;
160 bool StartBackup(AppComm comm,
161 const Barry::ConfigFile::DBListType &backupList,
162 const std::string &directory, const std::string &backupLabel);
163 bool StartRestore(AppComm comm,
164 const Barry::ConfigFile::DBListType &restoreList,
165 const std::string &tarfilename);
166 // this is for debugging... starts a restore, and then does an
167 // immediate backup of the same DB before moving on to the next
168 bool StartRestoreAndBackup(AppComm comm,
169 const Barry::ConfigFile::DBListType &restoreAndBackupList,
170 const std::string &tarfilename,
171 const std::string &directory);
173 // Barry::Parser overrides
174 virtual void StartParser();
175 virtual void ParseRecord(const Barry::DBData &data,
176 const Barry::IConverter *ic);
177 virtual void EndParser();
179 // Barry::Builder overrides
180 virtual bool Retrieve();
181 virtual bool EndOfFile() const { return false; } // not used
182 virtual void BuildRecord(Barry::DBData &data, size_t &offset, const Barry::IConverter *ic);
183 virtual void BuildDone();
184 void SkipCurrentDB() throw(); // helper function for halding restore errors
187 #endif