47e694841286f66f4702691d769d38d66e8ade16
[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         , chooser (_("Select a folder to search"), FILE_CHOOSER_ACTION_SELECT_FOLDER)
39         , use_chosen (_("Add chosen folder to search path, and try again"))
40         , choice_group (use_chosen.get_group())
41         , stop_loading_button (choice_group, _("Stop loading this session"), false)
42         , all_missing_ok (choice_group, _("Skip all missing files"), false)
43         , this_missing_ok (choice_group, _("Skip this file"), false)
44 {
45         /* This dialog is always shown programatically. Center the window.*/
46         set_position (Gtk::WIN_POS_CENTER);
47
48         set_session (s);
49
50         add_button (_("Done"), RESPONSE_OK);
51         set_default_response (RESPONSE_OK);
52
53         string typestr;
54
55         switch (type) {
56         case DataType::AUDIO:
57                 typestr = _("audio");
58                 break;
59         case DataType::MIDI:
60                 typestr = _("MIDI");
61                 break;
62         }
63
64         vector<string> source_dirs = s->source_search_path (type);
65         vector<string>::iterator i = source_dirs.begin();
66         ostringstream oss;
67         oss << *i << endl;
68
69         while (++i != source_dirs.end()) {
70                 oss << *i << endl;
71         }
72
73         msg.set_justify (JUSTIFY_LEFT);
74         msg.set_markup (string_compose (_("%1 cannot find the %2 file\n\n<i>%3</i>\n\nin any of these folders:\n\n\
75 <tt>%4</tt>\n\n"), PROGRAM_NAME, typestr, Gtkmm2ext::markup_escape_text (path), Gtkmm2ext::markup_escape_text (oss.str())));
76
77         HBox* hbox = manage (new HBox);
78         hbox->pack_start (msg, false, true);
79         hbox->show ();
80
81         get_vbox()->pack_start (*hbox, false, false);
82
83         VBox* button_packer_box = manage (new VBox);
84
85         button_packer_box->set_spacing (6);
86         button_packer_box->set_border_width (12);
87
88         button_packer_box->pack_start (use_chosen, false, false);
89         button_packer_box->pack_start (this_missing_ok, false, false);
90         button_packer_box->pack_start (all_missing_ok, false, false);
91         button_packer_box->pack_start (stop_loading_button, false, false);
92
93         button_packer_box->show_all ();
94
95         get_vbox()->set_spacing (6);
96         get_vbox()->set_border_width (25);
97         get_vbox()->set_homogeneous (false);
98
99
100         hbox = manage (new HBox);
101         hbox->pack_start (*button_packer_box, false, true);
102         hbox->show ();
103
104         get_vbox()->pack_start (*hbox, false, false);
105
106         hbox = manage (new HBox);
107         Label* label = manage (new Label);
108         label->set_text (_("Click to choose an additional folder"));
109
110         hbox->set_spacing (6);
111         hbox->set_border_width (12);
112         hbox->pack_start (*label, false, false);
113         hbox->pack_start (chooser, true, true);
114         hbox->show_all ();
115
116         get_vbox()->pack_start (*hbox, true, true);
117
118         msg.show ();
119
120         chooser.set_current_folder (Glib::get_home_dir());
121         chooser.set_create_folders (false);
122 }
123
124 void
125 MissingFileDialog::add_chosen ()
126 {
127         string str;
128         string newdir;
129         vector<string> dirs;
130
131         switch (filetype) {
132         case DataType::AUDIO:
133                 str = _session->config.get_audio_search_path();
134                 break;
135         case DataType::MIDI:
136                 str = _session->config.get_midi_search_path();
137                 break;
138         }
139
140         split (str, dirs, G_SEARCHPATH_SEPARATOR);
141
142         newdir = chooser.get_filename ();
143
144         for (vector<string>::iterator d = dirs.begin(); d != dirs.end(); d++) {
145                 if (*d == newdir) {
146                         return;
147                 }
148         }
149
150         if (!str.empty()) {
151                 str += G_SEARCHPATH_SEPARATOR;
152         }
153
154         str += newdir;
155
156         switch (filetype) {
157         case DataType::AUDIO:
158                 _session->config.set_audio_search_path (str);
159                 break;
160         case DataType::MIDI:
161                 _session->config.set_midi_search_path (str);
162                 break;
163         }
164 }
165
166 int
167 MissingFileDialog::get_action ()
168 {
169         if (use_chosen.get_active ()) {
170                 add_chosen ();
171                 return 0;
172         }
173
174         if (this_missing_ok.get_active()) {
175                 return -1;
176         }
177
178         if (all_missing_ok.get_active ()) {
179                 return 3;
180         }
181
182         return 1;
183 }