1 /*GRB*
2 
3     Gerbera - https://gerbera.io/
4 
5     directory_tweak.h - this file is part of Gerbera.
6 
7     Copyright (C) 2020-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 directory_tweak.h
25 ///\brief Definitions of the DirectoryTweak class.
26 
27 #ifndef __DIRECTORYTWEAK_H__
28 #define __DIRECTORYTWEAK_H__
29 
30 #include <filesystem>
31 #include <map>
32 #include <mutex>
33 #include <vector>
34 namespace fs = std::filesystem;
35 
36 // forward declaration
37 class AutoscanDirectory;
38 class Config;
39 class DirectoryTweak;
40 
41 class AutoScanSetting {
42 public:
43     bool followSymlinks = false;
44     bool recursive = true;
45     bool hidden = false;
46     bool rescanResource = true;
47     std::shared_ptr<AutoscanDirectory> adir;
48     std::vector<std::string> resourcePatterns;
49 
50     void mergeOptions(const std::shared_ptr<Config>& config, const fs::path& location);
51 };
52 
53 class DirectoryConfigList {
54 public:
55     /// \brief Adds a new DirectoryTweak to the list.
56     ///
57     /// \param dir DirectoryTweak to add to the list.
58     /// \return scanID of the newly added DirectoryTweak
59     void add(const std::shared_ptr<DirectoryTweak>& dir, std::size_t index = std::numeric_limits<std::size_t>::max());
60 
61     std::shared_ptr<DirectoryTweak> get(std::size_t id, bool edit = false);
62 
63     std::shared_ptr<DirectoryTweak> get(const fs::path& location);
64 
65     std::size_t getEditSize() const;
66 
size()67     std::size_t size() const { return list.size(); }
68 
69     /// \brief removes the DirectoryTweak given by its ID
70     void remove(std::size_t id, bool edit = false);
71 
72     /// \brief returns a copy of the directory config list in the form of an array
73     std::vector<std::shared_ptr<DirectoryTweak>> getArrayCopy();
74 
75 protected:
76     std::size_t origSize {};
77     std::map<std::size_t, std::shared_ptr<DirectoryTweak>> indexMap;
78 
79     std::recursive_mutex mutex;
80     using AutoLock = std::lock_guard<std::recursive_mutex>;
81 
82     std::vector<std::shared_ptr<DirectoryTweak>> list;
83     void _add(const std::shared_ptr<DirectoryTweak>& dir, std::size_t index);
84 };
85 
86 #define SETTING_FANART "FanArt"
87 #define SETTING_CONTAINERART "ContainerArt"
88 #define SETTING_SUBTITLE "SubTitle"
89 #define SETTING_RESOURCE "Resource"
90 
91 /// \brief Provides information about one directory.
92 class DirectoryTweak {
93 public:
94     DirectoryTweak() = default;
DirectoryTweak(fs::path location,bool inherit)95     explicit DirectoryTweak(fs::path location, bool inherit)
96         : location(std::move(location))
97         , inherit(inherit)
98     {
99     }
100 
setLocation(const fs::path & location)101     void setLocation(const fs::path& location) { this->location = location; }
getLocation()102     fs::path getLocation() const { return location; }
103 
setInherit(bool inherit)104     void setInherit(bool inherit) { this->inherit = inherit; }
getInherit()105     bool getInherit() const { return inherit; }
106 
setOrig(bool orig)107     void setOrig(bool orig) { this->isOrig = orig; }
getOrig()108     bool getOrig() const { return isOrig; }
109 
setRecursive(bool recursive)110     void setRecursive(bool recursive) { this->flags["Recursive"] = recursive; }
hasRecursive()111     bool hasRecursive() const { return flags.find("Recursive") != flags.end(); }
getRecursive()112     bool getRecursive() const { return flags.at("Recursive"); }
113 
setHidden(bool hidden)114     void setHidden(bool hidden) { this->flags["Hidden"] = hidden; }
hasHidden()115     bool hasHidden() const { return flags.find("Hidden") != flags.end(); }
getHidden()116     bool getHidden() const { return flags.at("Hidden"); }
117 
setCaseSensitive(bool caseSensitive)118     void setCaseSensitive(bool caseSensitive) { this->flags["CaseSens"] = caseSensitive; }
hasCaseSensitive()119     bool hasCaseSensitive() const { return flags.find("CaseSens") != flags.end(); }
getCaseSensitive()120     bool getCaseSensitive() const { return flags.at("CaseSens"); }
121 
setFollowSymlinks(bool followSymlinks)122     void setFollowSymlinks(bool followSymlinks) { this->flags["FollowSymlinks"] = followSymlinks; }
hasFollowSymlinks()123     bool hasFollowSymlinks() const { return flags.find("FollowSymlinks") != flags.end(); }
getFollowSymlinks()124     bool getFollowSymlinks() const { return flags.at("FollowSymlinks"); }
125 
setMetaCharset(const std::string & metaCharset)126     void setMetaCharset(const std::string& metaCharset) { this->resourceFiles["MetaCharset"] = metaCharset; }
hasMetaCharset()127     bool hasMetaCharset() const { return resourceFiles.find("MetaCharset") != resourceFiles.end(); }
getMetaCharset()128     std::string getMetaCharset() const { return resourceFiles.at("MetaCharset"); }
129 
setFanArtFile(const std::string & fanArtFile)130     void setFanArtFile(const std::string& fanArtFile) { this->resourceFiles[SETTING_FANART] = fanArtFile; }
hasFanArtFile()131     bool hasFanArtFile() const { return resourceFiles.find(SETTING_FANART) != resourceFiles.end(); }
getFanArtFile()132     std::string getFanArtFile() const { return resourceFiles.at(SETTING_FANART); }
133 
setSubTitleFile(const std::string & subTitleFile)134     void setSubTitleFile(const std::string& subTitleFile) { this->resourceFiles[SETTING_SUBTITLE] = subTitleFile; }
hasSubTitleFile()135     bool hasSubTitleFile() const { return resourceFiles.find(SETTING_SUBTITLE) != resourceFiles.end(); }
getSubTitleFile()136     std::string getSubTitleFile() const { return resourceFiles.at(SETTING_SUBTITLE); }
137 
setResourceFile(const std::string & resourceFile)138     void setResourceFile(const std::string& resourceFile) { this->resourceFiles[SETTING_RESOURCE] = resourceFile; }
hasResourceFile()139     bool hasResourceFile() const { return resourceFiles.find(SETTING_RESOURCE) != resourceFiles.end(); }
getResourceFile()140     std::string getResourceFile() const { return resourceFiles.at(SETTING_RESOURCE); }
141 
hasSetting(const std::string & setting)142     bool hasSetting(const std::string& setting) const { return resourceFiles.find(setting) != resourceFiles.end(); }
getSetting(const std::string & setting)143     std::string getSetting(const std::string& setting) const { return resourceFiles.at(setting); }
144 
145 protected:
146     fs::path location;
147     bool isOrig {};
148     bool inherit { true };
149     std::map<std::string, std::string> resourceFiles;
150     std::map<std::string, bool> flags;
151 };
152 
153 #endif
154