Home · All Classes · All Namespaces · Modules · Functions · Files

optional-interface-factory.h

00001 /*
00002  * This file is part of TelepathyQt4
00003  *
00004  * Copyright (C) 2008-2009 Collabora Ltd. <http://www.collabora.co.uk/>
00005  * Copyright (C) 2008-2009 Nokia Corporation
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020  */
00021 
00022 #ifndef _TelepathyQt4_optional_interface_factory_h_HEADER_GUARD_
00023 #define _TelepathyQt4_optional_interface_factory_h_HEADER_GUARD_
00024 
00025 #ifndef IN_TELEPATHY_QT4_HEADER
00026 #error IN_TELEPATHY_QT4_HEADER
00027 #endif
00028 
00029 #include <TelepathyQt4/Global>
00030 
00031 #include <QObject>
00032 #include <QStringList>
00033 #include <QtGlobal>
00034 
00035 namespace Tp
00036 {
00037 
00038 class AbstractInterface;
00039 
00040 class TELEPATHY_QT4_EXPORT OptionalInterfaceCache
00041 {
00042     Q_DISABLE_COPY(OptionalInterfaceCache)
00043 
00044 public:
00045     explicit OptionalInterfaceCache(QObject *proxy);
00046 
00047     ~OptionalInterfaceCache();
00048 
00049 protected:
00050     AbstractInterface *getCached(const QString &name) const;
00051     void cache(AbstractInterface *interface) const;
00052     QObject *proxy() const;
00053 
00054 private:
00055     struct Private;
00056     friend struct Private;
00057     Private *mPriv;
00058 };
00059 
00060 template <typename DBusProxySubclass> class OptionalInterfaceFactory
00061     : private OptionalInterfaceCache
00062 {
00063     Q_DISABLE_COPY(OptionalInterfaceFactory)
00064 
00065 public:
00066     enum InterfaceSupportedChecking
00067     {
00068         CheckInterfaceSupported,
00069         BypassInterfaceCheck
00070     };
00071 
00072     inline OptionalInterfaceFactory(DBusProxySubclass *this_)
00073         : OptionalInterfaceCache(this_)
00074     {
00075     }
00076 
00077     inline ~OptionalInterfaceFactory()
00078     {
00079     }
00080 
00081     inline QStringList interfaces() const { return mInterfaces; }
00082 
00083     TELEPATHY_QT4_DEPRECATED inline bool hasInterface(const char *name)
00084     {
00085         return mInterfaces.contains(QLatin1String(name));
00086     }
00087 
00088     // FIXME: (API/ABI break) make it const
00089     inline bool hasInterface(const QString &name)
00090     {
00091         return mInterfaces.contains(name);
00092     }
00093 
00094     template <class Interface>
00095     inline Interface *optionalInterface(
00096             InterfaceSupportedChecking check = CheckInterfaceSupported) const
00097     {
00098         // Check for the remote object supporting the interface
00099         QString name(QLatin1String(Interface::staticInterfaceName()));
00100         if (check == CheckInterfaceSupported && !mInterfaces.contains(name)) {
00101             return 0;
00102         }
00103 
00104         // If present or forced, delegate to OptionalInterfaceFactory
00105         return interface<Interface>();
00106     }
00107 
00108     template <typename Interface>
00109     inline Interface *interface() const
00110     {
00111         AbstractInterface* interfaceMustBeASubclassOfAbstractInterface = static_cast<Interface *>(NULL);
00112         Q_UNUSED(interfaceMustBeASubclassOfAbstractInterface);
00113 
00114         // If there is a interface cached already, return it
00115         QString name(QLatin1String(Interface::staticInterfaceName()));
00116         AbstractInterface *cached = getCached(name);
00117         if (cached)
00118             return static_cast<Interface *>(cached);
00119 
00120         // Otherwise, cache and return a newly constructed proxy
00121         Interface *interface = new Interface(
00122                 static_cast<DBusProxySubclass *>(proxy()));
00123         cache(interface);
00124         return interface;
00125     }
00126 
00127 protected:
00128     inline void setInterfaces(const QStringList &interfaces)
00129     {
00130         mInterfaces = interfaces;
00131     }
00132 
00133 private:
00134     QStringList mInterfaces;
00135 };
00136 
00137 } // Tp
00138 
00139 #endif


Copyright © 2008-2010 Collabora Ltd. and Nokia Corporation
Telepathy-Qt4 0.4.4