receipt.hpp (3462B)
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_RECEIPT_HPP 19 #define REAPACK_RECEIPT_HPP 20 21 #include "registry.hpp" 22 #include "errors.hpp" 23 24 #include <memory> 25 #include <set> 26 #include <sstream> 27 #include <vector> 28 29 class Index; 30 class InstallTicket; 31 class Path; 32 class ReceiptPage; 33 class Version; 34 35 typedef std::shared_ptr<const Index> IndexPtr; 36 37 class Receipt { 38 public: 39 enum Flag { 40 NoFlag = 0, 41 ErrorFlag = 1<<0, 42 RestartNeededFlag = 1<<1, 43 IndexChangedFlag = 1<<2, 44 PackageChangedFlag = 1<<3, 45 InstalledFlag = 1<<4, 46 RemovedFlag = 1<<5, 47 ExportedFlag = 1<<6, 48 49 InstalledOrRemoved = InstalledFlag | RemovedFlag, 50 RefreshBrowser = IndexChangedFlag | PackageChangedFlag | InstalledOrRemoved, 51 }; 52 53 Receipt(); 54 55 int flags() const { return m_flags; } 56 bool test(Flag f) const { return (m_flags & f) != 0; } 57 bool empty() const; 58 59 void setIndexChanged() { m_flags |= IndexChangedFlag; } 60 void setPackageChanged() { m_flags |= PackageChangedFlag; } 61 void addInstall(const Version *, const Registry::Entry &); 62 void addRemoval(const Path &p); 63 void addExport(const Path &p); 64 void addError(const ErrorInfo &); 65 66 ReceiptPage installedPage() const; 67 ReceiptPage removedPage() const; 68 ReceiptPage exportedPage() const; 69 ReceiptPage errorPage() const; 70 71 private: 72 int m_flags; 73 74 std::multiset<InstallTicket> m_installs; 75 std::set<Path> m_removals; 76 std::set<Path> m_exports; 77 std::vector<ErrorInfo> m_errors; 78 }; 79 80 class ReceiptPage { 81 public: 82 template<typename T> 83 ReceiptPage(const T &list, const char *singular, const char *plural = nullptr) 84 : m_size(list.size()) 85 { 86 setTitle(m_size == 1 || !plural ? singular : plural); 87 88 std::ostringstream stream; 89 90 for(const auto &item : list) { 91 if(stream.tellp() > 0) 92 stream << "\r\n"; 93 94 stream << item; 95 } 96 97 m_contents = stream.str(); 98 } 99 100 const std::string &title() const { return m_title; } 101 const std::string &contents() const { return m_contents; } 102 bool empty() const { return m_size == 0; } 103 104 private: 105 void setTitle(const char *title); 106 size_t m_size; 107 std::string m_title; 108 std::string m_contents; 109 }; 110 111 class InstallTicket { 112 public: 113 enum Type { 114 Install, 115 Reinstall, 116 Update, 117 Downgrade, 118 }; 119 120 InstallTicket(const Version *ver, const Registry::Entry &previousEntry); 121 122 bool operator<(const InstallTicket &) const; 123 124 private: 125 friend std::ostream &operator<<(std::ostream &, const InstallTicket &); 126 Type deduceType(const Registry::Entry &) const; 127 128 const Version *m_version; 129 VersionName m_previous; 130 Type m_type; 131 132 IndexPtr m_index; // to keep it alive long enough 133 }; 134 135 std::ostream &operator<<(std::ostream &, const InstallTicket &); 136 137 #endif