/* Copyright 2003 Rikard Björklind, Mikael Gransell This file is part of dc-qt. dc-qt is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. dc-qt is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with dc-qt; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "filetree.h" #include "filetreeitem.h" #include "debugdlg.h" #include "dc_settings.h" #include "util.h" #include #include #include #include extern Pixmap *pixmaps; FileTree::FileTree( QWidget* parent, const char* name, bool withGetFileList ) : QListView( parent, name ) { addColumn( "File/Dir",200 ); addColumn( "Size/Slots" ); setResizeMode(QListView::LastColumn); menu = new QPopupMenu( this, "Filetree menu" ); menu->insertItem( "Download", this, SLOT(slot_downloadFile()) ); menu->insertItem( "Download to...", this, SLOT(slot_downloadFileTo()) ); if(withGetFileList) { menu->insertItem( "Get File List", this, SLOT(getUserFileList()) ); menu->insertSeparator(); } dirMenu = new QPopupMenu( this, "Filetree dir-item menu" ); dirMenu->insertItem( "Download files", this, SLOT(slot_downloadDir()) ); dirMenu->insertItem( "Download files to...", this, SLOT(slot_downloadDirTo()) ); dirMenu->insertSeparator(); dirMenu->insertItem( "Download files and sub-dirs", this, SLOT(slot_downloadDirRecursive()) ); dirMenu->insertItem( "Download files and sub-dirs to...", this, SLOT(slot_downloadDirRecursiveTo()) ); if(withGetFileList) { dirMenu->insertSeparator(); dirMenu->insertItem( "Get File List", this, SLOT(getUserFileList()) ); } // I added double-click for download, YAY! Always bugged me that I couldn't. /* connect(this, SIGNAL(doubleClicked(QListViewItem*)), this, SLOT(slot_downloadFile(QListViewItem*))); */ connect( this, SIGNAL(contextMenuRequested( QListViewItem *, const QPoint &, int )), this, SLOT(dispMenu(QListViewItem *, const QPoint &, int)) ); } void FileTree::dispMenu( QListViewItem* item, const QPoint& p, int column ) { if( !item ) // We didn't click an item but somewhere else in the list return; // Popup menu depending on type of item if( ((FileTreeItem*)item)->getPath() == "dir" ) dirMenu->popup( p ); else menu->popup( p ); } void FileTree::addItemToTree( const QString& nick, const QString& fileName, const QString& size, const QString& slot ) { // Check if the user is already in the list FileTreeItem* par = (FileTreeItem *)findItem( nick, 0 ); if( !par || par->parent() ) { // The user was not in the list so we create an item or we // found an item that had the same name as the user but // wasn't a user. Strange sentence. par = new FileTreeItem( nick, "dir", this, nick, slot ); par->setPixmap( 0, pixmaps->getUser() ); } // Start parsing the file string and adding items QStringList filePath; filePath = QStringList::split( "\\", fileName ); int numElements = filePath.size(); if( filePath.isEmpty() ) // Something wrong with filename { debugWin->print( "Damn filepath is fucked up\n" ); return; } // Take numElements - 1 because the last item is the actual // filename and the other only the path. When we add the filename // we want to add the file size also. We dont want this when we // add the path names for( int i = 0; i < numElements - 1; i++ ) { FileTreeItem* child = (FileTreeItem *)par->firstChild(); bool found = false; while( child ) { if( child->text( 0 ) == filePath[i] ) { par = child; found = true; break; } else child = (FileTreeItem *)child->nextSibling(); } if( !found ) { par = new FileTreeItem( nick, "dir", par, filePath[i] ); par->setPixmap( 0, pixmaps->getFolder() ); } } // We have added the path to the file, now its time to add the // actuall file FileTreeItem* file = new FileTreeItem( nick, filePath.join( "\\" ), par, filePath[numElements - 1], size ); if( filePath[numElements - 1].endsWith( ".avi") || filePath[numElements - 1].endsWith( ".mpg") || filePath[numElements - 1].endsWith( ".mpeg") ) file->setPixmap( 0, pixmaps->getVideo() ); else if( filePath[numElements - 1].endsWith( ".mp3") || filePath[numElements - 1].endsWith( ".wma") ) file->setPixmap( 0, pixmaps->getAudio() ); else if( filePath[numElements - 1].endsWith( ".zip" ) || filePath[numElements - 1].endsWith( ".rar" )|| filePath[numElements - 1].endsWith( ".tgz" )|| filePath[numElements - 1].endsWith( ".tar.gz" )|| filePath[numElements - 1].endsWith( ".ace" ) ) file->setPixmap( 0, pixmaps->getArchive() ); else if( filePath[numElements - 1].endsWith( ".jpg" ) || filePath[numElements - 1].endsWith( ".gif" )|| filePath[numElements - 1].endsWith( ".tiff" ) ) file->setPixmap( 0, pixmaps->getImage() ); else file->setPixmap( 0, pixmaps->getImage() ); } void FileTree::getUserFileList() { FileTreeItem* current = (FileTreeItem *) currentItem(); if( !current ) return; // Get the filename, size and username and emit signal emit getUserFileList( current->getUserName() ); } void FileTree::slot_downloadFile() { downloadFile(QString("")); } void FileTree::slot_downloadDir() { downloadDir((FileTreeItem *) currentItem(), QString(""), false); } void FileTree::slot_downloadDirRecursive() { downloadDir((FileTreeItem *) currentItem(), QString(""), true); } void FileTree::slot_downloadFileTo() { downloadFile(selectDlDirDlg()); } void FileTree::slot_downloadDirTo() { downloadDir((FileTreeItem *) currentItem(), selectDlDirDlg(), false); } void FileTree::slot_downloadDirRecursiveTo() { downloadDir((FileTreeItem *) currentItem(), selectDlDirDlg(), true); } // actual processing of all types of downloadFile signals void FileTree::downloadFile(QString target_dir) { FileTreeItem* current = (FileTreeItem *) currentItem(); if( !current || current->getPath() == "dir" ) return; // Get the filename, size and username and emit signal QString localFile = checkFile(current->text(0),current->Size()); if(localFile!=QString::null) { emit downloadFile( current->getUserName(), current->getPath(), target_dir + localFile, current->Size() ); } } // processing of all types of downloadDir signals void FileTree::downloadDir(QListViewItem *dirItem, QString target_dir, bool recursive) { FileTreeItem *dirChild = (FileTreeItem *) dirItem->firstChild(); while( dirChild ) { if(dirChild->getPath() != "dir") { QString localFile = checkFile(dirChild->text(0),dirChild->Size()); if(!localFile.isEmpty()) { if (target_dir.isEmpty()) { target_dir = settings.get_setting("dl_dir"); if (!target_dir.isEmpty() && target_dir.at(target_dir.length()-1)!='/' && target_dir.at(target_dir.length()-1)!='\\') target_dir = target_dir+'/'; } QString subdir(""); if (dirChild->parent()!=NULL && ((FileTreeItem*)dirChild->parent())->getPath()=="dir") { subdir = dirChild->parent()->text(0); if (!subdir.isEmpty() && subdir.at(subdir.length()-1)!='/' && subdir.at(subdir.length()-1)!='\\') subdir = subdir + "/"; } emit downloadFile( dirChild->getUserName(), dirChild->getPath(), target_dir+subdir+localFile, dirChild->Size() ); } } else { if (recursive) downloadDir(dirChild, target_dir, true); } dirChild = (FileTreeItem *)dirChild->nextSibling(); } } /* * * $Log: filetree.cc,v $ * Revision 1.7 2004/02/29 22:40:25 olof * dslkjfhsldhl * not working * * Revision 1.6 2004/02/18 18:21:08 estrato * Remember alternative dl path, bugfixes * * Revision 1.5 2004/02/18 07:58:42 estrato * download to dir, resume download, bugfixes * * Revision 1.4 2003/12/11 22:11:27 olof * some fixes * * Revision 1.3 2003/12/08 00:13:40 estrato * Directory downloads * * Revision 1.2 2003/11/25 00:10:07 olof * Fixed the dup. send bug and the file list downloading bug. * * Revision 1.1.1.1 2003/06/24 10:16:17 olof * remake of cvs tree * * Revision 1.8 2003/06/16 12:18:12 olof * loads * * Revision 1.7 2003/06/11 22:14:21 olof * transfers ok again * * Revision 1.6 2003/05/08 12:34:14 olof * GPL header added * * Revision 1.5 2003/05/07 00:11:54 olof * massa mandelmassa * * Revision 1.4 2003/04/16 20:54:47 olof * user file list fixed * * Revision 1.3 2003/03/18 14:27:28 mickeg * Filetree working * Hopefully the userfilelisting also works. * * Revision 1.2 2003/03/17 21:32:17 mickeg * FileTree working * * Revision 1.1 2003/03/16 21:00:47 mickeg * Added functionality for the tree view of the search results * Still some bugs * Doesn't iterate through the children accurately * * */