#include "list.h" #include "listitem.h" #include #include #include #include #include "config.h" #include #include #include #include #include "defines.h" /** * Constructor */ List::List(Config* conf, QWidget * parent, const char * name, WFlags f) : QListBox( parent, name, f ){ config = conf; setFont(QFont(config->getOption("font"), config->getOption("fontsize").toInt())); setPaletteForegroundColor(QColor("#"+config->getOption("fgcolor"))); setPaletteBackgroundColor(QColor("#"+config->getOption("bgcolor"))); setBottomScrollBar(false); setAutoScrollBar(false); connect(this, SIGNAL(selected(QListBoxItem*)), this, SLOT(action(QListBoxItem*))); connect(this, SIGNAL(highlighted(QListBoxItem*)), this, SLOT(handleSelection(QListBoxItem*))); connect(&timer, SIGNAL(timeout()), this, SLOT(getInfo())); readDir(config->getOption("menudir")); }; /** * Reads list of file from given directory and add them to the list */ void List::readDir(QString directory){ // mount/unmount CD Drive umount(currentpath); currentpath = directory; mount(directory); // Set choosen Path as default for images (Infopanel) QMimeSourceFactory::defaultFactory()->setFilePath(directory); //Read directory list QDir dir = QDir(); dir.setFilter( QDir::Files | QDir::Dirs | QDir::Readable ); dir.setSorting( QDir::Name | QDir::IgnoreCase | QDir::DirsFirst ); dir.setPath(directory); if(! dir.isReadable() ){ //XXX wrong path! display some kind of error! return; } //clear current list clear(); //read dir and add items const QFileInfoList *dlist = dir.entryInfoList(); QFileInfoListIterator it( *dlist ); QFileInfo *file; while ( (file = it.current()) != 0 ) { if(file->fileName() != "."){ addItem(file); } ++it; } //select 1st item setCurrentItem(0); } /** * Adds a single Item to the List */ void List::addItem(QFileInfo* file){ QString iconpath; QPixmap icon; QString prog; iconpath = config->getOption("iconpath"); iconpath.append("/"); if(file->isDir()){ if( (file->fileName() == "..") and QFileInfo(currentpath).absFilePath() == QFileInfo(config->getOption("menudir")).absFilePath() ){ // we're in top directory -> no up here! return; } iconpath.append("folder.png"); }else{ prog = config->getProgram(file->fileName()); if(prog.isEmpty()) return; iconpath.append(file->extension(false)); iconpath.append(".png"); if(!QFileInfo(iconpath).exists()){ iconpath = config->getOption("iconpath"); iconpath.append("/unknown.png"); } } if (!QPixmapCache::find(iconpath,icon)){ icon.load(iconpath); QPixmapCache::insert(iconpath,icon); } insertItem(new ListItem(icon,*file,prog)); } /** * SLOT. Gets called when a item was selected. Calls external program * or changes directory */ void List::action(QListBoxItem* qitem){ ListItem* item = (ListItem*) qitem; if( item == NULL) return; if(item->fi.isDir()){ readDir(item->fi.absFilePath()); }else{ qDebug("executing '%s'",item->getProgram().latin1()); emit runningExtern(true); qApp->mainWidget()->hide(); system(item->getProgram().latin1()); qApp->mainWidget()->show(); emit runningExtern(false); } } /** * SLOT. Moves the selection bar. Positive numbers move down negative * ones up. */ void List::moveSelection(int val){ int i = currentItem() + val; if (i < 0 ){ i = 0; } if (i > numRows()){ i = numRows()-1; } setCurrentItem(i); } /** * SLOT. Selects an item by emitting the appropriate signal. */ void List::select(){ emit selected(currentItem()); emit selected(selectedItem()); } /** * SLOT. Selects the upper dir. */ void List::back(){ if( QFileInfo(currentpath).absFilePath() == QFileInfo(config->getOption("menudir")).absFilePath() ){ // we're in top directory -> no up here! return; } readDir(currentpath+"/.."); } /** * Mounts the given directory if it is a defined mountpoint. */ void List::mount(QString dir){ if(config->isMountPoint(dir)){ qDebug("mounting %s",dir.latin1()); system("mount "+dir); } } /** * Unmounts the given directory if it is a defined mountpoint. */ void List::umount(QString dir){ if(config->isMountPoint(dir)){ qDebug("unmounting %s",dir.latin1()); system("umount "+dir); } } /** * SLOT. Handles thing to be done when selecting an item */ void List::handleSelection(QListBoxItem* qitem){ ListItem* item = (ListItem*) qitem; if (item == NULL) return; emit setLCD(item->getName()); emit setInfo(""); timer.start(INFOWAIT,true); //getInfo in 1.5 Seconds idle time } /** * Runs external program to get infos about the selected file */ void List::getInfo(){ ListItem* item = (ListItem*) selectedItem (); if( item == NULL) return; QString cmd = item->substArgs(config->getOption("infotool")); QProcess p; p.addArgument("/bin/sh"); p.addArgument("-c"); p.addArgument(cmd); p.setWorkingDirectory(currentpath); if(p.start()){ while(p.isRunning()){} //block for return emit setInfo(QString(p.readStdout())); }else{ qDebug("getting Info failed"); emit setInfo(""); } }