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