Remove internal edit mode and add "content" tool.
[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 PLATFORM_WINDOWS
33 #include <windows.h>
34 #include <shlobj.h> // CSIDL_*
35 #include "pbd/windows_special_dirs.h"
36 #endif
37
38 #ifdef interface
39 #undef interface
40 #endif
41
42 #include "video_server_dialog.h"
43 #include "utils_videotl.h"
44 #include "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_browse_button.set_name ("PaddedButton");
83         path_entry.set_text("/usr/bin/harvid");
84         docroot_entry.set_width_chars(38);
85         docroot_entry.set_text(Config->get_video_server_docroot());
86         docroot_browse_button.set_name ("PaddedButton");
87
88         listenaddr_combo.set_name ("PaddedButton");
89 #ifndef __APPLE__
90         /* Note: on OSX icsd is not able to bind to IPv4 localhost */
91         listenaddr_combo.append_text("127.0.0.1");
92 #endif
93         listenaddr_combo.append_text("0.0.0.0");
94         listenaddr_combo.set_active(0);
95
96 #ifdef PLATFORM_WINDOWS
97         HKEY key;
98         DWORD size = PATH_MAX;
99         char tmp[PATH_MAX+1];
100         const char *program_files = PBD::get_win_special_folder (CSIDL_PROGRAM_FILES);
101 #endif
102
103         std::string icsd_file_path;
104         if (find_file (PBD::Searchpath(Glib::getenv("PATH")), X_("harvid"), icsd_file_path)) {
105                 path_entry.set_text(icsd_file_path);
106         }
107 #ifdef PLATFORM_WINDOWS
108         else if ( (ERROR_SUCCESS == RegOpenKeyExA (HKEY_LOCAL_MACHINE, "Software\\RSS\\harvid", 0, KEY_READ, &key))
109                         &&  (ERROR_SUCCESS == RegQueryValueExA (key, "Install_Dir", 0, NULL, reinterpret_cast<LPBYTE>(tmp), &size))
110                         )
111         {
112                 path_entry.set_text(g_build_filename(Glib::locale_to_utf8(tmp).c_str(), "harvid.exe", NULL));
113         }
114         else if ( (ERROR_SUCCESS == RegOpenKeyExA (HKEY_LOCAL_MACHINE, "Software\\RSS\\harvid", 0, KEY_READ | KEY_WOW64_32KEY, &key))
115                         &&  (ERROR_SUCCESS == RegQueryValueExA (key, "Install_Dir", 0, NULL, reinterpret_cast<LPBYTE>(tmp), &size))
116                         )
117         {
118                 path_entry.set_text(g_build_filename(Glib::locale_to_utf8(tmp).c_str(), "harvid.exe", NULL));
119         }
120         else if (program_files && Glib::file_test(g_build_filename(program_files, "harvid", "harvid.exe", NULL), Glib::FILE_TEST_EXISTS))
121         {
122                 path_entry.set_text(g_build_filename(program_files, "harvid", "harvid.exe", NULL));
123         }
124 #endif
125         /* generic fallbacks to try */
126         else if (Glib::file_test(X_("C:\\Program Files\\harvid\\harvid.exe"), Glib::FILE_TEST_EXISTS)) {
127                 path_entry.set_text(X_("C:\\Program Files\\harvid\\harvid.exe"));
128         }
129         else {
130                 PBD::warning <<
131                         string_compose(
132                                         _("The external video server 'harvid' can not be found.\n"
133                                                 "The tool is included with the %1 releases from ardour.org, "
134                                                 "alternatively you can download it from http://x42.github.com/harvid/ "
135                                                 "or acquire it from your distribution.\n"
136                                                 "\n"
137                                                 "see also http://manual.ardour.org/video-timeline/setup/"
138                                          ), PROGRAM_NAME)
139                         << endmsg;
140         }
141
142
143         if (docroot_entry.get_text().empty()) {
144           std::string docroot =  Glib::path_get_dirname(_session->session_directory().root_path());
145           if ((docroot.empty() || docroot.at(docroot.length()-1) != '/')) { docroot += "/"; }
146                 docroot_entry.set_text(docroot);
147         }
148
149         path_hbox->pack_start (path_label, false, false, 3);
150         path_hbox->pack_start (path_entry, true, true, 3);
151         path_hbox->pack_start (path_browse_button, false, false, 3);
152
153         docroot_hbox->pack_start (docroot_label, false, false, 3);
154         docroot_hbox->pack_start (docroot_entry, true, true, 3);
155         docroot_hbox->pack_start (docroot_browse_button, false, false, 3);
156
157         l = manage (new Label (_("<b>Options</b>"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
158         l->set_use_markup ();
159         options_box->pack_start (*l, false, true, 4);
160
161         Table* t = manage (new Table (2, 3));
162         t->set_spacings (4);
163         options_box->pack_start (*t, true, true, 4);
164
165         l = manage (new Label (_("Listen Address:")));
166         l->set_alignment (0, 0.5);
167         t->attach (*l, 0, 1, 0, 1, FILL);
168         t->attach (listenaddr_combo, 1, 2, 0, 1);
169
170         l = manage (new Label (_("Listen Port:")));
171         l->set_alignment (0, 0.5);
172         t->attach (*l, 0, 1, 1, 2, FILL);
173         t->attach (listenport_spinner, 1, 2, 1, 2);
174
175         l = manage (new Label (_("Cache Size:")));
176         l->set_alignment (0, 0.5);
177         t->attach (*l, 0, 1, 2, 3, FILL);
178         t->attach (cachesize_spinner, 1, 2, 2, 3);
179
180         l = manage (new Label (string_compose(
181                                         _("%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)
182                                 , Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
183         l->set_max_width_chars(80);
184         l->set_line_wrap();
185         vbox->pack_start (*l, true, true, 4);
186         vbox->pack_start (*path_hbox, false, false);
187         if (Config->get_video_advanced_setup()){
188                 vbox->pack_start (*docroot_hbox, false, false);
189         } else {
190                 docroot_entry.set_text(X_("/"));
191                 listenport_spinner.set_sensitive(false);
192         }
193         vbox->pack_start (*options_box, false, true);
194
195         get_vbox()->set_spacing (4);
196         get_vbox()->pack_start (*vbox, false, false);
197         get_vbox()->pack_start (showagain_checkbox, false, false);
198         showagain_checkbox.set_active(!Config->get_show_video_server_dialog());
199
200         path_browse_button.signal_clicked().connect (sigc::mem_fun (*this, &VideoServerDialog::open_path_dialog));
201         docroot_browse_button.signal_clicked().connect (sigc::mem_fun (*this, &VideoServerDialog::open_docroot_dialog));
202
203         show_all_children ();
204         add_button (Stock::CANCEL, RESPONSE_CANCEL);
205         add_button (Stock::EXECUTE, RESPONSE_ACCEPT);
206 }
207
208 VideoServerDialog::~VideoServerDialog ()
209 {
210 }
211
212 void
213 VideoServerDialog::on_show ()
214 {
215         Dialog::on_show ();
216 }
217
218 void
219 VideoServerDialog::open_path_dialog ()
220 {
221         Gtk::FileChooserDialog dialog(_("Set Video Server Executable"), Gtk::FILE_CHOOSER_ACTION_OPEN);
222         dialog.set_filename (path_entry.get_text());
223
224         dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
225         dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
226
227         int result = dialog.run();
228
229         if (result == Gtk::RESPONSE_OK) {
230                 std::string filename = dialog.get_filename();
231
232                 if (filename.length()) {
233                         path_entry.set_text (filename);
234                 }
235         }
236 }
237
238 void
239 VideoServerDialog::open_docroot_dialog ()
240 {
241         Gtk::FileChooserDialog dialog(_("Server docroot"), Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);
242         dialog.set_filename (docroot_entry.get_text());
243
244         dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
245         dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
246
247         int result = dialog.run();
248
249         if (result == Gtk::RESPONSE_OK) {
250                 std::string dirname = dialog.get_filename();
251
252                 if (dirname.empty() || dirname.at(dirname.length()-1) != G_DIR_SEPARATOR) {
253                         dirname += "/";
254                 }
255
256                 if (dirname.length()) {
257                         docroot_entry.set_text (dirname);
258                 }
259         }
260 }
261
262 std::string
263 VideoServerDialog::get_docroot () {
264         return docroot_entry.get_text();
265 }