/[svn]/gigedit/trunk/src/gigedit/mainwindow.cpp
ViewVC logotype

Diff of /gigedit/trunk/src/gigedit/mainwindow.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2666 by schoenebeck, Mon Jul 7 15:01:01 2014 UTC revision 2683 by schoenebeck, Mon Dec 29 16:30:21 2014 UTC
# Line 708  void Loader::progress_callback(float fra Line 708  void Loader::progress_callback(float fra
708  void Loader::thread_function()  void Loader::thread_function()
709  {  {
710      printf("thread_function self=%x\n", Glib::Threads::Thread::self());      printf("thread_function self=%x\n", Glib::Threads::Thread::self());
711      printf("Start %s\n", filename);      printf("Start %s\n", filename.c_str());
712      RIFF::File* riff = new RIFF::File(filename);      try {
713      gig = new gig::File(riff);          RIFF::File* riff = new RIFF::File(filename);
714      gig::progress_t progress;          gig = new gig::File(riff);
715      progress.callback = loader_progress_callback;          gig::progress_t progress;
716      progress.custom = this;          progress.callback = loader_progress_callback;
717            progress.custom = this;
718      gig->GetInstrument(0, &progress);  
719      printf("End\n");          gig->GetInstrument(0, &progress);
720      finished_dispatcher();          printf("End\n");
721            finished_dispatcher();
722        } catch (RIFF::Exception e) {
723            error_message = e.Message;
724            error_dispatcher.emit();
725        } catch (...) {
726            error_message = _("Unknown exception occurred");
727            error_dispatcher.emit();
728        }
729  }  }
730    
731  Loader::Loader(const char* filename)  Loader::Loader(const char* filename)
732      : filename(filename), thread(0)      : filename(filename), thread(0), progress(0.f)
733  {  {
734  }  }
735    
# Line 755  Glib::Dispatcher& Loader::signal_finishe Line 763  Glib::Dispatcher& Loader::signal_finishe
763      return finished_dispatcher;      return finished_dispatcher;
764  }  }
765    
766  LoadDialog::LoadDialog(const Glib::ustring& title, Gtk::Window& parent)  Glib::Dispatcher& Loader::signal_error()
767    {
768        return error_dispatcher;
769    }
770    
771    void saver_progress_callback(gig::progress_t* progress)
772    {
773        Saver* saver = static_cast<Saver*>(progress->custom);
774        saver->progress_callback(progress->factor);
775    }
776    
777    void Saver::progress_callback(float fraction)
778    {
779        {
780            Glib::Threads::Mutex::Lock lock(progressMutex);
781            progress = fraction;
782        }
783        progress_dispatcher.emit();
784    }
785    
786    void Saver::thread_function()
787    {
788        printf("thread_function self=%x\n", Glib::Threads::Thread::self());
789        printf("Start %s\n", filename.c_str());
790        try {
791            gig::progress_t progress;
792            progress.callback = saver_progress_callback;
793            progress.custom = this;
794    
795            // if no filename was provided, that means "save", if filename was provided means "save as"
796            if (filename.empty()) {
797                gig->Save(&progress);
798            } else {
799                gig->Save(filename, &progress);
800            }
801    
802            printf("End\n");
803            finished_dispatcher.emit();
804        } catch (RIFF::Exception e) {
805            error_message = e.Message;
806            error_dispatcher.emit();
807        } catch (...) {
808            error_message = _("Unknown exception occurred");
809            error_dispatcher.emit();
810        }
811    }
812    
813    Saver::Saver(gig::File* file, Glib::ustring filename)
814        : gig(file), filename(filename), thread(0), progress(0.f)
815    {
816    }
817    
818    void Saver::launch()
819    {
820    #ifdef OLD_THREADS
821        thread = Glib::Thread::create(sigc::mem_fun(*this, &Saver::thread_function), true);
822    #else
823        thread = Glib::Threads::Thread::create(sigc::mem_fun(*this, &Saver::thread_function));
824    #endif
825        printf("launch thread=%x\n", thread);
826    }
827    
828    float Saver::get_progress()
829    {
830        float res;
831        {
832            Glib::Threads::Mutex::Lock lock(progressMutex);
833            res = progress;
834        }
835        return res;
836    }
837    
838    Glib::Dispatcher& Saver::signal_progress()
839    {
840        return progress_dispatcher;
841    }
842    
843    Glib::Dispatcher& Saver::signal_finished()
844    {
845        return finished_dispatcher;
846    }
847    
848    Glib::Dispatcher& Saver::signal_error()
849    {
850        return error_dispatcher;
851    }
852    
853    ProgressDialog::ProgressDialog(const Glib::ustring& title, Gtk::Window& parent)
854      : Gtk::Dialog(title, parent, true)      : Gtk::Dialog(title, parent, true)
855  {  {
856      get_vbox()->pack_start(progressBar);      get_vbox()->pack_start(progressBar);
857      show_all_children();      show_all_children();
858        resize(600,50);
859  }  }
860    
861  // Clear all GUI elements / controls. This method is typically called  // Clear all GUI elements / controls. This method is typically called
# Line 828  bool MainWindow::close_confirmation_dial Line 924  bool MainWindow::close_confirmation_dial
924      dialog.set_default_response(Gtk::RESPONSE_YES);      dialog.set_default_response(Gtk::RESPONSE_YES);
925      int response = dialog.run();      int response = dialog.run();
926      dialog.hide();      dialog.hide();
927      if (response == Gtk::RESPONSE_YES) return file_save();  
928      return response != Gtk::RESPONSE_CANCEL;      // TODO: the following return valid is disabled and hard coded instead for
929        // now, due to the fact that saving with progress bar is now implemented
930        // asynchronously, as a result the app does not close automatically anymore
931        // after saving the file has completed
932        //
933        //   if (response == Gtk::RESPONSE_YES) return file_save();
934        //   return response != Gtk::RESPONSE_CANCEL;
935        //
936        if (response == Gtk::RESPONSE_YES) file_save();
937        return false; // always prevent closing the app for now (see comment above)
938  }  }
939    
940  bool MainWindow::leaving_shared_mode_dialog() {  bool MainWindow::leaving_shared_mode_dialog() {
# Line 880  void MainWindow::on_action_file_open() Line 985  void MainWindow::on_action_file_open()
985  void MainWindow::load_file(const char* name)  void MainWindow::load_file(const char* name)
986  {  {
987      __clear();      __clear();
988      load_dialog = new LoadDialog(_("Loading..."), *this);  
989      load_dialog->show_all();      progress_dialog = new ProgressDialog( //FIXME: memory leak!
990      loader = new Loader(strdup(name));          _("Loading") +  Glib::ustring(" '") +
991            Glib::filename_display_basename(name) + "' ...",
992            *this
993        );
994        progress_dialog->show_all();
995        loader = new Loader(name); //FIXME: memory leak!
996      loader->signal_progress().connect(      loader->signal_progress().connect(
997          sigc::mem_fun(*this, &MainWindow::on_loader_progress));          sigc::mem_fun(*this, &MainWindow::on_loader_progress));
998      loader->signal_finished().connect(      loader->signal_finished().connect(
999          sigc::mem_fun(*this, &MainWindow::on_loader_finished));          sigc::mem_fun(*this, &MainWindow::on_loader_finished));
1000        loader->signal_error().connect(
1001            sigc::mem_fun(*this, &MainWindow::on_loader_error));
1002      loader->launch();      loader->launch();
1003  }  }
1004    
# Line 928  void MainWindow::load_instrument(gig::In Line 1040  void MainWindow::load_instrument(gig::In
1040    
1041  void MainWindow::on_loader_progress()  void MainWindow::on_loader_progress()
1042  {  {
1043      load_dialog->set_fraction(loader->get_progress());      progress_dialog->set_fraction(loader->get_progress());
1044  }  }
1045    
1046  void MainWindow::on_loader_finished()  void MainWindow::on_loader_finished()
1047  {  {
1048      printf("Loader finished!\n");      printf("Loader finished!\n");
1049      printf("on_loader_finished self=%x\n", Glib::Threads::Thread::self());      printf("on_loader_finished self=%x\n", Glib::Threads::Thread::self());
1050      load_gig(loader->gig, loader->filename);      load_gig(loader->gig, loader->filename.c_str());
1051      load_dialog->hide();      progress_dialog->hide();
1052    }
1053    
1054    void MainWindow::on_loader_error()
1055    {
1056        Glib::ustring txt = _("Could not load file: ") + loader->error_message;
1057        Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
1058        msg.run();
1059        progress_dialog->hide();
1060  }  }
1061    
1062  void MainWindow::on_action_file_save()  void MainWindow::on_action_file_save()
# Line 975  bool MainWindow::file_save() Line 1095  bool MainWindow::file_save()
1095    
1096      std::cout << "Saving file\n" << std::flush;      std::cout << "Saving file\n" << std::flush;
1097      file_structure_to_be_changed_signal.emit(this->file);      file_structure_to_be_changed_signal.emit(this->file);
1098      try {  
1099          file->Save();      progress_dialog = new ProgressDialog( //FIXME: memory leak!
1100          if (file_is_changed) {          _("Saving") +  Glib::ustring(" '") +
1101              set_title(get_title().substr(1));          Glib::filename_display_basename(this->filename) + "' ...",
1102              file_is_changed = false;          *this
1103          }      );
1104      } catch (RIFF::Exception e) {      progress_dialog->show_all();
1105          file_structure_changed_signal.emit(this->file);      saver = new Saver(this->file); //FIXME: memory leak!
1106          Glib::ustring txt = _("Could not save file: ") + e.Message;      saver->signal_progress().connect(
1107          Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);          sigc::mem_fun(*this, &MainWindow::on_saver_progress));
1108          msg.run();      saver->signal_finished().connect(
1109          return false;          sigc::mem_fun(*this, &MainWindow::on_saver_finished));
1110      }      saver->signal_error().connect(
1111      std::cout << "Saving file done\n" << std::flush;          sigc::mem_fun(*this, &MainWindow::on_saver_error));
1112        saver->launch();
1113    
1114        return true;
1115    }
1116    
1117    void MainWindow::on_saver_progress()
1118    {
1119        progress_dialog->set_fraction(saver->get_progress());
1120    }
1121    
1122    void MainWindow::on_saver_error()
1123    {
1124        file_structure_changed_signal.emit(this->file);
1125        Glib::ustring txt = _("Could not save file: ") + saver->error_message;
1126        Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
1127        msg.run();
1128    }
1129    
1130    void MainWindow::on_saver_finished()
1131    {
1132        this->file = saver->gig;
1133        this->filename = saver->filename;
1134        current_gig_dir = Glib::path_get_dirname(filename);
1135        set_title(Glib::filename_display_basename(filename));
1136        file_has_name = true;
1137        file_is_changed = false;
1138        std::cout << "Saving file done. Importing queued samples now ...\n" << std::flush;
1139      __import_queued_samples();      __import_queued_samples();
1140        std::cout << "Importing queued samples done.\n" << std::flush;
1141    
1142      file_structure_changed_signal.emit(this->file);      file_structure_changed_signal.emit(this->file);
1143      return true;  
1144        load_gig(this->file, this->filename.c_str());
1145        progress_dialog->hide();
1146  }  }
1147    
1148  void MainWindow::on_action_file_save_as()  void MainWindow::on_action_file_save_as()
# Line 1056  bool MainWindow::file_save_as() Line 1207  bool MainWindow::file_save_as()
1207      descriptionArea.show_all();      descriptionArea.show_all();
1208    
1209      if (dialog.run() == Gtk::RESPONSE_OK) {      if (dialog.run() == Gtk::RESPONSE_OK) {
1210          file_structure_to_be_changed_signal.emit(this->file);          std::string filename = dialog.get_filename();
1211          try {          if (!Glib::str_has_suffix(filename, ".gig")) {
1212              std::string filename = dialog.get_filename();              filename += ".gig";
             if (!Glib::str_has_suffix(filename, ".gig")) {  
                 filename += ".gig";  
             }  
             printf("filename=%s\n", filename.c_str());  
             file->Save(filename);  
             this->filename = filename;  
             current_gig_dir = Glib::path_get_dirname(filename);  
             set_title(Glib::filename_display_basename(filename));  
             file_has_name = true;  
             file_is_changed = false;  
         } catch (RIFF::Exception e) {  
             file_structure_changed_signal.emit(this->file);  
             Glib::ustring txt = _("Could not save file: ") + e.Message;  
             Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);  
             msg.run();  
             return false;  
1213          }          }
1214          __import_queued_samples();          printf("filename=%s\n", filename.c_str());
1215          file_structure_changed_signal.emit(this->file);  
1216            progress_dialog = new ProgressDialog( //FIXME: memory leak!
1217                _("Saving") +  Glib::ustring(" '") +
1218                Glib::filename_display_basename(filename) + "' ...",
1219                *this
1220            );
1221            progress_dialog->show_all();
1222    
1223            saver = new Saver(file, filename); //FIXME: memory leak!
1224            saver->signal_progress().connect(
1225                sigc::mem_fun(*this, &MainWindow::on_saver_progress));
1226            saver->signal_finished().connect(
1227                sigc::mem_fun(*this, &MainWindow::on_saver_finished));
1228            saver->signal_error().connect(
1229                sigc::mem_fun(*this, &MainWindow::on_saver_error));
1230            saver->launch();
1231    
1232          return true;          return true;
1233      }      }
1234      return false;      return false;
# Line 2735  void MainWindow::mergeFiles(const std::v Line 2886  void MainWindow::mergeFiles(const std::v
2886          );          );
2887      }      }
2888    
2889      // Note: requires that this file already has a filename !      // Finally save gig file persistently to disk ...
2890      this->file->Save();      //NOTE: requires that this gig file already has a filename !
2891        {
2892            std::cout << "Saving file\n" << std::flush;
2893            file_structure_to_be_changed_signal.emit(this->file);
2894    
2895            progress_dialog = new ProgressDialog( //FIXME: memory leak!
2896                _("Saving") +  Glib::ustring(" '") +
2897                Glib::filename_display_basename(this->filename) + "' ...",
2898                *this
2899            );
2900            progress_dialog->show_all();
2901            saver = new Saver(this->file); //FIXME: memory leak!
2902            saver->signal_progress().connect(
2903                sigc::mem_fun(*this, &MainWindow::on_saver_progress));
2904            saver->signal_finished().connect(
2905                sigc::mem_fun(*this, &MainWindow::on_saver_finished));
2906            saver->signal_error().connect(
2907                sigc::mem_fun(*this, &MainWindow::on_saver_error));
2908            saver->launch();
2909        }
2910  }  }
2911    
2912  void MainWindow::on_action_merge_files() {  void MainWindow::on_action_merge_files() {

Legend:
Removed from v.2666  
changed lines
  Added in v.2683

  ViewVC Help
Powered by ViewVC