service_model.h

00001 
00002 /***************************************************************************
00003  *  service_model.h - Manages list of discovered services of given type
00004  *
00005  *  Created: Mon Sep 29 16:26:04 2008
00006  *  Copyright  2008  Daniel Beck
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023 
00024 #ifndef __LIBS_GUI_UTILS_SERVICE_MODEL_H_
00025 #define __LIBS_GUI_UTILS_SERVICE_MODEL_H_
00026 
00027 #include <netcomm/service_discovery/browse_handler.h>
00028 #include <core/utils/lock_queue.h>
00029 #include <gtkmm.h>
00030 
00031 namespace fawkes {
00032 class AvahiThread;
00033 
00034 class ServiceModel : public fawkes::ServiceBrowseHandler
00035 {
00036  public:
00037   ServiceModel(const char* service = "_fawkes._tcp");
00038   ServiceModel(fawkes::AvahiThread* avahi_thread);
00039   virtual ~ServiceModel();
00040 
00041   Glib::RefPtr<Gtk::ListStore>& get_list_store();
00042   
00043   class ServiceRecord : public Gtk::TreeModelColumnRecord
00044     {
00045     public:
00046       ServiceRecord()
00047         {
00048           add(name);
00049           add(type);
00050           add(domain);
00051           add(hostname);
00052           add(ipaddr);
00053           add(port);
00054         }
00055       
00056       Gtk::TreeModelColumn<Glib::ustring> name;      /**< The name of the service */
00057       Gtk::TreeModelColumn<Glib::ustring> type;      /**< The type of the service */
00058       Gtk::TreeModelColumn<Glib::ustring> domain;    /**< The domain of the service */
00059       Gtk::TreeModelColumn<Glib::ustring> hostname;  /**< The name of the host the service is running on */
00060       Gtk::TreeModelColumn<Glib::ustring> ipaddr;    /**< The IP address as string of the host the service is running on */
00061       Gtk::TreeModelColumn<unsigned short> port;     /**< The port the service is running on */
00062     };
00063 
00064   ServiceRecord& get_column_record();
00065   
00066  protected:
00067   // service browser handler
00068   void all_for_now();
00069   void cache_exhausted();
00070   void browse_failed( const char* name,
00071                       const char* type,
00072                       const char* domain );
00073   void service_added( const char* name,
00074                       const char* type,
00075                       const char* domain,
00076                       const char* host_name,
00077                       const struct sockaddr* addr,
00078                       const socklen_t addr_size,
00079                       uint16_t port,
00080                       std::list<std::string>& txt,
00081                       int flags );
00082   void service_removed( const char* name,
00083                         const char* type,
00084                         const char* domain );
00085   
00086   struct ServiceAddedRecord
00087   {
00088     std::string name;      /**< the name of the new service */
00089     std::string type;      /**< the type of the new service */
00090     std::string domain;    /**< the domain of the new service */
00091     std::string hostname;  /**< the hostname of the new service */
00092     std::string ipaddr;    /**< the IP address of the new service */
00093     unsigned short port;   /**< the port the new service is running on */
00094   };
00095 
00096   struct ServiceRemovedRecord
00097   {
00098     std::string name;    /**< the name of the service */
00099     std::string type;    /**< the type of the service */
00100     std::string domain;  /**< the domain of the service */
00101   };
00102       
00103   fawkes::LockQueue<ServiceAddedRecord>   m_added_services;
00104   fawkes::LockQueue<ServiceRemovedRecord> m_removed_services;
00105   
00106   Glib::Dispatcher m_signal_service_added;
00107   Glib::Dispatcher m_signal_service_removed;
00108   
00109   virtual void on_service_added();
00110   virtual void on_service_removed();
00111 
00112   Glib::RefPtr<Gtk::ListStore> m_service_list;
00113   ServiceRecord                m_service_record;
00114   
00115   fawkes::AvahiThread* m_avahi;
00116   
00117  private:
00118   bool m_own_avahi_thread;
00119 };
00120  
00121 }
00122 
00123 #endif /* __LIBS_GUI_UTILS_HOST_MODEL_H_ */