remote.hpp (2903B)
1 /* ReaPack: Package manager for REAPER 2 * Copyright (C) 2015-2025 Christian Fillion 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU Lesser General Public License for more details. 13 * 14 * You should have received a copy of the GNU Lesser General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 #ifndef REAPACK_REMOTE_HPP 19 #define REAPACK_REMOTE_HPP 20 21 #include <boost/logic/tribool.hpp> 22 #include <map> 23 #include <string> 24 #include <vector> 25 26 typedef boost::logic::tribool tribool; 27 28 class Remote { 29 public: 30 static Remote fromString(const std::string &data); 31 32 Remote(); 33 Remote(const std::string &name, const std::string &url, bool enabled = true, 34 const tribool &autoInstall = boost::logic::indeterminate); 35 36 std::string toString() const; 37 38 void setName(const std::string &name); 39 const std::string &name() const { return m_name; } 40 41 void setUrl(const std::string &url); 42 const std::string &url() const { return m_url; } 43 44 bool isNull() const { return m_name.empty() || m_url.empty(); } 45 46 void enable() { setEnabled(true); } 47 void disable() { setEnabled(false); } 48 void setEnabled(const bool enabled) { m_enabled = enabled; } 49 bool isEnabled() const { return m_enabled; } 50 51 void setAutoInstall(const tribool &autoInstall) { m_autoInstall = autoInstall; } 52 tribool autoInstall() const { return m_autoInstall; } 53 bool autoInstall(bool fallback) const; 54 55 void protect() { m_protected = true; } 56 bool isProtected() const { return m_protected; } 57 58 bool operator<(const Remote &o) const { return m_name < o.name(); } 59 operator bool() const { return !isNull(); } 60 61 private: 62 std::string m_name; 63 std::string m_url; 64 bool m_enabled; 65 bool m_protected; 66 tribool m_autoInstall; 67 }; 68 69 class RemoteList { 70 public: 71 RemoteList() {} 72 RemoteList(const RemoteList &) = delete; 73 74 void add(const Remote &); 75 void remove(const Remote &remote) { remove(remote.name()); } 76 void remove(const std::string &name); 77 Remote get(const std::string &name) const; 78 std::vector<Remote> getEnabled() const; 79 80 bool empty() const { return m_remotes.empty(); } 81 size_t size() const { return m_remotes.size(); } 82 bool hasName(const std::string &name) const { return m_map.count(name) > 0; } 83 84 std::vector<Remote>::const_iterator begin() const { return m_remotes.begin(); } 85 std::vector<Remote>::const_iterator end() const { return m_remotes.end(); } 86 87 private: 88 std::vector<Remote> m_remotes; 89 std::map<std::string, size_t> m_map; 90 }; 91 92 #endif