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


Copyright © 2009 Collabora Ltd. and Nokia Corporation
Telepathy-Qt4 0.1.10