blob: 10bb11757686d4400183c88a6300758c51d9849d [file] [log] [blame]
Yingdi Yu0b0a7362014-08-05 16:31:30 -07001/* -*- 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 Ding0cfc1512015-02-17 17:44:11 -08009 * Qiuhan Ding <qiuhanding@cs.ucla.edu>
Yingdi Yu0b0a7362014-08-05 16:31:30 -070010 */
11
Yingdi Yueb692ac2015-02-10 18:46:18 -080012#ifndef CHRONOCHAT_CONTACT_STORAGE_HPP
13#define CHRONOCHAT_CONTACT_STORAGE_HPP
Yingdi Yu0b0a7362014-08-05 16:31:30 -070014
15#include "contact.hpp"
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080016#include "endorse-collection.hpp"
Yingdi Yu0b0a7362014-08-05 16:31:30 -070017#include <sqlite3.h>
18
Yingdi Yueb692ac2015-02-10 18:46:18 -080019namespace chronochat {
Yingdi Yu0b0a7362014-08-05 16:31:30 -070020
21class ContactStorage
22{
23
24public:
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
Varun Patil3d850902020-11-23 12:19:14 +053047 shared_ptr<EndorseCertificate>
48 getSelfEndorseCertificate();
49
Yingdi Yu0b0a7362014-08-05 16:31:30 -070050 void
51 addEndorseCertificate(const EndorseCertificate& endorseCertificate, const Name& identity);
52
53 void
54 updateCollectEndorse(const EndorseCertificate& endorseCertificate);
55
56 void
57 getCollectEndorse(EndorseCollection& endorseCollection);
58
Varun Patil3d850902020-11-23 12:19:14 +053059 shared_ptr<EndorseCertificate>
60 getCollectEndorseByName(const Name& name);
61
Yingdi Yu0b0a7362014-08-05 16:31:30 -070062 void
63 getEndorseList(const Name& identity, std::vector<std::string>& endorseList);
64
65 void
66 removeContact(const Name& identity);
67
68 void
69 addContact(const Contact& contact);
70
71 shared_ptr<Contact>
72 getContact(const Name& identity) const;
73
74 void
75 updateIsIntroducer(const Name& identity, bool isIntroducer);
76
77 void
78 updateAlias(const Name& identity, const std::string& alias);
79
80 void
81 getAllContacts(std::vector<shared_ptr<Contact> >& contacts) const;
82
83 void
84 updateDnsSelfProfileData(const Data& data)
85 {
86 updateDnsData(data.wireEncode(), "N/A", "PROFILE", data.getName().toUri());
87 }
88
89 void
90 updateDnsEndorseOthers(const Data& data, const std::string& endorsee)
91 {
92 updateDnsData(data.wireEncode(), endorsee, "ENDORSEE", data.getName().toUri());
93 }
94
95 void
96 updateDnsOthersEndorse(const Data& data)
97 {
98 updateDnsData(data.wireEncode(), "N/A", "ENDORSED", data.getName().toUri());
99 }
100
101 shared_ptr<Data>
102 getDnsData(const Name& name);
103
104 shared_ptr<Data>
105 getDnsData(const std::string& name, const std::string& type);
106
107private:
108 std::string
109 getDBName();
110
111 void
112 initializeTable(const std::string& tableName, const std::string& sqlCreateStmt);
113
114 bool
115 doesContactExist(const Name& name);
116
117 void
118 updateDnsData(const Block& data,
119 const std::string& name,
120 const std::string& type,
121 const std::string& dataName);
122
123private:
124 Name m_identity;
125
126 sqlite3 *m_db;
127};
128
Yingdi Yueb692ac2015-02-10 18:46:18 -0800129} // namespace chronochat
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700130
Yingdi Yueb692ac2015-02-10 18:46:18 -0800131#endif // CHRONOCHAT_CONTACT_STORAGE_HPP