qavahiservicebrowsermodel.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2011 Girish Ramakrishnan (girish@forwardbias.in)
  4. **
  5. ** Use, modification and distribution is allowed without limitation,
  6. ** warranty, liability or support of any kind.
  7. **
  8. ****************************************************************************/
  9. #ifndef QAVAHISERVICEBROWSERMODEL_H
  10. #define QAVAHISERVICEBROWSERMODEL_H
  11. #include <avahi-client/client.h>
  12. #include <avahi-client/lookup.h>
  13. #include <avahi-qt4/qt-watch.h>
  14. #include <avahi-common/error.h>
  15. #include <avahi-common/malloc.h>
  16. #include <QObject>
  17. #include <QStringList>
  18. #include <QHostAddress>
  19. #include <QMetaType>
  20. #include <QAbstractListModel>
  21. class QAvahiServiceBrowserModel : public QAbstractListModel
  22. {
  23. Q_OBJECT
  24. public:
  25. QAvahiServiceBrowserModel(QObject *parent = 0);
  26. ~QAvahiServiceBrowserModel();
  27. enum Options {
  28. HideLocal = 1,
  29. HideIPv4 = 2,
  30. HideIPv6 = 4,
  31. HideSameLocalClient = 8
  32. };
  33. enum CustomRoles {
  34. NameRole = Qt::UserRole + 1,
  35. TypeRole,
  36. DomainRole,
  37. ProtocolRole,
  38. InterfaceRole,
  39. HostNameRole,
  40. AddressRole,
  41. PortRole,
  42. TxtRole
  43. };
  44. void setAutoResolve(bool ar) { m_autoResolve = ar; }
  45. bool autoResolve() const { return m_autoResolve; }
  46. struct Service {
  47. Service() : protocol(-1), interface(-1), port(0), flags(0), resolved(false), resolver(0) { }
  48. QString name;
  49. QString type;
  50. QString domain;
  51. AvahiProtocol protocol;
  52. AvahiIfIndex interface;
  53. // valid only for resolved entries
  54. QString hostName;
  55. QHostAddress address;
  56. qint32 port;
  57. QStringList textRecords;
  58. int flags;
  59. bool isCached() const { return flags & AVAHI_LOOKUP_RESULT_CACHED; }
  60. bool isLocal() const { return flags & AVAHI_LOOKUP_RESULT_LOCAL; }
  61. bool isFromMulticastDns() const { return flags & AVAHI_LOOKUP_RESULT_MULTICAST; }
  62. bool isSameLocalClient() const { return flags & AVAHI_LOOKUP_RESULT_OUR_OWN; }
  63. bool isStatic() const { return flags & AVAHI_LOOKUP_RESULT_STATIC; }
  64. bool isFromWanDns() const { return flags & AVAHI_LOOKUP_RESULT_WIDE_AREA; }
  65. bool resolved;
  66. bool isValid() const { return protocol != -1 && interface != -1 && !address.isNull() && port != 0; }
  67. bool operator==(const Service &other) const {
  68. return name == other.name && type == other.type && domain == other.domain
  69. && protocol == other.protocol && interface == other.interface
  70. && hostName == other.hostName && address == other.address
  71. && port == other.port && textRecords == other.textRecords
  72. && flags == other.flags;
  73. }
  74. private:
  75. AvahiServiceResolver *resolver;
  76. friend class QAvahiServiceBrowserModel;
  77. };
  78. int error() const { return m_error; }
  79. QString errorString() const { return m_errorString; }
  80. enum Notification {
  81. Error,
  82. ClientConnecting,
  83. ServerRunning,
  84. ServerNameCollision,
  85. ServerRegistering,
  86. };
  87. Service serviceFromIndex(const QModelIndex &idx) const { return m_services.value(idx.row()); }
  88. void browse(const QString &serviceType, int options = 0);
  89. // reimp
  90. int rowCount(const QModelIndex &parent = QModelIndex()) const;
  91. int columnCount(const QModelIndex &parent = QModelIndex()) const;
  92. QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
  93. QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
  94. public slots:
  95. void resolve(int idx);
  96. void resolve(const QModelIndex &idx);
  97. signals:
  98. void changeNotification(Notification notification);
  99. void done();
  100. void resolved(int idx);
  101. private:
  102. static void client_callback(AvahiClient *c, AvahiClientState state, void *userdata);
  103. void clientCallback(AvahiClient *client, AvahiClientState state);
  104. static void browser_callback(AvahiServiceBrowser *browser, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *name, const char *type, const char *domain, AvahiLookupResultFlags flags, void *userdata);
  105. void browserCallback(AvahiServiceBrowser *browser, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *name, const char *type, const char *domain, AvahiLookupResultFlags flags);
  106. static void resolver_callback(AvahiServiceResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event,
  107. const char *name, const char *type, const char *domain, const char *host_name, const AvahiAddress *address,
  108. uint16_t port, AvahiStringList *txt, AvahiLookupResultFlags flags, void* userdata);
  109. void resolverCallback(AvahiServiceResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event,
  110. const char *name, const char *type, const char *domain, const char *host_name, const AvahiAddress *address,
  111. uint16_t port, AvahiStringList *txt, AvahiLookupResultFlags flags);
  112. void initialize();
  113. void uninitialize();
  114. int serviceIndex(const char *name, const char *type, const char *domain, AvahiIfIndex interface, AvahiProtocol protocol);
  115. void doBrowse(AvahiClient *client);
  116. void resetModel();
  117. void resolve(Service *service);
  118. AvahiClient *m_client;
  119. QString m_serviceType;
  120. int m_options;
  121. enum BrowseType { NoBrowserType, ServiceBrowser, TypeBrowser } m_browserType;
  122. void *m_browser;
  123. int m_error;
  124. QString m_errorString;
  125. bool m_autoResolve;
  126. QList<Service> m_services;
  127. QList<int> m_rowToServiceIndex;
  128. };
  129. Q_DECLARE_METATYPE(QAvahiServiceBrowserModel::Service)
  130. #endif // QAVAHISERVICEBROWSERMODEL_H