replace ::cast_dynamic() with relevant ActionManager::get_*_action() calls
[ardour.git] / gtk2_ardour / video_server_dialog.cc
1 /*
2     Copyright (C) 2010 Paul Davis
3     Author: Robin Gareus <robin@gareus.org>
4
5     This program 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.
9
10     This program 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.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20 #include <cstdio>
21 #include <cmath>
22
23 #include <sigc++/bind.h>
24
25 #include <gtkmm/box.h>
26 #include <gtkmm/filechooserdialog.h>
27 #include <gtkmm/stock.h>
28 #include <gtkmm/table.h>
29
30 #include "pbd/error.h"
31 #include "pbd/file_utils.h"
32 #include "ardour/filesystem_paths.h"
33 #include "ardour/session_directory.h"
34 #include "gtkmm2ext/utils.h"
35 #include "ardour/template_utils.h"
36 #include "ardour/session.h"
37
38 #ifdef interface
39 #undef interface
40 #endif
41
42 #include "video_server_dialog.h"
43 #include "utils_videotl.h"
44 #include "pbd/i18n.h"
45
46 #ifdef PLATFORM_WINDOWS
47 #include <windows.h>
48 #endif
49
50 using namespace Gtk;
51 using namespace std;
52 using namespace PBD;
53 using namespace ARDOUR;
54 using namespace VideoUtils;
55
56 VideoServerDialog::VideoServerDialog (Session* s)
57         : ArdourDialog (_("Launch Video Server"))
58         , path_label (_("Server Executable:"), Gtk::ALIGN_LEFT)
59         , path_browse_button (_("Browse"))
60         , docroot_label (_("Server Docroot:"), Gtk::ALIGN_LEFT)
61         , docroot_browse_button (_("Browse"))
62         , listenport_adjustment (1554, 1025, 65536, 1, 10, 0)
63         , listenport_spinner (listenport_adjustment)
64         , cachesize_adjustment (256, 32, 32768, 1, 32, 0)
65         , cachesize_spinner (cachesize_adjustment)
66         , showagain_checkbox (_("Don't show this dialog again. (Reset in Edit->Preferences)."))
67 {
68         set_session (s);
69
70         set_name ("VideoServerDialog");
71         set_modal (true);
72         set_skip_taskbar_hint (true);
73         set_resizable (false);
74
75         Gtk::Label* l;
76         VBox* vbox = manage (new VBox);
77         VBox* options_box = manage (new VBox);
78         HBox* path_hbox = manage (new HBox);
79         HBox* docroot_hbox = manage (new HBox);
80
81         path_entry.set_width_chars(38);
82         path_entry.set_text("/usr/bin/harvid");
83         docroot_entry.set_width_chars(38);
84         docroot_entry.set_text(video_get_docroot (Config));
85
86 #ifndef __APPLE__
87         /* Note: on OSX icsd is not able to bind to IPv4 localhost */
88         listenaddr_combo.append_text("127.0.0.1");
89 #endif
90         listenaddr_combo.append_text("0.0.0.0");
91         listenaddr_combo.set_active(0);
92
93         std::string harvid_exe;
94         if (ArdourVideoToolPaths::harvid_exe(harvid_exe)) {
95                 path_entry.set_text(harvid_exe);
96         } else {
97                 PBD::warning <<
98                         string_compose(
99                                         _("The external video server 'harvid' can not be found.\n"
100                                                 "The tool is included with the %1 releases from ardour.org, "
101                                                 "alternatively you can download it from http://x42.github.com/harvid/ "
102                                                 "or acquire it from your distribution.\n"
103                                                 "\n"
104                                                 "see also http://manual.ardour.org/video-timeline/setup/"
105                                          ), PROGRAM_NAME)
106                         << endmsg;
107         }
108
109 #ifdef PLATFORM_WINDOWS
110         if (VideoUtils::harvid_version >= 0x000802) {
111                 /* empty docroot -> all drive letters */
112         } else
113 #endif
114         if (docroot_entry.get_text().empty()) {
115           std::string docroot =  Glib::path_get_dirname(_session->session_directory().root_path());
116           if ((docroot.empty() || docroot.at(docroot.length()-1) != '/')) { docroot += "/"; }
117                 docroot_entry.set_text(docroot);
118         }
119
120         path_hbox->pack_start (path_label, false, false, 3);
121         path_hbox->pack_start (path_entry, true, true, 3);
122         path_hbox->pack_start (path_browse_button, false, false, 3);
123
124         docroot_hbox->pack_start (docroot_label, false, false, 3);
125         docroot_hbox->pack_start (docroot_entry, true, true, 3);
126         docroot_hbox->pack_start (docroot_browse_button, false, false, 3);
127
128         l = manage (new Label (_("<b>Options</b>"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
129         l->set_use_markup ();
130         options_box->pack_start (*l, false, true, 4);
131
132         Table* t = manage (new Table (2, 3));
133         t->set_spacings (4);
134         options_box->pack_start (*t, true, true, 4);
135
136         l = manage (new Label (_("Listen Address:")));
137         l->set_alignment (0, 0.5);
138         t->attach (*l, 0, 1, 0, 1, FILL);
139         t->attach (listenaddr_combo, 1, 2, 0, 1);
140
141         l = manage (new Label (_("Listen Port:")));
142         l->set_alignment (0, 0.5);
143         t->attach (*l, 0, 1, 1, 2, FILL);
144         t->attach (listenport_spinner, 1, 2, 1, 2);
145
146         l = manage (new Label (_("Cache Size:")));
147         l->set_alignment (0, 0.5);
148         t->attach (*l, 0, 1, 2, 3, FILL);
149         t->attach (cachesize_spinner, 1, 2, 2, 3);
150
151         l = manage (new Label (string_compose(
152                                         _("%1 relies on an external video server for the videotimeline.\nThe server configured in Edit -> Preferences -> Video is not reachable.\nDo you want %1 to launch 'harvid' on this machine?"), PROGRAM_NAME)
153                                 , Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
154         l->set_max_width_chars(80);
155         l->set_line_wrap();
156         vbox->pack_start (*l, true, true, 4);
157         vbox->pack_start (*path_hbox, false, false);
158         if (Config->get_video_advanced_setup()){
159                 vbox->pack_start (*docroot_hbox, false, false);
160         } else {
161                 listenport_spinner.set_sensitive(false);
162         }
163         vbox->pack_start (*options_box, false, true);
164
165         get_vbox()->set_spacing (4);
166         get_vbox()->pack_start (*vbox, false, false);
167         get_vbox()->pack_start (showagain_checkbox, false, false);
168         showagain_checkbox.set_active(!Config->get_show_video_server_dialog());
169
170         path_browse_button.signal_clicked().connect (sigc::mem_fun (*this, &VideoServerDialog::open_path_dialog));
171         docroot_browse_button.signal_clicked().connect (sigc::mem_fun (*this, &VideoServerDialog::open_docroot_dialog));
172
173         show_all_children ();
174         add_button (Stock::CANCEL, RESPONSE_CANCEL);
175         add_button (Stock::EXECUTE, RESPONSE_ACCEPT);
176 }
177
178 VideoServerDialog::~VideoServerDialog ()
179 {
180 }
181
182 void
183 VideoServerDialog::on_show ()
184 {
185         Dialog::on_show ();
186 }
187
188 void
189 VideoServerDialog::open_path_dialog ()
190 {
191         Gtk::FileChooserDialog dialog(_("Set Video Server Executable"), Gtk::FILE_CHOOSER_ACTION_OPEN);
192         dialog.set_filename (path_entry.get_text());
193
194         dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
195         dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
196
197         int result = dialog.run();
198
199         if (result == Gtk::RESPONSE_OK) {
200                 std::string filename = dialog.get_filename();
201
202                 if (filename.length()) {
203                         path_entry.set_text (filename);
204                 }
205         }
206 }
207
208 void
209 VideoServerDialog::open_docroot_dialog ()
210 {
211         Gtk::FileChooserDialog dialog(_("Server docroot"), Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);
212         dialog.set_filename (docroot_entry.get_text());
213
214         dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
215         dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
216
217         int result = dialog.run();
218
219         if (result == Gtk::RESPONSE_OK) {
220                 std::string dirname = dialog.get_filename();
221
222                 if (dirname.empty() || dirname.at(dirname.length()-1) != G_DIR_SEPARATOR) {
223                         dirname += "/";
224                 }
225
226                 if (dirname.length()) {
227                         docroot_entry.set_text (dirname);
228                 }
229         }
230 }
231
232 std::string
233 VideoServerDialog::get_docroot () {
234         return docroot_entry.get_text();
235 }