Skip to content
This repository has been archived by the owner on Dec 28, 2021. It is now read-only.

Commit

Permalink
licq: split plugin and plugin instance
Browse files Browse the repository at this point in the history
Plugins now consists of a Licq::Plugin class (Licq::GeneralPlugin or
Licq::ProtocolPlugin) and one or more Licq::PluginInstance class(es)
(Licq::GeneralPluginInstance or Licq::ProtocolPluginInstance). General plugins
always have at most one instance, while protocol plugins can have number of
them.

Each instance has an unique id and runs in a separate thread.
  • Loading branch information
erijo committed Feb 25, 2013
1 parent e5e1f08 commit 7cb33d9
Show file tree
Hide file tree
Showing 44 changed files with 1,510 additions and 671 deletions.
3 changes: 3 additions & 0 deletions licq/include/licq/plugin/CMakeLists.txt
Expand Up @@ -2,14 +2,17 @@ set(licq_HEADERS
generalplugin.h
generalpluginfactory.h
generalpluginhelper.h
generalplugininstance.h
generalplugininterface.h
plugin.h
pluginfactory.h
plugininstance.h
plugininterface.h
pluginmanager.h
protocolplugin.h
protocolpluginfactory.h
protocolpluginhelper.h
protocolplugininstance.h
protocolplugininterface.h
)

Expand Down
12 changes: 4 additions & 8 deletions licq/include/licq/plugin/generalplugin.h
Expand Up @@ -20,6 +20,7 @@
#ifndef LICQ_GENERALPLUGIN_H
#define LICQ_GENERALPLUGIN_H

#include "generalplugininstance.h"
#include "plugin.h"

namespace Licq
Expand All @@ -44,14 +45,9 @@ class GeneralPlugin : public virtual Plugin
/// to BASE_DIR
virtual std::string configFile() const = 0;

/// Get the plugin's status.
virtual bool isEnabled() const = 0;

/// Ask the plugin to enable itself
virtual void enable() = 0;

/// Ask the plugin to disable itself
virtual void disable() = 0;
/// Get the plugin instance for this plugin. May be NULL if the instance has
/// exited.
virtual GeneralPluginInstance::Ptr instance() const = 0;

protected:
/// Destructor
Expand Down
59 changes: 59 additions & 0 deletions licq/include/licq/plugin/generalplugininstance.h
@@ -0,0 +1,59 @@
/*
* This file is part of Licq, an instant messaging client for UNIX.
* Copyright (C) 2013 Licq Developers <licq-dev@googlegroups.com>
*
* Please refer to the COPYRIGHT file distributed with this source
* distribution for the names of the individual contributors.
*
* Licq is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Licq is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Licq; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef LICQ_GENERALPLUGININSTANCE_H
#define LICQ_GENERALPLUGININSTANCE_H

#include "plugininstance.h"

namespace Licq
{

class GeneralPlugin;

/**
* Represents an instance of a loaded general plugin.
*/
class GeneralPluginInstance : public virtual PluginInstance
{
public:
typedef boost::shared_ptr<GeneralPluginInstance> Ptr;

/// Get the plugin for this instance
virtual boost::shared_ptr<GeneralPlugin> plugin() const = 0;

/// Get the plugin's status.
virtual bool isEnabled() const = 0;

/// Ask the plugin to enable itself
virtual void enable() = 0;

/// Ask the plugin to disable itself
virtual void disable() = 0;

protected:
virtual ~GeneralPluginInstance() { /* Empty */ }
};

} // namespace Licq

#endif
47 changes: 1 addition & 46 deletions licq/include/licq/plugin/plugin.h
Expand Up @@ -20,8 +20,6 @@
#ifndef LICQ_PLUGIN_H
#define LICQ_PLUGIN_H

#include "plugininterface.h"

#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
#include <string>
Expand All @@ -35,12 +33,9 @@ namespace Licq
class Plugin : private boost::noncopyable
{
public:
/// A smart pointer to a Plugin instance
/// A smart pointer to a Plugin
typedef boost::shared_ptr<Plugin> Ptr;

/// Get the plugin's unique id.
virtual int id() const = 0;

/// Get the plugin's name.
virtual std::string name() const = 0;

Expand All @@ -53,48 +48,8 @@ class Plugin : private boost::noncopyable
protected:
/// Destructor
virtual ~Plugin() { }

virtual boost::shared_ptr<PluginInterface> internalInterface() = 0;
template <typename T> friend boost::shared_ptr<T> plugin_internal_cast(Ptr);
};

/**
* Function to cast a plugin to a plugin specific interface to get access to
* methods that only apply for a specific plugin. To e.g. get access to ICQ
* specific methods, do:
* @code
* Licq::IcqProtocol::Ptr icq = plugin_internal_cast<Licq::IcqProtocol>(
* Licq::gPluginManager.getProtocolPlugin(LICQ_PPID));
* if (icq)
* icq->icqSendSms(...);
* @endcode
*/
template <typename T>
inline boost::shared_ptr<T> plugin_internal_cast(Plugin::Ptr plugin)
{
return plugin
? boost::dynamic_pointer_cast<T>(plugin->internalInterface())
: boost::shared_ptr<T>();
}

// plugin_internal_cast<>() is not supposed to be used to cast to
// PluginInterface, GeneralPluginInterface or ProtocolPluginInterface; only to
// plugin specific interfaces.

template <> inline boost::shared_ptr<PluginInterface>
plugin_internal_cast<PluginInterface>(Plugin::Ptr);

class GeneralPluginInterface;
template <> boost::shared_ptr<GeneralPluginInterface>
plugin_internal_cast<GeneralPluginInterface>(Plugin::Ptr);

class ProtocolPluginInterface;
template <> boost::shared_ptr<ProtocolPluginInterface>
plugin_internal_cast<ProtocolPluginInterface>(Plugin::Ptr);

} // namespace Licq

// Make available in global namespace
using Licq::plugin_internal_cast;

#endif
92 changes: 92 additions & 0 deletions licq/include/licq/plugin/plugininstance.h
@@ -0,0 +1,92 @@
/*
* This file is part of Licq, an instant messaging client for UNIX.
* Copyright (C) 2013 Licq Developers <licq-dev@googlegroups.com>
*
* Please refer to the COPYRIGHT file distributed with this source
* distribution for the names of the individual contributors.
*
* Licq is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Licq is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Licq; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef LICQ_PLUGININSTANCE_H
#define LICQ_PLUGININSTANCE_H

#include "plugininterface.h"

#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>

namespace Licq
{

/**
* Represents an instance of a loaded plugin.
*/
class PluginInstance : private boost::noncopyable
{
public:
/// A smart pointer to a Plugin instance
typedef boost::shared_ptr<PluginInstance> Ptr;

/// Get the instance's unique id.
virtual int id() const = 0;

protected:
virtual ~PluginInstance() { }

virtual boost::shared_ptr<PluginInterface> internalInterface() = 0;
template <typename T> friend boost::shared_ptr<T> plugin_internal_cast(Ptr);
};

/**
* Function to cast a plugin instance to a plugin specific interface to get
* access to methods that only apply for a specific plugin. To e.g. get access
* to ICQ specific methods, do:
* @code
* Licq::IcqProtocol::Ptr icq = plugin_internal_cast<Licq::IcqProtocol>(
* Licq::gPluginManager.getProtocolInstance(LICQ_PPID));
* if (icq)
* icq->icqSendSms(...);
* @endcode
*/
template <typename T>
inline boost::shared_ptr<T> plugin_internal_cast(PluginInstance::Ptr plugin)
{
return plugin
? boost::dynamic_pointer_cast<T>(plugin->internalInterface())
: boost::shared_ptr<T>();
}

// plugin_internal_cast<>() is not supposed to be used to cast to
// PluginInterface, GeneralPluginInterface or ProtocolPluginInterface; only to
// plugin specific interfaces.

template <> inline boost::shared_ptr<PluginInterface>
plugin_internal_cast<PluginInterface>(PluginInstance::Ptr);

class GeneralPluginInterface;
template <> boost::shared_ptr<GeneralPluginInterface>
plugin_internal_cast<GeneralPluginInterface>(PluginInstance::Ptr);

class ProtocolPluginInterface;
template <> boost::shared_ptr<ProtocolPluginInterface>
plugin_internal_cast<ProtocolPluginInterface>(PluginInstance::Ptr);

} // namespace Licq

// Make available in global namespace
using Licq::plugin_internal_cast;

#endif
26 changes: 25 additions & 1 deletion licq/include/licq/plugin/pluginmanager.h
Expand Up @@ -26,13 +26,14 @@

#include "generalplugin.h"
#include "protocolplugin.h"

#include "protocolplugininstance.h"

namespace Licq
{
class Event;
class PluginSignal;
class ProtocolSignal;
class USerId;

typedef std::list<std::string> StringList;

Expand Down Expand Up @@ -104,6 +105,29 @@ class PluginManager : private boost::noncopyable
virtual
ProtocolPlugin::Ptr getProtocolPlugin(unsigned long protocolId) const = 0;

/**
* Get the protocol instance that is used for the given owner id.
*
* @param ownerId Owner to get protocol instance for.
* @return Pointer to the ProtocolPlugin (if found) or an empty pointer if
* owner is invalid.
*/
virtual ProtocolPluginInstance::Ptr
getProtocolInstance(const UserId& ownerId) const = 0;

/**
* Get a protocol instance that is used for the given protocol id.
*
* This method should only be used when it doesn't matter witch protocol
* instance is returned, or if the protocol only supports one instance.
*
* @param protocolId Protocol id to get protocol instance for.
* @return Pointer to the ProtocolPlugin (if found) or an empty pointer if
* protocol id is unknown.
*/
virtual ProtocolPluginInstance::Ptr
getProtocolInstance(unsigned long protocolId) const = 0;

/**
* Load and start the general plugin @a name.
*
Expand Down
8 changes: 8 additions & 0 deletions licq/include/licq/plugin/protocolplugin.h
Expand Up @@ -21,6 +21,9 @@
#define LICQ_PROTOCOLPLUGIN_H

#include "plugin.h"
#include "protocolplugininstance.h"

#include <vector>

namespace Licq
{
Expand All @@ -31,6 +34,8 @@ namespace Licq
class ProtocolPlugin : public virtual Plugin
{
public:
typedef std::vector<ProtocolPluginInstance::Ptr> Instances;

enum Capabilities
{
CanSendMsg = 1<<0,
Expand Down Expand Up @@ -62,6 +67,9 @@ class ProtocolPlugin : public virtual Plugin
*/
virtual unsigned long capabilities() const = 0;

/// Get all instances that are active for this protocol
virtual Instances instances() const = 0;

protected:
/// Destructor
virtual ~ProtocolPlugin() { }
Expand Down
54 changes: 54 additions & 0 deletions licq/include/licq/plugin/protocolplugininstance.h
@@ -0,0 +1,54 @@
/*
* This file is part of Licq, an instant messaging client for UNIX.
* Copyright (C) 2013 Licq Developers <licq-dev@googlegroups.com>
*
* Please refer to the COPYRIGHT file distributed with this source
* distribution for the names of the individual contributors.
*
* Licq is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Licq is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Licq; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef LICQ_PROTOCOLPLUGININSTANCE_H
#define LICQ_PROTOCOLPLUGININSTANCE_H

#include "plugininstance.h"

namespace Licq
{

class ProtocolPlugin;
class UserId;

/**
* Represents an instance of a loaded protocol plugin.
*/
class ProtocolPluginInstance : public virtual PluginInstance
{
public:
typedef boost::shared_ptr<ProtocolPluginInstance> Ptr;

/// Get the plugin for this instance
virtual boost::shared_ptr<ProtocolPlugin> plugin() const = 0;

/// Get the owner ID that is associated with this protocol instance.
virtual const UserId& ownerId() const = 0;

protected:
virtual ~ProtocolPluginInstance() { }
};

} // namespace Licq

#endif

0 comments on commit 7cb33d9

Please sign in to comment.