Code::Blocks  SVN r11506
cbworkspace.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the Code::Blocks IDE and licensed under the GNU Lesser General Public License, version 3
3  * http://www.gnu.org/licenses/lgpl-3.0.html
4  *
5  * $Revision: 9173 $
6  * $Id: cbworkspace.cpp 9173 2013-07-03 20:14:50Z fuscated $
7  * $HeadURL: https://svn.code.sf.net/p/codeblocks/code/trunk/src/sdk/cbworkspace.cpp $
8  */
9 
10 #include "sdk_precomp.h"
11 
12 #ifndef CB_PRECOMP
13  #include "cbworkspace.h"
14  #include "globals.h"
15  #include "manager.h"
16  #include "configmanager.h"
17  #include "logmanager.h"
18  #include "workspaceloader.h"
19 
20  #include <wx/intl.h>
21 #endif
22 
23 #include <wx/filedlg.h>
24 #include "filefilters.h"
25 
27  m_IsOK(true),
28  m_IsDefault(true),
29  m_Modified(false),
30  m_Filename(DEFAULT_WORKSPACE),
31  m_Title(_("Default workspace")),
32  m_PreferredTargetName()
33 {
34  //ctor
35  if ( filename.Matches(DEFAULT_WORKSPACE) || filename.IsEmpty() )
36  {
37  // if no filename given, use the default workspace
39 
40  if (!wxDirExists(tmp))
41  wxMkdir(tmp, 0755);
42 
43  tmp << wxFILE_SEP_PATH << DEFAULT_WORKSPACE;
44  m_Filename = tmp;
45  }
46  else
47  {
48  m_Filename = filename;
49  m_IsDefault = false;
50  }
51 
52  if ( !filename.IsEmpty() )
53  {
54  Load();
55  }
56 }
57 
59 {
60 }
61 
63 {
65  Manager::Get()->GetLogManager()->DebugLog(F(_T("Loading workspace \"%s\""), fname.wx_str()));
66 
67  if (!m_Filename.FileExists())
68  {
69  Manager::Get()->GetLogManager()->DebugLog(_T("File does not exist."));
70  if (!m_IsDefault)
71  {
72  wxString msg;
73  msg.Printf(_("Workspace '%s' does not exist..."), fname.c_str());
74  cbMessageBox(msg, _("Error"), wxOK | wxCENTRE | wxICON_ERROR);
75  // workspace wasn't loaded successfully
76  m_IsOK = false;
77  return;
78  }
79  }
80 
81  if (FileTypeOf(fname) == ftCodeBlocksWorkspace)
82  {
84 
85  wxString Title;
86  m_IsOK = pWsp && (pWsp->Open(fname, Title) || m_IsDefault);
87  if(!Title.IsEmpty())
88  {
89  m_Title = Title;
90  }
91 
92  delete pWsp;
93  }
94 
95  // load workspace layout file
96  LoadLayout();
97 
99  SetModified(false);
100 }
101 
102 bool cbWorkspace::Save(bool force)
103 {
105  return SaveAs(_T(""));
106 
107  // always save the layout file
108  SaveLayout();
109 
110  // don't save workspace unless modified or forced
111  if (!force && !m_Modified)
112  return true;
113 
114  Manager::Get()->GetLogManager()->DebugLog(F(_T("Saving workspace \"%s\""), m_Filename.GetFullPath().wx_str()));
115  WorkspaceLoader wsp;
116  bool ret = wsp.Save(m_Title, m_Filename.GetFullPath());
117  SetModified(!ret);
118  if(!ret)
119  cbMessageBox(_("Couldn't save workspace ") + m_Filename.GetFullPath() + _("\n(Maybe the file is write-protected?)"), _("Warning"), wxICON_WARNING);
120  return ret;
121 }
122 
123 bool cbWorkspace::SaveAs(cb_unused const wxString& filename)
124 {
125  wxFileDialog dlg(Manager::Get()->GetAppWindow(),
126  _("Save workspace"),
131  PlaceWindow(&dlg);
132  if (dlg.ShowModal() != wxID_OK)
133  return false;
134 
135  m_Filename = dlg.GetPath();
137  m_Filename.SetExt(_T("workspace"));
138 
140  m_IsDefault = true;
141  else
142  m_IsDefault = false;
143 
144  return Save(true);
145 }
146 
147 void cbWorkspace::SetTitle(const wxString& title)
148 {
149  m_Title = title;
150  SetModified(true);
151 }
152 
153 void cbWorkspace::SetModified(bool modified)
154 {
155  m_Modified = modified;
156  // Manager::Get()->GetLogManager()->DebugLog(F(_T("Setting workspace to modified = \"%s\""), modified ? _T("true") : _T("false")));
157 }
158 
160 {
161  if ( !target.IsEmpty() )
162  m_PreferredTargetName = target;
163 }
164 
166 {
167  return m_PreferredTargetName;
168 }
169 
171 {
172  SaveLayout();
173 }
174 
176 {
178  WorkspaceLoader wsl;
180  fn.SetExt( _T("workspace.layout") );
181  log->DebugLog(F(_T("Saving workspace layout \"%s\""), fn.GetFullPath().wx_str()));
182  const bool rc = wsl.SaveLayout( fn.GetFullPath() );
183  if (!rc)
184  {
185  log->DebugLog(F(_T("Couldn't save workspace layout \"%s\""), fn.GetFullPath().wx_str()));
186  }
187  return rc;
188 }
189 
191 {
193  WorkspaceLoader wsl;
195  fn.SetExt( _T("workspace.layout") );
196  bool rc = false;
197  if ( fn.FileExists() )
198  {
199  log->DebugLog(F(_T("Loading workspace layout \"%s\""), fn.GetFullPath().wx_str()));
200  rc = wsl.LoadLayout( fn.GetFullPath() );
201  if (!rc)
202  {
203  log->DebugLog(F(_T("Couldn't load workspace layout \"%s\""), fn.GetFullPath().wx_str()));
204  }
205  }
206  else
207  {
208  log->DebugLog(F(_T("Workspace layout file doesn't exist \"%s\""), fn.GetFullPath().wx_str()));
209  }
210  return rc;
211 }
wxString F(const wxChar *msg,...)
sprintf-like function
Definition: logmanager.h:20
bool Matches(const wxString &mask) const
#define wxICON_WARNING
static Manager * Get()
Use Manager::Get() to get a pointer to its instance Manager::Get() is guaranteed to never return an i...
Definition: manager.cpp:182
cbWorkspace(const wxString &filename=DEFAULT_WORKSPACE)
Constructor.
Definition: cbworkspace.cpp:26
#define wxICON_ERROR
virtual void SetTitle(const wxString &title)
Set the workspace&#39;s title.
virtual bool Open(const wxString &filename, wxString &Title)=0
bool m_IsDefault
Definition: cbworkspace.h:124
virtual bool Save(bool force=false)
Save the workspace.
wxCStrData c_str() const
bool wxDirExists(const wxString &dirname)
bool SaveLayout(const wxString &filename)
#define _T(string)
virtual void SetModified(bool modified)
Mark the workspace as modified or not.
bool LoadLayout()
DLLIMPORT FileType FileTypeOf(const wxString &filename)
Definition: globals.cpp:285
wxFileName m_Filename
Definition: cbworkspace.h:126
bool wxMkdir(const wxString &dir, int perm=wxS_DIR_DEFAULT)
wxString GetPreferredTarget() const
Get the preferred target for this workspace.
wxString GetExt() const
static wxString GetConfigFolder()
const DLLIMPORT wxString WORKSPACE_EXT
bool SaveLayout()
virtual ~cbWorkspace()
Destructor.
Definition: cbworkspace.cpp:58
LogManager * GetLogManager() const
Definition: manager.cpp:439
void SetPreferredTarget(const wxString &target)
Set the preferred target for this workspace.
const wxStringCharType * wx_str() const
bool Save(const wxString &title, const wxString &filename) override
wxString wxEmptyString
#define wxOK
const wxString & _(const wxString &string)
virtual bool SaveAs(const wxString &filename)
Save the workspace under a different filename.
DLLIMPORT wxString GetFilterString(const wxString &ext=wxEmptyString)
Generates and returns the filter string for use in file dialogs.
Definition: filefilters.cpp:60
bool IsEmpty() const
DLLIMPORT void PlaceWindow(wxTopLevelWindow *w, cbPlaceDialogMode mode=pdlBest, bool enforce=false)
Definition: globals.cpp:1177
wxString GetPath(int flags=wxPATH_GET_VOLUME, wxPathFormat format=wxPATH_NATIVE) const
wxString GetFullName() const
wxString m_Title
Definition: cbworkspace.h:127
DLLIMPORT const wxString DEFAULT_WORKSPACE
Definition: globals.cpp:55
void DebugLog(const wxString &msg, Logger::level lv=Logger::info)
Definition: logmanager.h:146
void SetExt(const wxString &ext)
bool FileExists() const
void ActiveProjectChanged()
int Printf(const wxString &pszFormat,...)
wxString m_PreferredTargetName
Definition: cbworkspace.h:128
bool LoadLayout(const wxString &filename)
wxString GetFullPath(wxPathFormat format=wxPATH_NATIVE) const
DLLIMPORT int cbMessageBox(const wxString &message, const wxString &caption=wxEmptyString, int style=wxOK, wxWindow *parent=NULL, int x=-1, int y=-1)
wxMessageBox wrapper.
Definition: globals.cpp:1395
bool m_Modified
Definition: cbworkspace.h:125