YCLIMP
Yandex Command Line Interface Music Player
Loading...
Searching...
No Matches
FileManager.hpp
1#pragma once
2
3#include "DatabaseIdentifiers.hpp"
4
5// I did't investigate it, but boost didn't compile without it
6#include <utility>
7
8//
9#include <boost/asio.hpp>
10#include <boost/asio/ip/address.hpp>
11#include <boost/asio/read_until.hpp>
12#include <boost/system/system_error.hpp>
13#include <filesystem>
14#include <memory>
15#include <regex>
16#include <string>
17#include <unordered_map>
18#include <vector>
19
20using namespace boost;
21
22namespace fs = std::filesystem;
23
24#define PARENT_DIR_NAME ".."
25
26const std::string TYPE_LOCAL = "LOCAL";
27const std::string TYPE_YANDEX = "YANDEX";
28const std::string TYPE_APPLICATION = "APPLICATION";
29const std::string TYPE_UNKNOWN = "UNKNOWN";
30
31class File;
32using File_Ptr = std::shared_ptr<File>;
33
34class Client;
35using Client_Ptr = std::shared_ptr<Client>;
36
37bool IsFileHasMusicExtension(const std::string& filename);
38
49class File
50{
51public:
52 enum class Type
53 {
54 Directory = 0,
55 Track = 1,
56 Radio = 2
57 };
58
59public:
60 File(Client& client, const std::string& name, fs::path path,
61 const std::string& location_id, File::Type type, int playlistId = -1);
62
63 virtual void NewFile(File_Ptr newFile);
64
65 virtual std::vector<File_Ptr> GetDirFiles(
66 bool noDirs = false, bool onlyMusicExtensions = false, bool recursively = false);
67 virtual std::pair<std::string, std::string> GetSongData();
68 virtual std::string GetFromType() const;
69
70public:
71 Client& client;
72 std::string name;
73 fs::path path;
74 std::string // FsFile: | YandexFile: | AppFile:
75 location_id; // NULL | yandex_song or album id | FsFile or YandexFile data
76 // -----------------------------------------------------------------------------------------------
78 int playlistId; // NULL(-1) | NULL(-1) | playlistID for tracks; self id for playlsits and NULL(-1) for root dir
79 File::Type type;
80};
81
82class FsFile : public File
83{
84public:
85 FsFile(Client& client, const std::string& name, fs::path path,
86 const std::string& location_id, File::Type type);
87
88 static File_Ptr InitFile(Client& client);
89 virtual std::string GetFromType() const override;
90};
91
92class YaFile : public File
93{
94public:
95 YaFile(Client& client, const std::string& name, fs::path path,
96 const std::string& location_id, File::Type type);
97
98 static File_Ptr InitFile(Client& client);
99 virtual std::string GetFromType() const override;
100};
101
102class AppFile : public File
103{
104public:
105 enum class FromType
106 {
107 Local = 0,
108 Yandex
109 };
110
111public:
112 AppFile(Client& client, const std::string& name, fs::path path,
113 const std::string& location_id, File::Type type, int playlistID = -1);
114
115 static File_Ptr InitFile(Client& client);
116 virtual std::string GetFromType() const override;
117
118 static File_Ptr ConvertFromDBTrack(DBTrack track);
119 static File_Ptr ConvertFromDBPlaylist(DBPlaylist playlist);
120
121public:
122 // FromType fromType;
123};
124
133{
134public:
135 // virtual void OpenParentDir(File* file) = 0;
136 virtual void NewFile(File* oldFile, File_Ptr newFile) = 0;
137
138 /***
139 * Returns vector of File_Ptr.
140 *
141 * @param noDirs Ignore directories (false by default)
142 * @param onlyMusicExtensions if true regex_match to std::make_shared<File>(this)
143 * (false by default)
144 * ".*\\.(?:mp3|mp2|mp1|ogg|wav|aiff|flac)"
145 * @param recursively if true traverse through all folders recursively (false by
146 * default)
147 */
148 virtual std::vector<File_Ptr> GetDirFiles(File* file, bool noDirs = false,
149 bool onlyMusicExtensions = false,
150 bool recursively = false) = 0; // if directory
151 virtual std::pair<std::string, std::string> GetSongData(File* file) = 0; // if file
152 // virtual std::string getFileNewName() = 0;
153
154public:
155 // fs::path currentPath; // test
156};
157
158class FSClient : public Client
159{
160private:
161 FSClient() = default;
162
163public:
164 FSClient(const FSClient&) = delete;
165 void operator=(const FSClient&) = delete;
166
167 static FSClient& Instance();
168
169public:
170 virtual void NewFile(File* oldFile, File_Ptr newFile) override;
171
172 virtual std::vector<File_Ptr> GetDirFiles(File* file, bool noDirs = false,
173 bool onlyMusicExtensions = false,
174 bool recursively = false) override; // if directory
175 virtual std::pair<std::string, std::string> GetSongData(
176 File* file) override; // if file
177};
178
179class YandexClient : public Client
180{
181public:
182 static std::unordered_map<std::string, std::string> folderCache;
183
184private:
185 YandexClient();
186
187public:
188 YandexClient(const YandexClient&) = delete;
189 void operator=(const YandexClient&) = delete;
190
191 static YandexClient& Instance();
192
193public:
194 virtual void NewFile(File* oldFile, File_Ptr newFile) override;
195
196 virtual std::vector<File_Ptr> GetDirFiles(File* file, bool noDirs = false,
197 bool onlyMusicExtensions = false,
198 bool recursively = false) override; // if directory
199 virtual std::pair<std::string, std::string> GetSongData(
200 File* file) override; // if file
201private:
202 void Connect();
203 void Disconnect();
204
205private:
206 bool mIsConnectionEstablished;
207
208 asio::ip::tcp::endpoint ep;
209 asio::io_service ios;
210 asio::ip::tcp::socket sock;
211};
212
213class AppClient : public Client
214{
215private:
216 AppClient(FSClient& fsClient, YandexClient& yaClient);
217
218public:
219 AppClient(const FSClient&) = delete;
220 void operator=(const AppClient&) = delete;
221
222 static AppClient& Instance();
223
224public:
225 virtual void NewFile(File* oldFile, File_Ptr newFile) override;
226
227 virtual std::vector<File_Ptr> GetDirFiles(File* file, bool noDirs = false,
228 bool onlyMusicExtensions = false,
229 bool recursively = false) override; // if directory
230 virtual std::pair<std::string, std::string> GetSongData(
231 File* file) override; // if file
232
233private:
234 FSClient& fsClient;
235 YandexClient& yaClient;
236};
The Client class - abstract class for FSClient, YandexClient, AppClient. Designed to work with File d...
Definition FileManager.hpp:133
Definition FileManager.hpp:159
The File class - Abstract class for FsFile, YaFile and AppFile.
Definition FileManager.hpp:50
virtual std::vector< File_Ptr > GetDirFiles(bool noDirs=false, bool onlyMusicExtensions=false, bool recursively=false)
File::GetDirFiles - get files in current directory/folder. Will be processed by it's specific client.
Definition FileManager.cpp:67
virtual void NewFile(File_Ptr newFile)
File::NewFile - substitute current file with a new file. Used to navigate between directories/folders...
Definition FileManager.cpp:54
Definition FileManager.hpp:180
DataBasePlaylist - playlist representation in database.
Definition PlaylistDao.hpp:11
The DataBaseTrack - track representation in Database.
Definition TrackDao.hpp:11