1 /* PlayerPluginHandler.cpp */
2
3 /* Copyright (C) 2011-2020 Michael Lugmair (Lucio Carreras)
4 *
5 * This file is part of sayonara player
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "PlayerPluginBase.h"
22 #include "PlayerPluginHandler.h"
23
24 #include "Utils/Algorithm.h"
25 #include "Utils/Logger/Logger.h"
26 #include "Utils/Settings/Settings.h"
27
28 #include <QAction>
29
30 using PlayerPlugin::Handler;
31 using PlayerPlugin::Base;
32
33 namespace Algorithm=Util::Algorithm;
34
35 struct Handler::Private
36 {
37 QList<Base*> plugins;
38 Base* currentPlugin=nullptr;
39
40 bool isShutdown;
41
PrivateHandler::Private42 Private() :
43 isShutdown(false)
44 {}
45 };
46
Handler()47 Handler::Handler() :
48 QObject()
49 {
50 m = Pimpl::make<Private>();
51
52 ListenSetting(Set::Player_Language, Handler::languageChanged);
53 }
54
55 Handler::~Handler() = default;
56
shutdown()57 void Handler::shutdown()
58 {
59 m->isShutdown = true;
60
61 if(m->currentPlugin)
62 {
63 SetSetting(Set::Player_ShownPlugin, m->currentPlugin->name());
64 }
65
66 else {
67 SetSetting(Set::Player_ShownPlugin, QString());
68 }
69
70 for(Base* plugin : m->plugins)
71 {
72 plugin->close();
73 plugin->deleteLater();
74 }
75
76 m->plugins.clear();
77 }
78
79
findPlugin(const QString & name)80 Base* Handler::findPlugin(const QString& name)
81 {
82 for(Base* p : Algorithm::AsConst(m->plugins))
83 {
84 if(p->name().compare(name) == 0)
85 {
86 return p;
87 }
88 }
89
90 return nullptr;
91 }
92
93
addPlugin(Base * plugin)94 void Handler::addPlugin(Base* plugin)
95 {
96 if(!plugin){
97 return;
98 }
99
100 m->plugins.push_back(plugin);
101
102 connect(plugin, &Base::sigActionTriggered, this, &Handler::pluginActionTriggered);
103
104 QString last_plugin = GetSetting(Set::Player_ShownPlugin);
105 if(plugin->name() == last_plugin)
106 {
107 m->currentPlugin = plugin;
108 plugin->pluginAction()->setChecked(true);
109 }
110
111 emit sigPluginAdded(plugin);
112 }
113
showPlugin(const QString & name)114 void Handler::showPlugin(const QString& name)
115 {
116 Base* plugin = findPlugin(name);
117 if(!plugin)
118 {
119 return;
120 }
121
122 m->currentPlugin = plugin;
123 m->currentPlugin->pluginAction()->trigger();
124 }
125
pluginActionTriggered(bool b)126 void Handler::pluginActionTriggered(bool b)
127 {
128 if(m->isShutdown)
129 {
130 return;
131 }
132
133 Base* plugin = static_cast<Base*>(sender());
134
135 if(b)
136 {
137 if(plugin)
138 {
139 m->currentPlugin = plugin;
140 }
141 }
142
143 else
144 {
145 if(m->currentPlugin == plugin)
146 {
147 m->currentPlugin = nullptr;
148 }
149 }
150
151 emit sigPluginActionTriggered(b);
152 }
153
languageChanged()154 void Handler::languageChanged()
155 {
156 for(Base* p : Algorithm::AsConst(m->plugins))
157 {
158 p->languageChanged();
159 p->pluginAction()->setText(p->displayName());
160 }
161 }
162
allPlugins() const163 QList<Base*> Handler::allPlugins() const
164 {
165 return m->plugins;
166 }
167
currentPlugin() const168 Base* Handler::currentPlugin() const
169 {
170 return m->currentPlugin;
171 }
172