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


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