Improve speed of category tab title updates
[amule.git] / src / DirectoryTreeCtrl.cpp
blob41810a074687513de67c9973088852e3ffbcda0c
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-2008 Robert Rostek ( tecxx@rrs.at )
6 // Copyright (c) 2002-2008 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 "amule.h" // Needed for theApp
36 #include "muuli_wdr.h" // Needed for amuleSpecial
39 BEGIN_EVENT_TABLE(CDirectoryTreeCtrl, wxTreeCtrl)
40 EVT_TREE_ITEM_RIGHT_CLICK(wxID_ANY, CDirectoryTreeCtrl::OnRButtonDown)
41 EVT_TREE_ITEM_ACTIVATED(wxID_ANY, CDirectoryTreeCtrl::OnItemActivated)
42 EVT_TREE_ITEM_EXPANDED(wxID_ANY, CDirectoryTreeCtrl::OnItemExpanding)
43 END_EVENT_TABLE()
46 class CItemData : public wxTreeItemData
48 public:
49 CItemData(const CPath& pathComponent)
50 : m_path(pathComponent)
54 ~CItemData() {}
56 const CPath& GetPathComponent() const { return m_path; }
57 private:
58 CPath m_path;
62 CDirectoryTreeCtrl::CDirectoryTreeCtrl(wxWindow* parent, int id, const wxPoint& pos, wxSize siz, int flags)
63 : wxTreeCtrl(parent,id,pos,siz,flags,wxDefaultValidator,wxT("ShareTree"))
65 m_IsInit = false;
66 HasChanged = false;
67 #ifdef CLIENT_GUI
68 m_IsRemote = !theApp->m_connect->IsConnectedToLocalHost();
69 #else
70 m_IsRemote = false;
71 #endif
75 CDirectoryTreeCtrl::~CDirectoryTreeCtrl()
79 enum {
80 IMAGE_FOLDER = 0,
81 IMAGE_FOLDER_SUB_SHARED
85 void CDirectoryTreeCtrl::Init()
87 // already done ?
88 if (m_IsInit) {
89 return;
91 m_IsInit = true;
93 // init image(s)
94 wxImageList* images = new wxImageList(16, 16);
95 images->Add(wxBitmap(amuleSpecial(1)));
96 images->Add(wxBitmap(amuleSpecial(2)));
97 // Gives wxTreeCtrl ownership of the list
98 AssignImageList(images);
101 // Create an empty root item, which we can
102 // safely append when creating a full path.
103 m_root = AddRoot(wxEmptyString, IMAGE_FOLDER, -1,
104 new CItemData(CPath()));
106 if (!m_IsRemote) {
107 #ifndef __WXMSW__
108 AddChildItem(m_root, CPath(wxT("/")));
109 #else
110 // this might take awhile, so change the cursor
111 ::wxSetCursor(*wxHOURGLASS_CURSOR);
112 // retrieve bitmask of all drives available
113 uint32 drives = GetLogicalDrives();
114 drives >>= 1;
115 for (char drive = 'C'; drive <= 'Z'; drive++) {
116 drives >>= 1;
117 if (! (drives & 1)) { // skip non existant drives
118 continue;
120 wxString driveStr = CFormat(wxT("%c:")) % drive;
121 uint32 type = GetDriveType(driveStr + wxT("\\"));
123 // skip removable/undefined drives, share only fixed or remote drives
124 if ((type == 3 || type == 4) // fixed drive / remote drive
125 && CPath::DirExists(driveStr)) {
126 AddChildItem(m_root, CPath(driveStr));
129 ::wxSetCursor(*wxSTANDARD_CURSOR);
130 #endif
133 HasChanged = false;
135 UpdateSharedDirectories();
139 void CDirectoryTreeCtrl::OnItemExpanding(wxTreeEvent& evt)
141 wxTreeItemId hItem = evt.GetItem();
143 // Force reloading of the path
144 DeleteChildren(hItem);
145 AddSubdirectories(hItem, GetFullPath(hItem));
147 SortChildren(hItem);
151 void CDirectoryTreeCtrl::OnItemActivated(wxTreeEvent& evt)
153 if (!m_IsRemote) {
154 CheckChanged(evt.GetItem(), !IsBold(evt.GetItem()), false);
155 HasChanged = true;
160 void CDirectoryTreeCtrl::OnRButtonDown(wxTreeEvent& evt)
162 if (m_IsRemote) {
163 SelectItem(evt.GetItem()); // looks weird otherwise
164 } else {
165 // this might take awhile, so change the cursor
166 ::wxSetCursor(*wxHOURGLASS_CURSOR);
167 MarkChildren(evt.GetItem(), !IsBold(evt.GetItem()), false);
168 ::wxSetCursor(*wxSTANDARD_CURSOR);
169 HasChanged = true;
174 void CDirectoryTreeCtrl::MarkChildren(wxTreeItemId hChild, bool mark, bool recursed)
176 // Ensure that children are added, otherwise we might only get a "." entry.
177 if (!IsExpanded(hChild) && ItemHasChildren(hChild)) {
178 DeleteChildren(hChild);
179 AddSubdirectories(hChild, GetFullPath(hChild));
180 SortChildren(hChild);
183 wxTreeItemIdValue cookie;
184 wxTreeItemId hChild2 = GetFirstChild(hChild, cookie);
185 if (hChild2.IsOk()) {
186 SetHasSharedSubdirectory(hChild, mark);
188 while (hChild2.IsOk()) {
189 MarkChildren(hChild2, mark, true);
191 hChild2 = GetNextSibling(hChild2);
194 CheckChanged(hChild, mark, recursed);
198 void CDirectoryTreeCtrl::AddChildItem(wxTreeItemId hBranch, const CPath& item)
200 wxCHECK_RET(hBranch.IsOk(), wxT("Attempted to add children to invalid item"));
202 CPath fullPath = GetFullPath(hBranch).JoinPaths(item);
203 wxTreeItemId treeItem = AppendItem(hBranch, item.GetPrintable(),
204 IMAGE_FOLDER, -1,
205 new CItemData(item));
207 if (IsShared(fullPath)) {
208 SetItemBold(treeItem, true);
211 if (HasSharedSubdirectory(fullPath)) {
212 SetHasSharedSubdirectory(treeItem, true);
215 if (HasSubdirectories(fullPath)) {
216 // Trick. will show + if it has subdirs
217 AppendItem(treeItem, wxT("."));
222 CPath CDirectoryTreeCtrl::GetFullPath(wxTreeItemId hItem)
224 wxCHECK_MSG(hItem.IsOk(), CPath(), wxT("Invalid item in GetFullPath"));
226 CPath result;
227 for (; hItem.IsOk(); hItem = GetItemParent(hItem)) {
228 CItemData* data = dynamic_cast<CItemData*>(GetItemData(hItem));
229 wxCHECK_MSG(data, CPath(), wxT("Missing data-item in GetFullPath"));
231 result = data->GetPathComponent().JoinPaths(result);
234 return result;
238 void CDirectoryTreeCtrl::AddSubdirectories(wxTreeItemId hBranch, const CPath& path)
240 wxCHECK_RET(path.IsOk(), wxT("Invalid path in AddSubdirectories"));
242 CDirIterator sharedDir(path);
244 CPath dirName = sharedDir.GetFirstFile(CDirIterator::Dir);
245 while (dirName.IsOk()) {
246 AddChildItem(hBranch, dirName);
248 dirName = sharedDir.GetNextFile();
253 bool CDirectoryTreeCtrl::HasSubdirectories(const CPath& folder)
255 // Prevent error-messages if we try to traverse somewhere we have no access.
256 wxLogNull logNo;
258 return CDirIterator(folder).HasSubDirs();
262 void CDirectoryTreeCtrl::GetSharedDirectories(PathList* list)
264 wxCHECK_RET(list, wxT("Invalid list in GetSharedDirectories"));
266 for (SharedMap::iterator it = m_lstShared.begin(); it != m_lstShared.end(); it++) {
267 list->push_back(it->second);
272 void CDirectoryTreeCtrl::SetSharedDirectories(PathList* list)
274 wxCHECK_RET(list, wxT("Invalid list in SetSharedDirectories"));
276 m_lstShared.clear();
277 for (PathList::iterator it = list->begin(); it != list->end(); it++) {
278 m_lstShared.insert(SharedMapItem(GetKey(*it), *it));
281 if (m_IsInit) {
282 UpdateSharedDirectories();
287 wxString CDirectoryTreeCtrl::GetKey(const CPath& path)
289 if (m_IsRemote) {
290 return path.GetRaw();
293 // Sanity check, see IsSameAs() in Path.cpp
294 const wxString cwd = wxGetCwd();
295 const int flags = (wxPATH_NORM_ALL | wxPATH_NORM_CASE) & ~wxPATH_NORM_ENV_VARS;
296 wxFileName fn(path.GetRaw());
297 fn.Normalize(flags, cwd);
298 return fn.GetFullPath();
302 void CDirectoryTreeCtrl::UpdateSharedDirectories()
304 // ugly hack to at least show shared dirs in remote gui
305 if (m_IsRemote) {
306 DeleteChildren(m_root);
307 for (SharedMap::iterator it = m_lstShared.begin(); it != m_lstShared.end(); it++) {
308 AppendItem(m_root, it->second.GetPrintable(), IMAGE_FOLDER, -1, new CItemData(it->second));
310 return;
313 // Mark all shared root items (on windows this can be multiple
314 // drives, on unix there is only the root dir).
315 wxTreeItemIdValue cookie;
316 wxTreeItemId hChild = GetFirstChild(GetRootItem(), cookie);
318 while (hChild.IsOk()) {
319 // Does this drive have shared subfolders?
320 if (HasSharedSubdirectory(GetFullPath(hChild))) {
321 SetHasSharedSubdirectory(hChild, true);
324 // Is this drive shared?
325 if (IsShared(GetFullPath(hChild))) {
326 SetItemBold(hChild, true);
329 hChild = GetNextSibling(hChild);
334 bool CDirectoryTreeCtrl::HasSharedSubdirectory(const CPath& path)
336 SharedMap::iterator it = m_lstShared.upper_bound(GetKey(path) + wxFileName::GetPathSeparator());
337 if (it == m_lstShared.end()) {
338 return false;
340 // upper_bound() doesn't find the directory itself, so no need to check for that.
341 return it->second.StartsWith(path);
345 void CDirectoryTreeCtrl::SetHasSharedSubdirectory(wxTreeItemId hItem, bool add)
347 SetItemImage(hItem, add ? IMAGE_FOLDER_SUB_SHARED : IMAGE_FOLDER);
351 void CDirectoryTreeCtrl::CheckChanged(wxTreeItemId hItem, bool bChecked, bool recursed)
353 if (IsBold(hItem) != bChecked) {
354 SetItemBold(hItem, bChecked);
356 if (bChecked) {
357 AddShare(GetFullPath(hItem));
358 } else {
359 DelShare(GetFullPath(hItem));
362 if (!recursed) {
363 UpdateParentItems(hItem, bChecked);
369 bool CDirectoryTreeCtrl::IsShared(const CPath& path)
371 wxCHECK_MSG(path.IsOk(), false, wxT("Invalid path in IsShared"));
373 return m_lstShared.find(GetKey(path)) != m_lstShared.end();
377 void CDirectoryTreeCtrl::AddShare(const CPath& path)
379 wxCHECK_RET(path.IsOk(), wxT("Invalid path in AddShare"));
381 if (IsShared(path)) {
382 return;
385 m_lstShared.insert(SharedMapItem(GetKey(path), path));
389 void CDirectoryTreeCtrl::DelShare(const CPath& path)
391 wxCHECK_RET(path.IsOk(), wxT("Invalid path in DelShare"));
393 m_lstShared.erase(GetKey(path));
397 void CDirectoryTreeCtrl::UpdateParentItems(wxTreeItemId hChild, bool add)
399 wxTreeItemId parent = hChild;
400 while (parent != GetRootItem()) {
401 parent = GetItemParent(parent);
402 if (add) {
403 if (GetItemImage(parent) == IMAGE_FOLDER_SUB_SHARED) {
404 // parent already marked -> so are all its parents, finished
405 break;
406 } else {
407 SetHasSharedSubdirectory(parent, true);
409 } else {
410 if (GetItemImage(parent) == IMAGE_FOLDER_SUB_SHARED) {
411 // check if now there are still other shared dirs
412 if (HasSharedSubdirectory(GetFullPath(parent))) {
413 // yes, then further parents can stay red
414 break;
415 } else {
416 // no, further parents have to be checked too
417 SetHasSharedSubdirectory(parent, false);
419 } else { // should not happen (unmark child of which the parent is already unmarked
420 break;
425 // File_checked_for_headers