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