2010-12-31 19:13:28 +01:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
|
|
|
|
|
|
|
Clementine 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 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
2011-05-15 15:41:54 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_SCRIPTING_PYTHON
|
|
|
|
# include "scripting/python/pythonengine.h"
|
|
|
|
#endif
|
|
|
|
|
2010-12-31 19:13:28 +01:00
|
|
|
#include "languageengine.h"
|
|
|
|
#include "script.h"
|
2011-01-01 20:03:33 +01:00
|
|
|
#include "scriptinterface.h"
|
2010-12-31 19:13:28 +01:00
|
|
|
#include "scriptmanager.h"
|
2011-01-02 19:10:26 +01:00
|
|
|
#include "uiinterface.h"
|
2011-04-22 18:50:29 +02:00
|
|
|
#include "core/logging.h"
|
2010-12-31 19:13:28 +01:00
|
|
|
#include "core/utilities.h"
|
|
|
|
|
2011-01-16 17:43:19 +01:00
|
|
|
#ifdef Q_OS_MAC
|
|
|
|
# include "core/mac_startup.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <QCoreApplication>
|
2010-12-31 19:13:28 +01:00
|
|
|
#include <QDirIterator>
|
2011-01-16 16:11:23 +01:00
|
|
|
#include <QFileSystemWatcher>
|
2010-12-31 19:13:28 +01:00
|
|
|
#include <QSettings>
|
2011-01-02 02:07:19 +01:00
|
|
|
#include <QTextDocument>
|
2011-01-16 16:11:23 +01:00
|
|
|
#include <QTimer>
|
2010-12-31 19:13:28 +01:00
|
|
|
#include <QtDebug>
|
|
|
|
|
2011-01-01 19:37:08 +01:00
|
|
|
const char* ScriptManager::kSettingsGroup = "Scripts";
|
2010-12-31 19:13:28 +01:00
|
|
|
|
|
|
|
ScriptManager::ScriptManager(QObject* parent)
|
2011-01-02 19:10:26 +01:00
|
|
|
: QAbstractListModel(parent),
|
2011-01-16 16:11:23 +01:00
|
|
|
ui_interface_(new UIInterface(this)),
|
|
|
|
watcher_(new QFileSystemWatcher(this)),
|
|
|
|
rescan_timer_(new QTimer(this))
|
2010-12-31 19:13:28 +01:00
|
|
|
{
|
|
|
|
#ifdef HAVE_SCRIPTING_PYTHON
|
2011-01-02 02:07:19 +01:00
|
|
|
engines_ << new PythonEngine(this);
|
2010-12-31 19:13:28 +01:00
|
|
|
#endif
|
|
|
|
|
2011-01-16 16:11:23 +01:00
|
|
|
connect(watcher_, SIGNAL(directoryChanged(QString)), SLOT(ScriptDirectoryChanged()));
|
|
|
|
|
|
|
|
rescan_timer_->setSingleShot(true);
|
|
|
|
rescan_timer_->setInterval(1000);
|
|
|
|
connect(rescan_timer_, SIGNAL(timeout()), SLOT(RescanScripts()));
|
|
|
|
|
|
|
|
// Create the user's scripts directory if it doesn't exist yet
|
|
|
|
QString local_path = Utilities::GetConfigPath(Utilities::Path_Scripts);
|
|
|
|
if (!QFile::exists(local_path)) {
|
|
|
|
if (!QDir().mkpath(local_path)) {
|
2011-04-22 18:50:29 +02:00
|
|
|
qLog(Warning) << "Couldn't create directory" << local_path;
|
2011-01-16 16:11:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
search_paths_
|
|
|
|
<< local_path
|
|
|
|
#ifdef USE_INSTALL_PREFIX
|
|
|
|
<< CMAKE_INSTALL_PREFIX "/share/clementine/scripts"
|
|
|
|
#endif
|
|
|
|
<< "/usr/share/clementine/scripts"
|
|
|
|
<< "/usr/local/share/clementine/scripts";
|
|
|
|
|
|
|
|
#if defined(Q_OS_WIN32)
|
|
|
|
search_paths_ << QCoreApplication::applicationDirPath() + "/scripts";
|
|
|
|
#elif defined(Q_OS_MAC)
|
2011-01-16 18:26:25 +01:00
|
|
|
search_paths_ << mac::GetResourcesPath() + "/scripts";
|
2011-01-16 16:11:23 +01:00
|
|
|
#endif
|
2011-04-24 19:02:32 +02:00
|
|
|
|
|
|
|
qLog(Debug) << "Script search paths:" << search_paths_;
|
2010-12-31 19:13:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ScriptManager::~ScriptManager() {
|
2011-01-01 19:37:08 +01:00
|
|
|
foreach (const ScriptInfo& info, info_) {
|
2011-01-17 21:01:16 +01:00
|
|
|
if (info.loaded()) {
|
|
|
|
info.loaded()->language()->DestroyScript(info.loaded());
|
2011-01-01 19:37:08 +01:00
|
|
|
}
|
|
|
|
}
|
2011-01-28 21:52:38 +01:00
|
|
|
qDeleteAll(engines_);
|
2010-12-31 19:13:28 +01:00
|
|
|
}
|
|
|
|
|
2010-12-31 21:29:52 +01:00
|
|
|
void ScriptManager::Init(const GlobalData& data) {
|
2011-04-24 19:02:32 +02:00
|
|
|
qLog(Debug) << "Initialising";
|
|
|
|
|
2011-01-02 02:07:19 +01:00
|
|
|
data_ = data;
|
2010-12-31 21:29:52 +01:00
|
|
|
|
2011-01-01 19:37:08 +01:00
|
|
|
// Load settings
|
|
|
|
LoadSettings();
|
2010-12-31 19:13:28 +01:00
|
|
|
|
|
|
|
// Search for scripts
|
2011-01-16 16:11:23 +01:00
|
|
|
info_ = LoadAllScriptInfo().values();
|
|
|
|
|
|
|
|
// Enable the ones that were enabled last time
|
|
|
|
for (int i=0 ; i<info_.count() ; ++i) {
|
|
|
|
MaybeAutoEnable(&info_[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptManager::MaybeAutoEnable(ScriptInfo* info) {
|
|
|
|
// Load the script if it's enabled
|
2011-01-17 21:01:16 +01:00
|
|
|
if (enabled_scripts_.contains(info->id())) {
|
2011-01-16 16:11:23 +01:00
|
|
|
// Find an engine for it
|
2011-01-17 21:01:16 +01:00
|
|
|
LanguageEngine* engine = EngineForLanguage(info->language());
|
2011-01-16 16:11:23 +01:00
|
|
|
if (!engine) {
|
2011-04-22 18:50:29 +02:00
|
|
|
qLog(Warning) << "Unknown language in" << info->path();
|
2011-01-16 16:11:23 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-01-17 21:01:16 +01:00
|
|
|
info->set_loaded(engine->CreateScript(*info));
|
|
|
|
if (!info->loaded()) {
|
2011-01-16 16:11:23 +01:00
|
|
|
// Failed to load? Disable it so we don't try again
|
2011-01-17 21:01:16 +01:00
|
|
|
enabled_scripts_.remove(info->id());
|
2011-01-16 16:11:23 +01:00
|
|
|
SaveSettings();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-17 21:01:16 +01:00
|
|
|
QMap<QString, ScriptInfo> ScriptManager::LoadAllScriptInfo() const {
|
2011-01-16 16:11:23 +01:00
|
|
|
QMap<QString, ScriptInfo> ret;
|
|
|
|
|
2010-12-31 19:13:28 +01:00
|
|
|
foreach (const QString& search_path, search_paths_) {
|
2011-01-16 16:11:23 +01:00
|
|
|
if (!QFile::exists(search_path))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!watcher_->directories().contains(search_path)) {
|
2011-04-24 19:02:32 +02:00
|
|
|
qLog(Debug) << "Adding directory watch:" << search_path;
|
2011-01-16 16:11:23 +01:00
|
|
|
watcher_->addPath(search_path);
|
|
|
|
}
|
|
|
|
|
2010-12-31 19:13:28 +01:00
|
|
|
QDirIterator it(search_path,
|
|
|
|
QDir::Dirs | QDir::NoDotAndDotDot | QDir::Readable | QDir::Executable,
|
|
|
|
QDirIterator::FollowSymlinks);
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
const QString path = it.filePath();
|
2011-01-16 16:11:23 +01:00
|
|
|
if (!watcher_->directories().contains(path)) {
|
2011-04-24 19:02:32 +02:00
|
|
|
qLog(Debug) << "Adding directory watch:" << path;
|
2011-01-16 16:11:23 +01:00
|
|
|
watcher_->addPath(path);
|
|
|
|
}
|
2010-12-31 19:13:28 +01:00
|
|
|
|
2011-01-17 21:01:16 +01:00
|
|
|
ScriptInfo info;
|
|
|
|
info.InitFromDirectory(this, path);
|
2010-12-31 19:13:28 +01:00
|
|
|
if (!info.is_valid()) {
|
2011-04-22 18:50:29 +02:00
|
|
|
qLog(Warning) << "Not a valid Clementine script directory, ignoring:"
|
2011-04-24 19:02:32 +02:00
|
|
|
<< path;
|
2010-12-31 19:13:28 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-01-17 21:01:16 +01:00
|
|
|
if (ret.contains(info.id())) {
|
2011-01-16 16:11:23 +01:00
|
|
|
// Seen this script already
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-01-17 21:01:16 +01:00
|
|
|
ret.insert(info.id(), info);
|
2010-12-31 19:13:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-16 16:11:23 +01:00
|
|
|
return ret;
|
2010-12-31 19:13:28 +01:00
|
|
|
}
|
|
|
|
|
2011-01-01 19:37:08 +01:00
|
|
|
void ScriptManager::LoadSettings() {
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(kSettingsGroup);
|
|
|
|
enabled_scripts_ = QSet<QString>::fromList(
|
|
|
|
s.value("enabled_scripts").toStringList());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptManager::SaveSettings() const {
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(kSettingsGroup);
|
|
|
|
s.setValue("enabled_scripts", QVariant::fromValue<QStringList>(enabled_scripts_.toList()));
|
|
|
|
}
|
|
|
|
|
2010-12-31 19:13:28 +01:00
|
|
|
LanguageEngine* ScriptManager::EngineForLanguage(const QString& language_name) const {
|
|
|
|
foreach (LanguageEngine* engine, engines_) {
|
|
|
|
if (engine->name() == language_name) {
|
|
|
|
return engine;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-01-17 21:01:16 +01:00
|
|
|
LanguageEngine* ScriptManager::EngineForLanguage(ScriptInfo::Language language) const {
|
2010-12-31 19:13:28 +01:00
|
|
|
foreach (LanguageEngine* engine, engines_) {
|
|
|
|
if (engine->language() == language) {
|
|
|
|
return engine;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ScriptManager::rowCount(const QModelIndex& parent) const {
|
|
|
|
if (parent.isValid())
|
|
|
|
return 0;
|
|
|
|
return info_.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant ScriptManager::data(const QModelIndex& index, int role) const {
|
|
|
|
if (index.parent().isValid() || index.row() < 0 || index.row() >= info_.count())
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
const ScriptInfo& info = info_[index.row()];
|
|
|
|
|
|
|
|
switch (role) {
|
|
|
|
case Qt::DisplayRole:
|
2011-01-17 21:01:16 +01:00
|
|
|
return info.name();
|
2010-12-31 19:13:28 +01:00
|
|
|
|
2011-01-01 19:37:08 +01:00
|
|
|
case Qt::DecorationRole:
|
2011-01-17 21:01:16 +01:00
|
|
|
return info.icon();
|
2011-01-01 19:37:08 +01:00
|
|
|
|
2010-12-31 19:13:28 +01:00
|
|
|
case Role_Author:
|
2011-01-17 21:01:16 +01:00
|
|
|
return info.author();
|
2010-12-31 19:13:28 +01:00
|
|
|
|
|
|
|
case Role_Description:
|
2011-01-17 21:01:16 +01:00
|
|
|
return info.description();
|
2010-12-31 19:13:28 +01:00
|
|
|
|
|
|
|
case Role_Language:
|
2011-01-17 21:01:16 +01:00
|
|
|
return info.language();
|
2010-12-31 19:13:28 +01:00
|
|
|
|
|
|
|
case Role_ScriptFile:
|
2011-01-17 21:01:16 +01:00
|
|
|
return info.script_file();
|
2010-12-31 19:13:28 +01:00
|
|
|
|
|
|
|
case Role_Url:
|
2011-01-17 21:01:16 +01:00
|
|
|
return info.url();
|
2010-12-31 19:13:28 +01:00
|
|
|
|
2011-01-01 19:37:08 +01:00
|
|
|
case Role_IsEnabled:
|
2011-01-17 21:01:16 +01:00
|
|
|
return info.loaded() != NULL;
|
2011-01-01 19:37:08 +01:00
|
|
|
|
2010-12-31 19:13:28 +01:00
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
2011-01-01 19:37:08 +01:00
|
|
|
|
|
|
|
void ScriptManager::Enable(const QModelIndex& index) {
|
|
|
|
if (index.row() < 0 || index.row() >= info_.count())
|
|
|
|
return;
|
|
|
|
|
|
|
|
ScriptInfo* info = &info_[index.row()];
|
2011-01-17 21:01:16 +01:00
|
|
|
if (info->loaded())
|
2011-01-01 19:37:08 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Find an engine for it
|
2011-01-17 21:01:16 +01:00
|
|
|
LanguageEngine* engine = EngineForLanguage(info->language());
|
2011-01-01 19:37:08 +01:00
|
|
|
if (!engine) {
|
2011-04-22 18:50:29 +02:00
|
|
|
qLog(Warning) << "Unknown language in" << info->path();
|
2011-01-01 19:37:08 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the script
|
2011-01-17 21:01:16 +01:00
|
|
|
info->set_loaded(engine->CreateScript(*info));
|
2011-01-01 19:37:08 +01:00
|
|
|
|
|
|
|
// If it loaded correctly then automatically load it in the future
|
2011-01-17 21:01:16 +01:00
|
|
|
if (info->loaded()) {
|
|
|
|
enabled_scripts_.insert(info->id());
|
2011-01-01 19:37:08 +01:00
|
|
|
SaveSettings();
|
|
|
|
}
|
|
|
|
|
|
|
|
emit dataChanged(index, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptManager::Disable(const QModelIndex& index) {
|
|
|
|
if (index.row() < 0 || index.row() >= info_.count())
|
|
|
|
return;
|
|
|
|
|
|
|
|
ScriptInfo* info = &info_[index.row()];
|
2011-01-17 21:01:16 +01:00
|
|
|
if (!info->loaded())
|
2011-01-01 19:37:08 +01:00
|
|
|
return;
|
|
|
|
|
2011-01-17 21:01:16 +01:00
|
|
|
info->loaded()->language()->DestroyScript(info->loaded());
|
|
|
|
info->set_loaded(NULL);
|
2011-01-01 19:37:08 +01:00
|
|
|
|
2011-01-17 21:01:16 +01:00
|
|
|
enabled_scripts_.remove(info->id());
|
2011-01-01 19:37:08 +01:00
|
|
|
SaveSettings();
|
|
|
|
|
|
|
|
emit dataChanged(index, index);
|
|
|
|
}
|
2011-01-01 20:03:33 +01:00
|
|
|
|
|
|
|
void ScriptManager::ShowSettingsDialog(const QModelIndex& index) {
|
|
|
|
if (index.row() < 0 || index.row() >= info_.count())
|
|
|
|
return;
|
|
|
|
|
|
|
|
ScriptInfo* info = &info_[index.row()];
|
2011-01-17 21:01:16 +01:00
|
|
|
if (!info->loaded())
|
2011-01-01 20:03:33 +01:00
|
|
|
return;
|
|
|
|
|
2011-01-17 21:01:16 +01:00
|
|
|
info->loaded()->interface()->ShowSettingsDialog();
|
2011-01-01 20:03:33 +01:00
|
|
|
}
|
2011-01-02 02:07:19 +01:00
|
|
|
|
|
|
|
void ScriptManager::AddLogLine(const QString& who, const QString& message, bool error) {
|
|
|
|
foreach (const QString& line, message.split("\n", QString::SkipEmptyParts)) {
|
|
|
|
QString plain = "[" + who + "] " + line;
|
|
|
|
QString html = "<b>[" + Qt::escape(who) + "]</b> " + Qt::escape(line);
|
|
|
|
html.replace(' ', " ");
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
html = "<font color=\"red\">" + html + "</font>";
|
|
|
|
}
|
|
|
|
|
|
|
|
log_lines_ << html;
|
2011-01-28 21:52:38 +01:00
|
|
|
log_lines_plain_ << plain;
|
2011-01-02 02:07:19 +01:00
|
|
|
emit LogLineAdded(html);
|
|
|
|
}
|
|
|
|
}
|
2011-01-16 16:11:23 +01:00
|
|
|
|
|
|
|
void ScriptManager::ScriptDirectoryChanged() {
|
|
|
|
rescan_timer_->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptManager::RescanScripts() {
|
|
|
|
// Get the new list of scripts
|
|
|
|
QMap<QString, ScriptInfo> new_info = LoadAllScriptInfo();
|
|
|
|
|
|
|
|
// Look at existing scripts, find ones that have changed or been deleted
|
|
|
|
for (int i=0 ; i<info_.count() ; ++i) {
|
|
|
|
ScriptInfo* info = &info_[i];
|
2011-01-17 21:01:16 +01:00
|
|
|
const QString id = info->id();
|
2011-01-16 16:11:23 +01:00
|
|
|
|
|
|
|
if (!new_info.contains(id)) {
|
|
|
|
// This script was deleted - unload it and remove it from the model
|
|
|
|
Disable(index(i));
|
|
|
|
|
|
|
|
beginRemoveRows(QModelIndex(), i, i);
|
|
|
|
info_.removeAt(i);
|
|
|
|
endRemoveRows();
|
|
|
|
|
|
|
|
--i;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*info != new_info[id]) {
|
|
|
|
// This script was modified - change the metadata in the existing entry
|
|
|
|
info->TakeMetadataFrom(new_info[id]);
|
|
|
|
|
|
|
|
emit dataChanged(index(i), index(i));
|
|
|
|
|
|
|
|
AddLogLine("Watcher",
|
|
|
|
tr("The '%1' script was modified, you might have to reload it").arg(id), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
new_info.remove(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Things that are left in new_info are newly added scripts
|
|
|
|
if (!new_info.isEmpty()) {
|
|
|
|
const int begin = info_.count();
|
|
|
|
const int end = begin + new_info.count() - 1;
|
|
|
|
|
|
|
|
beginInsertRows(QModelIndex(), begin, end);
|
|
|
|
info_.append(new_info.values());
|
|
|
|
|
|
|
|
for (int i=begin ; i<=end ; ++i) {
|
|
|
|
MaybeAutoEnable(&info_[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
}
|