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     // TODO make it const
00084     inline bool hasInterface(const char *name)
00085     {
00086         return mInterfaces.contains(QLatin1String(name));
00087     }
00088 
00089     // TODO make it const
00090     inline bool hasInterface(const QString &name)
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-2010 Collabora Ltd. and Nokia Corporation
Telepathy-Qt4 0.3.13