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     : private OptionalInterfaceCache
00067 {
00068     Q_DISABLE_COPY(OptionalInterfaceFactory)
00069 
00070 public:
00071     enum InterfaceSupportedChecking
00072     {
00073         CheckInterfaceSupported,
00074         BypassInterfaceCheck
00075     };
00076 
00077     inline OptionalInterfaceFactory(DBusProxySubclass *this_)
00078         : OptionalInterfaceCache(this_)
00079     {
00080     }
00081 
00082     inline ~OptionalInterfaceFactory()
00083     {
00084     }
00085 
00086     inline QStringList interfaces() const { return mInterfaces; }
00087 
00088     inline bool hasInterface(const QString &name) const
00089     {
00090         return mInterfaces.contains(name);
00091     }
00092 
00093     template <class Interface>
00094     inline Interface *optionalInterface(
00095             InterfaceSupportedChecking check = CheckInterfaceSupported) const
00096     {
00097         // Check for the remote object supporting the interface
00098         QString name(QLatin1String(Interface::staticInterfaceName()));
00099         if (check == CheckInterfaceSupported && !mInterfaces.contains(name)) {
00100             return 0;
00101         }
00102 
00103         // If present or forced, delegate to OptionalInterfaceFactory
00104         return interface<Interface>();
00105     }
00106 
00107     template <typename Interface>
00108     inline Interface *interface() const
00109     {
00110         AbstractInterface* interfaceMustBeASubclassOfAbstractInterface = static_cast<Interface *>(NULL);
00111         Q_UNUSED(interfaceMustBeASubclassOfAbstractInterface);
00112 
00113         // If there is a interface cached already, return it
00114         QString name(QLatin1String(Interface::staticInterfaceName()));
00115         AbstractInterface *cached = getCached(name);
00116         if (cached)
00117             return static_cast<Interface *>(cached);
00118 
00119         // Otherwise, cache and return a newly constructed proxy
00120         Interface *interface = new Interface(
00121                 static_cast<DBusProxySubclass *>(proxy()));
00122         cache(interface);
00123         return interface;
00124     }
00125 
00126 protected:
00127     inline void setInterfaces(const QStringList &interfaces)
00128     {
00129         mInterfaces = interfaces;
00130     }
00131 
00132 private:
00133     QStringList mInterfaces;
00134 };
00135 
00136 } // Tp
00137 
00138 #endif


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