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 Robert Rostek ( tecxx@rrs.at )
6 // Copyright (c) 2002 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 "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
)
45 class CItemData
: public wxTreeItemData
48 CItemData(const CPath
& pathComponent
)
50 , m_path(pathComponent
)
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
; }
66 CDirectoryTreeCtrl::CDirectoryTreeCtrl(wxWindow
* parent
, int id
, const wxPoint
& pos
, wxSize siz
, int flags
)
67 : wxTreeCtrl(parent
,id
,pos
,siz
,flags
,wxDefaultValidator
,wxT("ShareTree"))
73 CDirectoryTreeCtrl::~CDirectoryTreeCtrl()
79 IMAGE_FOLDER_SUB_SHARED
83 void CDirectoryTreeCtrl::Init()
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()));
100 AddChildItem(root
, CPath(wxT("/")));
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
));
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
));
127 void CDirectoryTreeCtrl::OnItemActivated(wxTreeEvent
& evt
)
129 CheckChanged(evt
.GetItem(), !IsBold(evt
.GetItem()));
134 void CDirectoryTreeCtrl::OnRButtonDown(wxTreeEvent
& evt
)
136 MarkChildren(evt
.GetItem(), !IsBold(evt
.GetItem()));
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(),
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"));
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
);
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.
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"));
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
))) {
277 void CDirectoryTreeCtrl::CheckChanged(wxTreeItemId hItem
, bool bChecked
)
279 if (IsBold(hItem
) != bChecked
) {
280 SetItemBold(hItem
, bChecked
);
283 AddShare(GetFullPath(hItem
));
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
)) {
308 void CDirectoryTreeCtrl::AddShare(const CPath
& path
)
310 wxCHECK_RET(path
.IsOk(), wxT("Invalid path in AddShare"));
312 if (IsShared(path
)) {
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
);
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
));
341 parent_data
->AddCount();
342 if (parent_data
->GetCount()==1) {
343 SetItemImage(parent
, IMAGE_FOLDER_SUB_SHARED
);
346 switch (parent_data
->GetCount()) {
350 SetItemImage(parent
, IMAGE_FOLDER
);
352 parent_data
->SubCount();
358 // File_checked_for_headers