Session Metadata: add a Description field.
[ardour.git] / gtk2_ardour / luawindow.cc
1 /*
2     Copyright (C) 2016 Robin Gareus <robin@gareus.org>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #ifdef PLATFORM_WINDOWS
21 #define random() rand()
22 #endif
23
24 #ifdef WAF_BUILD
25 #include "gtk2ardour-config.h"
26 #endif
27
28 #include "pbd/gstdio_compat.h"
29 #include <glibmm/fileutils.h>
30 #include <gtkmm/messagedialog.h>
31
32 #include "pbd/basename.h"
33 #include "pbd/file_utils.h"
34 #include "pbd/md5.h"
35
36 #include "gtkmm2ext/gtk_ui.h"
37 #include "gtkmm2ext/utils.h"
38 #include "gtkmm2ext/window_title.h"
39
40 #include "widgets/pane.h"
41 #include "widgets/tooltips.h"
42
43 #include "ardour/filesystem_paths.h"
44 #include "ardour/luabindings.h"
45 #include "LuaBridge/LuaBridge.h"
46
47 #include "ardour_ui.h"
48 #include "gui_thread.h"
49 #include "luainstance.h"
50 #include "luawindow.h"
51 #include "public_editor.h"
52 #include "utils.h"
53 #include "utils_videotl.h"
54
55 #include "pbd/i18n.h"
56
57 using namespace ARDOUR;
58 using namespace PBD;
59 using namespace Gtk;
60 using namespace Glib;
61 using namespace Gtkmm2ext;
62 using namespace std;
63
64
65 inline LuaWindow::BufferFlags operator| (const LuaWindow::BufferFlags& a, const LuaWindow::BufferFlags& b) {
66         return static_cast<LuaWindow::BufferFlags> (static_cast <int>(a) | static_cast<int> (b));
67 }
68
69 inline LuaWindow::BufferFlags operator|= (LuaWindow::BufferFlags& a, const LuaWindow::BufferFlags& b) {
70         return a = static_cast<LuaWindow::BufferFlags> (static_cast <int>(a) | static_cast<int> (b));
71 }
72
73 inline LuaWindow::BufferFlags operator&= (LuaWindow::BufferFlags& a, const LuaWindow::BufferFlags& b) {
74         return a = static_cast<LuaWindow::BufferFlags> (static_cast <int>(a) & static_cast<int> (b));
75 }
76
77 LuaWindow* LuaWindow::_instance = 0;
78
79 LuaWindow*
80 LuaWindow::instance ()
81 {
82         if (!_instance) {
83                 _instance  = new LuaWindow;
84         }
85
86         return _instance;
87 }
88
89 LuaWindow::LuaWindow ()
90         : Window (Gtk::WINDOW_TOPLEVEL)
91         , VisibilityTracker (*((Gtk::Window*) this))
92         , lua (0)
93         , _visible (false)
94         , _menu_scratch (0)
95         , _menu_snippet (0)
96         , _menu_actions (0)
97         , _btn_run (_("Run"))
98         , _btn_clear (_("Clear Output"))
99         , _btn_open (_("Import"))
100         , _btn_save (_("Save"))
101         , _btn_delete (_("Delete"))
102         , _btn_revert (_("Revert"))
103         , _current_buffer ()
104 {
105         set_name ("Lua");
106
107         reinit_lua ();
108         update_title ();
109         set_wmclass (X_("ardour_mixer"), PROGRAM_NAME);
110
111         script_select.disable_scrolling ();
112
113         set_border_width (0);
114
115         outtext.set_editable (false);
116         outtext.set_wrap_mode (Gtk::WRAP_WORD);
117         outtext.set_cursor_visible (false);
118
119         signal_delete_event().connect (sigc::mem_fun (*this, &LuaWindow::hide_window));
120         signal_configure_event().connect (sigc::mem_fun (*ARDOUR_UI::instance(), &ARDOUR_UI::configure_handler));
121
122         _btn_run.signal_clicked.connect (sigc::mem_fun(*this, &LuaWindow::run_script));
123         _btn_clear.signal_clicked.connect (sigc::mem_fun(*this, &LuaWindow::clear_output));
124         _btn_open.signal_clicked.connect (sigc::mem_fun(*this, &LuaWindow::import_script));
125         _btn_save.signal_clicked.connect (sigc::mem_fun(*this, &LuaWindow::save_script));
126         _btn_delete.signal_clicked.connect (sigc::mem_fun(*this, &LuaWindow::delete_script));
127         _btn_revert.signal_clicked.connect (sigc::mem_fun(*this, &LuaWindow::revert_script));
128
129         _btn_open.set_sensitive (false); // TODO
130         _btn_save.set_sensitive (false);
131         _btn_delete.set_sensitive (false);
132         _btn_revert.set_sensitive (false);
133
134         // layout
135
136         Gtk::ScrolledWindow *scrollin = manage (new Gtk::ScrolledWindow);
137         scrollin->set_policy (Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
138         scrollin->add (entry);
139         scrollout.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_ALWAYS);
140         scrollout.add (outtext);
141
142         entry.set_name ("ArdourLuaEntry");
143         outtext.set_name ("ArdourLuaEntry");
144
145         Gtk::HBox *hbox = manage (new HBox());
146
147         hbox->pack_start (_btn_run, false, false, 2);
148         hbox->pack_start (_btn_clear, false, false, 2);
149         hbox->pack_start (_btn_open, false, false, 2);
150         hbox->pack_start (_btn_save, false, false, 2);
151         hbox->pack_start (_btn_delete, false, false, 2);
152         hbox->pack_start (_btn_revert, false, false, 2);
153         hbox->pack_start (script_select, false, false, 2);
154
155         Gtk::VBox *vbox = manage (new VBox());
156         vbox->pack_start (*scrollin, true, true, 0);
157         vbox->pack_start (*hbox, false, false, 2);
158
159         ArdourWidgets::VPane *vpane = manage (new ArdourWidgets::VPane ());
160         vpane->add (*vbox);
161         vpane->add (scrollout);
162         vpane->set_divider (0, 0.75);
163
164         vpane->show_all ();
165         add (*vpane);
166         set_size_request (640, 480); // XXX
167         ArdourWidgets::set_tooltip (script_select, _("Select Editor Buffer"));
168
169         setup_buffers ();
170         LuaScripting::instance().scripts_changed.connect (*this, invalidator (*this), boost::bind (&LuaWindow::refresh_scriptlist, this), gui_context());
171
172         Glib::RefPtr<Gtk::TextBuffer> tb (entry.get_buffer());
173         _script_changed_connection = tb->signal_changed().connect (sigc::mem_fun(*this, &LuaWindow::script_changed));
174 }
175
176 LuaWindow::~LuaWindow ()
177 {
178         delete lua;
179 }
180
181 void
182 LuaWindow::show_window ()
183 {
184         present();
185         _visible = true;
186 }
187
188 bool
189 LuaWindow::hide_window (GdkEventAny *ev)
190 {
191         if (!_visible) return 0;
192         _visible = false;
193         return ARDOUR_UI_UTILS::just_hide_it (ev, static_cast<Gtk::Window *>(this));
194 }
195
196 void LuaWindow::reinit_lua ()
197 {
198         ENSURE_GUI_THREAD (*this, &LuaWindow::session_going_away);
199         delete lua;
200         lua = new LuaState();
201         lua->Print.connect (sigc::mem_fun (*this, &LuaWindow::append_text));
202         lua->sandbox (false);
203
204         lua_State* L = lua->getState();
205         LuaInstance::register_classes (L);
206         luabridge::push <PublicEditor *> (L, &PublicEditor::instance());
207         lua_setglobal (L, "Editor");
208 }
209
210 void LuaWindow::set_session (Session* s)
211 {
212         SessionHandlePtr::set_session (s);
213         if (!_session) {
214                 return;
215         }
216
217         update_title ();
218         _session->DirtyChanged.connect (_session_connections, invalidator (*this), boost::bind (&LuaWindow::update_title, this), gui_context());
219
220         lua_State* L = lua->getState();
221         LuaBindings::set_session (L, _session);
222 }
223
224 void
225 LuaWindow::session_going_away ()
226 {
227         ENSURE_GUI_THREAD (*this, &LuaWindow::session_going_away);
228         reinit_lua (); // drop state (all variables, session references)
229
230         SessionHandlePtr::session_going_away ();
231         _session = 0;
232         update_title ();
233
234         lua_State* L = lua->getState();
235         LuaBindings::set_session (L, _session);
236 }
237
238 void
239 LuaWindow::update_title ()
240 {
241         if (_session) {
242                 string n;
243
244                 if (_session->snap_name() != _session->name()) {
245                         n = _session->snap_name ();
246                 } else {
247                         n = _session->name ();
248                 }
249
250                 if (_session->dirty ()) {
251                         n = "*" + n;
252                 }
253
254                 WindowTitle title (n);
255                 title += S_("Window|Lua");
256                 title += Glib::get_application_name ();
257                 set_title (title.get_string());
258
259         } else {
260                 WindowTitle title (S_("Window|Lua"));
261                 title += Glib::get_application_name ();
262                 set_title (title.get_string());
263         }
264 }
265
266 void
267 LuaWindow::scroll_to_bottom ()
268 {
269         Gtk::Adjustment *adj;
270         adj = scrollout.get_vadjustment();
271         adj->set_value (MAX(0,(adj->get_upper() - adj->get_page_size())));
272 }
273
274 void
275 LuaWindow::run_script ()
276 {
277         Glib::RefPtr<Gtk::TextBuffer> tb (entry.get_buffer());
278         std::string script = tb->get_text();
279         const std::string& bytecode = LuaScripting::get_factory_bytecode (script);
280         if (bytecode.empty()) {
281                 // plain script or faulty script -- run directly
282                 try {
283                         lua->do_command ("function ardour () end");
284                         if (0 == lua->do_command (script)) {
285                                 append_text ("> OK");
286                         }
287                 } catch (luabridge::LuaException const& e) {
288                         append_text (string_compose (_("LuaException: %1"), e.what()));
289                 }
290         } else {
291                 // script with factory method
292                 try {
293                         lua_State* L = lua->getState();
294                         lua->do_command ("function ardour () end");
295
296                         LuaScriptParamList args = LuaScriptParams::script_params (script, "action_param", false);
297                         luabridge::LuaRef tbl_arg (luabridge::newTable(L));
298                         LuaScriptParams::params_to_ref (&tbl_arg, args);
299                         lua->do_command (script); // register "factory"
300                         luabridge::LuaRef lua_factory = luabridge::getGlobal (L, "factory");
301                         if (lua_factory.isFunction()) {
302                                 lua_factory(tbl_arg)();
303                         }
304                         lua->do_command ("factory = nil;");
305                 } catch (luabridge::LuaException const& e) {
306                         append_text (string_compose (_("LuaException: %1"), e.what()));
307                 }
308         }
309 }
310
311 void
312 LuaWindow::append_text (std::string s)
313 {
314         Glib::RefPtr<Gtk::TextBuffer> tb (outtext.get_buffer());
315         tb->insert (tb->end(), s + "\n");
316         scroll_to_bottom ();
317         Gtkmm2ext::UI::instance()->flush_pending (0.05);
318 }
319
320 void
321 LuaWindow::clear_output ()
322 {
323         Glib::RefPtr<Gtk::TextBuffer> tb (outtext.get_buffer());
324         tb->set_text ("");
325 }
326
327 void
328 LuaWindow::edit_script (const std::string& name, const std::string& script)
329 {
330         ScriptBuffer* sb = new LuaWindow::ScriptBuffer (name);
331         sb->script = script;
332         script_buffers.push_back (ScriptBufferPtr (sb));
333         script_selection_changed (script_buffers.back ());
334         refresh_scriptlist ();
335         show_window ();
336 }
337
338 void
339 LuaWindow::new_script ()
340 {
341         char buf[32];
342         snprintf (buf, sizeof (buf), "#%d", count_scratch_buffers () + 1);
343         script_buffers.push_back (ScriptBufferPtr (new LuaWindow::ScriptBuffer (buf)));
344         script_selection_changed (script_buffers.back ());
345         refresh_scriptlist ();
346 }
347
348 void
349 LuaWindow::delete_script ()
350 {
351         assert ((_current_buffer->flags & Buffer_Scratch) || !(_current_buffer->flags & Buffer_ReadOnly));
352         bool refresh = false;
353         bool neednew = true;
354         if (_current_buffer->flags & Buffer_HasFile) {
355                 if (0 == ::g_unlink (_current_buffer->path.c_str())) {
356                         append_text (X_("> ") + string_compose (_("Deleted %1"), _current_buffer->path));
357                         refresh = true;
358                 } else {
359                         append_text (X_("> ") + string_compose (_("Failed to delete %1"), _current_buffer->path));
360                 }
361         }
362         for (ScriptBufferList::iterator i = script_buffers.begin (); i != script_buffers.end (); ++i) {
363                 if ((*i) == _current_buffer) {
364                         script_buffers.erase (i);
365                         break;
366                 }
367         }
368
369         for (ScriptBufferList::const_iterator i = script_buffers.begin (); i != script_buffers.end (); ++i) {
370                 if ((*i)->flags & Buffer_Scratch) {
371                         script_selection_changed (*i);
372                         neednew = false;
373                 }
374         }
375         if (neednew) {
376                 new_script ();
377         }
378         if (refresh) {
379                 LuaScripting::instance ().refresh (true);
380         }
381 }
382
383 void
384 LuaWindow::revert_script ()
385 {
386         _current_buffer->flags &= BufferFlags(~Buffer_Valid);
387         script_selection_changed (_current_buffer, true);
388 }
389
390 void
391 LuaWindow::import_script ()
392 {
393         // TODO: dialog to select file or enter URL
394         // TODO convert a few URL (eg. pastebin) to raw.
395 #if 0
396         char *url = "http://pastebin.com/raw/3UMkZ6nV";
397         char *rv = ArdourCurl::http_get (url, 0);
398         if (rv) {
399                 new_script ();
400                 Glib::RefPtr<Gtk::TextBuffer> tb (entry.get_buffer());
401                 tb->set_text (rv);
402                 _current_buffer->flags &= BufferFlags(~Buffer_Dirty);
403                 update_gui_state ();
404         }
405         free (rv);
406 #endif
407 }
408
409 void
410 LuaWindow::save_script ()
411 {
412         Glib::RefPtr<Gtk::TextBuffer> tb (entry.get_buffer());
413         std::string script = tb->get_text();
414         std::string msg = "Unknown error";
415
416         std::string path;
417         LuaScriptInfoPtr lsi = LuaScripting::script_info (script);
418         ScriptBuffer & sb (*_current_buffer);
419
420         assert (sb.flags & Buffer_Dirty);
421
422         // 1) check if it has a valid header and factory
423         const std::string& bytecode = LuaScripting::get_factory_bytecode (script);
424         if (bytecode.empty()) {
425                 msg = _("Missing script header.\nThe script requires an '{ardour}' info table and a 'factory' function.");
426                 goto errorout;
427         }
428
429         if (!LuaScripting::try_compile (script, LuaScriptParams::script_params (script, "action_param", false))) {
430                 msg = _("Script fails to compile.");
431                 goto errorout;
432         }
433
434         // 2) check script name & type
435         lsi = LuaScripting::script_info (script);
436         if (!lsi) {
437                 msg = _("Invalid or missing script-name or script-type.");
438                 goto errorout;
439         }
440
441         if (lsi->type != LuaScriptInfo::Snippet && lsi->type != LuaScriptInfo::EditorAction) {
442                 msg = _("Invalid script-type.\nValid types are 'EditorAction' and 'Snippet'.");
443                 goto errorout;
444         }
445
446         // 3) if there's already a writable file,...
447         if ((sb.flags & Buffer_HasFile) && !(sb.flags & Buffer_ReadOnly)) {
448                 try {
449                         Glib::file_set_contents (sb.path, script);
450                         sb.name = lsi->name;
451                         sb.flags &= BufferFlags(~Buffer_Dirty);
452                         update_gui_state (); // XXX here?
453                         append_text (X_("> ") + string_compose (_("Saved as %1"), sb.path));
454                         return; // OK
455                 } catch (Glib::FileError e) {
456                         msg = string_compose (_("Error saving file: %1"), e.what());
457                         goto errorout;
458                 }
459         }
460
461         // 4) check if the name is unique for the given type; locally at least
462         if (true /*sb.flags & Buffer_HasFile*/) {
463                 LuaScriptList& lsl (LuaScripting::instance ().scripts (lsi->type));
464                 for (LuaScriptList::const_iterator s = lsl.begin(); s != lsl.end(); ++s) {
465                         if ((*s)->name == lsi->name) {
466                                 msg = string_compose (_("Script with given name '%1' already exists.\nUse a different name in the descriptor."), lsi->name);
467                                 goto errorout;
468                         }
469                 }
470         }
471
472         // 5) construct filename -- TODO ask user for name, ask to replace file.
473         do {
474                 char buf[80];
475                 time_t t = time(0);
476                 struct tm * timeinfo = localtime (&t);
477                 strftime (buf, sizeof(buf), "%s%d", timeinfo);
478                 sprintf (buf, "%s%ld", buf, random ()); // is this valid?
479                 MD5 md5;
480                 std::string fn = md5.digestString (buf);
481
482                 switch (lsi->type) {
483                         case LuaScriptInfo::EditorAction:
484                                 fn = "a_" + fn;
485                                 break;
486                         case LuaScriptInfo::Snippet:
487                                 fn = "s_" + fn;
488                                 break;
489                         default:
490                                 break;
491                 }
492                 path = Glib::build_filename (LuaScripting::user_script_dir (), fn.substr(0, 11) + ".lua");
493         } while (Glib::file_test (path, Glib::FILE_TEST_EXISTS));
494
495         try {
496                 Glib::file_set_contents (path, script);
497                 sb.path = path;
498                 sb.name = lsi->name;
499                 sb.flags |= Buffer_HasFile;
500                 sb.flags &= BufferFlags(~Buffer_Dirty);
501                 sb.flags &= BufferFlags(~Buffer_ReadOnly);
502                 update_gui_state (); // XXX here? .refresh (true) may trigger this, too
503                 LuaScripting::instance().refresh (true);
504                 append_text (X_("> ") + string_compose (_("Saved as %1"), path));
505                 return; // OK
506         } catch (Glib::FileError e) {
507                 msg = string_compose (_("Error saving file: %1"), e.what());
508                 goto errorout;
509         }
510
511 errorout:
512                 MessageDialog am (msg);
513                 am.run ();
514 }
515
516 void
517 LuaWindow::setup_buffers ()
518 {
519         if (script_buffers.size() > 0) {
520                 return;
521         }
522         script_buffers.push_back (ScriptBufferPtr (new LuaWindow::ScriptBuffer("#1")));
523         _current_buffer = script_buffers.front();
524
525         Glib::RefPtr<Gtk::TextBuffer> tb (entry.get_buffer());
526         tb->set_text (_current_buffer->script);
527
528         refresh_scriptlist ();
529         update_gui_state ();
530 }
531
532 uint32_t
533 LuaWindow::count_scratch_buffers () const
534 {
535         uint32_t n = 0;
536         for (ScriptBufferList::const_iterator i = script_buffers.begin (); i != script_buffers.end (); ++i) {
537                 if ((*i)->flags & Buffer_Scratch) {
538                         ++n;
539                 }
540         }
541         return n;
542 }
543
544 void
545 LuaWindow::refresh_scriptlist ()
546 {
547         for (ScriptBufferList::iterator i = script_buffers.begin (); i != script_buffers.end ();) {
548                 if ((*i)->flags & Buffer_Scratch) {
549                         ++i;
550                         continue;
551                 }
552                 i = script_buffers.erase (i);
553         }
554         LuaScriptList& lsa (LuaScripting::instance ().scripts (LuaScriptInfo::EditorAction));
555         for (LuaScriptList::const_iterator s = lsa.begin(); s != lsa.end(); ++s) {
556                 script_buffers.push_back (ScriptBufferPtr (new LuaWindow::ScriptBuffer(*s)));
557         }
558
559         LuaScriptList& lss (LuaScripting::instance ().scripts (LuaScriptInfo::Snippet));
560         for (LuaScriptList::const_iterator s = lss.begin(); s != lss.end(); ++s) {
561                 script_buffers.push_back (ScriptBufferPtr (new LuaWindow::ScriptBuffer(*s)));
562         }
563         rebuild_menu ();
564 }
565
566 void
567 LuaWindow::rebuild_menu ()
568 {
569         using namespace Menu_Helpers;
570
571         _menu_scratch = manage (new Menu);
572         _menu_snippet = manage (new Menu);
573         _menu_actions = manage (new Menu);
574
575         MenuList& items_scratch (_menu_scratch->items());
576         MenuList& items_snippet (_menu_snippet->items());
577         MenuList& items_actions (_menu_actions->items());
578
579         {
580                 Menu_Helpers::MenuElem elem = Gtk::Menu_Helpers::MenuElem(_("New"),
581                                 sigc::mem_fun(*this, &LuaWindow::new_script));
582                 items_scratch.push_back(elem);
583         }
584
585         items_scratch.push_back(SeparatorElem());
586
587         for (ScriptBufferList::const_iterator i = script_buffers.begin (); i != script_buffers.end (); ++i) {
588                 std::string name;
589                 if ((*i)->flags & Buffer_ReadOnly) {
590                         name = "[R] " + (*i)->name;
591                 } else {
592                         name = (*i)->name;
593                 }
594                 Menu_Helpers::MenuElem elem = Gtk::Menu_Helpers::MenuElem(name,
595                                 sigc::bind(sigc::mem_fun(*this, &LuaWindow::script_selection_changed), (*i), false));
596
597                 if ((*i)->flags & Buffer_Scratch) {
598                         items_scratch.push_back(elem);
599                 }
600                 else if ((*i)->type == LuaScriptInfo::EditorAction) {
601                                 items_actions.push_back(elem);
602                 }
603                 else if ((*i)->type == LuaScriptInfo::Snippet) {
604                                 items_snippet.push_back(elem);
605                 }
606         }
607
608         script_select.clear_items ();
609         script_select.AddMenuElem (Menu_Helpers::MenuElem ("Scratch", *_menu_scratch));
610         script_select.AddMenuElem (Menu_Helpers::MenuElem ("Snippets", *_menu_snippet));
611         script_select.AddMenuElem (Menu_Helpers::MenuElem ("Actions", *_menu_actions));
612 }
613
614 void
615 LuaWindow::script_selection_changed (ScriptBufferPtr n, bool force)
616 {
617         if (n == _current_buffer && !force) {
618                 return;
619         }
620
621         Glib::RefPtr<Gtk::TextBuffer> tb (entry.get_buffer());
622
623         if (_current_buffer->flags & Buffer_Valid) {
624                 _current_buffer->script = tb->get_text();
625         }
626
627         if (!(n->flags & Buffer_Valid)) {
628                 if (!n->load()) {
629                         append_text ("! Failed to load buffer.");
630                 }
631         }
632
633         if (n->flags & Buffer_Valid) {
634                 _current_buffer = n;
635                 _script_changed_connection.block ();
636                 tb->set_text (n->script);
637                 _script_changed_connection.unblock ();
638         } else {
639                 append_text ("! Failed to switch buffer.");
640         }
641         update_gui_state ();
642 }
643
644 void
645 LuaWindow::update_gui_state ()
646 {
647         const ScriptBuffer & sb (*_current_buffer);
648         std::string name;
649         if (sb.flags & Buffer_Scratch) {
650                 name = string_compose (_("Scratch Buffer %1"), sb.name);
651         } else if (sb.type == LuaScriptInfo::EditorAction) {
652                 name = string_compose (_("Action: '%1'"), sb.name);
653         } else if (sb.type == LuaScriptInfo::Snippet) {
654                 name = string_compose (_("Snippet: %1"), sb.name);
655         } else {
656                 cerr << "Invalid Script type\n";
657                 assert (0);
658                 return;
659         }
660         if (sb.flags & Buffer_Dirty) {
661                 name += " *";
662         }
663         script_select.set_text(name);
664
665         if (sb.flags & Buffer_ReadOnly) {
666                 _btn_save.set_text (_("Save as"));
667         } else {
668                 _btn_save.set_text (_("Save"));
669         }
670         _btn_save.set_sensitive (sb.flags & Buffer_Dirty);
671         _btn_delete.set_sensitive (sb.flags & Buffer_Scratch || ((sb.flags & (Buffer_ReadOnly | Buffer_HasFile)) == Buffer_HasFile));
672         _btn_revert.set_sensitive ((sb.flags & Buffer_Dirty) && (sb.flags & Buffer_HasFile));
673 }
674
675 void
676 LuaWindow::script_changed () {
677         if (_current_buffer->flags & Buffer_Dirty) {
678                 return;
679         }
680         _current_buffer->flags |= Buffer_Dirty;
681         update_gui_state ();
682 }
683
684 LuaWindow::ScriptBuffer::ScriptBuffer (const std::string& n)
685         : name (n)
686         , flags (Buffer_Scratch | Buffer_Valid)
687 {
688         script =
689                 "---- this header is (only) required to save the script\n"
690                 "-- ardour { [\"type\"] = \"Snippet\", name = \"\" }\n"
691                 "-- function factory () return function () -- -- end end\n";
692 }
693
694 LuaWindow::ScriptBuffer::ScriptBuffer (LuaScriptInfoPtr p)
695         : name (p->name)
696         , path (p->path)
697         , flags (Buffer_HasFile)
698         , type (p->type)
699 {
700         if (!PBD::exists_and_writable (path)) {
701                 flags |= Buffer_ReadOnly;
702         }
703         if (path.find (user_config_directory ()) != 0) {
704                 // mark non-user scripts as read-only
705                 flags |= Buffer_ReadOnly;
706         }
707 }
708
709 #if 0
710 LuaWindow::ScriptBuffer::ScriptBuffer (const ScriptBuffer& other)
711         : script (other.script)
712         , name (other.name)
713         , path (other.path)
714         , flags (other.flags)
715         , type (other.type)
716 {
717 }
718 #endif
719
720 LuaWindow::ScriptBuffer::~ScriptBuffer ()
721 {
722 }
723
724 bool
725 LuaWindow::ScriptBuffer::load ()
726 {
727         assert (!(flags & Buffer_Valid));
728         if (!(flags & Buffer_HasFile)) return false;
729         try {
730                 script = Glib::file_get_contents (path);
731                 flags |= Buffer_Valid;
732                 flags &= BufferFlags(~Buffer_Dirty);
733         } catch (Glib::FileError e) {
734                 return false;
735         }
736         return true;
737 }