summaryrefslogtreecommitdiffstats
path: root/kommander/widget/kommanderwidget.h
blob: 90a748b2039cc67aea8f668d32cdc05ab496889e (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
/***************************************************************************
                    kommanderwidget.h - Text widget core functionality 
                             -------------------
    copyright          : (C) 2002-2003 Marc Britton <consume@optusnet.com.au>
                         (C) 2004      Michal Rudolf <mrudolf@kdewebdwev.org>
    
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
 
#ifndef _HAVE_KOMMANDERWIDGET_H_
#define _HAVE_KOMMANDERWIDGET_H_

/* KDE INCLUDES */
#include <kprocess.h>

#include "kommander_export.h"
/* QT INCLUDES */
#include <qmap.h>
#include <qobject.h>
#include <qpair.h>
#include <qstring.h>
#include <qstringlist.h>

class ParserData;

class KOMMANDER_EXPORT KommanderWidget
{
  friend class MyProcess;
public:
  KommanderWidget(QObject *);
  virtual ~KommanderWidget();

  //List of available states. Most widgets have only one state, but f. e. radiobutton has both 
  // 'checked' and 'unchecked'
  virtual QStringList states() const;
  virtual QStringList displayStates() const;
  virtual QString currentState() const = 0;

  virtual bool isKommanderWidget() const = 0;

  // Associated script
  virtual void setAssociatedText(const QStringList& a_associations);
  virtual QStringList associatedText() const;
  virtual bool hasAssociatedText();

  // Execute default script, expanding all @macros.
  virtual QString evalAssociatedText();
  // Execute given script, expanding all @macros.
  virtual QString evalAssociatedText(const QString&);
  // Evaluate given Kommander function using given args.
  virtual QString evalFunction(const QString& function, const QStringList& args);
  // Parse and evaluate function for given widget, converting it to appropriate DCOP call.
  virtual QString evalWidgetFunction(const QString& identifier, const QString& s, int& pos);
  // Evaluate given array function using given args.
  virtual QString evalArrayFunction(const QString&, const QStringList&);
  // Parse and evaluate given execBegin..execEnd block.
  virtual QString evalExecBlock(const QStringList&, const QString& s, int& pos);
  // Parse and evaluate given forEach..end block.
  virtual QString evalForEachBlock(const QStringList&, const QString& s, int& pos);
  // Parse and evaluate given for..end block.
  virtual QString evalForBlock(const QStringList&, const QString& s, int& pos);
  // Parse and evaluate given switch..case..end block.
  virtual QString evalSwitchBlock(const QStringList&, const QString& s, int& pos);
  // Parse and evaluate given if..endif block.
  virtual QString evalIfBlock(const QStringList&, const QString& s, int& pos);
  // Population text. It will become widgetText after populate() is called
  virtual QString populationText() const;
  virtual void setPopulationText(const QString&);
  virtual void populate() = 0;

  // Handles all widget-specific DCOP calls 
  virtual QString handleDCOP(int function, const QStringList& args = QStringList());
  // Checks if appropriate function is supported by widget. By default all functions
  // are reported as supported: use this to allow recognizing incorrect function calls.
  virtual bool isFunctionSupported(int function);
  // Checks if the function is common widget function (i. e. supported by all widgets)
  virtual bool isCommonFunction(int function);
  // Checks if the string is a valid widget name)
  virtual bool isWidget(const QString& a_name) const;
  // Returns widget from name 
  virtual KommanderWidget* widgetByName(const QString& a_name) const;
  // Returns current widget name;
  virtual QString widgetName() const;
  // Returns filename associated with the dialog
  virtual QString fileName();

  QObject* object() { return m_thisObject;}


  // Recognizes editor vs executor mode
  static bool inEditor;
  // Prints errors in message boxes, not in stderr
  static bool showErrors;
  // Default parser
  static bool useInternalParser;
  // Return global variable value
  QString global(const QString& variableName);
  // Set global variable value
  void setGlobal(const QString& variableName, const QString& value);

protected:
  virtual void setStates(const QStringList& a_states);
  virtual void setDisplayStates(const QStringList& a_displayStates);

  // Execute DCOP query and return its result or null on failure
  // Only QString and int are now handled
  QString DCOPQuery(const QStringList& args);
  QString localDCOPQuery(const QString function, const QStringList& args = QStringList());
  QString localDCOPQuery(const QString function, const QString& arg1, 
     const QString& arg2, const QString& arg3 = QString::null,
     const QString& arg4 = QString::null);
  // Execute given command, return its result
  QString execCommand(const QString& a_command, const QString& a_shell = QString::null) const;
  // Find and run dialog (with optional parameters)
  QString runDialog(const QString& a_dialog, const QString& a_params = QString::null);
  // Display error message a_error; display current class name if no other is given
  void printError(const QString& a_error) const;
  // Auxiliary functions for parser
  // Find matching brackets starting from current position
  QString parseBrackets(const QString& s, int& from, bool& ok) const;
  // Return identifier: the longest string of letters and numbers starting from i
  QString parseIdentifier(const QString& s, int& from) const;
  // Parse arguments for given function. Returns list of arguments without quotations
  QStringList parseArgs(const QString& s, bool &ok);
  // Remove quotes from given identifier
  QString parseQuotes(const QString& s) const;
  // Parse function
  QStringList parseFunction(const QString& group, const QString& function,
    const QString& s, int& from, bool& ok);
  // Detect and return block boundary
  int parseBlockBoundary(const QString& s, int from, const QStringList& args) const;

  // Parse given identifier as widget name
  KommanderWidget* parseWidget(const QString& name) const;
  // Return parent dialog of this widget
  QWidget* parentDialog() const;
  QString substituteVariable(QString text, QString variable, QString value) const;

  ParserData* internalParserData() const;

  QObject *m_thisObject;
  QStringList m_states;
  QStringList m_displayStates;
  QStringList m_associatedText;
  QString m_populationText;

  // Internal parser data
  static ParserData* m_parserData;
};


#define ESCCHAR '@'

#endif