1 /***************************************************************************
2 * Copyright (C) 2003-2007 by The Amarok Developers *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Steet, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
20 #ifndef AMAROK_SCANCONTROLLER_H
21 #define AMAROK_SCANCONTROLLER_H
23 #include <QCustomEvent>
25 #include <QtXml> //baseclass
27 #include "threadmanager.h" //baseclass
33 * @class ScanController
34 * @short Starts and controls the external amarokcollectionscanner application.
35 * @author Mark Kretschmann <markey@web.de>
37 * The collection scanner itself is run in an external process, unlike before, where it
38 * used to be thread. The advantage is that the scanner cannot crash the Amarok main
39 * application any more. If it crashes we can simply restart it.
41 * Amarok communicates with the scanner via the ScanController class, which processes
42 * XML entities written to stdout by the scanner process. For XML parsing an event
43 * driven SAX2 parser is used, which can process the entities as they arrive, without
44 * the need for a DOM document structure.
47 class ScanController
: public ThreadManager::DependentJob
, public QXmlDefaultHandler
52 static const int RestartEventType
= 8891;
54 class RestartEvent
: public QCustomEvent
{
56 RestartEvent() : QCustomEvent( RestartEventType
) {}
59 static const int PlaylistFoundEventType
= 8890;
61 class PlaylistFoundEvent
: public QCustomEvent
{
63 PlaylistFoundEvent( QString path
)
64 : QCustomEvent( PlaylistFoundEventType
)
66 QString
path() { return m_path
; }
72 ScanController( CollectionDB
* parent
, bool incremental
, const QStringList
& folders
= QStringList() );
74 static ScanController
* instance();
76 virtual void completeJob( void );
78 bool isIncremental() const { return m_incremental
; }
79 bool hasChanged() const { return m_hasChanged
; }
81 bool tablesCreated() { return m_tablesCreated
; }
84 void scannerAcknowledged();
85 void scanDone( bool changed
);
88 void slotFileMoved( const QString
&src
, const QString
&dest
);
94 void initIncremental();
96 static void setInstance( ScanController
* instance
);
98 bool startElement( const QString
&, const QString
&localName
, const QString
&, const QXmlAttributes
&attrs
);
99 void customEvent( QCustomEvent
* );
102 static const int MAX_RESTARTS
= 80;
103 static const int MAX_FAILURE_PERCENTAGE
= 5;
106 QStringList m_folders
;
107 QStringList m_foldersToRemove
;
113 QXmlInputSource
* m_source
;
114 QXmlSimpleReader
* m_reader
;
116 QStringList m_crashedFiles
;
118 // Every file that the collection scanner finds is marked
119 // here, as well as the source of all files that the AFT code
120 // detects as having been moved. These are the files that
121 // have definitely not been deleted. The key is the absolute
123 QMap
<QString
,QString
> m_filesAdded
;
124 QMap
<QString
,QString
> m_filesDeleted
;
125 QMutex m_fileMapsMutex
;
127 static ScanController
* currController
;
129 bool m_tablesCreated
;
134 #endif // AMAROK_SCANCONTROLLER_H