filelist.cpp 4.64 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/***************************************************************************
                          filelist.cpp  -  description
                             -------------------
    begin                : Sat Feb 16 2002
    copyright            : (C) 2002 by Scott Wheeler
    email                : scott@slackorama.net
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program 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.                                   *
 *                                                                         *
 ***************************************************************************/

#include <klocale.h>

#include <qfileinfo.h>
#include <qdir.h>
#include <qtimer.h>
#include <qapplication.h>
24
#include <qptrlist.h> 
25 26 27 28 29 30 31

#include "filelist.h"

////////////////////////////////////////////////////////////////////////////////
// public members
////////////////////////////////////////////////////////////////////////////////

32
FileList::FileList(QWidget *parent, const char *name) : KListView(parent, name)
33
{
34
  processed = 0;
35 36 37
  setup();
}

38
FileList::FileList(QString item, QWidget *parent, const char *name) : KListView(parent, name)
39
{
40
  processed = 0;
41 42 43 44
  setup();
  append(item);
}

45
FileList::FileList(QStringList *items, QWidget *parent, const char *name) : KListView(parent, name)
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
{
  setup();
  append(items);
}

FileList::~FileList()
{
}

void FileList::append(QString item)
{
  QApplication::setOverrideCursor(Qt::waitCursor);
  appendImpl(item);
  QApplication::restoreOverrideCursor();
}

void FileList::append(QStringList *items)
{
  QApplication::setOverrideCursor(Qt::waitCursor);
65
  for(QStringList::Iterator it = items->begin(); it != items->end(); ++it)
66 67 68 69
    appendImpl(*it);
  QApplication::restoreOverrideCursor();
}

70 71
void FileList::append(FileListItem *item)
{
72 73
  if(item && members.contains(item->absFilePath()) == 0) {
    members.append(item->absFilePath());
74
    (void) new FileListItem(*item, this);
75
  }
76 77 78 79
}

void FileList::append(QPtrList<QListViewItem> *items)
{
80 81 82 83 84
  QPtrListIterator<QListViewItem> it(*items);
  while(it.current()) {
    append(dynamic_cast<FileListItem *>(it.current()));
    ++it;
  }
85 86
}

87 88 89 90
void FileList::remove(QPtrList<QListViewItem> *items)
{
  QPtrListIterator<QListViewItem> it(*items);
  while(it.current()) {
91
    members.remove(static_cast<FileListItem *>(it.current())->absFilePath());
92 93 94 95 96
    delete(it.current());    
    ++it;
  }
}

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
FileListItem *FileList::getSelectedItem()
{
  return(dynamic_cast<FileListItem *>(currentItem()));
}

////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

void FileList::setup()
{
  extensions.append("mp3");

  addColumn(i18n("Track Name"));
  addColumn(i18n("Artist"));
  addColumn(i18n("Album"));
  addColumn(i18n("Track"));
  addColumn(i18n("Genre"));
  addColumn(i18n("Year"));
116
  addColumn(i18n("Length"));
117
  addColumn(i18n("File Name"));
118

119
  setAllColumnsShowFocus(true);
120
  setSelectionMode(QListView::Extended);
121 122 123 124 125 126 127 128
  setShowSortIndicator(true);
  setItemMargin(3);

  setSorting(1);
}

void FileList::appendImpl(QString item)
{
129
  processEvents();
130 131 132 133
  QFileInfo file(QDir::cleanDirPath(item));
  if(file.exists()) {
    if(file.isDir()) {
      QDir dir(file.filePath());
134 135 136
      QStringList dirContents=dir.entryList();
      for(QStringList::Iterator it = dirContents.begin(); it != dirContents.end(); ++it) {
        if(*it != "." && *it != "..") {
137
          appendImpl(file.filePath() + QDir::separator() + *it);
138 139 140 141
        }
      }
    }
    else {
142
      // QFileInfo::extension() doesn't always work, so I'm getting old-school on this. -- fixed in Qt 3
143 144 145 146
      // QString extension = file.filePath().right(file.filePath().length() - (file.filePath().findRev(".") + 1));
      QString extension = file.extension(false);
      if(extensions.contains(extension) > 0 && members.contains(file.absFilePath()) == 0) {
        members.append(file.absFilePath());
147 148 149 150 151
	(void) new FileListItem(file, this);
      }
    }
  }
}
152 153 154 155 156 157 158 159 160 161 162

////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////

void FileList::processEvents()
{
  if(processed == 0)
    qApp->processEvents();
  processed = ( processed + 1 ) % 10;
}