~format-junkie-team/format-junkie/format_junkie_bzr

« back to all changes in this revision

Viewing changes to mainwindow.cpp

  • Committer: Alex Solanos
  • Date: 2012-08-30 20:14:08 UTC
  • Revision ID: alexsol.developer@gmail.com-20120830201408-8qc9e6awjicjahn0
First testing bzr push for format junkie

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*Format Junkie
 
2
A tool for converting your media files to
 
3
all the popular formats
 
4
Copyright © 2012 by Alex Solanos and Leon Vytanos
 
5
 
 
6
This program is free software; you can redistribute it and/or
 
7
modify it under the terms of the GNU General Public License
 
8
as published by the Free Software Foundation; either version 3
 
9
of the License, or (at your option) any later version.
 
10
 
 
11
This program is distributed in the hope that it will be useful,
 
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
GNU General Public License for more details.
 
15
 
 
16
You should have received a copy of the GNU General Public License
 
17
along with this program; if not, write to the Free Software
 
18
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.*/
 
19
 
 
20
#define QT_NO_KEYWORDS
 
21
 
 
22
#include "mainwindow.h"
 
23
#include "ui_mainwindow.h"
 
24
#include "glob.h"
 
25
 
 
26
#include <iostream>
 
27
#include <unity/unity/unity.h>
 
28
#include <libnotify/notify.h>
 
29
 
 
30
#include <QIcon>
 
31
#include <QUrl>
 
32
#include <QFileDialog>
 
33
#include <QDirIterator>
 
34
#include <QProcess>
 
35
#include <QObject>
 
36
#include <QRegExp>
 
37
#include <QCloseEvent>
 
38
#include <QSettings>
 
39
#include <QShortcut>
 
40
#include <QMessageBox>
 
41
#include <QFileInfo>
 
42
#include <QGraphicsOpacityEffect>
 
43
#include <QPropertyAnimation>
 
44
#include <QDBusMessage>
 
45
#include <QDBusInterface>
 
46
#include <QMovie>
 
47
 
 
48
using namespace std;
 
49
 
 
50
QProcess *audio_converter=new QProcess(0);
 
51
QProcess *video_converter=new QProcess(0);
 
52
QProcess *image_converter=new QProcess(0);
 
53
QProcess *files_to_iso=new QProcess(0);
 
54
QProcess *iso_to_cso=new QProcess(0);
 
55
QProcess *cso_to_iso=new QProcess(0);
 
56
QProcess *sub_encoding=new QProcess(0);
 
57
float duration_secs=0;
 
58
QString input_file="";
 
59
QString output_file="";
 
60
//tmp file to be removed after the wav to m4r conversion
 
61
QString output_file_rm="";
 
62
QString command="";
 
63
QString body="";
 
64
QString file_exists="";
 
65
QString file_not_exists="";
 
66
//glob_pro_label is a string
 
67
//which is placed in a completed file
 
68
QString globpro_string="100%";
 
69
QString all_errors="";
 
70
QString current_file_errors="";
 
71
 
 
72
QString video_resize_value="hd720";
 
73
int num_files=0;
 
74
int current_file=0;
 
75
int quality=0;
 
76
int cur_index=0;
 
77
int error_count=0;
 
78
int image_conversion_result=0;
 
79
/*AUDIO PREFERENCES START*/
 
80
bool audio_bitrate=false;
 
81
bool audio_samplerate=false;
 
82
bool audio_channels=false;
 
83
bool audio_volume=false;
 
84
int audio_bitrate_value=198;
 
85
int audio_samplerate_value=44100;
 
86
int audio_channels_value=2;
 
87
int audio_volume_value=256;
 
88
int image_width=300;
 
89
int image_height=300;
 
90
int already_completed=0;
 
91
/*AUDIO PREFERENCES END*/
 
92
bool m4r_or_a_time=false;
 
93
bool m4r_or_a_previous_was_error=false;
 
94
bool faac_percent_found=false;
 
95
bool full_percent_instant=false;
 
96
bool overwrite_existing=false;
 
97
bool delete_original=false;
 
98
bool audio_stop_pressed=false;
 
99
bool video_stop_pressed=false;
 
100
bool image_stop_pressed=false;
 
101
bool sub_stop_pressed=false;
 
102
bool globpro_hasbeendeleted=false;
 
103
bool about_shown=false;
 
104
bool unity_progressbar=true;
 
105
bool unity_count=true;
 
106
bool desktop_notifications=true;
 
107
bool indicator=false;
 
108
bool image_width_height=false;
 
109
bool image_respect_ratio=false;
 
110
bool image_colors_number=false;
 
111
int image_colors_number_value=256;
 
112
bool video_crop=false;
 
113
int video_width_value=100;
 
114
int video_height_value=100;
 
115
int video_x_value=0;
 
116
int video_y_value=0;
 
117
bool video_resize=false;
 
118
bool video_bitrate=false;
 
119
bool video_framerate=false;
 
120
bool image_comment=false;
 
121
int video_bitrate_value=198;
 
122
int video_framerate_value=44100;
 
123
QString image_comment_value="";
 
124
bool image_crop=false;
 
125
bool indicator_has_been_created=false;
 
126
bool iso_to_cso_stop_pressed=false;
 
127
bool cso_to_iso_stop_pressed=false;
 
128
int image_width_value=100;
 
129
int image_height_value=100;
 
130
int image_x_value=0;
 
131
int image_y_value=0;
 
132
 
 
133
//variable to check if the program should proceed to
 
134
//faac conversion after converting to a temp file
 
135
//for converting to m4r and m4a files
 
136
//The variable becomes false when the return_code of
 
137
//ffmpeg is not 0
 
138
bool proceed_to_faac_conversion=true;
 
139
 
 
140
//notification specific...
 
141
bool first_notification=true;
 
142
NotifyNotification* notification;
 
143
gboolean            success;
 
144
GError*             error = NULL;
 
145
 
 
146
//unity launcher integration
 
147
UnityLauncherEntry *unity_progress;
 
148
 
 
149
MainWindow::MainWindow(QWidget *parent) :
 
150
    QMainWindow(parent),
 
151
    ui(new Ui::MainWindow)
 
152
{
 
153
    ui->setupUi(this);
 
154
 
 
155
    ui->stop->hide();
 
156
    ui->stop_2->hide();
 
157
    ui->stop_3->hide();
 
158
    ui->stop_6->hide();
 
159
    ui->progressBar->hide();
 
160
    ui->progressBar_2->hide();
 
161
    ui->progressBar_3->hide();
 
162
    ui->startBtn->setEnabled(false);
 
163
    ui->startBtn_2->setEnabled(false);
 
164
    ui->startBtn_3->setEnabled(false);
 
165
 
 
166
    on_after_convertion2_currentIndexChanged(ui->after_convertion2->currentIndex());
 
167
 
 
168
    //connecting audio...
 
169
    QObject::connect(audio_converter, SIGNAL(readyReadStandardOutput()), this, SLOT(audio_read_output()));
 
170
    QObject::connect(audio_converter, SIGNAL(readyReadStandardError()), this, SLOT(audio_read_output()));
 
171
    QObject::connect(audio_converter, SIGNAL(finished(int)), this, SLOT(audio_conversion(int)));
 
172
 
 
173
    //connecting video...
 
174
    QObject::connect(video_converter, SIGNAL(readyReadStandardOutput()), this, SLOT(video_read_output()));
 
175
    QObject::connect(video_converter, SIGNAL(readyReadStandardError()), this, SLOT(video_read_output()));
 
176
    QObject::connect(video_converter, SIGNAL(finished(int)),this, SLOT(video_conversion(int)));
 
177
 
 
178
    //connecting image...
 
179
    QObject::connect(image_converter, SIGNAL(readyReadStandardOutput()), this, SLOT(image_read_output()));
 
180
    QObject::connect(image_converter, SIGNAL(readyReadStandardError()), this, SLOT(image_read_output()));
 
181
    QObject::connect(image_converter, SIGNAL(finished(int)),this, SLOT(image_conversion(int)));
 
182
 
 
183
    //connection files_to_iso
 
184
    QObject::connect(files_to_iso, SIGNAL(finished(int)),this, SLOT(files_to_iso_end(int)));
 
185
 
 
186
    //connection iso_to_cso
 
187
    QObject::connect(iso_to_cso, SIGNAL(finished(int)),this, SLOT(iso_to_cso_end(int)));
 
188
 
 
189
    //connection cso_to_iso
 
190
    QObject::connect(cso_to_iso, SIGNAL(finished(int)),this, SLOT(cso_to_iso_end(int)));
 
191
 
 
192
    //connecting sub_encoding
 
193
    QObject::connect(sub_encoding, SIGNAL(finished(int)),this, SLOT(sub_encoding_end(int)));
 
194
 
 
195
    QSettings settings( "FormatJunkie", "MainWindow" );
 
196
    ui->outputFormatComboBox->setCurrentIndex(settings.value("output_format_audio").toInt());
 
197
    ui->outputFormatComboBox_2->setCurrentIndex(settings.value("output_format_video").toInt());
 
198
    ui->outputFormatComboBox_3->setCurrentIndex(settings.value("output_format_image").toInt());
 
199
    if(!add_audio && !add_video && !add_image){//<-we will head to the appropriate Index for each of these...
 
200
        cur_index=settings.value("current_index",0).toInt();
 
201
        ui->stackedWidget->setCurrentIndex(cur_index);
 
202
        switch(settings.value("current_index",0).toInt()){
 
203
        case 0:
 
204
            ui->musicButton->setChecked(true);
 
205
            break;
 
206
        case 1:
 
207
            ui->videoButton->setChecked(true);
 
208
            break;
 
209
        case 2:
 
210
            ui->imageButton->setChecked(true);
 
211
            break;
 
212
        case 3:
 
213
            ui->rom_deviceButton->setChecked(true);
 
214
            break;
 
215
        case 4:
 
216
            ui->advancedButton->setChecked(true);
 
217
            break;
 
218
        default:
 
219
            ui->musicButton->setChecked(true);
 
220
            break;
 
221
        }
 
222
    }
 
223
    else
 
224
    {
 
225
        //a command line argument is hereee (checked from main.cpp)!
 
226
        if(add_audio){
 
227
            add_audio_files();
 
228
        }
 
229
        else if(add_video){
 
230
            add_video_files();
 
231
        }
 
232
        else if(add_image){
 
233
            add_image_files();
 
234
        }
 
235
    }
 
236
 
 
237
    QSettings settings2( "FormatJunkie", "Preferences" );
 
238
    //GENERAL
 
239
    quality=settings2.value("quality", 100).toInt();
 
240
    if (quality<0 || quality>100)
 
241
        quality=100;
 
242
    overwrite_existing=settings2.value("overwrite", false).toBool();
 
243
    delete_original=settings2.value("deleteoriginal", false).toBool();
 
244
    unity_progressbar=settings2.value("unity_progressbar", true).toBool();
 
245
    unity_count=settings2.value("unity_count",true).toBool();
 
246
    desktop_notifications=settings2.value("desktop_notifications", true).toBool();
 
247
    indicator=settings2.value("indicator", false).toBool();
 
248
    //AUDIO
 
249
    audio_bitrate=settings2.value("audio_bitrate", false).toBool();
 
250
    audio_samplerate=settings2.value("audio_sample_rate", false).toBool();
 
251
    audio_channels=settings2.value("audio_channels", false).toBool();
 
252
    audio_volume=settings2.value("audio_volume", false).toBool();
 
253
    audio_bitrate_value=settings2.value("audio_bitrate_spinbox", 198).toInt();
 
254
    audio_samplerate_value=settings2.value("audio_sample_rate_spinbox", 44100).toInt();
 
255
    audio_channels_value=settings2.value("audio_channels_spinbox", 2).toInt();
 
256
    audio_volume_value=settings2.value("audio_volume_spinbox", 256).toInt();
 
257
    //VIDEO
 
258
    video_bitrate=settings2.value("video_bitrate", false).toBool();
 
259
    video_framerate=settings2.value("video_frame_rate", false).toBool();
 
260
    video_crop=settings2.value("video_crop", false).toBool();
 
261
    video_width_value=settings2.value("video_width_spinbox", 100).toInt();
 
262
    video_height_value=settings2.value("video_height_spinbox", 100).toInt();
 
263
    video_x_value=settings2.value("video_x_spinbox", 100).toInt();
 
264
    video_y_value=settings2.value("video_y_spinbox", 100).toInt();
 
265
    video_resize=settings2.value("video_resize", false).toBool();
 
266
    if(video_resize){
 
267
        set_video_resize_value(settings.value("video_resize_index",0).toInt());
 
268
    }
 
269
    //IMAGE
 
270
    image_width_height=settings2.value("image_width_height", false).toBool();
 
271
    image_width=settings2.value("image_width", 300).toInt();
 
272
    image_height=settings2.value("image_height", 300).toInt();
 
273
    image_respect_ratio=settings2.value("image_respect_ratio", false).toBool();
 
274
    image_colors_number=settings2.value("image_colors_number", false).toBool();
 
275
    image_colors_number_value=settings2.value("image_colors_number_spinbox", 256).toBool();
 
276
    image_comment=settings2.value("image_comment", false).toBool();
 
277
    image_comment_value=settings2.value("image_comment_lineedit", "").toString();
 
278
    image_crop=settings2.value("image_crop", false).toBool();
 
279
    image_width_value=settings2.value("image_width_spinbox", 100).toInt();
 
280
    image_height_value=settings2.value("image_height_spinbox", 100).toInt();
 
281
    image_x_value=settings2.value("image_x_spinbox", 100).toInt();
 
282
    image_y_value=settings2.value("image_y_spinbox", 100).toInt();
 
283
 
 
284
    QString qoutput_folder=settings.value("output",QDir::homePath()+"/Documents/FJOutput").toString();
 
285
    //crop the output folder if it doesn't fit...
 
286
    crop_output_folder(qoutput_folder);
 
287
 
 
288
    ui->tableWidget->setColumnWidth(0, 370);
 
289
    ui->tableWidget->verticalHeader()->hide();
 
290
    ui->tableWidget_2->setColumnWidth(0,370);
 
291
    ui->tableWidget_2->verticalHeader()->hide();
 
292
    ui->tableWidget_3->setColumnWidth(0,370);
 
293
    ui->tableWidget_3->verticalHeader()->hide();
 
294
    ui->tableWidget_6->setColumnWidth(0,380);
 
295
    ui->tableWidget_6->verticalHeader()->hide();
 
296
 
 
297
    globpro = new QProgressBar(this);
 
298
    globpro->hide();
 
299
 
 
300
    ui->show_errors->hide();
 
301
    setup_shortcuts();
 
302
    QDBusConnection::sessionBus().connect(QString(),QString(), "do.action", "formatjunkie_message", this, SLOT(dbus_action(QString)));
 
303
    unity_progress = unity_launcher_entry_get_for_desktop_id("formatjunkie.desktop");
 
304
 
 
305
    //accept drag en drop
 
306
    setAcceptDrops(true);
 
307
 
 
308
    //creating the indicator
 
309
    if(indicator)
 
310
        make_indicator();
 
311
}
 
312
 
 
313
MainWindow::~MainWindow()
 
314
{
 
315
    delete ui;
 
316
}
 
317
 
 
318
void MainWindow::closeEvent( QCloseEvent * ){
 
319
    QApplication::setQuitOnLastWindowClosed(true);
 
320
    QSettings settings( "FormatJunkie", "MainWindow" );
 
321
    settings.setValue( "output", ui->output_folder->toolTip() );
 
322
    settings.setValue( "output_format_audio", ui->outputFormatComboBox->currentIndex() );
 
323
    settings.setValue( "output_format_video", ui->outputFormatComboBox_2->currentIndex() );
 
324
    settings.setValue( "output_format_image", ui->outputFormatComboBox_3->currentIndex() );
 
325
    settings.setValue( "current_index", cur_index );
 
326
    settings.sync();
 
327
}
 
328
 
 
329
void MainWindow::setup_shortcuts(){
 
330
    (void) new QShortcut(Qt::CTRL + Qt::Key_P, this, SLOT(on_actionPreferences_triggered()));
 
331
    (void) new QShortcut(Qt::CTRL + Qt::Key_Q, this, SLOT(close()));
 
332
    (void) new QShortcut(Qt::CTRL + Qt::Key_M, this, SLOT(add_audio_files()));
 
333
    (void) new QShortcut(Qt::CTRL + Qt::Key_V, this, SLOT(add_video_files()));
 
334
    (void) new QShortcut(Qt::CTRL + Qt::Key_I, this, SLOT(add_image_files()));
 
335
    (void) new QShortcut(Qt::Key_F1, this, SLOT(on_actionContents_F1_triggered()));
 
336
    (void) new QShortcut(Qt::Key_Delete, this, SLOT(on_removeBtn_clicked()));
 
337
}
 
338
 
 
339
//drag and drop
 
340
void MainWindow::dragEnterEvent(QDragEnterEvent *event){
 
341
    event->acceptProposedAction();
 
342
}
 
343
 
 
344
 
 
345
void MainWindow::dropEvent(QDropEvent *event)
 
346
{
 
347
    QList<QUrl> urlList = event->mimeData()->urls();
 
348
    QStringList audio,video,image;
 
349
 
 
350
    for (QList<QUrl>::const_iterator i = urlList.begin(); i != urlList.end();i++)
 
351
    {
 
352
       QString received_file = (*i).toString();
 
353
       received_file=received_file.mid(7,received_file.length()-2);
 
354
       if(received_file.endsWith(".mp3") || received_file.endsWith(".MP3") || received_file.endsWith(".wav") || received_file.endsWith(".WAV") || received_file.endsWith(".ogg") || received_file.endsWith(".OGG") || received_file.endsWith(".wma") || received_file.endsWith(".WMA") || received_file.endsWith(".flac") || received_file.endsWith(".FLAC") || received_file.endsWith(".m4r") || received_file.endsWith(".M4R") || received_file.endsWith(".aac") || received_file.endsWith(".AAC") || received_file.endsWith(".m4a") || received_file.endsWith(".M4A") || received_file.endsWith(".mp2") || received_file.endsWith(".MP2")){
 
355
          audio << received_file;
 
356
 
 
357
       }
 
358
       else if(received_file.endsWith(".avi") || received_file.endsWith(".AVI") || received_file.endsWith(".wmv") || received_file.endsWith(".WMV") || received_file.endsWith(".ogv") || received_file.endsWith(".OGV") || received_file.endsWith(".vob") || received_file.endsWith(".VOB") || received_file.endsWith(".3gp") || received_file.endsWith(".3GP") || received_file.endsWith(".mkv") || received_file.endsWith(".MKV") || received_file.endsWith(".mpg") || received_file.endsWith(".MPG") || received_file.endsWith(".flv") || received_file.endsWith(".FLV") || received_file.endsWith(".mov") || received_file.endsWith(".MOV")){
 
359
           video << received_file;
 
360
       }
 
361
       else if(received_file.endsWith(".png") || received_file.endsWith(".gif") || received_file.endsWith(".bmp") || received_file.endsWith(".jpg") || received_file.endsWith(".jpeg") || received_file.endsWith(".svg") || received_file.endsWith(".PNG") || received_file.endsWith(".GIF") || received_file.endsWith(".BMP") || received_file.endsWith(".JPG") || received_file.endsWith(".JPEG") || received_file.endsWith(".SVG")){
 
362
           image << received_file;
 
363
       }
 
364
       else if(QDir(received_file).exists()){
 
365
           //a folder has been dropped, process it!
 
366
           process_dir(received_file);
 
367
       }
 
368
    }
 
369
    int audio_count=audio.count();
 
370
    int video_count=video.count();
 
371
    int image_count=image.count();
 
372
    if(audio_count){
 
373
        int old_r_count=ui->tableWidget->rowCount();
 
374
        ui->tableWidget->setRowCount(ui->tableWidget->rowCount()+audio_count);
 
375
        int new_r_count=ui->tableWidget->rowCount();
 
376
        QFileInfo inf;
 
377
        for(int i=old_r_count;i<new_r_count;i++){
 
378
            QString file=audio.at(i-old_r_count);
 
379
            inf.setFile(file);
 
380
            QTableWidgetItem *qtitem = new QTableWidgetItem;
 
381
            qtitem->setText(inf.completeBaseName()+"."+inf.suffix());
 
382
            qtitem->setToolTip(file);
 
383
            ui->tableWidget->setItem(i,0,qtitem);
 
384
            QTableWidgetItem *qtsize = new QTableWidgetItem;
 
385
            float size=(float)inf.size()/1048576;
 
386
            qtsize->setText(QString::number(size, 'f', 2)+" MB");
 
387
            ui->tableWidget->setItem(i,1,qtsize);
 
388
            QTableWidgetItem *qtwait = new QTableWidgetItem;
 
389
            qtwait->setText("Waiting");
 
390
            ui->tableWidget->setItem(i,2,qtwait);
 
391
        }
 
392
        if(ui->outputFormatComboBox->isEnabled())
 
393
            ui->startBtn->setEnabled(true);
 
394
        else if(unity_progressbar)
 
395
            update_unity_count_value(2);
 
396
    }
 
397
    if(video_count){
 
398
        int old_r_count=ui->tableWidget_2->rowCount();
 
399
        ui->tableWidget_2->setRowCount(ui->tableWidget_2->rowCount()+video_count);
 
400
        int new_r_count=ui->tableWidget_2->rowCount();
 
401
        QFileInfo inf;
 
402
        for(int i=old_r_count;i<new_r_count;i++){
 
403
            QString file=video.at(i-old_r_count);
 
404
            inf.setFile(file);
 
405
            QTableWidgetItem *qtitem = new QTableWidgetItem;
 
406
            qtitem->setText(inf.completeBaseName()+"."+inf.suffix());
 
407
            qtitem->setToolTip(file);
 
408
            ui->tableWidget_2->setItem(i,0,qtitem);
 
409
            QTableWidgetItem *qtsize = new QTableWidgetItem;
 
410
            float size=(float)inf.size()/1048576;
 
411
            qtsize->setText(QString::number(size, 'f', 2)+" MB");
 
412
            ui->tableWidget_2->setItem(i,1,qtsize);
 
413
            QTableWidgetItem *qtwait = new QTableWidgetItem;
 
414
            qtwait->setText("Waiting");
 
415
            ui->tableWidget_2->setItem(i,2,qtwait);
 
416
        }
 
417
        if(ui->outputFormatComboBox_2->isEnabled())
 
418
            ui->startBtn_2->setEnabled(true);
 
419
        else if(unity_progressbar)
 
420
            update_unity_count_value(2);
 
421
    }
 
422
    if(image_count){
 
423
        int old_r_count=ui->tableWidget_3->rowCount();
 
424
        ui->tableWidget_3->setRowCount(ui->tableWidget_3->rowCount()+image_count);
 
425
        int new_r_count=ui->tableWidget_3->rowCount();
 
426
        QFileInfo inf;
 
427
        for(int i=old_r_count;i<new_r_count;i++){
 
428
            QString file=image.at(i-old_r_count);
 
429
            inf.setFile(file);
 
430
            QTableWidgetItem *qtitem = new QTableWidgetItem;
 
431
            qtitem->setText(inf.completeBaseName()+"."+inf.suffix());
 
432
            qtitem->setToolTip(file);
 
433
            ui->tableWidget_3->setItem(i,0,qtitem);
 
434
            QTableWidgetItem *qtsize = new QTableWidgetItem;
 
435
            float size=(float)inf.size()/1048576;
 
436
            qtsize->setText(QString::number(size, 'f', 3)+" MB");
 
437
            ui->tableWidget_3->setItem(i,1,qtsize);
 
438
            QTableWidgetItem *qtwait = new QTableWidgetItem;
 
439
            qtwait->setText("Waiting");
 
440
            ui->tableWidget_3->setItem(i,2,qtwait);
 
441
        }
 
442
        if(ui->outputFormatComboBox_3->isEnabled())
 
443
            ui->startBtn_3->setEnabled(true);
 
444
        else if(unity_progressbar)
 
445
            update_unity_count_value(2);
 
446
    }
 
447
}
 
448
 
 
449
void MainWindow::set_video_resize_value(int index_value){
 
450
    switch(index_value)
 
451
    {
 
452
    case 0:
 
453
        video_resize_value="sqcif";
 
454
        break;
 
455
    case 1:
 
456
        video_resize_value="qqvga";
 
457
        break;
 
458
    case 2:
 
459
        video_resize_value="qcif";
 
460
        break;
 
461
    case 3:
 
462
        video_resize_value="cga";
 
463
        break;
 
464
    case 4:
 
465
        video_resize_value="qvga";
 
466
        break;
 
467
    case 5:
 
468
        video_resize_value="cif";
 
469
        break;
 
470
    case 6:
 
471
        video_resize_value="4cif";
 
472
        break;
 
473
    case 7:
 
474
        video_resize_value="ega";
 
475
        break;
 
476
    case 8:
 
477
        video_resize_value="vga";
 
478
        break;
 
479
    case 9:
 
480
        video_resize_value="svga";
 
481
        break;
 
482
    case 10:
 
483
        video_resize_value="hd480";
 
484
        break;
 
485
    case 11:
 
486
        video_resize_value="wvga";
 
487
        break;
 
488
    case 12:
 
489
        video_resize_value="xga";
 
490
        break;
 
491
    case 13:
 
492
        video_resize_value="hd720";
 
493
        break;
 
494
    case 14:
 
495
        video_resize_value="sxga";
 
496
        break;
 
497
    case 15:
 
498
        video_resize_value="wxga";
 
499
        break;
 
500
    case 16:
 
501
        video_resize_value="16cif";
 
502
        break;
 
503
    case 17:
 
504
        video_resize_value="uxga";
 
505
        break;
 
506
    case 18:
 
507
        video_resize_value="wsxga";
 
508
        break;
 
509
    case 19:
 
510
        video_resize_value="hd1080";
 
511
        break;
 
512
    case 20:
 
513
        video_resize_value="wuxga";
 
514
        break;
 
515
    case 21:
 
516
        video_resize_value="qxga";
 
517
        break;
 
518
    case 22:
 
519
        video_resize_value="woxga";
 
520
        break;
 
521
    case 23:
 
522
        video_resize_value="qsxga";
 
523
        break;
 
524
    case 24:
 
525
        video_resize_value="wqsxga";
 
526
        break;
 
527
    case 25:
 
528
        video_resize_value="wquxga";
 
529
        break;
 
530
    case 26:
 
531
        video_resize_value="hsxga";
 
532
        break;
 
533
    case 27:
 
534
        video_resize_value="whsxga";
 
535
        break;
 
536
    case 28:
 
537
        video_resize_value="whuxga";
 
538
        break;
 
539
    }
 
540
}
 
541
 
 
542
void MainWindow::add_audio_files(){
 
543
    cur_index=0;
 
544
    ui->stackedWidget->setCurrentIndex(0);
 
545
    ui->musicButton->setChecked(true);
 
546
    on_addBtn_clicked();
 
547
}
 
548
 
 
549
void MainWindow::add_video_files(){
 
550
    cur_index=1;
 
551
    ui->stackedWidget->setCurrentIndex(1);
 
552
    ui->videoButton->setChecked(true);
 
553
    on_addBtn_clicked();
 
554
}
 
555
 
 
556
void MainWindow::add_image_files(){
 
557
    cur_index=2;
 
558
    ui->stackedWidget->setCurrentIndex(2);
 
559
    ui->imageButton->setChecked(true);
 
560
    on_addBtn_clicked();
 
561
}
 
562
 
 
563
void MainWindow::crop_output_folder(QString output_folder_location){
 
564
 
 
565
    if(output_folder_location.count()>30){
 
566
        QString print_to_btn=output_folder_location.left(12)+"..."+output_folder_location.right(13);
 
567
        ui->output_folder->setText(print_to_btn);
 
568
    }
 
569
    else
 
570
        ui->output_folder->setText(output_folder_location);
 
571
    ui->output_folder->setToolTip(output_folder_location);
 
572
}
 
573
 
 
574
void MainWindow::audio_read_output(){
 
575
    //read all possible output so as to calculate the percentages...
 
576
    QByteArray newData_stdout=audio_converter->readAllStandardOutput();
 
577
    QString stdout=QString::fromLocal8Bit(newData_stdout);
 
578
    QByteArray newData_stderr=audio_converter->readAllStandardError();
 
579
    QString stderr=QString::fromLocal8Bit(newData_stderr);
 
580
    QString out=stdout+stderr;
 
581
    current_file_errors+=out;
 
582
    int format_index=ui->outputFormatComboBox->currentIndex();
 
583
        if((format_index==5 || format_index==7) && m4r_or_a_time==false && out.contains("%)|")){
 
584
            //M4R/M4A MUSIC FILES
 
585
            faac_percent_found=true;
 
586
            QRegExp regex("\\(.*%");//("  (.*%)|");
 
587
            regex.setMinimal(true);
 
588
            regex.indexIn(out);
 
589
            QString unformatted=regex.capturedTexts().at(0);
 
590
            unformatted.replace("%","");
 
591
            unformatted.replace("( ","");
 
592
            int percentage=unformatted.toInt();
 
593
            if(percentage!=0){
 
594
                int simplified = 20+percentage/1.25;
 
595
                globpro->setFormat(QString::number(simplified)+"%");
 
596
                globpro->setValue(20+percentage/1.25);
 
597
                ui->progressBar->setFormat(QString::number(simplified/(num_files-already_completed)+(current_file-1-already_completed)*(100/(num_files-already_completed)))+"%");
 
598
                ui->progressBar->setValue(simplified/(num_files-already_completed)+(current_file-1-already_completed)*(100/(num_files-already_completed)));
 
599
            }
 
600
        }
 
601
        else
 
602
        {
 
603
            //ALL OTHER MUSIC FILES
 
604
            //duration has been checked
 
605
            //Now process the output so as to get how much of the duration of
 
606
            //the output file has been processed so as to print the percentage
 
607
            if(out.contains("kB time=")){
 
608
                QRegExp regex("time=.* ");
 
609
                regex.setMinimal(true);
 
610
 
 
611
                QStringList list;
 
612
                int pos = 0;
 
613
 
 
614
                while ((pos = regex.indexIn(out, pos)) != -1)
 
615
                {
 
616
                    list << regex.cap(0).remove(QChar(' '),Qt::CaseInsensitive);
 
617
                    pos += regex.matchedLength();
 
618
                }
 
619
                if(!list.count()){
 
620
                    //error
 
621
                    return;
 
622
                }
 
623
                QString current=list.at(0);
 
624
                current.replace("time=","");
 
625
                float cur_time=current.toFloat();
 
626
                int percent=0;
 
627
                if(format_index==5 || format_index==7)
 
628
                    percent=(cur_time*100/duration_secs)/5;
 
629
                else
 
630
                    percent=cur_time*100/duration_secs;
 
631
 
 
632
                globpro->setFormat(QString::number(percent)+"%");
 
633
                globpro->setValue(percent);
 
634
                if(format_index==5 || format_index==7){
 
635
                    ui->progressBar->setFormat(QString::number(percent/(num_files-already_completed)+(current_file-already_completed)*(100/(num_files-already_completed)))+"%");
 
636
                    ui->progressBar->setValue(percent/(num_files-already_completed)+(current_file-already_completed)*(100/(num_files-already_completed)));
 
637
                    /*
 
638
                    ui->progressBar->setFormat(QString::number(percent/num_files+(current_file)*(100/num_files))+"%");
 
639
                    ui->progressBar->setValue(percent/num_files+(current_file)*(100/num_files));*/
 
640
                }
 
641
                else
 
642
                {
 
643
                    ui->progressBar->setFormat(QString::number(percent/(num_files-already_completed)+(current_file-1-already_completed)*(100/(num_files-already_completed)))+"%");
 
644
                    ui->progressBar->setValue(percent/(num_files-already_completed)+(current_file-1-already_completed)*(100/(num_files-already_completed)));
 
645
                    /*
 
646
                    ui->progressBar->setFormat(QString::number(percent/num_files+(current_file-1)*(100/num_files))+"%");
 
647
                    ui->progressBar->setValue(percent/num_files+(current_file-1)*(100/num_files));
 
648
                    */
 
649
                }
 
650
            }
 
651
        }
 
652
}
 
653
 
 
654
//Change pages when one of the 5 buttons (Audio,video,Image..) are pressed
 
655
void MainWindow::on_musicButton_clicked()
 
656
{
 
657
    ui->stackedWidget->setCurrentIndex(0);
 
658
}
 
659
 
 
660
void MainWindow::on_videoButton_clicked()
 
661
{
 
662
    ui->stackedWidget->setCurrentIndex(1);
 
663
}
 
664
 
 
665
void MainWindow::on_imageButton_clicked()
 
666
{
 
667
    ui->stackedWidget->setCurrentIndex(2);
 
668
}
 
669
 
 
670
void MainWindow::on_rom_deviceButton_clicked()
 
671
{
 
672
    ui->stackedWidget->setCurrentIndex(3);
 
673
}
 
674
 
 
675
void MainWindow::on_advancedButton_clicked()
 
676
{
 
677
 
 
678
    //encode subtitles
 
679
    //mencoder has to be installed
 
680
    int pr_index=cur_index;
 
681
    if(QFile("/usr/bin/mencoder").exists()){
 
682
        ui->stackedWidget->setCurrentIndex(4);
 
683
    }
 
684
    else
 
685
    {
 
686
        QMessageBox msgBox;
 
687
        QPushButton *Yes;
 
688
        msgBox.setWindowTitle("Format Junkie");
 
689
        msgBox.setInformativeText("This function (subtitle encoding) requires 'mencoder', while it isn't installed! Do you want to install mencoder now?");
 
690
        msgBox.setText(tr("<b>Mencoder is not installed</b>"));
 
691
        msgBox.addButton("No", QMessageBox::ActionRole);
 
692
        Yes = msgBox.addButton("Yes", QMessageBox::ActionRole);
 
693
        msgBox.setIconPixmap(QIcon::fromTheme("info").pixmap(QSize(60,60)));
 
694
        msgBox.exec();
 
695
        if (msgBox.clickedButton() == Yes){
 
696
            QMessageBox::information(this, "Mencoder Installation", "The software center will now launch. Once mencoder is installed, please try to use this function again!");
 
697
            if(system("software-center apt://mencoder&"))
 
698
                cerr << "Error while launching software-center! Please install mencoder manually using 'sudo apt-get install mencoder'\n";
 
699
            //returning to the previous index, meanwhile...
 
700
            switch(pr_index){
 
701
            case 0:
 
702
                ui->musicButton->setChecked(true);
 
703
                break;
 
704
            case 1:
 
705
                ui->videoButton->setChecked(true);
 
706
                break;
 
707
            case 2:
 
708
                ui->imageButton->setChecked(true);
 
709
                break;
 
710
            case 3:
 
711
                ui->rom_deviceButton->setChecked(true);
 
712
                break;
 
713
            default:
 
714
                ui->musicButton->setChecked(true);
 
715
                break;
 
716
            }
 
717
        }
 
718
        else
 
719
        {
 
720
            //user chose not to install mencoder, return to the previous index...
 
721
            switch(pr_index){
 
722
            case 0:
 
723
                ui->musicButton->setChecked(true);
 
724
                break;
 
725
            case 1:
 
726
                ui->videoButton->setChecked(true);
 
727
                break;
 
728
            case 2:
 
729
                ui->imageButton->setChecked(true);
 
730
                break;
 
731
            case 3:
 
732
                ui->rom_deviceButton->setChecked(true);
 
733
                break;
 
734
            default:
 
735
                ui->musicButton->setChecked(true);
 
736
                break;
 
737
            }
 
738
        }
 
739
    }
 
740
}
 
741
 
 
742
void MainWindow::on_addfolder_clicked()
 
743
{
 
744
    if (!ui->addfolder->isEnabled())
 
745
        return;
 
746
    if(cur_index==0){
 
747
 
 
748
        /* ---AUDIO--- */
 
749
 
 
750
        QString qpath;
 
751
        if(QDir(QDir::homePath()+"/Music").exists())
 
752
            qpath= QFileDialog::getExistingDirectory(this, "Choose Folder", QDir::homePath()+"/Music");
 
753
        else
 
754
            qpath= QFileDialog::getExistingDirectory(this, "Choose Folder", QDir::homePath());
 
755
        if(!qpath.count())
 
756
            return;
 
757
        //getting all the subfolders of the folder specified...
 
758
        QStringList all_folders = list_folders(qpath);
 
759
        int all_folders_count=all_folders.count();
 
760
        for(int i=0;i<all_folders_count;i++){
 
761
            //adding the files of all subfolders...
 
762
            add_files(all_folders.at(i), cur_index);
 
763
        }
 
764
 
 
765
        if (ui->tableWidget->rowCount())
 
766
            ui->startBtn->setEnabled(true);
 
767
        num_files=ui->tableWidget->rowCount();
 
768
        if(!ui->outputFormatComboBox->isEnabled())
 
769
            update_unity_count_value(1);
 
770
    }
 
771
    else if(cur_index==1){
 
772
 
 
773
        /* ---VIDEO--- */
 
774
 
 
775
        QString qpath;
 
776
        if(QDir(QDir::homePath()+"/Videos").exists())
 
777
            qpath= QFileDialog::getExistingDirectory(this, "Choose Folder", QDir::homePath()+"/Videos");
 
778
        else
 
779
            qpath= QFileDialog::getExistingDirectory(this, "Choose Folder", QDir::homePath());
 
780
        if(!qpath.count())
 
781
            return;
 
782
        //getting all the subfolders of the folder specified...
 
783
        QStringList all_folders = list_folders(qpath);
 
784
        int all_folders_count=all_folders.count();
 
785
        for(int i=0;i<all_folders_count;i++){
 
786
            //adding the files of all subfolders...
 
787
            add_files(all_folders.at(i), cur_index);
 
788
        }
 
789
 
 
790
        if (ui->tableWidget_2->rowCount())
 
791
            ui->startBtn_2->setEnabled(true);
 
792
        num_files=ui->tableWidget_2->rowCount();
 
793
        if(!ui->outputFormatComboBox_2->isEnabled())
 
794
            update_unity_count_value(1);
 
795
    }
 
796
    else if(cur_index==2){
 
797
 
 
798
        /* ---IMAGE--- */
 
799
 
 
800
        QString qpath;
 
801
        if(QDir(QDir::homePath()+"/Pictures").exists())
 
802
            qpath= QFileDialog::getExistingDirectory(this, "Choose Folder", QDir::homePath()+"/Pictures");
 
803
        else
 
804
            qpath= QFileDialog::getExistingDirectory(this, "Choose Folder", QDir::homePath());
 
805
        if(!qpath.count())
 
806
            return;
 
807
        //getting all the subfolders of the folder specified...
 
808
        QStringList all_folders = list_folders(qpath);
 
809
        int all_folders_count=all_folders.count();
 
810
        for(int i=0;i<all_folders_count;i++){
 
811
            //adding the files of all subfolders...
 
812
            add_files(all_folders.at(i), cur_index);
 
813
        }
 
814
 
 
815
        if (ui->tableWidget_3->rowCount())
 
816
            ui->startBtn_3->setEnabled(true);
 
817
        num_files=ui->tableWidget_3->rowCount();
 
818
        if(!ui->outputFormatComboBox_3->isEnabled())
 
819
            update_unity_count_value(1);
 
820
    }
 
821
    else if(cur_index==3){
 
822
        /* ---ISO IMAGES AND STUFF--- */
 
823
        QString qpath;
 
824
        if(QDir(QDir::homePath()+"/Documents").exists())
 
825
            qpath= QFileDialog::getExistingDirectory(this, "Choose Folder", QDir::homePath()+"/Documents");
 
826
        else
 
827
            qpath= QFileDialog::getExistingDirectory(this, "Choose Folder", QDir::homePath());
 
828
        if(!qpath.count())
 
829
            return;
 
830
        //getting all the subfolders of the folder specified...
 
831
        int old_r_count=ui->tableWidget_6->rowCount();
 
832
        ui->tableWidget_6->setRowCount(old_r_count+1);
 
833
        QDir dir;
 
834
        dir.setPath(qpath);
 
835
        QTableWidgetItem *qtitem = new QTableWidgetItem;
 
836
        qtitem->setText(dir.dirName());
 
837
        qtitem->setToolTip(qpath);
 
838
        ui->tableWidget_6->setItem(old_r_count,0,qtitem);
 
839
        QTableWidgetItem *qtsize = new QTableWidgetItem;
 
840
        qtsize->setText(QString::number(folder_size(qpath), 'f', 1)+" MB");
 
841
        ui->tableWidget_6->setItem(old_r_count,1,qtsize);
 
842
        ui->startBtn_8->setEnabled(true);
 
843
    }
 
844
 
 
845
}
 
846
 
 
847
void MainWindow::on_addBtn_clicked()
 
848
{
 
849
    if(!ui->addBtn->isEnabled())
 
850
        return;
 
851
    QStringList path;
 
852
    if(cur_index==0){
 
853
 
 
854
        /* ---AUDIO--- */
 
855
 
 
856
        if(QDir(QDir::homePath()+"/Music").exists())
 
857
            path = QFileDialog::getOpenFileNames(this, "Choose Audio Files", QDir::homePath()+"/Music", "*.mp3 *.wav *.ogg *.flac *.wma *.aac");
 
858
        else
 
859
            path = QFileDialog::getOpenFileNames(this, "Choose Audio Files", QDir::homePath(), "*.mp3 *.wav *.ogg *.flac *.wma *.aac");
 
860
        if(!path.count())
 
861
            return;
 
862
        //remove_complete();
 
863
        int path_count = path.count();
 
864
        int old_r_count=ui->tableWidget->rowCount();
 
865
        ui->tableWidget->setRowCount(ui->tableWidget->rowCount()+path_count);
 
866
        QFileInfo inf;
 
867
        for(int count=0; count < path_count; count++){
 
868
            QString file=path[count];
 
869
            inf.setFile(file);
 
870
            QTableWidgetItem *qtitem = new QTableWidgetItem;
 
871
            qtitem->setText(inf.completeBaseName()+"."+inf.suffix());
 
872
            qtitem->setToolTip(file);
 
873
            ui->tableWidget->setItem(old_r_count+count,0,qtitem);
 
874
            QTableWidgetItem *qtsize = new QTableWidgetItem;
 
875
            float size=(float)inf.size()/1048576;
 
876
            qtsize->setText(QString::number(size, 'f', 2)+" MB");
 
877
            ui->tableWidget->setItem(old_r_count+count,1,qtsize);
 
878
            QTableWidgetItem *qtwait = new QTableWidgetItem;
 
879
            qtwait->setText("Waiting");
 
880
            ui->tableWidget->setItem(old_r_count+count,2,qtwait);
 
881
        }
 
882
        if (ui->tableWidget->rowCount())
 
883
            ui->startBtn->setEnabled(true);
 
884
        num_files=ui->tableWidget->rowCount();
 
885
        if(!ui->outputFormatComboBox->isEnabled())
 
886
            update_unity_count_value(0);
 
887
    }
 
888
    else if(cur_index==1){
 
889
 
 
890
        /* ---VIDEO--- */
 
891
 
 
892
        if(QDir(QDir::homePath()+"/Videos").exists())
 
893
            path = QFileDialog::getOpenFileNames(this, "Choose Video Files", QDir::homePath()+"/Videos", "*.avi *.ogv *.vob *.mp4 *.3gp *.wmv *.mkv *.mpg *.mov *.flv");
 
894
        else
 
895
            path = QFileDialog::getOpenFileNames(this, "Choose Video Files", QDir::homePath(), "*.avi *.ogv *.vob *.mp4 *.3gp *.wmv *.mkv *.mpg *.mov *.flv");
 
896
        if(!path.count())
 
897
            return;
 
898
        //remove_complete();
 
899
        int path_count = path.count();
 
900
        int old_r_count=ui->tableWidget_2->rowCount();
 
901
        ui->tableWidget_2->setRowCount(ui->tableWidget_2->rowCount()+path_count);
 
902
        QFileInfo inf;
 
903
        for(int count=0; count < path_count; count++){
 
904
            QString file=path[count];
 
905
            inf.setFile(file);
 
906
            QTableWidgetItem *qtitem = new QTableWidgetItem;
 
907
            qtitem->setText(inf.completeBaseName()+"."+inf.suffix());
 
908
            qtitem->setToolTip(file);
 
909
            ui->tableWidget_2->setItem(old_r_count+count,0,qtitem);
 
910
            QTableWidgetItem *qtsize = new QTableWidgetItem;
 
911
            float size=(float)inf.size()/1048576;
 
912
            qtsize->setText(QString::number(size, 'f', 2)+" MB");
 
913
            ui->tableWidget_2->setItem(old_r_count+count,1,qtsize);
 
914
            QTableWidgetItem *qtwait = new QTableWidgetItem;
 
915
            qtwait->setText("Waiting");
 
916
            ui->tableWidget_2->setItem(old_r_count+count,2,qtwait);
 
917
        }
 
918
        if (ui->tableWidget_2->rowCount())
 
919
            ui->startBtn_2->setEnabled(true);
 
920
        num_files=ui->tableWidget_2->rowCount();
 
921
        if(!ui->outputFormatComboBox_2->isEnabled())
 
922
            update_unity_count_value(0);
 
923
    }
 
924
    else if(cur_index==2){
 
925
 
 
926
        /* ---PICTURE--- */
 
927
 
 
928
        if(QDir(QDir::homePath()+"/Pictures").exists())
 
929
            path = QFileDialog::getOpenFileNames(this, "Choose Image Files", QDir::homePath()+"/Pictures", "*.png *.jpg *.jpeg *.gif *.bmp *.svg");
 
930
        else
 
931
            path = QFileDialog::getOpenFileNames(this, "Choose Image Files", QDir::homePath(), "*.png *.jpg *.jpeg *.gif *.bmp *.svg");
 
932
        if(!path.count())
 
933
            return;
 
934
        //remove_complete();
 
935
        int path_count = path.count();
 
936
        int old_r_count=ui->tableWidget_3->rowCount();
 
937
        ui->tableWidget_3->setRowCount(ui->tableWidget_3->rowCount()+path_count);
 
938
        QFileInfo inf;
 
939
        for(int count=0; count < path_count; count++){
 
940
            QString file=path[count];
 
941
            inf.setFile(file);
 
942
            QTableWidgetItem *qtitem = new QTableWidgetItem;
 
943
            qtitem->setText(inf.completeBaseName()+"."+inf.suffix());
 
944
            qtitem->setToolTip(file);
 
945
            ui->tableWidget_3->setItem(old_r_count+count,0,qtitem);
 
946
            QTableWidgetItem *qtsize = new QTableWidgetItem;
 
947
            float size=(float)inf.size()/1048576;
 
948
            qtsize->setText(QString::number(size, 'f', 3)+" MB");
 
949
            ui->tableWidget_3->setItem(old_r_count+count,1,qtsize);
 
950
            QTableWidgetItem *qtwait = new QTableWidgetItem;
 
951
            qtwait->setText("Waiting");
 
952
            ui->tableWidget_3->setItem(old_r_count+count,2,qtwait);
 
953
        }
 
954
        if (ui->tableWidget_3->rowCount())
 
955
            ui->startBtn_3->setEnabled(true);
 
956
        num_files=ui->tableWidget_3->rowCount();
 
957
        if(unity_count && !ui->outputFormatComboBox_3->isEnabled())
 
958
            update_unity_count_value(0);
 
959
    }
 
960
    else if(cur_index==3){
 
961
 
 
962
        /* ---CSO/ISO AND STUFF--- */
 
963
 
 
964
        path = QFileDialog::getOpenFileNames(this, "Choose Files", QDir::homePath());
 
965
        if(!path.count())
 
966
            return;
 
967
        int path_count = path.count();
 
968
        int old_r_count=ui->tableWidget_6->rowCount();
 
969
        ui->tableWidget_6->setRowCount(ui->tableWidget_6->rowCount()+path_count);
 
970
        QFileInfo inf;
 
971
        for(int count=0; count < path_count; count++){
 
972
            QString file=path[count];
 
973
            inf.setFile(file);
 
974
            QTableWidgetItem *qtitem = new QTableWidgetItem;
 
975
            qtitem->setText(inf.completeBaseName()+"."+inf.suffix());
 
976
            qtitem->setToolTip(file);
 
977
            ui->tableWidget_6->setItem(old_r_count+count,0,qtitem);
 
978
            QTableWidgetItem *qtsize = new QTableWidgetItem;
 
979
            float size=(float)inf.size()/1048576;
 
980
            qtsize->setText(QString::number(size, 'f', 2)+" MB");
 
981
            ui->tableWidget_6->setItem(old_r_count+count,1,qtsize);
 
982
            QTableWidgetItem *qtwait = new QTableWidgetItem;
 
983
            qtwait->setText("Waiting");
 
984
            ui->tableWidget_6->setItem(old_r_count+count,2,qtwait);
 
985
        }
 
986
        if (ui->tableWidget_6->rowCount())
 
987
            ui->startBtn_8->setEnabled(true);
 
988
    }
 
989
}
 
990
 
 
991
void MainWindow::remove_complete(){
 
992
    int r_count=ui->tableWidget->rowCount();
 
993
    for(int i=0;i<r_count;i++){
 
994
        if(ui->tableWidget->item(0,2)->toolTip()=="Completed"){
 
995
            ui->tableWidget->removeRow(0);
 
996
        }
 
997
        else{
 
998
            /*
 
999
              Only files in a row can be 'Completed'. The 'Completed'
 
1000
              files are not allowed to move in any possible way, thus
 
1001
              it is logical to break this for loop after an item is to
 
1002
              be found not labeled as 'Completed'...
 
1003
            */
 
1004
            break;
 
1005
        }
 
1006
    }
 
1007
}
 
1008
 
 
1009
 
 
1010
void MainWindow::clear_convertion(){
 
1011
    QMessageBox msgBox;
 
1012
    QPushButton *Continue;
 
1013
    msgBox.setWindowTitle("Format Junkie");
 
1014
    msgBox.setInformativeText("By clearing the list the current process will stop. Are you sure you want to do so?");
 
1015
    msgBox.setText(tr("<b>Clear while process is running.</b>"));
 
1016
    msgBox.addButton("Cancel", QMessageBox::ActionRole);
 
1017
    Continue = msgBox.addButton("Continue", QMessageBox::ActionRole);
 
1018
    msgBox.setIconPixmap(QIcon::fromTheme("info").pixmap(QSize(60,60)));
 
1019
    msgBox.exec();
 
1020
    if (msgBox.clickedButton() == Continue){
 
1021
 
 
1022
        //stop the current conversion progress
 
1023
        if(cur_index==0){
 
1024
            //audio
 
1025
            on_stop_clicked();
 
1026
        }
 
1027
        else if(cur_index==1){
 
1028
            //video
 
1029
            on_stop_2_clicked();
 
1030
        }
 
1031
        else if(cur_index==2){
 
1032
            //image
 
1033
            on_stop_3_clicked();
 
1034
        }
 
1035
        //clear
 
1036
        on_clearBtn_clicked();
 
1037
    }
 
1038
}
 
1039
 
 
1040
void MainWindow::on_clearBtn_clicked()
 
1041
{
 
1042
    if(cur_index==0){
 
1043
        //audio
 
1044
        if(ui->outputFormatComboBox->isEnabled()){
 
1045
            //the conversion process is off!
 
1046
            ui->tableWidget->setRowCount(0);
 
1047
            ui->startBtn->setEnabled(false);
 
1048
        }
 
1049
        else
 
1050
        {
 
1051
            //the conversion is running! Display an informative message!
 
1052
            clear_convertion();
 
1053
        }
 
1054
    }
 
1055
    else if(cur_index==1){
 
1056
        //video
 
1057
        if(ui->outputFormatComboBox_2->isEnabled()){
 
1058
            //the conversion process is off!
 
1059
            ui->tableWidget_2->setRowCount(0);
 
1060
            ui->startBtn_2->setEnabled(false);
 
1061
        }
 
1062
        else
 
1063
        {
 
1064
            //the conversion is running! Display an informative message!
 
1065
            clear_convertion();
 
1066
        }
 
1067
    }
 
1068
    else if(cur_index==2){
 
1069
        //image
 
1070
        if(ui->outputFormatComboBox_3->isEnabled()){
 
1071
            //the conversion process is off!
 
1072
            ui->tableWidget_3->setRowCount(0);
 
1073
            ui->startBtn_3->setEnabled(false);
 
1074
        }
 
1075
        else
 
1076
        {
 
1077
            //the conversion is running! Display an informative message!
 
1078
            clear_convertion();
 
1079
        }
 
1080
    }
 
1081
    else if(cur_index==3){
 
1082
        //burn
 
1083
        ui->tableWidget_6->setRowCount(0);
 
1084
        ui->startBtn_8->setEnabled(false);
 
1085
    }
 
1086
}
 
1087
 
 
1088
 
 
1089
void MainWindow::on_removeBtn_clicked()
 
1090
{
 
1091
    if(cur_index==0){
 
1092
        /* ---AUDIO---*/
 
1093
        if(!ui->tableWidget->rowCount())
 
1094
            return;
 
1095
        int cur_row=ui->tableWidget->currentRow();
 
1096
        if(ui->tableWidget->cellWidget(cur_row,2) && ui->tableWidget->item(cur_row,2)->toolTip()!="Completed"){
 
1097
            //this file is being processed
 
1098
            return;
 
1099
        }
 
1100
        if(!ui->outputFormatComboBox->isEnabled()){
 
1101
            if(ui->tableWidget->currentRow()<current_file)
 
1102
                current_file--;
 
1103
            else
 
1104
            {
 
1105
                if(unity_progressbar)
 
1106
                    update_unity_count_value(2);
 
1107
            }
 
1108
            num_files--;
 
1109
        }
 
1110
        if (ui->tableWidget->rowCount()==1)
 
1111
        {
 
1112
            ui->tableWidget->setRowCount(0);
 
1113
            ui->startBtn->setEnabled(false);
 
1114
        }
 
1115
        else
 
1116
        {
 
1117
            if(cur_row!=0){
 
1118
                if(ui->tableWidget->item(cur_row-1,2)->toolTip()=="Completed"){
 
1119
                    if((cur_row+1)==ui->tableWidget->rowCount())
 
1120
                        ui->startBtn->setEnabled(false);
 
1121
                }
 
1122
            }
 
1123
            ui->tableWidget->removeRow(cur_row);
 
1124
        }
 
1125
    }
 
1126
    else if(cur_index==1){
 
1127
 
 
1128
        /* ---VIDEO---*/
 
1129
        if(!ui->tableWidget_2->rowCount())
 
1130
            return;
 
1131
        int cur_row=ui->tableWidget_2->currentRow();
 
1132
        if(ui->tableWidget_2->cellWidget(cur_row,2) && ui->tableWidget_2->item(cur_row,2)->toolTip()!="Completed"){
 
1133
            //this file is being processed
 
1134
            return;
 
1135
        }
 
1136
        if(!ui->outputFormatComboBox_2->isEnabled()){
 
1137
            if(ui->tableWidget_2->currentRow()<current_file)
 
1138
                current_file--;
 
1139
            else
 
1140
            {
 
1141
                if(unity_count)
 
1142
                    update_unity_count_value(2);
 
1143
            }
 
1144
            num_files--;
 
1145
        }
 
1146
        if (ui->tableWidget_2->rowCount()==1)
 
1147
        {
 
1148
            ui->tableWidget_2->setRowCount(0);
 
1149
            ui->startBtn_2->setEnabled(false);
 
1150
        }
 
1151
        else
 
1152
            ui->tableWidget_2->removeRow(cur_row);
 
1153
    }
 
1154
    else if(cur_index==2){
 
1155
 
 
1156
        /* ---PICTURE---*/
 
1157
        if(!ui->tableWidget_3->rowCount())
 
1158
            return;
 
1159
        int cur_row=ui->tableWidget_3->currentRow();
 
1160
        if(ui->tableWidget_3->cellWidget(cur_row,2) && ui->tableWidget_3->item(cur_row,2)->toolTip()!="Completed"){
 
1161
            //this file is being processed
 
1162
            return;
 
1163
        }
 
1164
        if(!ui->outputFormatComboBox_3->isEnabled()){
 
1165
            if(ui->tableWidget_3->currentRow()<current_file)
 
1166
                current_file--;
 
1167
            else
 
1168
            {
 
1169
                if(unity_count)
 
1170
                    update_unity_count_value(2);
 
1171
            }
 
1172
            num_files--;
 
1173
            ui->progressBar_3->setValue((current_file-1)*100/num_files);
 
1174
            ui->progressBar_3->setFormat(QString::number(ui->progressBar_3->value())+"%");
 
1175
        }
 
1176
        if (ui->tableWidget_3->rowCount()==1)
 
1177
        {
 
1178
            ui->tableWidget_3->setRowCount(0);
 
1179
            ui->startBtn_3->setEnabled(false);
 
1180
        }
 
1181
        else
 
1182
        {
 
1183
            if(cur_row!=0){
 
1184
                if(ui->tableWidget_3->item(cur_row-1,2)->toolTip()=="Completed"){
 
1185
                    if((cur_row+1)==ui->tableWidget_3->rowCount())
 
1186
                        ui->startBtn_3->setEnabled(false);
 
1187
                }
 
1188
            }
 
1189
            ui->tableWidget_3->removeRow(cur_row);
 
1190
        }
 
1191
    }
 
1192
    else if(cur_index==3){
 
1193
        if(!ui->tableWidget_6->rowCount())
 
1194
            return;
 
1195
        int cur_row=ui->tableWidget_6->currentRow();
 
1196
        if (ui->tableWidget_6->rowCount()==1)
 
1197
        {
 
1198
            ui->tableWidget_6->setRowCount(0);
 
1199
            ui->startBtn_8->setEnabled(false);
 
1200
        }
 
1201
        else
 
1202
        {
 
1203
            ui->tableWidget_6->removeRow(cur_row);
 
1204
        }
 
1205
    }
 
1206
}
 
1207
 
 
1208
void MainWindow::on_moveupButton_clicked()
 
1209
{
 
1210
    if(cur_index==0){
 
1211
 
 
1212
        /* ---AUDIO--- */
 
1213
 
 
1214
        if(ui->tableWidget->rowCount()>1 && ui->tableWidget->selectionModel()->hasSelection() && ui->tableWidget->currentRow()!=0){
 
1215
            if(ui->tableWidget->item(ui->tableWidget->currentRow(),2)->text()!="Waiting")
 
1216
                return;
 
1217
            if(ui->tableWidget->item(ui->tableWidget->currentRow()-1,2)->text()!="Waiting")
 
1218
                return;
 
1219
            if(ui->tableWidget->cellWidget(ui->tableWidget->currentRow(),2))
 
1220
                return;
 
1221
            if(ui->tableWidget->cellWidget(ui->tableWidget->currentRow()-1,2))
 
1222
                return;
 
1223
 
 
1224
            QStringList item_up;
 
1225
            item_up << ui->tableWidget->item(ui->tableWidget->selectedItems().at(0)->row()-1,0)->text();
 
1226
            item_up << ui->tableWidget->item(ui->tableWidget->selectedItems().at(0)->row()-1,1)->text();
 
1227
            item_up << ui->tableWidget->item(ui->tableWidget->selectedItems().at(0)->row()-1,2)->text();
 
1228
            item_up << ui->tableWidget->item(ui->tableWidget->selectedItems().at(0)->row()-1,2)->toolTip();
 
1229
            item_up << ui->tableWidget->item(ui->tableWidget->selectedItems().at(0)->row()-1,0)->toolTip();
 
1230
            ui->tableWidget->item(ui->tableWidget->selectedItems().at(0)->row()-1,0)->setText(ui->tableWidget->item(ui->tableWidget->selectedItems().at(0)->row(),0)->text());
 
1231
            ui->tableWidget->item(ui->tableWidget->selectedItems().at(0)->row()-1,1)->setText(ui->tableWidget->item(ui->tableWidget->selectedItems().at(0)->row(),1)->text());
 
1232
            ui->tableWidget->item(ui->tableWidget->selectedItems().at(0)->row()-1,2)->setText(ui->tableWidget->item(ui->tableWidget->selectedItems().at(0)->row(),2)->text());
 
1233
            ui->tableWidget->item(ui->tableWidget->selectedItems().at(0)->row()-1,2)->setToolTip(ui->tableWidget->item(ui->tableWidget->selectedItems().at(0)->row(),2)->toolTip());
 
1234
            ui->tableWidget->item(ui->tableWidget->selectedItems().at(0)->row()-1,0)->setToolTip(ui->tableWidget->item(ui->tableWidget->selectedItems().at(0)->row(),0)->toolTip());
 
1235
            ui->tableWidget->item(ui->tableWidget->selectedItems().at(0)->row(),0)->setText(item_up.at(0));
 
1236
            ui->tableWidget->item(ui->tableWidget->selectedItems().at(0)->row(),1)->setText(item_up.at(1));
 
1237
            ui->tableWidget->item(ui->tableWidget->selectedItems().at(0)->row(),2)->setText(item_up.at(2));
 
1238
            ui->tableWidget->item(ui->tableWidget->selectedItems().at(0)->row(),2)->setToolTip(item_up.at(3));
 
1239
            ui->tableWidget->item(ui->tableWidget->selectedItems().at(0)->row(),0)->setToolTip(item_up.at(4));
 
1240
            ui->tableWidget->selectRow(ui->tableWidget->selectedItems().at(0)->row()-1);
 
1241
        }
 
1242
    }
 
1243
    else if(cur_index==1){
 
1244
 
 
1245
        /* ---VIDEO--- */
 
1246
 
 
1247
        if(ui->tableWidget_2->rowCount()>1 && ui->tableWidget_2->selectionModel()->hasSelection() && ui->tableWidget_2->currentRow()!=0){
 
1248
            if(ui->tableWidget_2->item(ui->tableWidget_2->currentRow(),2)->text()!="Waiting")
 
1249
                return;
 
1250
            if(ui->tableWidget_2->item(ui->tableWidget_2->currentRow()-1,2)->text()!="Waiting")
 
1251
                return;
 
1252
            if(ui->tableWidget_2->cellWidget(ui->tableWidget_2->currentRow(),2))
 
1253
                return;
 
1254
            if(ui->tableWidget_2->cellWidget(ui->tableWidget_2->currentRow()-1,2))
 
1255
                return;
 
1256
 
 
1257
            QStringList item_up;
 
1258
            item_up << ui->tableWidget_2->item(ui->tableWidget_2->selectedItems().at(0)->row()-1,0)->text();
 
1259
            item_up << ui->tableWidget_2->item(ui->tableWidget_2->selectedItems().at(0)->row()-1,1)->text();
 
1260
            item_up << ui->tableWidget_2->item(ui->tableWidget_2->selectedItems().at(0)->row()-1,2)->text();
 
1261
            item_up << ui->tableWidget_2->item(ui->tableWidget_2->selectedItems().at(0)->row()-1,2)->toolTip();
 
1262
            item_up << ui->tableWidget_2->item(ui->tableWidget_2->selectedItems().at(0)->row()-1,0)->toolTip();
 
1263
            ui->tableWidget_2->item(ui->tableWidget_2->selectedItems().at(0)->row()-1,0)->setText(ui->tableWidget_2->item(ui->tableWidget_2->selectedItems().at(0)->row(),0)->text());
 
1264
            ui->tableWidget_2->item(ui->tableWidget_2->selectedItems().at(0)->row()-1,1)->setText(ui->tableWidget_2->item(ui->tableWidget_2->selectedItems().at(0)->row(),1)->text());
 
1265
            ui->tableWidget_2->item(ui->tableWidget_2->selectedItems().at(0)->row()-1,2)->setText(ui->tableWidget_2->item(ui->tableWidget_2->selectedItems().at(0)->row(),2)->text());
 
1266
            ui->tableWidget_2->item(ui->tableWidget_2->selectedItems().at(0)->row()-1,2)->setToolTip(ui->tableWidget_2->item(ui->tableWidget_2->selectedItems().at(0)->row(),2)->toolTip());
 
1267
            ui->tableWidget_2->item(ui->tableWidget_2->selectedItems().at(0)->row()-1,0)->setToolTip(ui->tableWidget_2->item(ui->tableWidget_2->selectedItems().at(0)->row(),0)->toolTip());
 
1268
            ui->tableWidget_2->item(ui->tableWidget_2->selectedItems().at(0)->row(),0)->setText(item_up.at(0));
 
1269
            ui->tableWidget_2->item(ui->tableWidget_2->selectedItems().at(0)->row(),1)->setText(item_up.at(1));
 
1270
            ui->tableWidget_2->item(ui->tableWidget_2->selectedItems().at(0)->row(),2)->setText(item_up.at(2));
 
1271
            ui->tableWidget_2->item(ui->tableWidget_2->selectedItems().at(0)->row(),2)->setToolTip(item_up.at(3));
 
1272
            ui->tableWidget_2->item(ui->tableWidget_2->selectedItems().at(0)->row(),0)->setToolTip(item_up.at(4));
 
1273
            ui->tableWidget_2->selectRow(ui->tableWidget_2->selectedItems().at(0)->row()-1);
 
1274
        }
 
1275
    }
 
1276
    else if(cur_index==2){
 
1277
 
 
1278
        /* ---PICTURE--- */
 
1279
 
 
1280
        if(ui->tableWidget_3->rowCount()>1 && ui->tableWidget_3->selectionModel()->hasSelection() && ui->tableWidget_3->currentRow()!=0){
 
1281
            if(ui->tableWidget_3->item(ui->tableWidget_3->currentRow(),2)->text()!="Waiting")
 
1282
                return;
 
1283
            if(ui->tableWidget_3->item(ui->tableWidget_3->currentRow()-1,2)->text()!="Waiting")
 
1284
                return;
 
1285
            if(ui->tableWidget_3->cellWidget(ui->tableWidget_3->currentRow(),2))
 
1286
                return;
 
1287
            if(ui->tableWidget_3->cellWidget(ui->tableWidget_3->currentRow()-1,2))
 
1288
                return;
 
1289
 
 
1290
            QStringList item_up;
 
1291
            item_up << ui->tableWidget_3->item(ui->tableWidget_3->selectedItems().at(0)->row()-1,0)->text();
 
1292
            item_up << ui->tableWidget_3->item(ui->tableWidget_3->selectedItems().at(0)->row()-1,1)->text();
 
1293
            item_up << ui->tableWidget_3->item(ui->tableWidget_3->selectedItems().at(0)->row()-1,2)->text();
 
1294
            item_up << ui->tableWidget_3->item(ui->tableWidget_3->selectedItems().at(0)->row()-1,2)->toolTip();
 
1295
            item_up << ui->tableWidget_3->item(ui->tableWidget_3->selectedItems().at(0)->row()-1,0)->toolTip();
 
1296
            ui->tableWidget_3->item(ui->tableWidget_3->selectedItems().at(0)->row()-1,0)->setText(ui->tableWidget_3->item(ui->tableWidget_3->selectedItems().at(0)->row(),0)->text());
 
1297
            ui->tableWidget_3->item(ui->tableWidget_3->selectedItems().at(0)->row()-1,1)->setText(ui->tableWidget_3->item(ui->tableWidget_3->selectedItems().at(0)->row(),1)->text());
 
1298
            ui->tableWidget_3->item(ui->tableWidget_3->selectedItems().at(0)->row()-1,2)->setText(ui->tableWidget_3->item(ui->tableWidget_3->selectedItems().at(0)->row(),2)->text());
 
1299
            ui->tableWidget_3->item(ui->tableWidget_3->selectedItems().at(0)->row()-1,2)->setToolTip(ui->tableWidget_3->item(ui->tableWidget_3->selectedItems().at(0)->row(),2)->toolTip());
 
1300
            ui->tableWidget_3->item(ui->tableWidget_3->selectedItems().at(0)->row()-1,0)->setToolTip(ui->tableWidget_3->item(ui->tableWidget_3->selectedItems().at(0)->row(),0)->toolTip());
 
1301
            ui->tableWidget_3->item(ui->tableWidget_3->selectedItems().at(0)->row(),0)->setText(item_up.at(0));
 
1302
            ui->tableWidget_3->item(ui->tableWidget_3->selectedItems().at(0)->row(),1)->setText(item_up.at(1));
 
1303
            ui->tableWidget_3->item(ui->tableWidget_3->selectedItems().at(0)->row(),2)->setText(item_up.at(2));
 
1304
            ui->tableWidget_3->item(ui->tableWidget_3->selectedItems().at(0)->row(),2)->setToolTip(item_up.at(3));
 
1305
            ui->tableWidget_3->item(ui->tableWidget_3->selectedItems().at(0)->row(),0)->setToolTip(item_up.at(4));
 
1306
            ui->tableWidget_3->selectRow(ui->tableWidget_3->selectedItems().at(0)->row()-1);
 
1307
        }
 
1308
    }
 
1309
    else if(cur_index==3){
 
1310
        if(ui->tableWidget_6->rowCount()>1 && ui->tableWidget_6->selectionModel()->hasSelection() && ui->tableWidget_6->currentRow()!=0){
 
1311
            QStringList item_up;
 
1312
            item_up << ui->tableWidget_6->item(ui->tableWidget_6->selectedItems().at(0)->row()-1,0)->text();
 
1313
            item_up << ui->tableWidget_6->item(ui->tableWidget_6->selectedItems().at(0)->row()-1,1)->text();
 
1314
            item_up << ui->tableWidget_6->item(ui->tableWidget_6->selectedItems().at(0)->row()-1,0)->toolTip();
 
1315
            ui->tableWidget_6->item(ui->tableWidget_6->selectedItems().at(0)->row()-1,0)->setText(ui->tableWidget_6->item(ui->tableWidget_6->selectedItems().at(0)->row(),0)->text());
 
1316
            ui->tableWidget_6->item(ui->tableWidget_6->selectedItems().at(0)->row()-1,1)->setText(ui->tableWidget_6->item(ui->tableWidget_6->selectedItems().at(0)->row(),1)->text());
 
1317
            ui->tableWidget_6->item(ui->tableWidget_6->selectedItems().at(0)->row()-1,0)->setToolTip(ui->tableWidget_6->item(ui->tableWidget_6->selectedItems().at(0)->row(),0)->toolTip());
 
1318
            ui->tableWidget_6->item(ui->tableWidget_6->selectedItems().at(0)->row(),0)->setText(item_up.at(0));
 
1319
            ui->tableWidget_6->item(ui->tableWidget_6->selectedItems().at(0)->row(),1)->setText(item_up.at(1));
 
1320
            ui->tableWidget_6->item(ui->tableWidget_6->selectedItems().at(0)->row(),0)->setToolTip(item_up.at(2));
 
1321
            ui->tableWidget_6->selectRow(ui->tableWidget_6->selectedItems().at(0)->row()-1);
 
1322
        }
 
1323
    }
 
1324
}
 
1325
 
 
1326
void MainWindow::on_movedownButton_clicked()
 
1327
{
 
1328
    if(cur_index==0){
 
1329
 
 
1330
        /* ---AUDIO--- */
 
1331
 
 
1332
        if(ui->tableWidget->rowCount()>1 && ui->tableWidget->selectionModel()->hasSelection() && ui->tableWidget->currentRow()!=ui->tableWidget->rowCount()-1){
 
1333
            if(ui->tableWidget->item(ui->tableWidget->currentRow(),2)->text()!="Waiting")
 
1334
                return;
 
1335
            if(ui->tableWidget->item(ui->tableWidget->currentRow()+1,2)->text()!="Waiting")
 
1336
                return;
 
1337
            if(ui->tableWidget->cellWidget(ui->tableWidget->currentRow(),2))
 
1338
                return;
 
1339
            if(ui->tableWidget->cellWidget(ui->tableWidget->currentRow()+1,2))
 
1340
                return;
 
1341
            QStringList item_down;
 
1342
            item_down << ui->tableWidget->item(ui->tableWidget->selectedItems().at(0)->row()+1,0)->text();
 
1343
            item_down << ui->tableWidget->item(ui->tableWidget->selectedItems().at(0)->row()+1,1)->text();
 
1344
            item_down << ui->tableWidget->item(ui->tableWidget->selectedItems().at(0)->row()+1,2)->text();
 
1345
            item_down << ui->tableWidget->item(ui->tableWidget->selectedItems().at(0)->row()+1,2)->toolTip();
 
1346
            item_down << ui->tableWidget->item(ui->tableWidget->selectedItems().at(0)->row()+1,0)->toolTip();
 
1347
            ui->tableWidget->item(ui->tableWidget->selectedItems().at(0)->row()+1,0)->setText(ui->tableWidget->item(ui->tableWidget->selectedItems().at(0)->row(),0)->text());
 
1348
            ui->tableWidget->item(ui->tableWidget->selectedItems().at(0)->row()+1,1)->setText(ui->tableWidget->item(ui->tableWidget->selectedItems().at(0)->row(),1)->text());
 
1349
            ui->tableWidget->item(ui->tableWidget->selectedItems().at(0)->row()+1,2)->setText(ui->tableWidget->item(ui->tableWidget->selectedItems().at(0)->row(),2)->text());
 
1350
            ui->tableWidget->item(ui->tableWidget->selectedItems().at(0)->row()+1,2)->setToolTip(ui->tableWidget->item(ui->tableWidget->selectedItems().at(0)->row(),2)->toolTip());
 
1351
            ui->tableWidget->item(ui->tableWidget->selectedItems().at(0)->row()+1,0)->setToolTip(ui->tableWidget->item(ui->tableWidget->selectedItems().at(0)->row(),0)->toolTip());
 
1352
            ui->tableWidget->item(ui->tableWidget->selectedItems().at(0)->row(),0)->setText(item_down.at(0));
 
1353
            ui->tableWidget->item(ui->tableWidget->selectedItems().at(0)->row(),1)->setText(item_down.at(1));
 
1354
            ui->tableWidget->item(ui->tableWidget->selectedItems().at(0)->row(),2)->setText(item_down.at(2));
 
1355
            ui->tableWidget->item(ui->tableWidget->selectedItems().at(0)->row(),2)->setToolTip(item_down.at(3));
 
1356
            ui->tableWidget->item(ui->tableWidget->selectedItems().at(0)->row(),0)->setToolTip(item_down.at(4));
 
1357
            ui->tableWidget->selectRow(ui->tableWidget->selectedItems().at(0)->row()+1);
 
1358
        }
 
1359
    }
 
1360
    else if(cur_index==1){
 
1361
 
 
1362
        /* ---VIDEO--- */
 
1363
 
 
1364
        if(ui->tableWidget_2->rowCount()>1 && ui->tableWidget_2->selectionModel()->hasSelection() && ui->tableWidget_2->currentRow()!=ui->tableWidget_2->rowCount()-1){
 
1365
            if(ui->tableWidget_2->item(ui->tableWidget_2->currentRow(),2)->text()!="Waiting")
 
1366
                return;
 
1367
            if(ui->tableWidget_2->item(ui->tableWidget_2->currentRow()+1,2)->text()!="Waiting")
 
1368
                return;
 
1369
            if(ui->tableWidget_2->cellWidget(ui->tableWidget_2->currentRow(),2))
 
1370
                return;
 
1371
            if(ui->tableWidget_2->cellWidget(ui->tableWidget_2->currentRow()+1,2))
 
1372
                return;
 
1373
            QStringList item_down;
 
1374
            item_down << ui->tableWidget_2->item(ui->tableWidget_2->selectedItems().at(0)->row()+1,0)->text();
 
1375
            item_down << ui->tableWidget_2->item(ui->tableWidget_2->selectedItems().at(0)->row()+1,1)->text();
 
1376
            item_down << ui->tableWidget_2->item(ui->tableWidget_2->selectedItems().at(0)->row()+1,2)->text();
 
1377
            item_down << ui->tableWidget_2->item(ui->tableWidget_2->selectedItems().at(0)->row()+1,2)->toolTip();
 
1378
            item_down << ui->tableWidget_2->item(ui->tableWidget_2->selectedItems().at(0)->row()+1,0)->toolTip();
 
1379
            ui->tableWidget_2->item(ui->tableWidget_2->selectedItems().at(0)->row()+1,0)->setText(ui->tableWidget_2->item(ui->tableWidget_2->selectedItems().at(0)->row(),0)->text());
 
1380
            ui->tableWidget_2->item(ui->tableWidget_2->selectedItems().at(0)->row()+1,1)->setText(ui->tableWidget_2->item(ui->tableWidget_2->selectedItems().at(0)->row(),1)->text());
 
1381
            ui->tableWidget_2->item(ui->tableWidget_2->selectedItems().at(0)->row()+1,2)->setText(ui->tableWidget_2->item(ui->tableWidget_2->selectedItems().at(0)->row(),2)->text());
 
1382
            ui->tableWidget_2->item(ui->tableWidget_2->selectedItems().at(0)->row()+1,2)->setToolTip(ui->tableWidget_2->item(ui->tableWidget_2->selectedItems().at(0)->row(),2)->toolTip());
 
1383
            ui->tableWidget_2->item(ui->tableWidget_2->selectedItems().at(0)->row()+1,0)->setToolTip(ui->tableWidget_2->item(ui->tableWidget_2->selectedItems().at(0)->row(),0)->toolTip());
 
1384
            ui->tableWidget_2->item(ui->tableWidget_2->selectedItems().at(0)->row(),0)->setText(item_down.at(0));
 
1385
            ui->tableWidget_2->item(ui->tableWidget_2->selectedItems().at(0)->row(),1)->setText(item_down.at(1));
 
1386
            ui->tableWidget_2->item(ui->tableWidget_2->selectedItems().at(0)->row(),2)->setText(item_down.at(2));
 
1387
            ui->tableWidget_2->item(ui->tableWidget_2->selectedItems().at(0)->row(),2)->setToolTip(item_down.at(3));
 
1388
            ui->tableWidget_2->item(ui->tableWidget_2->selectedItems().at(0)->row(),0)->setToolTip(item_down.at(4));
 
1389
            ui->tableWidget_2->selectRow(ui->tableWidget_2->selectedItems().at(0)->row()+1);
 
1390
        }
 
1391
    }
 
1392
    else if(cur_index==2){
 
1393
 
 
1394
        /* ---PICTURE--- */
 
1395
 
 
1396
        if(ui->tableWidget_3->rowCount()>1 && ui->tableWidget_3->selectionModel()->hasSelection() && ui->tableWidget_3->currentRow()!=ui->tableWidget_3->rowCount()-1){
 
1397
            if(ui->tableWidget_3->item(ui->tableWidget_3->currentRow(),2)->text()!="Waiting")
 
1398
                return;
 
1399
            if(ui->tableWidget_3->item(ui->tableWidget_3->currentRow()+1,2)->text()!="Waiting")
 
1400
                return;
 
1401
            if(ui->tableWidget_3->cellWidget(ui->tableWidget_3->currentRow(),2))
 
1402
                return;
 
1403
            if(ui->tableWidget_3->cellWidget(ui->tableWidget_3->currentRow()+1,2))
 
1404
                return;
 
1405
            QStringList item_down;
 
1406
            item_down << ui->tableWidget_3->item(ui->tableWidget_3->selectedItems().at(0)->row()+1,0)->text();
 
1407
            item_down << ui->tableWidget_3->item(ui->tableWidget_3->selectedItems().at(0)->row()+1,1)->text();
 
1408
            item_down << ui->tableWidget_3->item(ui->tableWidget_3->selectedItems().at(0)->row()+1,2)->text();
 
1409
            item_down << ui->tableWidget_3->item(ui->tableWidget_3->selectedItems().at(0)->row()+1,2)->toolTip();
 
1410
            item_down << ui->tableWidget_3->item(ui->tableWidget_3->selectedItems().at(0)->row()+1,0)->toolTip();
 
1411
            ui->tableWidget_3->item(ui->tableWidget_3->selectedItems().at(0)->row()+1,0)->setText(ui->tableWidget_3->item(ui->tableWidget_3->selectedItems().at(0)->row(),0)->text());
 
1412
            ui->tableWidget_3->item(ui->tableWidget_3->selectedItems().at(0)->row()+1,1)->setText(ui->tableWidget_3->item(ui->tableWidget_3->selectedItems().at(0)->row(),1)->text());
 
1413
            ui->tableWidget_3->item(ui->tableWidget_3->selectedItems().at(0)->row()+1,2)->setText(ui->tableWidget_3->item(ui->tableWidget_3->selectedItems().at(0)->row(),2)->text());
 
1414
            ui->tableWidget_3->item(ui->tableWidget_3->selectedItems().at(0)->row()+1,2)->setToolTip(ui->tableWidget_3->item(ui->tableWidget_3->selectedItems().at(0)->row(),2)->toolTip());
 
1415
            ui->tableWidget_3->item(ui->tableWidget_3->selectedItems().at(0)->row()+1,0)->setToolTip(ui->tableWidget_3->item(ui->tableWidget_3->selectedItems().at(0)->row(),0)->toolTip());
 
1416
            ui->tableWidget_3->item(ui->tableWidget_3->selectedItems().at(0)->row(),0)->setText(item_down.at(0));
 
1417
            ui->tableWidget_3->item(ui->tableWidget_3->selectedItems().at(0)->row(),1)->setText(item_down.at(1));
 
1418
            ui->tableWidget_3->item(ui->tableWidget_3->selectedItems().at(0)->row(),2)->setText(item_down.at(2));
 
1419
            ui->tableWidget_3->item(ui->tableWidget_3->selectedItems().at(0)->row(),2)->setToolTip(item_down.at(3));
 
1420
            ui->tableWidget_3->item(ui->tableWidget_3->selectedItems().at(0)->row(),0)->setToolTip(item_down.at(4));
 
1421
            ui->tableWidget_3->selectRow(ui->tableWidget_3->selectedItems().at(0)->row()+1);
 
1422
        }
 
1423
    }
 
1424
    else if(cur_index){
 
1425
        if(ui->tableWidget_6->rowCount()>1 && ui->tableWidget_6->selectionModel()->hasSelection() && ui->tableWidget_6->currentRow()!=ui->tableWidget_6->rowCount()-1){
 
1426
            QStringList item_down;
 
1427
            item_down << ui->tableWidget_6->item(ui->tableWidget_6->selectedItems().at(0)->row()+1,0)->text();
 
1428
            item_down << ui->tableWidget_6->item(ui->tableWidget_6->selectedItems().at(0)->row()+1,1)->text();
 
1429
            item_down << ui->tableWidget_6->item(ui->tableWidget_6->selectedItems().at(0)->row()+1,0)->toolTip();
 
1430
            ui->tableWidget_6->item(ui->tableWidget_6->selectedItems().at(0)->row()+1,0)->setText(ui->tableWidget_6->item(ui->tableWidget_6->selectedItems().at(0)->row(),0)->text());
 
1431
            ui->tableWidget_6->item(ui->tableWidget_6->selectedItems().at(0)->row()+1,1)->setText(ui->tableWidget_6->item(ui->tableWidget_6->selectedItems().at(0)->row(),1)->text());
 
1432
            ui->tableWidget_6->item(ui->tableWidget_6->selectedItems().at(0)->row()+1,0)->setToolTip(ui->tableWidget_6->item(ui->tableWidget_6->selectedItems().at(0)->row(),0)->toolTip());
 
1433
            ui->tableWidget_6->item(ui->tableWidget_6->selectedItems().at(0)->row(),0)->setText(item_down.at(0));
 
1434
            ui->tableWidget_6->item(ui->tableWidget_6->selectedItems().at(0)->row(),1)->setText(item_down.at(1));
 
1435
            ui->tableWidget_6->item(ui->tableWidget_6->selectedItems().at(0)->row(),0)->setToolTip(item_down.at(2));
 
1436
            ui->tableWidget_6->selectRow(ui->tableWidget_6->selectedItems().at(0)->row()+1);
 
1437
        }
 
1438
    }
 
1439
}
 
1440
 
 
1441
void MainWindow::update_unity_count_value(int after){
 
1442
    /*
 
1443
      'after' handles the remove button.
 
1444
      If after is 2, then the user has pressed the remove button
 
1445
      (so, after it has been done current_file++), but this function
 
1446
      is also called from audio_conversion(int) function, before the current_file++
 
1447
    */
 
1448
    if(after==1)
 
1449
        unity_launcher_entry_set_count (unity_progress, num_files-current_file-1);
 
1450
    else if(after==0)
 
1451
        unity_launcher_entry_set_count (unity_progress, num_files-current_file);
 
1452
    else if(after==2)
 
1453
        unity_launcher_entry_set_count (unity_progress,unity_launcher_entry_get_count(unity_progress)-1);
 
1454
}
 
1455
 
 
1456
void MainWindow::on_startBtn_clicked()
 
1457
{
 
1458
    if(!ui->tableWidget->rowCount())
 
1459
        return;
 
1460
    if(!QDir(ui->output_folder->toolTip()).exists()){
 
1461
        QDir dir;
 
1462
        if(!dir.mkpath(ui->output_folder->toolTip())){
 
1463
            QMessageBox::warning(this, tr("Error"), tr("The output folder doesn't exist! Please specify an existent directory!"));
 
1464
            return;
 
1465
        }
 
1466
    }
 
1467
    audio_stop_pressed=false;
 
1468
    num_files=ui->tableWidget->rowCount();
 
1469
    if(unity_count)
 
1470
        unity_launcher_entry_set_count (unity_progress, num_files);
 
1471
    ui->progressBar->setFormat("0%");
 
1472
    ui->progressBar->setValue(0);
 
1473
    ui->stop->raise();
 
1474
    progressbar_animation(0,0); //<< doesn't this look like an owl?
 
1475
 
 
1476
    already_completed=0;
 
1477
    for(int i=0;i<num_files;i++){
 
1478
        if(ui->tableWidget->item(i,2)->toolTip()!="Completed"){
 
1479
            current_file=i;
 
1480
            already_completed=i;
 
1481
            break;
 
1482
        }
 
1483
    }
 
1484
    if(current_file==0){
 
1485
        //that's a completely new conversion
 
1486
        all_errors.clear();
 
1487
        error_count=0;
 
1488
        ui->show_errors->hide();
 
1489
    }
 
1490
    ui->outputFormatComboBox->setEnabled(false);
 
1491
    globpro_string="100%";
 
1492
    if(unity_progressbar)
 
1493
        unity_launcher_entry_set_progress_visible(unity_progress, true);
 
1494
    if(unity_count)
 
1495
        unity_launcher_entry_set_count_visible(unity_progress, true);
 
1496
    audio_conversion(0);
 
1497
}
 
1498
 
 
1499
void MainWindow::actions_after_convert(){
 
1500
    int conv2_index=ui->after_convertion2->currentIndex();
 
1501
    if(conv2_index==5)
 
1502
        this->close();
 
1503
    else if(conv2_index!=0){
 
1504
        /*
 
1505
          1=shutdown
 
1506
          2=suspend
 
1507
          3=restart
 
1508
          4=lock
 
1509
        */
 
1510
        QString after_convert_cmd="";
 
1511
        switch(conv2_index){
 
1512
        case 1:
 
1513
            after_convert_cmd="dbus-send --system --print-reply --dest=org.freedesktop.ConsoleKit /org/freedesktop/ConsoleKit/Manager org.freedesktop.ConsoleKit.Manager.Stop > /dev/null 2> /dev/null&";
 
1514
            break;
 
1515
        case 2:
 
1516
            after_convert_cmd="dbus-send --system --print-reply --dest=org.freedesktop.UPower /org/freedesktop/UPower org.freedesktop.UPower.Suspend > /dev/null 2> /dev/null&";
 
1517
            break;
 
1518
        case 3:
 
1519
            after_convert_cmd="dbus-send --system --print-reply --dest=org.freedesktop.ConsoleKit /org/freedesktop/ConsoleKit/Manager org.freedesktop.ConsoleKit.Manager.Restart > /dev/null 2> /dev/null&";
 
1520
            break;
 
1521
        case 4:
 
1522
            after_convert_cmd="gnome-screensaver-command --lock > /dev/null 2> /dev/null&";
 
1523
            break;
 
1524
        default:
 
1525
            break;
 
1526
        }
 
1527
        if(system(after_convert_cmd.toLocal8Bit().data()))
 
1528
            cerr << QString("Error running command '"+after_convert_cmd+"'\n").toLocal8Bit().data();
 
1529
    }
 
1530
}
 
1531
 
 
1532
void MainWindow::audio_conversion(int return_code){
 
1533
    /*
 
1534
      This function is called like audio_conversion(0) the 1st time
 
1535
      so as to begin the conversion. Then, it is called again
 
1536
      when the 1st conversion has finished and launches the
 
1537
      2nd conversion and so on, untill no files are left
 
1538
      for conversion.
 
1539
    */
 
1540
    //Values:
 
1541
    //current_file <-> 0 1 2
 
1542
    //num_files    <-> 1 2 3
 
1543
    int format_index=ui->outputFormatComboBox->currentIndex();
 
1544
    QString format_text=ui->outputFormatComboBox->currentText();
 
1545
    //checking if the conversion is about m4(a/r) files or other
 
1546
    if(format_index==5 || format_index==7){
 
1547
        /*^^^^^^^^^^^^^^^^^^^^^^^^^^^M4A OR M4R FILES^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
 
1548
        if(audio_stop_pressed){
 
1549
            if(ui->tableWidget->item(current_file-1,2)->toolTip()!="Completed")
 
1550
                ui->tableWidget->removeCellWidget(current_file-1,2);
 
1551
            else
 
1552
                ui->tableWidget->removeCellWidget(current_file,2);
 
1553
            globpro_hasbeendeleted=true;
 
1554
            QFileInfo inf;
 
1555
            inf.setFile(output_file);
 
1556
            QString tmp_file_rm="/tmp/"+inf.completeBaseName()+".wav";
 
1557
            QString output_file_rm=ui->output_folder->toolTip()+"/"+inf.completeBaseName()+"."+format_text;
 
1558
            QFile::remove(tmp_file_rm);
 
1559
            QFile::remove(output_file_rm);
 
1560
            m4r_or_a_time=false;
 
1561
            return;
 
1562
        }
 
1563
        if(return_code){
 
1564
            /*
 
1565
              The conversion of the previous file wasn't successful!
 
1566
              Print error and guide the user to see the error logs...
 
1567
            */
 
1568
            if(all_errors.isEmpty())
 
1569
                all_errors+="'<b>"+input_file+"</b>'<br>";
 
1570
            else
 
1571
                all_errors+="\n'<b>"+input_file+"</b>'<br>";
 
1572
            current_file_errors.replace("\n","<br>");
 
1573
            if(m4r_or_a_time){
 
1574
                //error while converting the input file to temp wav
 
1575
                //current_file_errors=input_file+": File non-existent or bad filetype!<br>";
 
1576
                proceed_to_faac_conversion=false;
 
1577
            }
 
1578
            all_errors+="<font color=red>"+current_file_errors+"</font>";
 
1579
            QTableWidgetItem *item_error1 = new QTableWidgetItem;
 
1580
            item_error1->setText(ui->tableWidget->item(current_file,0)->text()+" - ERROR");
 
1581
            item_error1->setToolTip(ui->tableWidget->item(current_file,0)->toolTip());
 
1582
            item_error1->setBackgroundColor(Qt::red);
 
1583
            item_error1->setTextColor(Qt::white);
 
1584
            ui->tableWidget->setItem(current_file,0, item_error1);
 
1585
            QTableWidgetItem *item_error2 = new QTableWidgetItem;
 
1586
            item_error2->setText(ui->tableWidget->item(current_file,1)->text());
 
1587
            item_error2->setToolTip(ui->tableWidget->item(current_file,1)->toolTip());
 
1588
            item_error2->setBackgroundColor(Qt::red);
 
1589
            item_error2->setTextColor(Qt::white);
 
1590
            ui->tableWidget->setItem(current_file,1, item_error2);
 
1591
            ui->show_errors->show();
 
1592
            //take it like the percentage was found, so as to continue normally to the next file...
 
1593
            m4r_or_a_previous_was_error=true;
 
1594
            error_count++;
 
1595
        }
 
1596
        else
 
1597
        {
 
1598
            //IF NOT RETURN CODE
 
1599
            if(!m4r_or_a_time){
 
1600
                //if it is to convert to m4r
 
1601
                current_file_errors.clear();
 
1602
            }
 
1603
            /*
 
1604
              The conversion of the previous file was successful!
 
1605
              Check if the original (source) file is to be deleted...
 
1606
            */
 
1607
            if(delete_original){
 
1608
                if(QFile(input_file).exists()){
 
1609
                    if(system(QString("rm -f \""+input_file+"\"").toLocal8Bit().data()))
 
1610
                        cerr << QString("Could not delete source file '"+input_file+"'\nDo I have the permissions to do so?\n").toLocal8Bit().data();
 
1611
                }
 
1612
            }
 
1613
        }
 
1614
        if (current_file>num_files-1){
 
1615
            /*
 
1616
              !!!ALL FILES HAVE BEEN CONVERTED!!!
 
1617
              Update the progressbars and do the actions
 
1618
              that the user has chosen to do from bottom
 
1619
              right!!!
 
1620
            */
 
1621
            //just update the progressbar
 
1622
            ui->progressBar->setFormat("100%");
 
1623
            ui->progressBar->setValue(100);
 
1624
            globpro->setFormat(globpro_string);
 
1625
            globpro->setValue(100);
 
1626
            QTableWidgetItem *item_progress = new QTableWidgetItem;
 
1627
            item_progress->setToolTip("Completed");
 
1628
            ui->tableWidget->setItem(current_file-1,2, item_progress);
 
1629
            ui->startBtn->setEnabled(false);
 
1630
            ui->startBtn->raise();
 
1631
            progressbar_animation(1,0);
 
1632
            //removing the last tmp file...
 
1633
            QFile::remove(output_file_rm);
 
1634
            ui->outputFormatComboBox->setEnabled(true);
 
1635
            if(unity_progressbar)
 
1636
                unity_launcher_entry_set_progress_visible(unity_progress, false);
 
1637
            if(unity_count)
 
1638
                unity_launcher_entry_set_count_visible(unity_progress, false);
 
1639
            if(desktop_notifications)
 
1640
                show_desktop_notification(0);
 
1641
            error_count=0;
 
1642
            actions_after_convert();
 
1643
            return;
 
1644
        }
 
1645
        if(ui->tableWidget->item(current_file,2)->toolTip()=="Completed"){
 
1646
            /*
 
1647
              The file's conversion has been completed, proceed to the next file...
 
1648
              The program goes through here if the user add more files after the
 
1649
              previous conversion has finished and choose to start converting...
 
1650
            */
 
1651
            current_file++;
 
1652
            audio_conversion(0);
 
1653
            return;
 
1654
        }
 
1655
        if(!m4r_or_a_time || globpro_string=="Already Exists"){
 
1656
            /*
 
1657
              The program always goes through there, unless
 
1658
              the user has chosen to convert the file to m4r
 
1659
              and this is the 2nd conversion that has to be
 
1660
              done (audio files to m4r are being converted
 
1661
              to wav first and then to m4r with faac)
 
1662
              So, if(m4r_or_a_time) then the conversion is 50%,
 
1663
              because a 2nd conversion has to be done...
 
1664
            */
 
1665
            /*if(globpro_hasbeendeleted){
 
1666
                globpro = new QProgressBar(this);
 
1667
                globpro_hasbeendeleted=false;
 
1668
            }*/
 
1669
            if(current_file!=0){
 
1670
                if(globpro_hasbeendeleted){
 
1671
                    globpro = new QProgressBar(this);
 
1672
                    globpro_hasbeendeleted=false;
 
1673
                }
 
1674
                if(globpro->format()!="Already Exists" && ui->tableWidget->item(current_file-1,2)->toolTip()!="Completed"){
 
1675
                    QProgressBar *qtpro=new QProgressBar(this);
 
1676
                    ui->tableWidget->setCellWidget(current_file-1, 2, qtpro);
 
1677
                    QTableWidgetItem *item_progress = new QTableWidgetItem;
 
1678
                    item_progress->setToolTip("Completed");
 
1679
                    ui->tableWidget->setItem(current_file-1,2, item_progress);
 
1680
                    qtpro->setFormat(globpro_string);
 
1681
                    globpro_string="100%";
 
1682
                    qtpro->setValue(100);
 
1683
                }
 
1684
            }
 
1685
            if(!ui->tableWidget->cellWidget(current_file,2)){
 
1686
                QProgressBar *qtpro=new QProgressBar(this);
 
1687
                ui->tableWidget->setCellWidget(current_file, 2, qtpro);
 
1688
                if(!globpro)
 
1689
                    globpro = new QProgressBar(this);
 
1690
                globpro=qtpro;
 
1691
            }
 
1692
        }
 
1693
        //Action starts here...
 
1694
        input_file=ui->tableWidget->item(current_file,0)->toolTip();
 
1695
        QFileInfo info;
 
1696
        info.setFile(input_file);
 
1697
        if(!m4r_or_a_time){
 
1698
            //Convert the input file to a temp wav file
 
1699
            if(current_file!=0){
 
1700
                //a tmp file has been created from the previous conversion. Delete this file...
 
1701
                QFile::remove(output_file_rm);
 
1702
            }
 
1703
            m4r_or_a_time=true;
 
1704
            if(m4r_or_a_previous_was_error)
 
1705
                faac_percent_found=true;
 
1706
            if(!faac_percent_found && current_file!=0 && globpro_string!="Already Exists"){
 
1707
                /*
 
1708
                  We couldn't find the percentage sent by
 
1709
                  faac, because the file was extremely small
 
1710
                  so, go instantly to 100%
 
1711
                */
 
1712
                full_percent_instant=true;
 
1713
            }
 
1714
            output_file=output_file_rm="/tmp/"+info.completeBaseName()+".wav";
 
1715
            if(QFile(output_file).exists()){
 
1716
                if(!QFile::remove(output_file))
 
1717
                    cerr << QString("Error removing temp file '"+output_file+"'\n").toLocal8Bit().data();
 
1718
            }
 
1719
            command="avconv -i \""+input_file+"\" -aq "+QString::number(quality);
 
1720
            if(audio_bitrate)
 
1721
                command+=" -ab "+QString::number(audio_bitrate_value)+"k";//<- a custom bitrate has been selected!
 
1722
            if(audio_samplerate)
 
1723
                command+=" -ar "+QString::number(audio_samplerate_value);
 
1724
            if(audio_channels)
 
1725
                command+=" -ac "+QString::number(audio_channels_value);
 
1726
            if(audio_volume)
 
1727
                command+=" -vol "+QString::number(audio_volume_value);
 
1728
            command+=" \""+output_file+"\"";
 
1729
        }
 
1730
        else{
 
1731
            if(!proceed_to_faac_conversion){
 
1732
                proceed_to_faac_conversion=true;
 
1733
                //next file PLEASE :D
 
1734
                current_file++;
 
1735
                m4r_or_a_time=false;
 
1736
                audio_conversion(0);
 
1737
                return;
 
1738
            }
 
1739
            m4r_or_a_time=false;
 
1740
            faac_percent_found=false;
 
1741
            output_file=ui->output_folder->toolTip()+"/"+info.completeBaseName()+"."+format_text;
 
1742
            command="faac -b 192 -c 44100 \"/tmp/"+info.completeBaseName()+".wav\" --mpeg-vers 4 -o \""+output_file+"\"";
 
1743
        }
 
1744
        /*
 
1745
          Checking whether the output_file already exists. If it does, then
 
1746
          overwrite it, if it checked so, or proceed to the next file, if not...
 
1747
         */
 
1748
        //checking if the final file exists, not if the temp wav file exists...
 
1749
        if(m4r_or_a_time){
 
1750
            QString file_rm=ui->output_folder->toolTip()+"/"+info.completeBaseName()+"."+format_text;
 
1751
            if(QFile(file_rm).exists()){
 
1752
                if(overwrite_existing){
 
1753
                    if(!QFile::remove(file_rm)){
 
1754
                        cerr << QString("Couldn't remove the already existing file '"+output_file+"'\n").toLocal8Bit().data();
 
1755
                        current_file++;
 
1756
                        globpro_string="Already Exists";
 
1757
                        faac_percent_found=true;
 
1758
                        m4r_or_a_time=false;
 
1759
                        audio_conversion(0);
 
1760
                        return;
 
1761
                    }
 
1762
                }
 
1763
                else
 
1764
                {
 
1765
                    current_file++;
 
1766
                    globpro_string="Already Exists";
 
1767
                    faac_percent_found=true;
 
1768
                    m4r_or_a_time=false;
 
1769
                    audio_conversion(0);
 
1770
                    return;
 
1771
                }
 
1772
            }
 
1773
        }
 
1774
        //getting the duration of the input file...
 
1775
        duration_secs=Global::duration(input_file);
 
1776
        audio_converter->start(command);
 
1777
        if(full_percent_instant){
 
1778
            full_percent_instant=false;
 
1779
            if(!m4r_or_a_previous_was_error){
 
1780
                m4r_or_a_previous_was_error=false;
 
1781
                globpro->setValue(100);
 
1782
                globpro->setFormat(globpro_string);
 
1783
            }
 
1784
        }
 
1785
        if(!m4r_or_a_time){
 
1786
            current_file++;
 
1787
        }
 
1788
        else
 
1789
        {
 
1790
            update_unity_count_value(0);
 
1791
        }
 
1792
 
 
1793
    }
 
1794
    else
 
1795
    {
 
1796
        /*^^^^^^^^^^^^^^^^^^^^^^^^^^^ALL OTHER AUDIO FILES^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
 
1797
        if(audio_stop_pressed){
 
1798
            if(current_file!=0)
 
1799
                ui->tableWidget->removeCellWidget(current_file-1,2);
 
1800
            else
 
1801
                ui->tableWidget->removeCellWidget(current_file,2);
 
1802
            globpro_hasbeendeleted=true;
 
1803
            if(!QFile::remove(output_file))
 
1804
                cerr << QString("Error removing file '"+output_file+"'\n").toLocal8Bit().data();
 
1805
            return;
 
1806
        }
 
1807
        if(return_code){
 
1808
            /*
 
1809
              The conversion of the previous file wasn't successful!
 
1810
              Print error and guide the user to see the error logs...
 
1811
            */
 
1812
            if(all_errors.isEmpty())
 
1813
                all_errors+="'<b>"+input_file+"</b>'<br>";
 
1814
            else
 
1815
                all_errors+="\n'<b>"+input_file+"</b>'<br>";
 
1816
            current_file_errors.replace("\n","<br>");
 
1817
            all_errors+="<font color=red>"+current_file_errors+"</font><br>";
 
1818
            int to_apply=current_file;
 
1819
            if(to_apply>0)
 
1820
                to_apply--;
 
1821
            QTableWidgetItem *item_error1 = new QTableWidgetItem;
 
1822
            item_error1->setText(ui->tableWidget->item(to_apply,0)->text()+" - ERROR");
 
1823
            item_error1->setToolTip(ui->tableWidget->item(to_apply,0)->toolTip());
 
1824
            item_error1->setBackgroundColor(Qt::red);
 
1825
            item_error1->setTextColor(Qt::white);
 
1826
            ui->tableWidget->setItem(to_apply,0, item_error1);
 
1827
            QTableWidgetItem *item_error2 = new QTableWidgetItem;
 
1828
            item_error2->setText(ui->tableWidget->item(to_apply,1)->text());
 
1829
            item_error2->setToolTip(ui->tableWidget->item(to_apply,1)->toolTip());
 
1830
            item_error2->setBackgroundColor(Qt::red);
 
1831
            item_error2->setTextColor(Qt::white);
 
1832
            ui->tableWidget->setItem(to_apply,1, item_error2);
 
1833
            ui->show_errors->show();
 
1834
            current_file_errors.clear();
 
1835
            error_count++;
 
1836
        }
 
1837
        else
 
1838
        {
 
1839
            //IF NOT RETURN CODE
 
1840
            current_file_errors.clear();
 
1841
            /*
 
1842
              The conversion of the previous file was successful!
 
1843
              Check if the original (source) file is to be deleted...
 
1844
            */
 
1845
            if(delete_original){
 
1846
                if(QFile(input_file).exists()){
 
1847
                    if(system(QString("rm -f \""+input_file+"\"").toLocal8Bit().data()))
 
1848
                        cerr << QString("Could not delete source file '"+input_file+"'\nDo I have the permissions to do so?\n").toLocal8Bit().data();
 
1849
                }
 
1850
            }
 
1851
        }
 
1852
        if (current_file>num_files-1){
 
1853
            /*
 
1854
              !!!ALL FILES HAVE BEEN CONVERTED!!!
 
1855
              Update the progressbars and do the actions
 
1856
              that the user has chosen to do from bottom
 
1857
              right!!!
 
1858
            */
 
1859
            //all files have been converted!
 
1860
            //just update the progressbar
 
1861
            ui->progressBar->setFormat("100%");
 
1862
            ui->progressBar->setValue(100);
 
1863
            globpro->setFormat(globpro_string);
 
1864
            globpro->setValue(100);
 
1865
            QTableWidgetItem *item_progress = new QTableWidgetItem;
 
1866
            item_progress->setToolTip("Completed");
 
1867
            ui->tableWidget->setItem(current_file-1,2, item_progress);
 
1868
            ui->startBtn->setEnabled(false);
 
1869
            ui->startBtn->raise();
 
1870
            progressbar_animation(1,0);
 
1871
            ui->outputFormatComboBox->setEnabled(true);
 
1872
            if(unity_progressbar)
 
1873
                unity_launcher_entry_set_progress_visible(unity_progress, false);
 
1874
            if(unity_count)
 
1875
                unity_launcher_entry_set_count_visible(unity_progress, false);
 
1876
            if(desktop_notifications)
 
1877
                show_desktop_notification(0);
 
1878
            actions_after_convert();
 
1879
            return;
 
1880
        }
 
1881
        if(ui->tableWidget->item(current_file,2)->toolTip()=="Completed"){
 
1882
            /*
 
1883
              The file's conversion has been completed, proceed to the next file...
 
1884
              The program goes through here if the user add more files after the
 
1885
              previous conversion has finished and choose to start converting...
 
1886
            */
 
1887
            current_file++;
 
1888
            audio_conversion(0);
 
1889
            return;
 
1890
        }
 
1891
        if(globpro_string=="Already Exists" || !m4r_or_a_time){
 
1892
            /*
 
1893
              The program always goes through there, unless
 
1894
              the user has chosen to convert the file to m4r
 
1895
              and this is the 2nd conversion that has to be
 
1896
              done (audio files to m4r are being converted
 
1897
              to wav first and then to m4r with faac)
 
1898
              So, if(m4r_or_a_time) then the conversion is 50%,
 
1899
              because a 2nd conversion has to be done...
 
1900
            */
 
1901
            if(current_file!=0){
 
1902
                if(globpro_hasbeendeleted){
 
1903
                    globpro = new QProgressBar(this);
 
1904
                    globpro_hasbeendeleted=false;
 
1905
                }
 
1906
                if(globpro->format()!="Already Exists" && ui->tableWidget->item(current_file-1,2)->toolTip()!="Completed"){
 
1907
                    QProgressBar *qtpro=new QProgressBar(this);
 
1908
                    ui->tableWidget->setCellWidget(current_file-1, 2, qtpro);
 
1909
                    QTableWidgetItem *item_progress = new QTableWidgetItem;
 
1910
                    item_progress->setToolTip("Completed");
 
1911
                    ui->tableWidget->setItem(current_file-1,2, item_progress);
 
1912
                    qtpro->setFormat(globpro_string);
 
1913
                    qtpro->setValue(100);
 
1914
                }
 
1915
            }
 
1916
            QProgressBar *qtpro=new QProgressBar(this);
 
1917
            ui->tableWidget->setCellWidget(current_file, 2, qtpro);
 
1918
            if(!globpro)
 
1919
                globpro = new QProgressBar(this);
 
1920
            globpro=qtpro;
 
1921
        }
 
1922
        //Action starts here...
 
1923
        input_file=ui->tableWidget->item(current_file,0)->toolTip();
 
1924
        QFileInfo info;
 
1925
        info.setFile(input_file);
 
1926
        output_file=ui->output_folder->toolTip()+"/"+info.completeBaseName()+"."+format_text;
 
1927
        command="avconv -i \""+input_file+"\" -aq "+QString::number(quality);
 
1928
        if(format_text=="aac")
 
1929
            command+=" -acodec aac -strict experimental";
 
1930
        if(audio_bitrate)
 
1931
            command+=" -ab "+QString::number(audio_bitrate_value)+"k";//<- a custom bitrate has been selected!
 
1932
        if(audio_samplerate)
 
1933
            command+=" -ar "+QString::number(audio_samplerate_value);
 
1934
        if(audio_channels)
 
1935
            command+=" -ac "+QString::number(audio_channels_value);
 
1936
        if(audio_volume)
 
1937
            command+=" -vol "+QString::number(audio_volume_value);
 
1938
        command+=" \""+output_file+"\"";
 
1939
        /*
 
1940
          Checking whether the output_file already exists. If it does, then
 
1941
          overwrite it, if it checked so, or proceed to the next file, if not...
 
1942
         */
 
1943
        if(QFile(output_file).exists()){
 
1944
            if(overwrite_existing){
 
1945
                if(!QFile::remove(output_file)){
 
1946
                    cerr << QString("Couldn't remove the already existing file '"+output_file+"'\n").toLocal8Bit().data();
 
1947
                    current_file++;
 
1948
                    globpro_string="Already Exists";
 
1949
                    audio_conversion(0);
 
1950
                    return;
 
1951
                }
 
1952
            }
 
1953
            else
 
1954
            {
 
1955
                current_file++;
 
1956
                globpro_string="Already Exists";
 
1957
                audio_conversion(0);
 
1958
                return;
 
1959
            }
 
1960
        }
 
1961
        //getting the duration of the input file...
 
1962
        duration_secs=Global::duration(input_file);
 
1963
        globpro_string="100%";
 
1964
        audio_converter->start(command);
 
1965
        if(full_percent_instant){
 
1966
            full_percent_instant=false;
 
1967
            globpro->setValue(100);
 
1968
            globpro->setFormat(globpro_string);
 
1969
        }
 
1970
        update_unity_count_value(0);
 
1971
        current_file++;
 
1972
    }
 
1973
}
 
1974
 
 
1975
//All
 
1976
 
 
1977
void MainWindow::add_files(QString path, int type){
 
1978
    //this function is for adding files from add-folder function or from drag-en-drop
 
1979
        /*
 
1980
          type 0 -> audio
 
1981
          type 1 -> video
 
1982
          type 2 -> image
 
1983
          type 3 -> all of the above
 
1984
        */
 
1985
    if(type<3){
 
1986
        QDir dir_get_all_files;
 
1987
        dir_get_all_files.setPath(path);
 
1988
        dir_get_all_files.setFilter(QDir::Files);
 
1989
        QStringList filters;
 
1990
        if (type==0)
 
1991
            filters << "*.mp3" << "*.MP3" << "*.wav" << "*.WAV" << "*.ogg" << "*.OGG" << "*.flac" << "*.FLAC" << "*.m4r" << "*.M4R" << "*.WMA" << "*.wma" << "*.AAC" << "*.aac";
 
1992
        else if (type==1)
 
1993
            filters << "*.avi" << "*.AVI" << "*ogv" << "*OGV" << "*vob" << "*VOB" << "*.mp4" << "*.MP4" << "*3gp" << "*3GP" << "*wmv" << "*WMV" << "*.mkv" << "*.MKV" << "*mpg" << "*MPG" << "*mov" << "*MOV" << "*flv" << "*FLV";
 
1994
        else if (type==2)
 
1995
            filters << "*.png" << "*.PNG" << "*.jpg" << "*.JPG" << "*.jpeg" << "*.JPEG" << "*.gif" << "*.GIF" << "*.bmp" << "*.BMP" << "*.svg" << "*.SVG";
 
1996
        QStringList all_files=dir_get_all_files.entryList(filters);
 
1997
        int all_files_count = all_files.count();
 
1998
        QString temp;
 
1999
        int old_r_count=0;
 
2000
        if(type==0){
 
2001
            old_r_count=ui->tableWidget->rowCount();
 
2002
            ui->tableWidget->setRowCount(ui->tableWidget->rowCount()+all_files_count);
 
2003
        }
 
2004
        else if(type==1){
 
2005
            old_r_count=ui->tableWidget_2->rowCount();
 
2006
            ui->tableWidget_2->setRowCount(ui->tableWidget_2->rowCount()+all_files_count);
 
2007
        }
 
2008
        else if(type==2){
 
2009
            old_r_count=ui->tableWidget_3->rowCount();
 
2010
            ui->tableWidget_3->setRowCount(ui->tableWidget_3->rowCount()+all_files_count);
 
2011
        }
 
2012
        QFileInfo inf;
 
2013
        if(type==0){
 
2014
 
 
2015
            /* ---AUDIO--- */
 
2016
 
 
2017
            for(int i=0; i<all_files_count; i++){
 
2018
                temp=path+"/"+all_files.at(i);
 
2019
                inf.setFile(temp);
 
2020
                QTableWidgetItem *qtitem = new QTableWidgetItem;
 
2021
                qtitem->setText(all_files.at(i));
 
2022
                qtitem->setToolTip(temp);
 
2023
                ui->tableWidget->setItem(old_r_count+i,0,qtitem);
 
2024
                QTableWidgetItem *qtsize = new QTableWidgetItem;
 
2025
                float size=(float)inf.size()/1048576;
 
2026
                qtsize->setText(QString::number(size, 'f', 2)+" MB");
 
2027
                ui->tableWidget->setItem(old_r_count+i,1,qtsize);
 
2028
                QTableWidgetItem *qtwait = new QTableWidgetItem;
 
2029
                qtwait->setText("Waiting");
 
2030
                ui->tableWidget->setItem(old_r_count+i,2,qtwait);
 
2031
            }
 
2032
        }
 
2033
        else if(type==1){
 
2034
 
 
2035
            /* ---VIDEO--- */
 
2036
 
 
2037
            for(int i=0; i<all_files_count; i++){
 
2038
                temp=path+"/"+all_files.at(i);
 
2039
                inf.setFile(temp);
 
2040
                QTableWidgetItem *qtitem = new QTableWidgetItem;
 
2041
                qtitem->setText(all_files.at(i));
 
2042
                qtitem->setToolTip(temp);
 
2043
                ui->tableWidget_2->setItem(old_r_count+i,0,qtitem);
 
2044
                QTableWidgetItem *qtsize = new QTableWidgetItem;
 
2045
                float size=(float)inf.size()/1048576;
 
2046
                qtsize->setText(QString::number(size, 'f', 2)+" MB");
 
2047
                ui->tableWidget_2->setItem(old_r_count+i,1,qtsize);
 
2048
                QTableWidgetItem *qtwait = new QTableWidgetItem;
 
2049
                qtwait->setText("Waiting");
 
2050
                ui->tableWidget_2->setItem(old_r_count+i,2,qtwait);
 
2051
            }
 
2052
        }
 
2053
        else if(type==2){
 
2054
 
 
2055
            /* ---PICTURE--- */
 
2056
 
 
2057
            for(int i=0; i<all_files_count; i++){
 
2058
                temp=path+"/"+all_files.at(i);
 
2059
                inf.setFile(temp);
 
2060
                QTableWidgetItem *qtitem = new QTableWidgetItem;
 
2061
                qtitem->setText(all_files.at(i));
 
2062
                qtitem->setToolTip(temp);
 
2063
                ui->tableWidget_3->setItem(old_r_count+i,0,qtitem);
 
2064
                QTableWidgetItem *qtsize = new QTableWidgetItem;
 
2065
                float size=(float)inf.size()/1048576;
 
2066
                qtsize->setText(QString::number(size, 'f', 3)+" MB");
 
2067
                ui->tableWidget_3->setItem(old_r_count+i,1,qtsize);
 
2068
                QTableWidgetItem *qtwait = new QTableWidgetItem;
 
2069
                qtwait->setText("Waiting");
 
2070
                ui->tableWidget_3->setItem(old_r_count+i,2,qtwait);
 
2071
            }
 
2072
        }
 
2073
    }
 
2074
    else
 
2075
    {
 
2076
        /* ---ALL--- */
 
2077
        QDir dir_get_all_files;
 
2078
        dir_get_all_files.setPath(path);
 
2079
        dir_get_all_files.setFilter(QDir::Files);
 
2080
        QStringList audio_filters, video_filters, image_filters;
 
2081
        audio_filters << "*.mp3" << "*.MP3" << "*.wav" << "*.WAV" << "*.ogg" << "*.OGG" << "*.flac" << "*.FLAC" << "*.m4r" << "*.M4R" << "*.WMA" << "*.wma";
 
2082
        video_filters << "*.avi" << "*.AVI" << "*ogv" << "*OGV" << "*vob" << "*VOB" << "*.mp4" << "*.MP4" << "*3gp" << "*3GP" << "*wmv" << "*WMV" << "*.mkv" << "*.MKV" << "*mpg" << "*MPG" << "*mov" << "*MOV" << "*flv" << "*FLV";
 
2083
        image_filters << "*.png" << "*.PNG" << "*.jpg" << "*.JPG" << "*.jpeg" << "*.JPEG" << "*.gif" << "*.GIF" << "*.bmp" << "*.BMP" << "*.svg" << "*.SVG";
 
2084
        QStringList audio_files=dir_get_all_files.entryList(audio_filters);
 
2085
        QStringList video_files=dir_get_all_files.entryList(video_filters);
 
2086
        QStringList image_files=dir_get_all_files.entryList(image_filters);
 
2087
        int audio_files_count = audio_files.count();
 
2088
        int video_files_count = video_files.count();
 
2089
        int image_files_count = image_files.count();
 
2090
        if(audio_files_count){
 
2091
            QString temp;
 
2092
            int old_r_count=ui->tableWidget->rowCount();
 
2093
            ui->tableWidget->setRowCount(ui->tableWidget->rowCount()+audio_files_count);
 
2094
            QFileInfo inf;
 
2095
            for(int i=0; i<audio_files_count; i++){
 
2096
                temp=path+"/"+audio_files.at(i);
 
2097
                inf.setFile(temp);
 
2098
                QTableWidgetItem *qtitem = new QTableWidgetItem;
 
2099
                qtitem->setText(audio_files.at(i));
 
2100
                qtitem->setToolTip(temp);
 
2101
                ui->tableWidget->setItem(old_r_count+i,0,qtitem);
 
2102
                QTableWidgetItem *qtsize = new QTableWidgetItem;
 
2103
                float size=(float)inf.size()/1048576;
 
2104
                qtsize->setText(QString::number(size, 'f', 2)+" MB");
 
2105
                ui->tableWidget->setItem(old_r_count+i,1,qtsize);
 
2106
                QTableWidgetItem *qtwait = new QTableWidgetItem;
 
2107
                qtwait->setText("Waiting");
 
2108
                ui->tableWidget->setItem(old_r_count+i,2,qtwait);
 
2109
            }
 
2110
        }
 
2111
        if(video_files_count){
 
2112
            QString temp;
 
2113
            int old_r_count=ui->tableWidget_2->rowCount();
 
2114
            ui->tableWidget_2->setRowCount(ui->tableWidget_2->rowCount()+video_files_count);
 
2115
            QFileInfo inf;
 
2116
            for(int i=0; i<video_files_count; i++){
 
2117
                temp=path+"/"+video_files.at(i);
 
2118
                inf.setFile(temp);
 
2119
                QTableWidgetItem *qtitem = new QTableWidgetItem;
 
2120
                qtitem->setText(video_files.at(i));
 
2121
                qtitem->setToolTip(temp);
 
2122
                ui->tableWidget_2->setItem(old_r_count+i,0,qtitem);
 
2123
                QTableWidgetItem *qtsize = new QTableWidgetItem;
 
2124
                float size=(float)inf.size()/1048576;
 
2125
                qtsize->setText(QString::number(size, 'f', 2)+" MB");
 
2126
                ui->tableWidget_2->setItem(old_r_count+i,1,qtsize);
 
2127
                QTableWidgetItem *qtwait = new QTableWidgetItem;
 
2128
                qtwait->setText("Waiting");
 
2129
                ui->tableWidget_2->setItem(old_r_count+i,2,qtwait);
 
2130
            }
 
2131
        }
 
2132
        if(image_files_count){
 
2133
            QString temp;
 
2134
            int old_r_count=ui->tableWidget_3->rowCount();
 
2135
            ui->tableWidget_3->setRowCount(ui->tableWidget_3->rowCount()+image_files_count);
 
2136
            QFileInfo inf;
 
2137
            for(int i=0; i<image_files_count; i++){
 
2138
                temp=path+"/"+image_files.at(i);
 
2139
                inf.setFile(temp);
 
2140
                QTableWidgetItem *qtitem = new QTableWidgetItem;
 
2141
                qtitem->setText(image_files.at(i));
 
2142
                qtitem->setToolTip(temp);
 
2143
                ui->tableWidget_3->setItem(old_r_count+i,0,qtitem);
 
2144
                QTableWidgetItem *qtsize = new QTableWidgetItem;
 
2145
                float size=(float)inf.size()/1048576;
 
2146
                qtsize->setText(QString::number(size, 'f', 3)+" MB");
 
2147
                ui->tableWidget_3->setItem(old_r_count+i,1,qtsize);
 
2148
                QTableWidgetItem *qtwait = new QTableWidgetItem;
 
2149
                qtwait->setText("Waiting");
 
2150
                ui->tableWidget_3->setItem(old_r_count+i,2,qtwait);
 
2151
            }
 
2152
        }
 
2153
    }
 
2154
}
 
2155
 
 
2156
float MainWindow::get_size(QString folder){
 
2157
    /*
 
2158
      This function returns the size of a specified folder in MB (non recursively)
 
2159
    */
 
2160
    QDir dir_get_all_files;
 
2161
    dir_get_all_files.setPath(folder);
 
2162
    dir_get_all_files.setFilter(QDir::Files);
 
2163
    QStringList all_files=dir_get_all_files.entryList();
 
2164
    int all_files_count = all_files.count();
 
2165
    float complete_size=0;
 
2166
    if(all_files_count){
 
2167
        QString temp;
 
2168
        QFileInfo inf;
 
2169
        for(int i=0; i<all_files_count; i++){
 
2170
            temp=folder+"/"+all_files.at(i);
 
2171
            inf.setFile(temp);
 
2172
            complete_size+=(float)inf.size()/1048576;
 
2173
        }
 
2174
        return complete_size;
 
2175
    }
 
2176
    else
 
2177
        return complete_size;
 
2178
}
 
2179
 
 
2180
float MainWindow::folder_size(QString folder){
 
2181
    /*
 
2182
      this function returns a float number, which
 
2183
      represents the size of the parent folder 'folder'
 
2184
      in MB!
 
2185
    */
 
2186
 
 
2187
    //getting all the subfolders of the folder specified...
 
2188
    QStringList all_folders = list_folders(folder);
 
2189
    int all_folders_count=all_folders.count();
 
2190
    float complete_size=0;
 
2191
    for(int i=0;i<all_folders_count;i++){
 
2192
        //getting the size of each of the subfolders
 
2193
        float folder_size=get_size(all_folders.at(i));
 
2194
        complete_size+=folder_size;
 
2195
    }
 
2196
    return complete_size;
 
2197
}
 
2198
 
 
2199
QStringList MainWindow::list_folders(QString parent_folder){
 
2200
    //this function return a QStringList with all the subdirectories
 
2201
    //of a parent_folder, and the parent_folder itself.
 
2202
    QStringList all_dirs;
 
2203
    all_dirs << parent_folder;
 
2204
    QDirIterator directories(parent_folder, QDir::Dirs | QDir::NoSymLinks | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
 
2205
 
 
2206
    while(directories.hasNext()){
 
2207
        directories.next();
 
2208
        all_dirs << directories.filePath();
 
2209
    }
 
2210
    return all_dirs;
 
2211
}
 
2212
 
 
2213
void MainWindow::on_stop_clicked()
 
2214
{
 
2215
   audio_stop_pressed=true;
 
2216
   progressbar_animation(1,0);
 
2217
   ui->startBtn->raise();
 
2218
   audio_converter->close();
 
2219
   ui->outputFormatComboBox->setEnabled(true);
 
2220
   if(unity_progressbar)
 
2221
       unity_launcher_entry_set_progress_visible(unity_progress, false);
 
2222
   if(unity_count)
 
2223
       unity_launcher_entry_set_count_visible(unity_progress, false);
 
2224
}
 
2225
 
 
2226
void MainWindow::on_output_folder_clicked()
 
2227
{
 
2228
    QString output_dir=ui->output_folder->toolTip();
 
2229
    if(!QDir(output_dir).exists()){
 
2230
        QDir dir;
 
2231
        if(!dir.mkpath(output_dir)){
 
2232
            QMessageBox::information(this, "Error", "Folder '"+output_dir+"' doesn't exist and I failed to create it, please choose another output_folder");
 
2233
            return;
 
2234
        }
 
2235
    }
 
2236
    if(system(QString("xdg-open \""+output_dir+"\"&").toLocal8Bit().data()))
 
2237
        cerr << QString("Error opening folder '"+output_dir+"'\n").toLocal8Bit().data();
 
2238
}
 
2239
 
 
2240
void MainWindow::on_actionPreferences_triggered()
 
2241
{
 
2242
    pref =new Preferences;
 
2243
    connect(this,SIGNAL(send_output_folder(QString)),pref,SLOT(put_output_folder_at_line(QString)));
 
2244
    connect(pref,SIGNAL(send_new_output(QString)),this,SLOT(crop_output_folder(QString)));
 
2245
    connect(pref, SIGNAL(create_indicator()), this, SLOT(make_indicator()));
 
2246
    connect(pref, SIGNAL(remove_indicator()), this, SLOT(delete_indicator()));
 
2247
    Q_EMIT send_output_folder(ui->output_folder->toolTip());
 
2248
    pref->exec();
 
2249
}
 
2250
 
 
2251
void MainWindow::delete_indicator(){
 
2252
    trayIcon->hide();
 
2253
}
 
2254
 
 
2255
void MainWindow::progressbar_animation(int end_value, int type){
 
2256
    /*
 
2257
      If end_value is 1, then show the stop button
 
2258
      0, show the start buttonS
 
2259
      type==0->audio
 
2260
      type==1->video
 
2261
      type==2->picture
 
2262
    */
 
2263
    //animation for start button
 
2264
    if(type==0){
 
2265
        QGraphicsOpacityEffect* opacityEffect = new QGraphicsOpacityEffect(this);
 
2266
        if(end_value)
 
2267
            opacityEffect->setOpacity(0.0);
 
2268
        else
 
2269
            opacityEffect->setOpacity(1.0);
 
2270
        ui->startBtn->setGraphicsEffect(opacityEffect);
 
2271
        QPropertyAnimation* anim = new QPropertyAnimation(this);
 
2272
        anim->setTargetObject(opacityEffect);
 
2273
        anim->setPropertyName("opacity");
 
2274
        anim->setDuration(500);
 
2275
        anim->setStartValue(opacityEffect->opacity());
 
2276
        anim->setEndValue(end_value);
 
2277
        anim->setEasingCurve(QEasingCurve::OutQuad);
 
2278
        anim->start(QAbstractAnimation::DeleteWhenStopped);
 
2279
 
 
2280
        //animation for stop button
 
2281
        QGraphicsOpacityEffect* opacityEffect2 = new QGraphicsOpacityEffect(this);
 
2282
        if(end_value)
 
2283
            opacityEffect2->setOpacity(1.0);
 
2284
        else
 
2285
            opacityEffect2->setOpacity(0.0);
 
2286
        ui->stop->setGraphicsEffect(opacityEffect2);
 
2287
        QPropertyAnimation* anim2 = new QPropertyAnimation(this);
 
2288
        anim2->setTargetObject(opacityEffect2);
 
2289
        anim2->setPropertyName("opacity");
 
2290
        anim2->setDuration(500);
 
2291
        anim2->setStartValue(opacityEffect2->opacity());
 
2292
        anim2->setEndValue(!end_value);
 
2293
        anim2->setEasingCurve(QEasingCurve::OutQuad);
 
2294
        anim2->start(QAbstractAnimation::DeleteWhenStopped);
 
2295
        if(!end_value && ui->stop->isHidden())
 
2296
            ui->stop->show();
 
2297
 
 
2298
        //animation for stop button
 
2299
        QGraphicsOpacityEffect* opacityEffect3 = new QGraphicsOpacityEffect(this);
 
2300
        if(end_value)
 
2301
            opacityEffect3->setOpacity(1.0);
 
2302
        else
 
2303
            opacityEffect3->setOpacity(0.0);
 
2304
        ui->progressBar->setGraphicsEffect(opacityEffect3);
 
2305
        QPropertyAnimation* anim3 = new QPropertyAnimation(this);
 
2306
        anim3->setTargetObject(opacityEffect3);
 
2307
        anim3->setPropertyName("opacity");
 
2308
        anim3->setDuration(1500);
 
2309
        anim3->setStartValue(opacityEffect3->opacity());
 
2310
        anim3->setEndValue(!end_value);
 
2311
        anim3->setEasingCurve(QEasingCurve::OutQuad);
 
2312
        anim3->start(QAbstractAnimation::DeleteWhenStopped);
 
2313
        if(!end_value && ui->progressBar->isHidden())
 
2314
            ui->progressBar->show();
 
2315
    }
 
2316
    else if(type==1){
 
2317
        QGraphicsOpacityEffect* opacityEffect = new QGraphicsOpacityEffect(this);
 
2318
        if(end_value)
 
2319
            opacityEffect->setOpacity(0.0);
 
2320
        else
 
2321
            opacityEffect->setOpacity(1.0);
 
2322
        ui->startBtn_2->setGraphicsEffect(opacityEffect);
 
2323
        QPropertyAnimation* anim = new QPropertyAnimation(this);
 
2324
        anim->setTargetObject(opacityEffect);
 
2325
        anim->setPropertyName("opacity");
 
2326
        anim->setDuration(500);
 
2327
        anim->setStartValue(opacityEffect->opacity());
 
2328
        anim->setEndValue(end_value);
 
2329
        anim->setEasingCurve(QEasingCurve::OutQuad);
 
2330
        anim->start(QAbstractAnimation::DeleteWhenStopped);
 
2331
 
 
2332
        //animation for stop button
 
2333
        QGraphicsOpacityEffect* opacityEffect2 = new QGraphicsOpacityEffect(this);
 
2334
        if(end_value)
 
2335
            opacityEffect2->setOpacity(1.0);
 
2336
        else
 
2337
            opacityEffect2->setOpacity(0.0);
 
2338
        ui->stop_2->setGraphicsEffect(opacityEffect2);
 
2339
        QPropertyAnimation* anim2 = new QPropertyAnimation(this);
 
2340
        anim2->setTargetObject(opacityEffect2);
 
2341
        anim2->setPropertyName("opacity");
 
2342
        anim2->setDuration(500);
 
2343
        anim2->setStartValue(opacityEffect2->opacity());
 
2344
        anim2->setEndValue(!end_value);
 
2345
        anim2->setEasingCurve(QEasingCurve::OutQuad);
 
2346
        anim2->start(QAbstractAnimation::DeleteWhenStopped);
 
2347
        if(!end_value && ui->stop_2->isHidden())
 
2348
            ui->stop_2->show();
 
2349
 
 
2350
        //animation for stop button
 
2351
        QGraphicsOpacityEffect* opacityEffect3 = new QGraphicsOpacityEffect(this);
 
2352
        if(end_value)
 
2353
            opacityEffect3->setOpacity(1.0);
 
2354
        else
 
2355
            opacityEffect3->setOpacity(0.0);
 
2356
        ui->progressBar_2->setGraphicsEffect(opacityEffect3);
 
2357
        QPropertyAnimation* anim3 = new QPropertyAnimation(this);
 
2358
        anim3->setTargetObject(opacityEffect3);
 
2359
        anim3->setPropertyName("opacity");
 
2360
        anim3->setDuration(1500);
 
2361
        anim3->setStartValue(opacityEffect3->opacity());
 
2362
        anim3->setEndValue(!end_value);
 
2363
        anim3->setEasingCurve(QEasingCurve::OutQuad);
 
2364
        anim3->start(QAbstractAnimation::DeleteWhenStopped);
 
2365
        if(!end_value && ui->progressBar_2->isHidden())
 
2366
            ui->progressBar_2->show();
 
2367
    }
 
2368
    else if(type==2){
 
2369
        QGraphicsOpacityEffect* opacityEffect = new QGraphicsOpacityEffect(this);
 
2370
        if(end_value)
 
2371
            opacityEffect->setOpacity(0.0);
 
2372
        else
 
2373
            opacityEffect->setOpacity(1.0);
 
2374
        ui->startBtn_3->setGraphicsEffect(opacityEffect);
 
2375
        QPropertyAnimation* anim = new QPropertyAnimation(this);
 
2376
        anim->setTargetObject(opacityEffect);
 
2377
        anim->setPropertyName("opacity");
 
2378
        anim->setDuration(500);
 
2379
        anim->setStartValue(opacityEffect->opacity());
 
2380
        anim->setEndValue(end_value);
 
2381
        anim->setEasingCurve(QEasingCurve::OutQuad);
 
2382
        anim->start(QAbstractAnimation::DeleteWhenStopped);
 
2383
 
 
2384
        //animation for stop button
 
2385
        QGraphicsOpacityEffect* opacityEffect2 = new QGraphicsOpacityEffect(this);
 
2386
        if(end_value)
 
2387
            opacityEffect2->setOpacity(1.0);
 
2388
        else
 
2389
            opacityEffect2->setOpacity(0.0);
 
2390
        ui->stop_3->setGraphicsEffect(opacityEffect2);
 
2391
        QPropertyAnimation* anim2 = new QPropertyAnimation(this);
 
2392
        anim2->setTargetObject(opacityEffect2);
 
2393
        anim2->setPropertyName("opacity");
 
2394
        anim2->setDuration(500);
 
2395
        anim2->setStartValue(opacityEffect2->opacity());
 
2396
        anim2->setEndValue(!end_value);
 
2397
        anim2->setEasingCurve(QEasingCurve::OutQuad);
 
2398
        anim2->start(QAbstractAnimation::DeleteWhenStopped);
 
2399
        if(!end_value && ui->stop_3->isHidden())
 
2400
            ui->stop_3->show();
 
2401
 
 
2402
        //animation for stop button
 
2403
        QGraphicsOpacityEffect* opacityEffect3 = new QGraphicsOpacityEffect(this);
 
2404
        if(end_value)
 
2405
            opacityEffect3->setOpacity(1.0);
 
2406
        else
 
2407
            opacityEffect3->setOpacity(0.0);
 
2408
        ui->progressBar_3->setGraphicsEffect(opacityEffect3);
 
2409
        QPropertyAnimation* anim3 = new QPropertyAnimation(this);
 
2410
        anim3->setTargetObject(opacityEffect3);
 
2411
        anim3->setPropertyName("opacity");
 
2412
        anim3->setDuration(1500);
 
2413
        anim3->setStartValue(opacityEffect3->opacity());
 
2414
        anim3->setEndValue(!end_value);
 
2415
        anim3->setEasingCurve(QEasingCurve::OutQuad);
 
2416
        anim3->start(QAbstractAnimation::DeleteWhenStopped);
 
2417
        if(!end_value && ui->progressBar_3->isHidden())
 
2418
            ui->progressBar_3->show();
 
2419
    }
 
2420
}
 
2421
 
 
2422
void MainWindow::on_stackedWidget_currentChanged(int arg1)
 
2423
{
 
2424
    cur_index=arg1;
 
2425
    if(cur_index==3 && ui->stackedWidget_3->currentIndex()==1){
 
2426
        ui->addBtn->hide();
 
2427
        ui->addfolder->hide();
 
2428
        ui->clearBtn->hide();
 
2429
        ui->removeBtn->hide();
 
2430
        ui->movedownButton->hide();
 
2431
        ui->moveupButton->hide();
 
2432
    }
 
2433
    else if(ui->addBtn->isHidden()){
 
2434
        ui->addBtn->show();
 
2435
        ui->addfolder->show();
 
2436
        ui->clearBtn->show();
 
2437
        ui->removeBtn->show();
 
2438
        ui->movedownButton->show();
 
2439
        ui->moveupButton->show();
 
2440
    }
 
2441
}
 
2442
 
 
2443
void MainWindow::on_show_errors_clicked()
 
2444
{
 
2445
    showlogs =new error_logs(all_errors, this);
 
2446
    showlogs->exec();
 
2447
}
 
2448
 
 
2449
void MainWindow::on_actionAbout_triggered()
 
2450
{
 
2451
    About = new about(this);
 
2452
    if(about_shown)
 
2453
    {
 
2454
        About->raise();
 
2455
        About->activateWindow();
 
2456
    }
 
2457
    else
 
2458
    {
 
2459
        about_shown=true;
 
2460
        About->exec();
 
2461
        about_shown=false;
 
2462
    }
 
2463
}
 
2464
 
 
2465
void MainWindow::on_actionQuit_triggered()
 
2466
{
 
2467
    this->close();
 
2468
}
 
2469
 
 
2470
void MainWindow::on_actionGet_Help_Online_triggered()
 
2471
{
 
2472
    if(system("xdg-open https://answers.launchpad.net/format-junkie/+addquestion"))
 
2473
        cerr << "Error opening https://answers.launchpad.net/format-junkie/+addquestion\n";
 
2474
}
 
2475
void MainWindow::on_actionReport_A_Bug_triggered()
 
2476
{
 
2477
    if(system("xdg-open https://bugs.launchpad.net/format-junkie/+filebug"))
 
2478
        cerr << "Error opening https://bugs.launchpad.net/format-junkie/+filebug\n";
 
2479
}
 
2480
 
 
2481
void MainWindow::dbus_action(QString msg){
 
2482
    //this function checks for dbus messages!
 
2483
    if(msg=="focus"){
 
2484
        /*
 
2485
          This 'this->stackUnder(parentWidget());' works but only for the 1st time :(
 
2486
          I haven't found a solution on how to set strong focus and bring to front my
 
2487
          qt application :(
 
2488
        */
 
2489
        this->setFocusPolicy(Qt::StrongFocus);
 
2490
        this->setFocus();
 
2491
        this->showNormal();
 
2492
        this->activateWindow();
 
2493
        showNormal();
 
2494
        activateWindow();
 
2495
        this->raise();
 
2496
        this->stackUnder(0);
 
2497
    }
 
2498
    else if(msg=="--add-audio"){
 
2499
        cur_index=0;
 
2500
        ui->stackedWidget->setCurrentIndex(0);
 
2501
        ui->musicButton->setChecked(true);
 
2502
        on_addBtn_clicked();
 
2503
    }
 
2504
    else if(msg=="--add-video"){
 
2505
        cur_index=1;
 
2506
        ui->stackedWidget->setCurrentIndex(1);
 
2507
        ui->videoButton->setChecked(true);
 
2508
        on_addBtn_clicked();
 
2509
    }
 
2510
    else if(msg=="--add-image"){
 
2511
        cur_index=2;
 
2512
        ui->stackedWidget->setCurrentIndex(2);
 
2513
        ui->imageButton->setChecked(true);
 
2514
        on_addBtn_clicked();
 
2515
    }
 
2516
    else if(msg=="--open-output"){
 
2517
        on_output_folder_clicked();
 
2518
    }
 
2519
    else{
 
2520
        cerr << QString("Received message "+msg+" but it's not recognized as an argument!\nPlease give formatjunkie --help/-h for all available options!\n").toLocal8Bit().data();
 
2521
    }
 
2522
}
 
2523
 
 
2524
void MainWindow::update_unity_value(int value){
 
2525
    unity_launcher_entry_set_progress(unity_progress, (float)value/100);
 
2526
}
 
2527
 
 
2528
void MainWindow::on_progressBar_valueChanged(int value)
 
2529
{
 
2530
    if(unity_progressbar)
 
2531
        update_unity_value(value);
 
2532
}
 
2533
 
 
2534
void MainWindow::on_progressBar_2_valueChanged(int value)
 
2535
{
 
2536
    if(unity_progressbar)
 
2537
        update_unity_value(value);
 
2538
}
 
2539
 
 
2540
void MainWindow::on_progressBar_3_valueChanged(int value)
 
2541
{
 
2542
    if(unity_progressbar)
 
2543
        update_unity_value(value);
 
2544
}
 
2545
 
 
2546
void MainWindow::handle_it(){
 
2547
 
 
2548
}
 
2549
 
 
2550
void MainWindow::show_desktop_notification(int noti_type){
 
2551
    /*
 
2552
      noti_type:
 
2553
      0->Notification about convertion complete
 
2554
      1->Notification about a file that already exists
 
2555
      2->Notification about successful ISO creation
 
2556
      3->Notification about a file that doesn't exist while it should
 
2557
      4->Notification about successful CSO creation
 
2558
      5->Notification about unsuccessful ISO creation
 
2559
      6->Notification about unsuccessful CSO creation
 
2560
      7->Notification about already existent avi file (on subtitles encoding)
 
2561
      8->Notification about encoding successful
 
2562
    */
 
2563
    if (noti_type==0){
 
2564
    QString body1,body2,body3;
 
2565
    body1="The conversion of "+QString::number(num_files-already_completed);
 
2566
    if(num_files-already_completed>1)
 
2567
        body2=" files has finished";
 
2568
    else
 
2569
        body2=" file has finished";
 
2570
    if(error_count){
 
2571
        if(error_count>1)
 
2572
            body3=" with "+QString::number(error_count)+" errors!";
 
2573
        else
 
2574
            body3=" with 1 error!";
 
2575
    }
 
2576
    else
 
2577
        body3="!";
 
2578
 
 
2579
    body=body1+body2+body3;
 
2580
    }
 
2581
    else if(noti_type==1)
 
2582
        body="The file "+file_exists+" already exists.";
 
2583
    else if(noti_type==2)
 
2584
        body="ISO file has been created successfully";
 
2585
    else if(noti_type==3)
 
2586
        body="File "+file_not_exists+" doesn't exist.";
 
2587
    else if(noti_type==4)
 
2588
        body="CSO file has been created successfully";
 
2589
    else if(noti_type==5)
 
2590
        body="Error creating your ISO file!";
 
2591
    else if(noti_type==6)
 
2592
        body="Error creating your CSO file!";
 
2593
    else if(noti_type==7)
 
2594
        body="The file"+file_exists+" already exists!";
 
2595
    else if(noti_type==8)
 
2596
        body="Encoding has been completed successfully ";
 
2597
    else if(noti_type==9)
 
2598
        body="Error while trying to encode!";
 
2599
 
 
2600
    if (!notify_init ("update-notifications")){
 
2601
        cerr << "Couldn't display notification, unknown error!\n";
 
2602
        return;
 
2603
    }
 
2604
    if(first_notification){
 
2605
        first_notification=false;
 
2606
        notification = notify_notification_new ( "Format Junkie", body.toLocal8Bit().data(), "/opt/extras.ubuntu.com/formatjunkie/pixmap/fjt.png");
 
2607
        error = NULL;
 
2608
        success = notify_notification_show (notification, &error);
 
2609
        if (!success)
 
2610
        {
 
2611
                g_print ("The notification did not work ... \"%s\".\n", error->message);
 
2612
                g_error_free (error);
 
2613
        }
 
2614
 
 
2615
        g_signal_connect (G_OBJECT (notification), "closed", G_CALLBACK (&handle_it), NULL);
 
2616
 
 
2617
        return;
 
2618
    }
 
2619
    success = notify_notification_update (notification, "Format Junkie",body.toLocal8Bit().data(), "/opt/extras.ubuntu.com/formatjunkie/pixmap/fjt.png");
 
2620
    error = NULL;
 
2621
    success = notify_notification_show (notification, &error);
 
2622
    if (!success)
 
2623
    {
 
2624
            g_print ("The notification did not work ... \"%s\".\n", error->message);
 
2625
            g_error_free (error);
 
2626
            return;
 
2627
    }
 
2628
    g_signal_connect (G_OBJECT (notification), "closed", G_CALLBACK (&handle_it), NULL);
 
2629
}
 
2630
 
 
2631
void MainWindow::on_actionContents_F1_triggered()
 
2632
{
 
2633
    if(system("yelp file:///opt/extras.ubuntu.com/formatjunkie/help/C/formatjunkie.xml 2> /dev/null&"))
 
2634
        cerr << "Error opening /opt/extras.ubuntu.com/formatjunkie/help/C/formatjunkie.xml with yelp! Check for file existence and/or for /usr/bin/yelp\n";
 
2635
}
 
2636
 
 
2637
void MainWindow::process_dir(QString dir){
 
2638
    /*
 
2639
     This function that takes a QString parent dir as an argument and process it.
 
2640
     Specifically, it takes  all the files from the subfolders of the 'dir' and
 
2641
     adds them to the corresponding tableWidget of mainwindow.ui
 
2642
     It is used from the drag and drop event (if a folder has been dropped)!
 
2643
    */
 
2644
    QStringList all_folders=list_folders(dir);
 
2645
    int all_folders_count=all_folders.count();
 
2646
    for(int i=0;i<all_folders_count;i++){
 
2647
        add_files(all_folders.at(i), 3);
 
2648
    }
 
2649
    if(ui->tableWidget->rowCount() && ui->outputFormatComboBox->isEnabled())
 
2650
        ui->startBtn->setEnabled(true);
 
2651
    if(ui->tableWidget_2->rowCount() && ui->outputFormatComboBox_2->isEnabled())
 
2652
        ui->startBtn_2->setEnabled(true);
 
2653
    if(ui->tableWidget_3->rowCount() && ui->outputFormatComboBox_3->isEnabled())
 
2654
        ui->startBtn_3->setEnabled(true);
 
2655
}
 
2656
 
 
2657
void MainWindow::on_actionAdd_Audio_Files_triggered()
 
2658
{
 
2659
    add_audio_files();
 
2660
}
 
2661
 
 
2662
void MainWindow::on_actionAdd_Video_Files_triggered()
 
2663
{
 
2664
    add_video_files();
 
2665
}
 
2666
 
 
2667
void MainWindow::on_actionAdd_Image_Files_triggered()
 
2668
{
 
2669
    add_image_files();
 
2670
}
 
2671
 
 
2672
void MainWindow::show_me(){
 
2673
    //this function gives strong focus to the application. It's called from the trayIcon
 
2674
    this->setFocusPolicy(Qt::StrongFocus);
 
2675
    this->setFocus();
 
2676
    this->showNormal();
 
2677
    this->activateWindow();
 
2678
    showNormal();
 
2679
    activateWindow();
 
2680
    this->raise();
 
2681
    this->stackUnder(0);
 
2682
}
 
2683
 
 
2684
void MainWindow::changeEvent(QEvent *e)
 
2685
{
 
2686
    switch (e->type()){
 
2687
    case QEvent::LanguageChange:
 
2688
        ui->retranslateUi(this);
 
2689
        break;
 
2690
 
 
2691
    //see if the event is changing the window state
 
2692
 
 
2693
    case QEvent::WindowStateChange:
 
2694
        //if it is, we need to see that this event is minimize
 
2695
        if(isMinimized()){
 
2696
            if(indicator){
 
2697
                trayIcon->show();
 
2698
                this->hide();
 
2699
            }
 
2700
        }
 
2701
 
 
2702
    default:
 
2703
        break;
 
2704
 
 
2705
        QMainWindow::changeEvent(e);
 
2706
    }
 
2707
}
 
2708
 
 
2709
void MainWindow::make_indicator()
 
2710
{
 
2711
    QApplication::setQuitOnLastWindowClosed(false);
 
2712
    if(indicator_has_been_created){
 
2713
        trayIcon->show();
 
2714
        return;
 
2715
    }
 
2716
    indicator_has_been_created=true;
 
2717
    trayIcon = new QSystemTrayIcon(this);
 
2718
    trayIcon->setIcon(QIcon(":/pictures/Pictures/tray.png"));
 
2719
    trayIcon->setToolTip("Format Junkie Tray Icon");
 
2720
 
 
2721
    QMenu *changer_menu = new QMenu;
 
2722
 
 
2723
    Show_action = new QAction("S&how",this);
 
2724
    Show_action->setIconVisibleInMenu(true);
 
2725
    connect(Show_action, SIGNAL(triggered()), this, SLOT(show_me()));
 
2726
    changer_menu->addAction(Show_action);
 
2727
 
 
2728
    changer_menu->addSeparator();
 
2729
 
 
2730
    Show_audio = new QAction(QIcon::fromTheme("audio-x-generic"), "Add Audio files", this);
 
2731
    Show_audio->setIconVisibleInMenu(true);
 
2732
    connect(Show_audio, SIGNAL(triggered()), this, SLOT(add_audio_files()));
 
2733
    changer_menu->addAction(Show_audio);
 
2734
 
 
2735
    Show_video = new QAction(QIcon::fromTheme("video-x-generic"), "Add Video files", this);
 
2736
    Show_video->setIconVisibleInMenu(true);
 
2737
    connect(Show_video, SIGNAL(triggered()), this, SLOT(add_video_files()));
 
2738
    changer_menu->addAction(Show_video);
 
2739
 
 
2740
    Show_image = new QAction(QIcon::fromTheme("image-x-generic"), "Add Image files", this);
 
2741
    Show_image->setIconVisibleInMenu(true);
 
2742
    connect(Show_image, SIGNAL(triggered()), this, SLOT(add_image_files()));
 
2743
    changer_menu->addAction(Show_image);
 
2744
 
 
2745
    Show_preferences = new QAction(QIcon::fromTheme("preferences-desktop"), "Preferences", this);
 
2746
    Show_preferences->setIconVisibleInMenu(true);
 
2747
    connect(Show_preferences, SIGNAL(triggered()), this, SLOT(on_actionPreferences_triggered()));
 
2748
    changer_menu->addAction(Show_preferences);
 
2749
 
 
2750
    Show_outputfolder = new QAction(QIcon::fromTheme("folder"), "Open Output Folder", this);
 
2751
    Show_outputfolder->setIconVisibleInMenu(true);
 
2752
    connect(Show_outputfolder, SIGNAL(triggered()), this, SLOT(on_output_folder_clicked()));
 
2753
    changer_menu->addAction(Show_outputfolder);
 
2754
 
 
2755
    Show_about = new QAction(QIcon::fromTheme("help-about"), "About", this);
 
2756
    Show_about->setIconVisibleInMenu(true);
 
2757
    connect(Show_about, SIGNAL(triggered()), this, SLOT(on_actionAbout_triggered()));
 
2758
    changer_menu->addAction(Show_about);
 
2759
 
 
2760
    changer_menu->addSeparator();
 
2761
 
 
2762
    Quit_action = new QAction(QIcon::fromTheme("window-close"), "&Quit", this);
 
2763
    Quit_action->setIconVisibleInMenu(true);
 
2764
    connect(Quit_action, SIGNAL(triggered()), this, SLOT(close()));
 
2765
    changer_menu->addAction(Quit_action);
 
2766
 
 
2767
 
 
2768
    trayIcon->setContextMenu(changer_menu);
 
2769
    trayIcon->show();
 
2770
}
 
2771
 
 
2772
void MainWindow::image_read_output(){
 
2773
    //read all possible output so as to calculate the percentages...
 
2774
    QByteArray newData_stdout=image_converter->readAllStandardOutput();
 
2775
    QString stdout=QString::fromLocal8Bit(newData_stdout);
 
2776
    QByteArray newData_stderr=image_converter->readAllStandardError();
 
2777
    QString stderr=QString::fromLocal8Bit(newData_stderr);
 
2778
    QString out=stdout+stderr;
 
2779
    current_file_errors+=out;
 
2780
}
 
2781
 
 
2782
void MainWindow::image_conversion(int return_code){
 
2783
    QString format_text=ui->outputFormatComboBox_3->currentText();
 
2784
 
 
2785
    if(image_stop_pressed){
 
2786
        if(ui->tableWidget_3->item(current_file-1,2)->toolTip()!="Completed")
 
2787
            ui->tableWidget_3->removeCellWidget(current_file-1,2);
 
2788
        else
 
2789
            ui->tableWidget_3->removeCellWidget(current_file,2);
 
2790
        ui->tableWidget_3->item(current_file-1,2)->setText("Waiting");
 
2791
        QFileInfo inf;
 
2792
        inf.setFile(output_file);
 
2793
        QString output_file_rm=ui->output_folder->toolTip()+"/"+inf.completeBaseName()+"."+format_text;
 
2794
        QFile::remove(output_file_rm);
 
2795
        return;
 
2796
    }
 
2797
    if(return_code){
 
2798
        //AN ERROR HAS OCCURED!
 
2799
        image_conversion_result=2;
 
2800
        image_conversion(0);
 
2801
        return;
 
2802
    }
 
2803
    else
 
2804
    {
 
2805
        //IF NOT RETURN CODE
 
2806
        /*
 
2807
          The conversion of the previous file was successful!
 
2808
          Check if the original (source) file is to be deleted...
 
2809
        */
 
2810
        if(delete_original){
 
2811
            if(QFile(input_file).exists()){
 
2812
                if(system(QString("rm -f \""+input_file+"\"").toLocal8Bit().data()))
 
2813
                    cerr << QString("Could not delete source file '"+input_file+"'\nDo I have the permissions to do so?\n").toLocal8Bit().data();
 
2814
            }
 
2815
        }
 
2816
    }
 
2817
 
 
2818
    if (current_file>num_files-1){
 
2819
        /*
 
2820
          !!!ALL IMAGE FILES HAVE BEEN CONVERTED!!!
 
2821
          Update the progressbars and do the actions
 
2822
          that the user has chosen to do from bottom
 
2823
          right!!!
 
2824
        */
 
2825
        //just set to the last file the appropriate icon!
 
2826
        ui->startBtn_3->setEnabled(false);
 
2827
        ui->startBtn_3->raise();
 
2828
        ui->progressBar_3->setFormat("100%");
 
2829
        ui->progressBar_3->setValue(100);
 
2830
        progressbar_animation(1,2);
 
2831
        if(ui->tableWidget_3->cellWidget(current_file-1, 2))
 
2832
            ui->tableWidget_3->removeCellWidget(current_file-1, 2);
 
2833
        if(image_conversion_result==0){
 
2834
            if(ui->tableWidget_3->item(current_file-1,2)->toolTip()!="Completed"){
 
2835
                current_file_errors.clear();
 
2836
                QLabel *lab = new QLabel(this);
 
2837
                lab->hide();
 
2838
                ui->tableWidget_3->item(current_file-1, 2)->setText("");
 
2839
                lab->setPixmap(QPixmap(":/pictures/Pictures/tick.png"));
 
2840
                ui->tableWidget_3->setCellWidget(current_file-1, 2, lab);
 
2841
                ui->tableWidget_3->item(current_file-1,2)->setToolTip("Completed");
 
2842
            }
 
2843
        }
 
2844
        else if(image_conversion_result==1){
 
2845
            image_conversion_result=0;
 
2846
            if(ui->tableWidget_3->item(current_file-1,2)->toolTip()!="Completed"){
 
2847
                current_file_errors.clear();
 
2848
                if(ui->tableWidget_3->cellWidget(current_file-1, 2))
 
2849
                    ui->tableWidget_3->removeCellWidget(current_file-1, 2);
 
2850
                ui->tableWidget_3->item(current_file-1, 2)->setText("Already Exists!");
 
2851
                ui->tableWidget_3->item(current_file-1,2)->setToolTip("Completed");
 
2852
            }
 
2853
        }
 
2854
        else if(image_conversion_result==2){
 
2855
            image_conversion_result=0;
 
2856
            if(ui->tableWidget_3->item(current_file-1,2)->toolTip()!="Completed"){
 
2857
                error_count++;
 
2858
                QLabel *lab = new QLabel(this);
 
2859
                lab->hide();
 
2860
                ui->tableWidget_3->item(current_file-1, 2)->setText("");
 
2861
                lab->setPixmap(QPixmap(":/pictures/Pictures/error.png"));
 
2862
                ui->tableWidget_3->setCellWidget(current_file-1, 2, lab);
 
2863
                if(all_errors.isEmpty())
 
2864
                    all_errors+="'<b>"+input_file+"</b>'<br>";
 
2865
                else
 
2866
                    all_errors+="\n'<b>"+input_file+"</b>'<br>";
 
2867
                current_file_errors.replace("\n","<br>");
 
2868
                all_errors+="<font color=red>"+current_file_errors+"</font>";
 
2869
                QTableWidgetItem *item_error1 = new QTableWidgetItem;
 
2870
                item_error1->setText(ui->tableWidget_3->item(current_file-1,0)->text()+" - ERROR");
 
2871
                item_error1->setToolTip(ui->tableWidget_3->item(current_file-1,0)->toolTip());
 
2872
                item_error1->setBackgroundColor(Qt::red);
 
2873
                item_error1->setTextColor(Qt::white);
 
2874
                ui->tableWidget_3->setItem(current_file-1,0, item_error1);
 
2875
                QTableWidgetItem *item_error2 = new QTableWidgetItem;
 
2876
                item_error2->setText(ui->tableWidget_3->item(current_file-1,1)->text());
 
2877
                item_error2->setToolTip(ui->tableWidget_3->item(current_file-1,1)->toolTip());
 
2878
                item_error2->setBackgroundColor(Qt::red);
 
2879
                item_error2->setTextColor(Qt::white);
 
2880
                ui->tableWidget_3->setItem(current_file-1,1, item_error2);
 
2881
                QTableWidgetItem *item_error3 = new QTableWidgetItem;
 
2882
                item_error3->setBackgroundColor(Qt::red);
 
2883
                item_error3->setTextColor(Qt::white);
 
2884
                ui->tableWidget_3->setItem(current_file-1,2, item_error3);
 
2885
                ui->show_errors->show();
 
2886
                ui->tableWidget_3->item(current_file-1,2)->setToolTip("Completed");
 
2887
            }
 
2888
        }
 
2889
        ui->outputFormatComboBox_3->setEnabled(true);
 
2890
        if(unity_progressbar)
 
2891
            unity_launcher_entry_set_progress_visible(unity_progress, false);
 
2892
        if(unity_count)
 
2893
            unity_launcher_entry_set_count_visible(unity_progress, false);
 
2894
        if(desktop_notifications)
 
2895
            show_desktop_notification(0);
 
2896
        error_count=0;
 
2897
        actions_after_convert();
 
2898
        return;
 
2899
    }
 
2900
    if(current_file!=0){
 
2901
        /*
 
2902
          Setting to the previous file the image_conversion_result.
 
2903
          It will be OK (tick) if the previous conversion was OK
 
2904
          ,FAIL (x) if the previous conversion has failed
 
2905
          and it will be 'Already Exists' if
 
2906
          the previous file already existed. This has no sense
 
2907
          if current_file==0, because there isn't any previous
 
2908
          file!
 
2909
          So:
 
2910
          image_conversion_result==0 -> OK!
 
2911
          image_conversion_result==1 -> Already Exists!
 
2912
          image_conversion_result==2 -> ERROR!
 
2913
        */
 
2914
        //removing the previous file's gif, unless it is already completed(the user has added more files after the conversion has finished)...
 
2915
        if(ui->tableWidget_3->cellWidget(current_file-1, 2) && ui->tableWidget_3->item(current_file-1,2)->toolTip()!="Completed")
 
2916
            ui->tableWidget_3->removeCellWidget(current_file-1, 2);
 
2917
        //and setting the new...
 
2918
        if(image_conversion_result==0){
 
2919
            if(ui->tableWidget_3->item(current_file-1,2)->toolTip()!="Completed"){
 
2920
                current_file_errors.clear();
 
2921
                QLabel *lab = new QLabel(this);
 
2922
                lab->hide();
 
2923
                ui->tableWidget_3->item(current_file-1, 2)->setText("");
 
2924
                lab->setPixmap(QPixmap(":/pictures/Pictures/tick.png"));
 
2925
                ui->tableWidget_3->setCellWidget(current_file-1, 2, lab);
 
2926
                ui->tableWidget_3->item(current_file-1,2)->setToolTip("Completed");
 
2927
            }
 
2928
        }
 
2929
        else if(image_conversion_result==1){
 
2930
            image_conversion_result=0;
 
2931
            if(ui->tableWidget_3->item(current_file-1,2)->toolTip()!="Completed"){
 
2932
                current_file_errors.clear();
 
2933
                ui->tableWidget_3->item(current_file-1, 2)->setText("Already Exists!");
 
2934
                ui->tableWidget_3->item(current_file-1,2)->setToolTip("Completed");
 
2935
            }
 
2936
        }
 
2937
        else if(image_conversion_result==2){
 
2938
            image_conversion_result=0;
 
2939
            if(ui->tableWidget_3->item(current_file-1,2)->toolTip()!="Completed"){
 
2940
                error_count++;
 
2941
                QLabel *lab = new QLabel(this);
 
2942
                lab->hide();
 
2943
                ui->tableWidget_3->item(current_file-1, 2)->setText("");
 
2944
                lab->setPixmap(QPixmap(":/pictures/Pictures/error.png"));
 
2945
                ui->tableWidget_3->setCellWidget(current_file-1, 2, lab);
 
2946
                if(all_errors.isEmpty())
 
2947
                    all_errors+="'<b>"+input_file+"</b>'<br>";
 
2948
                else
 
2949
                    all_errors+="\n'<b>"+input_file+"</b>'<br>";
 
2950
                current_file_errors.replace("\n","<br>");
 
2951
                all_errors+="<font color=red>"+current_file_errors+"</font>";
 
2952
                QTableWidgetItem *item_error1 = new QTableWidgetItem;
 
2953
                item_error1->setText(ui->tableWidget_3->item(current_file-1,0)->text()+" - ERROR");
 
2954
                item_error1->setToolTip(ui->tableWidget_3->item(current_file-1,0)->toolTip());
 
2955
                item_error1->setBackgroundColor(Qt::red);
 
2956
                item_error1->setTextColor(Qt::white);
 
2957
                ui->tableWidget_3->setItem(current_file-1,0, item_error1);
 
2958
                QTableWidgetItem *item_error2 = new QTableWidgetItem;
 
2959
                item_error2->setText(ui->tableWidget_3->item(current_file-1,1)->text());
 
2960
                item_error2->setToolTip(ui->tableWidget_3->item(current_file-1,1)->toolTip());
 
2961
                item_error2->setBackgroundColor(Qt::red);
 
2962
                item_error2->setTextColor(Qt::white);
 
2963
                ui->tableWidget_3->setItem(current_file-1,1, item_error2);
 
2964
                QTableWidgetItem *item_error3 = new QTableWidgetItem;
 
2965
                item_error3->setBackgroundColor(Qt::red);
 
2966
                item_error3->setTextColor(Qt::white);
 
2967
                ui->tableWidget_3->setItem(current_file-1,2, item_error3);
 
2968
                ui->show_errors->show();
 
2969
                ui->tableWidget_3->item(current_file-1,2)->setToolTip("Completed");
 
2970
            }
 
2971
        }
 
2972
    }
 
2973
 
 
2974
    input_file=ui->tableWidget_3->item(current_file,0)->toolTip();
 
2975
 
 
2976
    QFileInfo inf;
 
2977
    inf.setFile(input_file);
 
2978
    output_file=ui->output_folder->toolTip()+"/"+inf.completeBaseName()+"."+format_text;
 
2979
 
 
2980
    if(QFile(output_file).exists()){
 
2981
        if(overwrite_existing){
 
2982
            if(!QFile(output_file).remove())
 
2983
                cerr << QString("Error while removing the already existing file '"+output_file+"'\n").toLocal8Bit().data();
 
2984
        }
 
2985
        else
 
2986
        {//overwrite_existing is false, but the output file already exists!
 
2987
            //continue to the next file...
 
2988
            image_conversion_result=1;
 
2989
            current_file++;
 
2990
            image_conversion(0);
 
2991
            return;
 
2992
        }
 
2993
    }
 
2994
    //making the command...
 
2995
    QString command="convert \""+input_file+"\"";
 
2996
    if(image_crop){
 
2997
        command+=" -crop "+QString::number(image_width_value)+"x"+QString::number(image_height_value)+"+"+QString::number(image_x_value)+"+"+QString::number(image_y_value)+" +repage";
 
2998
    }
 
2999
    if(image_width_height){
 
3000
        command+=" -resize "+QString::number(image_width)+"x"+QString::number(image_height);
 
3001
        if(!image_respect_ratio)
 
3002
            command+="!";
 
3003
    }
 
3004
    if(image_colors_number){
 
3005
        command+=" -colors "+QString::number(image_colors_number_value);
 
3006
    }
 
3007
    if(image_comment){
 
3008
        command+=" -comment \""+image_comment_value+"\"";
 
3009
    }
 
3010
    command+=" -quality "+QString::number(quality)+" \""+output_file+"\"";
 
3011
    //setting the looping gif to the current file...
 
3012
    QMovie *mov= new QMovie(this);
 
3013
    mov->setFileName(":/pictures/Pictures/c.gif");
 
3014
    QLabel *lab = new QLabel(this);
 
3015
    lab->hide();
 
3016
    ui->tableWidget_3->item(current_file, 2)->setText("");
 
3017
    lab->setMovie(mov);
 
3018
    ui->tableWidget_3->setCellWidget(current_file, 2, lab);
 
3019
    mov->start();
 
3020
    image_converter->start(command);
 
3021
    update_unity_count_value(0);
 
3022
    ui->progressBar_3->setValue((current_file-already_completed)*100/(num_files-already_completed));
 
3023
    ui->progressBar_3->setFormat(QString::number((current_file-already_completed)*100/(num_files-already_completed))+"%");
 
3024
    current_file++;
 
3025
}
 
3026
 
 
3027
void MainWindow::on_startBtn_3_clicked()
 
3028
{
 
3029
    if(!ui->tableWidget_3->rowCount())
 
3030
        return;
 
3031
    if(!QDir(ui->output_folder->toolTip()).exists()){
 
3032
        QDir dir;
 
3033
        if(!dir.mkpath(ui->output_folder->toolTip())){
 
3034
            QMessageBox::warning(this, tr("Error"), tr("The output folder doesn't exist! Please specify an existent directory!"));
 
3035
            return;
 
3036
        }
 
3037
    }
 
3038
    image_stop_pressed=false;
 
3039
    num_files=ui->tableWidget_3->rowCount();
 
3040
    if(unity_count)
 
3041
        unity_launcher_entry_set_count (unity_progress, num_files);
 
3042
    ui->progressBar_3->setFormat("0%");
 
3043
    ui->progressBar_3->setValue(0);
 
3044
    ui->stop_3->raise();
 
3045
    progressbar_animation(0,2);
 
3046
    already_completed=0;
 
3047
    for(int i=0;i<num_files;i++){
 
3048
        if(ui->tableWidget_3->item(i,2)->toolTip()!="Completed"){
 
3049
            current_file=i;
 
3050
            already_completed=i;
 
3051
            break;
 
3052
        }
 
3053
    }
 
3054
    if(current_file==0){
 
3055
        //that's a completely new conversion
 
3056
        current_file_errors.clear();
 
3057
        all_errors.clear();
 
3058
        error_count=0;
 
3059
        ui->show_errors->hide();
 
3060
    }
 
3061
    ui->outputFormatComboBox_3->setEnabled(false);
 
3062
    if(unity_progressbar)
 
3063
        unity_launcher_entry_set_progress_visible(unity_progress, true);
 
3064
    if(unity_count)
 
3065
        unity_launcher_entry_set_count_visible(unity_progress, true);
 
3066
    image_conversion(0);
 
3067
}
 
3068
 
 
3069
void MainWindow::on_stop_3_clicked()
 
3070
{
 
3071
    image_stop_pressed=true;
 
3072
    progressbar_animation(1,2);
 
3073
    ui->startBtn_3->raise();
 
3074
    image_converter->close();
 
3075
    ui->outputFormatComboBox_3->setEnabled(true);
 
3076
    if(unity_progressbar)
 
3077
        unity_launcher_entry_set_progress_visible(unity_progress, false);
 
3078
    if(unity_count)
 
3079
        unity_launcher_entry_set_count_visible(unity_progress, false);
 
3080
}
 
3081
 
 
3082
void MainWindow::on_startBtn_2_clicked()
 
3083
{
 
3084
    if(!ui->tableWidget_2->rowCount())
 
3085
        return;
 
3086
    if(!QDir(ui->output_folder->toolTip()).exists()){
 
3087
        QDir dir;
 
3088
        if(!dir.mkpath(ui->output_folder->toolTip())){
 
3089
            QMessageBox::warning(this, tr("Error"), tr("The output folder doesn't exist! Please specify an existent directory!"));
 
3090
            return;
 
3091
        }
 
3092
    }
 
3093
    video_stop_pressed=false;
 
3094
    num_files=ui->tableWidget_2->rowCount();
 
3095
    if(unity_count)
 
3096
        unity_launcher_entry_set_count (unity_progress, num_files);
 
3097
    ui->progressBar_2->setFormat("0%");
 
3098
    ui->progressBar_2->setValue(0);
 
3099
    ui->stop_2->raise();
 
3100
    progressbar_animation(0,1);
 
3101
    already_completed=0;
 
3102
    for(int i=0;i<num_files;i++){
 
3103
        if(ui->tableWidget_2->item(i,2)->toolTip()!="Completed"){
 
3104
            current_file=i;
 
3105
            already_completed=i;
 
3106
            break;
 
3107
        }
 
3108
    }
 
3109
    if(current_file==0){
 
3110
        //that's a completely new conversion
 
3111
        all_errors.clear();
 
3112
        error_count=0;
 
3113
        ui->show_errors->hide();
 
3114
    }
 
3115
    ui->outputFormatComboBox_2->setEnabled(false);
 
3116
    globpro_string="100%";
 
3117
    if(unity_progressbar)
 
3118
        unity_launcher_entry_set_progress_visible(unity_progress, true);
 
3119
    if(unity_count)
 
3120
        unity_launcher_entry_set_count_visible(unity_progress, true);
 
3121
    video_conversion(0);
 
3122
}
 
3123
 
 
3124
void MainWindow::video_conversion(int return_code){
 
3125
    /*
 
3126
      This function is called like video_conversion(0) the 1st time
 
3127
      so as to begin the conversion. Then, it is called again
 
3128
      when the 1st conversion has finished and launches the
 
3129
      2nd conversion and so on, untill no files are left
 
3130
      for conversion.
 
3131
    */
 
3132
    //Values:
 
3133
    //current_file <-> 0 1 2
 
3134
    //num_files    <-> 1 2 3
 
3135
    QString format_text=ui->outputFormatComboBox_2->currentText();
 
3136
 
 
3137
        if(video_stop_pressed){
 
3138
            if(current_file!=0)
 
3139
                ui->tableWidget_2->removeCellWidget(current_file-1,2);
 
3140
            else
 
3141
                ui->tableWidget_2->removeCellWidget(current_file,2);
 
3142
            globpro_hasbeendeleted=true;
 
3143
            if(!QFile::remove(output_file))
 
3144
                cerr << QString("Error removing file '"+output_file+"'\n").toLocal8Bit().data();
 
3145
            return;
 
3146
        }
 
3147
        if(return_code){
 
3148
            /*
 
3149
              The conversion of the previous file wasn't successful!
 
3150
              Print error and guide the user to see the error logs...
 
3151
            */
 
3152
            if(all_errors.isEmpty())
 
3153
                all_errors+="'<b>"+input_file+"</b>'<br>";
 
3154
            else
 
3155
                all_errors+="\n'<b>"+input_file+"</b>'<br>";
 
3156
            current_file_errors.replace("\n","<br>");
 
3157
            all_errors+="<font color=red>"+current_file_errors+"</font><br>";
 
3158
            int to_apply=current_file;
 
3159
            if(to_apply>0)
 
3160
                to_apply--;
 
3161
            QTableWidgetItem *item_error1 = new QTableWidgetItem;
 
3162
            item_error1->setText(ui->tableWidget_2->item(to_apply,0)->text()+" - ERROR");
 
3163
            item_error1->setToolTip(ui->tableWidget_2->item(to_apply,0)->toolTip());
 
3164
            item_error1->setBackgroundColor(Qt::red);
 
3165
            item_error1->setTextColor(Qt::white);
 
3166
            ui->tableWidget_2->setItem(to_apply,0, item_error1);
 
3167
            QTableWidgetItem *item_error2 = new QTableWidgetItem;
 
3168
            item_error2->setText(ui->tableWidget_2->item(to_apply,1)->text());
 
3169
            item_error2->setToolTip(ui->tableWidget_2->item(to_apply,1)->toolTip());
 
3170
            item_error2->setBackgroundColor(Qt::red);
 
3171
            item_error2->setTextColor(Qt::white);
 
3172
            ui->tableWidget_2->setItem(to_apply,1, item_error2);
 
3173
            ui->show_errors->show();
 
3174
            current_file_errors.clear();
 
3175
            error_count++;
 
3176
        }
 
3177
        else
 
3178
        {
 
3179
            //IF NOT RETURN CODE
 
3180
            current_file_errors.clear();
 
3181
            /*
 
3182
              The conversion of the previous file was successful!
 
3183
              Check if the original (source) file is to be deleted...
 
3184
            */
 
3185
            if(delete_original){
 
3186
                if(QFile(input_file).exists()){
 
3187
                    if(system(QString("rm -f \""+input_file+"\"").toLocal8Bit().data()))
 
3188
                        cerr << QString("Could not delete source file '"+input_file+"'\nDo I have the permissions to do so?\n").toLocal8Bit().data();
 
3189
                }
 
3190
            }
 
3191
        }
 
3192
        if (current_file>num_files-1){
 
3193
            /*
 
3194
              !!!ALL FILES HAVE BEEN CONVERTED!!!
 
3195
              Update the progressbars and do the actions
 
3196
              that the user has chosen to do from bottom
 
3197
              right!!!
 
3198
            */
 
3199
            //all files have been converted!
 
3200
            //just update the progressbar
 
3201
            ui->progressBar_2->setFormat("100%");
 
3202
            ui->progressBar_2->setValue(100);
 
3203
            globpro->setFormat(globpro_string);
 
3204
            globpro->setValue(100);
 
3205
            QTableWidgetItem *item_progress = new QTableWidgetItem;
 
3206
            item_progress->setToolTip("Completed");
 
3207
            ui->tableWidget_2->setItem(current_file-1,2, item_progress);
 
3208
            ui->startBtn_2->setEnabled(false);
 
3209
            ui->startBtn_2->raise();
 
3210
            progressbar_animation(1,1);
 
3211
            ui->outputFormatComboBox_2->setEnabled(true);
 
3212
            if(unity_progressbar)
 
3213
                unity_launcher_entry_set_progress_visible(unity_progress, false);
 
3214
            if(unity_count)
 
3215
                unity_launcher_entry_set_count_visible(unity_progress, false);
 
3216
            if(desktop_notifications)
 
3217
                show_desktop_notification(0);
 
3218
            error_count=0;
 
3219
            actions_after_convert();
 
3220
            return;
 
3221
        }
 
3222
        if(ui->tableWidget_2->item(current_file,2)->toolTip()=="Completed"){
 
3223
            /*
 
3224
              The file's conversion has been completed, proceed to the next file...
 
3225
              The program goes through here if the user add more files after the
 
3226
              previous conversion has finished and choose to start converting...
 
3227
            */
 
3228
            current_file++;
 
3229
            video_conversion(0);
 
3230
            return;
 
3231
        }
 
3232
        if(globpro_string=="Already Exists" || !m4r_or_a_time){
 
3233
            if(current_file!=0){
 
3234
                if(globpro_hasbeendeleted){
 
3235
                    globpro = new QProgressBar(this);
 
3236
                    globpro_hasbeendeleted=false;
 
3237
                }
 
3238
                if(globpro->format()!="Already Exists" && ui->tableWidget_2->item(current_file-1,2)->toolTip()!="Completed"){
 
3239
                    QProgressBar *qtpro=new QProgressBar(this);
 
3240
                    ui->tableWidget_2->setCellWidget(current_file-1, 2, qtpro);
 
3241
                    QTableWidgetItem *item_progress = new QTableWidgetItem;
 
3242
                    item_progress->setToolTip("Completed");
 
3243
                    ui->tableWidget_2->setItem(current_file-1,2, item_progress);
 
3244
                    qtpro->setFormat(globpro_string);
 
3245
                    qtpro->setValue(100);
 
3246
                }
 
3247
            }
 
3248
            QProgressBar *qtpro=new QProgressBar(this);
 
3249
            ui->tableWidget_2->setCellWidget(current_file, 2, qtpro);
 
3250
            if(!globpro)
 
3251
                globpro = new QProgressBar(this);
 
3252
            globpro=qtpro;
 
3253
        }
 
3254
        //Action starts here...
 
3255
        input_file=ui->tableWidget_2->item(current_file,0)->toolTip();
 
3256
        QFileInfo info;
 
3257
        info.setFile(input_file);
 
3258
        output_file=ui->output_folder->toolTip()+"/"+info.completeBaseName()+"."+format_text;
 
3259
        command="avconv -i \""+input_file+"\" -qscale 1 ";
 
3260
        //Custom commands for the right extensions
 
3261
        if(format_text=="mov" ||format_text=="vob" ||format_text=="mp4" ||format_text=="mpg" ||format_text=="wmv" ||format_text=="mkv"){
 
3262
            if(info.completeSuffix()=="flv")
 
3263
                command+="-ar 44100 -f flv";
 
3264
            else if(info.completeSuffix()=="3gp"||info.completeSuffix()=="ogv")
 
3265
                command+="-ar 44100 -strict experimental";
 
3266
            else
 
3267
                command+="-strict experimental";
 
3268
        }
 
3269
        else if(format_text=="3gp"){
 
3270
            command+="-acodec aac -s qcif -ar 8000 -b 120000 -vcodec h263 -ab 10.2k -ac 1 -strict experimental";
 
3271
        }
 
3272
        else if(format_text=="avi"){
 
3273
            if(info.completeSuffix()=="flv")
 
3274
                command+="-ar 44100 -f flv";
 
3275
            else if(info.completeSuffix()=="ogv")
 
3276
                command+="-vcodec mpeg4 -acodec copy";
 
3277
            else
 
3278
                command+="-vcodec copy -acodec copy";
 
3279
        }
 
3280
        else if(format_text=="flv" || format_text=="ogv")
 
3281
            command+="-ar 44100 -f flv";
 
3282
 
 
3283
        if(video_bitrate)
 
3284
            command+=" -b:v "+QString::number(video_bitrate_value);
 
3285
        if(video_framerate)
 
3286
            command+=" -r "+QString::number(video_framerate_value);
 
3287
        if(video_crop)
 
3288
            command+=" -vf crop="+QString::number(video_width_value)+":"+QString::number(video_height_value)+":"+QString::number(video_x_value)+":"+QString::number(video_y_value);
 
3289
        if(video_resize)
 
3290
            command+=" -s "+video_resize_value;
 
3291
 
 
3292
        command+=" \""+output_file+"\"";
 
3293
        /*
 
3294
          Checking whether the output_file already exists. If it does, then
 
3295
          overwrite it, if it checked so, or proceed to the next file, if not...
 
3296
         */
 
3297
        if(QFile(output_file).exists()){
 
3298
            if(overwrite_existing){
 
3299
                if(!QFile::remove(output_file)){
 
3300
                    cerr << QString("Couldn't remove the already existing file '"+output_file+"'\n").toLocal8Bit().data();
 
3301
                    current_file++;
 
3302
                    globpro_string="Already Exists";
 
3303
                    video_conversion(0);
 
3304
                    return;
 
3305
                }
 
3306
            }
 
3307
            else
 
3308
            {
 
3309
                current_file++;
 
3310
                globpro_string="Already Exists";
 
3311
                video_conversion(0);
 
3312
                return;
 
3313
            }
 
3314
        }
 
3315
        //getting the duration of the input file...
 
3316
        duration_secs=Global::duration(input_file);
 
3317
        globpro_string="100%";
 
3318
        video_converter->start(command);
 
3319
        if(full_percent_instant){
 
3320
            full_percent_instant=false;
 
3321
            globpro->setValue(100);
 
3322
            globpro->setFormat(globpro_string);
 
3323
        }
 
3324
        update_unity_count_value(0);
 
3325
        current_file++;
 
3326
}
 
3327
 
 
3328
void MainWindow::video_read_output(){
 
3329
    //read all possible output so as to calculate the percentages...
 
3330
    QByteArray newData_stdout=video_converter->readAllStandardOutput();
 
3331
    QString stdout=QString::fromLocal8Bit(newData_stdout);
 
3332
    QByteArray newData_stderr=video_converter->readAllStandardError();
 
3333
    QString stderr=QString::fromLocal8Bit(newData_stderr);
 
3334
    QString out=stdout+stderr;
 
3335
    current_file_errors+=out;
 
3336
    if(out.contains("kB time=")){
 
3337
 
 
3338
        QRegExp regex("time=.* ");
 
3339
        regex.setMinimal(true);
 
3340
 
 
3341
        QStringList list;
 
3342
        int pos = 0;
 
3343
 
 
3344
        while ((pos = regex.indexIn(out, pos)) != -1)
 
3345
        {
 
3346
            list << regex.cap(0).remove(QChar(' '),Qt::CaseInsensitive);
 
3347
            pos += regex.matchedLength();
 
3348
        }
 
3349
        if(!list.count()){
 
3350
            //error
 
3351
            return;
 
3352
        }
 
3353
        QString current=list.at(0);
 
3354
        current.replace("time=","");
 
3355
        float cur_time=current.toFloat();
 
3356
        int percent=0;
 
3357
            percent=cur_time*100/duration_secs;
 
3358
 
 
3359
        globpro->setFormat(QString::number(percent)+"%");
 
3360
        globpro->setValue(percent);
 
3361
        ui->progressBar_2->setFormat(QString::number(percent/(num_files-already_completed)+(current_file-1-already_completed)*(100/(num_files-already_completed)))+"%");
 
3362
        ui->progressBar_2->setValue(percent/(num_files-already_completed)+(current_file-1-already_completed)*(100/(num_files-already_completed)));
 
3363
    }
 
3364
}
 
3365
 
 
3366
void MainWindow::on_stop_2_clicked()
 
3367
{
 
3368
    video_stop_pressed=true;
 
3369
    progressbar_animation(1,1);
 
3370
    ui->startBtn_2->raise();
 
3371
    video_converter->close();
 
3372
    ui->outputFormatComboBox_2->setEnabled(true);
 
3373
    if(unity_progressbar)
 
3374
        unity_launcher_entry_set_progress_visible(unity_progress, false);
 
3375
    if(unity_count)
 
3376
        unity_launcher_entry_set_count_visible(unity_progress, false);
 
3377
}
 
3378
 
 
3379
void MainWindow::on_radioButton_files_to_iso_clicked()
 
3380
{
 
3381
    ui->stackedWidget_3->setCurrentIndex(0);
 
3382
    ui->addBtn->show();
 
3383
    ui->addfolder->show();
 
3384
    ui->clearBtn->show();
 
3385
    ui->removeBtn->show();
 
3386
    ui->movedownButton->show();
 
3387
    ui->moveupButton->show();
 
3388
}
 
3389
 
 
3390
void MainWindow::on_radioButton_iso_cso_clicked()
 
3391
{
 
3392
    ui->stackedWidget_3->setCurrentIndex(1);
 
3393
    ui->addBtn->hide();
 
3394
    ui->addfolder->hide();
 
3395
    ui->clearBtn->hide();
 
3396
    ui->removeBtn->hide();
 
3397
    ui->movedownButton->hide();
 
3398
    ui->moveupButton->hide();
 
3399
}
 
3400
 
 
3401
void MainWindow::on_startBtn_8_clicked()
 
3402
{
 
3403
    if(ui->lineEdit_6->text().contains("/")){
 
3404
        QMessageBox::warning(this, "Error", "The output filename should not contain the '/' character!");
 
3405
        return;
 
3406
    }
 
3407
    QString output_iso=ui->output_folder->text()+"/"+ui->lineEdit_6->text()+".iso";
 
3408
    if(QFile(output_iso).exists()){
 
3409
        if(overwrite_existing){
 
3410
            if(!QFile::remove(output_iso)){
 
3411
                QMessageBox::warning(this, "Error", "The file '"+output_iso+"' already exists, but I cannot overwrite it!");
 
3412
                return;
 
3413
            }
 
3414
        }
 
3415
        else{
 
3416
            if(desktop_notifications){
 
3417
                file_exists=output_iso;
 
3418
                show_desktop_notification(1);
 
3419
                return;
 
3420
            }
 
3421
            else
 
3422
            {
 
3423
                QMessageBox::warning(this, "Error", "The file '"+output_iso+"' already exists!");
 
3424
                return;
 
3425
            }
 
3426
        }
 
3427
    }
 
3428
 
 
3429
    command="genisoimage -o \""+output_iso+"\"";
 
3430
    int table_count=ui->tableWidget_6->rowCount();
 
3431
    for(int i=0;i<table_count;i++){
 
3432
        QString file=ui->tableWidget_6->item(i,0)->toolTip();
 
3433
        if(!QFile(file).exists()){
 
3434
            if(desktop_notifications){
 
3435
                file_not_exists=file;
 
3436
                show_desktop_notification(3);
 
3437
                return;
 
3438
            }
 
3439
            else
 
3440
            {
 
3441
                QMessageBox::warning(this, "Error", "The file '"+file+"' doesn't exist!");
 
3442
                return;
 
3443
            }
 
3444
        }
 
3445
        command+=" \""+file+"\"";
 
3446
    }
 
3447
    ui->loading_label->show();
 
3448
    if(!ui->loading_label->movie()){
 
3449
        QMovie *mov= new QMovie(this);
 
3450
        mov->setFileName(":/pictures/Pictures/c.gif");
 
3451
        ui->loading_label->setMovie(mov);
 
3452
        mov->start();
 
3453
    }
 
3454
    else
 
3455
        ui->loading_label->movie()->start();
 
3456
 
 
3457
    files_to_iso->start(command);
 
3458
    ui->startBtn_8->hide();
 
3459
    ui->stop_6->show();
 
3460
    ui->lineEdit_6->setReadOnly(true);
 
3461
    ui->tableWidget_6->setFocus();
 
3462
}
 
3463
 
 
3464
void MainWindow::files_to_iso_end(int return_code)
 
3465
{
 
3466
    ui->stop_6->hide();
 
3467
    ui->startBtn_8->show();
 
3468
    ui->lineEdit_6->setReadOnly(false);
 
3469
    ui->loading_label->movie()->stop();
 
3470
    ui->loading_label->hide();
 
3471
    if(return_code){
 
3472
        //something went wrong
 
3473
        if(desktop_notifications)
 
3474
            show_desktop_notification(5);
 
3475
        else
 
3476
            QMessageBox::warning(this, "Error", "The creation of the iso file has failed!");
 
3477
    }
 
3478
    else
 
3479
    {
 
3480
    if(desktop_notifications)
 
3481
        show_desktop_notification(2);
 
3482
    else
 
3483
        QMessageBox::information(this, "Success!", "Your iso file has been created successfully!");
 
3484
    }
 
3485
}
 
3486
 
 
3487
void MainWindow::on_stop_6_clicked()
 
3488
{
 
3489
    files_to_iso->close();
 
3490
}
 
3491
 
 
3492
void MainWindow::on_select_iso_clicked()
 
3493
{
 
3494
    QString path;
 
3495
    path = QFileDialog::getOpenFileName(this, "Choose Iso File", QDir::homePath(), "*.iso");
 
3496
 
 
3497
    if(!path.isEmpty()){
 
3498
        ui->iso_to_cso_file->setText(path);
 
3499
        on_iso_to_cso_output_textEdited();
 
3500
    }
 
3501
 
 
3502
}
 
3503
 
 
3504
void MainWindow::on_select_cso_clicked()
 
3505
{
 
3506
    QString path;
 
3507
    path = QFileDialog::getOpenFileName(this, "Choose Iso File", QDir::homePath(), "*.cso");
 
3508
 
 
3509
    if(!path.isEmpty()){
 
3510
        ui->cso_to_iso_file->setText(path);
 
3511
        on_cso_to_iso_output_textEdited();
 
3512
    }
 
3513
}
 
3514
 
 
3515
void MainWindow::on_iso_to_cso_start_clicked()
 
3516
{
 
3517
    if(overwrite_existing && QFile(ui->output_folder->text()+"/"+ui->iso_to_cso_output->text()+".cso").exists()){
 
3518
        if(!QFile::remove(ui->output_folder->text()+"/"+ui->cso_to_iso_output->text()+".cso"))
 
3519
            cerr << QString("Couldn't delete '"+ui->output_folder->text()+"/"+ui->cso_to_iso_output->text()+".cso"+"'\nPlease, unless the file is problematic, report this bug and attach the file that it caused it!\n").toLocal8Bit().data();
 
3520
    }
 
3521
    else if(QFile(ui->output_folder->text()+"/"+ui->iso_to_cso_output->text()+".cso").exists()){
 
3522
        file_exists=ui->output_folder->text()+"/"+ui->iso_to_cso_output->text()+".cso";
 
3523
        if(desktop_notifications)
 
3524
            show_desktop_notification(1);
 
3525
        else{
 
3526
            QMessageBox::warning(this, "Error", "The file '"+file_exists+"' already exists!");
 
3527
            return;
 
3528
        }
 
3529
 
 
3530
        return;
 
3531
    }
 
3532
    if(!QFile(ui->iso_to_cso_file->text()).exists()){
 
3533
        file_not_exists=ui->iso_to_cso_file->text();
 
3534
        if(desktop_notifications)
 
3535
            show_desktop_notification(3);
 
3536
        else{
 
3537
            QMessageBox::warning(this, "Error", "The file '"+file_not_exists+"' doesn't exist!");
 
3538
            return;
 
3539
        }
 
3540
        return;
 
3541
    }
 
3542
    command="ciso "+QString::number(ui->iso_to_cso_spinBox->value())+" \""+ui->iso_to_cso_file->text()+"\" \""+ui->output_folder->text()+"/"+ui->iso_to_cso_output->text()+".cso\"";
 
3543
    ui->loading_label_2->show();
 
3544
    if(!ui->loading_label_2->movie()){
 
3545
        QMovie *mov= new QMovie(this);
 
3546
        mov->setFileName(":/pictures/Pictures/c.gif");
 
3547
        ui->loading_label_2->setMovie(mov);
 
3548
        mov->start();
 
3549
    }
 
3550
    else
 
3551
        ui->loading_label_2->movie()->start();
 
3552
    iso_to_cso->start(command);
 
3553
    ui->select_iso->setEnabled(false);
 
3554
    ui->iso_to_cso_output->setReadOnly(true);
 
3555
    ui->iso_to_cso_spinBox->setEnabled(false);
 
3556
    ui->iso_to_cso_start->hide();
 
3557
    ui->iso_to_cso_stop->show();
 
3558
}
 
3559
 
 
3560
void MainWindow::on_iso_to_cso_output_textEdited()
 
3561
{
 
3562
    if(!ui->iso_to_cso_output->text().isEmpty()&&!ui->iso_to_cso_file->text().isEmpty()&&!ui->iso_to_cso_start->isEnabled())
 
3563
      ui->iso_to_cso_start->setEnabled(true);
 
3564
    else if(ui->iso_to_cso_output->text().isEmpty()||ui->iso_to_cso_file->text().isEmpty())
 
3565
      ui->iso_to_cso_start->setEnabled(false);
 
3566
}
 
3567
 
 
3568
void MainWindow::on_cso_to_iso_output_textEdited()
 
3569
{
 
3570
    if(!ui->cso_to_iso_output->text().isEmpty()&&!ui->cso_to_iso_file->text().isEmpty()&&!ui->cso_to_iso_start->isEnabled())
 
3571
      ui->cso_to_iso_start->setEnabled(true);
 
3572
    else if(ui->cso_to_iso_output->text().isEmpty()||ui->cso_to_iso_file->text().isEmpty())
 
3573
      ui->cso_to_iso_start->setEnabled(false);
 
3574
}
 
3575
 
 
3576
void MainWindow::on_cso_to_iso_start_clicked()
 
3577
{
 
3578
    if(overwrite_existing && QFile(ui->output_folder->text()+"/"+ui->cso_to_iso_output->text()+".iso").exists()){
 
3579
        if(!QFile::remove(ui->output_folder->text()+"/"+ui->cso_to_iso_output->text()+".iso"))
 
3580
            cerr << QString("Couldn't delete '"+ui->output_folder->text()+"/"+ui->cso_to_iso_output->text()+".iso"+"'\nPlease, unless the file is problematic, report this bug and attach the file that it caused it!\n").toLocal8Bit().data();
 
3581
    }
 
3582
    else if(QFile(ui->output_folder->text()+"/"+ui->cso_to_iso_output->text()+".iso").exists()){
 
3583
        file_exists=ui->output_folder->text()+"/"+ui->cso_to_iso_output->text()+".iso";
 
3584
        if(desktop_notifications)
 
3585
            show_desktop_notification(1);
 
3586
        else{
 
3587
            QMessageBox::warning(this, "Error", "The file '"+file_exists+"' already exists!");
 
3588
            return;
 
3589
        }
 
3590
        return;
 
3591
    }
 
3592
    if(!QFile(ui->cso_to_iso_file->text()).exists()){
 
3593
        file_not_exists=ui->cso_to_iso_file->text();
 
3594
        if(desktop_notifications)
 
3595
            show_desktop_notification(3);
 
3596
        else{
 
3597
            QMessageBox::warning(this, "Error", "The file '"+file_not_exists+"' doesn't exist!");
 
3598
            return;
 
3599
        }
 
3600
        return;
 
3601
    }
 
3602
    command="ciso 0 \""+ui->cso_to_iso_file->text()+"\" \""+ui->output_folder->text()+"/"+ui->cso_to_iso_output->text()+".iso\"";
 
3603
    ui->loading_label_3->show();
 
3604
    if(!ui->loading_label_3->movie()){
 
3605
        QMovie *mov= new QMovie(this);
 
3606
        mov->setFileName(":/pictures/Pictures/c.gif");
 
3607
        ui->loading_label_3->setMovie(mov);
 
3608
        mov->start();
 
3609
    }
 
3610
    else
 
3611
        ui->loading_label_3->movie()->start();
 
3612
    cso_to_iso->start(command);
 
3613
    ui->select_cso->setEnabled(false);
 
3614
    ui->cso_to_iso_output->setReadOnly(true);
 
3615
    ui->cso_to_iso_start->hide();
 
3616
    ui->cso_to_iso_stop->show();
 
3617
}
 
3618
 
 
3619
void MainWindow::iso_to_cso_end(int return_code)
 
3620
{
 
3621
    if(return_code){
 
3622
        if(desktop_notifications)
 
3623
            show_desktop_notification(6);
 
3624
        else
 
3625
            QMessageBox::warning(this, "Error", "Your CSO file wasn't created successfully!");
 
3626
    }
 
3627
    else
 
3628
    {
 
3629
        if(!iso_to_cso_stop_pressed){
 
3630
            if(desktop_notifications)
 
3631
                show_desktop_notification(4);
 
3632
            else
 
3633
                QMessageBox::information(this, "Success!","Your CSO file has been successfully created!");
 
3634
        }
 
3635
     }
 
3636
    ui->loading_label_2->movie()->stop();
 
3637
    ui->loading_label_2->hide();
 
3638
    ui->iso_to_cso_stop->hide();
 
3639
    ui->iso_to_cso_start->show();
 
3640
    ui->iso_to_cso_output->setReadOnly(false);
 
3641
    ui->select_iso->setEnabled(true);
 
3642
    ui->iso_to_cso_spinBox->setEnabled(true);
 
3643
}
 
3644
 
 
3645
void MainWindow::cso_to_iso_end(int return_code)
 
3646
{
 
3647
    //5
 
3648
    if(return_code){
 
3649
        if(desktop_notifications)
 
3650
            show_desktop_notification(5);
 
3651
        else
 
3652
            QMessageBox::warning(this, "Error", "Error creating your ISO file!");
 
3653
    }
 
3654
    else
 
3655
    {
 
3656
        if(!cso_to_iso_stop_pressed){
 
3657
            if(desktop_notifications)
 
3658
                show_desktop_notification(2);
 
3659
            else
 
3660
                QMessageBox::information(this, "Success!","Your ISO file has been successfully created!");
 
3661
        }
 
3662
    }
 
3663
    ui->loading_label_3->movie()->stop();
 
3664
    ui->loading_label_3->hide();
 
3665
    ui->cso_to_iso_stop->hide();
 
3666
    ui->cso_to_iso_start->show();
 
3667
    ui->cso_to_iso_output->setReadOnly(false);
 
3668
    ui->select_cso->setEnabled(true);
 
3669
}
 
3670
 
 
3671
void MainWindow::on_iso_to_cso_stop_clicked()
 
3672
{
 
3673
    iso_to_cso_stop_pressed=true;
 
3674
    iso_to_cso->close();
 
3675
    ui->iso_to_cso_stop->hide();
 
3676
    ui->iso_to_cso_start->show();
 
3677
    if(!QFile::remove(ui->output_folder->text()+"/"+ui->iso_to_cso_output->text()+".cso"))
 
3678
        cerr << QString("Couldn't delete '"+ui->output_folder->text()+"/"+ui->iso_to_cso_output->text()+".cso"+"'\nPlease, unless the file is problematic, report this bug and attach the file that it caused it!\n").toLocal8Bit().data();
 
3679
}
 
3680
 
 
3681
void MainWindow::on_cso_to_iso_stop_clicked()
 
3682
{
 
3683
    cso_to_iso_stop_pressed=true;
 
3684
    cso_to_iso->close();
 
3685
    ui->cso_to_iso_stop->hide();
 
3686
    ui->cso_to_iso_start->show();
 
3687
    if(!QFile::remove(ui->output_folder->text()+"/"+ui->iso_to_cso_output->text()+".cso"))
 
3688
        cerr << QString("Couldn't delete '"+ui->output_folder->text()+"/"+ui->iso_to_cso_output->text()+".cso"+"'\nPlease, unless the file is problematic, report this bug and attach the file that it caused it!\n").toLocal8Bit().data();
 
3689
}
 
3690
 
 
3691
void MainWindow::open_containing_folder(){
 
3692
    if(QFile("/usr/bin/nautilus").exists() && !system("pidof nautilus > /dev/null")){
 
3693
        //nautilus is installed and it runs! Better open the file with it, so as to be selected!
 
3694
        if(cur_index==0){
 
3695
            if(system(QString("nautilus \""+ui->tableWidget->item(ui->tableWidget->currentRow(), 0)->toolTip()+"\"").toLocal8Bit().data()))
 
3696
                cerr << "Could not open containing folder with nautilus.\n";
 
3697
        }
 
3698
        else if(cur_index==1){
 
3699
            if(system(QString("nautilus \""+ui->tableWidget_2->item(ui->tableWidget_2->currentRow(), 0)->toolTip()+"\"").toLocal8Bit().data()))
 
3700
                cerr << "Could not open containing folder with nautilus.\n";
 
3701
        }
 
3702
        else if(cur_index==2){
 
3703
            if(system(QString("nautilus \""+ui->tableWidget_3->item(ui->tableWidget_3->currentRow(), 0)->toolTip()+"\"").toLocal8Bit().data()))
 
3704
                cerr << "Could not open containing folder with nautilus.\n";
 
3705
        }
 
3706
    }
 
3707
    else
 
3708
    {
 
3709
        QFileInfo inf;
 
3710
        if(cur_index==0)
 
3711
            inf.setFile(ui->tableWidget->item(ui->tableWidget->currentRow(), 0)->toolTip());
 
3712
        else if(cur_index==1)
 
3713
                inf.setFile(ui->tableWidget_2->item(ui->tableWidget_2->currentRow(), 0)->toolTip());
 
3714
        else if(cur_index==1)
 
3715
                inf.setFile(ui->tableWidget_3->item(ui->tableWidget_3->currentRow(), 0)->toolTip());
 
3716
        if(system(QString("xdg-open \""+inf.path()+"\"").toLocal8Bit().data()))
 
3717
            cerr << "Could not open containing folder.\n";
 
3718
    }
 
3719
}
 
3720
 
 
3721
void MainWindow::show_audio_video_item_properties(){
 
3722
    if(cur_index==0){
 
3723
        if(!QFile(ui->tableWidget->item(ui->tableWidget->currentRow(), 0)->toolTip()).exists()){
 
3724
            QMessageBox::information(this, "Error", "The file you specified does not exist!");
 
3725
            return;
 
3726
        }
 
3727
        prop=new audio_video_properties(ui->tableWidget->item(ui->tableWidget->currentRow(), 0)->toolTip(), this);
 
3728
    }
 
3729
    else if(cur_index==1){
 
3730
        if(!QFile(ui->tableWidget_2->item(ui->tableWidget_2->currentRow(), 0)->toolTip()).exists()){
 
3731
            QMessageBox::information(this, "Error", "The file you specified does not exist!");
 
3732
            return;
 
3733
        }
 
3734
        prop=new audio_video_properties(ui->tableWidget_2->item(ui->tableWidget_2->currentRow(), 0)->toolTip(), this);
 
3735
    }
 
3736
    prop->exec();
 
3737
}
 
3738
 
 
3739
void MainWindow::show_image_properties(){
 
3740
    if(!QFile(ui->tableWidget_3->item(ui->tableWidget_3->currentRow(), 0)->toolTip()).exists()){
 
3741
        QMessageBox::information(this, "Error", "The image you specified does not exist!");
 
3742
        return;
 
3743
    }
 
3744
    if(QImage(ui->tableWidget_3->item(ui->tableWidget_3->currentRow(), 0)->toolTip()).isNull()){
 
3745
        QMessageBox::information(this, "Error", "The image you specified is invalid!");
 
3746
        return;
 
3747
    }
 
3748
    improp=new image_properties(ui->tableWidget_3->item(ui->tableWidget_3->currentRow(), 0)->toolTip(), this);
 
3749
    improp->exec();
 
3750
}
 
3751
 
 
3752
void MainWindow::on_tableWidget_customContextMenuRequested()
 
3753
{
 
3754
    QMenu menu;
 
3755
    menu.addAction("Open Folder",this,SLOT(open_containing_folder()));
 
3756
    menu.addAction("Remove",this,SLOT(on_removeBtn_clicked()));
 
3757
    menu.addAction("Properties",this,SLOT(show_audio_video_item_properties()));
 
3758
    if (ui->tableWidget->rowCount() > 0){
 
3759
        if(! ui->tableWidget->currentIndex().isValid() || !ui->tableWidget->currentItem()->isSelected())
 
3760
        {
 
3761
            menu.actions().at(0)->setEnabled(false);
 
3762
            menu.actions().at(1)->setEnabled(false);
 
3763
            menu.actions().at(2)->setEnabled(false);
 
3764
        }
 
3765
    menu.exec(QCursor::pos());
 
3766
    }
 
3767
}
 
3768
 
 
3769
void MainWindow::on_tableWidget_2_customContextMenuRequested()
 
3770
{
 
3771
    QMenu menu;
 
3772
    menu.addAction("Open Folder",this,SLOT(open_containing_folder()));
 
3773
    menu.addAction("Remove",this,SLOT(on_removeBtn_clicked()));
 
3774
    menu.addAction("Properties",this,SLOT(show_audio_video_item_properties()));
 
3775
    if (ui->tableWidget_2->rowCount() > 0){
 
3776
        if(! ui->tableWidget_2->currentIndex().isValid() || !ui->tableWidget_2->currentItem()->isSelected())
 
3777
        {
 
3778
            menu.actions().at(0)->setEnabled(false);
 
3779
            menu.actions().at(1)->setEnabled(false);
 
3780
            menu.actions().at(2)->setEnabled(false);
 
3781
        }
 
3782
    menu.exec(QCursor::pos());
 
3783
    }
 
3784
}
 
3785
 
 
3786
void MainWindow::on_tableWidget_3_customContextMenuRequested()
 
3787
{
 
3788
    QMenu menu;
 
3789
    menu.addAction("Open Folder",this,SLOT(open_containing_folder()));
 
3790
    menu.addAction("Remove",this,SLOT(on_removeBtn_clicked()));
 
3791
    menu.addAction("Properties",this,SLOT(show_image_properties()));
 
3792
    if (ui->tableWidget_3->rowCount() > 0){
 
3793
        if(! ui->tableWidget_3->currentIndex().isValid() || !ui->tableWidget_3->currentItem()->isSelected())
 
3794
        {
 
3795
            menu.actions().at(0)->setEnabled(false);
 
3796
            menu.actions().at(1)->setEnabled(false);
 
3797
            menu.actions().at(2)->setEnabled(false);
 
3798
        }
 
3799
    menu.exec(QCursor::pos());
 
3800
    }
 
3801
}
 
3802
void MainWindow::on_selectavi_clicked()
 
3803
{
 
3804
    QString avi_file;
 
3805
    if(QFile(QDir::homePath()+"/Videos").exists())
 
3806
        avi_file=QFileDialog::getOpenFileName(this, "Select avi file", QDir::homePath()+"/Videos", "*.avi");
 
3807
    else
 
3808
        avi_file=QFileDialog::getOpenFileName(this, "Select avi file", QDir::homePath(), "*.avi");
 
3809
    if(avi_file.isEmpty())
 
3810
        return;
 
3811
    ui->avifile_path->setText(avi_file);
 
3812
}
 
3813
 
 
3814
void MainWindow::on_selectsubtitle_clicked()
 
3815
{
 
3816
    QString sub_file;
 
3817
    if(QFile(QDir::homePath()+"/Videos").exists())
 
3818
        sub_file=QFileDialog::getOpenFileName(this, "Select avi file", QDir::homePath()+"/Videos", "*.sub *.srt");
 
3819
    else
 
3820
        sub_file=QFileDialog::getOpenFileName(this, "Select avi file", QDir::homePath(), "*.sub *.srt");
 
3821
    if(sub_file.isEmpty())
 
3822
        return;
 
3823
    ui->subtitlefile_path->setText(sub_file);
 
3824
}
 
3825
 
 
3826
void MainWindow::on_after_convertion2_currentIndexChanged(int index)
 
3827
{
 
3828
    switch(index){
 
3829
    case 0:
 
3830
        ui->after_convertion_icon->setPixmap(QIcon::fromTheme("dialog-error").pixmap(22));
 
3831
        break;
 
3832
    case 1:
 
3833
        ui->after_convertion_icon->setPixmap(QIcon::fromTheme("system-shutdown").pixmap(22));
 
3834
        break;
 
3835
    case 2:
 
3836
        ui->after_convertion_icon->setPixmap(QIcon::fromTheme("weather-few-clouds-night").pixmap(22));
 
3837
        break;
 
3838
    case 3:
 
3839
        ui->after_convertion_icon->setPixmap(QIcon::fromTheme("system-restart").pixmap(22));
 
3840
        break;
 
3841
    case 4:
 
3842
        ui->after_convertion_icon->setPixmap(QIcon::fromTheme("system-lock-screen").pixmap(22));
 
3843
        break;
 
3844
    case 5:
 
3845
        ui->after_convertion_icon->setPixmap(QIcon::fromTheme("window-close").pixmap(22));
 
3846
        break;
 
3847
    }
 
3848
}
 
3849
 
 
3850
void MainWindow::on_encode_start_clicked()
 
3851
{
 
3852
    QString input_avi=ui->avifile_path->text();
 
3853
    QString input_sub=ui->subtitlefile_path->text();
 
3854
    if(!QFile(input_avi).exists()){
 
3855
        QMessageBox::warning(this, "Error", "The avi file specified does not exist!");
 
3856
        return;
 
3857
    }
 
3858
    if(!QFile(input_sub).exists()){
 
3859
        QMessageBox::warning(this, "Error", "The subtitle file specified does not exist!");
 
3860
        return;
 
3861
    }
 
3862
    QFileInfo inf;
 
3863
    inf.setFile(input_avi);
 
3864
 
 
3865
    QString output_avi=ui->output_folder->text()+"/"+inf.completeBaseName()+".avi";
 
3866
    if(QFile(output_avi).exists()){
 
3867
        if(overwrite_existing){
 
3868
            if(!QFile::remove(output_avi)){
 
3869
                QMessageBox::warning(this, "Error", "The file '"+output_avi+"' already exists, but I cannot overwrite it!");
 
3870
                return;
 
3871
            }
 
3872
        }
 
3873
        else{
 
3874
            if(desktop_notifications){
 
3875
                file_exists=output_avi;
 
3876
                show_desktop_notification(7);
 
3877
                return;
 
3878
            }
 
3879
            else
 
3880
            {
 
3881
                QMessageBox::warning(this, "Error", "The file '"+output_avi+"' already exists!");
 
3882
                return;
 
3883
            }
 
3884
        }
 
3885
    }
 
3886
 
 
3887
    //mencoder input.avi -oac copy -ovc lavc -sub input.srt -utf8-o output.avi
 
3888
 
 
3889
    command="mencoder \""+input_avi+"\" -oac copy -ovc lavc -sub \""+input_sub+"\" -utf8 -o \""+output_avi+"\"";
 
3890
    ui->loading_label_sub_encoding->show();
 
3891
    if(!ui->loading_label->movie()){
 
3892
        QMovie *mov= new QMovie(this);
 
3893
        mov->setFileName(":/pictures/Pictures/c.gif");
 
3894
        ui->loading_label_sub_encoding->setMovie(mov);
 
3895
        mov->start();
 
3896
    }
 
3897
    else
 
3898
        ui->loading_label->movie()->start();
 
3899
 
 
3900
    sub_encoding->start(command);
 
3901
    ui->encode_start->hide();
 
3902
    ui->encode_stop->show();
 
3903
    ui->selectavi->setEnabled(false);
 
3904
    ui->selectsubtitle->setEnabled(false);
 
3905
}
 
3906
 
 
3907
void MainWindow::on_encode_stop_clicked()
 
3908
{
 
3909
    sub_stop_pressed=true;
 
3910
    sub_encoding->close();
 
3911
    ui->loading_label_sub_encoding->movie()->stop();
 
3912
    ui->loading_label_sub_encoding->hide();
 
3913
    ui->encode_stop->hide();
 
3914
    ui->encode_start->show();
 
3915
    ui->selectavi->setEnabled(true);
 
3916
    ui->selectsubtitle->setEnabled(true);
 
3917
}
 
3918
 
 
3919
void MainWindow::sub_encoding_end(int return_code){
 
3920
    if(return_code){
 
3921
        if(!sub_stop_pressed){
 
3922
            if(desktop_notifications)
 
3923
                show_desktop_notification(9);
 
3924
            else
 
3925
                QMessageBox::warning(this, "Error", "Your subtitles' encoding hasn't finished successfully!");
 
3926
        }
 
3927
    }
 
3928
    else
 
3929
    {
 
3930
        if(!iso_to_cso_stop_pressed){
 
3931
            if(desktop_notifications)
 
3932
                show_desktop_notification(8);
 
3933
            else
 
3934
                QMessageBox::information(this, "Success!","Your subtitles' encoding was successful!");
 
3935
        }
 
3936
     }
 
3937
    ui->loading_label_sub_encoding->movie()->stop();
 
3938
    ui->loading_label_sub_encoding->hide();
 
3939
    ui->encode_stop->hide();
 
3940
    ui->encode_start->show();
 
3941
    ui->selectavi->setEnabled(true);
 
3942
    ui->selectsubtitle->setEnabled(true);
 
3943
}