Upstream tarball 10017
[amule.git] / src / DirectoryTreeCtrl.cpp
blobf4f87e375beb1cc5fc59d0074d19c9aecd0b6ea5
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 #ifdef CLIENT_GUI
67 m_IsRemote = !theApp->m_connect->IsConnectedToLocalHost();
68 #else
69 m_IsRemote = false;
70 #endif
74 CDirectoryTreeCtrl::~CDirectoryTreeCtrl()
78 enum {
79 IMAGE_FOLDER = 0,
80 IMAGE_FOLDER_SUB_SHARED
84 void CDirectoryTreeCtrl::Init()
86 // already done ?
87 if (m_IsInit) {
88 return;
90 m_IsInit = true;
92 // init image(s)
93 wxImageList* images = new wxImageList(16, 16);
94 images->Add(wxBitmap(amuleSpecial(1)));
95 images->Add(wxBitmap(amuleSpecial(2)));
96 // Gives wxTreeCtrl ownership of the list
97 AssignImageList(images);
100 // Create an empty root item, which we can
101 // safely append when creating a full path.
102 m_root = AddRoot(wxEmptyString, IMAGE_FOLDER, -1,
103 new CItemData(CPath()));
105 if (!m_IsRemote) {
106 #ifndef __WXMSW__
107 AddChildItem(m_root, CPath(wxT("/")));
108 #else
109 // this might take awhile, so change the cursor
110 ::wxSetCursor(*wxHOURGLASS_CURSOR);
111 // retrieve bitmask of all drives available
112 uint32 drives = GetLogicalDrives();
113 drives >>= 1;
114 for (char drive = 'C'; drive <= 'Z'; drive++) {
115 drives >>= 1;
116 if (! (drives & 1)) { // skip non existant drives
117 continue;
119 wxString driveStr = wxString::Format(wxT("%c:"), drive);
120 uint32 type = GetDriveType(driveStr + wxT("\\"));
122 // skip removable/undefined drives, share only fixed or remote drives
123 if ((type == 3 || type == 4) // fixed drive / remote drive
124 && CPath::DirExists(driveStr)) {
125 AddChildItem(m_root, CPath(driveStr));
128 ::wxSetCursor(*wxSTANDARD_CURSOR);
129 #endif
132 HasChanged = false;
134 UpdateSharedDirectories();
138 void CDirectoryTreeCtrl::OnItemExpanding(wxTreeEvent& evt)
140 wxTreeItemId hItem = evt.GetItem();
142 // Force reloading of the path
143 DeleteChildren(hItem);
144 AddSubdirectories(hItem, GetFullPath(hItem));
146 SortChildren(hItem);
150 void CDirectoryTreeCtrl::OnItemActivated(wxTreeEvent& evt)
152 if (!m_IsRemote) {
153 CheckChanged(evt.GetItem(), !IsBold(evt.GetItem()), false);
154 HasChanged = true;
159 void CDirectoryTreeCtrl::OnRButtonDown(wxTreeEvent& evt)
161 if (m_IsRemote) {
162 SelectItem(evt.GetItem()); // looks weird otherwise
163 } else {
164 // this might take awhile, so change the cursor
165 ::wxSetCursor(*wxHOURGLASS_CURSOR);
166 MarkChildren(evt.GetItem(), !IsBold(evt.GetItem()), false);
167 ::wxSetCursor(*wxSTANDARD_CURSOR);
168 HasChanged = true;
173 void CDirectoryTreeCtrl::MarkChildren(wxTreeItemId hChild, bool mark, bool recursed)
175 // Ensure that children are added, otherwise we might only get a "." entry.
176 if (!IsExpanded(hChild) && ItemHasChildren(hChild)) {
177 DeleteChildren(hChild);
178 AddSubdirectories(hChild, GetFullPath(hChild));
179 SortChildren(hChild);
182 wxTreeItemIdValue cookie;
183 wxTreeItemId hChild2 = GetFirstChild(hChild, cookie);
184 if (hChild2.IsOk()) {
185 SetHasSharedSubdirectory(hChild, mark);
187 while (hChild2.IsOk()) {
188 MarkChildren(hChild2, mark, true);
190 hChild2 = GetNextSibling(hChild2);
193 CheckChanged(hChild, mark, recursed);
197 void CDirectoryTreeCtrl::AddChildItem(wxTreeItemId hBranch, const CPath& item)
199 wxCHECK_RET(hBranch.IsOk(), wxT("Attempted to add children to invalid item"));
201 CPath fullPath = GetFullPath(hBranch).JoinPaths(item);
202 wxTreeItemId treeItem = AppendItem(hBranch, item.GetPrintable(),
203 IMAGE_FOLDER, -1,
204 new CItemData(item));
206 if (IsShared(fullPath)) {
207 SetItemBold(treeItem, true);
210 if (HasSharedSubdirectory(fullPath)) {
211 SetHasSharedSubdirectory(treeItem, true);
214 if (HasSubdirectories(fullPath)) {
215 // Trick. will show + if it has subdirs
216 AppendItem(treeItem, wxT("."));
221 CPath CDirectoryTreeCtrl::GetFullPath(wxTreeItemId hItem)
223 wxCHECK_MSG(hItem.IsOk(), CPath(), wxT("Invalid item in GetFullPath"));
225 CPath result;
226 for (; hItem.IsOk(); hItem = GetItemParent(hItem)) {
227 CItemData* data = dynamic_cast<CItemData*>(GetItemData(hItem));
228 wxCHECK_MSG(data, CPath(), wxT("Missing data-item in GetFullPath"));
230 result = data->GetPathComponent().JoinPaths(result);
233 return result;
237 void CDirectoryTreeCtrl::AddSubdirectories(wxTreeItemId hBranch, const CPath& path)
239 wxCHECK_RET(path.IsOk(), wxT("Invalid path in AddSubdirectories"));
241 CDirIterator sharedDir(path);
243 CPath dirName = sharedDir.GetFirstFile(CDirIterator::Dir);
244 while (dirName.IsOk()) {
245 AddChildItem(hBranch, dirName);
247 dirName = sharedDir.GetNextFile();
252 bool CDirectoryTreeCtrl::HasSubdirectories(const CPath& folder)
254 // Prevent error-messages if we try to traverse somewhere we have no access.
255 wxLogNull logNo;
257 return CDirIterator(folder).HasSubDirs();
261 void CDirectoryTreeCtrl::GetSharedDirectories(PathList* list)
263 wxCHECK_RET(list, wxT("Invalid list in GetSharedDirectories"));
265 for (SharedMap::iterator it = m_lstShared.begin(); it != m_lstShared.end(); it++) {
266 list->push_back(it->second);
271 void CDirectoryTreeCtrl::SetSharedDirectories(PathList* list)
273 wxCHECK_RET(list, wxT("Invalid list in SetSharedDirectories"));
275 m_lstShared.clear();
276 for (PathList::iterator it = list->begin(); it != list->end(); it++) {
277 m_lstShared.insert(SharedMapItem(GetKey(*it), *it));
280 if (m_IsInit) {
281 UpdateSharedDirectories();
286 wxString CDirectoryTreeCtrl::GetKey(const CPath& path)
288 if (m_IsRemote) {
289 return path.GetRaw();
292 // Sanity check, see IsSameAs() in Path.cpp
293 const wxString cwd = wxGetCwd();
294 const int flags = (wxPATH_NORM_ALL | wxPATH_NORM_CASE) & ~wxPATH_NORM_ENV_VARS;
295 wxFileName fn(path.GetRaw());
296 fn.Normalize(flags, cwd);
297 return fn.GetFullPath();
301 void CDirectoryTreeCtrl::UpdateSharedDirectories()
303 // ugly hack to at least show shared dirs in remote gui
304 if (m_IsRemote) {
305 DeleteChildren(m_root);
306 for (SharedMap::iterator it = m_lstShared.begin(); it != m_lstShared.end(); it++) {
307 AppendItem(m_root, it->second.GetPrintable(), IMAGE_FOLDER, -1, new CItemData(it->second));
309 return;
312 // Mark all shared root items (on windows this can be multiple
313 // drives, on unix there is only the root dir).
314 wxTreeItemIdValue cookie;
315 wxTreeItemId hChild = GetFirstChild(GetRootItem(), cookie);
317 while (hChild.IsOk()) {
318 // Does this drive have shared subfolders?
319 if (HasSharedSubdirectory(GetFullPath(hChild))) {
320 SetHasSharedSubdirectory(hChild, true);
323 // Is this drive shared?
324 if (IsShared(GetFullPath(hChild))) {
325 SetItemBold(hChild, true);
328 hChild = GetNextSibling(hChild);
333 bool CDirectoryTreeCtrl::HasSharedSubdirectory(const CPath& path)
335 SharedMap::iterator it = m_lstShared.upper_bound(GetKey(path) + wxFileName::GetPathSeparator());
336 if (it == m_lstShared.end()) {
337 return false;
339 // upper_bound() doesn't find the directory itself, so no need to check for that.
340 return it->second.StartsWith(path);
344 void CDirectoryTreeCtrl::SetHasSharedSubdirectory(wxTreeItemId hItem, bool add)
346 SetItemImage(hItem, add ? IMAGE_FOLDER_SUB_SHARED : IMAGE_FOLDER);
350 void CDirectoryTreeCtrl::CheckChanged(wxTreeItemId hItem, bool bChecked, bool recursed)
352 if (IsBold(hItem) != bChecked) {
353 SetItemBold(hItem, bChecked);
355 if (bChecked) {
356 AddShare(GetFullPath(hItem));
357 } else {
358 DelShare(GetFullPath(hItem));
361 if (!recursed) {
362 UpdateParentItems(hItem, bChecked);
368 bool CDirectoryTreeCtrl::IsShared(const CPath& path)
370 wxCHECK_MSG(path.IsOk(), false, wxT("Invalid path in IsShared"));
372 return m_lstShared.find(GetKey(path)) != m_lstShared.end();
376 void CDirectoryTreeCtrl::AddShare(const CPath& path)
378 wxCHECK_RET(path.IsOk(), wxT("Invalid path in AddShare"));
380 if (IsShared(path)) {
381 return;
384 m_lstShared.insert(SharedMapItem(GetKey(path), path));
388 void CDirectoryTreeCtrl::DelShare(const CPath& path)
390 wxCHECK_RET(path.IsOk(), wxT("Invalid path in DelShare"));
392 m_lstShared.erase(GetKey(path));
396 void CDirectoryTreeCtrl::UpdateParentItems(wxTreeItemId hChild, bool add)
398 wxTreeItemId parent = hChild;
399 while (parent != GetRootItem()) {
400 parent = GetItemParent(parent);
401 if (add) {
402 if (GetItemImage(parent) == IMAGE_FOLDER_SUB_SHARED) {
403 // parent already marked -> so are all its parents, finished
404 break;
405 } else {
406 SetHasSharedSubdirectory(parent, true);
408 } else {
409 if (GetItemImage(parent) == IMAGE_FOLDER_SUB_SHARED) {
410 // check if now there are still other shared dirs
411 if (HasSharedSubdirectory(GetFullPath(parent))) {
412 // yes, then further parents can stay red
413 break;
414 } else {
415 // no, further parents have to be checked too
416 SetHasSharedSubdirectory(parent, false);
418 } else { // should not happen (unmark child of which the parent is already unmarked
419 break;
424 // File_checked_for_headers