summaryrefslogtreecommitdiffstats
path: root/amarok/src/directorylist.cpp
blob: 9b8c46602a254e7a82ebac64808ebe5caa92a20d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/***************************************************************************
                         directorylist.cpp
                            -------------------
   begin                : Tue Feb 4 2003
   copyright            : (C) 2003 Scott Wheeler <wheeler@kde.org>
                        : (C) 2004 Max Howell <max.howell@methylblue.com>
                        : (C) 2004 Mark Kretschmann <markey@web.de>
***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 "amarokconfig.h"
#include "directorylist.h"
#include "mountpointmanager.h"

#include <tdefileitem.h>
#include <tdelocale.h>

#include <tqfile.h>
#include <tqlabel.h>
#include <tqpainter.h>
#include <tqtooltip.h>


CollectionSetup* CollectionSetup::s_instance;


CollectionSetup::CollectionSetup( TQWidget *parent )
        : TQVBox( parent, "CollectionSetup" )
{
    s_instance = this;

    (new TQLabel( i18n(
        "These folders will be scanned for "
        "media to make up your collection:"), this ))->setAlignment( TQt::WordBreak );

    m_view = new TQFixedListView( this );
    m_recursive = new TQCheckBox( i18n("&Scan folders recursively"), this );
    m_monitor   = new TQCheckBox( i18n("&Watch folders for changes"), this );

    TQToolTip::add( m_recursive, i18n( "If selected, Amarok will read all subfolders." ) );
    TQToolTip::add( m_monitor,   i18n( "If selected, folders will automatically get rescanned when the content is modified, e.g. when a new file was added." ) );

    // Read config values
    //we have to detect if this is the actual first run and not get the collectionFolders in that case
    //there won't be any anyway and accessing them creates a Sqlite database, even if the user wants to
    //use another database
    //bug 131719 131724
    if( !Amarok::config()->readBoolEntry( "First Run", true ) )
        m_dirs = MountPointManager::instance()->collectionFolders();

    m_recursive->setChecked( AmarokConfig::scanRecursively() );
    m_monitor->setChecked( AmarokConfig::monitorChanges() );

    m_view->addColumn( TQString() );
    m_view->setRootIsDecorated( true );
    m_view->setSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Expanding );
    m_view->setResizeMode( TQListView::LastColumn );

    reinterpret_cast<TQWidget*>(m_view->header())->hide();
    new Collection::Item( m_view );

    setSpacing( 6 );
}


void
CollectionSetup::writeConfig()
{
    //If we are in recursive mode then we don't need to store the names of the
    //subdirectories of the selected directories
    if ( recursive() )
    {
        for ( TQStringList::iterator it=m_dirs.begin(); it!=m_dirs.end(); ++it )
        {
            TQStringList::iterator jt=m_dirs.begin();
            while ( jt!=m_dirs.end() )
            {
                if ( it==jt )
                {
                    ++jt;
                    continue;
                }
                //Note: all directories except "/" lack a trailing '/'.
                //If (*jt) is a subdirectory of (*it) it is redundant.
                //As all directories are subdirectories of "/", if "/" is selected, we
                //can delete everything else.
                if ( ( *jt ).startsWith( *it + '/' ) || *it=="/" )
                    jt = m_dirs.remove( jt );
                else
                    ++jt;
            }
        }
    }

    MountPointManager::instance()->setCollectionFolders( m_dirs );
    AmarokConfig::setScanRecursively( recursive() );
    AmarokConfig::setMonitorChanges( monitor() );
}


//////////////////////////////////////////////////////////////////////////////////////////
// CLASS Item
//////////////////////////////////////////////////////////////////////////////////////////

namespace Collection {

Item::Item( TQListView *parent )
    : TQCheckListItem( parent, "/", TQCheckListItem::CheckBox  )
    , m_lister( true )
    , m_url( "file:/" )
    , m_listed( false )
    , m_fullyDisabled( false )
{
    //Since we create the "/" checklistitem here, we need to enable it if needed
    if ( CollectionSetup::instance()->m_dirs.contains( "/" ) )
        static_cast<TQCheckListItem*>( this )->setOn(true);
    m_lister.setDirOnlyMode( true );
    connect( &m_lister, TQT_SIGNAL(newItems( const KFileItemList& )), TQT_SLOT(newItems( const KFileItemList& )) );
    setOpen( true );
    setVisible( true );
}


Item::Item( TQListViewItem *parent, const KURL &url , bool full_disable /* default=false */ )
    : TQCheckListItem( parent, url.fileName(), TQCheckListItem::CheckBox )
    , m_lister( true )
    , m_url( url )
    , m_listed( false )
    , m_fullyDisabled( full_disable )
{
    m_lister.setDirOnlyMode( true );
    setExpandable( true );
    connect( &m_lister, TQT_SIGNAL(newItems( const KFileItemList& )), TQT_SLOT(newItems( const KFileItemList& )) );
    connect( &m_lister, TQT_SIGNAL(completed()), TQT_SLOT(completed()) );
    connect( &m_lister, TQT_SIGNAL(canceled()), TQT_SLOT(completed()) );
}


TQString
Item::fullPath() const
{
    TQString path;

    for( const TQListViewItem *item = this; item != listView()->firstChild(); item = item->parent() )
    {
        path.prepend( item->text( 0 ) );
        path.prepend( '/' );
    }

    return path;
}


void
Item::setOpen( bool b )
{
    if ( !m_listed )
    {
        m_lister.openURL( m_url, true );
        m_listed = true;
    }

    TQListViewItem::setOpen( b );
}


void
Item::stateChange( bool b )
{
    TQStringList &cs_m_dirs = CollectionSetup::instance()->m_dirs;

    if ( isFullyDisabled() )
        return;

    if( CollectionSetup::instance()->recursive() )
        for( TQListViewItem *item = firstChild(); item; item = item->nextSibling() )
            if ( dynamic_cast<Item*>( item ) && !dynamic_cast<Item*>( item )->isFullyDisabled() )
               static_cast<TQCheckListItem*>(item)->TQCheckListItem::setOn( b );

    //If it is disabled, allow us to change its appearance (above code) but not add it
    //to the list of folders (code below)
    if ( isDisabled() )
        return;

    // Update folder list
    TQStringList::Iterator it = cs_m_dirs.find( m_url.path() );
    if ( isOn() ) {
        if ( it == cs_m_dirs.end() )
            cs_m_dirs << m_url.path();

        // Deselect subdirectories if we are in recursive mode as they are redundant
        if ( CollectionSetup::instance()->recursive() )
        {
            TQStringList::Iterator diriter = cs_m_dirs.begin();
            while ( diriter != cs_m_dirs.end() )
            {
                // Since the dir "/" starts with '/', we need a hack to stop it removing
                // itself (it being the only path with a trailing '/')
                if ( (*diriter).startsWith( m_url.path(1) ) && *diriter != "/" )
                    diriter = cs_m_dirs.erase(diriter);
                else
                    ++diriter;
            }
        }
    }
    else {
        //Deselect item and recurse through children but only deselect children if they
        //do not exist unless we are in recursive mode (where no children should be
        //selected if the parent is being unselected)
        //Note this does not do anything to the checkboxes, but they should be doing
        //the same thing as we are (hopefully)
        //Note: all paths lack a trailing '/' except for "/", which must be handled as a
        //special case
        if ( it != cs_m_dirs.end() )
            cs_m_dirs.erase( it );
        TQStringList::Iterator diriter = cs_m_dirs.begin();
        while ( diriter != cs_m_dirs.end() )
        {
            if ( (*diriter).startsWith( m_url.path(1) ) )   //path(1) adds a trailing '/'
            {
                if ( CollectionSetup::instance()->recursive() ||
                     !TQFile::exists( *diriter ) )
                {
                    diriter = cs_m_dirs.erase(diriter);
                }
                else
                    ++diriter;
            }
            else
                ++diriter;
        }
    }

    // Redraw parent items
    listView()->triggerUpdate();
}


void
Item::activate()
{
    if( !isDisabled() )
        TQCheckListItem::activate();
}


void
Item::newItems( const KFileItemList &list ) //SLOT
{
    for( KFileItemListIterator it( list ); *it; ++it )
    {
        //Fully disable (always appears off and grayed-out) if it is "/proc", "/sys" or
        //"/dev" or one of their children. This is because we will never scan them, so we
        //might as well show that.
        //These match up with the skipped dirs in CollectionScanner::readDir.
        bool fully_disable=false;

        if ( this->m_url.fileName().isEmpty() && ( ( *it )->url().fileName()=="proc"
             || ( *it )->url().fileName()=="dev" || ( *it )->url().fileName()=="sys" ) )
        {
            fully_disable=true;
        }

        Item *item = new Item( this, (*it)->url() , fully_disable || this->isFullyDisabled() );

        if ( !item->isFullyDisabled() )
        {
            if( CollectionSetup::instance()->recursive() && isOn() ||
                CollectionSetup::instance()->m_dirs.contains( item->fullPath() ) )
            {
                item->setOn( true );
            }
        }

        item->setPixmap( 0, (*it)->pixmap( TDEIcon::SizeSmall ) );
    }
}


void
Item::paintCell( TQPainter * p, const TQColorGroup & cg, int column, int width, int align )
{
    bool dirty = false;
    TQStringList &cs_m_dirs = CollectionSetup::instance()->m_dirs;

    // Figure out if a child folder is activated
    for ( TQStringList::const_iterator iter = cs_m_dirs.begin(); iter != cs_m_dirs.end();
            ++iter )
        if ( ( *iter ).startsWith( m_url.path(1) ) )
            if ( *iter != "/" ) // "/" should not match as a child of "/"
                dirty = true;

    // Use a different color if this folder has an activated child folder
    const TQFont f = p->font();
    TQColorGroup _cg = cg;
    if ( dirty )
    {
        _cg.setColor( TQColorGroup::Text, listView()->colorGroup().link() );
        TQFont font = p->font();
        font.setBold( !font.bold() );
        p->setFont( font );
    }

    TQCheckListItem::paintCell( p, isDisabled() ? listView()->palette().disabled() : _cg, column, width, align );
    p->setFont( f );
}

} //namespace Collection

#include "directorylist.moc"