Addressing some compilation warnings:
[qwerkisync] / EventWriters / CSVSymbianEventLogWriter.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 "CSVSymbianEventLogWriter.h"
20
21 #include "EventTypes/iCSVSymbianEvent.h"
22 #include "EventTypes/iEvent.h"
23 #include "Settings.h"
24
25 #include <QDebug>
26
27 #include <QDir>
28 #include <QFile>
29 #include <QTextStream>
30
31 #include <typeinfo>
32 #include <utime.h>
33
34 using namespace EventWriters;
35
36 CSVSymbianEventLogWriter::CSVSymbianEventLogWriter(const Settings &settings, const NumberToNameLookup &numberToNameLookup)
37         : m_Settings(settings), m_NumberToNameLookup(numberToNameLookup), m_Stream(NULL), m_Delimiter('|'), m_File(NULL)
38 {
39         m_Headers << "EType" << "ETime" << "DType" << "Flag1" << "Flag2" << "Flag3"
40                         << "Flag4" << "Id" << "Remote" << "Direction" << "Duration"
41                         << "Status" << "Subject" << "Number" << "Contact" << "Link"
42                         << "Data" << "Recent" << "Duplicate";
43 }
44
45 CSVSymbianEventLogWriter::~CSVSymbianEventLogWriter()
46 {
47         if(Stream())
48         {
49                 Stream()->flush();
50                 delete Stream();
51         }
52
53         if(File())
54         {
55                 File()->close();
56                 delete File();
57         }
58
59         // Build the strings filepath
60         QString stringsFilename(CurrentSettings().Directory());
61         stringsFilename += "/Call Logs/Strings.csv";
62
63         QFile stringsFile(stringsFilename);
64         if (stringsFile.open(QFile::WriteOnly | QFile::Truncate))
65         {
66                 QTextStream stream(&stringsFile);
67                 stream.setAutoDetectUnicode(false);
68                 stream.setCodec("UTF-16LE");
69
70                 WriteStrings(stream);
71         }
72 }
73
74 void CSVSymbianEventLogWriter::Write(EventTypes::iEvent &event)
75 {
76         EventTypes::iCSVSymbianEvent *csvEvent(dynamic_cast<EventTypes::iCSVSymbianEvent *>(&event));
77         if(csvEvent)
78         {
79                 if(!Stream())
80                         OpenStream();
81
82                 csvEvent->WriteCSVSymbian(*Stream(), Headers(), Delimiter(), NameLookup(), Strings());
83         }
84 }
85
86 void CSVSymbianEventLogWriter::OpenStream()
87 {
88         // Build the path and ensure it exists...
89         QString eventFilename(CurrentSettings().Directory());
90         eventFilename += "/Call Logs";
91         QDir().mkpath(eventFilename);
92
93         // ...then build the filename and open it.
94         eventFilename += "/Symbian Event Log.csv";
95         File(new QFile(eventFilename));
96         if (File()->open(QFile::WriteOnly | QFile::Truncate))
97         {
98                 Stream(new QTextStream(File()));
99                 Stream()->setAutoDetectUnicode(false);
100                 Stream()->setCodec("UTF-16LE");
101
102                 WriteHeaders();
103         }
104 }
105
106 void CSVSymbianEventLogWriter::WriteHeaders()
107 {
108         foreach(QString header, Headers())
109         {
110                 *Stream() << header;
111                 if(Headers().back() != header)
112                         *Stream() << "|";
113         }
114         *Stream() << endl;
115 }
116
117 void CSVSymbianEventLogWriter::WriteStrings(QTextStream &stringsStream)
118 {
119         foreach(QString string, Strings().keys())
120                 stringsStream << "\"" << string << "\"|" << Strings().value(string) << endl;
121 }