1 #include "bcwindowbase.h"
2 #include "bcdisplayinfo.h"
5 #include "awindowgui.h"
10 #include "mainsession.h"
12 #include "shbtnprefs.h"
13 #include "preferences.h"
14 #include "preferencesthread.h"
19 ShBtnRun::ShBtnRun(const char *nm, const char *cmds, int warn)
22 argv.set_array_delete();
23 strncpy(name, nm, sizeof(name)-1);
24 name[sizeof(name)-1] = 0;
25 strncpy(commands, cmds, sizeof(commands)-1);
26 commands[sizeof(commands)-1] = 0;
31 argv.remove_all_objects();
34 void ShBtnRun::add_arg(const char *v)
36 argv.append(cstrdup(v));
49 execvp(argv[0], &argv[0]);
52 // warn <0:always, =0:never, >0:on err
54 int stat; waitpid(pid, &stat, 0);
56 if( warn > 0 ) return;
57 sprintf(msg, "%s: completed", name);
60 sprintf(msg, "%s: error exit status %d", name, stat);
61 MainError::show_error(msg);
64 ShBtnPref::ShBtnPref(const char *nm, const char *cmds, int warn, int run_script)
66 strncpy(name, nm, sizeof(name));
67 strncpy(commands, cmds, sizeof(commands));
69 this->run_script = run_script;
72 ShBtnPref::~ShBtnPref()
76 void ShBtnPref::execute(ArrayList<Indexable*> &args)
78 // thread async+autodelete, no explicit delete
79 ShBtnRun *job = new ShBtnRun(name, commands, warn);
80 job->add_arg("/bin/bash");
81 job->add_arg(commands);
83 for( int i=0; i<n; ++i ) {
84 Indexable *idxbl = args[i];
85 if( !idxbl->is_asset ) continue;
86 job->add_arg(idxbl->path);
91 void ShBtnPref::execute()
93 ShBtnRun *job = new ShBtnRun(name, commands, warn);
94 job->add_arg("/bin/bash");
96 job->add_arg(commands);
100 ShBtnEditDialog::ShBtnEditDialog(PreferencesWindow *pwindow)
103 this->pwindow = pwindow;
106 ShBtnEditDialog::~ShBtnEditDialog()
111 BC_Window* ShBtnEditDialog::new_gui()
113 BC_DisplayInfo display_info;
114 int x = display_info.get_abs_cursor_x();
115 int y = display_info.get_abs_cursor_y();
117 sb_window = new ShBtnEditWindow(this, x, y);
118 sb_window->create_objects();
122 void ShBtnEditDialog::handle_close_event(int result)
128 ShBtnEditWindow::ShBtnEditWindow(ShBtnEditDialog *shbtn_edit, int x, int y)
129 : BC_Window(_(PROGRAM_NAME ": Shell"), x, y,
130 xS(300), yS(200), xS(300), yS(200), 0, 0, 1)
132 this->shbtn_edit = shbtn_edit;
136 ShBtnEditWindow::~ShBtnEditWindow()
141 int ShBtnEditWindow::list_update()
143 shbtn_items.remove_all_objects();
144 Preferences *preferences = shbtn_edit->pwindow->thread->preferences;
145 for( int i=0; i<preferences->shbtn_prefs.size(); ++i ) {
146 shbtn_items.append(new ShBtnPrefItem(preferences->shbtn_prefs[i]));
148 return op_list->update(&shbtn_items, 0, 0, 1);
151 ShBtnAddButton::ShBtnAddButton(ShBtnEditWindow *sb_window, int x, int y)
152 : BC_GenericButton(x, y, _("Add"))
154 this->sb_window = sb_window;
157 ShBtnAddButton::~ShBtnAddButton()
161 int ShBtnAddButton::handle_event()
164 Preferences *preferences = sb_window->shbtn_edit->pwindow->thread->preferences;
165 ShBtnPref *pref = new ShBtnPref(_("new"), "", 0, 0);
166 preferences->shbtn_prefs.append(pref);
167 sb_window->list_update();
168 return sb_window->start_edit(pref);
171 ShBtnDelButton::ShBtnDelButton(ShBtnEditWindow *sb_window, int x, int y)
172 : BC_GenericButton(x, y, _("Del"))
174 this->sb_window = sb_window;
177 ShBtnDelButton::~ShBtnDelButton()
181 int ShBtnDelButton::handle_event()
183 ShBtnPrefItem *sp = (ShBtnPrefItem *)sb_window->op_list->get_selection(0,0);
185 Preferences *preferences = sb_window->shbtn_edit->pwindow->thread->preferences;
186 preferences->shbtn_prefs.remove(sp->pref);
187 sb_window->list_update();
191 ShBtnEditButton::ShBtnEditButton(ShBtnEditWindow *sb_window, int x, int y)
192 : BC_GenericButton(x, y, _("Edit"))
194 this->sb_window = sb_window;
197 ShBtnEditButton::~ShBtnEditButton()
201 int ShBtnEditButton::handle_event()
203 ShBtnPrefItem *sp = (ShBtnPrefItem *)sb_window->op_list->get_selection(0,0);
205 return sb_window->start_edit(sp->pref);
208 ShBtnTextDialog::ShBtnTextDialog(ShBtnEditWindow *sb_window)
211 this->sb_window = sb_window;
215 ShBtnTextDialog::~ShBtnTextDialog()
220 ShBtnTextWindow::ShBtnTextWindow(ShBtnEditWindow *sb_window, int x, int y)
221 : BC_Window(_(PROGRAM_NAME ": Commands"), x, y,
222 xS(640), yS(160), xS(640), yS(150), 0, 0, 1)
224 this->sb_window = sb_window;
225 warn = sb_window->sb_dialog->pref->warn;
226 run_script = sb_window->sb_dialog->pref->run_script;
229 ShBtnTextWindow::~ShBtnTextWindow()
234 ShBtnErrWarnItem::ShBtnErrWarnItem(ShBtnErrWarn *popup,
235 const char *text, int warn)
242 int ShBtnErrWarnItem::handle_event()
244 popup->set_text(get_text());
245 popup->st_window->warn = warn;
249 ShBtnErrWarn::ShBtnErrWarn(ShBtnTextWindow *st_window, int x, int y)
250 : BC_PopupMenu(x, y, xS(120), st_window->warn < 0 ? _("Always"):
251 !st_window->warn ? _("Never") : _("On Error"))
253 this->st_window = st_window;
255 ShBtnErrWarn::~ShBtnErrWarn()
258 int ShBtnErrWarn::handle_event()
263 void ShBtnErrWarn::create_objects()
265 add_item(new ShBtnErrWarnItem(this,_("Always"), -1));
266 add_item(new ShBtnErrWarnItem(this,_("Never"), 0));
267 add_item(new ShBtnErrWarnItem(this,_("On Error"), 1));
271 ShBtnRunScript::ShBtnRunScript(ShBtnTextWindow *st_window, int x, int y)
272 : BC_CheckBox(x, y, &st_window->run_script, _("run /path/script.sh + argvs"))
274 this->st_window = st_window;
277 ShBtnRunScript::~ShBtnRunScript()
281 void ShBtnTextWindow::create_objects()
283 lock_window("ShBtnTextWindow::create_objects");
284 int x = xS(10), y = yS(10);
286 BC_Title *title = new BC_Title(x, y, _("Label:"));
287 add_subwindow(title);
288 title = new BC_Title(x1, y, _("Commands:"));
289 add_subwindow(title);
290 y += title->get_h() + yS(8);
291 ShBtnPref *pref = sb_window->sb_dialog->pref;
292 cmd_name = new BC_TextBox(x, y, xS(140), 1, pref->name);
293 add_subwindow(cmd_name);
294 cmd_text = new BC_ScrollTextBox(this, x1, y, get_w()-x1-xS(20), 4, pref->commands);
295 cmd_text->create_objects();
296 y += cmd_text->get_h() + yS(16);
297 add_subwindow(title = new BC_Title(x1,y, _("OnExit Notify:")));
298 x1 += title->get_w() + xS(10);
299 add_subwindow(st_err_warn = new ShBtnErrWarn(this, x1, y));
300 st_err_warn->create_objects();
301 x1 += st_err_warn->get_w() + xS(20);
302 add_subwindow(st_run_script = new ShBtnRunScript(this, x1, y));
303 y = get_h() - ShBtnTextOK::calculate_h() - yS(10);
304 add_subwindow(new ShBtnTextOK(this, x, y));
309 ShBtnTextOK::ShBtnTextOK(ShBtnTextWindow *st_window, int x, int y)
312 this->st_window = st_window;
315 ShBtnTextOK::~ShBtnTextOK()
319 int ShBtnTextOK::handle_event()
321 ShBtnPref *pref = st_window->sb_window->sb_dialog->pref;
322 strcpy(pref->name, st_window->cmd_name->get_text());
323 strcpy(pref->commands, st_window->cmd_text->get_text());
324 pref->warn = st_window->warn;
325 pref->run_script = st_window->run_script;
326 return BC_OKButton::handle_event();
330 BC_Window *ShBtnTextDialog::new_gui()
332 BC_DisplayInfo display_info;
333 int x = display_info.get_abs_cursor_x();
334 int y = display_info.get_abs_cursor_y();
336 st_window = new ShBtnTextWindow(sb_window, x, y);
337 st_window->create_objects();
341 void ShBtnTextDialog::handle_close_event(int result)
344 sb_window->lock_window("ShBtnTextDialog::handle_close_event");
345 sb_window->list_update();
346 sb_window->unlock_window();
351 int ShBtnTextDialog::start_edit(ShBtnPref *pref)
358 void ShBtnEditWindow::create_objects()
360 lock_window("ShBtnEditWindow::create_objects");
361 Preferences *preferences = shbtn_edit->pwindow->thread->preferences;
362 for( int i=0; i<preferences->shbtn_prefs.size(); ++i ) {
363 shbtn_items.append(new ShBtnPrefItem(preferences->shbtn_prefs[i]));
365 int x = xS(10), y = yS(10);
366 add_subwindow(op_list = new ShBtnPrefList(this, x, y));
368 add_subwindow(add_button = new ShBtnAddButton(this, x, y));
369 y += add_button->get_h() + yS(8);
370 add_subwindow(del_button = new ShBtnDelButton(this, x, y));
371 y += del_button->get_h() + yS(8);
372 add_subwindow(edit_button = new ShBtnEditButton(this, x, y));
373 add_subwindow(new BC_OKButton(this));
378 int ShBtnEditWindow::start_edit(ShBtnPref *pref)
381 sb_dialog = new ShBtnTextDialog(this);
382 return sb_dialog->start_edit(pref);
386 ShBtnPrefItem::ShBtnPrefItem(ShBtnPref *pref)
387 : BC_ListBoxItem(pref->name)
392 ShBtnPrefItem::~ShBtnPrefItem()
396 ShBtnPrefList::ShBtnPrefList(ShBtnEditWindow *sb_window, int x, int y)
397 : BC_ListBox(x, y, xS(140), yS(100), LISTBOX_TEXT, &sb_window->shbtn_items, 0, 0)
399 this->sb_window = sb_window;
402 ShBtnPrefList::~ShBtnPrefList()
406 int ShBtnPrefList::handle_event()
411 MainShBtnItem::MainShBtnItem(MainShBtns *shbtns, ShBtnPref *pref)
412 : BC_MenuItem(pref->name)
414 this->shbtns = shbtns;
418 int MainShBtnItem::handle_event()
420 MWindow *mwindow = shbtns->mwindow;
421 if( pref->run_script ) {
422 AWindowGUI *agui = mwindow->awindow->gui;
423 agui->lock_window("MainShBtnItem::handle_event");
424 mwindow->awindow->gui->collect_assets();
425 pref->execute(*mwindow->session->drag_assets);
426 agui->unlock_window();
433 MainShBtns::MainShBtns(MWindow *mwindow, int x, int y)
434 : BC_PopupMenu(x, y, 0, "", -1, mwindow->theme->shbtn_data)
436 this->mwindow = mwindow;
437 set_tooltip(_("shell cmds"));
440 int MainShBtns::load(Preferences *preferences)
442 while( total_items() ) del_item(0);
443 for( int i=0; i<preferences->shbtn_prefs.size(); ++i )
444 add_item(new MainShBtnItem(this, preferences->shbtn_prefs[i]));
448 int MainShBtns::handle_event()