00001
00023 #ifndef _TelepathyQt_optional_interface_factory_h_HEADER_GUARD_
00024 #define _TelepathyQt_optional_interface_factory_h_HEADER_GUARD_
00025
00026 #ifndef IN_TP_QT_HEADER
00027 #error IN_TP_QT_HEADER
00028 #endif
00029
00030 #include <TelepathyQt/Global>
00031
00032 #include <QObject>
00033 #include <QStringList>
00034 #include <QtGlobal>
00035
00036 namespace Tp
00037 {
00038
00039 class AbstractInterface;
00040
00041 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00042
00043 class TP_QT_EXPORT OptionalInterfaceCache
00044 {
00045 Q_DISABLE_COPY(OptionalInterfaceCache)
00046
00047 public:
00048 explicit OptionalInterfaceCache(QObject *proxy);
00049
00050 ~OptionalInterfaceCache();
00051
00052 protected:
00053 AbstractInterface *getCached(const QString &name) const;
00054 void cache(AbstractInterface *interface) const;
00055 QObject *proxy() const;
00056
00057 private:
00058 struct Private;
00059 friend struct Private;
00060 Private *mPriv;
00061 };
00062
00063 #endif
00064
00065 template <typename DBusProxySubclass> class OptionalInterfaceFactory
00066 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00067 : private OptionalInterfaceCache
00068 #endif
00069 {
00070 Q_DISABLE_COPY(OptionalInterfaceFactory)
00071
00072 public:
00073 enum InterfaceSupportedChecking
00074 {
00075 CheckInterfaceSupported,
00076 BypassInterfaceCheck
00077 };
00078
00079 inline OptionalInterfaceFactory(DBusProxySubclass *this_)
00080 : OptionalInterfaceCache(this_)
00081 {
00082 }
00083
00084 inline ~OptionalInterfaceFactory()
00085 {
00086 }
00087
00088 inline QStringList interfaces() const { return mInterfaces; }
00089
00090 inline bool hasInterface(const QString &name) const
00091 {
00092 return mInterfaces.contains(name);
00093 }
00094
00095 template <class Interface>
00096 inline Interface *optionalInterface(
00097 InterfaceSupportedChecking check = CheckInterfaceSupported) const
00098 {
00099
00100
00101
00102 QString name( (QLatin1String(Interface::staticInterfaceName())) );
00103 if (check == CheckInterfaceSupported && !mInterfaces.contains(name)) {
00104 return 0;
00105 }
00106
00107
00108 return interface<Interface>();
00109 }
00110
00111 template <typename Interface>
00112 inline Interface *interface() const
00113 {
00114 AbstractInterface* interfaceMustBeASubclassOfAbstractInterface = static_cast<Interface *>(NULL);
00115 Q_UNUSED(interfaceMustBeASubclassOfAbstractInterface);
00116
00117
00118
00119
00120 QString name( (QLatin1String(Interface::staticInterfaceName())) );
00121 AbstractInterface *cached = getCached(name);
00122 if (cached)
00123 return static_cast<Interface *>(cached);
00124
00125
00126 Interface *interface = new Interface(
00127 static_cast<DBusProxySubclass *>(proxy()));
00128 cache(interface);
00129 return interface;
00130 }
00131
00132 protected:
00133 inline void setInterfaces(const QStringList &interfaces)
00134 {
00135 mInterfaces = interfaces;
00136 }
00137
00138 private:
00139 QStringList mInterfaces;
00140 };
00141
00142 }
00143
00144 #endif