Freya  0.0.1
A Gtk3 MPD Client
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
src/MPD/Listener.hh
Go to the documentation of this file.
00001 /***********************************************************
00002 * This file is part of Freya
00003 * - A free MPD Gtk3 MPD Client -
00004 *
00005 * Authors: Christopher Pahl, Christoph Piechula,
00006 *          Eduard Schneider
00007 *
00008 * Copyright (C) [2011-2012]
00009 * Hosted at: https://github.com/studentkittens/Freya
00010 *
00011 *              __..--''``---....___   _..._    __
00012 *    /// //_.-'    .-/";  `        ``<._  ``.''_ `. / // /
00013 *   ///_.-' _..--.'_                        `( ) ) // //
00014 *   / (_..-' // (< _     ;_..__               ; `' / ///
00015 *    / // // //  `-._,_)' // / ``--...____..-' /// / //
00016 *  Ascii-Art by Felix Lee <flee@cse.psu.edu>
00017 *
00018 * Freya is free software: you can redistribute it and/or modify
00019 * it under the terms of the GNU General Public License as published by
00020 * the Free Software Foundation, either version 3 of the License, or
00021 * (at your option) any later version.
00022 *
00023 * Freya is distributed in the hope that it will be useful,
00024 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00025 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00026 * GNU General Public License for more details.
00027 *
00028 * You should have received a copy of the GNU General Public License
00029 * along with Freya. If not, see <http://www.gnu.org/licenses/>.
00030 **************************************************************/
00031 #ifndef FREYA_LISTENER_GUARD
00032 #define FREYA_LISTENER_GUARD
00033 
00034 #include <mpd/client.h>
00035 #include <mpd/async.h>
00036 #include <mpd/parser.h>
00037 #include <glibmm.h>
00038 
00039 #include "Status.hh"
00040 #include "NotifyData.hh"
00041 #include "Connection.hh"
00042 
00043 /* Typedef, so no error-prone rewriting of this sig */
00044 typedef sigc::signal<void,enum mpd_idle, MPD::NotifyData&> EventNotifier;
00045 
00046 namespace MPD
00047 {
00051     class Listener
00052     {
00053     public:
00054         Listener(EventNotifier * notifier, Connection& sync_conn);
00055         ~Listener();
00056 
00057         /* Enter idle mode
00058          * Returns: true on success
00059          */
00060         bool enter();
00061 
00062         /* Leave idle mode (in order to send commands)
00063         */
00064         void leave();
00065 
00066         /* Checks if connection is in idle mode
00067         */
00068         bool is_idling();
00069 
00070         //---------------//
00071         // Classmembers  //
00072         //---------------//
00073 
00074         NotifyData& get_data();
00075 
00076 
00080         void force_update();
00081 
00082     private:
00083 
00088         enum mpd_async_event GIOCondition_to_MPDAsyncEvent(Glib::IOCondition condition);
00089 
00090         //--------------------------------
00091 
00095         Glib::IOCondition MPDAsyncEvent_to_GIOCondition(enum mpd_async_event events);
00096 
00097 
00098         /* check if async connection is doing weird things */
00099         bool check_async_error();
00100         bool recv_parseable();
00101         gboolean io_callback(Glib::IOCondition condition);
00102         bool parse_response(char *line);
00103         void invoke_user_callback();
00104         void create_watch(enum mpd_async_event events);
00105 
00106         //-----------------//
00107         // Instancemembers //
00108         //-----------------//
00109 
00110         /* true if in idlemode */
00111         bool is_idle;
00112 
00113         /* True while executing leave(),
00114          * prevents enter() from being called
00115          * */
00116         bool is_leaving;
00117 
00118         /* True while callback is executed */
00119         bool is_working;
00120 
00121         /* True when a forced update was requested */
00122         bool is_forced;
00123 
00124         /* A reponse parser */
00125         struct mpd_parser * mp_Parser;
00126 
00127         /* Non blocking connection */
00128         struct mpd_async  * async_conn;
00129 
00130         /* The fd id of async_conn */
00131         int async_socket_fd;
00132 
00133         /* What IO events did happen? */
00134         mpd_async_event io_eventmask;
00135 
00136         /* The actual events (like "player","mixer".. etc) */
00137         unsigned idle_events;
00138 
00139         /* A functor representing the io_callback */
00140         sigc::connection io_functor;
00141 
00142         /* emit() is called on this on events */
00143         EventNotifier * mp_Notifier;
00144 
00145         /* MPD Status */
00146         Status * mp_Status;
00147 
00148         /* Synchronous connection */
00149         Connection * mp_Conn;
00150 
00151         /* The data passed to the observer */
00152         NotifyData m_NData;
00153     };
00154 }
00155 #endif