File: tabgroupmanager.cpp

package info (click to toggle)
codelite 6.1.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 48,992 kB
  • ctags: 43,502
  • sloc: cpp: 334,263; ansic: 18,441; xml: 4,713; yacc: 2,653; lex: 2,449; python: 1,188; sh: 385; makefile: 40
file content (179 lines) | stat: -rw-r--r-- 5,870 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright            : (C) 2010 by Eran Ifrah
// file name            : tabgroupmanager.cpp
//
// -------------------------------------------------------------------------
// A
//              _____           _      _     _ _
//             /  __ \         | |    | |   (_) |
//             | /  \/ ___   __| | ___| |    _| |_ ___
//             | |    / _ \ / _  |/ _ \ |   | | __/ _ )
//             | \__/\ (_) | (_| |  __/ |___| | ||  __/
//              \____/\___/ \__,_|\___\_____/_|\__\___|
//
//                                                  F i l e
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

#include <wx/xml/xml.h>
#include <wx/filename.h>
#include <wx/dir.h>
#include <wx/stdpaths.h>

#include "manager.h"
#include "xmlutils.h"
#include "wx_xml_compatibility.h"
#include "tabgroupmanager.h"

TabgroupManager::~TabgroupManager()
{
	m_tabgroups.clear();
}

vTabGrps& TabgroupManager::GetTabgroups()
{
	if (m_tabgroups.empty()) {
		LoadKnownTabgroups();	// Of course there might just not *be* any, but safer to try loading
	}
	return m_tabgroups;
}

bool TabgroupManager::FindTabgroup(const wxString& tabgroupname, wxArrayString& items)
{
	items.Empty();
	vTabGrps::const_iterator iter = m_tabgroups.begin();
	for (; iter != m_tabgroups.end(); ++iter) {
		if (iter->first == tabgroupname) {
			items = iter->second;
			return true;
		}
	}
	return false;
}

wxString TabgroupManager::GetTabgroupDirectory()
{
	if (m_tabgroupdir.IsEmpty()) {
		SetTabgroupDirectory();
	}
	return m_tabgroupdir;
}

void TabgroupManager::SetTabgroupDirectory()
{
	wxFileName TabgrpPath = wxFileName::DirName(WorkspaceST::Get()->GetPrivateFolder() + wxT("/tabgroups/"));
	if (!TabgrpPath.DirExists()) {
		TabgrpPath.Mkdir(0777, wxPATH_MKDIR_FULL);
	}
	m_tabgroupdir = TabgrpPath.GetPath();
}

void TabgroupManager::LoadKnownTabgroups()
{
	wxArrayString Tabgrpfiles;
	// Load tabgroups from the tabgroup dir
	wxDir::GetAllFiles ( GetTabgroupDirectory(), &Tabgrpfiles, wxT("*.tabgroup"), wxDIR_FILES );
	for (size_t n=0; n < Tabgrpfiles.GetCount(); ++n) {
		LoadTabgroupData(Tabgrpfiles.Item(n));
	}
}

void TabgroupManager::LoadTabgroupData(const wxString& tabgroup)
{
	// 'tabgroup' is the filepath of the tabgroup to load
	if (tabgroup.IsEmpty()) {
		return;
	}

	// Load the data: we're only interested in the tab names here, not each CurrentLine etc
	TabGroupEntry session;
	wxString filepath = tabgroup.BeforeLast(wxT('.')); // FindSession() doesn't want the .ext here
	if (SessionManager::Get().GetSession(filepath, session, "tabgroup", tabgroupTag) ) {
		wxArrayString tabnames;
		const std::vector<TabInfo> &vTabInfoArr = session.GetTabInfoArr();
		for (size_t i = 0; i < vTabInfoArr.size(); ++i) {
			const TabInfo &ti = vTabInfoArr[i];
			tabnames.Add(ti.GetFileName());
		}
		std::pair<wxString,wxArrayString> TabgroupItem(tabgroup, tabnames);
		m_tabgroups.push_back(TabgroupItem);
	}
}

bool TabgroupManager::DoAddItemToTabgroup(wxXmlDocument& doc, wxXmlNode* node, const wxString& filepath, const wxString& nextitemfilepath)
{
	wxXmlNode* TabInfoArrayNode = XmlUtils::FindFirstByTagName(doc.GetRoot(), wxT("TabInfoArray"));
	if (!TabInfoArrayNode) {
		return false;
	}

	// If previousnode is valid, insert the new tab after it
	wxXmlNode* previousnode = NULL;
	if (!nextitemfilepath.IsEmpty()) {
		previousnode = FindTabgroupItem(doc, filepath, nextitemfilepath);
	}
	if (previousnode) {
#if wxVERSION_NUMBER >= 2808
		return TabInfoArrayNode->InsertChildAfter(node, previousnode); // >=2.8.8 has a convenient function to do this
#else
		wxXmlNode* nextnode = previousnode->GetNext();
		if (nextnode) {
			return TabInfoArrayNode->InsertChild(node, nextnode);
		} else {
			TabInfoArrayNode->AddChild(node);
			return true;
		}
#endif
	} else {
		TabInfoArrayNode->AddChild(node);
	}

return true;
}


wxXmlNode* TabgroupManager::FindTabgroupItem(wxXmlDocument& doc, const wxString& filepath, const wxString& itemfilepath)
{
	wxXmlNode* TabInfoArrayNode = XmlUtils::FindFirstByTagName(doc.GetRoot(), wxT("TabInfoArray"));
	if (TabInfoArrayNode) {
		wxXmlNode* TabInfoNode = TabInfoArrayNode->GetChildren();
		while (TabInfoNode) {
			wxXmlNode* child = XmlUtils::FindFirstByTagName(TabInfoNode, wxT("wxString"));
			if (child && child->GetPropVal(wxT("Value"), wxEmptyString) == itemfilepath) {
				// Found it. Return the data in TabInfoNode
				return TabInfoNode;
			}
			TabInfoNode = TabInfoNode->GetNext();
		}
	}
	// The Find failed, so return null
	return NULL;
}

wxXmlNode* TabgroupManager::DoDeleteTabgroupItem(wxXmlDocument& doc, const wxString& filepath, const wxString& itemfilepath)
{
	wxXmlNode* TabInfoArrayNode = XmlUtils::FindFirstByTagName(doc.GetRoot(), wxT("TabInfoArray"));
	if (TabInfoArrayNode) {
		wxXmlNode* TabInfoNode = TabInfoArrayNode->GetChildren();
		while (TabInfoNode) {
			wxXmlNode* child = XmlUtils::FindFirstByTagName(TabInfoNode, wxT("wxString"));
			if (child && child->GetPropVal(wxT("Value"), wxEmptyString) == itemfilepath) {
				TabInfoArrayNode->RemoveChild(TabInfoNode);
				doc.Save(filepath);
				return TabInfoNode;
			}
			TabInfoNode = TabInfoNode->GetNext();
		}
	}

	return NULL;
}