Audacity  3.0.3
Project.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity: A Digital Audio Editor
4 
5  Project.cpp
6 
7  Dominic Mazzoni
8  Vaughan Johnson
9 
10 *//*******************************************************************/
11 
12 
13 #include "Project.h"
14 
15 #include "KeyboardCapture.h"
16 #include "TempDirectory.h"
17 #include "./widgets/ErrorDialog.h"
18 
19 #include <wx/display.h>
20 #include <wx/filename.h>
21 #include <wx/frame.h>
22 
23 wxDEFINE_EVENT(EVT_TRACK_PANEL_TIMER, wxCommandEvent);
24 wxDEFINE_EVENT(EVT_PROJECT_ACTIVATION, wxCommandEvent);
25 
26 size_t AllProjects::size() const
27 {
28  return gAudacityProjects.size();
29 }
30 
32 {
33  return gAudacityProjects.begin();
34 }
35 
37 {
38  return gAudacityProjects.end();
39 }
40 
42 {
43  return gAudacityProjects.rbegin();
44 }
45 
47 {
48  return gAudacityProjects.rend();
49 }
50 
52 {
53  std::lock_guard<std::mutex> guard{ Mutex() };
54  auto start = begin(), finish = end(), iter = std::find_if(
55  start, finish,
56  [&]( const value_type &ptr ){ return ptr.get() == &project; }
57  );
58  if (iter == finish)
59  return nullptr;
60  auto result = *iter;
61  gAudacityProjects.erase( iter );
62  return result;
63 }
64 
65 void AllProjects::Add( const value_type &pProject )
66 {
67  std::lock_guard<std::mutex> guard{ Mutex() };
68  gAudacityProjects.push_back( pProject );
69 }
70 
71 bool AllProjects::sbClosing = false;
72 
73 bool AllProjects::Close( bool force )
74 {
75  ValueRestorer<bool> cleanup{ sbClosing, true };
76  while (AllProjects{}.size())
77  {
78  // Closing the project has global side-effect
79  // of deletion from gAudacityProjects
80  if ( force )
81  {
82  GetProjectFrame( **AllProjects{}.begin() ).Close(true);
83  }
84  else
85  {
86  if (! GetProjectFrame( **AllProjects{}.begin() ).Close())
87  return false;
88  }
89  }
90  return true;
91 }
92 
93 std::mutex &AllProjects::Mutex()
94 {
95  static std::mutex theMutex;
96  return theMutex;
97 }
98 
99 int AudacityProject::mProjectCounter=0;// global counter.
100 
101 /* Define Global Variables */
102 //This is a pointer to the currently-active project.
104 //This array holds onto all of the projects currently open
106 
108 {
109  return gActiveProject;
110 }
111 
113 {
114  if ( gActiveProject != project ) {
115  gActiveProject = project;
116  KeyboardCapture::Capture( nullptr );
117  wxTheApp->QueueEvent( safenew wxCommandEvent{ EVT_PROJECT_ACTIVATION } );
118  }
119  wxTheApp->SetTopWindow( FindProjectFrame( project ) );
120 }
121 
123 {
124  mProjectNo = mProjectCounter++; // Bug 322
126  // But not for the attached windows. They get built only on demand, such as
127  // from menu items.
128 
129  // Make sure there is plenty of space for Sqlite files
130  wxLongLong freeSpace = 0;
131 
132  auto path = TempDirectory::TempDir();
133  if (wxGetDiskSpace(path, NULL, &freeSpace)) {
134  if (freeSpace < wxLongLong(wxLL(100 * 1048576))) {
135  auto volume = FileNames::AbbreviatePath( path );
136  /* i18n-hint: %s will be replaced by the drive letter (on Windows) */
137  ShowErrorDialog(nullptr,
138  XO("Warning"),
139  XO("There is very little free disk space left on %s\n"
140  "Please select a bigger temporary directory location in\n"
141  "Directories Preferences.").Format( volume ),
142  "Error:_Disk_full_or_not_writable"
143  );
144  }
145  }
146 
147 }
148 
150 {
151 }
152 
153 void AudacityProject::SetFrame( wxFrame *pFrame )
154 {
155  mFrame = pFrame;
156 }
157 
158 void AudacityProject::SetPanel( wxWindow *pPanel )
159 {
160  mPanel = pPanel;
161 }
162 
163 const wxString &AudacityProject::GetProjectName() const
164 {
165  return mName;
166 }
167 
169 {
170  mName = name;
171 }
172 
174 {
175  return mInitialImportPath;
176 }
177 
179 {
180  if (mInitialImportPath.empty())
181  {
182  mInitialImportPath = path;
183  }
184 }
185 
186 AUDACITY_DLL_API wxFrame &GetProjectFrame( AudacityProject &project )
187 {
188  auto ptr = project.GetFrame();
189  if ( !ptr )
191  return *ptr;
192 }
193 
194 AUDACITY_DLL_API const wxFrame &GetProjectFrame( const AudacityProject &project )
195 {
196  auto ptr = project.GetFrame();
197  if ( !ptr )
199  return *ptr;
200 }
201 
202 AUDACITY_DLL_API wxWindow &GetProjectPanel( AudacityProject &project )
203 {
204  auto ptr = project.GetPanel();
205  if ( !ptr )
207  return *ptr;
208 }
209 
210 AUDACITY_DLL_API const wxWindow &GetProjectPanel(
211  const AudacityProject &project )
212 {
213  auto ptr = project.GetPanel();
214  if ( !ptr )
216  return *ptr;
217 }
FilePath
wxString FilePath
Definition: Identifier.h:227
AudacityProject::GetPanel
wxWindow * GetPanel()
Definition: Project.h:124
AllProjects::begin
const_iterator begin() const
Definition: Project.cpp:31
AudacityProject::SetFrame
void SetFrame(wxFrame *pFrame)
Definition: Project.cpp:153
gActiveProject
static AudacityProject * gActiveProject
Definition: Project.cpp:103
AudacityProject::GetFrame
wxFrame * GetFrame()
Definition: Project.h:120
Project.h
AudacityProject::~AudacityProject
virtual ~AudacityProject()
Definition: Project.cpp:149
SetActiveProject
void SetActiveProject(AudacityProject *project)
Definition: Project.cpp:112
AllProjects::Close
static bool Close(bool force=false)
Definition: Project.cpp:73
AllProjects::const_iterator
Container::const_iterator const_iterator
Definition: Project.h:50
AllProjects::end
const_iterator end() const
Definition: Project.cpp:36
KeyboardCapture.h
Format
Abstract base class used in importing a file.
GetActiveProject
AUDACITY_DLL_API AudacityProject * GetActiveProject()
Definition: Project.cpp:107
XO
#define XO(s)
Definition: Internat.h:31
AudacityProject::SetInitialImportPath
void SetInitialImportPath(const FilePath &path)
Definition: Project.cpp:178
AllProjects::Container
std::vector< AProjectHolder > Container
Definition: Project.h:41
AudacityProject::AudacityProject
AudacityProject()
Definition: Project.cpp:122
AllProjects::Remove
value_type Remove(AudacityProject &project)
Definition: Project.cpp:51
AudacityProject::SetPanel
void SetPanel(wxWindow *pPanel)
Definition: Project.cpp:158
AudacityProject::GetInitialImportPath
FilePath GetInitialImportPath() const
Definition: Project.cpp:173
AllProjects::size
size_t size() const
Definition: Project.cpp:26
AudacityProject::mProjectNo
int mProjectNo
Definition: Project.h:152
AllProjects::rbegin
const_reverse_iterator rbegin() const
Definition: Project.cpp:41
GetProjectFrame
AUDACITY_DLL_API wxFrame & GetProjectFrame(AudacityProject &project)
Get the top-level window associated with the project (as a wxFrame only, when you do not need to use ...
Definition: Project.cpp:186
AllProjects::gAudacityProjects
static Container gAudacityProjects
Definition: Project.h:42
FindProjectFrame
wxFrame * FindProjectFrame(AudacityProject *project)
Get a pointer to the window associated with a project, or null if the given pointer is null.
Definition: Project.h:172
FileNames::AbbreviatePath
AUDACITY_DLL_API wxString AbbreviatePath(const wxFileName &fileName)
Give enough of the path to identify the device. (On Windows, drive letter plus ':')
AllProjects::Add
void Add(const value_type &pProject)
Definition: Project.cpp:65
KeyboardCapture::Capture
void Capture(wxWindow *handler)
Definition: KeyboardCapture.cpp:69
name
const TranslatableString name
Definition: Distortion.cpp:98
AudacityProject::mFrame
wxWeakRef< wxFrame > mFrame
Definition: Project.h:161
THROW_INCONSISTENCY_EXCEPTION
#define THROW_INCONSISTENCY_EXCEPTION
Throw InconsistencyException, using C++ preprocessor to identify the source code location.
Definition: InconsistencyException.h:79
TempDirectory.h
ShowErrorDialog
void ShowErrorDialog(wxWindow *parent, const TranslatableString &dlogTitle, const TranslatableString &message, const wxString &helpPage, const bool Close, const wxString &log)
Displays an error dialog with a button that offers help.
Definition: ErrorDialog.cpp:152
AllProjects::const_reverse_iterator
Container::const_reverse_iterator const_reverse_iterator
Definition: Project.h:54
GetProjectPanel
AUDACITY_DLL_API wxWindow & GetProjectPanel(AudacityProject &project)
Get the main sub-window of the project frame that displays track data.
Definition: Project.cpp:202
AllProjects::sbClosing
static bool sbClosing
Definition: Project.h:79
AudacityProject::mPanel
wxWeakRef< wxWindow > mPanel
Definition: Project.h:162
AudacityProject
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:112
ErrorDialog.h
ClientData::Site::BuildAll
void BuildAll()
For each RegisteredFactory, if the corresponding attachment is absent in this, build and store it.
Definition: ClientData.h:441
AllProjects
an object of class AllProjects acts like a standard library container, but refers to a global array o...
Definition: Project.h:38
AudacityProject::mName
wxString mName
Definition: Project.h:149
ValueRestorer
Set a variable temporarily in a scope.
Definition: MemoryX.h:331
AllProjects::Mutex
static std::mutex & Mutex()
Definition: Project.cpp:93
wxDEFINE_EVENT
wxDEFINE_EVENT(EVT_TRACK_PANEL_TIMER, wxCommandEvent)
safenew
#define safenew
Definition: MemoryX.h:10
AudacityProject::SetProjectName
void SetProjectName(const wxString &name)
Definition: Project.cpp:168
AudacityProject::GetProjectName
const wxString & GetProjectName() const
Definition: Project.cpp:163
AudacityProject::mProjectCounter
static int mProjectCounter
Definition: Project.h:151
AllProjects::value_type
Container::value_type value_type
Definition: Project.h:58
AllProjects::rend
const_reverse_iterator rend() const
Definition: Project.cpp:46
AudacityProject::mInitialImportPath
FilePath mInitialImportPath
Definition: Project.h:154
TempDirectory::TempDir
AUDACITY_DLL_API wxString TempDir()
Definition: TempDirectory.cpp:26