blob: aa137b4a073c7ced62f7757a7a51583569b2507b [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
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
101private:
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
117private:
118 Name m_identity;
119
120 sqlite3 *m_db;
121};
122
Yingdi Yueb692ac2015-02-10 18:46:18 -0800123} // namespace chronochat
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700124
Yingdi Yueb692ac2015-02-10 18:46:18 -0800125#endif // CHRONOCHAT_CONTACT_STORAGE_HPP