Upstream tarball 20080414
[amule.git] / src / DirectoryTreeCtrl.cpp
blob48350333a07017c9587e205d44a891948e6775fc
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2003 Robert Rostek ( tecxx@rrs.at )
6 // Copyright (c) 2002 Merkur ( devs@emule-project.net / http://www.emule-project.net )
7 //
8 // Any parts of this program derived from the xMule, lMule or eMule project,
9 // or contributed by third-party developers are copyrighted by their
10 // respective authors.
12 // This program is free software; you can redistribute it and/or modify
13 // it under the terms of the GNU General Public License as published by
14 // the Free Software Foundation; either version 2 of the License, or
15 // (at your option) any later version.
17 // This program is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU General Public License
23 // along with this program; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "DirectoryTreeCtrl.h" // Interface declarations
29 #include <wx/app.h>
30 #include <wx/filename.h>
31 #include <wx/imaglist.h>
33 #include <common/StringFunctions.h>
34 #include <common/FileFunctions.h>
35 #include "muuli_wdr.h" // Needed for amuleSpecial
38 BEGIN_EVENT_TABLE(CDirectoryTreeCtrl, wxTreeCtrl)
39 EVT_TREE_ITEM_RIGHT_CLICK(wxID_ANY, CDirectoryTreeCtrl::OnRButtonDown)
40 EVT_TREE_ITEM_ACTIVATED(wxID_ANY, CDirectoryTreeCtrl::OnItemActivated)
41 EVT_TREE_ITEM_EXPANDED(wxID_ANY, CDirectoryTreeCtrl::OnItemExpanding)
42 END_EVENT_TABLE()
45 class CItemData : public wxTreeItemData
47 public:
48 CItemData(const CPath& pathComponent)
49 : m_data(0)
50 , m_path(pathComponent)
54 ~CItemData() {}
56 void AddCount() { m_data++; }
57 void SubCount() { m_data--; }
58 int GetCount() const { return m_data; }
59 const CPath& GetPathComponent() const { return m_path; }
60 private:
61 int m_data;
62 CPath m_path;
66 CDirectoryTreeCtrl::CDirectoryTreeCtrl(wxWindow* parent, int id, const wxPoint& pos, wxSize siz, int flags)
67 : wxTreeCtrl(parent,id,pos,siz,flags,wxDefaultValidator,wxT("ShareTree"))
69 Init();
73 CDirectoryTreeCtrl::~CDirectoryTreeCtrl()
77 enum {
78 IMAGE_FOLDER = 0,
79 IMAGE_FOLDER_SUB_SHARED
83 void CDirectoryTreeCtrl::Init()
85 // init image(s)
86 wxImageList* images = new wxImageList(16, 16);
87 images->Add(wxBitmap(amuleSpecial(1)));
88 images->Add(wxBitmap(amuleSpecial(2)));
89 // Gives wxTreeCtrl ownership of the list
90 AssignImageList(images);
93 // Create an empty root item, which we can
94 // safely append when creating a full path.
95 wxTreeItemId root = AddRoot(wxEmptyString, IMAGE_FOLDER, -1,
96 new CItemData(CPath()));
99 #ifndef __WXMSW__
100 AddChildItem(root, CPath(wxT("/")));
101 #else
102 for (char drive = 'C'; drive <= 'Z'; drive++) {
103 wxString driveStr = wxString::Format(wxT("%c:"), drive);
105 if (CPath::DirExists(driveStr)) {
106 AddChildItem(root, CPath(driveStr));
109 #endif
111 HasChanged = false;
115 void CDirectoryTreeCtrl::OnItemExpanding(wxTreeEvent& evt)
117 wxTreeItemId hItem = evt.GetItem();
119 // Force reloading of the path
120 DeleteChildren(hItem);
121 AddSubdirectories(hItem, GetFullPath(hItem));
123 SortChildren(hItem);
127 void CDirectoryTreeCtrl::OnItemActivated(wxTreeEvent& evt)
129 CheckChanged(evt.GetItem(), !IsBold(evt.GetItem()));
130 HasChanged = true;
134 void CDirectoryTreeCtrl::OnRButtonDown(wxTreeEvent& evt)
136 MarkChildren(evt.GetItem(), !IsBold(evt.GetItem()));
137 HasChanged = true;
141 void CDirectoryTreeCtrl::MarkChildren(wxTreeItemId hChild, bool mark)
143 // Ensure that children are added, otherwise we might only get a "." entry.
144 if (!IsExpanded(hChild) && ItemHasChildren(hChild)) {
145 DeleteChildren(hChild);
146 AddSubdirectories(hChild, GetFullPath(hChild));
147 SortChildren(hChild);
150 wxTreeItemIdValue cookie;
151 wxTreeItemId hChild2 = GetFirstChild(hChild, cookie);
152 while (hChild2.IsOk()) {
153 MarkChildren(hChild2, mark);
155 hChild2 = GetNextSibling(hChild2);
158 CheckChanged(hChild, mark);
162 void CDirectoryTreeCtrl::AddChildItem(wxTreeItemId hBranch, const CPath& item)
164 wxCHECK_RET(hBranch.IsOk(), wxT("Attempted to add children to invalid item"));
166 CPath fullPath = GetFullPath(hBranch).JoinPaths(item);
167 wxTreeItemId treeItem = AppendItem(hBranch, item.GetPrintable(),
168 IMAGE_FOLDER, -1,
169 new CItemData(item));
171 if (IsShared(fullPath)) {
172 SetItemBold(treeItem, true);
173 UpdateParentItems(treeItem, true);
176 if (HasSharedSubdirectory(fullPath)) {
177 SetItemImage(treeItem, IMAGE_FOLDER_SUB_SHARED);
180 if (HasSubdirectories(fullPath)) {
181 // Trick. will show + if it has subdirs
182 AppendItem(treeItem, wxT("."));
187 CPath CDirectoryTreeCtrl::GetFullPath(wxTreeItemId hItem)
189 wxCHECK_MSG(hItem.IsOk(), CPath(), wxT("Invalid item in GetFullPath"));
191 CPath result;
192 for (; hItem.IsOk(); hItem = GetItemParent(hItem)) {
193 CItemData* data = dynamic_cast<CItemData*>(GetItemData(hItem));
194 wxCHECK_MSG(data, CPath(), wxT("Missing data-item in GetFullPath"));
196 result = data->GetPathComponent().JoinPaths(result);
199 return result;
203 void CDirectoryTreeCtrl::AddSubdirectories(wxTreeItemId hBranch, const CPath& path)
205 wxCHECK_RET(path.IsOk(), wxT("Invalid path in AddSubdirectories"));
207 CDirIterator sharedDir(path);
209 CPath dirName = sharedDir.GetFirstFile(CDirIterator::Dir);
210 while (dirName.IsOk()) {
211 AddChildItem(hBranch, dirName);
213 dirName = sharedDir.GetNextFile();
218 bool CDirectoryTreeCtrl::HasSubdirectories(const CPath& folder)
220 // Prevent error-messages if we try to traverse somewhere we have no access.
221 wxLogNull logNo;
223 return CDirIterator(folder).HasSubDirs();
227 void CDirectoryTreeCtrl::GetSharedDirectories(PathList* list)
229 wxCHECK_RET(list, wxT("Invalid list in GetSharedDirectories"));
231 list->insert(list->end(), m_lstShared.begin(), m_lstShared.end());
235 void CDirectoryTreeCtrl::SetSharedDirectories(PathList* list)
237 wxCHECK_RET(list, wxT("Invalid list in SetSharedDirectories"));
239 m_lstShared.clear();
240 m_lstShared.insert(m_lstShared.end(), list->begin(), list->end());
242 // Mark all shared root items (on windows this can be multiple
243 // drives, on unix there is only the root dir).
244 wxTreeItemIdValue cookie;
245 wxTreeItemId hChild = GetFirstChild(GetRootItem(), cookie);
247 while (hChild.IsOk()) {
248 // Does this drive have shared subfolders?
249 if (HasSharedSubdirectory(GetFullPath(hChild))) {
250 SetItemImage(hChild, IMAGE_FOLDER_SUB_SHARED);
253 // Is this drive shared?
254 if (IsShared(GetFullPath(hChild))) {
255 SetItemBold(hChild, true);
258 hChild = GetNextSibling(hChild);
263 bool CDirectoryTreeCtrl::HasSharedSubdirectory(const CPath& path)
265 PathList::iterator it = m_lstShared.begin();
266 for (; it != m_lstShared.end(); ++it) {
267 // IsSameDir to avoid the case where 'path' itself is shared.
268 if (it->StartsWith(path) && (!it->IsSameDir(path))) {
269 return true;
273 return false;
277 void CDirectoryTreeCtrl::CheckChanged(wxTreeItemId hItem, bool bChecked)
279 if (IsBold(hItem) != bChecked) {
280 SetItemBold(hItem, bChecked);
282 if (bChecked) {
283 AddShare(GetFullPath(hItem));
284 } else {
285 DelShare(GetFullPath(hItem));
288 UpdateParentItems(hItem, bChecked);
293 bool CDirectoryTreeCtrl::IsShared(const CPath& path)
295 wxCHECK_MSG(path.IsOk(), false, wxT("Invalid path in IsShared"));
297 PathList::iterator it = m_lstShared.begin();
298 for (; it != m_lstShared.end(); ++it) {
299 if (it->IsSameDir(path)) {
300 return true;
304 return false;
308 void CDirectoryTreeCtrl::AddShare(const CPath& path)
310 wxCHECK_RET(path.IsOk(), wxT("Invalid path in AddShare"));
312 if (IsShared(path)) {
313 return;
316 m_lstShared.push_back(path);
320 void CDirectoryTreeCtrl::DelShare(const CPath& path)
322 wxCHECK_RET(path.IsOk(), wxT("Invalid path in DelShare"));
324 PathList::iterator it = m_lstShared.begin();
325 for (; it != m_lstShared.end(); ++it ) {
326 if (it->IsSameDir(path)) {
327 m_lstShared.erase(it);
328 return;
334 void CDirectoryTreeCtrl::UpdateParentItems(wxTreeItemId hChild, bool add)
336 wxTreeItemId parent = hChild;
337 while (parent != GetRootItem()) {
338 parent = GetItemParent(parent);
339 CItemData* parent_data = dynamic_cast<CItemData*>(GetItemData(parent));
340 if (add) {
341 parent_data->AddCount();
342 if (parent_data->GetCount()==1) {
343 SetItemImage(parent, IMAGE_FOLDER_SUB_SHARED);
345 } else {
346 switch (parent_data->GetCount()) {
347 case 0:
348 break;
349 case 1:
350 SetItemImage(parent, IMAGE_FOLDER);
351 default:
352 parent_data->SubCount();
353 break;
358 // File_checked_for_headers