| 1 |
/* |
| 2 |
* This program is free software; you can redistribute it and/or modify |
| 3 |
* it under the terms of the GNU General Public License as published by |
| 4 |
* the Free Software Foundation; either version 2 of the License, or |
| 5 |
* (at your option) any later version. |
| 6 |
* |
| 7 |
* This program is distributed in the hope that it will be useful, |
| 8 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 |
* GNU Library General Public License for more details. |
| 11 |
* |
| 12 |
* You should have received a copy of the GNU General Public License |
| 13 |
* along with this program; if not, write to the Free Software |
| 14 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 15 |
* |
| 16 |
* Plugin.cpp |
| 17 |
* Base plugin class for ola |
| 18 |
* Copyright (C) 2005 Simon Newton |
| 19 |
*/ |
| 20 |
|
| 21 |
#include <string> |
| 22 |
#include "ola/Logging.h" |
| 23 |
#include "olad/Plugin.h" |
| 24 |
#include "olad/PluginAdaptor.h" |
| 25 |
#include "olad/Preferences.h" |
| 26 |
|
| 27 |
namespace ola { |
| 28 |
|
| 29 |
using std::string; |
| 30 |
|
| 31 |
const char Plugin::ENABLED_KEY[] = "enabled"; |
| 32 |
|
| 33 |
bool Plugin::LoadPreferences() { |
| 34 |
if (m_preferences) { |
| 35 |
return true; |
| 36 |
} |
| 37 |
|
| 38 |
if (PluginPrefix() == "") { |
| 39 |
OLA_WARN << Name() << ", no prefix provided"; |
| 40 |
return false; |
| 41 |
} |
| 42 |
|
| 43 |
m_preferences = m_plugin_adaptor->NewPreference(PluginPrefix()); |
| 44 |
|
| 45 |
if (!m_preferences) { |
| 46 |
return false; |
| 47 |
} |
| 48 |
|
| 49 |
m_preferences->Load(); |
| 50 |
|
| 51 |
bool save = m_preferences->SetDefaultValue( |
| 52 |
ENABLED_KEY, |
| 53 |
BoolValidator(), |
| 54 |
DefaultMode()); |
| 55 |
if (save) { |
| 56 |
m_preferences->Save(); |
| 57 |
} |
| 58 |
|
| 59 |
if (!SetDefaultPreferences()) { |
| 60 |
OLA_INFO << Name() << ", SetDefaultPreferences failed"; |
| 61 |
return false; |
| 62 |
} |
| 63 |
return true; |
| 64 |
} |
| 65 |
|
| 66 |
string Plugin::PreferenceConfigLocation() const { |
| 67 |
return m_preferences->ConfigLocation(); |
| 68 |
} |
| 69 |
|
| 70 |
bool Plugin::IsEnabled() const { |
| 71 |
return m_preferences->GetValueAsBool(ENABLED_KEY); |
| 72 |
} |
| 73 |
|
| 74 |
void Plugin::SetEnabledState(bool enable) { |
| 75 |
m_preferences->SetValueAsBool(ENABLED_KEY, enable); |
| 76 |
m_preferences->Save(); |
| 77 |
} |
| 78 |
|
| 79 |
bool Plugin::Start() { |
| 80 |
string enabled; |
| 81 |
|
| 82 |
if (m_enabled) { |
| 83 |
return false; |
| 84 |
} |
| 85 |
|
| 86 |
// setup prefs |
| 87 |
if (!LoadPreferences()) { |
| 88 |
return false; |
| 89 |
} |
| 90 |
|
| 91 |
if (!StartHook()) { |
| 92 |
return false; |
| 93 |
} |
| 94 |
|
| 95 |
m_enabled = true; |
| 96 |
return true; |
| 97 |
} |
| 98 |
|
| 99 |
bool Plugin::Stop() { |
| 100 |
if (!m_enabled) { |
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
bool ret = StopHook(); |
| 105 |
|
| 106 |
m_enabled = false; |
| 107 |
return ret; |
| 108 |
} |
| 109 |
} // namespace ola |