1 /*GRB*
2 
3     Gerbera - https://gerbera.io/
4 
5     dynamic_content.h - this file is part of Gerbera.
6 
7     Copyright (C) 2021 Gerbera Contributors
8 
9     Gerbera is free software; you can redistribute it and/or modify
10     it under the terms of the GNU General Public License version 2
11     as published by the Free Software Foundation.
12 
13     Gerbera is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17 
18     You should have received a copy of the GNU General Public License
19     along with Gerbera.  If not, see <http://www.gnu.org/licenses/>.
20 
21     $Id$
22 */
23 
24 /// \file dynamic_content.h
25 ///\brief Definitions of the DynamicContent class.
26 
27 #ifndef __DYNAMICCONTENT_H__
28 #define __DYNAMICCONTENT_H__
29 
30 #include <filesystem>
31 #include <map>
32 #include <mutex>
33 #include <vector>
34 namespace fs = std::filesystem;
35 
36 // forward declarations
37 class Config;
38 class DynamicContent;
39 
40 class DynamicContentList {
41 public:
42     /// \brief Adds a new DynamicContent to the list.
43     ///
44     /// \param cont DynamicContent to add to the list.
45     /// \return index of the newly added DynamicContent
46     void add(const std::shared_ptr<DynamicContent>& cont, std::size_t index = std::numeric_limits<std::size_t>::max());
47 
48     std::shared_ptr<DynamicContent> get(std::size_t id, bool edit = false);
49 
50     std::shared_ptr<DynamicContent> get(const fs::path& location);
51 
52     std::size_t getEditSize() const;
53 
size()54     std::size_t size() const { return list.size(); }
55 
56     /// \brief removes the DynamicContent given by its ID
57     void remove(std::size_t id, bool edit = false);
58 
59     /// \brief returns a copy of the directory config list in the form of an array
60     std::vector<std::shared_ptr<DynamicContent>> getArrayCopy();
61 
62 protected:
63     std::size_t origSize {};
64     std::map<std::size_t, std::shared_ptr<DynamicContent>> indexMap;
65 
66     std::recursive_mutex mutex;
67     using AutoLock = std::lock_guard<std::recursive_mutex>;
68 
69     std::vector<std::shared_ptr<DynamicContent>> list;
70     void _add(const std::shared_ptr<DynamicContent>& cont, std::size_t index);
71 };
72 
73 /// \brief dynamic content reader
74 class DynamicContent {
75 public:
76     DynamicContent() = default;
DynamicContent(fs::path location)77     explicit DynamicContent(fs::path location)
78         : location(std::move(location))
79     {
80         if (this->location.empty())
81             this->location = "/Auto";
82     }
83 
setLocation(const fs::path & location)84     void setLocation(const fs::path& location)
85     {
86         if (!location.empty())
87             this->location = location;
88     }
getLocation()89     fs::path getLocation() const { return location; }
90 
setOrig(bool orig)91     void setOrig(bool orig) { this->isOrig = orig; }
getOrig()92     bool getOrig() const { return isOrig; }
93 
setFilter(const std::string & filter)94     void setFilter(const std::string& filter) { this->filter = filter; }
getFilter()95     std::string getFilter() const { return filter; }
96 
setSort(const std::string & sort)97     void setSort(const std::string& sort) { this->sort = sort; }
getSort()98     std::string getSort() const { return sort; }
99 
setImage(const fs::path & image)100     void setImage(const fs::path& image) { this->image = image; }
getImage()101     fs::path getImage() const { return image; }
102 
setTitle(const std::string & title)103     void setTitle(const std::string& title) { this->title = title; }
getTitle()104     std::string getTitle() const { return title; }
105 
106 private:
107     /// \brief virtual tree location
108     fs::path location { "/Auto" };
109 
110     /// \brief title
111     std::string title;
112 
113     /// \brief search filter
114     std::string filter;
115 
116     /// \brief sort criteria
117     std::string sort;
118 
119     /// \brief folder image
120     fs::path image;
121 
122     bool isOrig {};
123 };
124 
125 #endif
126