| 1 | /* |
| 2 | Copyright (C) 2005-2014 Mario Stephan <[email protected]> |
| 3 | |
| 4 | This library is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU Lesser General Public License as published |
| 6 | by the Free Software Foundation; either version 2.1 of the License, or |
| 7 | (at your option) any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public License |
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | #include "dj.h" |
| 19 | #include "filter.h" |
| 20 | #include <qdebug.h> |
| 21 | #include <qmutex.h> |
| 22 | #include <qwaitcondition.h> |
| 23 | |
| 24 | struct DjPrivate { |
| 25 | int rotation; |
| 26 | QList<Filter*> filters; |
| 27 | Filter* filter; |
| 28 | long countTracks; |
| 29 | long lengthTracks; |
| 30 | QString description; |
| 31 | }; |
| 32 | |
| 33 | Dj::Dj() |
| 34 | : p(new DjPrivate) |
| 35 | { |
| 36 | p->rotation = 0; |
| 37 | p->filter = nullptr; |
| 38 | p->description = QString::null; |
| 39 | p->countTracks = 0; |
| 40 | p->lengthTracks = 0; |
| 41 | } |
| 42 | |
| 43 | Dj::~Dj() |
| 44 | { |
| 45 | delete p; |
| 46 | } |
| 47 | |
| 48 | void Dj::addFilter(Filter* filter) |
| 49 | { |
| 50 | //qDebug() << Q_FUNC_INFO; |
| 51 | p->filters.append(filter); |
| 52 | connect(filter, SIGNAL(activated()), |
| 53 | this, SLOT(on_filter_activated())); |
| 54 | connect(filter, SIGNAL(filterChanged()), |
| 55 | this, SLOT(on_filter_filterChanged())); |
| 56 | connect(filter, SIGNAL(maxUsageChanged()), |
| 57 | this, SLOT(on_filter_maxUsageChanged())); |
| 58 | } |
| 59 | |
| 60 | void Dj::removeFilter(Filter* filter) |
| 61 | { |
| 62 | p->filters.removeOne(filter); |
| 63 | } |
| 64 | |
| 65 | QList<Filter*> Dj::filters() |
| 66 | { |
| 67 | return p->filters; |
| 68 | } |
| 69 | |
| 70 | void Dj::setActiveFilterIdx(int idx) |
| 71 | { |
| 72 | p->rotation = idx; |
| 73 | if (p->rotation >= p->filters.count()) |
| 74 | p->rotation = 0; |
| 75 | p->filters[p->rotation]->setActive(true); |
| 76 | } |
| 77 | |
| 78 | int Dj::activeFilterIdx() |
| 79 | { |
| 80 | return p->rotation; |
| 81 | } |
| 82 | |
| 83 | void Dj::on_filter_filterChanged() |
| 84 | { |
| 85 | qDebug() << Q_FUNC_INFO; |
| 86 | Filter* f = qobject_cast<Filter*>(QObject::sender()); |
| 87 | Q_EMIT filterChanged(f); |
| 88 | } |
| 89 | |
| 90 | void Dj::on_filter_countChanged() |
| 91 | { |
| 92 | //qDebug() << Q_FUNC_INFO ; |
| 93 | //Filter* f = qobject_cast<Filter*>(QObject::sender()); |
| 94 | //Q_EMIT countChanged(); |
| 95 | } |
| 96 | |
| 97 | void Dj::on_filter_activated() |
| 98 | { |
| 99 | Filter* f = qobject_cast<Filter*>(QObject::sender()); |
| 100 | QList<Filter*>::iterator i; |
| 101 | for (i = p->filters.begin(); i != p->filters.end(); ++i) { |
| 102 | if ((*i) != f) |
| 103 | (*i)->setActive(false); |
| 104 | else { |
| 105 | p->rotation = p->filters.indexOf((*i)); |
| 106 | } |
| 107 | } |
| 108 | //p->wc.wakeAll(); |
| 109 | } |
| 110 | |
| 111 | void Dj::on_filter_maxUsageChanged() |
| 112 | { |
| 113 | Filter* f = qobject_cast<Filter*>(QObject::sender()); |
| 114 | if (p->filter == f) |
| 115 | checkSequence(); |
| 116 | } |
| 117 | |
| 118 | void Dj::checkSequence() |
| 119 | { |
| 120 | |
| 121 | qDebug() << Q_FUNC_INFO << "rotation=" << p->rotation << "/" << p->filters.count() |
| 122 | << " repeat=" << p->filter->usage() << "/" << p->filter->maxUsage(); |
| 123 | |
| 124 | if (p->filter->usage() >= p->filter->maxUsage()) { |
| 125 | int i = p->filters.count(); |
| 126 | do { |
| 127 | p->rotation++; |
| 128 | i--; |
| 129 | if (p->rotation >= p->filters.count()) |
| 130 | p->rotation = 0; |
| 131 | } while (p->filters.at(p->rotation)->maxUsage() == 0 |
| 132 | && i > 0); |
| 133 | |
| 134 | p->filters.at(p->rotation)->setActive(true); |
| 135 | |
| 136 | // setActive goes twice through SIGNAL/SLOT and back here, |
| 137 | // therefore a waitcondition is necessary to avoid race condition |
| 138 | /*p->mutex.lock(); |
| 139 | p->wc.wait(&p->mutex); |
| 140 | p->mutex.unlock();*/ |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | //ToDo: - Switch LED before add the Track |
| 145 | // - Allow 0 filter (skip) |
| 146 | // - DJwidget: Playbutton für start |
| 147 | Filter* Dj::requestFilter() |
| 148 | { |
| 149 | if (p->filters.isEmpty()) |
| 150 | return nullptr; |
| 151 | |
| 152 | p->filter = p->filters.at(p->rotation); |
| 153 | |
| 154 | p->filter->setUsage(p->filter->usage() + 1); |
| 155 | |
| 156 | checkSequence(); |
| 157 | |
| 158 | qDebug() << Q_FUNC_INFO << "rotation=" << p->rotation << "/" << p->filters.count() |
| 159 | << " repeat=" << p->filter->usage() << "/" << p->filter->maxUsage(); |
| 160 | |
| 161 | qDebug() << Q_FUNC_INFO << "return filter=" << p->filters.at(p->rotation)->path() |
| 162 | << "/" << p->filters.at(p->rotation)->genre() |
| 163 | << "/" << p->filters.at(p->rotation)->artist(); |
| 164 | |
| 165 | return p->filter; |
| 166 | } |
| 167 | |
| 168 | QString Dj::description() |
| 169 | { |
| 170 | return p->description; |
| 171 | } |
| 172 | |
| 173 | void Dj::setDescription(QString value) |
| 174 | { |
| 175 | p->description = value; |
| 176 | } |
| 177 | |
| 178 | long Dj::countTracks() |
| 179 | { |
| 180 | return p->countTracks; |
| 181 | } |
| 182 | |
| 183 | void Dj::setCountTracks(int value) |
| 184 | { |
| 185 | p->countTracks = value; |
| 186 | Q_EMIT countChanged(); |
| 187 | } |
| 188 | |
| 189 | long Dj::lengthTracks() |
| 190 | { |
| 191 | return p->lengthTracks; |
| 192 | } |
| 193 | |
| 194 | void Dj::setLengthTracks(int value) |
| 195 | { |
| 196 | p->lengthTracks = value; |
| 197 | } |