]> icculus.org git repositories - duncan/yast2-web-wt.git/blob - src/YWebUI_core.cc
link http library
[duncan/yast2-web-wt.git] / src / YWebUI_core.cc
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                               core system                            |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:         YWebUI_core.cc
14
15   Author:       Stefan Hundhammer <sh@suse.de>
16                 Stanislav Visnovsky <visnov@suse.cz>
17
18 /-*/
19
20 #include <rpc/types.h>          // MAXHOSTNAMELEN
21 #include <dlfcn.h>
22
23 #include <ycp/YCPTerm.h>
24 #include <ycp/YCPCode.h>
25
26 #define y2log_component "web-ui"
27 #include <ycp/y2log.h>
28
29 #include <WApplication>
30
31 #include "YWebUI.h"
32 #include "YWebApplication.h"
33 #include "YWebWidgetFactory.h"
34 #include "YWebOptionalWidgetFactory.h"
35 #include "YWebDialog.h"
36
37 using namespace Wt;
38
39 YWebUI * YWebUI::_ui = 0;
40 int YWebUI::_argc = 0;
41 char ** YWebUI::_argv = 0L;
42
43 YWebUI::YWebUI( const Wt::WEnvironment& env, const char * macro_file )
44     : WApplication(env),
45     YUI( true /* with_threads */ )
46     , _main_dialog_id(0)
47     , _do_exit_loop( false )
48 {
49     _ui                         = this;
50     _fatal_error                = false;
51
52 //    processCommandLineArgs( argc, argv );
53
54     topmostConstructorHasFinished();
55 }
56
57
58 void YWebUI::processCommandLineArgs( int argc, char **argv )
59 {
60     if ( argv )
61     {
62         for( int i=0; i < argc; i++ )
63         {
64             string opt = argv[i];
65
66             y2milestone ("Web argument: %s", argv[i]);
67
68         }
69     }
70 }
71
72
73
74 YWebUI::~YWebUI()
75 {
76     y2debug("Closing down Web UI.");
77 }
78
79
80
81 YWidgetFactory *
82 YWebUI::createWidgetFactory()
83 {
84     YWebWidgetFactory * factory = new YWebWidgetFactory();
85     YUI_CHECK_NEW( factory );
86
87     return factory;
88 }
89
90
91
92 YOptionalWidgetFactory *
93 YWebUI::createOptionalWidgetFactory()
94 {
95     YWebOptionalWidgetFactory * factory = new YWebOptionalWidgetFactory();
96     YUI_CHECK_NEW( factory );
97
98     return factory;
99 }
100
101
102 YApplication *
103 YWebUI::createApplication()
104 {
105     YWebApplication * app = new YWebApplication();
106                 YUI_CHECK_NEW( app );
107                 return app;
108 }
109
110
111
112 void YWebUI::internalError( const char * msg )
113 {
114     raiseFatalError();
115     abort();
116 }
117
118
119 void YWebUI::idleLoop( int fd_ycp )
120 {
121     // FIXME
122     _leave_idle_loop = true;
123 }
124
125
126 void YWebUI::leaveIdleLoop( int )
127 {
128     // FIXME
129     _leave_idle_loop = true;
130 }
131
132
133 void YWebUI::sendEvent( YEvent * event )
134 {
135     if ( event )
136     {
137         _event_handler.sendEvent( event );
138
139         if ( _do_exit_loop ) {
140         // FIXME:
141         }
142     }
143 }
144
145
146 YEvent * YWebUI::userInput( unsigned long timeout_millisec )
147 {
148     YEvent *    event  = 0;
149 #if 0
150     YQDialog *  dialog = dynamic_cast<YQDialog *> ( YDialog::currentDialog( false ) );
151
152     if ( _user_input_timer.isActive() )
153         _user_input_timer.stop();
154
155     if ( dialog )
156     {
157         if ( timeout_millisec > 0 )
158             _user_input_timer.start( timeout_millisec, true ); // single shot
159
160         dialog->activate( true );
161
162         if ( qApp->focusWidget() )
163             qApp->focusWidget()->setFocus();
164
165         normalCursor();
166         _do_exit_loop = true; // should exit_loop() be called in sendEvent()?
167
168         while ( ! pendingEvent() )
169         {
170             qApp->enter_loop();
171         }
172
173         _do_exit_loop = false;
174         event = _event_handler.consumePendingEvent();
175         dialog->activate( false );
176
177         // Display a busy cursor, but only if there is no other activity within
178         // BUSY_CURSOR_TIMEOUT milliseconds (avoid cursor flicker)
179
180         _busy_cursor_timer.start( BUSY_CURSOR_TIMEOUT, true ); // single shot
181     }
182
183     if ( _user_input_timer.isActive() )
184         _user_input_timer.stop();
185
186 #endif
187     return event;
188 }
189
190
191 YEvent * YWebUI::pollInput()
192 {
193     YEvent * event = 0;
194
195 #if 0
196     if ( _user_input_timer.isActive() )
197         _user_input_timer.stop();
198
199     if ( ! pendingEvent() )
200     {
201         YQDialog * dialog = dynamic_cast<YQDialog *> ( YDialog::currentDialog( false ) );
202
203         if ( dialog )
204         {
205             dialog->activate( true );
206             qApp->processEvents();
207             event = _event_handler.consumePendingEvent();
208             dialog->activate( false );
209         }
210     }
211
212     if ( pendingEvent() )
213         event = _event_handler.consumePendingEvent();
214 #endif
215     return event;
216 }
217
218
219 YDialog * YWebUI::createDialog( YWidgetOpt & opt )
220 {
221     return new YWebDialog( opt );
222 }
223
224
225 void YWebUI::showDialog( YDialog * dialog )
226 {
227     // FIXME
228     // event loop in Qt is here
229 }
230
231
232 void YWebUI::closeDialog( YDialog * dialog )
233 {
234     // FIXME
235 }
236
237
238 WApplication *createApplication(const WEnvironment& env)
239 {
240   // Instantiate the Wt application.
241   YWebUI *appl = new YWebUI( env, 0 );
242
243     // Set window title
244
245     std::string title( "YaST2" );
246     char hostname[ MAXHOSTNAMELEN+1 ];
247
248     if ( gethostname( hostname, sizeof( hostname )-1 ) == 0 )
249     {
250         hostname[ sizeof( hostname ) -1 ] = '\0'; // make sure it's terminated
251
252         if ( strlen( hostname ) > 0 )
253         {
254             if ( ( strcmp( hostname, "(none)" ) != 0 &&
255                    strcmp( hostname, "linux"  ) != 0 )
256                   )
257             {
258                 title += "@";
259                 title += hostname;
260             }
261         }
262     }
263
264   y2milestone( "Created WApplication (%p)", appl );
265
266   // Set application title
267   appl->setTitle(title);
268
269 #if 0
270   // Set up a timer for handling YaST requests
271   appl->createTimer();
272
273   _wtimer_work = true;
274 #endif
275
276   return appl;
277 }
278
279 void *start_webserver_thread( void * yui )
280 {
281     // FIXME: do the work
282     YWebUI* ui = (YWebUI*)yui;
283
284     WRun(ui->_argc, ui->_argv, &createApplication);
285
286     return 0;
287 }