libyui-rest-api
 
Loading...
Searching...
No Matches
YHttpWidgetsActionHandler.h
1/*
2 Copyright (C) 2017 SUSE LLC
3
4 This library is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) version 3.0 of the License. This library
8 is distributed in the hope that it will be useful, but WITHOUT ANY
9 WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
11 License for more details. You should have received a copy of the GNU
12 Lesser General Public License along with this library; if not, write
13 to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
14 Floor, Boston, MA 02110-1301 USA
15*/
16
17#ifndef YHttpWidgetsActionHandler_h
18#define YHttpWidgetsActionHandler_h
19
20#include <iostream>
21#include <functional>
22#include <microhttpd.h>
23#include <sstream>
24#include <boost/algorithm/string.hpp>
25
26#define TreePathDelimiter "|"
27
28#include <yui/YCheckBoxFrame.h>
29#include <yui/YComboBox.h>
30#include <yui/YDateField.h>
31#include <yui/YInputField.h>
32#include <yui/YItem.h>
33#include <yui/YMultiSelectionBox.h>
34#include <yui/YMenuItem.h>
35#include <yui/YRadioButton.h>
36#include <yui/YSelectionBox.h>
37#include <yui/YTimeField.h>
38#include <yui/YWidget.h>
39
40#include "YDumbTabActionHandler.h"
41#include "YMenuWidgetActionHandler.h"
42#include "YTableActionHandler.h"
43#include "YMultiSelectionBoxActionHandler.h"
44#include "YWidgetActionHandler.h"
45#include "YWidgetFinder.h"
46
47#include "YHttpHandler.h"
48
49
50#ifdef MHD_HTTP_UNPROCESSABLE_CONTENT
51# define YHTTP_UNPROCESSABLE MHD_HTTP_UNPROCESSABLE_CONTENT
52#else
53// Deprecated since libmicrohttpd 0.9.74 / 2021-12 (bsc#1193956)
54# define YHTTP_UNPROCESSABLE MHD_HTTP_UNPROCESSABLE_ENTITY
55#endif
56
57
58class YHttpWidgetsActionHandler : public YHttpHandler
59{
60
61public:
62
63 YHttpWidgetsActionHandler() {};
64 virtual ~YHttpWidgetsActionHandler() {};
65
66protected:
67
68 virtual void process_request(struct MHD_Connection* connection,
69 const char* url, const char* method, const char* upload_data,
70 size_t* upload_data_size, std::ostream& body, int& error_code,
71 std::string& content_type, bool *redraw);
72
73 int do_action( YWidget *widget, const std::string &action, struct MHD_Connection *connection, std::ostream& body );
74
80 virtual YMenuWidgetActionHandler* get_menu_handler();
81 virtual YWidgetActionHandler* get_widget_handler();
82 virtual YTableActionHandler* get_table_handler();
83 virtual YMultiSelectionBoxActionHandler* get_multiselectionbox_handler();
84
95 template<typename T>
96 int action_handler( YWidget *widget, std::ostream& body, std::function<void (T*)> handler_func, const bool allow_disabled = false ) {
97 if (auto w = dynamic_cast<T*>(widget)) {
98 try
99 {
100 // allow changing only the enabled widgets by default, as disabled ones
101 // cannot be changed by user from the UI in most of the cases
102 if( !widget->isEnabled() && !allow_disabled )
103 {
104 std::string error ("Cannot operate on disabled widget: ");
105 error.append( typeid(*widget).name() );
106 return handle_error( body, error, YHTTP_UNPROCESSABLE );
107 }
108 if ( handler_func )
109 handler_func(w);
110 }
111 // some widgets may throw an exception when setting invalid values
112 catch (const YUIException &e)
113 {
114 std::string error ("");
115 error.append( typeid(*widget).name() ).append( " " ).append( e.what() );
116 return handle_error( body, error, YHTTP_UNPROCESSABLE );
117 }
118 }
119 else {
120 return MHD_HTTP_NOT_FOUND;
121 }
122
123 return MHD_HTTP_OK;
124 }
125
126 YDumbTabActionHandler * dumb_tab_action_handler = nullptr;
127 YMenuWidgetActionHandler * menu_action_handler = nullptr;
128 YTableActionHandler * table_action_handler = nullptr;
129 YWidgetActionHandler * widget_action_handler = nullptr;
130 YMultiSelectionBoxActionHandler * multiselection_action_handler = nullptr;
131
132};
133
134#endif // YHttpWidgetsActionHandler_h
Definition YDumbTabActionHandler.h:26
int action_handler(YWidget *widget, std::ostream &body, std::function< void(T *)> handler_func, const bool allow_disabled=false)
Definition YHttpWidgetsActionHandler.h:96
virtual YDumbTabActionHandler * get_dumb_tab_handler()
Definition YHttpWidgetsActionHandler.cc:471
Definition YMenuWidgetActionHandler.h:24
Definition YMultiSelectionBoxActionHandler.h:28
Definition YTableActionHandler.h:31
Definition YWidgetActionHandler.h:45