~ubuntu-branches/ubuntu/oneiric/codelite/oneiric

« back to all changes in this revision

Viewing changes to LiteEditor/tabgroupmanager.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessio Treglia
  • Date: 2010-06-14 15:30:55 UTC
  • mfrom: (1.1.8 upstream) (0.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20100614153055-dfmu128mp9862wpo
Tags: 2.5.3.4075~dfsg-1
* New upstream bugfix release.
* Update debian/copyright
* Change my email address.
* Refresh patches.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//////////////////////////////////////////////////////////////////////////////
 
2
//////////////////////////////////////////////////////////////////////////////
 
3
//
 
4
// copyright            : (C) 2010 by Eran Ifrah
 
5
// file name            : tabgroupmanager.cpp
 
6
//
 
7
// -------------------------------------------------------------------------
 
8
// A
 
9
//              _____           _      _     _ _
 
10
//             /  __ \         | |    | |   (_) |
 
11
//             | /  \/ ___   __| | ___| |    _| |_ ___
 
12
//             | |    / _ \ / _  |/ _ \ |   | | __/ _ )
 
13
//             | \__/\ (_) | (_| |  __/ |___| | ||  __/
 
14
//              \____/\___/ \__,_|\___\_____/_|\__\___|
 
15
//
 
16
//                                                  F i l e
 
17
//
 
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.
 
22
//
 
23
//////////////////////////////////////////////////////////////////////////////
 
24
//////////////////////////////////////////////////////////////////////////////
 
25
 
 
26
#include <wx/xml/xml.h>
 
27
#include <wx/filename.h>
 
28
#include <wx/dir.h>
 
29
#include <wx/stdpaths.h>
 
30
 
 
31
#include "manager.h"
 
32
#include "xmlutils.h"
 
33
#include "wx_xml_compatibility.h"
 
34
#include "tabgroupmanager.h"
 
35
 
 
36
TabgroupManager::~TabgroupManager()
 
37
{
 
38
        m_tabgroups.clear();
 
39
}
 
40
 
 
41
vTabGrps& TabgroupManager::GetTabgroups()
 
42
{
 
43
        if (m_tabgroups.empty()) {
 
44
                LoadKnownTabgroups();   // Of course there might just not *be* any, but safer to try loading
 
45
        }
 
46
        return m_tabgroups;
 
47
}
 
48
 
 
49
bool TabgroupManager::FindTabgroup(const wxString& tabgroupname, wxArrayString& items)
 
50
{
 
51
        items.Empty();
 
52
        vTabGrps::const_iterator iter = m_tabgroups.begin();
 
53
        for (; iter != m_tabgroups.end(); ++iter) {
 
54
                if (iter->first == tabgroupname) {
 
55
                        items = iter->second;
 
56
                        return true;
 
57
                }
 
58
        }
 
59
        return false;
 
60
}
 
61
 
 
62
wxString TabgroupManager::GetTabgroupDirectory()
 
63
{
 
64
        if (m_tabgroupdir.IsEmpty()) {
 
65
                SetTabgroupDirectory();
 
66
        }
 
67
        return m_tabgroupdir;
 
68
}
 
69
 
 
70
void TabgroupManager::SetTabgroupDirectory()
 
71
{
 
72
        wxFileName TabgrpPath = wxFileName::DirName( wxStandardPaths::Get().GetUserDataDir() + wxT("/tabgroups/") );
 
73
        if (!TabgrpPath.DirExists()) {
 
74
                TabgrpPath.Mkdir(0777, wxPATH_MKDIR_FULL);
 
75
        }
 
76
        m_tabgroupdir = TabgrpPath.GetPath();
 
77
}
 
78
 
 
79
void TabgroupManager::LoadKnownTabgroups()
 
80
{
 
81
        TransferKnownTabgroupsToTabgroupDir(GetTabgroupDirectory());    // Transitional code
 
82
 
 
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));
 
88
        }
 
89
}
 
90
 
 
91
void TabgroupManager::LoadTabgroupData(const wxString& tabgroup)
 
92
{
 
93
        // 'tabgroup' is the filepath of the tabgroup to load
 
94
        if (tabgroup.IsEmpty()) {
 
95
                return;
 
96
        }
 
97
 
 
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());
 
107
                }
 
108
                std::pair<wxString,wxArrayString> TabgroupItem(tabgroup, tabnames);
 
109
                m_tabgroups.push_back(TabgroupItem);
 
110
        }
 
111
}
 
112
 
 
113
bool TabgroupManager::DoAddItemToTabgroup(wxXmlDocument& doc, wxXmlNode* node, const wxString& filepath, const wxString& nextitemfilepath)
 
114
{
 
115
        wxXmlNode* TabInfoArrayNode = XmlUtils::FindFirstByTagName(doc.GetRoot(), wxT("TabInfoArray"));
 
116
        if (!TabInfoArrayNode) {
 
117
                return false;
 
118
        }
 
119
 
 
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);
 
124
        }
 
125
        if (previousnode) {
 
126
#if wxVERSION_NUMBER >= 2808
 
127
                return TabInfoArrayNode->InsertChildAfter(node, previousnode); // >=2.8.8 has a convenient function to do this
 
128
#else
 
129
                wxXmlNode* nextnode = previousnode->GetNext();
 
130
                if (nextnode) {
 
131
                        return TabInfoArrayNode->InsertChild(node, nextnode)
 
132
                } else {
 
133
                        return TabInfoArrayNode->AddChild(node);
 
134
                }
 
135
#endif
 
136
        } else {
 
137
                TabInfoArrayNode->AddChild(node);
 
138
        }
 
139
 
 
140
return true;
 
141
}
 
142
 
 
143
 
 
144
wxXmlNode* TabgroupManager::FindTabgroupItem(wxXmlDocument& doc, const wxString& filepath, const wxString& itemfilepath)
 
145
{
 
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
 
153
                                return TabInfoNode;
 
154
                        }
 
155
                        TabInfoNode = TabInfoNode->GetNext();
 
156
                }
 
157
        }
 
158
        // The Find failed, so return null
 
159
        return NULL;
 
160
}
 
161
 
 
162
wxXmlNode* TabgroupManager::DoDeleteTabgroupItem(wxXmlDocument& doc, const wxString& filepath, const wxString& itemfilepath)
 
163
{
 
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);
 
171
                                doc.Save(filepath);
 
172
                                return TabInfoNode;
 
173
                        }
 
174
                        TabInfoNode = TabInfoNode->GetNext();
 
175
                }
 
176
        }
 
177
 
 
178
        return NULL;
 
179
}
 
180
 
 
181
 
 
182
        //***************** April 2010: Transitional code. Remove sometime ****************************//
 
183
 
 
184
#include "editor_config.h"
 
185
void TabgroupManager::TransferKnownTabgroupsToTabgroupDir(const wxString& TabgrpPath)
 
186
{
 
187
        // First transfer any 'recent tabgroups' to the new tabgroup dir
 
188
        wxArrayString previousgroups, newpreviousgroups;
 
189
        EditorConfigST::Get()->GetRecentItems( previousgroups, wxT("RecentTabgroups") );
 
190
 
 
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);
 
196
                } else {
 
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());
 
201
                        }
 
202
                        newpreviousgroups.Add(newstyleitem.GetFullPath());
 
203
                }
 
204
        }
 
205
        EditorConfigST::Get()->SetRecentItems( newpreviousgroups, wxT("RecentTabgroups") ); // Store the new filepaths
 
206
 
 
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());
 
219
                        }
 
220
                }
 
221
        }
 
222
}
 
223
        //********************** /Transitional code *************************************//
 
224
 
 
225