ktrader.cpp
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
00029 template class KStaticDeleter<KTrader>;
00030
00031 using namespace KIO;
00032
00033 class KTraderSorter
00034 {
00035 public:
00036 KTraderSorter() { m_pService = 0; };
00037 KTraderSorter( const KTraderSorter& s ) : m_userPreference( s.m_userPreference ),
00038 m_bAllowAsDefault( s.m_bAllowAsDefault ),
00039 m_traderPreference( s.m_traderPreference ), m_pService( s.m_pService ) { }
00040 KTraderSorter( const KService::Ptr &_service, double _pref1, int _pref2, bool _default )
00041 { m_pService = _service;
00042 m_userPreference = _pref2;
00043 m_traderPreference = _pref1;
00044 m_bAllowAsDefault = _default;
00045 }
00046
00047 KService::Ptr service() const { return m_pService; }
00048
00049 bool operator< ( const KTraderSorter& ) const;
00050
00051 private:
00056 int m_userPreference;
00060 bool m_bAllowAsDefault;
00061
00066 double m_traderPreference;
00067
00068 KService::Ptr m_pService;
00069 };
00070
00071 bool KTraderSorter::operator< ( const KTraderSorter& _o ) const
00072 {
00073 if ( _o.m_bAllowAsDefault && !m_bAllowAsDefault )
00074 return true;
00075 if ( _o.m_userPreference > m_userPreference )
00076 return true;
00077 if ( _o.m_userPreference < m_userPreference )
00078 return false;
00079 if ( _o.m_traderPreference > m_traderPreference )
00080 return true;
00081 return false;
00082 }
00083
00084
00085
00086 KTrader* KTrader::s_self = 0;
00087 KStaticDeleter<KTrader> ktradersd;
00088
00089 KTrader* KTrader::self()
00090 {
00091 if ( !s_self )
00092 ktradersd.setObject( s_self, new KTrader );
00093
00094 return s_self;
00095 }
00096
00097 KTrader::KTrader()
00098 {
00099 }
00100
00101 KTrader::~KTrader()
00102 {
00103 }
00104
00105 KTrader::OfferList KTrader::query( const QString& _servicetype, const QString& _constraint,
00106 const QString& _preferences ) const
00107 {
00108 return query( _servicetype, QString::null, _constraint, _preferences );
00109 }
00110
00111 KTrader::OfferList KTrader::query( const QString& _servicetype, const QString& _genericServiceType,
00112 const QString& _constraint,
00113 const QString& _preferences ) const
00114 {
00115
00116 ParseTreeBase::Ptr constr;
00117 ParseTreeBase::Ptr prefs;
00118
00119 if ( !_constraint.isEmpty() )
00120 constr = KIO::parseConstraints( _constraint );
00121
00122 if ( !_preferences.isEmpty() )
00123 prefs = KIO::parsePreferences( _preferences );
00124
00125 KServiceTypeProfile::OfferList lst;
00126 KTrader::OfferList ret;
00127
00128
00129 lst = KServiceTypeProfile::offers( _servicetype, _genericServiceType );
00130 if ( lst.count() == 0 )
00131 return ret;
00132
00133 if ( !!constr )
00134 {
00135
00136
00137 KServiceTypeProfile::OfferList::Iterator it = lst.begin();
00138 while( it != lst.end() )
00139 {
00140 if ( matchConstraint( constr, (*it).service(), lst ) != 1 )
00141 it = lst.remove( it );
00142 else
00143 ++it;
00144 }
00145 }
00146
00147 if ( !!prefs )
00148 {
00149 QValueList<KTraderSorter> sorter;
00150 KServiceTypeProfile::OfferList::Iterator it = lst.begin();
00151 for( ; it != lst.end(); ++it )
00152 {
00153 PreferencesReturn p = matchPreferences( prefs, (*it).service(), lst );
00154 if ( p.type == PreferencesReturn::PRT_DOUBLE )
00155 sorter.append( KTraderSorter( (*it).service(), p.f, (*it).preference(), (*it).allowAsDefault() ) );
00156 }
00157 qBubbleSort( sorter );
00158
00159 QValueList<KTraderSorter>::Iterator it2 = sorter.begin();
00160 for( ; it2 != sorter.end(); ++it2 )
00161 ret.prepend( (*it2).service() );
00162 }
00163 else
00164 {
00165 KServiceTypeProfile::OfferList::Iterator it = lst.begin();
00166 for( ; it != lst.end(); ++it )
00167 ret.append( (*it).service() );
00168 }
00169
00170 return ret;
00171 }
00172
00173 void KTrader::virtual_hook( int, void* )
00174 { }
00175
00176 #include "ktrader.moc"
This file is part of the documentation for kdelibs Version 3.1.0.