summaryrefslogtreecommitdiffstats
path: root/noatun-plugins/oblique/query.h
blob: 60a19ff8184e466fe6c65642f6e716922d6b9f1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright (c) 2003 Charles Samuels <charles@kde.org>
// See the file COPYING for redistribution terms.

#ifndef QUERY_H
#define QUERY_H

#include "base.h"

#include <qregexp.h>
#include <qstring.h>

class Query;
class QDomElement;

class QueryGroup
{
	friend class Query;

	QueryGroup *mFirstChild;
	QueryGroup *mNextSibling;

	int mFuzzyness;
	int mOptions;

	QString mPropertyName;
	QString mPresentation;
	QRegExp mValue;

public:
	QueryGroup();
	QueryGroup(const QueryGroup &copy);
	QueryGroup &operator =(const QueryGroup &copy);


	/**
	 * delete my first child, and my next sibling
	 **/
	~QueryGroup();
	void setFirstChild(QueryGroup *g) { mFirstChild = g; }
	void setNextSibling(QueryGroup *g) { mNextSibling = g; }


	QueryGroup *firstChild() { return mFirstChild; }
	const QueryGroup *firstChild() const { return mFirstChild; }
	QueryGroup *lastChild();
	QueryGroup *nextSibling() { return mNextSibling; }
	const QueryGroup *nextSibling() const { return mNextSibling; }

	/**
	 * insert @p after as a sibling immediately after this
	 **/
	void insertAfter(QueryGroup *insert);

	/**
	 * insert @p immediately after this as a child
	 **/
	void insertUnder(QueryGroup *insert);

	/**
	 * Try get the "best fit" for the two parameters
	 **/
	void move(Query *query, QueryGroup *under, QueryGroup *after);

	QString propertyName() const { return mPropertyName; }
	QRegExp value() const { return mValue; }
	QString presentation() const { return mPresentation; }

	void setPropertyName(const QString &v) { mPropertyName = v; }
	void setPresentation(const QString &v) { mPresentation = v; }
	void setValue(const QRegExp &v) { mValue = v; }

	enum Fuzzyness
	{
		Case = 1<<0, Spaces = 1<<1, Articles = 1<<2, Symbols = 1<<3
	};

	bool fuzzyness(Fuzzyness f) const;

	enum Option
	{
		AutoHide = 1<<0, Disabled = 1<<1, Playable = 1<<2,
		ChildrenVisible = 1<<3, AutoOpen = 1<<4
	};

	bool option(Option option) const;
	void setOption(Option option, bool on);

	/**
	 * @return if I match @p file
	 **/
	bool matches(const File &file) const;

	QString presentation(const File &file) const;

private:
	/**
	 * apply all the "normalizing" transformations according
	 * to the fuzzyness
	 **/
	QString fuzzify(const QString &str) const;
	/**
	 * @returns the previous or parent of this item (slow)
	 **/
	QueryGroup *previous(Query *query);
	QueryGroup *previous(QueryGroup *startWith);

};




/**
 * a query is the tree structure that is shown to the user
 **/
class Query
{
	QueryGroup *mGroupFirst;
	QString mName;

public:
	Query();
	Query(const Query &copy);
	~Query();

	Query &operator =(const Query &copy);

	QueryGroup *firstChild();
	const QueryGroup *firstChild() const;

	void setFirstChild(QueryGroup *g);
	void insertFirst(QueryGroup *g);

	void clear();
	
	/**
	 * @returns the name to be used internally
	 **/
	QString name() const { return mName; }
	void setName(const QString &name) { mName = name; }

	/**
	 * @returns the name of the query
	 **/
	QString load(const QString &filename);
	void save(const QString &name, QDomElement &element);
	void save(const QString &name, const QString &filename);

	/**
	 * remove any trace of this from the tree, but don't actually delete it
	 **/
	void take(QueryGroup *item);

	void dump();
	
	/**
	 * @returns the name of this query as used internally by the db.
	 *
	 * Will give it a name in the db if necessary
	 **/
	QString dbname(Base *base);

private:
	void loadGroup(QDomElement element, QueryGroup *parent=0);
	void saveGroup(QDomElement &parent, QueryGroup *group);

	void deepCopy(const QueryGroup *from, QueryGroup *toParent);
	
	/**
	 * @returns the name of the query
	 **/
	QString load(QDomElement element);
};



#endif