1 /*
2  * This file is part of Licq, an instant messaging client for UNIX.
3  * Copyright (C) 2010-2014 Licq developers <licq-dev@googlegroups.com>
4  *
5  * Licq is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * Licq is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with Licq; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19 
20 #include "protocolplugin.h"
21 #include "protocolplugininstance.h"
22 
23 #include <licq/plugin/protocolpluginfactory.h>
24 #include <licq/thread/mutexlocker.h>
25 
26 #include <boost/make_shared.hpp>
27 
28 using namespace LicqDaemon;
29 
ProtocolPlugin(DynamicLibrary::Ptr lib,boost::shared_ptr<Licq::ProtocolPluginFactory> factory,PluginThread::Ptr thread)30 ProtocolPlugin::ProtocolPlugin(
31     DynamicLibrary::Ptr lib,
32     boost::shared_ptr<Licq::ProtocolPluginFactory> factory,
33     PluginThread::Ptr thread)
34   : Plugin(lib),
35     myFactory(factory),
36     myMainThread(thread)
37 {
38   // Empty
39 }
40 
~ProtocolPlugin()41 ProtocolPlugin::~ProtocolPlugin()
42 {
43   // Empty
44 }
45 
46 boost::shared_ptr<ProtocolPluginInstance>
createInstance(int id,const Licq::UserId & ownerId,void (* callback)(const PluginInstance &))47 ProtocolPlugin::createInstance(int id, const Licq::UserId& ownerId,
48                                void (*callback)(const PluginInstance&))
49 {
50   PluginThread::Ptr thread;
51   if (myMainThread)
52     thread.swap(myMainThread);
53   else
54     thread = boost::make_shared<PluginThread>();
55 
56   ProtocolPluginInstance::Ptr instance =
57       boost::make_shared<ProtocolPluginInstance>(
58           id, ownerId,
59           boost::dynamic_pointer_cast<ProtocolPlugin>(shared_from_this()),
60           thread);
61 
62   if (instance->create(callback))
63     registerInstance(instance);
64   else
65     instance.reset();
66 
67   return instance;
68 }
69 
70 boost::shared_ptr<Licq::ProtocolPluginFactory>
protocolFactory()71 ProtocolPlugin::protocolFactory()
72 {
73   return myFactory;
74 }
75 
protocolId() const76 unsigned long ProtocolPlugin::protocolId() const
77 {
78   return myFactory->protocolId();
79 }
80 
capabilities() const81 unsigned long ProtocolPlugin::capabilities() const
82 {
83   return myFactory->capabilities();
84 }
85 
statuses() const86 unsigned long ProtocolPlugin::statuses() const
87 {
88   return myFactory->statuses();
89 }
90 
instances() const91 Licq::ProtocolPlugin::Instances ProtocolPlugin::instances() const
92 {
93   Instances list;
94 
95   Licq::MutexLocker locker(myMutex);
96 
97   for (std::vector< boost::weak_ptr<PluginInstance> >::const_iterator it =
98            myInstances.begin(); it != myInstances.end(); ++it)
99   {
100     ProtocolPluginInstance::Ptr instance =
101         boost::dynamic_pointer_cast<ProtocolPluginInstance>(it->lock());
102     if (instance)
103       list.push_back(instance);
104   }
105 
106   return list;
107 }
108 
createUser(const Licq::UserId & id,bool temporary)109 Licq::User* ProtocolPlugin::createUser(const Licq::UserId& id, bool temporary)
110 {
111   return myFactory->createUser(id, temporary);
112 }
113 
createOwner(const Licq::UserId & id)114 Licq::Owner* ProtocolPlugin::createOwner(const Licq::UserId& id)
115 {
116   return myFactory->createOwner(id);
117 }
118 
factory()119 boost::shared_ptr<Licq::PluginFactory> ProtocolPlugin::factory()
120 {
121   return myFactory;
122 }
123 
factory() const124 boost::shared_ptr<const Licq::PluginFactory> ProtocolPlugin::factory() const
125 {
126   return myFactory;
127 }
128