1 /*
2 	This file is part of Browse Tracker, a plugin for Code::Blocks
3 	Copyright (C) 2007 Pecan Heber
4 
5 	This program is free software; you can redistribute it and/or
6 	modify it under the terms of the GNU General Public License
7 	as published by the Free Software Foundation; either version 2
8 	of the License, or (at your option) any later version.
9 
10 	This program is distributed in the hope that it will be useful,
11 	but WITHOUT ANY WARRANTY; without even the implied warranty of
12 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 	GNU General Public License for more details.
14 
15 	You should have received a copy of the GNU General Public License
16 	along with this program; if not, write to the Free Software
17 	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 // RCS-ID: $Id: ProjectData.cpp 11347 2018-03-26 14:24:13Z pecanh $
20 
21 #if defined(CB_PRECOMP)
22 #include "sdk.h"
23 #else
24 	#include "sdk_events.h"
25 	#include "manager.h"
26 	#include "editormanager.h"
27 	#include "editorbase.h"
28 	#include "cbeditor.h"
29 	#include "cbproject.h"
30 #endif
31 
32     #include <wx/dynarray.h> //for wxArray and wxSortedArray
33     #include <projectloader_hooks.h>
34     #include <editor_hooks.h>
35     #include "personalitymanager.h"
36 	#include <wx/stdpaths.h>
37 	#include <wx/app.h>
38 	#include <wx/menu.h>
39 
40 #include "Version.h"
41 #include "BrowseMarks.h"
42 #include "BrowseTrackerDefs.h"
43 #include "ProjectData.h"
44 #include "BrowseTrackerLayout.h"
45 
46 // ----------------------------------------------------------------------------
ProjectData()47 ProjectData::ProjectData()
48 // ----------------------------------------------------------------------------
49 {
50     //private ctor
51 }
52 // ----------------------------------------------------------------------------
ProjectData(cbProject * pcbProject)53 ProjectData::ProjectData(cbProject* pcbProject)
54 // ----------------------------------------------------------------------------
55 {
56     //ctor
57     #if defined(LOGGING)
58     if (not pcbProject) asm("int3"); /*trap*/;
59     #endif
60     if (not pcbProject) return;
61     m_pCBProject = pcbProject;
62     m_ProjectFilename = pcbProject->GetFilename();
63     m_CurrIndexEntry = 0;
64     m_LastIndexEntry = MaxEntries-1;
65     m_pEdMgr = Manager::Get()->GetEditorManager();
66     m_ActivationCount = 0;
67     m_bLayoutLoaded = false;
68     LoadLayout();
69 }
70 // ----------------------------------------------------------------------------
~ProjectData()71 ProjectData::~ProjectData()
72 // ----------------------------------------------------------------------------
73 {
74     //dtor
75 
76     // *BrowseMarksArchive* release the editor layout hash table ptrs to BrowseMarks
77     for (FileBrowse_MarksHash::iterator it = m_FileBrowse_MarksArchive.begin(); it != m_FileBrowse_MarksArchive.end(); ++it)
78     {
79         delete it->second;
80     }
81     m_FileBrowse_MarksArchive.clear();
82 
83 
84 }
85 // ----------------------------------------------------------------------------
IncrementActivationCount()86 void ProjectData::IncrementActivationCount()
87 // ----------------------------------------------------------------------------
88 {
89     m_ActivationCount += 1;
90 }
91 // ----------------------------------------------------------------------------
GetActivationCount()92 int ProjectData::GetActivationCount()
93 // ----------------------------------------------------------------------------
94 {
95      return m_ActivationCount++;
96 }
97 // ----------------------------------------------------------------------------
GetProjectFilename()98 wxString ProjectData::GetProjectFilename()
99 // ----------------------------------------------------------------------------
100 {
101     return m_ProjectFilename;
102 }
103 // ----------------------------------------------------------------------------
AddEditor(wxString)104 void ProjectData::AddEditor( wxString /*filePath */)
105 // ----------------------------------------------------------------------------
106 {
107 ////    // Don't stow duplicates
108 ////    if ( m_EditorBaseArray.Index( eb ) != wxNOT_FOUND ) return;
109 ////
110 ////    // not found, stow new data into arrays
111 ////    int index = m_LastIndexEntry;
112 ////    if (++index >= MaxEntries) index = 0;
113 ////    m_LastIndexEntry = index;
114 ////    m_EditorBaseArray[index] = eb;
115 ////    m_cbEditorArray[index] = cbed;
116 ////    m_cbSTCArray[index] = control;
117 
118 }
119 // ----------------------------------------------------------------------------
FindFilename(const wxString filePath)120 bool ProjectData::FindFilename( const wxString filePath)
121 // ----------------------------------------------------------------------------
122 {
123     //Return true if we own a BrowseMarks array by this filepath;
124 
125     FileBrowse_MarksHash& hash = m_FileBrowse_MarksArchive;
126     FileBrowse_MarksHash::iterator it = hash.find(filePath);
127     if ( it == hash.end() ) {
128         //DumpHash(wxT("BrowseMarks"));
129         return false;
130     }
131     return true;
132 }
133 // ----------------------------------------------------------------------------
GetBrowse_MarksFromHash(wxString filePath)134 BrowseMarks* ProjectData::GetBrowse_MarksFromHash( wxString filePath)
135 // ----------------------------------------------------------------------------
136 {
137     // Return a pointer to a BrowseMarks array with this filePath
138 
139     FileBrowse_MarksHash& hash = m_FileBrowse_MarksArchive;
140     return GetPointerToBrowse_MarksArray(hash, filePath);
141 }
142 // ----------------------------------------------------------------------------
GetPointerToBrowse_MarksArray(FileBrowse_MarksHash & hash,wxString filePath)143 BrowseMarks* ProjectData::GetPointerToBrowse_MarksArray(FileBrowse_MarksHash& hash, wxString filePath)
144 // ----------------------------------------------------------------------------
145 {
146     // Return a pointer to a BrowseMarks array with this filePath
147 
148     for (FileBrowse_MarksHash::iterator it = hash.begin(); it != hash.end(); it++)
149     {
150         BrowseMarks* p = it->second;
151         if ( p->GetFilePath() == filePath ) {return p;}
152     }
153 
154     return 0;
155 }
156 // ----------------------------------------------------------------------------
HashAddBrowse_Marks(const wxString fullPath)157 BrowseMarks* ProjectData::HashAddBrowse_Marks( const wxString fullPath )
158 // ----------------------------------------------------------------------------
159 {
160     // EditorManager calls fail during the OnEditorClose event
161     //EditorBase* eb = Manager::Get()->GetEditorManager()->GetEditor(filename);
162 
163     EditorBase* eb = m_pEdMgr->GetEditor(fullPath);
164     #if defined(LOGGING)
165         if(not eb) asm("int3"); /*trap*/
166     #endif
167     if(not eb) return 0;
168     wxString filePath = eb->GetFilename();
169     if (filePath.IsEmpty()) return 0;
170     // don't add duplicates
171     BrowseMarks* pBrowse_Marks = GetBrowse_MarksFromHash( filePath );
172     if (pBrowse_Marks) return pBrowse_Marks ;
173     pBrowse_Marks = new BrowseMarks( fullPath );
174     if (pBrowse_Marks) m_FileBrowse_MarksArchive[filePath] = pBrowse_Marks;
175 
176     return pBrowse_Marks;
177 }
178 // ----------------------------------------------------------------------------
LoadLayout()179 void ProjectData::LoadLayout()
180 // ----------------------------------------------------------------------------
181 {
182     // Load a layout file for this project
183     #if defined(LOGGING)
184     LOGIT( _T("ProjectData::LoadLayout()for[%s]"),m_ProjectFilename.c_str() );
185     #endif
186 
187     if (m_ProjectFilename.IsEmpty())
188         return ;
189 
190     wxFileName fname(m_ProjectFilename);
191     fname.SetExt(_T("bmarks"));
192     BrowseTrackerLayout layout( m_pCBProject );
193     layout.Open(fname.GetFullPath(), m_FileBrowse_MarksArchive );
194     m_bLayoutLoaded = true;
195 }
196 // ----------------------------------------------------------------------------
SaveLayout()197 void ProjectData::SaveLayout()
198 // ----------------------------------------------------------------------------
199 {
200     // Write a layout file for this project
201     #if defined(LOGGING)
202     LOGIT( _T("ProjectData::SAVELayout()") );
203     #endif
204 
205     if (m_ProjectFilename.IsEmpty())
206         return ;
207 
208     wxFileName fname(m_ProjectFilename);
209     fname.SetExt(_T("bmarks"));
210     BrowseTrackerLayout layout( m_pCBProject );
211     //DumpBrowse_Marks(wxT("BrowseMarks"));
212     layout.Save(fname.GetFullPath(), m_FileBrowse_MarksArchive );
213 
214 
215     // *Testing* See if cbEditor is actually there
216     //EditorBase* eb = m_EditorBaseArray[1];
217     //cbEditor* cbed = Manager::Get()->GetEditorManager()->GetBuiltinEditor(eb);
218     //cbStyledTextCtrl* control = cbed->GetControl();
219     //#if defined(LOGGING)
220     //LOGIT( _T("eb[%p]cbed[%p]control[%p]"), eb, cbed, control );
221     //#endif
222 
223     // *Testing* Check against our array
224     //eb = m_EditorBaseArray[1];
225     //cbed = m_cbEditorArray[1];
226     //control = m_cbSTCArray[1];
227     //#if defined(LOGGING)
228     //LOGIT( _T("eb[%p]cbed[%p]control[%p]"), eb, cbed, control );
229     //#endif
230 
231 
232 }
233 // ----------------------------------------------------------------------------
DumpHash(const wxString hashType)234 void ProjectData::DumpHash( const wxString
235 #if defined(LOGGING)
236 	hashType
237 #endif
238 )
239 // ----------------------------------------------------------------------------
240 {
241 
242     #if defined(LOGGING)
243 
244     FileBrowse_MarksHash* phash = &m_FileBrowse_MarksArchive;
245     FileBrowse_MarksHash& hash = *phash;
246 
247     LOGIT( _T("--- DumpProjectHash ---[%s]Count[%lu]"), hashType.wx_str(), static_cast<unsigned long>(hash.size()), m_ProjectFilename.wx_str() );
248     for (FileBrowse_MarksHash::iterator it = hash.begin(); it != hash.end(); it++)
249     {
250         wxString filename = it->first; //an Editor filename withing this project
251         BrowseMarks* p = it->second;    // ptr to array of Editor Browse/Book mark cursor positions
252         LOGIT( _T("filename[%s]BrowseMark*[%p]name[%s]"), filename.c_str(), p, p->GetFilePath().c_str() );
253     }
254 
255     #endif
256 }
257 
258 // ----------------------------------------------------------------------------
DumpBrowse_Marks(const wxString hashType)259 void ProjectData::DumpBrowse_Marks( const wxString
260 #if defined(LOGGING)
261 	hashType
262 #endif
263 )
264 // ----------------------------------------------------------------------------
265 {
266     #if defined(LOGGING)
267     LOGIT( _T("--- DumpBrowseData ---[%s]"), hashType.c_str()  );
268 
269     FileBrowse_MarksHash* phash = &m_FileBrowse_MarksArchive;
270     FileBrowse_MarksHash& hash = *phash;
271 
272     LOGIT( _T("Dump_%s Size[%lu]"), hashType.wx_str(), static_cast<unsigned long>(hash.size()) );
273 
274     for (FileBrowse_MarksHash::iterator it = hash.begin(); it != hash.end(); ++it)
275     {
276         wxString filename = it->first;
277         BrowseMarks* p = it->second;
278         LOGIT( _T("Filename[%s]%s*[%p]name[%s]"), filename.c_str(), hashType.c_str(), p, p->GetFilePath().c_str() );
279         if (p)
280         {   //dump the browse marks
281             p->Dump();
282         }
283     }
284 
285     #endif
286 }
287 // ----------------------------------------------------------------------------
288 // 2008/01/23 MortenMacFly
289 // ----------------------------------------------------------------------------
290 // I have serious issues with this plugin (recently). Whenever I try to import a
291 // Visual Studio solution C::B crashes. The reason is the BT plugin, file
292 // ProjectData.cpp, line 63 (m_ProjectFilename = pcbProject->GetFilename();).
293 // It seems pcbProject *is* NULL sometimes in the case of an import.
294 // So you better don't rely on it. I had to disable the BT plugin to be able to
295 // import VS solutions again. Sad
296