kio Library: ktrader.cpp Source File (kio Library)

| # kio Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# ktrader.cpp

    00001 /* This file is part of the KDE libraries
    00002    Copyright (C) 2000 Torben Weis <weis@kde.org>
    00003
    00004    This library is free software; you can redistribute it and/or
    00005    modify it under the terms of the GNU Library General Public
    00006    License version 2 as published by the Free Software Foundation.
    00007
    00008    This library is distributed in the hope that it will be useful,
    00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
    00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00011    Library General Public License for more details.
    00012
    00013    You should have received a copy of the GNU Library General Public License
    00014    along with this library; see the file COPYING.LIB.  If not, write to
    00015    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00016    Boston, MA 02111-1307, USA.
    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   // TODO: catch errors here
    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   // Get all services of this service type.
    00130   lst = KServiceTypeProfile::offers( _servicetype, _genericServiceType );
    00131   if ( lst.count() == 0 )
    00132     return ret;
    00133
    00134   if ( !!constr )
    00135   {
    00136     // Find all services matching the constraint
    00137     // and remove the other ones
    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 { /*BASE::virtual_hook( id, data );*/ }
    00185
    00186 #include "ktrader.moc"

This file is part of the documentation for kio Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:24:04 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003