Amarok – versatile and easy to use audio player
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
amarok/amarok/src/collectionbrowser.cpp

4719 lines
166 KiB

// (c) 2004 Mark Kretschmann <markey@web.de>
// (c) 2004 Christian Muehlhaeuser <chris@chris.de>
// (c) 2005 GÁbor Lehel <illissius@gmail.com>
// (c) 2005 Alexandre Pereira de Oliveira <aleprj@gmail.com>
// (c) 2005 Christan Baumgart <christianbaumgart@web.de>
// (c) 2006 Joe Rabinoff <bobqwatson@yahoo.com>
// See COPYING file for licensing information.
#include <config.h>
#include "amarok.h"
#include "amarokconfig.h"
#include "browserbar.h"
#include "browserToolBar.h"
#include "clicklineedit.h"
#include "collectionbrowser.h"
#include "collectiondb.h"
#include "covermanager.h"
#include "debug.h"
#include "deletedialog.h"
#include "directorylist.h"
#include "editfilterdialog.h"
#include "k3bexporter.h"
#include "mediabrowser.h"
#include "metabundle.h"
#include "mountpointmanager.h"
#include "organizecollectiondialog.h"
#include "playlist.h" //insertMedia()
#include "playlistbrowser.h"
#include "starmanager.h"
#include "statusbar.h"
#include "tagdialog.h"
#include "threadmanager.h"
#include "qstringx.h"
#include <taglib/tfile.h> //TagLib::File::isWritable
#include <unistd.h> //CollectionView ctor
#include <tqapplication.h>
#include <tqcstring.h>
#include <tqdragobject.h>
#include <tqlayout.h> //infobox
#include <tqmap.h>
#include <tqpainter.h>
#include <tqpixmap.h>
#include <tqptrlist.h>
#include <tqpushbutton.h>
#include <tqsimplerichtext.h>
#include <tqtimer.h>
#include <tqtooltip.h> //TQToolTip::add()
#include <tqheader.h>
#include <tqregexp.h>
#include <tdeactioncollection.h>
#include <tdeapplication.h> //kapp
#include <tdeconfig.h>
#include <kcombobox.h>
#include <kcursor.h>
#include <kdialogbase.h>
#include <tdeglobal.h>
#include <kiconloader.h> //renderView()
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <tdepopupmenu.h>
#include <tdetoolbarbutton.h> //ctor
#include <kurldrag.h> //dragObject()
#include <tdeio/job.h>
#include <kpushbutton.h>
extern "C"
{
#if TDE_VERSION < TDE_MAKE_VERSION(3,3,91)
#include <X11/Xlib.h> //ControlMask in contentsDragMoveEvent()
#endif
}
using namespace CollectionBrowserIds;
namespace Amarok { extern TDEConfig *config( const TQString& ); }
class CoverFetcher;
CollectionBrowser *CollectionBrowser::s_instance = 0;
CollectionBrowser::CollectionBrowser( const char* name )
: TQVBox( 0, name )
, m_cat1Menu( new TDEPopupMenu( this ) )
, m_cat2Menu( new TDEPopupMenu( this ) )
, m_cat3Menu( new TDEPopupMenu( this ) )
, m_timer( new TQTimer( this ) )
, m_returnPressed( false )
{
s_instance = this;
setSpacing( 4 );
m_toolbar = new Browser::ToolBar( this );
{ //<Search LineEdit>
TDEToolBarButton *button;
TDEToolBar* searchToolBar = new Browser::ToolBar( this );
button = new TDEToolBarButton( "locationbar_erase", 0, searchToolBar );
m_searchEdit = new ClickLineEdit( i18n( "Enter search terms here" ), searchToolBar );
m_searchEdit->installEventFilter( this );
KPushButton *filterButton = new KPushButton("...", searchToolBar, "filter");
searchToolBar->setStretchableWidget( m_searchEdit );
m_searchEdit->setFrame( TQFrame::Sunken );
connect( button, TQT_SIGNAL( clicked() ), TQT_SLOT( slotClearFilter() ) );
connect( filterButton, TQT_SIGNAL( clicked() ), TQT_SLOT( slotEditFilter() ) );
TQToolTip::add( button, i18n( "Clear search field" ) );
TQToolTip::add( m_searchEdit, i18n( "Enter space-separated terms to search in the collection" ) );
TQToolTip::add( filterButton, i18n( "Click to edit collection filter" ) );
} //</Search LineEdit>
// We put a little toolbar for the forward/back buttons for iPod
// navigation to the right of m_timeFilter. This toolbar is
// hidden when not in iPod browsing mode; it is shown and hidden
// in CollectionView::setViewMode(). m_ipodHbox holds m_timeFilter
// and m_ipodToolbar
m_ipodHbox = new TQHBox( this );
m_ipodHbox->setSpacing( 7 ); // looks better
m_timeFilter = new KComboBox( m_ipodHbox, "timeFilter" );
m_ipodHbox->setStretchFactor( m_timeFilter, 1 );
// Allow the combobox to shrink so the iPod buttons are still visible
m_timeFilter->setSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Fixed );
m_timeFilter->insertItem( i18n( "Entire Collection" ) );
m_timeFilter->insertItem( i18n( "Added Today" ) );
m_timeFilter->insertItem( i18n( "Added Within One Week" ) );
m_timeFilter->insertItem( i18n( "Added Within One Month" ) );
m_timeFilter->insertItem( i18n( "Added Within Three Months" ) );
m_timeFilter->insertItem( i18n( "Added Within One Year" ) );
// m_ipodToolbar just holds the forward and back buttons, which are
// plugged below
m_ipodToolbar = new Browser::ToolBar( m_ipodHbox );
m_ipodHbox->setStretchFactor( m_ipodToolbar, 0 );
m_ipodToolbar->setIconText( TDEToolBar::IconOnly, false );
TDEActionCollection* ac = new TDEActionCollection( this );
m_view = new CollectionView( this );
m_view->installEventFilter( this );
m_configureAction = new TDEAction( i18n( "Configure Folders" ), Amarok::icon( "configure" ), 0, TQT_TQOBJECT(this), TQT_SLOT( setupDirs() ), ac, "Configure" );
m_treeViewAction = new TDERadioAction( i18n( "Tree View" ), "view_tree", 0, TQT_TQOBJECT(m_view), TQT_SLOT( setTreeMode() ), ac, "Tree View" );
m_flatViewAction = new TDERadioAction( i18n( "Flat View" ), "view_detailed", 0, TQT_TQOBJECT(m_view), TQT_SLOT( setFlatMode() ), ac, "Flat View" );
m_ipodViewAction = new TDERadioAction( i18n( "iPod View" ), Amarok::icon("device"), 0, TQT_TQOBJECT(m_view), TQT_SLOT( setIpodMode() ), ac, "iPod View" );
m_treeViewAction->setExclusiveGroup("view mode");
m_flatViewAction->setExclusiveGroup("view mode");
m_ipodViewAction->setExclusiveGroup("view mode");
switch( m_view->m_viewMode )
{
case CollectionView::modeTreeView:
m_treeViewAction->setChecked( true );
break;
case CollectionView::modeFlatView:
m_flatViewAction->setChecked( true );
break;
case CollectionView::modeIpodView:
m_ipodViewAction->setChecked( true );
break;
}
m_showDividerAction = new TDEToggleAction( i18n( "Show Divider" ), "format-justify-left", 0, TQT_TQOBJECT(this), TQT_SLOT( toggleDivider() ), ac, "Show Divider" );
m_showDividerAction->setChecked(m_view->m_showDivider);
// m_ipodIncrement and m_ipodDecrement are the actions that
// correspond to moving forward / backward in the iPod collection
// browser window; see the "For iPod-style navigation" comments below.
m_ipodDecrement = new TDEAction( i18n( "Browse backward" ),
TQIconSet( m_view->ipodDecrementIcon(), TQIconSet::Small ),
0, TQT_TQOBJECT(m_view), TQT_SLOT( decrementDepth() ), ac,
"iPod Decrement" );
m_ipodIncrement = new TDEAction( i18n( "Browse forward" ),
TQIconSet( m_view->ipodIncrementIcon(), TQIconSet::Small ),
0, TQT_TQOBJECT(m_view), TQT_SLOT( incrementDepth() ), ac,
"iPod Increment" );
m_ipodDecrement->plug( m_ipodToolbar );
m_ipodIncrement->plug( m_ipodToolbar );
// Show / hide m_ipodToolbar based on the view mode
ipodToolbar( m_view->m_viewMode == CollectionView::modeIpodView );
m_tagfilterMenuButton = new TDEActionMenu( i18n( "Group By" ), "filter", ac );
m_tagfilterMenuButton->setDelayed( false );
// FIXME: either both or nothing
//m_tagfilterMenuButton->setEnabled( m_view->m_viewMode == CollectionView::modeTreeView );
//connect ( m_treeViewAction, TQT_SIGNAL ( toggled(bool) ), m_tagfilterMenuButton, TQT_SLOT( setEnabled (bool) ) );
layoutToolbar();
m_categoryMenu = m_tagfilterMenuButton->popupMenu();
m_categoryMenu->insertItem( i18n( "Artist" ), m_view, TQT_SLOT( presetMenu( int ) ), 0, IdArtist );
m_categoryMenu->insertItem( i18n( "Artist / Album" ), m_view, TQT_SLOT( presetMenu( int ) ), 0, IdArtistAlbum );
m_categoryMenu->insertItem( i18n( "Artist" )+" / "+ i18n( "Year" ) + i18n( " - " ) + i18n( "Album" ), m_view, TQT_SLOT( presetMenu( int ) ), 0, IdArtistVisYearAlbum );
m_categoryMenu->insertItem( i18n( "Album" ), m_view, TQT_SLOT( presetMenu( int ) ), 0, IdAlbum );
m_categoryMenu->insertItem( i18n( "Genre / Artist" ), m_view, TQT_SLOT( presetMenu( int ) ), 0, IdGenreArtist );
m_categoryMenu->insertItem( i18n( "Genre / Artist / Album" ), m_view, TQT_SLOT( presetMenu( int ) ), 0, IdGenreArtistAlbum );
m_categoryMenu->insertSeparator();
m_categoryMenu->insertItem( i18n( "&First Level" ), m_cat1Menu );
m_categoryMenu->insertItem( i18n( "&Second Level"), m_cat2Menu );
m_categoryMenu->insertItem( i18n( "&Third Level" ), m_cat3Menu );
m_cat1Menu ->insertItem( i18n( "&Album" ), m_view, TQT_SLOT( cat1Menu( int ) ), 0, IdAlbum );
m_cat1Menu ->insertItem( i18n( "(Y&ear) - Album" ), m_view, TQT_SLOT( cat1Menu( int ) ), 0, IdVisYearAlbum);
m_cat1Menu ->insertItem( i18n( "A&rtist"), m_view, TQT_SLOT( cat1Menu( int ) ), 0, IdArtist );
m_cat1Menu ->insertItem( i18n( "&Composer"), m_view, TQT_SLOT( cat1Menu( int ) ), 0, IdComposer );
m_cat1Menu ->insertItem( i18n( "&Genre" ), m_view, TQT_SLOT( cat1Menu( int ) ), 0, IdGenre );
m_cat1Menu ->insertItem( i18n( "&Year" ), m_view, TQT_SLOT( cat1Menu( int ) ), 0, IdYear );
m_cat1Menu ->insertItem( i18n( "&Label" ), m_view, TQT_SLOT( cat1Menu( int ) ), 0, IdLabel );
m_cat2Menu ->insertItem( i18n( "&None" ), m_view, TQT_SLOT( cat2Menu( int ) ), 0, IdNone );
m_cat2Menu ->insertSeparator();
m_cat2Menu ->insertItem( i18n( "&Album" ), m_view, TQT_SLOT( cat2Menu( int ) ), 0, IdAlbum );
m_cat2Menu ->insertItem( i18n( "(Y&ear) - Album" ), m_view, TQT_SLOT( cat2Menu( int ) ), 0, IdVisYearAlbum);
m_cat2Menu ->insertItem( i18n( "A&rtist" ), m_view, TQT_SLOT( cat2Menu( int ) ), 0, IdArtist );
m_cat2Menu ->insertItem( i18n( "&Composer"), m_view, TQT_SLOT( cat2Menu( int ) ), 0, IdComposer );
m_cat2Menu ->insertItem( i18n( "&Genre" ), m_view, TQT_SLOT( cat2Menu( int ) ), 0, IdGenre );
m_cat2Menu ->insertItem( i18n( "&Year" ), m_view, TQT_SLOT( cat2Menu( int ) ), 0, IdYear );
m_cat2Menu ->insertItem( i18n( "&Label" ), m_view, TQT_SLOT( cat2Menu( int ) ), 0, IdLabel );
m_cat3Menu ->insertItem( i18n( "&None" ), m_view, TQT_SLOT( cat3Menu( int ) ), 0, IdNone );
m_cat3Menu ->insertSeparator();
m_cat3Menu ->insertItem( i18n( "A&lbum" ), m_view, TQT_SLOT( cat3Menu( int ) ), 0, IdAlbum );
m_cat3Menu ->insertItem( i18n( "(Y&ear) - Album" ), m_view, TQT_SLOT( cat3Menu( int ) ), 0, IdVisYearAlbum);
m_cat3Menu ->insertItem( i18n( "A&rtist" ), m_view, TQT_SLOT( cat3Menu( int ) ), 0, IdArtist );
m_cat3Menu ->insertItem( i18n( "&Composer"), m_view, TQT_SLOT( cat3Menu( int ) ), 0, IdComposer );
m_cat3Menu ->insertItem( i18n( "&Genre" ), m_view, TQT_SLOT( cat3Menu( int ) ), 0, IdGenre );
m_cat3Menu ->insertItem( i18n( "&Year" ), m_view, TQT_SLOT( cat3Menu( int ) ), 0, IdYear );
m_cat3Menu ->insertItem( i18n( "&Label" ), m_view, TQT_SLOT( cat3Menu( int ) ), 0, IdLabel );
m_view->cat1Menu( m_view->m_cat1, false );
m_view->cat2Menu( m_view->m_cat2, false );
m_view->cat3Menu( m_view->m_cat3, false );
m_view->setViewMode( m_view->m_viewMode );
connect( m_timer, TQT_SIGNAL( timeout() ), TQT_SLOT( slotSetFilter() ) );
connect( m_searchEdit, TQT_SIGNAL( textChanged( const TQString& ) ), TQT_SLOT( slotSetFilterTimeout() ) );
connect( m_timeFilter, TQT_SIGNAL( activated( int ) ), TQT_SLOT( slotSetFilter() ) );
setFocusProxy( m_view ); //default object to get focus
}
void
CollectionBrowser::slotClearFilter() //SLOT
{
m_searchEdit->clear();
kapp->processEvents(); //Let the search bar redraw fully.
TQTimer::singleShot( 0, this, TQT_SLOT( slotSetFilter() ) ); //Filter instantly
TQTimer::singleShot( 0, m_view, TQT_SLOT( slotEnsureSelectedItemVisible() ) );
}
void
CollectionBrowser::slotSetFilterTimeout() //SLOT
{
m_returnPressed = false;
m_timer->start( 280, true ); //stops the timer for us first
}
void
CollectionBrowser::slotSetFilter() //SLOT
{
m_timer->stop();
m_view->m_dirty = true;
m_view->setFilter( m_searchEdit->text() );
m_view->setTimeFilter( m_timeFilter->currentItem() );
m_view->renderView();
if ( m_returnPressed )
appendSearchResults();
m_returnPressed = false;
}
void
CollectionBrowser::slotSetFilter( const TQString &filter ) //SLOT
{
m_searchEdit->setText( filter );
kapp->processEvents(); //Let the search bar redraw fully.
TQTimer::singleShot( 0, this, TQT_SLOT( slotSetFilter() ) ); //Filter instantly
TQTimer::singleShot( 0, m_view, TQT_SLOT( slotEnsureSelectedItemVisible() ) );
}
void
CollectionBrowser::slotEditFilter() //SLOT
{
EditFilterDialog *cod = new EditFilterDialog( this, false, m_searchEdit->text() );
connect( cod, TQT_SIGNAL(filterChanged(const TQString &)), TQT_SLOT(slotSetFilter(const TQString &)) );
if( cod->exec() )
m_searchEdit->setText( cod->filter() );
delete cod;
}
void
CollectionBrowser::setupDirs() //SLOT
{
m_view->setupDirs();
}
void
CollectionBrowser::toggleDivider() //SLOT
{
m_view->setShowDivider( m_showDividerAction->isChecked() );
}
void
CollectionBrowser::appendSearchResults()
{
//If we are not filtering, or the search string has changed recently, do nothing
if ( m_searchEdit->text().stripWhiteSpace().isEmpty() || m_timer->isActive() )
return;
m_view->selectAll();
Playlist::instance()->insertMedia( m_view->listSelected(), Playlist::Unique | Playlist::Append );
m_view->clearSelection();
slotClearFilter();
}
bool
CollectionBrowser::eventFilter( TQObject *o, TQEvent *e )
{
switch( e->type() )
{
case 6/*TQEvent::KeyPress*/:
//there are a few keypresses that we intercept
#define e TQT_TQKEYEVENT(e)
if( TQT_BASE_OBJECT(o) == TQT_BASE_OBJECT(m_searchEdit) ) //the search lineedit
{
switch( e->key() )
{
case Key_Up:
case Key_Down:
case Key_PageDown:
case Key_PageUp:
m_view->setFocus();
TQApplication::sendEvent( m_view, e );
return true;
case Key_Escape:
slotClearFilter();
return true;
case Key_Return:
case Key_Enter:
if ( m_timer->isActive() )
{
//Immediately filter and add results
m_timer->stop();
m_returnPressed = true;
TQTimer::singleShot( 0, this, TQT_SLOT( slotSetFilter() ) );
}
else
{
//Add current results
appendSearchResults();
}
return true;
default:
return false;
}
}
// (Joe Rabinoff) the code that was here which dealt with wrapping
// the selection around when Key_Up or Key_Down was pressed was
// moved to CollectionView::keyPressEvent(). That code also
// skips dividers.
if( ( e->key() >= Key_0 && e->key() <= Key_Z ) || e->key() == Key_Backspace || e->key() == Key_Escape )
{
m_searchEdit->setFocus();
TQApplication::sendEvent( m_searchEdit, e );
return true;
}
#undef e
break;
default:
break;
}
return TQVBox::eventFilter( o, e );
}
void
CollectionBrowser::layoutToolbar()
{
if ( !m_toolbar ) return;
m_toolbar->clear();
m_toolbar->setIconText( TDEToolBar::IconTextRight, false );
m_tagfilterMenuButton->plug( m_toolbar );
m_toolbar->setIconText( TDEToolBar::IconOnly, false );
m_toolbar->insertLineSeparator();
m_treeViewAction->plug( m_toolbar );
m_flatViewAction->plug( m_toolbar );
m_ipodViewAction->plug( m_toolbar );
m_toolbar->insertLineSeparator();
m_showDividerAction->plug( m_toolbar );
m_configureAction->plug( m_toolbar );
//This would break things if the toolbar is too big, see bug #121915
//setMinimumWidth( m_toolbar->sizeHint().width() + 2 ); //set a reasonable minWidth
}
// (De)activate the iPod toolbar when switching into and out of
// iPod browsing mode
void
CollectionBrowser::ipodToolbar( bool activate )
{
if( activate )
m_ipodToolbar->show();
else
m_ipodToolbar->hide();
}
//////////////////////////////////////////////////////////////////////////////////////////
// CLASS CollectionView
//////////////////////////////////////////////////////////////////////////////////////////
CollectionView* CollectionView::m_instance = 0;
CollectionView::CollectionView( CollectionBrowser* parent )
: TDEListView( parent )
, m_parent( parent )
, m_timeFilter( 0 )
, m_currentDepth( 0 )
, m_ipodIncremented ( 1 )
, m_dirty( true )
, m_organizingFileCancelled( false )
{
DEBUG_FUNC_INFO
m_instance = this;
setSelectionMode( TQListView::Extended );
setItemsMovable( false );
setSorting( 0 );
setShowSortIndicator( true );
setAcceptDrops( true );
setAllColumnsShowFocus( true );
//<READ CONFIG>
TDEConfig* config = Amarok::config( "Collection Browser" );
m_cat1 = config->readNumEntry( "Category1", IdArtist );
m_cat2 = config->readNumEntry( "Category2", IdAlbum );
m_cat3 = config->readNumEntry( "Category3", IdNone );
#define saneCat(x) (x==IdAlbum||x==IdArtist||x==IdComposer||x==IdGenre||x==IdYear \
||x==IdNone \
||x==IdArtistAlbum||x==IdGenreArtist||x==IdGenreArtistAlbum||x==IdVisYearAlbum||x==IdArtistVisYearAlbum)
if( !saneCat(m_cat1) )
{
m_cat1 = IdArtist;
m_cat2 = IdAlbum;
m_cat2 = IdNone;
}
if( !saneCat(m_cat2) || !saneCat(m_cat3) )
{
m_cat2 = m_cat3 = IdNone;
}
#undef saneCat
m_viewMode = config->readNumEntry( "ViewMode", modeTreeView );
m_showDivider = config->readBoolEntry( "ShowDivider", true);
updateTrackDepth();
m_flatColumnWidths.clear();
TQStringList flatWidths = config->readListEntry( "FlatColumnWidths" );
for( TQStringList::iterator it = flatWidths.begin();
it != flatWidths.end();
it++ )
m_flatColumnWidths.push_back( (*it).toInt() );
//</READ CONFIG>
TDEActionCollection* ac = new TDEActionCollection( this );
KStdAction::selectAll( TQT_TQOBJECT(this), TQT_SLOT( selectAll() ), ac, "collectionview_select_all" );
connect( CollectionDB::instance(), TQT_SIGNAL( scanStarted() ),
this, TQT_SLOT( scanStarted() ) );
connect( CollectionDB::instance(), TQT_SIGNAL( scanDone( bool ) ),
this, TQT_SLOT( scanDone( bool ) ) );
connect( BrowserBar::instance(), TQT_SIGNAL( browserActivated( int ) ),
this, TQT_SLOT( renderView() ) ); // renderView() checks if current tab is this
connect( CollectionDB::instance(), TQT_SIGNAL( ratingChanged( const TQString&, int ) ),
this, TQT_SLOT( ratingChanged( const TQString&, int ) ) );
connect( this, TQT_SIGNAL( expanded( TQListViewItem* ) ),
this, TQT_SLOT( slotExpand( TQListViewItem* ) ) );
connect( this, TQT_SIGNAL( collapsed( TQListViewItem* ) ),
this, TQT_SLOT( slotCollapse( TQListViewItem* ) ) );
connect( this, TQT_SIGNAL( returnPressed( TQListViewItem* ) ),
this, TQT_SLOT( invokeItem( TQListViewItem* ) ) );
connect( this, TQT_SIGNAL( doubleClicked( TQListViewItem*, const TQPoint&, int ) ),
this, TQT_SLOT( invokeItem( TQListViewItem*, const TQPoint&, int ) ) );
connect( this, TQT_SIGNAL( clicked( TQListViewItem*, const TQPoint&, int ) ),
this, TQT_SLOT( ipodItemClicked( TQListViewItem*, const TQPoint&, int ) ) );
connect( this, TQT_SIGNAL( contextMenuRequested( TQListViewItem*, const TQPoint&, int ) ),
this, TQT_SLOT( rmbPressed( TQListViewItem*, const TQPoint&, int ) ) );
connect( header(), TQT_SIGNAL( sizeChange( int, int, int ) ),
this, TQT_SLOT( triggerUpdate() ) );
connect( MountPointManager::instance(), TQT_SIGNAL( mediumConnected( int ) ),
this, TQT_SLOT( databaseChanged() ) );
connect( MountPointManager::instance(), TQT_SIGNAL( mediumRemoved( int ) ),
this, TQT_SLOT( databaseChanged() ) );
}
CollectionView::~CollectionView() {
DEBUG_FUNC_INFO
TDEConfig* const config = Amarok::config( "Collection Browser" );
config->writeEntry( "Category1", m_cat1 );
config->writeEntry( "Category2", m_cat2 );
config->writeEntry( "Category3", m_cat3 );
config->writeEntry( "ViewMode", m_viewMode );
config->writeEntry( "ShowDivider", m_showDivider );
TQStringList flatWidths;
for( TQValueList<int>::iterator it = m_flatColumnWidths.begin();
it != m_flatColumnWidths.end();
it++ )
flatWidths.push_back( TQString::number( (*it) ) );
config->writeEntry( "FlatColumnWidths", flatWidths );
}
void
CollectionView::setShowDivider( bool show )
{
if (show != m_showDivider) {
m_showDivider = show;
renderView(true);
}
}
// Reimplemented for iPod-style navigation, and to skip dividers
// Specifically, this method traps the Key_Up/Down/Left/Right events.
// When Up or Down is pressed, it skips dividers and wraps around when
// necessary. When Left or Right is pressed and we are viewing in
// iPod mode, the iPod "move forward / backward" actions are activated.
void
CollectionView::keyPressEvent( TQKeyEvent *e )
{
typedef TQListViewItemIterator It;
// Reimplement up and down to skip dividers and to loop around.
// Some of this code used to be in CollectionBrowser::eventFilter.
// This rewritten code is more faithful to the ordinary moving
// behavior, even when looping around. (For instance, it behaves
// correctly if control-up is pressed at the top of the screen.)
// It sends fake keypress events to the parent instead of programatically
// selecting items.
if( (e->key() == Key_Up || e->key() == Key_Down ) && currentItem() )
{
// Handle both up and down at once to avoid code duplication (it's
// a delicate piece of logic, and was hard to get right)
TQListViewItem *cur = currentItem();
#define nextItem (e->key() == Key_Up ? cur->itemAbove() : cur->itemBelow())
bool wraparound = true;
// First skip any dividers directly above / below
do
{
TDEListView::keyPressEvent( e );
if( currentItem() == cur ) // Prevent infinite loops
{
if( nextItem != 0 )
wraparound = false;
break;
}
cur = currentItem();
if( cur && dynamic_cast<DividerItem*>( cur ) == 0 )
wraparound = false; // Found an item above / below
} while( cur != NULL
&& dynamic_cast<DividerItem*>(cur) != 0
&& nextItem != 0 );
if( cur == 0 ) return; // Shouldn't happen
// Wrap around if necessary, by sending a Key_Home/Key_End event.
if( wraparound )
{
TQKeyEvent e2 ( e->type(),
(e->key() == Key_Up ? Key_End : Key_Home),
0, e->state(),
TQString(), e->isAutoRepeat(), e->count() );
TQApplication::sendEvent( this, &e2 );
cur = currentItem();
// The first item may also be a divider, so keep moving
// until it's not
while ( cur != 0
&& dynamic_cast<DividerItem*>(cur) != 0
&& nextItem != 0 )
{
TDEListView::keyPressEvent( e );
if( currentItem() == cur ) // Prevent infinite loops
break;
cur = currentItem();
}
}
#undef nextItem
}
// When Right/Left is pressed in iPod view mode, activate the iPod
// "move forward/backward" action.
else if( (e->key() == Key_Left || e->key() == Key_Right)
&& m_viewMode == modeIpodView )
{
if( e->key() == Key_Right )
m_parent->m_ipodIncrement->activate();
else if( e->key() == Key_Left )
m_parent->m_ipodDecrement->activate();
}
else // we don't want the event
TDEListView::keyPressEvent( e );
}
//////////////////////////////////////////////////////////////////////////////////////////
// public slots
//////////////////////////////////////////////////////////////////////////////////////////
void
CollectionView::renderView(bool force /* = false */) //SLOT
{
SHOULD_BE_GUI
if(!force && !m_dirty )
return;
if( BrowserBar::instance()->currentBrowser() != m_parent )
{
// the collectionbrowser is intensive for sql, so we only renderView() if the tab
// is currently active. else, wait until user focuses it.
// debug() << "current browser is not collection, aborting renderView()" << endl;
m_dirty = true;
return;
}
m_dirty = false;
// Don't cache / restore view if we're in ipod mode and we've
// just incremented or decremented, since we'll run selectIpodItems()
// below anyway.
if( childCount() &&
!(m_viewMode == modeIpodView && m_ipodIncremented > 0) )
cacheView();
//clear();
safeClear();
if ( m_viewMode == modeFlatView )
{
renderFlatModeView( force );
}
if( m_viewMode == modeIpodView )
{
renderIpodModeView( force );
}
if( m_viewMode == modeTreeView )
{
renderTreeModeView( force );
}
// Don't cache or restore view when we're just going to run
// selectIpodItems() below anyway.
if( !(m_viewMode == modeIpodView && m_ipodIncremented > 0) )
restoreView();
else
selectIpodItems();
}
//////////////////////////////////////////////////////////////////////////////////////////
// private slots
//////////////////////////////////////////////////////////////////////////////////////////
void
CollectionView::setupDirs() //SLOT
{
KDialogBase dialog( this, 0, false );
kapp->setTopWidget( &dialog );
dialog.setCaption( kapp->makeStdCaption( i18n("Configure Collection") ) );
CollectionSetup *setup = new CollectionSetup( &dialog );
dialog.setMainWidget( setup );
dialog.showButtonApply( false );
dialog.adjustSize();
// Make the dialog a bit bigger, default is too small to be useful
dialog.resize( dialog.width() + 50, dialog.height() + 150 );
if ( dialog.exec() != TQDialog::Rejected )
{
const bool rescan = ( MountPointManager::instance()->collectionFolders() != setup->dirs() );
setup->writeConfig();
if ( rescan )
CollectionDB::instance()->startScan();
}
}
void
CollectionView::scanStarted() // SLOT
{
Amarok::actionCollection()->action("update_collection")->setEnabled( false );
}
void
CollectionView::scanDone( bool changed ) //SLOT
{
if( changed )
{
renderView(true);
}
Amarok::actionCollection()->action("update_collection")->setEnabled( true );
}
void
CollectionView::slotEnsureSelectedItemVisible() //SLOT
{
//Scroll to make sure the first selected item is visible
//Find the first selected item
TQListViewItem *r=0;
for ( TQListViewItem *i = firstChild(); i && !r; i=i->nextSibling() )
{
if ( i->isSelected() )
r = i;
for ( TQListViewItem *j = i->firstChild(); j && !r; j=j->nextSibling() )
{
if ( j->isSelected() )
r = j;
for ( TQListViewItem *k = j->firstChild(); k && !r; k=k->nextSibling() )
{
if ( k->isSelected() )
r = k;
}
}
}
if ( r )
{
//We've found the selected item. Now let's refocus on it.
//An elaborate agorithm to try to make as much as possible of the vicinity visible
//It looks better if things end up consistently in one place.
//So, scroll to the end so that we come at items from the bottom.
if ( lastChild() )
ensureItemVisible( lastChild() );
//Create a reverse list of parents, grandparents etc.
//Later we try to make the grandparents in view, then their children etc.
//This means that the selected item has the most priority as it is done last.
TQValueStack<TQListViewItem*> parents;
while ( r )
{
parents.push( r );
r = r->parent();
}
while ( !parents.isEmpty() )
{
//We would prefer the next item to be visible.
if ( parents.top()->nextSibling() )
ensureItemVisible( parents.top()->nextSibling() );
//It's even more important the actual item is visible than the next one.
ensureItemVisible( parents.top() );
parents.pop();
}
}
}
void
CollectionView::slotExpand( TQListViewItem* item ) //SLOT
{
if ( !item || !item->isExpandable() ) return;
int category = 0;
TQStringList values;
QueryBuilder qb;
bool c = false;
bool SortbyTrackFirst = false;
//Sort by track number first if album is in one of the categories, otherwise by track name first
if ( m_cat1 == IdAlbum ||
m_cat2 == IdAlbum ||
m_cat3 == IdAlbum )
SortbyTrackFirst = true;
// initialization for year - album mode
TQString tmptext;
int VisYearAlbum = -1;
int VisLabel = -1;
int q_cat1=m_cat1;
int q_cat2=m_cat2;
int q_cat3=m_cat3;
if( m_cat1 == IdVisYearAlbum ||
m_cat2 == IdVisYearAlbum ||
m_cat3 == IdVisYearAlbum )
{
SortbyTrackFirst = true;
if( m_cat1 == IdVisYearAlbum )
{
VisYearAlbum = 1;
q_cat1 = IdAlbum;
}
if( m_cat2 == IdVisYearAlbum )
{
VisYearAlbum = 2;
q_cat2 = IdAlbum;
}
if( m_cat3 == IdVisYearAlbum )
{
VisYearAlbum = 3;
q_cat3 = IdAlbum;
}
}
if( m_cat1 == IdLabel ||
m_cat2 == IdLabel ||
m_cat3 == IdLabel )
{
if( m_cat1 == IdLabel )
VisLabel = 1;
if( m_cat2 == IdLabel )
VisLabel = 2;
if ( m_cat3 == IdLabel )
VisLabel = 3;
}
if ( translateTimeFilter( timeFilter() ) > 0 )