2 // This file is part of the aMule Project.
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 )
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.
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
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
)
46 class CItemData
: public wxTreeItemData
49 CItemData(const CPath
& pathComponent
)
50 : m_path(pathComponent
)
56 const CPath
& GetPathComponent() const { return 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"))
67 m_IsRemote
= !theApp
->m_connect
->IsConnectedToLocalHost();
74 CDirectoryTreeCtrl::~CDirectoryTreeCtrl()
80 IMAGE_FOLDER_SUB_SHARED
84 void CDirectoryTreeCtrl::Init()
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()));
107 AddChildItem(m_root
, CPath(wxT("/")));
109 // this might take awhile, so change the cursor
110 ::wxSetCursor(*wxHOURGLASS_CURSOR
);
111 // retrieve bitmask of all drives available
112 uint32 drives
= GetLogicalDrives();
114 for (char drive
= 'C'; drive
<= 'Z'; drive
++) {
116 if (! (drives
& 1)) { // skip non existant drives
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
);
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
));
150 void CDirectoryTreeCtrl::OnItemActivated(wxTreeEvent
& evt
)
153 CheckChanged(evt
.GetItem(), !IsBold(evt
.GetItem()), false);
159 void CDirectoryTreeCtrl::OnRButtonDown(wxTreeEvent
& evt
)
162 SelectItem(evt
.GetItem()); // looks weird otherwise
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
);
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(),
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"));
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
);
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.
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"));
276 for (PathList::iterator it
= list
->begin(); it
!= list
->end(); it
++) {
277 m_lstShared
.insert(SharedMapItem(GetKey(*it
), *it
));
281 UpdateSharedDirectories();
286 wxString
CDirectoryTreeCtrl::GetKey(const CPath
& path
)
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
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
));
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()) {
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
);
356 AddShare(GetFullPath(hItem
));
358 DelShare(GetFullPath(hItem
));
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
)) {
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
);
402 if (GetItemImage(parent
) == IMAGE_FOLDER_SUB_SHARED
) {
403 // parent already marked -> so are all its parents, finished
406 SetHasSharedSubdirectory(parent
, true);
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
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
424 // File_checked_for_headers