Not crap after all...
[amarok.git] / src / scancontroller.h
blob335edde2a001561d86adb92b08bb5bc248b208db
1 /***************************************************************************
2 * Copyright (C) 2003-2007 by The Amarok Developers *
3 * *
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. *
8 * *
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. *
13 * *
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>
24 #include <QMutex>
25 #include <QtXml> //baseclass
27 #include "threadmanager.h" //baseclass
29 class CollectionDB;
30 class K3ProcIO;
32 /**
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
49 Q_OBJECT
51 public:
52 static const int RestartEventType = 8891;
54 class RestartEvent : public QCustomEvent {
55 public:
56 RestartEvent() : QCustomEvent( RestartEventType ) {}
59 static const int PlaylistFoundEventType = 8890;
61 class PlaylistFoundEvent : public QCustomEvent {
62 public:
63 PlaylistFoundEvent( QString path )
64 : QCustomEvent( PlaylistFoundEventType )
65 , m_path( path ) {}
66 QString path() { return m_path; }
67 private:
68 QString m_path;
71 public:
72 ScanController( CollectionDB* parent, bool incremental, const QStringList& folders = QStringList() );
73 ~ScanController();
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; }
83 signals:
84 void scannerAcknowledged();
85 void scanDone( bool changed );
87 public slots:
88 void slotFileMoved( const QString &src, const QString &dest );
90 private slots:
91 void slotReadReady();
93 private:
94 void initIncremental();
95 virtual bool doJob();
96 static void setInstance( ScanController* instance );
98 bool startElement( const QString&, const QString &localName, const QString&, const QXmlAttributes &attrs );
99 void customEvent( QCustomEvent* );
101 // Member variables:
102 static const int MAX_RESTARTS = 80;
103 static const int MAX_FAILURE_PERCENTAGE = 5;
105 K3ProcIO* m_scanner;
106 QStringList m_folders;
107 QStringList m_foldersToRemove;
108 bool m_incremental;
109 bool m_hasChanged;
111 QString m_xmlData;
112 QMutex m_dataMutex;
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
122 // path.
123 QMap<QString,QString> m_filesAdded;
124 QMap<QString,QString> m_filesDeleted;
125 QMutex m_fileMapsMutex;
127 static ScanController* currController;
129 bool m_tablesCreated;
130 int m_scanCount;
134 #endif // AMAROK_SCANCONTROLLER_H