// Decentralized Media // // Douglas Thrift // // $Id$ #include #include #include #include #include #include #include #include #include #include "DecentralizedMedia.hpp" int Main(const app::Options& options) { ext::RedBlackSet extensions, locals; api::Pcre::RegEx extension(_B("^-extension=(.+)$")), local(_B("^-local=(.+)$")); _foreach (const app::ArgumentList, arg, app::GetArguments()) { api::Pcre::RegEx::Match match; if (match = extension(*arg)) extensions.Insert(match[1]); else if (match = local(*arg)) locals.Insert(match[1]); } if (extensions.IsEmpty()) extensions.Insert(_B("mp3")); locals.Insert("Media"); _S media(extensions, locals); media.Block(); return 0; } DecentralizedMedia::DecentralizedMedia(const ext::RedBlackSet& extensions, const ext::RedBlackSet& locals) : waf::Server(_B("Web")), process(bmp.IsRunning() ? NULL : new api::Process(_B("/usr/X11R6/bin/beep-media-player"))), connection(dbi::GetDriver("pgsql")->Connect("", "douglas", "", "media")), extensions(extensions) { if (!locals.IsEmpty()) { _L args(locals.Begin(), locals.End()); _foreach (const ext::RedBlackSet, extension, extensions) { if (extension != _set.Begin()) args.InsertLast(_B("-or")); args.InsertLast(_B("-iname")); args.InsertLast(_S() << _B("*.") << *extension); } _H find(new api::Process(_B("/usr/bin/find"), args)); ext::String file; while (ios::ReadLine(*find->GetReader(), file)) /*media.Insert(file)*/; find->Join(); } AddPort(6996); } DecentralizedMedia::~DecentralizedMedia() { if (!process.IsEmpty()) { _H thread(new api::Thread(etl::BindAll(&DecentralizedMedia::Destroy, this))); bmp.Quit(); thread->Join(); } } _L DecentralizedMedia::GetArtists() const { _H artists_(connection->Execute(_B("SELECT DISTINCT artist FROM files WHERE live = TRUE"))); _L artists; while (artists_->MoveNext()) artists.InsertLast(artists_->GetString(_B("artist"))); return artists; } _L DecentralizedMedia::GetTitles() const { _H titles_(connection->Execute(_B("SELECT DISTINCT title FROM files WHERE live = TRUE"))); _L titles; while (titles_->MoveNext()) titles.InsertLast(titles_->GetString(_B("title"))); return titles; } _L DecentralizedMedia::GetAlbums() const { _H albums_(connection->Execute(_B("SELECT DISTINCT album FROM files WHERE live = TRUE"))); _L albums; while (albums_->MoveNext()) albums.InsertLast(albums_->GetString(_B("album"))); return albums; } _L DecentralizedMedia::GetGenres() const { _H genres_(connection->Execute(_B("SELECT DISTINCT album FROM files WHERE live = TRUE"))); _L genres; while (genres_->MoveNext()) genres.InsertLast(genres_->GetString(_B("genre"))); return genres; } _L DecentralizedMedia::GetFolders() const { _H paths(connection->Execute(_B("SELECT DISTINCT root FROM files WHERE live = TRUE"))); _L folders; while (paths->MoveNext()) folders.InsertLast(MediaFolder(connection, paths->GetString(_B("path")))); return folders; } void DecentralizedMedia::Process(const net::Http::Request& request, net::Http::Response& response) { if (request.method_ == _B("EXTENSIONS")) { response.SetStatus(200); _foreach (const ext::RedBlackSet, extension, extensions) response << *extension << ios::NewLineNoFlush; } else if (request.method_ == _B("MEDIA")) { // XXX: implement } else waf::Server::Process(request, response); }