1
//////////////////////////////////////////////////////////////////////////////
2
//////////////////////////////////////////////////////////////////////////////
4
// copyright : (C) 2010 by Eran Ifrah
5
// file name : tabgroupmanager.cpp
7
// -------------------------------------------------------------------------
10
// / __ \ | | | | (_) |
11
// | / \/ ___ __| | ___| | _| |_ ___
12
// | | / _ \ / _ |/ _ \ | | | __/ _ )
13
// | \__/\ (_) | (_| | __/ |___| | || __/
14
// \____/\___/ \__,_|\___\_____/_|\__\___|
18
// This program is free software; you can redistribute it and/or modify
19
// it under the terms of the GNU General Public License as published by
20
// the Free Software Foundation; either version 2 of the License, or
21
// (at your option) any later version.
23
//////////////////////////////////////////////////////////////////////////////
24
//////////////////////////////////////////////////////////////////////////////
26
#include <wx/xml/xml.h>
27
#include <wx/filename.h>
29
#include <wx/stdpaths.h>
33
#include "wx_xml_compatibility.h"
34
#include "tabgroupmanager.h"
36
TabgroupManager::~TabgroupManager()
41
vTabGrps& TabgroupManager::GetTabgroups()
43
if (m_tabgroups.empty()) {
44
LoadKnownTabgroups(); // Of course there might just not *be* any, but safer to try loading
49
bool TabgroupManager::FindTabgroup(const wxString& tabgroupname, wxArrayString& items)
52
vTabGrps::const_iterator iter = m_tabgroups.begin();
53
for (; iter != m_tabgroups.end(); ++iter) {
54
if (iter->first == tabgroupname) {
62
wxString TabgroupManager::GetTabgroupDirectory()
64
if (m_tabgroupdir.IsEmpty()) {
65
SetTabgroupDirectory();
70
void TabgroupManager::SetTabgroupDirectory()
72
wxFileName TabgrpPath = wxFileName::DirName( wxStandardPaths::Get().GetUserDataDir() + wxT("/tabgroups/") );
73
if (!TabgrpPath.DirExists()) {
74
TabgrpPath.Mkdir(0777, wxPATH_MKDIR_FULL);
76
m_tabgroupdir = TabgrpPath.GetPath();
79
void TabgroupManager::LoadKnownTabgroups()
81
TransferKnownTabgroupsToTabgroupDir(GetTabgroupDirectory()); // Transitional code
83
wxArrayString Tabgrpfiles;
84
// Load tabgroups from the tabgroup dir
85
wxDir::GetAllFiles ( GetTabgroupDirectory(), &Tabgrpfiles, wxT("*.tabgroup"), wxDIR_FILES );
86
for (size_t n=0; n < Tabgrpfiles.GetCount(); ++n) {
87
LoadTabgroupData(Tabgrpfiles.Item(n));
91
void TabgroupManager::LoadTabgroupData(const wxString& tabgroup)
93
// 'tabgroup' is the filepath of the tabgroup to load
94
if (tabgroup.IsEmpty()) {
98
// Load the data: we're only interested in the tab names here, not each CurrentLine etc
99
TabGroupEntry session;
100
wxString filepath = tabgroup.BeforeLast(wxT('.')); // FindSession() doesn't want the .ext here
101
if (SessionManager::Get().FindSession(filepath, session, wxString(wxT(".tabgroup")), tabgroupTag) ) {
102
wxArrayString tabnames;
103
const std::vector<TabInfo> &vTabInfoArr = session.GetTabInfoArr();
104
for (size_t i = 0; i < vTabInfoArr.size(); ++i) {
105
const TabInfo &ti = vTabInfoArr[i];
106
tabnames.Add(ti.GetFileName());
108
std::pair<wxString,wxArrayString> TabgroupItem(tabgroup, tabnames);
109
m_tabgroups.push_back(TabgroupItem);
113
bool TabgroupManager::DoAddItemToTabgroup(wxXmlDocument& doc, wxXmlNode* node, const wxString& filepath, const wxString& nextitemfilepath)
115
wxXmlNode* TabInfoArrayNode = XmlUtils::FindFirstByTagName(doc.GetRoot(), wxT("TabInfoArray"));
116
if (!TabInfoArrayNode) {
120
// If previousnode is valid, insert the new tab after it
121
wxXmlNode* previousnode = NULL;
122
if (!nextitemfilepath.IsEmpty()) {
123
previousnode = FindTabgroupItem(doc, filepath, nextitemfilepath);
126
#if wxVERSION_NUMBER >= 2808
127
return TabInfoArrayNode->InsertChildAfter(node, previousnode); // >=2.8.8 has a convenient function to do this
129
wxXmlNode* nextnode = previousnode->GetNext();
131
return TabInfoArrayNode->InsertChild(node, nextnode)
133
return TabInfoArrayNode->AddChild(node);
137
TabInfoArrayNode->AddChild(node);
144
wxXmlNode* TabgroupManager::FindTabgroupItem(wxXmlDocument& doc, const wxString& filepath, const wxString& itemfilepath)
146
wxXmlNode* TabInfoArrayNode = XmlUtils::FindFirstByTagName(doc.GetRoot(), wxT("TabInfoArray"));
147
if (TabInfoArrayNode) {
148
wxXmlNode* TabInfoNode = TabInfoArrayNode->GetChildren();
149
while (TabInfoNode) {
150
wxXmlNode* child = XmlUtils::FindFirstByTagName(TabInfoNode, wxT("wxString"));
151
if (child && child->GetPropVal(wxT("Value"), wxEmptyString) == itemfilepath) {
152
// Found it. Return the data in TabInfoNode
155
TabInfoNode = TabInfoNode->GetNext();
158
// The Find failed, so return null
162
wxXmlNode* TabgroupManager::DoDeleteTabgroupItem(wxXmlDocument& doc, const wxString& filepath, const wxString& itemfilepath)
164
wxXmlNode* TabInfoArrayNode = XmlUtils::FindFirstByTagName(doc.GetRoot(), wxT("TabInfoArray"));
165
if (TabInfoArrayNode) {
166
wxXmlNode* TabInfoNode = TabInfoArrayNode->GetChildren();
167
while (TabInfoNode) {
168
wxXmlNode* child = XmlUtils::FindFirstByTagName(TabInfoNode, wxT("wxString"));
169
if (child && child->GetPropVal(wxT("Value"), wxEmptyString) == itemfilepath) {
170
TabInfoArrayNode->RemoveChild(TabInfoNode);
174
TabInfoNode = TabInfoNode->GetNext();
182
//***************** April 2010: Transitional code. Remove sometime ****************************//
184
#include "editor_config.h"
185
void TabgroupManager::TransferKnownTabgroupsToTabgroupDir(const wxString& TabgrpPath)
187
// First transfer any 'recent tabgroups' to the new tabgroup dir
188
wxArrayString previousgroups, newpreviousgroups;
189
EditorConfigST::Get()->GetRecentItems( previousgroups, wxT("RecentTabgroups") );
191
// Check each previous item 1) still exists, and 2) isn't already in the new-style tabgroups dir
192
for (int n = (int)previousgroups.GetCount()-1; n >= 0; --n) {
193
wxFileName previousitem(previousgroups.Item(n));
194
if (!previousitem.FileExists() ) {
195
previousgroups.RemoveAt(n);
197
// This is the what the tabgroup filepath would be in the new regime. See if it exists; if not, transfer it
198
wxFileName newstyleitem(TabgrpPath, previousitem.GetFullName());
199
if (!newstyleitem.FileExists() ) {
200
wxRenameFile(previousitem.GetFullPath(), newstyleitem.GetFullPath());
202
newpreviousgroups.Add(newstyleitem.GetFullPath());
205
EditorConfigST::Get()->SetRecentItems( newpreviousgroups, wxT("RecentTabgroups") ); // Store the new filepaths
207
// Now look in each recent workspace, in case any tabgroups are lurking there
208
wxArrayString RecentWorkspaces;
209
EditorConfigST::Get()->GetRecentItems( RecentWorkspaces, wxT("RecentWorkspaces") );
210
for (size_t n = 0; n < RecentWorkspaces.GetCount(); ++n) {
211
previousgroups.Clear(); newpreviousgroups.Clear();
212
wxFileName workspace(RecentWorkspaces.Item(n));
213
wxDir::GetAllFiles ( workspace.GetPath(), &previousgroups, wxT("*.tabgroup"), wxDIR_FILES );
214
for (size_t i = 0; i < previousgroups.GetCount(); ++i) {
215
wxFileName previousitem(previousgroups.Item(i));
216
wxFileName newstyleitem(TabgrpPath, previousitem.GetFullName());
217
if (!newstyleitem.FileExists() ) {
218
wxCopyFile(previousitem.GetFullPath(), newstyleitem.GetFullPath());
223
//********************** /Transitional code *************************************//