summaryrefslogtreecommitdiffstats
path: root/krusader/UserAction/expander.h
blob: 3232e1646801fca843ba338156d019b572aa963d (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
//
// C++ Interface: expander
//
// Description: 
//
//
// Author: Jonas B�r (C) 2004
//
// Copyright: See COPYING file that comes with this distribution
//
//

#ifndef EXPANDER_H
#define EXPANDER_H

// class TQString;
#include <tqstring.h>
#include <tqstringlist.h>
#include <tqvaluelist.h>
#include "tstring.h"
// #include <tqstringlist.h>
class ListPanel;
class Expander;
class Error;

typedef TagString_t<TQStringList> TagString;
typedef TQValueList<TagString> TagStringList;

/**
 * This holds informations about each parameter
 */
class exp_parameter {
public:
	exp_parameter() {}
   inline exp_parameter( TQString desc, TQString pre, bool ness)
      { _description = desc; _preset = pre; _nessesary = ness; }
   inline TQString description() const ///< A description of the parameter
      { return _description; }
   inline TQString preset() const ///< the default of the parameter
      { return _preset; }
   inline bool nessesary() const ///< false if the parameter is optional
      { return _nessesary; }

private:
   TQString _description;
   TQString _preset;
   bool _nessesary;
};

#define EXP_FUNC virtual TagString expFunc ( const ListPanel*, const TagStringList&, const bool&, Expander& ) const
#define SIMPLE_EXP_FUNC virtual TagString expFunc ( const ListPanel*, const TQStringList&, const bool&, Expander& ) const
/** 
  *  Abstract baseclass for all expander-functions (which replace placeholder).
  *  A Placeholder is an entry containing the expression, its expanding function and Parameter.
  *
 * Not to be created on the heap
  *
  * @author Jonas B�r (http://www.jonas-baehr.de)
  */
class exp_placeholder {
public:
   inline TQString expression() const  ///< The placeholder (without '%' or panel-prefix)
      { return _expression; }
   inline TQString description() const ///< A description of the placeholder
      { return _description; }
   inline bool needPanel() const ///< true if the placeholder needs a panel to operate on
      { return _needPanel; }
   inline void addParameter( exp_parameter parameter ) ///< adds parameter to the placeholder
      { _parameter.append(parameter); }
   inline int parameterCount() const ///< returns the number of placeholders
      { return _parameter.count(); }
   inline const exp_parameter& parameter( int id ) const ///< returns a specific parameter
      { return _parameter[ id ]; }

   EXP_FUNC = 0;
protected:
	static void setError(Expander& exp,const Error& e) ;
	static void panelMissingError(const TQString &s, Expander& exp);
	static TQStringList splitEach(const TagString& s);
	static TQStringList fileList(const ListPanel* const panel,const TQString& type,const TQString& mask,const bool ommitPath,const bool useUrl,Expander&,const TQString&);
	exp_placeholder();
	exp_placeholder(const exp_placeholder& p);
	~exp_placeholder() { }
   TQString _expression;
   TQString _description;
   TQValueList <exp_parameter> _parameter;
   bool _needPanel;
};


	class Error {
	public:
		enum Cause {
			C_USER, C_SYNTAX, C_WORLD, C_ARGUMENT
		};
		enum Severity {
			S_OK, S_WARNING, S_ERROR, S_FATAL
		};
		Error() : s_(S_OK) {}
		Error(Severity s,Cause c,TQString d) : s_(s), c_(c), desc_(d) {}
		Cause cause() const { return c_; }
		operator bool() const { return s_!=S_OK; }
		const TQString& what() const { return desc_; }
	private:
		Severity s_;
		Cause c_;
		TQString desc_;
	};


/**
 * The Expander expands the command of an UserAction by replacing all placeholders by thier current values.@n
 * Each placeholder begins with a '%'-sign, followed by one char indicating the panel, followed by a command which may have some paramenter enclosed in brackets and also ends with a '%'-sign.
 * Examples are %aPath% or %rBookmark("/home/jonas/src/krusader_kde3", "yes")%.@n
 * The panel-indicator has to be either 'a' for the active, 'o' for the other, 'r' for the right, 'l' for the left or '_' for panel-independence.
 * 
 * Currently sopported are these commands can be ordered in three groups (childs are the parameter in the right order):
 * - Placeholders for Krusaders panel-data (panel-indicator has to be 'a', 'o', 'r' or 'l')
 *    - @em Path is replaced by the panel's path
 *    - @em Count is replaced by a nomber of
 *       -# Either "All", "Files", "Dirs", "Selected"
 *       .
 *    - @em Filter is preplaced by the panels filter-mask (ex: "*.cpp *.h")
 *    - @em Current is replaced by the current item or, in case of onmultiple="call_each", by each selected item.
 *       -# If "yes", only the filename (without path) is returned
 *       .
 *    - @em List isreplaced by a list of
 *       -# Either "All", "Files", "Dirs", "Selected"
 *       -# A seperator between the items (default: " " [one space])
 *       -# If "yes", only the filename (without path) is returned
 *       -# (for all but "Selected") a filter-mask (default: "*")
 *       .
 *   .
 * - Access to panel-dependent, krusader-internal, parameter-needed functions (panel-indicator has to be 'a', 'o', 'r' or 'l')
 *    - @em Select manipulates the selection of the panel
 *       -# A filter-mask (nessesary)
 *       -# Either "Add", "Remove", "Set" (default)
  *       .
 *    - @em Bookmark manipulates the selection of the panel
 *       -# A path or URL (nessesary)
 *       -# If "yes", the location is opend in a new tab
 *       .
 *   .
 * - Access to panel-independent, krusader-internal, parameter-needed functions (panel-indicator doesn't matter but should be set to '_')
 *    - @em Ask displays a lineedit and is replaced by its return
 *       -# The question (nessesary)
 *       -# A default answer
 *       -# A cation for the popup
 *       .
 *    - @em Clipboard manipulates the system-wide clipboard
 *       -# The string copied to clip (ex: "%aCurrent%") (nessesary)
 *       -# A separator. If set, parameter1 is append with this to the current clipboard content
 *       .
 *    .
 * .
 * Since all placeholders are expanded in the order they appear in the command, little one-line-scripts are possible
 *
 * @author Jonas B�r (http://www.jonas-baehr.de), Shie Erlich
 */
class Expander {
public:
  
   inline static int placeholderCount() ///< returns the number of placeholders
      { return _placeholder().count(); }
   inline static const exp_placeholder* placeholder( int id )
      { return _placeholder()[ id ]; }

   /**
     * This expands a whole commandline
     * 
     * @param stringToExpand the commandline with the placeholder
     * @param useUrl true iff the path's should be expanded to an URL instead of an local path
     * @return a list of all commands
     */
   void expand( const TQString& stringToExpand, bool useUrl );
	 
	 /**
	  * Returns the list of all commands to be executed, provided that #expand was called
	  * before, and there was no error (see #error). Otherwise, calls #abort
	  * 
	  * @return The list of commands to be executed
     */
	 const TQStringList& result() const { assert(!error()); return resultList; }

	 /**
	  *  Returns the error object of this Expander. You can test whether there was
	  * any error by 
	  * \code
	  * if(exp.error())
	  *		error behaviour...
	  * else
	  *   no error...
	  * \endcode
	  * 
	  * @return The error object
	  */
	 const Error& error() const { return _err; }
protected:
  /**
   * This expands a whole commandline by calling for each Placeholder the corresponding expander
   * 
   * @param stringToExpand the commandline with the placeholder
   * @param useUrl true if the path's should be expanded to an URL instead of an local path
   * @return the expanded commanline for the current item
   */
  TagString expandCurrent( const TQString& stringToExpand, bool useUrl );
  /**
   * This function searches for "@EACH"-marks to splitt the string in a list for each %_Each%-item
   * 
   * @param stringToSplit the string which should be splitted
   * @return the splitted list
   */
  static TQStringList splitEach( TagString stringToSplit );
  /**
   * @param panelIndicator either '_' for panel-independent placeholders, 'a', 'o', 'r', or 'l' for the active, other (inactive), right or left panel
   * @return a pointer to the right panel or NULL if no panel is needed.
   */
  static ListPanel* getPanel( const char panelIndicator ,const exp_placeholder*,Expander&);
  /**
   *  This splits the parameter-string into separate parameter and expands each
   * @param exp the string holding all parameter
   * @param useUrl true if the path's should be expanded to an URL instead of an local path
   * @return a list of all parameter
   */
  TagStringList separateParameter( TQString* const exp, bool useUrl );
  /**
   * This finds the end of a placeholder, taking care of the parameter
   * @return the position where the placeholder ends
   */
  int findEnd( const TQString& str, int start );
  
	void setError(const Error &e) { _err=e; }
	friend class exp_placeholder;
  
private:
  static TQValueList <const exp_placeholder*>& _placeholder();
	Error _err;
	TQStringList resultList;
};

inline void exp_placeholder::setError(Expander& exp,const Error& e) { exp.setError(e); }
inline TQStringList exp_placeholder::splitEach(const TagString& s) { return Expander::splitEach(s); }
inline exp_placeholder::exp_placeholder() { Expander::_placeholder().push_back(this); }

#endif // ifndef EXPANDER_H