-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconversionconsole.cpp
More file actions
226 lines (202 loc) · 7.23 KB
/
Copy pathconversionconsole.cpp
File metadata and controls
226 lines (202 loc) · 7.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/**
* Log2Log Chat Log Converter
* Execution
* Non-Graphical Conversion
*
* @author Deltik
*
* License:
* This file is part of Log2Log.
*
* Log2Log is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Log2Log is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Log2Log. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "conversionconsole.h"
#include "formatinfo.h"
#include "formats/stdconverter.h"
#include "formats/stdformat.h"
#include <QtCore>
#include <iostream>
using namespace std;
/**
* Constructor
*/
ConversionConsole::ConversionConsole(QStringList argv)
{
QHash<QString, QVariant> send;
for (int i = 0; i < argv.size(); i ++)
{
// Extract
QString a = argv[i];
// FROM
if (a == "-f" ||
a == "/f" ||
a == "--from")
if (i+1 < argv.size())
send["from"] = argv[i+1];
// TO
if (a == "-t" ||
a == "/t" ||
a == "--to")
if (i+1 < argv.size())
send["to"] = argv[i+1];
// SOURCE
if (a == "-s" ||
a == "/s" ||
a == "--source")
if (i+1 < argv.size())
send["source"] = argv[i+1];
// SOURCE USERNAME
if (a == "-su" ||
a == "/su" ||
a == "--source-username")
if (i+1 < argv.size())
send["source-username"] = argv[i+1];
// SOURCE PASSWORD
if (a == "-sp" ||
a == "/sp" ||
a == "--source-password")
if (i+1 < argv.size())
send["source-password"] = argv[i+1];
// DESTINATION
if (a == "-d" ||
a == "/d" ||
a == "--destination")
if (i+1 < argv.size())
send["destination"] = argv[i+1];
// DESTINATION USERNAME
if (a == "-du" ||
a == "/du" ||
a == "--destination-username")
if (i+1 < argv.size())
send["destination-username"] = argv[i+1];
// DESTINATION PASSWORD
if (a == "-dp" ||
a == "/dp" ||
a == "--destination-password")
if (i+1 < argv.size())
send["destination-password"] = argv[i+1];
}
/* ### GO!!! ### */
exitCode = runConversion(send);
cout << endl;
}
/**
* Run the whole conversion synchronously (source -> StdFormat -> destination).
* Unlike the GUI's threaded Conversion pipeline, this drives the converters
* directly, which is simpler and actually works in console mode.
*/
int ConversionConsole::runConversion(QHash<QString, QVariant> send)
{
// Resolve the format ids (e.g. "skype", "json") to converter <class> names.
FormatInfo srcFI(send["from"].toString()); srcFI.pointerDig();
FormatInfo dstFI(send["to"].toString()); dstFI.pointerDig();
QString from_class = srcFI.getName("class");
QString to_class = dstFI.getName("class");
StdConverter *from = Conversion::makeConverter(from_class);
StdConverter *to = Conversion::makeConverter(to_class);
if (from == NULL)
{
cerr << "\nError: unknown source format '"
<< send["from"].toString().toStdString()
<< "'. Use a format id such as: skype, pidgin, json, aim, trillian, wlm.\n";
return 2;
}
if (to == NULL)
{
cerr << "\nError: unknown destination format '"
<< send["to"].toString().toStdString()
<< "'. Use a format id such as: json, pidgin, meebo, trillian, wlm.\n";
return 2;
}
if (send["source"].toString().isEmpty() || send["destination"].toString().isEmpty())
{
cerr << "\nError: both --source and --destination are required.\n";
return 2;
}
connect(from, SIGNAL(updateProgress(int, QString)), this, SLOT(setProgress(int, QString)), Qt::DirectConnection);
connect(to, SIGNAL(updateProgress(int, QString)), this, SLOT(setProgress(int, QString)), Qt::DirectConnection);
connect(from, SIGNAL(error(QString)), this, SLOT(printError(QString)), Qt::DirectConnection);
connect(to, SIGNAL(error(QString)), this, SLOT(printError(QString)), Qt::DirectConnection);
// ### Source -> standard format ###
// We pass only the path; each converter reads it however it needs to
// (Skype reads its binary/SQLite files directly, text formats fall back to
// Helper::files_get_contents). We do NOT preload data["files"] here because
// that reader is text-mode and would corrupt binary formats.
QHash<QString, QVariant> fromData;
fromData["path"] = send["source"];
fromData["username"] = send["source-username"];
fromData["password"] = send["source-password"];
StdFormat *log = from->from(fromData);
// ### Standard format -> destination ###
to->setInput(log);
to->to(log);
QVariant output = to->getData(QVariant());
// ### Save ###
int written = saveOutput(output, send["destination"].toString());
setProgress(100, "Conversion complete! Wrote " + QVariant(written).toString() +
" file(s) to " + send["destination"].toString());
return 0;
}
/**
* Write the destination converter's output map to disk.
* The map is { filename: {"content": <bytes>, ...} }, mirroring Conversion::save.
*/
int ConversionConsole::saveOutput(const QVariant &output, const QString &destBase)
{
QMap<QString, QVariant> log = output.toMap();
int written = 0;
for (QMap<QString, QVariant>::const_iterator i = log.constBegin(); i != log.constEnd(); i++)
{
QString key = QDir::fromNativeSeparators(destBase) + "/" + i.key();
QVariant value = i.value();
QHash<QString, QVariant> info = value.toHash();
// The file contents may live under a few different keys (or be the value).
QVariant content = info["content"];
if (content.isNull()) content = info["data"];
if (content.isNull()) content = info["text"];
if (content.isNull()) content = value;
// Make sure the target directory exists.
QStringList parts = key.split("/");
parts.pop_back();
QDir().mkpath(parts.join("/"));
QFile file(key);
if (file.open(QIODevice::WriteOnly))
{
file.write(content.toByteArray());
file.close();
written++;
}
else
{
cerr << "\nWarning: could not write " << key.toStdString() << "\n";
}
}
return written;
}
/**
* Print a converter error to stderr.
*/
void ConversionConsole::printError(QString text)
{
cerr << "\nError: " << text.toStdString() << "\n";
}
/**
* Print a progress line.
*/
void ConversionConsole::setProgress(int meter, QString description)
{
cout << "\n" << QVariant(meter).toString().toStdString() << "% : " << description.toStdString();
cout.flush();
}