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     template <class Interface>
00084     inline Interface *optionalInterface(
00085             InterfaceSupportedChecking check = CheckInterfaceSupported) const
00086     {
00087         // Check for the remote object supporting the interface
00088         QString name(Interface::staticInterfaceName());
00089         if (check == CheckInterfaceSupported && !mInterfaces.contains(name)) {
00090             return 0;
00091         }
00092 
00093         // If present or forced, delegate to OptionalInterfaceFactory
00094         return interface<Interface>();
00095     }
00096 
00097     template <typename Interface>
00098     inline Interface *interface() const
00099     {
00100         AbstractInterface* interfaceMustBeASubclassOfAbstractInterface = static_cast<Interface *>(NULL);
00101         Q_UNUSED(interfaceMustBeASubclassOfAbstractInterface);
00102 
00103         // If there is a interface cached already, return it
00104         QString name(Interface::staticInterfaceName());
00105         AbstractInterface *cached = getCached(name);
00106         if (cached)
00107             return static_cast<Interface *>(cached);
00108 
00109         // Otherwise, cache and return a newly constructed proxy
00110         Interface *interface = new Interface(
00111                 static_cast<DBusProxySubclass *>(proxy()));
00112         cache(interface);
00113         return interface;
00114     }
00115 
00116 protected:
00117     inline void setInterfaces(const QStringList &interfaces)
00118     {
00119         mInterfaces = interfaces;
00120     }
00121 
00122 private:
00123     QStringList mInterfaces;
00124 };
00125 
00126 } // Tp
00127 
00128 #endif


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