initial pass at a missing file dialog and "relocatable" source files. lots more to...
[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
23 #include "ardour/session.h"
24
25 #include "missing_file_dialog.h"
26
27 using namespace Gtk;
28 using namespace std;
29 using namespace ARDOUR;
30 using namespace PBD;
31
32 MissingFileDialog::MissingFileDialog (Session* s, const std::string& path, DataType type)
33         : ArdourDialog (_("Missing File!"), true, false)
34         , filetype (type)
35         , chooser (FILE_CHOOSER_ACTION_SELECT_FOLDER)
36         , use_chosen (_("Add chosen folder to search path, and try again"))
37         , choice_group (use_chosen.get_group())
38         , use_chosen_and_no_more_questions (choice_group, _("Add chosen folder to search path, try again but don't ask me again"), false)
39         , stop_loading_button (choice_group, _("Stop loading this session"), false)
40         , all_missing_ok (choice_group, _("This and all other missing files are OK"), false)
41         , this_missing_ok (choice_group, _("This missing file is OK"), false)
42 {
43         set_session (s);
44
45         add_button (_("Done"), RESPONSE_OK);
46
47         string typestr;
48
49         switch (type) {
50         case DataType::AUDIO:
51                 typestr = _("An audio");
52                 break;
53         case DataType::MIDI:
54                 typestr = _("A MIDI");
55                 break;
56         }
57
58         string dirstr;
59
60         dirstr = s->source_search_path (type);
61         replace_all (dirstr, ":", "\n");
62
63         msg.set_markup (string_compose (_("%1 file (\"%2\") cannot be found.\n\n\
64 Currently, Ardour has searched in the following folders for this file:\n\n\
65 <tt>%3</tt>\n\n\
66 The following options are available:"), typestr, path, dirstr));
67
68         VBox* button_packer_box = manage (new VBox);
69
70         button_packer_box->set_spacing (6);
71         button_packer_box->set_border_width (12);
72
73         button_packer_box->pack_start (use_chosen, false, false);
74         button_packer_box->pack_start (use_chosen_and_no_more_questions, false, false);
75         button_packer_box->pack_start (this_missing_ok, false, false);
76         button_packer_box->pack_start (all_missing_ok, false, false);
77         button_packer_box->pack_start (stop_loading_button, false, false);
78
79         button_packer_box->show_all ();
80         
81         get_vbox()->set_spacing (6);
82         get_vbox()->set_border_width (12);
83         get_vbox()->set_homogeneous (false);
84
85         get_vbox()->pack_start (msg, false, false);
86
87         HBox* hbox = manage (new HBox);
88         hbox->pack_start (*button_packer_box, false, true);
89         hbox->show ();
90
91         get_vbox()->pack_start (*hbox, false, false);
92         get_vbox()->pack_start (chooser, true, true);
93
94         msg.show ();
95         chooser.set_size_request (-1, 300);
96         chooser.show ();
97         chooser.set_current_folder (Glib::get_home_dir());
98         chooser.set_create_folders (false);
99 }
100
101 void
102 MissingFileDialog::add_chosen ()
103 {
104         string str;
105         string newdir;
106         vector<string> dirs;
107
108         switch (filetype) {
109         case DataType::AUDIO:
110                 str = _session->config.get_audio_search_path();
111                 break;
112         case DataType::MIDI:
113                 str = _session->config.get_midi_search_path();
114                 break;
115         }
116
117         split (str, dirs, ':');
118
119         newdir = chooser.get_filename ();
120
121         for (vector<string>::iterator d = dirs.begin(); d != dirs.end(); d++) {
122                 if (*d == newdir) {
123                         return;
124                 }
125         }
126
127         if (!str.empty()) {
128                 str += ':';
129         } 
130         
131         str += newdir;
132
133         switch (filetype) {
134         case DataType::AUDIO:
135                 _session->config.set_audio_search_path (str);
136                 break;
137         case DataType::MIDI:
138                 _session->config.set_midi_search_path (str);
139                 break;
140         }
141 }
142
143 int
144 MissingFileDialog::get_action ()
145 {
146
147         if (use_chosen.get_active ()) {
148                 add_chosen ();
149                 return 0;
150         }
151
152         if (use_chosen_and_no_more_questions.get_active()) {
153                 add_chosen ();
154                 return 2;
155         }
156
157         if (this_missing_ok.get_active()) {
158                 return -1;
159         }
160
161         if (all_missing_ok.get_active ()) {
162                 return 3;
163         }
164
165         return 1;
166 }