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