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 "TrayUI.h"
8 
9 #include <LuminaXDG.h>
10 #include<QUuid>
11 
TrayUI(QObject * parent)12 TrayUI::TrayUI(QObject *parent) : QSystemTrayIcon(parent){
13   this->setContextMenu( new QMenu() );
14   this->setIcon(LXDG::findIcon("Insight-FileManager",""));
15   connect(this, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(TrayActivated()));
16 }
17 
~TrayUI()18 TrayUI::~TrayUI(){
19   this->contextMenu()->deleteLater();
20 }
21 
StartOperation(FILEOP op,QStringList oldF,QStringList newF)22 void TrayUI::StartOperation( FILEOP op, QStringList oldF, QStringList newF){
23   createOP(op, oldF, newF);
24   QTimer::singleShot(1000, this, SLOT(checkJobs()));
25 }
26 
createOP(FILEOP type,QStringList oldF,QStringList newF)27 void TrayUI::createOP( FILEOP type, QStringList oldF, QStringList newF){
28   OPWidget *OP = new OPWidget();
29   if(type==MOVE){ OP->setupOperation("move", oldF, newF); }
30   else if(type==COPY){ OP->setupOperation("copy", oldF, newF); }
31   else if(type==DELETE){ OP->setupOperation("delete",oldF, QStringList()); }
32   else{ OP->deleteLater(); return; } //invalid type of operation
33   OP->setWhatsThis( QUuid::createUuid().toString() );
34   this->contextMenu()->addAction(OP->widgetAction());
35   OPS << OP;
36   connect(OP, SIGNAL(starting(QString)), this, SLOT(OperationStarted(QString)) );
37   connect(OP, SIGNAL(finished(QString)), this, SLOT(OperationFinished(QString)) );
38   connect(OP, SIGNAL(closed(QString)), this, SLOT(OperationClosed(QString)) );
39   QTimer::singleShot(0, OP, SLOT(startOperation()) );
40 }
41 
TrayActivated()42 void TrayUI::TrayActivated(){
43   this->contextMenu()->popup( this->geometry().center() );
44 }
45 
46 //Operation Widget Responses
OperationClosed(QString ID)47 void TrayUI::OperationClosed(QString ID){
48   for(int i=0; i<OPS.length(); i++){
49     if(OPS[i]->whatsThis()==ID){
50       //qDebug() << "Removing OPWidget:" << ID;
51       //this->contextMenu()->removeAction(OPS[i]->widgetAction());
52       OPS.takeAt(i)->deleteLater();
53       break;
54     }
55   }
56   QTimer::singleShot(1000, this, SLOT(checkJobs()) );
57 }
58 
OperationStarted(QString ID)59 void TrayUI::OperationStarted(QString ID){
60   for(int i=0; i<OPS.length(); i++){
61     if(OPS[i]->whatsThis()==ID){
62       //NOTHING FOR NOW - ENABLE POPUPS LATER (if desired - they can get annoying for short operations)
63     }
64   }
65 }
66 
OperationFinished(QString ID)67 void TrayUI::OperationFinished(QString ID){
68   //qDebug() << "Op Finished:" << ID;
69   for(int i=0; i<OPS.length(); i++){
70     if(OPS[i]->whatsThis()!=ID){ continue; }
71     //qDebug() << " - found widget";
72     bool err = OPS[i]->hasErrors();
73     //qDebug() << " -- Errors:" << err << "Duration:" << OPS[i]->duration();
74     //Assemble the notification (if more than 1 second to perform operation)
75     if(OPS[i]->duration()>1){
76       this->showMessage( tr("Finished"), err ? tr("Errors during operation. Click to view details") : "", err ? QSystemTrayIcon::Warning : QSystemTrayIcon::Information);
77     }
78     //Close the widget if no errors
79     if(!err){ OperationClosed(ID); }
80     break;
81   }
82 }
83 
checkJobs()84 void TrayUI::checkJobs(){
85   if(OPS.isEmpty()){
86     emit JobsFinished();
87     this->hide();
88   }else{
89     bool showNotify = !this->isVisible();
90     this->show();
91     if(showNotify){ this->showMessage(tr("New Tasks Running"),"",QSystemTrayIcon::NoIcon, 2000); }
92   }
93 }
94