Commit b1933ed2 authored by Michael Pyne's avatar Michael Pyne

This commit changes the following:

* Use KDirLister as suggested by dfaure to track changes to the folders we're watching.  This allows us to track deletion and refreshes in addition to additions.
* Shave about 20% off of the time needed to initialize the tree view mode.
* Correct my spacing errors in playlistbox.cpp
* Change the map used to hold the tree view text tags to a hash table, which should be faster as well.

It's all in one commit because I'm crappy at patch management. =D

svn path=/trunk/kdemultimedia/juk/; revision=342054
parent 3970314b
......@@ -21,6 +21,7 @@
#include <kiconloader.h>
#include <kconfig.h>
#include <kaction.h>
#include <kurl.h>
#include "collectionlist.h"
#include "playlistcollection.h"
......@@ -124,9 +125,46 @@ void CollectionList::setupTreeViewEntries(ViewMode *viewMode) const
columnList << PlaylistItem::GenreColumn;
columnList << PlaylistItem::AlbumColumn;
for(QValueList<int>::Iterator colIt = columnList.begin(); colIt != columnList.end(); ++colIt)
QStringList items;
for(QValueList<int>::Iterator colIt = columnList.begin(); colIt != columnList.end(); ++colIt) {
items.clear();
for(TagCountDictIterator it(*m_columnTags[*colIt]); it.current(); ++it)
treeViewMode->slotAddItem(it.currentKey(), *colIt);
items << it.currentKey();
treeViewMode->slotAddItems(items, *colIt);
}
}
void CollectionList::slotNewItems(const KFileItemList &items, bool importPlaylists)
{
QStringList files;
for(KFileItemListIterator it(items); it.current(); ++it)
files.append((*it)->url().path());
addFiles(files, importPlaylists);
update();
}
void CollectionList::slotRefreshItems(const KFileItemList &items)
{
for(KFileItemListIterator it(items); it.current(); ++it) {
CollectionListItem *item = lookup((*it)->url().path());
if(item) {
item->refreshFromDisk();
item->repaint();
}
}
update();
}
void CollectionList::slotDeleteItem(KFileItem *item)
{
CollectionListItem *listItem = lookup(item->url().path());
if(listItem)
clearItem(listItem);
}
////////////////////////////////////////////////////////////////////////////////
......@@ -180,11 +218,7 @@ CollectionList::CollectionList(PlaylistCollection *collection) :
{
new KAction(i18n("Show Playing"), KShortcut(), actions(), "showPlaying");
m_dirWatch = new KDirWatch;
connect(m_dirWatch, SIGNAL(deleted(const QString &)), this, SLOT(slotRemoveItem(const QString &)));
connect(m_dirWatch, SIGNAL(dirty(const QString &)), this, SLOT(slotRefreshItem(const QString &)));
connect(action("showPlaying"), SIGNAL(activated()), this, SLOT(slotShowPlaying()));
m_dirWatch->startScan();
connect(action<KToolBarPopupAction>("back")->popupMenu(), SIGNAL(aboutToShow()),
this, SLOT(slotPopulateBackMenu()));
......@@ -217,8 +251,6 @@ CollectionList::~CollectionList()
clearItems(items());
for(TagCountDicts::Iterator it = m_columnTags.begin(); it != m_columnTags.end(); ++it)
delete *it;
delete m_dirWatch;
}
void CollectionList::contentsDropEvent(QDropEvent *e)
......
......@@ -18,6 +18,7 @@
#include <kapplication.h>
#include <kdirwatch.h>
#include <kfileitem.h>
#include <qdict.h>
#include <qclipboard.h>
......@@ -99,6 +100,10 @@ public slots:
void slotRemoveItem(const QString &file);
void slotRefreshItem(const QString &file);
void slotNewItems(const KFileItemList &items, bool importPlaylists);
void slotRefreshItems(const KFileItemList &items);
void slotDeleteItem(KFileItem *item);
protected:
CollectionList(PlaylistCollection *collection);
virtual ~CollectionList();
......
......@@ -132,14 +132,8 @@ PlaylistBox::PlaylistBox(QWidget *parent, QWidgetStack *playlistStack,
// We need to wait until after Collection List is created to set this up.
setupUpcomingPlaylist();
setSorting(-1); // Disable sorting for speed
performTreeViewSetup();
setSorting(0);
sort();
connect(CollectionList::instance(), SIGNAL(signalNewTag(const QString &, unsigned)),
this, SLOT(slotAddItem(const QString &, unsigned)));
connect(CollectionList::instance(), SIGNAL(signalRemovedTag(const QString &, unsigned)),
......@@ -148,6 +142,7 @@ PlaylistBox::PlaylistBox(QWidget *parent, QWidgetStack *playlistStack,
this, SLOT(slotPlaylistDestroyed(Playlist*)));
QTimer::singleShot(0, object(), SLOT(slotScanFolders()));
enableDirWatch(true);
}
PlaylistBox::~PlaylistBox()
......@@ -349,7 +344,7 @@ void PlaylistBox::slotPlaylistDestroyed(Playlist *p)
void PlaylistBox::slotAddItem(const QString &tag, unsigned column)
{
if(m_treeViewSetup)
static_cast<TreeViewMode*>(m_viewModes[2])->slotAddItem(tag, column);
static_cast<TreeViewMode*>(m_viewModes[2])->slotAddItems(tag, column);
}
void PlaylistBox::slotRemoveItem(const QString &tag, unsigned column)
......@@ -643,7 +638,11 @@ void PlaylistBox::performTreeViewSetup()
if(m_treeViewSetup || m_viewModeIndex != 2)
return;
setSorting(-1);
CollectionList::instance()->setupTreeViewEntries(m_viewModes[2]);
setSorting(0);
sort();
m_treeViewSetup = true;
}
......
......@@ -12,6 +12,8 @@
* *
***************************************************************************/
#include <kurl.h>
#include <config.h>
#include <qobjectlist.h>
......@@ -236,14 +238,14 @@ void PlaylistCollection::addFolder()
for(QStringList::Iterator it = result.addedDirs.begin();
it != result.addedDirs.end(); it++)
{
m_dirWatch.addDir(*it, false, true);
m_folderList.append(*it);
m_dirLister.openURL(*it, true);
m_folderList.append(*it);
}
for(QStringList::Iterator it = result.removedDirs.begin();
it != result.removedDirs.end(); it++)
{
m_dirWatch.removeDir(*it);
m_dirLister.stop(*it);
m_folderList.remove(*it);
}
......@@ -429,30 +431,30 @@ UpcomingPlaylist *PlaylistCollection::upcomingPlaylist() const
void PlaylistCollection::setUpcomingPlaylistEnabled(bool enable)
{
if((action<KToggleAction>("showUpcoming")->isChecked() && m_upcomingPlaylist) ||
(!enable && !m_upcomingPlaylist))
(!enable && !m_upcomingPlaylist))
{
return;
}
if(enable) {
action<KToggleAction>("showUpcoming")->setChecked(true);
if(!m_upcomingPlaylist)
m_upcomingPlaylist = new UpcomingPlaylist(this);
if(!m_upcomingPlaylist)
m_upcomingPlaylist = new UpcomingPlaylist(this);
m_upcomingPlaylist->initialize();
m_upcomingPlaylist->initialize();
setupPlaylist(m_upcomingPlaylist, "today");
}
else {
action<KToggleAction>("showUpcoming")->setChecked(false);
bool raiseCollection = m_playlistStack->visibleWidget() == m_upcomingPlaylist;
action<KToggleAction>("showUpcoming")->setChecked(false);
bool raiseCollection = m_playlistStack->visibleWidget() == m_upcomingPlaylist;
delete m_upcomingPlaylist;
m_upcomingPlaylist = 0;
if(raiseCollection) {
kapp->processEvents(); // Seems to stop a crash, weird.
raise(CollectionList::instance());
}
if(raiseCollection) {
kapp->processEvents(); // Seems to stop a crash, weird.
raise(CollectionList::instance());
}
}
}
......@@ -506,10 +508,17 @@ bool PlaylistCollection::containsPlaylistFile(const QString &file) const
void PlaylistCollection::enableDirWatch(bool enable)
{
if(enable)
m_dirWatch.startScan(false);
else
m_dirWatch.stopScan();
QObject *collection = CollectionList::instance();
m_dirLister.disconnect(object());
if(enable) {
QObject::connect(&m_dirLister, SIGNAL(newItems(const KFileItemList &)),
collection, SLOT(slotNewItems(const KFileItemList &)));
QObject::connect(&m_dirLister, SIGNAL(refreshItems(const KFileItemList &)),
collection, SLOT(slotRefreshItems(const KFileItemList &)));
QObject::connect(&m_dirLister, SIGNAL(deleteItem(KFileItem *)),
collection, SLOT(slotDeleteItem(KFileItem *)));
}
}
QString PlaylistCollection::playlistNameDialog(const QString &caption,
......@@ -595,16 +604,8 @@ void PlaylistCollection::readConfig()
m_importPlaylists = config.readBoolEntry("ImportPlaylists", true);
m_folderList = config.readPathListEntry("DirectoryList");
QObject::connect(&m_dirWatch, SIGNAL(dirty(const QString &)),
object(), SLOT(slotDirChanged(const QString &)));
for(QStringList::ConstIterator it = m_folderList.begin();
it != m_folderList.end(); ++it)
{
m_dirWatch.addDir(*it, false, true);
}
m_dirWatch.startScan();
for(QStringList::ConstIterator it = m_folderList.begin(); it != m_folderList.end(); ++it)
m_dirLister.openURL(*it, true);
}
void PlaylistCollection::saveConfig()
......
......@@ -20,10 +20,9 @@
#include "jukIface.h"
#include <kshortcut.h>
#include <kdirwatch.h>
#include <kconfig.h>
#include <klocale.h>
#include <kdirlister.h>
class QWidgetStack;
class KAction;
......@@ -142,7 +141,7 @@ private:
UpcomingPlaylist *m_upcomingPlaylist;
ActionHandler *m_actionHandler;
KDirWatch m_dirWatch;
KDirLister m_dirLister;
StringHash m_playlistNames;
StringHash m_playlistFiles;
QStringList m_folderList;
......
......@@ -14,10 +14,14 @@
***************************************************************************/
#include <kiconloader.h>
#include <kstandarddirs.h>
#include <kdebug.h>
#include <qpixmap.h>
#include <qpainter.h>
#include <qfile.h>
#include <qdir.h>
#include <qdatastream.h>
#include "viewmode.h"
#include "playlistbox.h"
......@@ -248,7 +252,7 @@ void CompactViewMode::updateHeights()
// TreeViewMode
////////////////////////////////////////////////////////////////////////////////
TreeViewMode::TreeViewMode(PlaylistBox *b) : CompactViewMode(b)
TreeViewMode::TreeViewMode(PlaylistBox *b) : CompactViewMode(b), m_treeViewItems(5003, false)
{
}
......@@ -287,17 +291,17 @@ void TreeViewMode::slotRemoveItem(const QString &item, unsigned column)
{
QString itemKey;
if(column == PlaylistItem::ArtistColumn)
itemKey = "artists" + item.lower();
itemKey = "artists" + item;
else if(column == PlaylistItem::GenreColumn)
itemKey = "genres" + item.lower();
itemKey = "genres" + item;
else if(column == PlaylistItem::AlbumColumn)
itemKey = "albums" + item.lower();
itemKey = "albums" + item;
else {
kdWarning() << k_funcinfo << "Unhandled column type " << column << endl;
return;
}
if(!m_treeViewItems.contains(itemKey))
if(!m_treeViewItems.find(itemKey))
return;
TreeViewItemPlaylist *itemPlaylist = m_treeViewItems[itemKey];
......@@ -306,12 +310,9 @@ void TreeViewMode::slotRemoveItem(const QString &item, unsigned column)
emit signalPlaylistDestroyed(itemPlaylist);
}
void TreeViewMode::slotAddItem(const QString &item, unsigned column)
void TreeViewMode::slotAddItems(const QStringList &items, unsigned column)
{
QValueList<int> columns;
columns.append(column);
QString itemKey, searchCategory;
QString searchCategory;
if(column == PlaylistItem::ArtistColumn)
searchCategory = "artists";
else if(column == PlaylistItem::GenreColumn)
......@@ -323,28 +324,38 @@ void TreeViewMode::slotAddItem(const QString &item, unsigned column)
return;
}
itemKey = searchCategory + item.lower();
if(m_treeViewItems.contains(itemKey))
return;
PlaylistSearch::ComponentList components;
QValueList<int> columns;
columns.append(column);
PlaylistSearch::Component::MatchMode mode = PlaylistSearch::Component::ContainsWord;
if(column != PlaylistItem::ArtistColumn)
mode = PlaylistSearch::Component::Exact;
components.append(PlaylistSearch::Component(item, true, columns, mode));
mode = PlaylistSearch::Component::Exact;
PlaylistSearch::ComponentList components;
PlaylistList playlists;
playlists.append(CollectionList::instance());
PlaylistSearch s(playlists, components, PlaylistSearch::MatchAny, false);
QString itemKey, item;
PlaylistBox::Item *itemParent = m_searchCategories[searchCategory];
for(QStringList::ConstIterator it = items.begin(); it != items.end(); ++it) {
item = *it;
itemKey = searchCategory + item;
TreeViewItemPlaylist *p = new TreeViewItemPlaylist(playlistBox(), s, item, false);
playlistBox()->setupPlaylist(p, "midi", m_searchCategories[searchCategory]);
if(m_treeViewItems.find(itemKey))
continue;
components.clear();
components.append(PlaylistSearch::Component(item, true, columns, mode));
m_treeViewItems[itemKey] = p;
PlaylistSearch s(playlists, components, PlaylistSearch::MatchAny, false);
TreeViewItemPlaylist *p = new TreeViewItemPlaylist(playlistBox(), s, item, false);
playlistBox()->setupPlaylist(p, "midi", itemParent);
m_treeViewItems.insert(itemKey, p);
}
}
void TreeViewMode::setupCategories()
......@@ -360,9 +371,6 @@ void TreeViewMode::setupCategories()
i = new PlaylistBox::Item(collectionItem, "cdimage", i18n("Genres"));
m_searchCategories.insert("genres", i);
for(QDictIterator<PlaylistBox::Item> it(m_searchCategories); it.current(); ++it)
it.current()->setSortedFirst(true);
}
#include "viewmode.moc"
......@@ -106,14 +106,14 @@ public:
public slots:
void slotRemoveItem(const QString &item, unsigned column);
void slotAddItem(const QString &item, unsigned column);
void slotAddItems(const QStringList &items, unsigned column);
signals:
void signalPlaylistDestroyed(Playlist*);
private:
QDict<PlaylistBox::Item> m_searchCategories;
QMap<QString, TreeViewItemPlaylist*> m_treeViewItems;
QDict<TreeViewItemPlaylist> m_treeViewItems;
};
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment