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