This repository has been archived by the owner on Apr 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.cpp
297 lines (271 loc) · 11 KB
/
convert.cpp
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Needed for basic logic and interaction with files
#include <iostream>
#include <string>
#include <filesystem>
#include <fstream>
#include <sstream>
#include <vector>
#include <set>
#include <algorithm>
#include <dirent.h>
#include <cstdlib>
// Needed for the GUI
#include <QApplication>
#include <QWidget>
#include <QLineEdit>
#include <QPushButton>
#include <QComboBox>
#include <QVBoxLayout>
#include <QLabel>
#include <QFileDialog>
#include <QMessageBox>
#include "functions.h"
#include "converter_specific.h"
using namespace std;
// this Class contains the GUI Program
class ConverterApp : public QWidget {
Q_OBJECT
public:
// This defines the layout of the GUI
// make changes here to change the user interfase.
ConverterApp(const string& file_input = "",
const string& file_output = "",
QWidget *parent = nullptr) : QWidget(parent) {
// Title
setWindowTitle("File Converter");
// Text boxes
path_textbox_in = new QLineEdit(this);
path_textbox_in->setText(QString::fromStdString(file_input));
connect(path_textbox_in, &QLineEdit::textChanged, this,
&ConverterApp::setDropdownChoices);
connect(path_textbox_in, &QLineEdit::textChanged, this,
&ConverterApp::setOutputPath);
path_textbox_out = new QLineEdit(this);
path_textbox_out->setText(QString::fromStdString(file_output));
// Browse button
browse_button = new QPushButton("Browse", this);
connect(browse_button, &QPushButton::clicked, this,
&ConverterApp::browseFile);
// Drop-down menu (ComboBox)
options_combobox = new QComboBox(this);
connect(options_combobox, static_cast<void (QComboBox::*)
(const QString &)>(&QComboBox::currentTextChanged), this,
&ConverterApp::setOutputPath);
// Confirm button
confirm_button = new QPushButton("Confirm", this);
connect(confirm_button, &QPushButton::clicked, this,
&ConverterApp::startConversion);
// Layout setup
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(new QLabel("Select input file:"));
layout->addWidget(path_textbox_in);
layout->addWidget(browse_button);
layout->addWidget(new QLabel("Select desired output location:"));
layout->addWidget(path_textbox_out);
layout->addWidget(new QLabel("Select output format:"));
layout->addWidget(options_combobox);
layout->addWidget(confirm_button);
setLayout(layout);
show();
}
// Various functions the GUI program requires.
private slots:
// Gets called each time the Input Text box changes content.
// Takes the extension and checks for possible output extensions,
// then lists those in the dropdown menu.
void setDropdownChoices() {
string input_file = path_textbox_in->text().toStdString();
string input_file_ext = getExtension(input_file);
string output_file = path_textbox_out->text().toStdString();
vector<string> optionsVector = getPossibleOutput(input_file);
options_combobox->clear();
for (const auto& option : optionsVector) {
// add to options only if it isnt the already-present type
if (option != input_file_ext) {
options_combobox->addItem(QString::fromStdString(option));
}
}
}
// Gets called each time the Input Text box or the dropdown menu changes.
void setOutputPath() {
string textbox_in_content = path_textbox_in->text().toStdString();
string input_path_filename = removeExtension(textbox_in_content);
string textbox_out_content = input_path_filename + "." +
options_combobox->currentText().toStdString();
path_textbox_out->setText(QString::fromStdString(textbox_out_content));
}
// Gets called whenever the user clicks on the file chooser button.
// Sets the selected path as the content of the path input textbox.
void browseFile() {
// Open a file dialog to browse for the input file
QString file_path = QFileDialog::getOpenFileName(
this, "Select Input File", "",
"All Files (*.*);;Text Files (*.txt)");
// Check if a file was selected
if (!file_path.isEmpty()) {
// Set the selected file path in the input text box
path_textbox_in->setText(file_path);
}
}
// Gets called when the User clicks Convert.
void startConversion() {
// Implement the conversion logic.
string input_file = path_textbox_in->text().toStdString();
string output_file = path_textbox_out->text().toStdString();
// convert using gui
string converter = getConverter(input_file, output_file);
if (converter == "") {
show_error("Error!", "A unexpected Error occured!",
"No converter found. Conversion is not possible.",
true, true); // is_error, use_gui
exit(-1);
}
// converter is set
int exit_code;
if (converter == "ffmpeg") {
exit_code = ffmpeg(input_file, output_file);
} else if (converter == "soffice") {
exit_code = soffice(input_file, output_file);
} else if (converter == "pandoc") {
exit_code = pandoc(input_file, output_file);
} else if (converter == "magick") {
exit_code = magick(input_file, output_file);
} else if (converter == "compressed") {
exit_code = compressed(input_file, output_file);
} else if (converter.find("!") == 0) {
// use other converter
converter.erase(0, 1);
exit_code = userdefined(converter, input_file, output_file);
} else {
show_error("Error!", "A unexpected Error occured!",
"A unknown converter was specified by the type file.\n\
See the README on how to define your own converter.",
true, true); // is_error, use_gui
exit(-1);
}
ifstream read(output_file);
bool isEmpty = read.peek() == EOF;
if (exit_code == 0 and isEmpty == false) {
show_error("Success!",
"The file has been converted successfully!\n\
Exit Code: 0",
"" , false, true); // is_error, use_gui
exit(0);
} else if (exit_code != 0){
show_error("Error!", "A unexpected Error occured!",
"The converter was called successfully, but returned a\n\
non-zero exit code. Exit Code: " + exit_code,
true, true); // is_error, use_gui
exit(-1);
} else {
show_error("Error!", "A unexpected Error occured!",
"The converter was called successfully and returned 0,\n\
but the resulting file is empty.",
true, true); // is_error, use_gui
}
}
// Set the variables that represent the GUI elements.
private:
QLineEdit *path_textbox_in;
QLineEdit *path_textbox_out;
QPushButton *browse_button;
QComboBox *options_combobox;
QPushButton *confirm_button;
};
// main function, either converts without GUI or calls the GUI application.
int main(int argc, char *argv[]) {
string input_file = "";
string output_file = "";
// Check command line arguments
// (-i, --input, -o, --output, -h, --help, -c, --console)
vector<string> args(argv, argv+argc);
bool use_gui = true;
for (size_t i = 1; i < args.size(); ++i) {
if (args[i] == "--console" or args[i] == "-c") {
use_gui = false;
} else if (args[i] == "--input" or args[i] == "-i") {
output_file = args[i+1];
} else if (args[i] == "--output" or args[i] == "-o") {
input_file = args[i+1];
} else if (args[i] == "--help" or args[i] == "-h") {
help();
}
}
// use_gui, input_file and output_file are set
if (use_gui == false) {
// if use_gui is false, convert using the console
if (input_file == "") {
cout << "Enter a input file path: " << endl;
getline(cin, input_file);
}
if (output_file == "") {
cout << "Enter a output file path: " << endl;
getline(cin, output_file);
}
string converter = getConverter(input_file, output_file);
if (converter == "") {
show_error("Error!", "A unexpected Error occured!",
"No converter found. Conversion is not possible.",
true, false); // is_error, use_gui
exit(-1);
}
// converter is set, convert
int exit_code;
if (converter == "ffmpeg") {
exit_code = ffmpeg(input_file, output_file);
} else if (converter == "soffice") {
exit_code = soffice(input_file, output_file);
} else if (converter == "pandoc") {
exit_code = pandoc(input_file, output_file);
} else if (converter == "compressed") {
exit_code = compressed(input_file, output_file);
} else if (converter == "magick") {
exit_code = magick(input_file, output_file);
} else if (converter.find("!") == 0) {
// use other converter
converter.erase(0, 1);
exit_code = userdefined(converter, input_file, output_file);
} else {
show_error("Error!", "A unexpected Error occured!",
"A unknown converter was specified by the type file.\n\
See the README on how to define your own converter.",
true, false); // is_error, use_gui
exit(-1);
}
// check if file is empty, show error if it is
ifstream read(output_file);
bool isEmpty = read.peek() == EOF;
if (exit_code == 0 and isEmpty == false) {
show_error("Success!" , "The file has been converted successfully!",
"" , false, false); // is_error, use_gui
exit(0);
} else if (exit_code != 0){
show_error("Error!", "A unexpected Error occured!",
"The converter was called successfully, but returned a \
non-zero exit code.",
true, false); // is_error, use_gui
exit(-1);
} else {
show_error("Error!", "A unexpected Error occured!",
"The converter was called successfully and returned 0, \
but the resulting file is empty.",
true, false); // is_error, use_gui
}
} else {
// Start the GUI, the convert function for the GUI is at line 125
if (argc >= 3) {
QApplication app(argc, argv);
ConverterApp converterApp(argv[1], argv[2]);
return app.exec();
} else if (argc >= 2) {
QApplication app(argc, argv);
ConverterApp converterApp(argv[1], "");
return app.exec();
} else {
QApplication app(argc, argv);
ConverterApp converterApp("", "");
return app.exec();
}
}
}
#include "main.moc"