kio
ktrader.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "ktrader.h"
00020 #include "ktraderparsetree.h"
00021
00022 #include <qtl.h>
00023 #include <qbuffer.h>
00024
00025 #include <kuserprofile.h>
00026 #include <kstandarddirs.h>
00027 #include <kstaticdeleter.h>
00028 #include <kdebug.h>
00029
00030 template class KStaticDeleter<KTrader>;
00031
00032 using namespace KIO;
00033
00034 class KTraderSorter
00035 {
00036 public:
00037 KTraderSorter() { m_pService = 0; };
00038 KTraderSorter( const KTraderSorter& s ) : m_userPreference( s.m_userPreference ),
00039 m_bAllowAsDefault( s.m_bAllowAsDefault ),
00040 m_traderPreference( s.m_traderPreference ), m_pService( s.m_pService ) { }
00041 KTraderSorter( const KService::Ptr &_service, double _pref1, int _pref2, bool _default )
00042 { m_pService = _service;
00043 m_userPreference = _pref2;
00044 m_traderPreference = _pref1;
00045 m_bAllowAsDefault = _default;
00046 }
00047
00048 KService::Ptr service() const { return m_pService; }
00049
00050 bool operator< ( const KTraderSorter& ) const;
00051
00052 private:
00057 int m_userPreference;
00061 bool m_bAllowAsDefault;
00062
00067 double m_traderPreference;
00068
00069 KService::Ptr m_pService;
00070 };
00071
00072 bool KTraderSorter::operator< ( const KTraderSorter& _o ) const
00073 {
00074 if ( _o.m_bAllowAsDefault && !m_bAllowAsDefault )
00075 return true;
00076 if ( _o.m_userPreference > m_userPreference )
00077 return true;
00078 if ( _o.m_userPreference < m_userPreference )
00079 return false;
00080 if ( _o.m_traderPreference > m_traderPreference )
00081 return true;
00082 return false;
00083 }
00084
00085
00086
00087 KTrader* KTrader::s_self = 0;
00088 static KStaticDeleter<KTrader> ktradersd;
00089
00090 KTrader* KTrader::self()
00091 {
00092 if ( !s_self )
00093 ktradersd.setObject( s_self, new KTrader );
00094
00095 return s_self;
00096 }
00097
00098 KTrader::KTrader()
00099 {
00100 }
00101
00102 KTrader::~KTrader()
00103 {
00104 }
00105
00106 KTrader::OfferList KTrader::query( const QString& _servicetype, const QString& _constraint,
00107 const QString& _preferences ) const
00108 {
00109 return query( _servicetype, QString::null, _constraint, _preferences );
00110 }
00111
00112 KTrader::OfferList KTrader::query( const QString& _servicetype, const QString& _genericServiceType,
00113 const QString& _constraint,
00114 const QString& _preferences ) const
00115 {
00116
00117 ParseTreeBase::Ptr constr;
00118 ParseTreeBase::Ptr prefs;
00119
00120 if ( !_constraint.isEmpty() )
00121 constr = KIO::parseConstraints( _constraint );
00122
00123 if ( !_preferences.isEmpty() )
00124 prefs = KIO::parsePreferences( _preferences );
00125
00126 KServiceTypeProfile::OfferList lst;
00127 KTrader::OfferList ret;
00128
00129
00130 lst = KServiceTypeProfile::offers( _servicetype, _genericServiceType );
00131 if ( lst.count() == 0 )
00132 return ret;
00133
00134 if ( !!constr )
00135 {
00136
00137
00138 KServiceTypeProfile::OfferList::Iterator it = lst.begin();
00139 while( it != lst.end() )
00140 {
00141 if ( matchConstraint( constr, (*it).service(), lst ) != 1 )
00142 it = lst.remove( it );
00143 else
00144 ++it;
00145 }
00146 }
00147
00148 if ( !!prefs )
00149 {
00150 QValueList<KTraderSorter> sorter;
00151 KServiceTypeProfile::OfferList::Iterator it = lst.begin();
00152 for( ; it != lst.end(); ++it )
00153 {
00154 PreferencesReturn p = matchPreferences( prefs, (*it).service(), lst );
00155 if ( p.type == PreferencesReturn::PRT_DOUBLE )
00156 sorter.append( KTraderSorter( (*it).service(), p.f, (*it).preference(), (*it).allowAsDefault() ) );
00157 }
00158 qBubbleSort( sorter );
00159
00160 QValueList<KTraderSorter>::Iterator it2 = sorter.begin();
00161 for( ; it2 != sorter.end(); ++it2 )
00162 ret.prepend( (*it2).service() );
00163 }
00164 else
00165 {
00166 KServiceTypeProfile::OfferList::Iterator it = lst.begin();
00167 for( ; it != lst.end(); ++it )
00168 ret.append( (*it).service() );
00169 }
00170
00171 #ifndef NDEBUG
00172 QString query = _servicetype;
00173 if ( !_genericServiceType.isEmpty() ) {
00174 query += ", ";
00175 query += _genericServiceType;
00176 }
00177 kdDebug(7014) << "query for " << query
00178 << " : returning " << ret.count() << " offers" << endl;
00179 #endif
00180 return ret;
00181 }
00182
00183 void KTrader::virtual_hook( int, void* )
00184 { }
00185
00186 #include "ktrader.moc"