Exemplo n.º 1
0
int TimerRecordDialog::ExecutePostRecordActions(bool bWasStopped) {
   // MY: We no longer automatically (and silently) call ->Save() when the 
   // timer recording is completed.  We can now Save and/or Export depending 
   // on the options selected by the user.
   // Once completed, we can also close Audacity, restart the system or
   // shutdown the system.
   // If there was any error with the auto save or export then we will not do
   // the actions requested and instead present an error mesasge to the user.
   // Finally, if there is no post-record action selected then we output
   // a dialog detailing what has been carried out instead.

   AudacityProject* pProject = GetActiveProject();

   bool bSaveOK = false;
   bool bExportOK = false;
   int iPostRecordAction = m_pTimerAfterCompleteChoiceCtrl->GetSelection();
   int iOverriddenAction = iPostRecordAction;
   bool bErrorOverride = false;

   // Do Automatic Save?
   if (m_bAutoSaveEnabled) {

      // MY: If this project has already been saved then simply execute a Save here
      if (m_bProjectAlreadySaved) {
         bSaveOK = pProject->Save();
      } else {
         bSaveOK = pProject->SaveFromTimerRecording(m_fnAutoSaveFile);
      }
   }

   // Do Automatic Export?
   if (m_bAutoExportEnabled) {
      bExportOK = pProject->ExportFromTimerRecording(m_fnAutoExportFile, m_iAutoExportFormat,
                                                     m_iAutoExportSubFormat, m_iAutoExportFilterIndex);
   }

   // Check if we need to override the post recording action
   bErrorOverride = ((m_bAutoSaveEnabled && !bSaveOK) || (m_bAutoExportEnabled && !bExportOK));
   if (bErrorOverride || bWasStopped) {
      iPostRecordAction = POST_TIMER_RECORD_NOTHING;
   }

   if (iPostRecordAction == POST_TIMER_RECORD_NOTHING) {
      // If there is no post-record action then we can show a message indicating what has been done

      wxString sMessage = (bWasStopped ? _("Timer Recording stopped.") :
                                         _("Timer Recording completed."));

      if (m_bAutoSaveEnabled) {
         if (bSaveOK) {
            sMessage.Printf(_("%s\n\nRecording saved: %s"),
                            sMessage, m_fnAutoSaveFile.GetFullPath());
         } else {
            sMessage.Printf(_("%s\n\nError saving recording."), sMessage);
         }
      }
      if (m_bAutoExportEnabled) {
         if (bExportOK) {
            sMessage.Printf(_("%s\n\nRecording exported: %s"),
                            sMessage, m_fnAutoExportFile.GetFullPath());
         } else {
            sMessage.Printf(_("%s\n\nError exporting recording."), sMessage);
         }
      }

      if (bErrorOverride) {

         if ((iOverriddenAction != iPostRecordAction) &&
             (iOverriddenAction != POST_TIMER_RECORD_NOTHING)) {
            // Inform the user that we have overridden the selected action
            sMessage.Printf(_("%s\n\n'%s' has been canceled due to the error(s) noted above."),
                            sMessage,
                            m_pTimerAfterCompleteChoiceCtrl->GetString(iOverriddenAction));
         }

         // Show Error Message Box
         AudacityMessageBox(sMessage, _("Error"), wxICON_EXCLAMATION | wxOK);
      } else {

         if (bWasStopped && (iOverriddenAction != POST_TIMER_RECORD_NOTHING)) {
            sMessage.Printf(_("%s\n\n'%s' has been canceled as the recording was stopped."),
                            sMessage,
                            m_pTimerAfterCompleteChoiceCtrl->GetString(iOverriddenAction));
         }

         AudacityMessageBox(sMessage, _("Timer Recording"), wxICON_INFORMATION | wxOK);
      }
   }

   // MY: Lets do some actions that only apply to Exit/Restart/Shutdown
   if (iPostRecordAction >= POST_TIMER_RECORD_CLOSE) {
      do {

         // Set the flags as appropriate based on what we have done
         wxUint32 eActionFlags = TR_ACTION_NOTHING;
         if (m_bAutoSaveEnabled && bSaveOK) {
            eActionFlags |= TR_ACTION_SAVED;
         }
         if (m_bAutoExportEnabled && bExportOK) {
            eActionFlags |= TR_ACTION_EXPORTED;
         }

         // Lets show a warning dialog telling the user what is about to happen.
         // If the user no longer wants to carry out this action then they can click
         // Cancel and we will do POST_TIMER_RECORD_NOTHING instead.
         auto iDelayOutcome = PreActionDelay(iPostRecordAction, (TimerRecordCompletedActions)eActionFlags);
         if (iDelayOutcome != ProgressResult::Success) {
            // Cancel the action!
            iPostRecordAction = POST_TIMER_RECORD_NOTHING;
            // Set this to true to avoid any chance of the temp files being deleted
            bErrorOverride = true;
            break;
         }


         // If we have simply recorded, exported and then plan to Exit/Restart/Shutdown
         // then we will have a temporary project setup.  Let's get rid of that!
         if (m_bAutoExportEnabled && !m_bAutoSaveEnabled) {
            // PRL:  Move the following cleanup into a finally?
            // No, I think you would want to skip this, in case recording
            // succeeded but then save or export threw an exception.
            DirManager::CleanTempDir();
         }
      } while (false);
   }

   // Do we need to cleanup the orphaned temporary project?
   if (m_bProjectCleanupRequired && !bErrorOverride) {
      // PRL:  Move the following cleanup into a finally?
      // No, I think you would want to skip this, in case recording
      // succeeded but then save or export threw an exception.
      RemoveAllAutoSaveFiles();
   }

   // Return the action as required
   return iPostRecordAction;
}