Home · All Classes · All Namespaces · Modules · Functions · Files
optional-interface-factory.h
00001 
00023 #ifndef _TelepathyQt4_optional_interface_factory_h_HEADER_GUARD_
00024 #define _TelepathyQt4_optional_interface_factory_h_HEADER_GUARD_
00025 
00026 #ifndef IN_TELEPATHY_QT4_HEADER
00027 #error IN_TELEPATHY_QT4_HEADER
00028 #endif
00029 
00030 #include <TelepathyQt4/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 TELEPATHY_QT4_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 /* DOXYGEN_SHOULD_SKIP_THIS */
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         // Check for the remote object supporting the interface
00100         QString name(QLatin1String(Interface::staticInterfaceName()));
00101         if (check == CheckInterfaceSupported && !mInterfaces.contains(name)) {
00102             return 0;
00103         }
00104 
00105         // If present or forced, delegate to OptionalInterfaceFactory
00106         return interface<Interface>();
00107     }
00108 
00109     template <typename Interface>
00110     inline Interface *interface() const
00111     {
00112         AbstractInterface* interfaceMustBeASubclassOfAbstractInterface = static_cast<Interface *>(NULL);
00113         Q_UNUSED(interfaceMustBeASubclassOfAbstractInterface);
00114 
00115         // If there is a interface cached already, return it
00116         QString name(QLatin1String(Interface::staticInterfaceName()));
00117         AbstractInterface *cached = getCached(name);
00118         if (cached)
00119             return static_cast<Interface *>(cached);
00120 
00121         // Otherwise, cache and return a newly constructed proxy
00122         Interface *interface = new Interface(
00123                 static_cast<DBusProxySubclass *>(proxy()));
00124         cache(interface);
00125         return interface;
00126     }
00127 
00128 protected:
00129     inline void setInterfaces(const QStringList &interfaces)
00130     {
00131         mInterfaces = interfaces;
00132     }
00133 
00134 private:
00135     QStringList mInterfaces;
00136 };
00137 
00138 } // Tp
00139 
00140 #endif


Copyright © 2008-2011 Collabora Ltd. and Nokia Corporation
Telepathy-Qt4 0.8.0