Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * Yingdi Yu |
| 5 | * |
| 6 | * BSD license, See the LICENSE file for more information |
| 7 | * |
| 8 | * Author: Yingdi Yu <yingdi@cs.ucla.edu> |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 9 | * Qiuhan Ding <qiuhanding@cs.ucla.edu> |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 12 | #ifndef CHRONOCHAT_CONTACT_STORAGE_HPP |
| 13 | #define CHRONOCHAT_CONTACT_STORAGE_HPP |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 14 | |
| 15 | #include "contact.hpp" |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 16 | #include "endorse-collection.hpp" |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 17 | #include <sqlite3.h> |
| 18 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 19 | namespace chronochat { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 20 | |
| 21 | class ContactStorage |
| 22 | { |
| 23 | |
| 24 | public: |
| 25 | class Error : public std::runtime_error |
| 26 | { |
| 27 | public: |
| 28 | Error(const std::string &what) |
| 29 | : std::runtime_error(what) |
| 30 | { |
| 31 | } |
| 32 | }; |
| 33 | |
| 34 | ContactStorage(const Name& identity); |
| 35 | |
| 36 | ~ContactStorage() |
| 37 | { |
| 38 | sqlite3_close(m_db); |
| 39 | } |
| 40 | |
| 41 | shared_ptr<Profile> |
| 42 | getSelfProfile(); |
| 43 | |
| 44 | void |
| 45 | addSelfEndorseCertificate(const EndorseCertificate& endorseCertificate); |
| 46 | |
| 47 | void |
| 48 | addEndorseCertificate(const EndorseCertificate& endorseCertificate, const Name& identity); |
| 49 | |
| 50 | void |
| 51 | updateCollectEndorse(const EndorseCertificate& endorseCertificate); |
| 52 | |
| 53 | void |
| 54 | getCollectEndorse(EndorseCollection& endorseCollection); |
| 55 | |
| 56 | void |
| 57 | getEndorseList(const Name& identity, std::vector<std::string>& endorseList); |
| 58 | |
| 59 | void |
| 60 | removeContact(const Name& identity); |
| 61 | |
| 62 | void |
| 63 | addContact(const Contact& contact); |
| 64 | |
| 65 | shared_ptr<Contact> |
| 66 | getContact(const Name& identity) const; |
| 67 | |
| 68 | void |
| 69 | updateIsIntroducer(const Name& identity, bool isIntroducer); |
| 70 | |
| 71 | void |
| 72 | updateAlias(const Name& identity, const std::string& alias); |
| 73 | |
| 74 | void |
| 75 | getAllContacts(std::vector<shared_ptr<Contact> >& contacts) const; |
| 76 | |
| 77 | void |
| 78 | updateDnsSelfProfileData(const Data& data) |
| 79 | { |
| 80 | updateDnsData(data.wireEncode(), "N/A", "PROFILE", data.getName().toUri()); |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | updateDnsEndorseOthers(const Data& data, const std::string& endorsee) |
| 85 | { |
| 86 | updateDnsData(data.wireEncode(), endorsee, "ENDORSEE", data.getName().toUri()); |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | updateDnsOthersEndorse(const Data& data) |
| 91 | { |
| 92 | updateDnsData(data.wireEncode(), "N/A", "ENDORSED", data.getName().toUri()); |
| 93 | } |
| 94 | |
| 95 | shared_ptr<Data> |
| 96 | getDnsData(const Name& name); |
| 97 | |
| 98 | shared_ptr<Data> |
| 99 | getDnsData(const std::string& name, const std::string& type); |
| 100 | |
| 101 | private: |
| 102 | std::string |
| 103 | getDBName(); |
| 104 | |
| 105 | void |
| 106 | initializeTable(const std::string& tableName, const std::string& sqlCreateStmt); |
| 107 | |
| 108 | bool |
| 109 | doesContactExist(const Name& name); |
| 110 | |
| 111 | void |
| 112 | updateDnsData(const Block& data, |
| 113 | const std::string& name, |
| 114 | const std::string& type, |
| 115 | const std::string& dataName); |
| 116 | |
| 117 | private: |
| 118 | Name m_identity; |
| 119 | |
| 120 | sqlite3 *m_db; |
| 121 | }; |
| 122 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 123 | } // namespace chronochat |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 124 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 125 | #endif // CHRONOCHAT_CONTACT_STORAGE_HPP |