1 //===========================================
2 //  Lumina-DE source code
3 //  Copyright (c) 2016, Ken Moore
4 //  Available under the 3-clause BSD license
5 //  See the LICENSE file for full details
6 //===========================================
7 #include "OPWidget.h"
8 #include "ui_OPWidget.h"
9 
10 #include "ScrollDialog.h"
11 
OPWidget(QWidget * parent)12 OPWidget::OPWidget(QWidget *parent) : QWidget(parent), ui(new Ui::OPWidget()){
13   starttime = endtime = -1;
14   WA = new QWidgetAction(0);
15   WA->setDefaultWidget(this);
16   worker = 0;
17   workthread = 0;
18   dlg = 0;
19   //Now create the widget
20   ui->setupUi(this);
21   ui->tool_close->setIcon( LXDG::findIcon("dialog-close","view-close") );
22   ui->tool_showerrors->setIcon(LXDG::findIcon("view-choose","view-preview"));
23   //connect the widget buttons
24   connect(ui->tool_close, SIGNAL(clicked()), this, SLOT(closeWidget()) );
25   connect(ui->tool_showerrors, SIGNAL(clicked()), this, SLOT(showErrors()) );
26 }
27 
~OPWidget()28 OPWidget::~OPWidget(){
29   if(worker!=0){ worker->stopped = true; worker->deleteLater(); }
30   if(workthread!=0){ workthread->quit(); workthread->wait(); delete workthread; }
31   WA->deleteLater();
32   if(dlg!=0){ dlg->deleteLater(); }
33 }
34 
widgetAction()35 QWidgetAction* OPWidget::widgetAction(){
36   return WA;
37 }
38 
setupOperation(QString optype,QStringList oldF,QStringList newF)39 void OPWidget::setupOperation(QString optype, QStringList oldF, QStringList newF){
40   if(workthread==0){ workthread = new QThread(); }
41   if(worker==0){
42     worker = new FOWorker();
43     connect(worker, SIGNAL(startingItem(int,int,QString,QString)), this, SLOT(opUpdate(int,int,QString,QString)) );
44     connect(worker, SIGNAL(finished(QStringList)), this, SLOT(opFinished(QStringList)) );
45     worker->moveToThread(workthread);
46   }
47   workthread->start();
48   //Now setup the worker with the desired operation
49   optype = optype.toLower();
50   worker->ofiles = oldF;
51   worker->nfiles = newF;
52   if(optype=="move"){ worker->isMV = true; tract = tr("Move"); }
53   else if(optype=="copy"){ worker->isCP = true; tract = tr("Copy"); }
54   else if(optype=="delete"){ worker->isRM = true; tract = tr("Remove"); }
55 
56 }
57 
58 
isDone()59 bool OPWidget::isDone(){
60   return (endtime>0);
61 }
62 
hasErrors()63 bool OPWidget::hasErrors(){
64   return !Errors.isEmpty();
65 }
66 
duration()67 float OPWidget::duration(){
68   return ( (endtime-starttime)/1000.0); //convert from ms to s
69 }
70 
71 
finalStat()72 QString OPWidget::finalStat(){
73   return ui->label->text();
74 }
75 
76 
77 //PUBLIC SLOTS
startOperation()78 void OPWidget::startOperation(){
79   starttime = QDateTime::currentMSecsSinceEpoch();
80   endtime = -1;
81   QTimer::singleShot(0, worker, SLOT(slotStartOperations()) );
82   emit starting(this->whatsThis());
83 }
84 
85 
86 // PRIVATE SLOTS
closeWidget()87 void OPWidget::closeWidget(){
88   if(!isDone()){ worker->stopped = true; }
89   else{ emit closed(this->whatsThis()); }
90 }
91 
showErrors()92 void OPWidget::showErrors(){
93   qDebug() << "Errors:" << Errors;
94   if(dlg==0){
95     dlg = new ScrollDialog(); //need this to persist outside this function
96     dlg->setWindowTitle(tr("File Operation Errors"));
97     dlg->setText( Errors.join("\n") );
98   }
99   dlg->showNormal();
100 }
101 
opFinished(QStringList errors)102 void OPWidget::opFinished(QStringList errors){
103   Errors = errors;
104   endtime = QDateTime::currentMSecsSinceEpoch();
105   emit finished(this->whatsThis());
106   ui->progressBar->setValue(ui->progressBar->maximum()); //last item finished
107   ui->tool_showerrors->setVisible(!Errors.isEmpty());
108   ui->label->setText( QString(tr("%1 Finished")).arg(tract) + (errors.isEmpty() ? "" : (" ("+tr("Errors Occured")+")") ) );
109 }
110 
opUpdate(int cur,int tot,QString ofile,QString nfile)111 void OPWidget::opUpdate(int cur, int tot, QString ofile, QString nfile){ //current, total, old file, new file
112   ui->progressBar->setRange(0,tot);
113   ui->progressBar->setValue(cur);
114   QString txt = tract +": "+ofile.section("/",-1);
115   if(!nfile.isEmpty()){txt.append(" -> "+nfile.section("/",-1) ); }
116   ui->label->setText( txt);
117 }
118