Skip to content
This repository has been archived by the owner on May 3, 2019. It is now read-only.

Commit

Permalink
#69 Add user script manager interface
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinDuquesnoy committed Sep 24, 2017
1 parent a879b1e commit 731fc8e
Show file tree
Hide file tree
Showing 22 changed files with 421 additions and 46 deletions.
1 change: 1 addition & 0 deletions lib/MellowPlayer/Application/UserScripts/IUserScript.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ namespace MellowPlayer::Application

virtual bool import(const QString& path) = 0;
virtual bool load(const QString& path) = 0;
virtual void removeFile() const = 0;
};
}
28 changes: 16 additions & 12 deletions lib/MellowPlayer/Application/UserScripts/UserScripts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ UserScripts::UserScripts(const QString& serviceName,
auto name = scriptNames.at(i);
auto* userScript = userScriptFactory_.create();
userScript->setName(name);
userScript->load(path);
_scripts.append(userScript);
if (userScript->load(path))
_scripts.append(userScript);
}
}

Expand All @@ -35,17 +35,19 @@ int UserScripts::count() const
return _scripts.count();
}

IUserScript& UserScripts::add(const QString& userScriptName, const QString& sourceScriptPath)
IUserScript* UserScripts::add(const QString& userScriptName, const QString& sourceScriptPath)
{
auto* userScript = userScriptFactory_.create();
userScript->setName(userScriptName);
userScript->import(sourceScriptPath);

_scripts.append(userScript);

save(userScriptName, userScript);

return *userScript;
if (userScript->import(sourceScriptPath)) {
_scripts.append(userScript);
save(userScriptName, userScript);
return userScript;
}
else {
delete userScript;
return nullptr;
}

}

Expand All @@ -63,15 +65,17 @@ void UserScripts::remove(const QString& scriptName)
{
int index = 0;
for (index = 0; index < _scripts.count(); ++index) {
if (_scripts.at(index)->name() == scriptName) {
IUserScript* script = _scripts.at(index);
if (script->name() == scriptName) {
auto scriptPaths = settingsProvider_.value(pathsKey(), QStringList()).toStringList();
auto scriptNames = settingsProvider_.value(namesKey(), QStringList()).toStringList();
scriptNames.removeOne(_scripts.at(index)->name());
scriptPaths.removeOne(_scripts.at(index)->path());
settingsProvider_.setValue(pathsKey(), scriptPaths);
settingsProvider_.setValue(namesKey(), scriptNames);

script->removeFile();
_scripts.removeAt(index);
delete script;
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/MellowPlayer/Application/UserScripts/UserScripts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace MellowPlayer::Application
~UserScripts();

int count() const;
IUserScript& add(const QString& userScriptName, const QString& sourceScriptPath);
IUserScript* add(const QString& userScriptName, const QString& sourceScriptPath);
void remove(const QString& scriptName);

typedef typename QList<IUserScript*>::const_iterator const_iterator;
Expand Down
14 changes: 11 additions & 3 deletions lib/MellowPlayer/Infrastructure/UserScripts/UserScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ QString UserScriptDirectory::path() const {

bool UserScript::import(const QString& sourcePath)
{
QString sourceUrl = sourcePath;
sourceUrl = sourceUrl.replace("file://", "");
UserScriptDirectory directory;
if (directory.create() && QFile::exists(sourcePath))
bool exists = QFile::exists(sourceUrl);
if (directory.create() && exists)
{
QString destinationPath = directory.generateFileName();
path_ = destinationPath;
qDebug() << "importing" << sourcePath << " to " << destinationPath;
if (QFile::copy(sourcePath, destinationPath))
qDebug() << "importing" << sourceUrl << " to " << destinationPath;
if (QFile::copy(sourceUrl, destinationPath))
return load(destinationPath);
return true;
}
Expand All @@ -51,3 +54,8 @@ bool UserScript::load(const QString& path)
}
return false;
}

void UserScript::removeFile() const
{
QFile::remove(path_);
}
2 changes: 1 addition & 1 deletion lib/MellowPlayer/Infrastructure/UserScripts/UserScript.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace MellowPlayer::Infrastructure
{

class UserScriptDirectory
{
public:
Expand All @@ -18,5 +17,6 @@ namespace MellowPlayer::Infrastructure
public:
bool import(const QString& sourcePath) override;
bool load(const QString& path) override;
void removeFile() const override;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,8 @@ QString StreamingServiceViewModel::isEnabledSettingsKey() const
{
return streamingService_.name() + "/isEnabled";
}

QObject* StreamingServiceViewModel::userScripts()
{
return &userScriptsViewModel_;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace MellowPlayer::Presentation
Q_PROPERTY(bool isEnabled READ isEnabled WRITE setEnabled NOTIFY isEnabledChanged)
Q_PROPERTY(int sortOrder READ sortOrder WRITE setSortOrder NOTIFY sortOrderChanged)
Q_PROPERTY(bool isRunning READ isRunning NOTIFY isRunningChanged)
Q_PROPERTY(QObject* userScripts READ userScripts CONSTANT)
public:
StreamingServiceViewModel(Application::StreamingService& streamingService,
Application::ISettingsProvider& settings,
Expand All @@ -55,6 +56,8 @@ namespace MellowPlayer::Presentation
bool isEnabled() const;
void setEnabled(bool enabled);

QObject* userScripts();

public slots:
void setUrl(const QString& newUrl);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ UserScriptsViewModel::UserScriptsViewModel(const QString& serviceName,
userScripts_(serviceName, userScriptFactory, settingsProvider)
{
for(auto* userScriptModel: userScripts_) {
create(*userScriptModel);
create(userScriptModel);
}
}

Expand All @@ -31,26 +31,58 @@ bool UserScriptsViewModel::isValidName(const QString& name) const
return !names.contains(name);
}

void UserScriptsViewModel::add(const QString& name, const QString& sourcePath)
bool UserScriptsViewModel::add(const QString& name, const QString& sourcePath)
{
IUserScript& userScriptModel = userScripts_.add(name, sourcePath);
bool hadUserScripts = hasScripts();
IUserScript* userScriptModel = userScripts_.add(name, sourcePath);
if (userScriptModel != nullptr) {
create(userScriptModel);
if (hadUserScripts != hasScripts())
emit hasScriptsChanged();

create(userScriptModel);
return true;
}

return false;
}

void UserScriptsViewModel::remove(const QString& name)
{
bool hadUserScripts = hasScripts();
for (int i = 0; i < model_.count(); ++i) {
if (model_.at(i)->name() == name) {
model_.remove(i);
}
}
userScripts_.remove(name);

if (hadUserScripts != hasScripts())
emit hasScriptsChanged();
}


void UserScriptsViewModel::create(IUserScript& userScriptModel)
void UserScriptsViewModel::create(IUserScript* userScriptModel)
{
auto* userScriptViewModel = new UserScriptViewModel(userScriptModel, this);
auto* userScriptViewModel = new UserScriptViewModel(*userScriptModel, this);
model_.append(userScriptViewModel);
}

bool UserScriptsViewModel::hasScripts() const
{
return userScripts_.count() != 0;
}

QString UserScriptsViewModel::generateUniqueName(const QString& path) const
{
QFileInfo fileInfo(path);
QString baseName = fileInfo.baseName();
QString name = baseName;

int i = 1;
while(!isValidName(name)) {
++i;
name = baseName + QString::number(i);
}

return name;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace MellowPlayer::Presentation
class UserScriptsViewModel : public QObject
{
Q_OBJECT
Q_PROPERTY(bool hasScripts READ hasScripts NOTIFY hasScriptsChanged)
Q_PROPERTY(QAbstractListModel* model READ model CONSTANT)
public:
UserScriptsViewModel(const QString& serviceName,
Expand All @@ -17,13 +18,18 @@ namespace MellowPlayer::Presentation
QObject* parent= nullptr);

QAbstractListModel* model();
bool hasScripts() const;

Q_INVOKABLE QString generateUniqueName(const QString& path) const;
Q_INVOKABLE bool isValidName(const QString& name) const;
Q_INVOKABLE void add(const QString& name, const QString& sourcePath);
Q_INVOKABLE bool add(const QString& name, const QString& sourcePath);
Q_INVOKABLE void remove(const QString& name);

signals:
void hasScriptsChanged();

private:
void create(Application::IUserScript& userScriptModel);
void create(Application::IUserScript* userScriptModel);

Application::UserScripts userScripts_;
UserScriptListModel model_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ ItemDelegate {
hoverEnabled: true
onClicked: colorDialog.open()

contentItem: RowLayout {
RowLayout {
anchors.fill: parent
anchors.leftMargin: parent.leftPadding
anchors.rightMargin: parent.rightPadding
anchors.topMargin: parent.topPadding
anchors.bottomMargin: parent.bottomPadding

Label {
text: Translator.translateName(model.name)
font.pixelSize: 16
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ ItemDelegate {
enabled: model.enabled
onClicked: comboBox.popup.open()

contentItem: RowLayout {
RowLayout {
anchors.fill: parent
anchors.leftMargin: parent.leftPadding
anchors.rightMargin: parent.rightPadding
anchors.topMargin: parent.topPadding
anchors.bottomMargin: parent.bottomPadding

Label {
text: Translator.translateName(model.name)
font.pixelSize: 16
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ Pane {
anchors.fill: parent
hoverEnabled: true

contentItem: ColumnLayout {
ColumnLayout {
anchors.fill: parent
anchors.leftMargin: parent.leftPadding
anchors.rightMargin: parent.rightPadding
anchors.topMargin: parent.topPadding
anchors.bottomMargin: parent.bottomPadding

RowLayout {
Layout.fillWidth: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import QtQuick.Controls.Material 2.2

import MellowPlayer 3.0
import ".."
import "../Dialogs"

ItemDelegate {
id: root
Expand All @@ -14,14 +15,23 @@ ItemDelegate {
textField.forceActiveFocus()
}

hoverEnabled: true
padding: 0
hoverEnabled: true
onClicked: switchEnabled.toggle()

Material.elevation: 2

contentItem: ColumnLayout {
width: layout.implicitWidth
height: layout.implicitHeight

ColumnLayout {
id: layout

anchors.fill: parent
anchors.leftMargin: parent.leftPadding
anchors.rightMargin: parent.rightPadding
anchors.topMargin: parent.topPadding
anchors.bottomMargin: parent.bottomPadding

RowLayout {
Layout.fillWidth: true
Expand Down Expand Up @@ -114,6 +124,12 @@ ItemDelegate {
flat: true
highlighted: true
text: qsTr("User scripts")

onClicked: {
userScriptsDialog.serviceName = model.name;
userScriptsDialog.viewModel = model.userScripts;
userScriptsDialog.open();
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ ItemDelegate {

onClicked: keySequenceEdit.forceActiveFocus()

contentItem: RowLayout {
RowLayout {
anchors.fill: parent
anchors.leftMargin: parent.leftPadding
anchors.rightMargin: parent.rightPadding
anchors.topMargin: parent.topPadding
anchors.bottomMargin: parent.bottomPadding

Label {
text: Translator.translateName(model.name)
font.pixelSize: 16
Expand Down
Loading

0 comments on commit 731fc8e

Please sign in to comment.