[Summary]

Improve logic of replacing session.
Improve delete unnecessary files and directories on replace session.

[Reviewed] GZharun

[git-p4: depot-paths = "//Abdaw/dev_main/tracks/": change = 465616]
This commit is contained in:
Nikolay Polyanovskii 2014-06-05 04:54:47 -05:00 committed by Paul Davis
parent 0ea0fe893d
commit 2ee481e8df
5 changed files with 189 additions and 145 deletions

View file

@ -83,6 +83,7 @@
#include "ardour/slave.h"
#include "ardour/system_exec.h"
#include "ardour/directory_names.h"
#include "ardour/filename_extensions.h"
#include "dbg_msg.h"
#ifdef WINDOWS_VST_SUPPORT
@ -2670,6 +2671,105 @@ ARDOUR_UI::idle_load (const std::string& path)
}
}
namespace
{
void run_message_dialog(std::string message)
{
MessageDialog msg (message.c_str(), false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true);
msg.set_keep_above(true);
msg.set_position (Gtk::WIN_POS_MOUSE);
msg.run();
}
void init_suffices(std::vector<std::string>& suffices)
{
suffices.push_back(template_suffix);
suffices.push_back(statefile_suffix);
suffices.push_back(pending_suffix);
suffices.push_back(peakfile_suffix);
suffices.push_back(backup_suffix);
suffices.push_back(temp_suffix);
suffices.push_back(history_suffix);
suffices.push_back(export_preset_suffix);
suffices.push_back(export_format_suffix);
suffices.push_back("instant.xml");
}
void init_directories(std::vector<std::string>& directories)
{
directories.push_back(string(peak_dir_name));
directories.push_back(string(dead_dir_name));
directories.push_back(string(export_dir_name));
directories.push_back(string(analysis_dir_name));
directories.push_back(string(plugins_dir_name));
directories.push_back(string(externals_dir_name));
directories.push_back(string(".DS_Store"));
}
bool is_tracks_file(std::string full_file_name)
{
std::vector<std::string> suffices;
init_suffices(suffices);
for(int i = 0; i < suffices.size(); i++)
{
int pos = full_file_name.rfind(suffices[i]);
// if suffix locates at the end of the full_file_name
if( pos == full_file_name.size() - suffices[i].size() )
return true;
}
return false;
}
/*@directory_name for example is "analysis", "example", etc. It is not a full path. */
bool is_tracks_directory(std::string directory_name)
{
std::vector<std::string> directories;
init_directories(directories);
return std::find(directories.begin(), directories.end(), directory_name) != directories.end();
}
}
// return true if session was successfully deleted
bool
ARDOUR_UI::delete_session_files(std::string session_path)
{
GDir *dir;
GError *error;
const gchar *file_name;
dir = g_dir_open(session_path.c_str(), 0, &error);
bool result = true;
while ((file_name = g_dir_read_name(dir)))
{
string object_name = string(file_name);
string full_object_name = Glib::build_filename( session_path, string(file_name) );
bool is_directory = Glib::file_test(full_object_name, Glib::FileTest (G_FILE_TEST_IS_DIR));
if( is_directory && is_tracks_directory(file_name) )
{
result = result && delete_session_files(full_object_name);
g_remove(full_object_name.c_str());
} else {
if( !is_directory && is_tracks_file(full_object_name) )
if( g_remove(full_object_name.c_str()) == -1 )
{
run_message_dialog("Can't remove file: " + full_object_name);
result = false;
}
}
}
return result;
}
/** @param quit_on_cancel true if exit() should be called if the user clicks `cancel' in the new session dialog */
int
ARDOUR_UI::get_session_parameters (bool quit_on_cancel, bool should_be_new, string load_template)
@ -2677,12 +2777,13 @@ ARDOUR_UI::get_session_parameters (bool quit_on_cancel, bool should_be_new, stri
string session_name;
string session_path;
string template_name;
string full_session_name;
int ret = -1;
bool likely_new = false;
bool cancel_not_quit;
/* deal with any existing DIRTY session now, rather than later. don't
* treat a non-dirty session this way, so that it stays visible
* treat a non-dirty session this way, so that it stays visible
* as we bring up the new session dialog.
*/
@ -2769,10 +2870,10 @@ ARDOUR_UI::get_session_parameters (bool quit_on_cancel, bool should_be_new, stri
/* if we run the startup dialog again, offer more than just "new session" */
should_be_new = false;
session_name = session_dialog.session_name (likely_new);
full_session_name = session_name = session_dialog.session_name (likely_new);
session_path = session_dialog.session_folder ();
if (nsm) {
likely_new = true;
}
@ -2786,6 +2887,7 @@ ARDOUR_UI::get_session_parameters (bool quit_on_cancel, bool should_be_new, stri
/* this shouldn't happen, but we catch it just in case it does */
if (session_name.empty()) {
run_message_dialog("Session name is invalid");
continue;
}
@ -2825,108 +2927,36 @@ ARDOUR_UI::get_session_parameters (bool quit_on_cancel, bool should_be_new, stri
}
}
if (Glib::file_test (session_path, Glib::FileTest (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))) {
if ( Glib::file_test (session_path, Glib::FileTest (G_FILE_TEST_EXISTS)) ) {
if (likely_new && !nsm) {
std::string existing = Glib::build_filename (session_path, session_name);
bool do_not_create_session = false;
GDir *dir;
GError *error;
const gchar *file_name;
dir = g_dir_open(session_path.c_str(), 0, &error);
// Replace session only if file with extension '.ardour' was chosen
string suffix = string(statefile_suffix);
while ((file_name = g_dir_read_name(dir)))
// if existed folder was choosen
if( Glib::file_test (full_session_name, Glib::FileTest (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) )
{
string ff = string(file_name);
string full_file_name = g_build_filename(session_path.c_str(), file_name);
string str_file_name = string(file_name);
string interchange_debug = string(ARDOUR::interchange_dir_name);
if( (g_file_test (full_file_name.c_str(), GFileTest (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))) &&
(str_file_name == interchange_debug) )
{
cout<<endl<<full_file_name<<" WAS NOT deleted \n\n"<<flush;
continue;
}
if( g_file_test (full_file_name.c_str(), GFileTest (G_FILE_TEST_EXISTS)) &&
string(file_name) != string(ARDOUR::interchange_dir_name) )
{
try {
if( g_remove(full_file_name.c_str() ) >=0 )
cout<<full_file_name<<" file was deleted \n"<<flush;
else
{
cout<<full_file_name<<" file was not deleted (ELSE) \n"<<flush;
string message = "Can not replace session. File: file " + full_file_name + " can not be deleted";
MessageDialog msg (message.c_str(),
false,
Gtk::MESSAGE_WARNING,
Gtk::BUTTONS_OK,
true);
msg.set_position (Gtk::WIN_POS_MOUSE);
msg.run();
do_not_create_session = true;
break;
}
} catch (...) {
cout<<full_file_name<<" file was not deleted (CATCH) \n"<<flush;
string message = "Can not replace session. File: file " + full_file_name + " can not be deleted";
MessageDialog msg (message.c_str(),
false,
Gtk::MESSAGE_WARNING,
Gtk::BUTTONS_OK,
true);
msg.set_position (Gtk::WIN_POS_MOUSE);
msg.run();
do_not_create_session = true;
break;
}
/*
// file is a directory
if( (Glib::file_test (full_file_name, Glib::FileTest (G_FILE_TEST_IS_DIR))) )
{
try {
if( g_rmdir(full_file_name.c_str())>=0 )
cout<<full_file_name<<" directory was deleted \n"<<flush;
else
cout<<full_file_name<<" directory was not deleted \n"<<flush;
} catch (...) {
cerr<<"ERROR :: Can not delete directroy: "<<file_name<<endl<<flush;
}
} else {
// file is not a directory
try {
if(unlink(full_file_name.c_str())>=0)
cout<<full_file_name<<" file was deleted"<<endl<<flush;
else
cout<<full_file_name<<" file was not deleted"<<endl<<flush;
} catch (...) {
cerr<<"ERROR :: Can not delete file: "<<file_name<<endl<<flush;
}
}
*/
}
run_message_dialog("Can not replace session. Folder " + session_path + " allready exists. If you need to replace session please choose file " + full_session_name + suffix);
continue;
}
int pos = full_session_name.rfind(suffix);
if( do_not_create_session )
// if not *.ardour file was choosen
if( !(pos == full_session_name.size() - suffix.size()) )
{
run_message_dialog("Invalid file name. In order to replace session select *" + suffix + " projectfile");
continue;
}
// .ardour file was choosen. Replace session: delete existing session
if( !delete_session_files(session_path) )
continue;
_session_is_new = true;

View file

@ -340,6 +340,8 @@ class ARDOUR_UI : public Gtkmm2ext::UI, public ARDOUR::SessionHandlePtr
void setup_transport ();
void setup_transport_trx ();
void setup_clock ();
bool delete_session_files(std::string session_path);
static ARDOUR_UI *theArdourUI;

View file

@ -43,6 +43,10 @@ namespace ARDOUR {
LIBARDOUR_API extern const char* const user_config_dir_name;
LIBARDOUR_API extern const char* const panner_dir_name;
LIBARDOUR_API extern const char* const backend_dir_name;
LIBARDOUR_API extern const char* const automation_dir_name;
LIBARDOUR_API extern const char* const analysis_dir_name;
LIBARDOUR_API extern const char* const plugins_dir_name;
LIBARDOUR_API extern const char* const externals_dir_name;
};

View file

@ -39,6 +39,10 @@ const char* const surfaces_dir_name = X_("surfaces");
const char* const ladspa_dir_name = X_("ladspa");
const char* const panner_dir_name = X_("panners");
const char* const backend_dir_name = X_("backends");
const char* const automation_dir_name = X_("automation");
const char* const analysis_dir_name = X_("analysis");
const char* const plugins_dir_name = X_("plugins");
const char* const externals_dir_name = X_("externals");
char config_dir_name[] = X_(PROGRAM_NAME "3");
#if defined (__APPLE__) || defined (PLATFORM_WINDOWS)

View file

@ -127,19 +127,6 @@ using namespace std;
using namespace ARDOUR;
using namespace PBD;
namespace
{
string
get_session_projectfile_name(string session_path)
{
string s0 = session_path;
string s1 = Glib::path_get_dirname (s0);
s0.erase(0, s1.size());
return session_path + s0 + ".ardour";
}
}
void
Session::pre_engine_init (string fullpath)
{
@ -160,7 +147,10 @@ Session::pre_engine_init (string fullpath)
/* is it new ? */
_is_new = !Glib::file_test ( get_session_projectfile_name(fullpath), Glib::FileTest (G_FILE_TEST_EXISTS));
string full_session_name = Glib::build_filename( fullpath, _name );
full_session_name += statefile_suffix;
_is_new = !Glib::file_test ( full_session_name, Glib::FileTest (G_FILE_TEST_EXISTS));
/* finish initialization that can't be done in a normal C++ constructor
definition.
@ -421,15 +411,17 @@ Session::ensure_subdirs ()
dir = session_directory().peak_path();
if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
error << string_compose(_("Session: cannot create session peakfile folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
return -1;
}
// if directory allready exists do not create it again
if ( !Glib::file_test (dir.c_str(), Glib::FileTest (G_FILE_TEST_EXISTS))) {
if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
error << string_compose(_("Session: cannot create session peakfile folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
return -1;
}
}
dir = session_directory().sound_path();
// if directory 'interchange' allready exists do not create it again
if ( !Glib::file_test (dir.c_str(), Glib::FileTest (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))) {
if ( !Glib::file_test (dir.c_str(), Glib::FileTest (G_FILE_TEST_EXISTS))) {
if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
error << string_compose(_("Session: cannot create session sounds dir \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
return -1;
@ -438,45 +430,57 @@ Session::ensure_subdirs ()
dir = session_directory().midi_path();
if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
error << string_compose(_("Session: cannot create session midi dir \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
return -1;
if ( !Glib::file_test (dir.c_str(), Glib::FileTest (G_FILE_TEST_EXISTS))) {
if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
error << string_compose(_("Session: cannot create session midi dir \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
return -1;
}
}
dir = session_directory().dead_path();
if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
error << string_compose(_("Session: cannot create session dead sounds folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
return -1;
}
if ( !Glib::file_test (dir.c_str(), Glib::FileTest (G_FILE_TEST_EXISTS))) {
if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
error << string_compose(_("Session: cannot create session dead sounds folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
return -1;
}
}
dir = session_directory().export_path();
if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
error << string_compose(_("Session: cannot create session export folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
return -1;
}
if ( !Glib::file_test (dir.c_str(), Glib::FileTest (G_FILE_TEST_EXISTS))) {
if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
error << string_compose(_("Session: cannot create session export folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
return -1;
}
}
dir = analysis_dir ();
if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
error << string_compose(_("Session: cannot create session analysis folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
return -1;
}
if ( !Glib::file_test (dir.c_str(), Glib::FileTest (G_FILE_TEST_EXISTS))) {
if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
error << string_compose(_("Session: cannot create session analysis folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
return -1;
}
}
dir = plugins_dir ();
if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
error << string_compose(_("Session: cannot create session plugins folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
return -1;
}
if ( !Glib::file_test (dir.c_str(), Glib::FileTest (G_FILE_TEST_EXISTS))) {
if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
error << string_compose(_("Session: cannot create session plugins folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
return -1;
}
}
dir = externals_dir ();
if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
error << string_compose(_("Session: cannot create session externals folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
return -1;
}
if ( !Glib::file_test (dir.c_str(), Glib::FileTest (G_FILE_TEST_EXISTS))) {
if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
error << string_compose(_("Session: cannot create session externals folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
return -1;
}
}
return 0;
}
@ -2207,25 +2211,25 @@ Session::get_best_session_directory_for_new_source ()
string
Session::automation_dir () const
{
return Glib::build_filename (_path, "automation");
return Glib::build_filename (_path, automation_dir_name);
}
string
Session::analysis_dir () const
{
return Glib::build_filename (_path, "analysis");
return Glib::build_filename (_path, analysis_dir_name);
}
string
Session::plugins_dir () const
{
return Glib::build_filename (_path, "plugins");
return Glib::build_filename (_path, plugins_dir_name);
}
string
Session::externals_dir () const
{
return Glib::build_filename (_path, "externals");
return Glib::build_filename (_path, externals_dir_name);
}
int