00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
#ifndef __parse_tree_h__
00021
#define __parse_tree_h__
00022
00023
#include <qstring.h>
00024
#include <qstringlist.h>
00025
#include <qvaluelist.h>
00026
#include <qmap.h>
00027
#include <qshared.h>
00028
00029
#include <kservice.h>
00030
#include <kuserprofile.h>
00031
00032
#include "ktrader.h"
00033
00034
namespace KIO {
00035
00036
class ParseTreeBase;
00037
00039
struct PreferencesReturn
00040 {
00041
enum Type { PRT_DOUBLE, PRT_ERROR };
00042
00043 PreferencesReturn() { type = PRT_ERROR; }
00044
00045 PreferencesReturn(
const PreferencesReturn& _r )
00046 {
00047 type = _r.type;
00048 f = _r.f;
00049 }
00050
00051 Type type;
00052
double f;
00053 };
00054
00055
00062
int matchConstraint(
const ParseTreeBase *_tree,
const KService::Ptr &,
00063
const KServiceTypeProfile::OfferList& );
00064
00069 PreferencesReturn matchPreferences(
const ParseTreeBase *_tree,
const KService::Ptr &,
00070
const KServiceTypeProfile::OfferList& );
00071
00075
struct PreferencesMaxima
00076 {
00077
enum Type { PM_ERROR, PM_INVALID_INT, PM_INVALID_DOUBLE, PM_DOUBLE, PM_INT };
00078
00079 Type type;
00080
int iMax;
00081
int iMin;
00082
double fMax;
00083
double fMin;
00084 };
00085
00089
class ParseContext
00090 {
00091
public:
00095 ParseContext(
const ParseContext* _ctx ) : service( _ctx->service ), maxima( _ctx->maxima ),
00096 offers( _ctx->offers ) {}
00097 ParseContext(
const KService::Ptr & _service,
const KServiceTypeProfile::OfferList& _offers,
00098
QMap<QString,PreferencesMaxima>& _m )
00099 : service( _service ), maxima( _m ), offers( _offers ) {}
00100
00101
bool initMaxima(
const QString& _prop);
00102
00103
enum Type { T_STRING = 1, T_DOUBLE = 2, T_NUM = 3, T_BOOL = 4,
00104 T_STR_SEQ = 5, T_SEQ = 6 };
00105
00106
QString str;
00107
int i;
00108
double f;
00109
bool b;
00110
QValueList<QVariant> seq;
00111
QStringList strSeq;
00112 Type type;
00113
00114 KService::Ptr service;
00115
00116
QMap<QString,PreferencesMaxima>& maxima;
00117
const KServiceTypeProfile::OfferList& offers;
00118 };
00119
00123
class ParseTreeBase :
public KShared
00124 {
00125
public:
00126
typedef KSharedPtr<ParseTreeBase> Ptr;
00127 ParseTreeBase() { }
00128
00129
virtual bool eval( ParseContext *_context )
const = 0;
00130
protected:
00131
virtual ~ParseTreeBase() { };
00132 };
00133
00134 ParseTreeBase::Ptr parseConstraints(
const QString& _constr );
00135 ParseTreeBase::Ptr parsePreferences(
const QString& _prefs );
00136
00140
class ParseTreeOR :
public ParseTreeBase
00141 {
00142
public:
00143 ParseTreeOR( ParseTreeBase *_ptr1, ParseTreeBase *_ptr2 ) { m_pLeft = _ptr1; m_pRight = _ptr2; }
00144
00145
bool eval( ParseContext *_context )
const;
00146
00147
protected:
00148 ParseTreeBase::Ptr m_pLeft;
00149 ParseTreeBase::Ptr m_pRight;
00150 };
00151
00155
class ParseTreeAND :
public ParseTreeBase
00156 {
00157
public:
00158 ParseTreeAND( ParseTreeBase *_ptr1, ParseTreeBase *_ptr2 ) { m_pLeft = _ptr1; m_pRight = _ptr2; }
00159
00160
bool eval( ParseContext *_context )
const;
00161
00162
protected:
00163 ParseTreeBase::Ptr m_pLeft;
00164 ParseTreeBase::Ptr m_pRight;
00165 };
00166
00170
class ParseTreeCMP :
public ParseTreeBase
00171 {
00172
public:
00173 ParseTreeCMP( ParseTreeBase *_ptr1, ParseTreeBase *_ptr2,
int _i ) { m_pLeft = _ptr1; m_pRight = _ptr2; m_cmd = _i; }
00174
00175
bool eval( ParseContext *_context )
const;
00176
00177
protected:
00178 ParseTreeBase::Ptr m_pLeft;
00179 ParseTreeBase::Ptr m_pRight;
00180
int m_cmd;
00181 };
00182
00186
class ParseTreeIN :
public ParseTreeBase
00187 {
00188
public:
00189 ParseTreeIN( ParseTreeBase *_ptr1, ParseTreeBase *_ptr2 ) { m_pLeft = _ptr1; m_pRight = _ptr2; }
00190
00191
bool eval( ParseContext *_context )
const;
00192
00193
protected:
00194 ParseTreeBase::Ptr m_pLeft;
00195 ParseTreeBase::Ptr m_pRight;
00196 };
00197
00201
class ParseTreeMATCH :
public ParseTreeBase
00202 {
00203
public:
00204 ParseTreeMATCH( ParseTreeBase *_ptr1, ParseTreeBase *_ptr2 ) { m_pLeft = _ptr1; m_pRight = _ptr2; }
00205
00206
bool eval( ParseContext *_context )
const;
00207
00208
protected:
00209 ParseTreeBase::Ptr m_pLeft;
00210 ParseTreeBase::Ptr m_pRight;
00211 };
00212
00216
class ParseTreeCALC :
public ParseTreeBase
00217 {
00218
public:
00219 ParseTreeCALC( ParseTreeBase *_ptr1, ParseTreeBase *_ptr2,
int _i ) { m_pLeft = _ptr1; m_pRight = _ptr2; m_cmd = _i; }
00220
00221
bool eval( ParseContext *_context )
const;
00222
00223
protected:
00224 ParseTreeBase::Ptr m_pLeft;
00225 ParseTreeBase::Ptr m_pRight;
00226
int m_cmd;
00227 };
00228
00232
class ParseTreeBRACKETS :
public ParseTreeBase
00233 {
00234
public:
00235 ParseTreeBRACKETS( ParseTreeBase *_ptr ) { m_pLeft = _ptr; }
00236
00237
bool eval( ParseContext *_context )
const {
return m_pLeft->eval( _context ); }
00238
00239
protected:
00240 ParseTreeBase::Ptr m_pLeft;
00241 };
00242
00246
class ParseTreeNOT :
public ParseTreeBase
00247 {
00248
public:
00249 ParseTreeNOT( ParseTreeBase *_ptr ) { m_pLeft = _ptr; }
00250
00251
bool eval( ParseContext *_context )
const;
00252
00253
protected:
00254 ParseTreeBase::Ptr m_pLeft;
00255 };
00256
00260
class ParseTreeEXIST :
public ParseTreeBase
00261 {
00262
public:
00263 ParseTreeEXIST(
const char *_id ) { m_id = _id; }
00264
00265
bool eval( ParseContext *_context )
const;
00266
00267
protected:
00268
QString m_id;
00269 };
00270
00274
class ParseTreeID :
public ParseTreeBase
00275 {
00276
public:
00277 ParseTreeID(
const char *arg ) { m_str = arg; }
00278
00279
bool eval( ParseContext *_context )
const;
00280
00281
protected:
00282
QString m_str;
00283 };
00284
00288
class ParseTreeSTRING :
public ParseTreeBase
00289 {
00290
public:
00291 ParseTreeSTRING(
const char *arg ) { m_str = arg; }
00292
00293
bool eval( ParseContext *_context )
const { _context->type = ParseContext::T_STRING; _context->str = m_str;
return true; }
00294
00295
protected:
00296
QString m_str;
00297 };
00298
00302
class ParseTreeNUM :
public ParseTreeBase
00303 {
00304
public:
00305 ParseTreeNUM(
int arg ) { m_int = arg; }
00306
00307
bool eval( ParseContext *_context )
const { _context->type = ParseContext::T_NUM; _context->i = m_int;
return true; }
00308
00309
protected:
00310
int m_int;
00311 };
00312
00316
class ParseTreeDOUBLE :
public ParseTreeBase
00317 {
00318
public:
00319 ParseTreeDOUBLE(
double arg ) { m_double = arg; }
00320
00321
bool eval( ParseContext *_context )
const { _context->type = ParseContext::T_DOUBLE; _context->f = m_double;
return true; }
00322
00323
protected:
00324
double m_double;
00325 };
00326
00330
class ParseTreeBOOL :
public ParseTreeBase
00331 {
00332
public:
00333 ParseTreeBOOL(
bool arg ) { m_bool = arg; }
00334
00335
bool eval( ParseContext *_context )
const { _context->type = ParseContext::T_BOOL; _context->b = m_bool;
return true; }
00336
00337
protected:
00338
bool m_bool;
00339 };
00340
00344
class ParseTreeMAX2 :
public ParseTreeBase
00345 {
00346
public:
00347 ParseTreeMAX2(
const char *_id ) { m_strId = _id; }
00348
00349
bool eval( ParseContext *_context )
const;
00350
00351
protected:
00352
QString m_strId;
00353 };
00354
00358
class ParseTreeMIN2 :
public ParseTreeBase
00359 {
00360
public:
00361 ParseTreeMIN2(
const char *_id ) { m_strId = _id; }
00362
00363
bool eval( ParseContext *_context )
const;
00364
00365
protected:
00366
QString m_strId;
00367 };
00368
00369 }
00370
00371
#endif