1 #ifndef CSM_DOC_LOADER_H
2 #define CSM_DOC_LOADER_H
3 
4 #include <vector>
5 
6 #include <QObject>
7 #include <QMutex>
8 #include <QTimer>
9 #include <QWaitCondition>
10 
11 namespace CSMDoc
12 {
13     class Document;
14 
15     class Loader : public QObject
16     {
17             Q_OBJECT
18 
19             struct Stage
20             {
21                 int mFile;
22                 int mRecordsLoaded;
23                 bool mRecordsLeft;
24 
25                 Stage();
26             };
27 
28             QMutex mMutex;
29             QWaitCondition mThingsToDo;
30             std::vector<std::pair<Document *, Stage> > mDocuments;
31 
32             QTimer* mTimer;
33             bool mShouldStop;
34 
35         public:
36 
37             Loader();
38 
39             QWaitCondition& hasThingsToDo();
40 
41             void stop();
42 
43         private slots:
44 
45             void load();
46 
47         public slots:
48 
49             void loadDocument (CSMDoc::Document *document);
50             ///< The ownership of \a document is not transferred.
51 
52             void abortLoading (CSMDoc::Document *document);
53             ///< Abort loading \a docuemnt (ignored if \a document has already finished being
54             /// loaded). Will result in a documentNotLoaded signal, once the Loader has finished
55             /// cleaning up.
56 
57         signals:
58 
59             void documentLoaded (Document *document);
60             ///< The ownership of \a document is not transferred.
61 
62             void documentNotLoaded (Document *document, const std::string& error);
63             ///< Document load has been interrupted either because of a call to abortLoading
64             /// or a problem during loading). In the former case error will be an empty string.
65 
66             void nextStage (CSMDoc::Document *document, const std::string& name,
67                 int totalRecords);
68 
69             void nextRecord (CSMDoc::Document *document, int records);
70             ///< \note This signal is only given once per group of records. The group size is
71             /// approximately the total number of records divided by the steps value of the
72             /// previous nextStage signal.
73 
74             void loadMessage (CSMDoc::Document *document, const std::string& message);
75             ///< Non-critical load error or warning
76     };
77 }
78 
79 #endif
80