From d4cb52ecd9b1d52c21686f4e019cc3b7b04bcb4e Mon Sep 17 00:00:00 2001 From: Thomas Frauendorfer Date: Mon, 20 Oct 2008 03:49:39 +0200 Subject: [PATCH] OTHER: Handle source preferences correctly This is done through some ugly hack, that will have to exist until xmms2d or the c++ bindings make things easier for us client developers ;-) --- lib/lib.pro | 1 + lib/sourcepref.h | 78 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/xclient.cpp | 35 +++++++++++++++++++++- lib/xclient.h | 8 ++++- 4 files changed, 120 insertions(+), 2 deletions(-) create mode 100644 lib/sourcepref.h diff --git a/lib/lib.pro b/lib/lib.pro index 9e2936c..b0830a6 100644 --- a/lib/lib.pro +++ b/lib/lib.pro @@ -27,6 +27,7 @@ HEADERS += xclient.h \ xcollection_p.h \ playlistmodel.h \ xmmsqt4.h \ + sourcepref.h \ debug.h QT += network diff --git a/lib/sourcepref.h b/lib/sourcepref.h new file mode 100644 index 0000000..aded617 --- /dev/null +++ b/lib/sourcepref.h @@ -0,0 +1,78 @@ +/** + * This file is a part of Promoe, an XMMS2 Client. + * + * Copyright (C) 2008 XMMS2 Team + * + * 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. + */ + +/* + * This file contains a hack to be able to honor source preferences when + * converting a Propdicts to a QHash. + * It is only included in xclient.cpp and only used internally in that class + * + * The limitations that make this file necessary will be fixed with Coll2. So + * this file can be removed as soon as Coll2 reaches xmms2-stabe + * + * + * Minor note: At the time of writing this file, Coll2 wasn't in -devel, so + * this might still take some time + */ + +#ifndef __SOURCEPREF__ +#define __SOURCEPREF__ + +#include + +#include +#include + +#include + +/* + * This class is used to get the source preference from a propdict as a + * QList. + */ +class MyPropDict : public Xmms::PropDict +{ + public: + MyPropDict (const Xmms::PropDict &d) : PropDict (d) {} + + QList getSourcePreference () { + const char **sourcepref = + xmmsc_result_source_preference_get (result_); + QList prio_list; + + for (; *sourcepref; ++sourcepref) { + prio_list.append (QRegExp (*sourcepref, + Qt::CaseSensitive, + QRegExp::Wildcard)); + } + + return prio_list; + } +}; + +int +getPriority (const QString source, const QList prio_list) { + for (int i=0; i < prio_list.size (); ++i) { + if (prio_list[i].exactMatch (source)) + return i; + } + + // If source doesn't match any expression in prio_list return an invalid + // value. (The biggest valid value is prio_list.length () -1 ) + // In that case, the caller should not use the value corresponding with + // this source + return prio_list.size (); +} + +#endif diff --git a/lib/xclient.cpp b/lib/xclient.cpp index 2b6f249..4e08384 100644 --- a/lib/xclient.cpp +++ b/lib/xclient.cpp @@ -31,6 +31,8 @@ #include "xmmsqt4.h" #include "debug.h" +#include "sourcepref.h" + QString XClient::stdToQ (const std::string &str) { @@ -186,8 +188,29 @@ void XClient::propDictToQHash (const std::string &key, const Xmms::Dict::Variant &value, const std::string &source, +#ifdef SOURCEPREF_HACK + const QList &prio_list, + QHash &curr_prio, +#endif QHash &hash) { +#ifdef SOURCEPREF_HACK + // braces because of tmp_prio definition + { + int tmp_prio = getPriority (QString::fromStdString (source), prio_list); + QString tmp_key = QString::fromStdString (key); + // Don't add a new value if the priority of it isn't better than the + // priority already present in hash. If there is no "*" source + // preference, this also get's rid of values we don't want at all + if (tmp_prio >= curr_prio.value (tmp_key, prio_list.size ())) + return; + + // Set the priority of the current source-key combination for the key, so + // that we do not overwrite our value with a worse source for this key. + // (higher priority values are worse) + curr_prio[tmp_key] = tmp_prio; + } +#endif if (value.type () == typeid (int32_t)) { hash.insert (QString::fromLatin1 (key.c_str ()), QVariant (boost::get< int32_t > (value))); @@ -220,8 +243,18 @@ QHash XClient::convert_propdict (const Xmms::PropDict &dict) { QHash hash; +#ifdef SOURCEPREF_HACK + MyPropDict d (dict); + QList priolist = d.getSourcePreference (); + QHash curr_prio; +#endif dict.each (boost::bind (&XClient::propDictToQHash, - _1, _2, _3, boost::ref (hash))); + _1, _2, _3, +#ifdef SOURCEPREF_HACK + boost::ref (priolist), + boost::ref (curr_prio), +#endif + boost::ref (hash))); return hash; } diff --git a/lib/xclient.h b/lib/xclient.h index 0720fd4..ff16727 100644 --- a/lib/xclient.h +++ b/lib/xclient.h @@ -23,6 +23,7 @@ #include #include #include +#include class QWidget; class XClientCache; @@ -30,6 +31,7 @@ class XConfig; class XPlayback; class XCollection; +#define SOURCEPREF_HACK class XClient : public QObject { Q_OBJECT @@ -41,7 +43,11 @@ class XClient : public QObject { static void propDictToQHash (const std::string &key, const Xmms::Dict::Variant &value, const std::string &source, - QHash &hash); +#ifdef SOURCEPREF_HACK + const QList &priolist, + QHash &curr_prio, +#endif + QHash &hash); static void dictToQHash (const std::string &key, const Xmms::Dict::Variant &value,