Reduced minimum password retry level from 6 to 3
[barry/pauldeden.git] / gui / src / DeviceIface.h
bloba338a6daa93393b813c902f810197561fc490e3e
1 ///
2 /// \file DeviceIface.h
3 /// Interface class for device backup and restore
4 ///
6 /*
7 Copyright (C) 2007-2008, 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 DeviceInterface :
41 public Barry::Parser,
42 public Barry::Builder
44 public:
45 struct AppComm // app communication
47 Glib::Dispatcher *m_erase_db; // to notify the app about the
48 // db erase stage of restore
49 Glib::Dispatcher *m_progress;
50 Glib::Dispatcher *m_error;
51 Glib::Dispatcher *m_done;
53 AppComm() :
54 m_erase_db(0),
55 m_progress(0),
56 m_error(0),
57 m_done(0)
59 AppComm(Glib::Dispatcher *progress,
60 Glib::Dispatcher *error,
61 Glib::Dispatcher *done,
62 Glib::Dispatcher *erase_db) :
63 m_erase_db(erase_db),
64 m_progress(progress),
65 m_error(error),
66 m_done(done)
68 bool IsValid() const
69 { return m_erase_db && m_progress && m_error && m_done; }
70 void Invalidate()
71 { m_erase_db = m_progress = m_error = m_done = 0; }
74 class Quit // quit exception to break out of upload/download
78 private:
79 Barry::Controller *m_con;
80 Barry::Mode::Desktop *m_desktop;
81 std::string m_last_error;
82 std::string m_last_thread_error;
84 AppComm m_AppComm;
85 std::auto_ptr<reuse::TarFile> m_tar, m_tarback;
87 // parser and builder data (only one side uses these at a time)
88 ConfigFile::DBListType m_dbList;
89 mutable Glib::Mutex *m_dbnameMutex;
90 std::string m_current_dbname_not_thread_safe;
91 std::string m_current_dbname;
92 uint8_t m_rec_type;
93 uint32_t m_unique_id;
94 std::string m_tar_id_text;
95 std::string m_record_data;
96 bool m_end_of_tar;
97 bool m_tar_record_loaded;
99 // thread quit flag... not locked, only a byte
100 volatile bool m_thread_quit;
102 protected:
103 bool False(const std::string &msg);
105 // threads
106 void BackupThread();
107 void RestoreThread();
108 void RestoreAndBackupThread();
110 // helpers
111 std::string MakeFilename(const std::string &pin);
112 int CountFiles(reuse::TarFile &tar, const ConfigFile::DBListType &restoreList);
113 bool SplitTarPath(const std::string &tarpath, std::string &dbname,
114 std::string &dbid_text, uint8_t &dbrectype, uint32_t &dbid);
116 // Sets the name of the database the thread is currently working on
117 void SetThreadDBName(const std::string &dbname);
119 public:
120 DeviceInterface();
121 ~DeviceInterface();
123 const std::string& get_last_error() const { return m_last_error; }
124 const std::string& get_last_thread_error() const { return m_last_thread_error; }
126 bool Connect(const Barry::ProbeResult &dev);
127 bool Password(const char *password);
128 void Disconnect();
130 const Barry::DatabaseDatabase& GetDBDB() const { return m_desktop->GetDBDB(); }
131 int GetDeviceRecordTotal(const ConfigFile::DBListType &backupList) const;
133 void QuitThread() { m_thread_quit = true; }
135 /// returns name of database the thread is currently working on
136 std::string GetThreadDBName() const;
138 bool StartBackup(AppComm comm,
139 const ConfigFile::DBListType &backupList,
140 const std::string &directory, const std::string &pin);
141 bool StartRestore(AppComm comm,
142 const ConfigFile::DBListType &restoreList,
143 const std::string &tarfilename, int *pRecordCount = 0);
144 // this is for debugging... starts a restore, and then does an
145 // immediate backup of the same DB before moving on to the next
146 bool StartRestoreAndBackup(AppComm comm,
147 const ConfigFile::DBListType &restoreAndBackupList,
148 const std::string &tarfilename,
149 const std::string &directory, const std::string &pin,
150 int *pRecordCount = 0);
152 // Barry::Parser overrides
153 virtual void SetIds(uint8_t RecType, uint32_t UniqueId);
154 virtual void ParseFields(const Barry::Data &data, size_t &offset);
155 virtual void Store();
157 // Barry::Builder overrides
158 virtual bool Retrieve(unsigned int dbId);
159 virtual uint8_t GetRecType() const;
160 virtual uint32_t GetUniqueId() const;
161 virtual void BuildHeader(Barry::Data &data, size_t &offset);
162 virtual void BuildFields(Barry::Data &data, size_t &offset);
163 void SkipCurrentDB() throw(); // helper function for halding restore errors
166 #endif