Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

/home/vlg/develop/gwavmerger/src/LoadProgress.cpp

Go to the documentation of this file.
00001 // -*- c++ -*-
00002 //------------------------------------------------------------------------------
00003 //                              LoadProgress.cpp
00004 //------------------------------------------------------------------------------
00005 // $Id: LoadProgress.cpp,v 1.6 2003/02/09 23:32:35 vlg Exp $
00006 //------------------------------------------------------------------------------
00007 //  Copyright (c) 2002 by Vladislav Grinchenko
00008 //
00009 //  This program is free software; you can redistribute it and/or
00010 //  modify it under the terms of the GNU General Public License
00011 //  as published by the Free Software Foundation; either version
00012 //  2 of the License, or (at your option) any later version.
00013 //------------------------------------------------------------------------------
00014 #ifdef HAVE_CONFIG_H
00015 #    include "config.h"
00016 #endif
00017 
00018 #include <libgnomeui/gnome-dialog.h>
00019 
00020 #include <gtkmm/buttonbox.h>
00021 #include <gtkmm/box.h>
00022 #include <gtkmm/label.h>
00023 
00024 #include <vector>
00025 #include <algorithm>
00026 
00027 #include <assa/CmdLineOpts.h>
00028 #include <assa/Fork.h>
00029 #include <assa/SigHandlers.h>
00030 
00031 #include "LoadProgress.h"
00032 #include "MainWindow.h"
00033 
00034 using namespace ASSA;
00035 
00036 
00037 LoadProgress::LoadProgress () 
00038     : m_dialog (NULL), Message (NULL), Progress (NULL),
00039       m_dp (0), m_max_val (0), m_entries (0), m_count (0)
00040 {  
00041     trace ("LoadProgress::LoadProgress");
00042 
00043     m_dialog = this;
00044     Gtk::Button* cancel_button = m_dialog->add_button(Gtk::Stock::CANCEL, 0);
00045 
00046     Gtk::Label *InfoLabel = 
00047         manage(new class Gtk::Label("Processing File :"));   
00048     Message = manage(new class Gtk::Label());
00049 
00050     Gtk::HBox *hbox1 = manage(new class Gtk::HBox(false, 0));
00051     Progress = manage(new class Gtk::ProgressBar());
00052 
00053     Gtk::VBox *vbox1 = manage(new class Gtk::VBox(false, 4));
00054 //    cancel_button->set_flags(GTK_CAN_FOCUS);
00055     InfoLabel->set_alignment(0.5, 0.5);
00056     InfoLabel->set_padding(0, 0);
00057     InfoLabel->set_justify(Gtk::JUSTIFY_LEFT);
00058     Message->set_alignment(0.5, 0.5);
00059     Message->set_padding(0, 0);
00060     hbox1->pack_start(*InfoLabel, false, false, 6);
00061     hbox1->pack_start(*Message, false, false, 0);
00062     Progress->set_size_request (235, 23);
00063     vbox1->pack_start(*hbox1, false, false, 0);
00064     vbox1->pack_start(*Progress, false, false, 0);
00065     m_dialog->get_vbox()->set_spacing(8);
00066     m_dialog->get_vbox()->pack_start(*vbox1);
00067 //    m_dialog->set_policy(false, false, false);
00068 
00069     Gtk::HButtonBox *dialog_action_area1 = m_dialog->get_action_area ();
00070     dialog_action_area1->set_spacing(8); 
00071     InfoLabel->show();
00072     Message->show();
00073     hbox1->show();
00074     Progress->show();
00075     vbox1->show();
00076     m_dialog->show();
00077     cancel_button->signal_clicked ().connect(
00078         SigC::slot(*(static_cast<class LoadProgress*>(this)), 
00079                    &LoadProgress::on_cancel_clicked));
00080 }
00081 
00082 //==============================================================================
00083 // Set up iteration loop. The iteration loop is a re-entrant function
00084 // process_file ().
00085 //------------------------------------------------------------------------------
00086 int
00087 
00088 LoadProgress::init (const string& subdir_, const string& prefix_, const string& proj_path_)
00089 {
00090     trace ("LoadProgress::init");
00091 
00092     string name;        // absolute file name
00093     m_dir = proj_path_ + "/" + subdir_;
00094     m_prefix = prefix_;
00095 
00096     if ((m_dp = opendir (m_dir.c_str ())) == NULL) {
00097         string msg ("Can't open directory\n\"");
00098         msg += m_dir + string ("\"");
00099         Gtk::MessageDialog emsg (msg, MESSAGE_ERROR);
00100         emsg.run ();
00101         return -1;
00102     }
00108     struct dirent* dirp = 0;
00109     while ((dirp = readdir (m_dp)) != NULL) {
00110         m_entries++;
00111     }
00112     rewinddir (m_dp);
00113     
00114     /* Setup idle work callback */
00115     Glib::signal_idle ().connect ((slot (*this, &LoadProgress::process_file)));
00116     dump ();
00117     return 0;
00118 }
00119 
00120 //==============================================================================
00121 // Idle work - called by Gtk+ event loop periodically.
00122 // Execute one iteration by processing a single WAV file.
00123 // Then, advance the progress dialog and change progress label
00124 //------------------------------------------------------------------------------
00125 bool
00126 
00127 LoadProgress::process_file (void)
00128 {
00129     trace ("LoadProgress::process_file");
00130 
00131     struct dirent* dirp = 0;
00132     string name;
00133     string wavinfo_args;
00134     string::size_type idx;
00135     
00136  skip:
00137     if (m_dp == NULL || (dirp = readdir (m_dp)) == NULL) {
00138         /*
00139          * We are at the end of the list; 
00140          * cancel dialog and idle work callback
00141          */
00142         closedir (m_dp);
00143         m_dp = NULL;
00144         sort (m_file_list.begin (), m_file_list.end ());
00145         // cause_close ();
00146         response (Gtk::RESPONSE_NONE);
00147         return false;
00148     }
00149     m_count++;
00150     name = dirp->d_name;
00151 
00155     if (name == "." || name == ".." ||
00156         name.find ("_pause.wav") != std::string::npos)
00157     {
00158         DL((TRACE,"Skiping \"%s\"\n", name.c_str ()));
00159         goto skip;          
00160     }
00161     wavinfo_args = "--silent " + m_dir + "/" + name;
00162 
00163     DL((TRACE,"Processing file \"%s\"\n", name.c_str ()));
00164     Message->set_text (name.c_str ());
00165     DL((TRACE,"Progress: %d / %d : %5.2f\n", m_count, m_entries,
00166         gfloat (m_count) / m_entries));
00167 
00168     Progress->set_fraction (gfloat (m_count) / m_entries);
00169 
00170     DL((TRACE,"Call fork_exec(wavinfo, \"%s\")\n",
00171         wavinfo_args.c_str ()));
00172 
00173     if (Fork::fork_exec ("wavinfo", wavinfo_args, true) != 0) {
00174         DL((APP,"Not a WAV file! Skipping \"%s\"\n", name.c_str ()));
00175         goto skip;
00176     }
00177 
00178     /* File name is of the format: [prefix][number].wav.
00179        For example, ru25_04.wav : prefix = "ru25_", number = 4,
00180        extension = ".wav".
00181        Remove prefix and extension, adjust maximun seen number and
00182        add number to the file list.
00183     */
00184     DL((APP,"Parsing file name \"%s\"\n", name.c_str ()));
00185     DL((APP,"Removing prefix \"%s\"\n", m_prefix.c_str ()));
00186 
00187     if ((idx = name.find (m_prefix)) == 0) {         // remove [prefix]
00188         DL((APP,"Prefix found in position = %d\n",idx));
00189 
00190         name.replace (0, m_prefix.size (), "");
00191         idx = name.find ('.');                      // remove '.wav' portion
00192         if (idx != string::npos) {
00193             DL((APP,"Found '.' in pos = %d\n", idx));
00194             if (name.substr (idx) != ".wav") {
00195                 DL((APP,"Extension \"%s\" != \".wav\"\n", 
00196                     name.substr(idx).c_str ()));
00197                 DL((APP,"Skipping file\n"));
00198                 goto skip;
00199             }
00200             DL((APP,"Removing extension; name = \"%s\"\n", name.c_str ()));
00201             name.replace (idx, name.size (), "");
00202         }
00203         DL((APP,"And the number is: %d\n", atoi (name.c_str ())));
00204         m_max_val = max2 (m_max_val, atoi (name.c_str ()));
00205         DL((APP,"New max_val = %d\n", m_max_val));
00206         m_file_list.push_back (name);
00207         DL((APP,"Added \"%s\" to the file list\n", name.c_str ()));
00208     }
00209     else {
00210         DL((APP, "Prefix is not found!\n"));
00211     }
00212     return true;        // continue processing
00213 }
00214 
00215 //==============================================================================
00216 // Run the dialog: show progress as files are tested
00217 //------------------------------------------------------------------------------
00218 int
00219 
00220 LoadProgress::run_load (const string&   subdir_,
00221           const string&   prefix_,
00222           const string&   proj_path_,
00223           vector<string>& file_list_)
00224 {
00225     trace ("LoadProgress::run");
00226 
00227     if (init (subdir_, prefix_, proj_path_) == -1) {
00228         return -1;
00229     }
00230 
00231     run ();
00232     file_list_ = m_file_list;   // copy the result
00233     return m_max_val;
00234 }
00235 
00236 void 
00237 
00238 LoadProgress::dump (void) const
00239 {
00240     trace("LoadProgress::dump");
00241 
00242     DL((TRACE,"m_dir     : \"%s\"\n", m_dir.c_str ()));
00243     DL((TRACE,"m_prefix  : \"%s\"\n", m_prefix.c_str ()));
00244     DL((TRACE,"m_entries : %d\n",     m_entries));
00245     DL((TRACE,"m_count   : %d\n",     m_count));
00246 
00247 }

Generated on Tue Feb 11 23:05:19 2003 for gwavmerger by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002