1 // Copyright (C) 2010-2015 Petr Pavlu <setup@dagobah.cz>
3 // This file is part of CenterIM.
5 // CenterIM is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
10 // CenterIM is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with CenterIM. If not, see <http://www.gnu.org/licenses/>.
21 #include <cppconsui/CheckBox.h>
22 #include <cppconsui/ComboBox.h>
23 #include <cppconsui/InputDialog.h>
24 #include <cppconsui/Label.h>
25 #include <cppconsui/SplitDialog.h>
26 #include <cppconsui/TextEntry.h>
27 #include <cppconsui/TreeView.h>
28 #include <libpurple/purple.h>
30 #define REQUEST (Request::instance())
34 static Request
*instance();
37 class RequestDialog
: public CppConsUI::SplitDialog
{
39 RequestDialog(const char *title
, const char *primary
, const char *secondary
,
40 const char *ok_text
, GCallback ok_cb
, const char *cancel_text
,
41 GCallback cancel_cb
, void *user_data
);
42 virtual ~RequestDialog() override
{}
45 virtual void onScreenResized() override
;
47 virtual PurpleRequestType
getRequestType() = 0;
54 // Convenience variable, same as dynamic_cast<ListBox *>(container).
55 CppConsUI::ListBox
*lbox_
;
57 virtual void responseHandler(
58 CppConsUI::SplitDialog
&activator
, ResponseType response
) = 0;
61 CONSUI_DISABLE_COPY(RequestDialog
);
64 class InputTextDialog
: public RequestDialog
{
66 InputTextDialog(const char *title
, const char *primary
,
67 const char *secondary
, const char *default_value
, bool masked
,
68 const char *ok_text
, GCallback ok_cb
, const char *cancel_text
,
69 GCallback cancel_cb
, void *user_data
);
70 virtual ~InputTextDialog() override
{}
73 virtual PurpleRequestType
getRequestType() override
;
76 CppConsUI::TextEntry
*entry_
;
79 virtual void responseHandler(
80 SplitDialog
&activator
, ResponseType response
) override
;
83 CONSUI_DISABLE_COPY(InputTextDialog
);
86 class ChoiceDialog
: public RequestDialog
{
88 ChoiceDialog(const char *title
, const char *primary
, const char *secondary
,
89 int default_value
, const char *ok_text
, GCallback ok_cb
,
90 const char *cancel_text
, GCallback cancel_cb
, void *user_data
,
92 virtual ~ChoiceDialog() override
{}
95 virtual PurpleRequestType
getRequestType() override
;
98 CppConsUI::ComboBox
*combo_
;
101 virtual void responseHandler(
102 SplitDialog
&activator
, ResponseType response
) override
;
105 CONSUI_DISABLE_COPY(ChoiceDialog
);
108 class ActionDialog
: public RequestDialog
{
110 ActionDialog(const char *title
, const char *primary
, const char *secondary
,
111 int default_value
, void *user_data
, std::size_t action_count
,
113 virtual ~ActionDialog() override
{}
116 virtual PurpleRequestType
getRequestType() override
;
120 virtual void responseHandler(
121 SplitDialog
&activator
, ResponseType response
) override
;
124 CONSUI_DISABLE_COPY(ActionDialog
);
127 CppConsUI::Button
&activator
, std::size_t i
, GCallback cb
);
130 class FieldsDialog
: public RequestDialog
{
132 FieldsDialog(const char *title
, const char *primary
, const char *secondary
,
133 PurpleRequestFields
*request_fields
, const char *ok_text
, GCallback ok_cb
,
134 const char *cancel_text
, GCallback cancel_cb
, void *user_data
);
135 virtual ~FieldsDialog() override
{}
138 virtual PurpleRequestType
getRequestType() override
;
141 PurpleRequestFields
*fields_
;
142 CppConsUI::TreeView
*treeview_
;
144 class StringField
: public CppConsUI::Button
{
146 StringField(PurpleRequestField
*field
);
147 virtual ~StringField() override
{}
150 PurpleRequestField
*field_
;
152 void onActivate(CppConsUI::Button
&activator
);
153 void responseHandler(CppConsUI::InputDialog
&activator
,
154 CppConsUI::AbstractDialog::ResponseType response
);
157 CONSUI_DISABLE_COPY(StringField
);
160 class IntegerField
: public CppConsUI::Button
{
162 IntegerField(PurpleRequestField
*field
);
163 virtual ~IntegerField() override
{}
166 PurpleRequestField
*field_
;
168 void onActivate(CppConsUI::Button
&activator
);
169 void responseHandler(CppConsUI::InputDialog
&activator
,
170 CppConsUI::AbstractDialog::ResponseType response
);
173 CONSUI_DISABLE_COPY(IntegerField
);
176 class BooleanField
: public CppConsUI::CheckBox
{
178 BooleanField(PurpleRequestField
*field
);
179 virtual ~BooleanField() override
{}
182 PurpleRequestField
*field_
;
184 void onToggle(CppConsUI::CheckBox
&activator
, bool new_state
);
187 CONSUI_DISABLE_COPY(BooleanField
);
190 class ChoiceField
: public CppConsUI::ComboBox
{
192 ChoiceField(PurpleRequestField
*field
);
193 virtual ~ChoiceField() override
{}
196 PurpleRequestField
*field_
;
198 void onSelectionChanged(CppConsUI::ComboBox
&activator
, int new_entry
,
199 const char *title
, intptr_t data
);
202 CONSUI_DISABLE_COPY(ChoiceField
);
205 class ListFieldMultiple
: public CppConsUI::ListBox
{
207 ListFieldMultiple(PurpleRequestField
*field
);
208 virtual ~ListFieldMultiple() override
{}
211 PurpleRequestField
*field_
;
213 class ListFieldItem
: public CppConsUI::CheckBox
{
215 ListFieldItem(PurpleRequestField
*field
, const char *text
);
216 virtual ~ListFieldItem() override
{}
219 PurpleRequestField
*field_
;
221 void onToggle(CppConsUI::CheckBox
&activator
, bool new_state
);
224 CONSUI_DISABLE_COPY(ListFieldItem
);
228 CONSUI_DISABLE_COPY(ListFieldMultiple
);
231 class ListFieldSingle
: public CppConsUI::ComboBox
{
233 ListFieldSingle(PurpleRequestField
*field
);
234 virtual ~ListFieldSingle() override
{}
237 PurpleRequestField
*field_
;
239 void onSelectionChanged(CppConsUI::ComboBox
&activator
, int new_entry
,
240 const char *title
, intptr_t data
);
243 CONSUI_DISABLE_COPY(ListFieldSingle
);
246 class LabelField
: public CppConsUI::Label
{
248 LabelField(PurpleRequestField
*field
);
249 virtual ~LabelField() override
{}
252 PurpleRequestField
*field_
;
255 CONSUI_DISABLE_COPY(LabelField
);
258 class ImageField
: public CppConsUI::Button
{
260 ImageField(PurpleRequestField
*field
);
261 virtual ~ImageField() override
{}
264 PurpleRequestField
*field_
;
266 void onActivate(CppConsUI::Button
&activator
);
269 CONSUI_DISABLE_COPY(ImageField
);
272 class AccountField
: public CppConsUI::ComboBox
{
274 AccountField(PurpleRequestField
*field
);
275 virtual ~AccountField() override
{}
278 PurpleRequestField
*field_
;
280 void onAccountChanged(CppConsUI::Button
&activator
, std::size_t new_entry
,
281 const char *title
, intptr_t data
);
284 CONSUI_DISABLE_COPY(AccountField
);
288 virtual void responseHandler(
289 CppConsUI::SplitDialog
&activator
, ResponseType response
) override
;
292 CONSUI_DISABLE_COPY(FieldsDialog
);
295 typedef std::set
<RequestDialog
*> Requests
;
299 PurpleRequestUiOps centerim_request_ui_ops_
;
301 static Request
*my_instance_
;
305 CONSUI_DISABLE_COPY(Request
);
308 static void finalize();
309 friend class CenterIM
;
311 void onDialogResponse(CppConsUI::SplitDialog
&dialog
,
312 CppConsUI::AbstractDialog::ResponseType response
);
314 static void *request_input_(const char *title
, const char *primary
,
315 const char *secondary
, const char *default_value
, gboolean multiline
,
316 gboolean masked
, char *hint
, const char *ok_text
, GCallback ok_cb
,
317 const char *cancel_text
, GCallback cancel_cb
, PurpleAccount
*account
,
318 const char *who
, PurpleConversation
*conv
, void *user_data
)
320 return REQUEST
->request_input(title
, primary
, secondary
, default_value
,
321 multiline
, masked
, hint
, ok_text
, ok_cb
, cancel_text
, cancel_cb
, account
,
322 who
, conv
, user_data
);
324 static void *request_choice_(const char *title
, const char *primary
,
325 const char *secondary
, int default_value
, const char *ok_text
,
326 GCallback ok_cb
, const char *cancel_text
, GCallback cancel_cb
,
327 PurpleAccount
*account
, const char *who
, PurpleConversation
*conv
,
328 void *user_data
, va_list choices
)
330 return REQUEST
->request_choice(title
, primary
, secondary
, default_value
,
331 ok_text
, ok_cb
, cancel_text
, cancel_cb
, account
, who
, conv
, user_data
,
334 static void *request_action_(const char *title
, const char *primary
,
335 const char *secondary
, int default_action
, PurpleAccount
*account
,
336 const char *who
, PurpleConversation
*conv
, void *user_data
,
337 std::size_t action_count
, va_list actions
)
339 return REQUEST
->request_action(title
, primary
, secondary
, default_action
,
340 account
, who
, conv
, user_data
, action_count
, actions
);
342 static void *request_fields_(const char *title
, const char *primary
,
343 const char *secondary
, PurpleRequestFields
*fields
, const char *ok_text
,
344 GCallback ok_cb
, const char *cancel_text
, GCallback cancel_cb
,
345 PurpleAccount
*account
, const char *who
, PurpleConversation
*conv
,
348 return REQUEST
->request_fields(title
, primary
, secondary
, fields
, ok_text
,
349 ok_cb
, cancel_text
, cancel_cb
, account
, who
, conv
, user_data
);
351 static void *request_file_(const char *title
, const char *filename
,
352 gboolean savedialog
, GCallback ok_cb
, GCallback cancel_cb
,
353 PurpleAccount
*account
, const char *who
, PurpleConversation
*conv
,
356 return REQUEST
->request_file(title
, filename
, savedialog
, ok_cb
, cancel_cb
,
357 account
, who
, conv
, user_data
);
359 static void close_request_(PurpleRequestType type
, void *ui_handle
)
361 REQUEST
->close_request(type
, ui_handle
);
363 static void *request_folder_(const char *title
, const char *dirname
,
364 GCallback ok_cb
, GCallback cancel_cb
, PurpleAccount
*account
,
365 const char *who
, PurpleConversation
*conv
, void *user_data
)
367 return REQUEST
->request_folder(
368 title
, dirname
, ok_cb
, cancel_cb
, account
, who
, conv
, user_data
);
370 static void *request_action_with_icon_(const char *title
, const char *primary
,
371 const char *secondary
, int default_action
, PurpleAccount
*account
,
372 const char *who
, PurpleConversation
*conv
, gconstpointer icon_data
,
373 gsize icon_size
, void *user_data
, std::size_t action_count
, va_list actions
)
375 return REQUEST
->request_action_with_icon(title
, primary
, secondary
,
376 default_action
, account
, who
, conv
, icon_data
, icon_size
, user_data
,
377 action_count
, actions
);
380 void *request_input(const char *title
, const char *primary
,
381 const char *secondary
, const char *default_value
, gboolean multiline
,
382 gboolean masked
, char *hint
, const char *ok_text
, GCallback ok_cb
,
383 const char *cancel_text
, GCallback cancel_cb
, PurpleAccount
*account
,
384 const char *who
, PurpleConversation
*conv
, void *user_data
);
385 void *request_choice(const char *title
, const char *primary
,
386 const char *secondary
, int default_value
, const char *ok_text
,
387 GCallback ok_cb
, const char *cancel_text
, GCallback cancel_cb
,
388 PurpleAccount
*account
, const char *who
, PurpleConversation
*conv
,
389 void *user_data
, va_list choices
);
390 void *request_action(const char *title
, const char *primary
,
391 const char *secondary
, int default_action
, PurpleAccount
*account
,
392 const char *who
, PurpleConversation
*conv
, void *user_data
,
393 std::size_t action_count
, va_list actions
);
394 void *request_fields(const char *title
, const char *primary
,
395 const char *secondary
, PurpleRequestFields
*fields
, const char *ok_text
,
396 GCallback ok_cb
, const char *cancel_text
, GCallback cancel_cb
,
397 PurpleAccount
*account
, const char *who
, PurpleConversation
*conv
,
399 void *request_file(const char *title
, const char *filename
,
400 gboolean savedialog
, GCallback ok_cb
, GCallback cancel_cb
,
401 PurpleAccount
*account
, const char *who
, PurpleConversation
*conv
,
403 void close_request(PurpleRequestType type
, void *ui_handle
);
404 void *request_folder(const char *title
, const char *dirname
, GCallback ok_cb
,
405 GCallback cancel_cb
, PurpleAccount
*account
, const char *who
,
406 PurpleConversation
*conv
, void *user_data
);
407 void *request_action_with_icon(const char *title
, const char *primary
,
408 const char *secondary
, int default_action
, PurpleAccount
*account
,
409 const char *who
, PurpleConversation
*conv
, gconstpointer icon_data
,
410 gsize icon_size
, void *user_data
, std::size_t action_count
,
416 // vim: set tabstop=2 shiftwidth=2 textwidth=80 expandtab: