album_art_helpers.h

Go to the documentation of this file.
00001 
00002 
00003 
00004 class CAlbumArtLoaderBase : private CSimpleThread {
00005 public:
00006         CAlbumArtLoaderBase() : m_api(static_api_ptr_t<album_art_manager>()->instantiate()) {}
00007         ~CAlbumArtLoaderBase() {Abort();}
00008         
00010         void AddType(const GUID & p_what) {
00011                 if (!HaveType(p_what)) {
00012                         Abort();
00013                         m_requestIds.add_item(p_what);
00014                         m_api->close();
00015                 }
00016         }
00017         bool HaveType(const GUID & p_what) const {
00018                 return m_requestIds.have_item(p_what);
00019         }
00020 
00021         void ResetTypes() {
00022                 Abort();
00023                 m_requestIds.remove_all();
00024                 m_api->close();
00025         }
00026 
00027         void Abort() {
00028                 AbortThread();
00029                 m_notify.release();
00030         }
00031 
00033         void Request(const char * p_path,completion_notify_ptr p_notify = NULL) {
00034                 Abort();
00035                 m_requestPath = p_path;
00036                 m_notify = p_notify;
00037                 StartThread();
00038         }
00039 
00040         bool IsReady() const {return !IsWorking();}
00041         bool IsWorking() const {return IsThreadActive();}
00042 
00043 protected:
00044         virtual void OnContent(const GUID & p_what,album_art_data_ptr p_data,abort_callback & p_abort) {}
00045         virtual void OnContentReset() {}
00046 
00047 private:
00048         unsigned ThreadProc(abort_callback & p_abort) {
00049                 try {
00050                         return ProcessRequest(p_abort);
00051                 } catch(exception_aborted) {
00052                         return 0;
00053                 } catch(std::exception const & e) {
00054                         console::complain("Album Art loading failure", e);
00055                         return 0;
00056                 }
00057         }
00058 
00059         unsigned ProcessRequest(abort_callback & p_abort) {
00060                 if (m_api->open(m_requestPath,p_abort)) {
00061                         OnContentReset();
00062                         for(pfc::chain_list_v2_t<GUID>::const_iterator walk = m_requestIds.first(); walk.is_valid(); ++walk) {
00063                                 album_art_data_ptr data;
00064                                 try {
00065                                         data = m_api->query(*walk,p_abort);
00066                                 } catch(exception_io const & e) {
00067                                         console::complain("Requested Album Art entry could not be retrieved", e);
00068                                         continue;
00069                                 }
00070                                 pfc::dynamic_assert( data.is_valid() );
00071                                 OnContent(*walk,data,p_abort);
00072                         }
00073                         return 1;
00074                 } else {
00075                         return 0;
00076                 }
00077         }
00078         void ThreadDone(unsigned p_code) {
00079                 //release our notify ptr before triggering callbacks, they might fire another query from inside that
00080                 completion_notify_ptr temp; temp << m_notify;
00081                 if (temp.is_valid()) temp->on_completion(p_code);
00082         }
00083 
00084         pfc::string8 m_requestPath;
00085         pfc::chain_list_v2_t<GUID> m_requestIds;
00086 
00087         completion_notify_ptr m_notify;
00088 
00089         const album_art_manager_instance_ptr m_api;
00090 
00091         PFC_CLASS_NOT_COPYABLE_EX(CAlbumArtLoaderBase);
00092 };
00093 
00094 class CAlbumArtLoader : public CAlbumArtLoaderBase {
00095 public:
00096         ~CAlbumArtLoader() {Abort();}
00097         bool Query(const GUID & p_what, album_art_data_ptr & p_data) const {
00098                 pfc::dynamic_assert( IsReady() );
00099                 return m_content.query(p_what,p_data);
00100         }
00101 protected:
00102         void OnContentReset() {
00103                 m_content.remove_all();
00104         }
00105         void OnContent(const GUID & p_what,album_art_data_ptr p_data,abort_callback & p_abort) {
00106                 m_content.set(p_what,p_data);
00107         }
00108         pfc::map_t<GUID,album_art_data_ptr> m_content;
00109 };

Generated on Fri Apr 25 18:49:33 2008 for foobar2000 SDK by  doxygen 1.5.5