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