additional API to make it nicer to get an existing action group
[ardour.git] / gtk2_ardour / missing_file_dialog.cc
1 /*
2     Copyright (C) 2010 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include "pbd/compose.h"
20 #include "pbd/replace_all.h"
21 #include "pbd/strsplit.h"
22 #include "pbd/search_path.h"
23
24 #include "ardour/session.h"
25 #include "gtkmm2ext/utils.h"
26
27 #include "missing_file_dialog.h"
28 #include "pbd/i18n.h"
29
30 using namespace Gtk;
31 using namespace std;
32 using namespace ARDOUR;
33 using namespace PBD;
34
35 MissingFileDialog::MissingFileDialog (Session* s, const std::string& path, DataType type)
36         : ArdourDialog (_("Missing File"), true, false)
37         , filetype (type)
38         , is_absolute_path (Glib::path_is_absolute (path))
39         , chooser (_("Select a folder to search"), FILE_CHOOSER_ACTION_SELECT_FOLDER)
40         , use_chosen (_("Add chosen folder to search path, and try again"))
41         , choice_group (use_chosen.get_group())
42         , stop_loading_button (choice_group, _("Stop loading this session"), false)
43         , all_missing_ok (choice_group, _("Skip all missing files"), false)
44         , this_missing_ok (choice_group, _("Skip this file"), false)
45 {
46         /* This dialog is always shown programatically. Center the window.*/
47         set_position (Gtk::WIN_POS_CENTER);
48
49         set_session (s);
50
51         add_button (_("Done"), RESPONSE_OK);
52         set_default_response (RESPONSE_OK);
53
54         string typestr;
55
56         switch (type) {
57                 case DataType::AUDIO:
58                         typestr = _("audio");
59                         break;
60                 case DataType::MIDI:
61                         typestr = _("MIDI");
62                         break;
63         }
64
65         vector<string> source_dirs = s->source_search_path (type);
66         vector<string>::iterator i = source_dirs.begin();
67         ostringstream oss;
68         oss << *i << endl;
69
70         while (++i != source_dirs.end()) {
71                 oss << *i << endl;
72         }
73
74         msg.set_justify (JUSTIFY_LEFT);
75         msg.set_markup (string_compose (_("%1 cannot find the %2 file\n\n<i>%3</i>\n\nin any of these folders:\n\n\
76                                         <tt>%4</tt>\n\n"), PROGRAM_NAME, typestr, Gtkmm2ext::markup_escape_text (path), Gtkmm2ext::markup_escape_text (oss.str())));
77
78         HBox* hbox = manage (new HBox);
79         hbox->pack_start (msg, false, true);
80         hbox->show ();
81
82         get_vbox()->pack_start (*hbox, false, false);
83
84         VBox* button_packer_box = manage (new VBox);
85
86         button_packer_box->set_spacing (6);
87         button_packer_box->set_border_width (12);
88
89         button_packer_box->pack_start (use_chosen, false, false);
90         button_packer_box->pack_start (this_missing_ok, false, false);
91         button_packer_box->pack_start (all_missing_ok, false, false);
92         button_packer_box->pack_start (stop_loading_button, false, false);
93
94         button_packer_box->show_all ();
95
96         get_vbox()->set_spacing (6);
97         get_vbox()->set_border_width (25);
98         get_vbox()->set_homogeneous (false);
99
100
101         hbox = manage (new HBox);
102         hbox->pack_start (*button_packer_box, false, true);
103         hbox->show ();
104
105         get_vbox()->pack_start (*hbox, false, false);
106
107         hbox = manage (new HBox);
108         Label* label = manage (new Label);
109         label->set_text (_("Click to choose an additional folder"));
110
111         hbox->set_spacing (6);
112         hbox->set_border_width (12);
113         hbox->pack_start (*label, false, false);
114         hbox->pack_start (chooser, true, true);
115         hbox->show_all ();
116
117         get_vbox()->pack_start (*hbox, true, true);
118
119         msg.show ();
120
121         Gtkmm2ext::add_volume_shortcuts (chooser);
122         chooser.set_current_folder (Glib::get_home_dir());
123         chooser.set_create_folders (false);
124 }
125
126 void
127 MissingFileDialog::set_absolute ()
128 {
129         _session->set_missing_file_replacement (chooser.get_filename ());
130 }
131
132 void
133 MissingFileDialog::add_chosen ()
134 {
135         string str;
136         string newdir;
137         vector<string> dirs;
138
139         switch (filetype) {
140                 case DataType::AUDIO:
141                         str = _session->config.get_audio_search_path();
142                         break;
143                 case DataType::MIDI:
144                         str = _session->config.get_midi_search_path();
145                         break;
146         }
147
148         split (str, dirs, G_SEARCHPATH_SEPARATOR);
149
150         newdir = chooser.get_filename ();
151
152         for (vector<string>::iterator d = dirs.begin(); d != dirs.end(); d++) {
153                 if (*d == newdir) {
154                         return;
155                 }
156         }
157
158         if (!str.empty()) {
159                 str += G_SEARCHPATH_SEPARATOR;
160         }
161
162         str += newdir;
163
164         switch (filetype) {
165                 case DataType::AUDIO:
166                         _session->config.set_audio_search_path (str);
167                         break;
168                 case DataType::MIDI:
169                         _session->config.set_midi_search_path (str);
170                         break;
171         }
172 }
173
174 int
175 MissingFileDialog::get_action ()
176 {
177         if (use_chosen.get_active ()) {
178                 if (is_absolute_path) {
179                         set_absolute ();
180                 } else {
181                         add_chosen ();
182                 }
183                 return 0;
184         }
185
186         if (this_missing_ok.get_active()) {
187                 return -1;
188         }
189
190         if (all_missing_ok.get_active ()) {
191                 return 3;
192         }
193
194         return 1;
195 }