00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include <stdio.h>
00016 #include <limits.h>
00017 #include <sys/types.h>
00018 #include <dirent.h>
00019 #include <libgen.h>
00020 #include <stdlib.h>
00021
00022 #include <vector>
00023 #include <algorithm>
00024 #include <sstream>
00025 using std::vector;
00026 using std::string;
00027
00028 #include <gtkmm/dialog.h>
00029
00030 #include "ActionsMenu.h"
00031 #include "MainWindow.h"
00032 #include "Config.h"
00033 #include "NameUtils.h"
00034
00035 using namespace nameutils;
00036 using ASSA::TRACE;
00037
00038
00039
00040
00041
00042
00043
00044
00045 template<class T>
00046 void
00047 dump_vector (const string& info_, vector<T>& v_)
00048 {
00049 DL((TRACE,"%s\n", info_.c_str ()));
00050 vector<T>::const_iterator i = v_.begin ();
00051 unsigned int j = 0;
00052 while (i != v_.end ()) {
00053 DL ((APP,"%03d : \"%s\"\n", j++, (*i).c_str ()));
00054 i++;
00055 }
00056 DL((TRACE,"========================================\n"));
00057 }
00058
00059 void
00060 log_rename (const string& src_, const string& dest_)
00061 {
00062 DL((TRACE,"\n\tExec rename (\"%s\","
00063 "\n\t \"%s\"\n", src_.c_str (), dest_.c_str ()));
00064 }
00065
00066
00067
00068
00069 void
00070
00071 ActionsMenu::shift (Column col_, ActionsMenu::Direction d_)
00072 {
00073 trace("ActionsMenu::shift");
00074 string msg;
00075 Config& cfg = m_mw.get_config ();
00076
00077
00078
00079
00080
00081
00082 string prefix;
00083 string proj_path;
00084 string dirpath;
00085 string format;
00086 string fname;
00087 string target;
00088 string src;
00089
00090 prefix = col_==left_column ? cfg.get_A_prefix () : cfg.get_B_prefix ();
00091 proj_path = cfg.get_proj_path ();
00092 dirpath = proj_path + "/"
00093 + (col_ == left_column ? cfg.get_A_dir () : cfg.get_B_dir ());
00094
00095 fname = prefix
00096 + (left_column ? m_mw.get_A_selection () : m_mw.get_B_selection ())
00097 + ".wav";
00098
00099 DL((APP,"dirpath = \"%s\"\n", dirpath.c_str ()));
00100 DL((APP,"fname = \"%s\"\n", fname.c_str ()));
00101
00102 vector<string> ls = m_mw.get_clist (col_);
00103 dump_vector ("=== CList sequence ===", ls);
00104
00105 format = parse_format (prefix, ls);
00106
00107 if (format.length () == 0) {
00108 DL((APP,"Failed to parse format!\n"));
00109 Gtk::MessageDialog error ("Error parsing format!",
00110 MESSAGE_ERROR, BUTTONS_CLOSE);
00111 error.run ();
00112 return;
00113 }
00114
00115 int row = m_mw.get_row ();
00116 DL((TRACE,"Selected row %d, \"%s\"\n", row, ls[row].c_str ()));
00117
00118
00119
00120
00121 if (d_ == ActionsMenu::shrink) {
00122 DL((TRACE,"Shrinking sequence...\n"));
00123 dump_vector ("=== Original sequence ===", ls);
00124
00125 src = ls [row];
00126
00127 if (row != 0) {
00128 ls.erase (ls.begin (), ls.begin () + row);
00129 }
00130 dump_vector ("=== Chopped sequence ===", ls);
00131
00132 if (src != "gap") {
00133 src = dirpath + "/" + prefix + src + ".wav";
00134 target = src + ".bkp";
00135 log_rename (src, target);
00136 ::rename (src.c_str (), target.c_str ());
00137 }
00138 int i = 0;
00139 while (i < ls.size ()) {
00140 DL((TRACE,"Processing ls[%d] = \"%s\"\n", i, ls[i].c_str ()));
00141 if (ls [i] != "gap") {
00142 src = dirpath + "/" + prefix + ls [i] + ".wav";
00143 target = make_prev_name (src, format, prefix, dirpath);
00144 log_rename (src, target);
00145 ::rename (src.c_str (), target.c_str ());
00146 }
00147 i++;
00148 }
00149 }
00150
00151
00152
00153 else {
00154 DL((TRACE,"Expanding sequence...\n"));
00155 dump_vector ("=== Original sequence ===", ls);
00156
00157 if (row != 0) {
00158 ls.erase (ls.begin (), ls.begin () + row);
00159 }
00160 dump_vector ("=== Chopped sequence ===", ls);
00161
00162 vector<string>::reverse_iterator rk = ls.rbegin ();
00163
00164 while (rk != ls.rend ()) {
00165 DL((TRACE,"--------------------\n"));
00166 DL((TRACE,"Processing \"%s\"\n", (*rk).c_str ()));
00167 if ( *rk != "gap") {
00168 src = dirpath + "/" + prefix + *rk + ".wav";
00169 DL((TRACE,"src: \"%s\"\n",src.c_str ()));
00170 target = make_next_name (src, format, prefix, dirpath);
00171 DL((TRACE,"target: \"%s\"\n",target.c_str ()));
00172 log_rename (src, target);
00173 ::rename (src.c_str (), target.c_str ());
00174 }
00175 rk++;
00176 }
00177 }
00178
00179 m_mw.reload_cb ();
00180 }
00181