source: src-qt4/pc-softwaremanager/processManager.cpp @ 78fa8b2

9.2-releasereleng/10.0releng/10.0.1releng/10.0.2
Last change on this file since 78fa8b2 was 78fa8b2, checked in by Ken Moore <ken@…>, 14 months ago

Merge the new AppCafe? into the src-qt4 tree from the projects directory

  • Property mode set to 100644
File size: 10.2 KB
Line 
1/***************************************************************************
2 *   Copyright (C) 2011 - iXsystems                                       *
3 *   kris@pcbsd.org  *
4 *   tim@pcbsd.org   *
5 *   ken@pcbsd.org   *
6 *                                                                         *
7 *   Permission is hereby granted, free of charge, to any person obtaining *
8 *   a copy of this software and associated documentation files (the       *
9 *   "Software"), to deal in the Software without restriction, including   *
10 *   without limitation the rights to use, copy, modify, merge, publish,   *
11 *   distribute, sublicense, and/or sell copies of the Software, and to    *
12 *   permit persons to whom the Software is furnished to do so, subject to *
13 *   the following conditions:                                             *
14 *                                                                         *
15 *   The above copyright notice and this permission notice shall be        *
16 *   included in all copies or substantial portions of the Software.       *
17 *                                                                         *
18 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       *
19 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    *
20 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
21 *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR     *
22 *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
23 *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
24 *   OTHER DEALINGS IN THE SOFTWARE.                                       *
25 ***************************************************************************/
26 #include "processManager.h"
27
28ProcessManager::ProcessManager(){
29  //Get the system environment for all the processes
30  QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
31    env.insert("PBI_FETCH_PARSING","YES"); //For readable download notifications
32    //Initialize the UPDATE Process
33    upProc = new QProcess; upProc->setProcessEnvironment(env);
34    upProc->setProcessChannelMode(QProcess::MergedChannels);
35    connect(upProc, SIGNAL(readyRead()),this,SLOT(slotUpProcMessage()) );
36    connect(upProc, SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(slotUpProcFinished()) );
37    //Initialize the REMOVE Process
38    remProc = new QProcess; remProc->setProcessEnvironment(env);
39    remProc->setProcessChannelMode(QProcess::MergedChannels);
40    connect(remProc, SIGNAL(readyRead()),this,SLOT(slotRemProcMessage()) );
41    connect(remProc, SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(slotRemProcFinished()) );
42    //Initialize the DOWNLOAD Process
43    dlProc = new QProcess; dlProc->setProcessEnvironment(env);
44    dlProc->setProcessChannelMode(QProcess::MergedChannels);
45    connect(dlProc, SIGNAL(readyRead()),this,SLOT(slotDlProcMessage()) );
46    connect(dlProc, SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(slotDlProcFinished()) );
47    //Initialize the INSTALL Process
48    inProc = new QProcess; inProc->setProcessEnvironment(env);
49    inProc->setProcessChannelMode(QProcess::MergedChannels);
50    connect(inProc, SIGNAL(readyRead()),this,SLOT(slotInProcMessage()) );
51    connect(inProc, SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(slotInProcFinished()) );
52    //Initialize the OTHER Process
53    otProc = new QProcess; otProc->setProcessEnvironment(env);
54    connect(otProc, SIGNAL(readyReadStandardOutput()),this,SLOT(slotOtProcMessage()) );
55    connect(otProc, SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(slotOtProcFinished()) );
56}
57
58ProcessManager::~ProcessManager(){
59}
60
61void ProcessManager::goToDirectory(ProcessID ID, QString dir){
62  if( ID == UPDATE ){
63    upProc->setWorkingDirectory(dir);     
64  }else if( ID == REMOVE ){
65    remProc->setWorkingDirectory(dir);           
66  }else if( ID == DOWNLOAD ){
67    dlProc->setWorkingDirectory(dir);     
68  }else if( ID == INSTALL ){
69    inProc->setWorkingDirectory(dir);             
70  }else if( ID == OTHER ){
71    otProc->setWorkingDirectory(dir);             
72  }     
73}
74// =========================
75// ===== PUBLIC ACCESS =====
76// =========================
77void ProcessManager::startProcess(ProcessID ID, QString cmd){
78  if( ID == UPDATE ){
79    qDebug() << "Update Process Started:" << cmd;
80    upProc->start(cmd);   
81  }else if( ID == REMOVE ){
82    qDebug() << "Removal Process Started:" << cmd;
83    remProc->start(cmd);                 
84  }else if( ID == DOWNLOAD ){
85    qDebug() << "Download Process Started:" << cmd;
86    dlProc->start(cmd);           
87  }else if( ID == INSTALL ){
88    qDebug() << "Install Process Started:" << cmd;
89    inProc->start(cmd);           
90  }else if( ID == OTHER ){
91    qDebug() << "Other Process Started:" << cmd;
92    otProc->start(cmd);           
93  }
94       
95}
96
97void ProcessManager::stopProcess(ProcessID ID){
98  if((ID == ALL) || (ID == UPDATE)){
99    upProc->terminate();         
100  }
101  if((ID == ALL) || (ID == REMOVE)){
102    remProc->terminate();         
103  }
104  if((ID == ALL) || (ID == DOWNLOAD)){
105    dlProc->terminate();         
106  }
107  if((ID == ALL) || (ID == INSTALL)){
108    inProc->terminate();         
109  }
110  if((ID == ALL) || (ID == OTHER)){
111    otProc->terminate();         
112  }     
113}
114
115
116// =========================
117// ===== PRIVATE SLOTS =====
118// =========================
119QString ProcessManager::parseDlLine(QString line){
120  QString out;
121  if(!line.startsWith("SIZE:")){ return out; }
122  //qDebug() << "parse Download Line:" << line;
123  //Line format: SIZE:  <KB> DOWNLOADED:  <KB> SPEED:  <KB/s> KB/s
124  line = line.simplified();
125  line.replace("SIZE: ","");
126  line.replace("DOWNLOADED: ", "");
127  line.replace("SPEED: ","");
128  line.replace("KB/s","");
129  bool totok, curok, spdok;
130  double tot, cur, spd;
131  tot = line.section(" ",0,0).toDouble(&totok);
132  cur = line.section(" ",1,1).toDouble(&curok);
133  spd = line.section(" ",2,2).toDouble(&spdok);
134  //qDebug() << " - DownloadStats:" << tot << cur << spd;
135  if(totok && tot==0){totok=FALSE;}
136  if(curok && cur==0){curok=FALSE;}
137  if(spdok && spd==0){spdok=FALSE;}
138  //Now format the output string
139  QString stats;
140  if(totok && curok){
141    bool totErr = (tot==cur); //catch for a display error where the cur is always identical to the tot
142    int i=0;
143    QStringList lab; lab << "KB" <<"MB"<<"GB"<<"TB"<<"PB";
144    while( (tot>1000) && (i<lab.length()) ){
145      cur=cur/1024; tot=tot/1024; i++;
146    }
147    float percent = (cur*100)/tot;
148    //round all numbers to one decimel place
149    percent = int(percent*10)/10.0;
150    cur = int(cur*10)/10.0;
151    tot = int(tot*10)/10.0;
152    if(totErr){ // cur==tot
153      stats = QString::number(tot)+" "+lab[i];
154    }else{
155      stats = QString::number(cur)+"/"+QString::number(tot)+" "+lab[i]+" ("+QString::number(percent)+"%)";
156    }
157    // Format:  <current>/<total> <size label> (<percent>%)
158  }else if(curok){
159    stats = Extras::sizeKToDisplay(QString::number(cur));
160  }
161  QString speed;
162  if(spdok){
163    speed = Extras::sizeKToDisplay(QString::number(spd))+"/s";   
164  }
165  //Now put the output string together and return it
166  if(stats.isEmpty() && speed.isEmpty()){}
167  else if(stats.isEmpty()){ out = speed; }
168  else if(speed.isEmpty()){ out = stats; }
169  else{ out = QString( tr("%1 at %2") ).arg(stats,speed); }
170  //qDebug() << " - Result:" << out;
171  return out;
172}
173
174// == UPDATE PROCESS ==
175void ProcessManager::slotUpProcMessage(){
176    while( upProc->canReadLine() ){
177    QString line = upProc->readLine().simplified();
178    QString dl = parseDlLine(line);
179    if(!dl.isEmpty()){ emit ProcessMessage(UPDATE,dl); }
180  }
181}
182
183void ProcessManager::slotUpProcFinished(){
184  if(upProc->exitStatus() != QProcess::NormalExit){
185    QString msg = upProc->readAllStandardError();
186    if(msg.isEmpty()){ msg = upProc->readAllStandardOutput(); }
187    if(msg.isEmpty()){ msg = tr("Unknown Error"); }
188    qDebug() << "Update Process Error:"<<msg;
189    emit ProcessError(UPDATE, msg);
190  }else{
191    qDebug() << "Update Process Finished";
192    emit ProcessFinished(UPDATE);         
193  }
194}
195
196// == REMOVE PROCESS ==
197void ProcessManager::slotRemProcMessage(){
198  QString msg = remProc->readAllStandardOutput();
199  emit ProcessMessage(REMOVE,msg);
200}
201
202void ProcessManager::slotRemProcFinished(){
203  if(remProc->exitStatus() != QProcess::NormalExit){
204    QString msg = remProc->readAllStandardError();
205    if(msg.isEmpty()){ msg = remProc->readAllStandardOutput(); }
206    if(msg.isEmpty()){ msg = tr("Unknown Error"); }
207    qDebug() << "Removal Process Error:"<<msg;
208    emit ProcessError(REMOVE, msg);
209  }else{
210    qDebug() << "Removal Process Finished";
211    emit ProcessFinished(REMOVE);         
212  }
213}
214
215// == DOWNLOAD PROCESS ==
216void ProcessManager::slotDlProcMessage(){
217  while( dlProc->canReadLine() ){
218    QString line = dlProc->readLine().simplified();
219    QString dl = parseDlLine(line);
220    if(!dl.isEmpty()){ emit ProcessMessage(DOWNLOAD,dl); }
221  }
222}
223
224void ProcessManager::slotDlProcFinished(){
225  if(dlProc->exitStatus() != QProcess::NormalExit){
226    QString msg = dlProc->readAllStandardError();
227    if(msg.isEmpty()){ msg = dlProc->readAllStandardOutput(); }
228    if(msg.isEmpty()){ msg = tr("Unknown Error"); }
229    qDebug() << "Download Process Error:"<<msg;
230    emit ProcessError(DOWNLOAD, msg);
231  }else{
232    qDebug() << "Download Process Finished";
233    emit ProcessFinished(DOWNLOAD);       
234  }
235}
236
237// == INSTALL PROCESS ==
238void ProcessManager::slotInProcMessage(){
239  QString msg = inProc->readAllStandardOutput();
240  emit ProcessMessage(INSTALL,msg);
241}
242
243void ProcessManager::slotInProcFinished(){
244  if(inProc->exitStatus() != QProcess::NormalExit){
245    QString msg = inProc->readAllStandardError();
246    if(msg.isEmpty()){ msg = inProc->readAllStandardOutput(); }
247    if(msg.isEmpty()){ msg = tr("Unknown Error"); }
248    qDebug() << "Install Process Error:"<<msg;
249    emit ProcessError(INSTALL, msg);
250  }else{
251    qDebug() << "Install Process Finished";
252    emit ProcessFinished(INSTALL);       
253  }
254}
255
256// == OTHER PROCESS ==
257void ProcessManager::slotOtProcMessage(){
258  QString msg = otProc->readAllStandardOutput();
259  emit ProcessMessage(OTHER,msg);
260}
261
262void ProcessManager::slotOtProcFinished(){
263  if(otProc->exitStatus() != QProcess::NormalExit){
264    QString msg = otProc->readAllStandardError();
265    if(msg.isEmpty()){ msg = otProc->readAllStandardOutput(); }
266    if(msg.isEmpty()){ msg = tr("Unknown Error"); }
267    qDebug() << "Other Process Error:"<<msg;
268    emit ProcessError(OTHER, msg);
269  }else{
270    qDebug() << "Other Process Finished";
271    emit ProcessFinished(OTHER);         
272  }
273}
274
Note: See TracBrowser for help on using the repository browser.