| # kio Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | 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
    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   // TODO: catch errors here
    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   // Get all services of this service type.
    00129   lst = KServiceTypeProfile::offers( _servicetype, _genericServiceType );
    00130   if ( lst.count() == 0 )
    00131     return ret;
    00132
    00133   if ( !!constr )
    00134   {
    00135     // Find all services matching the constraint
    00136     // and remove the other ones
    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 { /*BASE::virtual_hook( id, data );*/ }
    00175
    00176 #include "ktrader.moc"

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:14:29 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001