Added CSV parsing and export of Symbian-format Event logs that have had their tables...
[qwerkisync] / EventWriters / VMGWriter.cpp
1 /*
2  * Copyright (C) 2011, Jamie Thompson
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License as published by the Free Software Foundation; either
7  * version 3 of the License, or (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 GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public
15  * License along with this program; If not, see
16  * <http://www.gnu.org/licenses/>.
17  */
18
19 #include "EventWriters/VMGWriter.h"
20
21 #include "EventTypes/iVMGEvent.h"
22 #include "EventTypes/iEvent.h"
23 #include "Settings.h"
24
25 #include <QDateTime>
26 #include <QDir>
27 #include <QFile>
28 #include <QTextStream>
29
30 #include <typeinfo>
31 #include <utime.h>
32
33 using namespace EventWriters;
34
35 VMGWriter::VMGWriter(const Settings &settings, const NumberToNameLookup &numberToNameLookup)
36         : m_Settings(settings), m_NumberToNameLookup(numberToNameLookup)
37 {
38 }
39
40 void VMGWriter::Write(EventTypes::iEvent &event)
41 {
42         EventTypes::iVMGEvent *vmgEvent(dynamic_cast<EventTypes::iVMGEvent *>(&event));
43         if(vmgEvent)
44         {
45                 // Build the path and ensure it exists...
46                 QString eventFilename(CurrentSettings().Directory());
47                 eventFilename += vmgEvent->PathForVMG();
48                 QDir().mkpath(eventFilename);
49
50                 // ...then build the filename and open it.
51                 eventFilename += QString::number(event.Timestamp().toUTC().toTime_t()) + ".vmg";
52                 QFile data(eventFilename);
53                 if (data.open(QFile::WriteOnly | QFile::Truncate))
54                 {
55                         QTextStream stream(&data);
56
57                         //QTextCodec *oldCodec = stream.codec();
58                         stream.setAutoDetectUnicode(false);
59                         stream.setCodec("UTF-16LE");
60
61                         vmgEvent->WriteVMG(stream, NameLookup());
62
63                         data.close();
64                 }
65
66                 utimbuf fileTimes;
67                 fileTimes.modtime = event.Timestamp().toUTC().toTime_t();
68                 utime(eventFilename.toAscii(), &fileTimes);
69         }
70 }