Chris@92
|
1 #include "filestates.h"
|
Chris@74
|
2
|
Chris@86
|
3 #include "debug.h"
|
Chris@86
|
4
|
Chris@74
|
5 #include <QMap>
|
Chris@74
|
6
|
Chris@92
|
7 FileStates::FileStates(QString text)
|
Chris@74
|
8 {
|
Chris@74
|
9 text.replace("\r\n", "\n");
|
Chris@74
|
10
|
Chris@74
|
11 QMap<QChar, QStringList *> buckets;
|
Chris@74
|
12 buckets['M'] = &modified;
|
Chris@74
|
13 buckets['A'] = &added;
|
Chris@74
|
14 buckets['R'] = &removed;
|
Chris@74
|
15 buckets['!'] = &missing;
|
Chris@74
|
16 buckets['?'] = &unknown;
|
Chris@74
|
17
|
Chris@74
|
18 QStringList lines = text.split("\n", QString::SkipEmptyParts);
|
Chris@74
|
19 foreach (QString line, lines) {
|
Chris@86
|
20 if (line.length() < 3 || line[1] != ' ') {
|
Chris@86
|
21 continue;
|
Chris@86
|
22 }
|
Chris@74
|
23 QChar tag = line[0];
|
Chris@74
|
24 QString file = line.right(line.length() - 2);
|
Chris@74
|
25 if (buckets.contains(tag)) {
|
Chris@74
|
26 buckets[tag]->push_back(file);
|
Chris@74
|
27 }
|
Chris@74
|
28 }
|
Chris@86
|
29
|
Chris@92
|
30 DEBUG << "FileStates: " << modified.size() << " modified, " << added.size()
|
Chris@86
|
31 << " added, " << removed.size() << " removed, " << missing.size()
|
Chris@86
|
32 << " missing, " << unknown.size() << " unknown" << endl;
|
Chris@74
|
33 }
|
Chris@92
|
34
|
Chris@92
|
35 QStringList FileStates::getFilesInState(State s)
|
Chris@92
|
36 {
|
Chris@92
|
37 switch (s) {
|
Chris@92
|
38
|
Chris@92
|
39 case Modified: return modified;
|
Chris@92
|
40 case Added: return added;
|
Chris@92
|
41 case Unknown: return unknown;
|
Chris@92
|
42 case Removed: return removed;
|
Chris@92
|
43 case Missing: return missing;
|
Chris@92
|
44
|
Chris@92
|
45 case UpToDate: // not supported yet
|
Chris@92
|
46 default:
|
Chris@92
|
47 return QStringList();
|
Chris@92
|
48 }
|
Chris@92
|
49 }
|
Chris@92
|
50
|
Chris@92
|
51 FileStates::State FileStates::getStateOfFile(QString file)
|
Chris@92
|
52 {
|
Chris@92
|
53 // slow, but let's worry about that if it becomes a problem
|
Chris@92
|
54 for (int is = int(FirstState); is <= int(LastState); ++is) {
|
Chris@92
|
55 QStringList fl = getFilesInState(State(is));
|
Chris@92
|
56 foreach (QString f, fl) if (f == file) return State(is);
|
Chris@92
|
57 }
|
Chris@92
|
58 DEBUG << "FileStates: WARNING: getStateOfFile: file "
|
Chris@92
|
59 << file << " is unknown to us: returning Unknown state, "
|
Chris@92
|
60 << "but unknown to us is not supposed to be the same "
|
Chris@92
|
61 << "thing as unknown state..."
|
Chris@92
|
62 << endl;
|
Chris@92
|
63 return Unknown;
|
Chris@92
|
64 }
|