blob: 833c7d600760d8e4b357d25f0ef52adee63c9dc9 [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>
9 */
10
Yingdi Yueb692ac2015-02-10 18:46:18 -080011#ifndef CHRONOCHAT_CONTACT_STORAGE_HPP
12#define CHRONOCHAT_CONTACT_STORAGE_HPP
Yingdi Yu0b0a7362014-08-05 16:31:30 -070013
14#include "contact.hpp"
15#include "endorse-collection.pb.h"
16#include <sqlite3.h>
17
Yingdi Yueb692ac2015-02-10 18:46:18 -080018namespace chronochat {
Yingdi Yu0b0a7362014-08-05 16:31:30 -070019
20class ContactStorage
21{
22
23public:
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
100private:
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
116private:
117 Name m_identity;
118
119 sqlite3 *m_db;
120};
121
Yingdi Yueb692ac2015-02-10 18:46:18 -0800122} // namespace chronochat
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700123
Yingdi Yueb692ac2015-02-10 18:46:18 -0800124#endif // CHRONOCHAT_CONTACT_STORAGE_HPP