]> icculus.org git repositories - duncan/yast2-web-wt.git/blob - src/YWebUI.h
added WTimer to evaluate the UI calls in the correct thread, does not
[duncan/yast2-web-wt.git] / src / YWebUI.h
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                               core system                            |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:         YWebUI.h
14
15   Author:       Stefan Hundhammer <sh@suse.de>
16                 Stanislav Visnovsky <visnov@suse.cz>
17
18 /-*/
19
20 #ifndef YWebUI_h
21 #define YWebUI_h
22
23 #include "YSimpleEventHandler.h"
24 #include <YUI.h>
25
26 #include <WApplication>
27 #include <WTimer>
28 #include <WEnvironment>
29
30 class YEvent;
31 class YWebOptionalWidgetFactory;
32 class YWebWidgetFactory;
33
34 using std::string;
35 using std::vector;
36
37
38 Wt::WApplication *createApplication(const Wt::WEnvironment& env);
39 void *start_webserver_thread( void * yui );
40
41 class YWebUI: public Wt::WApplication, public YUI
42 {
43 public:
44
45     /**
46      * Constructor.
47      **/
48     YWebUI( const Wt::WEnvironment& env,
49           const char *  macro_file );
50
51     /**
52      * Destructor.
53      **/
54     ~YWebUI();
55
56     /**
57      * Access the global Qt-UI.
58      **/
59     static YWebUI * ui() { return _ui; }
60
61
62 protected:
63     /**
64      * Create the widget factory that provides all the createXY() methods for
65      * standard (mandatory, i.e. non-optional) widgets.
66      *
67      * Reimplemented from YUI.
68      **/
69     virtual YWidgetFactory * createWidgetFactory();
70
71     /**
72      * Create the widget factory that provides all the createXY() methods for
73      * optional ("special") widgets and the corresponding hasXYWidget()
74      * methods.
75      *
76      * Reimplemented from YUI.
77      **/
78     virtual YOptionalWidgetFactory * createOptionalWidgetFactory();
79
80     /*
81      * Create the YApplication object that provides global methods.
82      *
83      * Reimplemented from YUI.
84      **/
85     virtual YApplication * createApplication();
86
87 public:
88
89     /**
90      * Widget event handlers (slots) call this when an event occured that
91      * should be the answer to a UserInput() / PollInput() (etc.) call.
92      *
93      * The UI assumes ownership of the event object that 'event' points to.
94      * In particular, it takes care to delete that object.
95      *
96      * It is an error to pass 0 for 'event'.
97      **/
98     void sendEvent( YEvent * event );
99
100     /**
101      * Returns 'true' if there is any event pending for the specified widget.
102      **/
103     bool eventPendingFor( YWidget * widget ) const
104         { return _event_handler.eventPendingFor( widget ); }
105
106     /**
107      * Returns the last event that isn't processed yet or 0 if there is none.
108      *
109      * The Qt UI keeps track of only one single (the last one) event.
110      **/
111     YEvent * pendingEvent() const { return _event_handler.pendingEvent(); }
112
113     /**
114      * Returns 'true' if the UI had a fatal error that requires the application
115      * to abort.
116      **/
117     bool fatalError() const { return _fatal_error; }
118
119     /**
120      * Raise a fatal UI error. It will be delivered when it is safe to do so.
121      * The caller should make sure it can continue for some time until the
122      * error is delivered.
123      **/
124     void raiseFatalError() { _fatal_error = true; }
125
126     /**
127      * UI-specific runPkgSeleciton method: Start the package selection.
128      * This implementation does the same as UserInput().
129      *
130      * Reimplemented from YUI.
131      **/
132     YCPValue runPkgSelection( YWidget * packageSelector );
133
134     /**
135      * Issue an internal error: Open popup with that message and wait.
136      *
137      * Reimplemented from YUI.
138      **/
139     void internalError( const char * msg );
140
141
142     /**
143      * Block (or unblock) events. If events are blocked, any event sent
144      * should be ignored until events are unblocked again.
145      *
146      * Reimplemented from YUI.
147      **/
148     virtual void blockEvents( bool block = true )
149         { _event_handler.blockEvents( block ); }
150
151     /**
152      * Returns 'true' if events are currently blocked.
153      *
154      * Reimplemented from YUI.
155      **/
156     virtual bool eventsBlocked() const
157         { return _event_handler.eventsBlocked(); }
158
159 protected:
160
161     /**
162      * Idle around until fd_ycp is readable and handle repaints.
163      * This is only used when a separate ui thread is running.
164      *
165      * Reimplemented from YUI.
166      **/
167     void idleLoop( int fd_ycp );
168
169     /**
170      * Return a representation for the glyph symbol specified in UTF-8 encoding
171      * or an empty string to get a default textual representation.
172      *
173      * Reimplemented from YUI.
174      **/
175     YCPString glyph( const YCPSymbol & glyphSymbol );
176
177     /**
178      * Go into event loop until next user input is available.
179      *
180      * Reimplemented from YUI.
181      **/
182     YEvent * userInput( unsigned long timeout_millisec = 0 );
183
184     /**
185      * Check the event queue for user input. Don't wait.
186      *
187      * Reimplemented from YUI.
188      **/
189     YEvent * pollInput();
190
191     /**
192      * Create a dialog.
193      *
194      * Reimplemented from YUI.
195      **/
196     YDialog * createDialog( YWidgetOpt & opt );
197
198     /**
199      * Show and activate a dialog.
200      *
201      * Reimplemented from YUI.
202      **/
203     void showDialog( YDialog * dialog );
204
205     /**
206      * Decativate and close a dialog. This does not delete the dialog yet.
207      *
208      * Reimplemented from YUI.
209      **/
210     void closeDialog( YDialog * dialog );
211
212     /*** Widget creation methods for optional widgets, all reimplemented from YUI ***/
213
214     bool        hasBarGraph();
215     YWidget *   createBarGraph          ( YWidget * parent, YWidgetOpt & opt);
216
217
218     bool        hasDownloadProgress();
219     YWidget *   createDownloadProgress  ( YWidget *             parent,
220                                           YWidgetOpt &          opt,
221                                           const YCPString &     label,
222                                           const YCPString &     filename,
223                                           int                   expectedSize );
224
225     bool        hasDumbTab();
226     YWidget *   createDumbTab           ( YWidget *             parent,
227                                           YWidgetOpt &          opt );
228
229     bool        hasSlider();
230     YWidget *   createSlider            ( YWidget *             parent,
231                                           YWidgetOpt &          opt,
232                                           const YCPString &     label,
233                                           int                   minValue,
234                                           int                   maxValue,
235                                           int                   initialValue );
236
237     bool        hasPartitionSplitter();
238     YWidget *   createPartitionSplitter( YWidget *              parent,
239                                          YWidgetOpt &           opt,
240                                          int                    usedSize,
241                                          int                    totalFreeSize,
242                                          int                    newPartSize,
243                                          int                    minNewPartSize,
244                                          int                    minFreeSize,
245                                          const YCPString &      usedLabel,
246                                          const YCPString &      freeLabel,
247                                          const YCPString &      newPartLabel,
248                                          const YCPString &      freeFieldLabel,
249                                          const YCPString &      newPartFieldLabel );
250
251
252     /*** END widget creation methods ***/
253
254
255 public:
256
257     /**
258      *
259      * Open a directory selection box and prompt the user for an existing directory.
260      * [Reimplemented from YUI]
261      *
262      * 'startDir' is the initial directory that is displayed.
263      *
264      * 'headline' is an explanatory text for the directory selection box.
265      * Graphical UIs may omit that if no window manager is running.
266      *
267      * Returns the selected directory name
268      * or 'nil' (YCPVoid() ) if the user canceled the operation.
269      **/
270     YCPValue askForExistingDirectory ( const YCPString & startDir,
271                                        const YCPString & headline );
272
273     /**
274      * Open a file selection box and prompt the user for an existing file.
275      * [Reimplemented from YUI]
276      *
277      * 'startWith' is the initial directory or file.
278      *
279      * 'filter' is one or more blank-separated file patterns, e.g. "*.png *.jpg"
280      *
281      * 'headline' is an explanatory text for the file selection box.
282      * Graphical UIs may omit that if no window manager is running.
283      *
284      * Returns the selected file name
285      * or 'nil' (YCPVoid() ) if the user canceled the operation.
286      **/
287     YCPValue askForExistingFile ( const YCPString & startWith,
288                                   const YCPString & filter,
289                                   const YCPString & headline );
290
291     /**
292      * Open a file selection box and prompt the user for a file to save data to.
293      * Automatically asks for confirmation if the user selects an existing file.
294      * [Reimplemented from YUI]
295      *
296      * 'startWith' is the initial directory or file.
297      *
298      * 'filter' is one or more blank-separated file patterns, e.g. "*.png *.jpg"
299      *
300      * 'headline' is an explanatory text for the file selection box.
301      * Graphical UIs may omit that if no window manager is running.
302      *
303      * Returns the selected file name
304      * or 'nil' (YCPVoid() ) if the user canceled the operation.
305      **/
306     YCPValue askForSaveFileName ( const YCPString & startWith,
307                                   const YCPString & filter,
308                                   const YCPString & headline );
309
310     /**
311      * Lower-level version that works with QStrings and does not change
312      * the mouse cursor.
313      **/
314     string askForSaveFileName(  const string & startWith,
315                                 const string & filter,
316                                 const string & headline );
317
318 protected:
319
320     /**
321      * Display capabilities.
322      * [Reimplemented from YUI]
323      * See UI builtin GetDisplayInfo() doc for details.
324      **/
325     bool textMode()                     { return false; }
326     bool hasImageSupport()              { return true;  }
327     bool hasLocalImageSupport()         { return false; }
328     bool hasAnimationSupport()          { return true;  }
329     bool hasIconSupport()               { return false; }       // not yet
330     bool hasFullUtf8Support()           { return true;  }
331     bool richTextSupportsTable()        { return true; }
332
333
334     /**
335      * Sets @ref #leave_idle_loop to true.
336      **/
337     void leaveIdleLoop( int );
338
339
340 protected:
341
342     /**
343      * Handle command line args
344      **/
345     void processCommandLineArgs( int argc, char **argv );
346
347     /**
348      * Calculate size of `opt(`defaultsize) dialogs
349      **/
350     void calcDefaultSize();
351
352
353     //
354     // Data members
355     //
356
357     /**
358      * Stack to keep track of the stacking order of popup dialogs.
359      **/
360     // FIXME: vector<QWidget *> _popup_stack;
361
362     /**
363      * Numeric ID for defaultsize dialogs for the widget stack
364      **/
365     int _main_dialog_id;
366
367     /**
368      * A flag used during the idle loop. If it is set to true,
369      * the idle loop is left. This happens, if the ycp-ui-communication
370      * pipe to the ui gets readable.
371      **/
372     bool _leave_idle_loop;
373
374     /**
375      * This flag is set during @ref #userInput in order to tell
376      * @ref #returnNow to call exit_loop, which only may be called
377      * after enter_loop.
378      **/
379     bool _do_exit_loop;
380
381
382     /**
383      * Global reference to the UI
384      **/
385     static YWebUI * _ui;
386
387     /**
388      * Indicate a fatal error that requires the UI to terminate
389      **/
390     bool _fatal_error;
391
392     /**
393      * The handler for the single pending event this UI keeps track of
394      **/
395     YSimpleEventHandler _event_handler;
396
397 public:
398     void createTimer() ;
399
400     void triggeredTimer();
401
402     Wt::WTimer* _timer;
403     
404     bool _wtimer_work;
405     
406     static int _argc;
407     static char** _argv;
408 };
409
410
411 #endif // YWebUI_h