/* * Cantata * * Copyright (c) 2011-2021 Craig Drummond * * ---- * * This program 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 2 of the License, or * (at your option) any later version. * * This program 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 this program; see the file COPYING. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include "customactions.h" #include "mainwindow.h" #include "support/globalstatic.h" #include "support/configuration.h" #include "support/utils.h" #include #include #include GLOBAL_STATIC(CustomActions, instance) #include static bool debugIsEnabled=false; #define DBUG if (debugIsEnabled) qWarning() << "CustomActions" << __FUNCTION__ void CustomActions::enableDebug() { debugIsEnabled=true; } bool CustomActions::Command::operator<(const Command &o) const { int c=Utils::compare(name, o.name); if (c<0) { return true; } if (c==0) { return Utils::compare(cmd, o.cmd)<0; } return false; } CustomActions::CustomActions() : Action(tr("Custom Actions"), nullptr) , mainWindow(nullptr) { QMenu *m=new QMenu(nullptr); setMenu(m); Configuration cfg(metaObject()->className()); int count=cfg.get("count", 0); for (int i=0; iaddAction(cmd.act); commands.append(cmd); connect(cmd.act, SIGNAL(triggered()), this, SLOT(doAction())); } } setVisible(!commands.isEmpty()); } void CustomActions::set(QList cmds) { std::sort(cmds.begin(), cmds.end()); bool diff=cmds.length()!=commands.length(); if (!diff) { for (int i=0; iremoveAction(cmd.act); disconnect(cmd.act, SIGNAL(triggered()), this, SLOT(doAction())); cmd.act->deleteLater(); } commands.clear(); for (const Command &cmd: cmds) { Command c(cmd); c.act=new Action(c.name, this); m->addAction(c.act); commands.append(c); connect(c.act, SIGNAL(triggered()), this, SLOT(doAction())); } Configuration cfg; cfg.removeGroup(metaObject()->className()); if (!commands.isEmpty()) { cfg.beginGroup(metaObject()->className()); cfg.set("count", commands.count()); for (int i=0; i(sender()); if (!act) { DBUG << "No action"; return; } QString mpdDir; for (const Command &cmd: commands) { if (cmd.act==act) { QList songs=mainWindow->selectedSongs(); if (songs.isEmpty()) { DBUG << "No selected songs?"; return; } if (!songs.at(0).isLocalFile()) { if (!MPDConnection::self()->getDetails().dirReadable) { DBUG << "MPD dir is not readable"; return; } mpdDir=MPDConnection::self()->getDetails().dir; } QStringList items; if (cmd.cmd.contains("%d")) { QSet used; for (const Song &s: songs) { if (Song::Playlist!=s.type) { QString dir=Utils::getDir(s.file); if (!used.contains(dir)) { used.insert(dir); items.append(mpdDir+dir); } } } } else { for (const Song &s: songs) { if (Song::Playlist!=s.type) { items.append(mpdDir+s.file); } } } if (!items.isEmpty()) { QStringList parts=cmd.cmd.split(' '); bool added=false; QString cmd=parts.takeFirst(); QStringList args; for (const QString &part: parts) { if (part.startsWith('%')) { args+=items; added=true; } else { args+=part; } } if (!added) { args+=items; } DBUG << "Start" << cmd << args; QProcess::startDetached(cmd, args); } return; } } DBUG << "Command not found"; } #include "moc_customactions.cpp"