//=========================================== // Lumina-DE source code // Copyright (c) 2013-2016, Ken Moore // Available under the 3-clause BSD license // See the LICENSE file for full details //=========================================== #include "LUtils.h" #include "LuminaOS.h" #include "LuminaXDG.h" #include #include #include inline QStringList ProcessRun(QString cmd, QStringList args){ //Assemble outputs QStringList out; out << "1" << ""; //error code, string output QProcess proc; QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); env.insert("LANG", "C"); env.insert("LC_MESSAGES", "C"); proc.setProcessEnvironment(env); proc.setProcessChannelMode(QProcess::MergedChannels); if(args.isEmpty()){ proc.start(cmd, QIODevice::ReadOnly); }else{ proc.start(cmd,args ,QIODevice::ReadOnly); } QString info; while(!proc.waitForFinished(1000)){ if(proc.state() == QProcess::NotRunning){ break; } //somehow missed the finished signal QString tmp = proc.readAllStandardOutput(); if(tmp.isEmpty()){ proc.terminate(); } else{ info.append(tmp); } } out[0] = QString::number(proc.exitCode()); out[1] = info+QString(proc.readAllStandardOutput()); return out; } //============= // LUtils Functions //============= int LUtils::runCmd(QString cmd, QStringList args){ /*QProcess proc; proc.setProcessChannelMode(QProcess::MergedChannels); if(args.isEmpty()){ proc.start(cmd); }else{ proc.start(cmd, args); } //if(!proc.waitForStarted(30000)){ return 1; } //process never started - max wait of 30 seconds while(!proc.waitForFinished(300)){ if(proc.state() == QProcess::NotRunning){ break; } //somehow missed the finished signal QCoreApplication::processEvents(); } int ret = proc.exitCode(); return ret;*/ QFuture future = QtConcurrent::run(ProcessRun, cmd, args); return future.result()[0].toInt(); //turn it back into an integer return code } QStringList LUtils::getCmdOutput(QString cmd, QStringList args){ /*QProcess proc; QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); env.insert("LANG", "C"); env.insert("LC_MESSAGES", "C"); proc.setProcessEnvironment(env); proc.setProcessChannelMode(QProcess::MergedChannels); if(args.isEmpty()){ proc.start(cmd); }else{ proc.start(cmd,args); } //if(!proc.waitForStarted(30000)){ return QStringList(); } //process never started - max wait of 30 seconds while(!proc.waitForFinished(300)){ if(proc.state() == QProcess::NotRunning){ break; } //somehow missed the finished signal QCoreApplication::processEvents(); } QStringList out = QString(proc.readAllStandardOutput()).split("\n"); return out;*/ QFuture future = QtConcurrent::run(ProcessRun, cmd, args); return future.result()[1].split("\n"); //Split the return message into lines } QStringList LUtils::readFile(QString filepath){ QStringList out; QFile file(filepath); if(file.open(QIODevice::Text | QIODevice::ReadOnly)){ QTextStream in(&file); while(!in.atEnd()){ out << in.readLine(); } file.close(); } return out; } bool LUtils::writeFile(QString filepath, QStringList contents, bool overwrite){ QFile file(filepath); if(file.exists() && !overwrite){ return false; } bool ok = false; if(contents.isEmpty()){ contents << "\n"; } if( file.open(QIODevice::WriteOnly | QIODevice::Truncate) ){ QTextStream out(&file); out << contents.join("\n"); if(!contents.last().isEmpty()){ out << "\n"; } //always end with a new line file.close(); ok = true; } return ok; } bool LUtils::isValidBinary(QString& bin){ if(!bin.startsWith("/")){ //Relative path: search for it on the current "PATH" settings QStringList paths = QString(qgetenv("PATH")).split(":"); for(int i=0; i fmt = QImageReader::supportedImageFormats(); for(int i=0; iremoveTranslator(cTrans); } //Setup the translator cTrans = new QTranslator(); //Use the shortened locale code if specific code does not have a corresponding file if(!QFile::exists(LOS::LuminaShare()+"i18n/"+appname+"_" + langCode + ".qm") && langCode!="en_US" ){ langCode.truncate( langCode.indexOf("_") ); } QString filename = appname+"_"+langCode+".qm"; //qDebug() << "FileName:" << filename << "Dir:" << LOS::LuminaShare()+"i18n/"; if( cTrans->load( filename, LOS::LuminaShare()+"i18n/" ) ){ app->installTranslator( cTrans ); }else{ //Translator could not be loaded for some reason cTrans = 0; if(langCode!="en_US"){ qWarning() << " - Could not load Locale:" << langCode; } } }else{ //Only going to set the encoding since no application given qDebug() << "Loading System Encoding:" << langEnc; } //Load current encoding for this locale QTextCodec::setCodecForLocale( QTextCodec::codecForName(langEnc.toUtf8()) ); return cTrans; } QStringList LUtils::knownLocales(){ QDir i18n = QDir(LOS::LuminaShare()+"i18n"); if( !i18n.exists() ){ return QStringList(); } QStringList files = i18n.entryList(QStringList() << "lumina-desktop_*.qm", QDir::Files, QDir::Name); if(files.isEmpty()){ return QStringList(); } //Now strip off the filename and just leave the locale tag for(int i=0; i=1000 && c=100){ //No decimel places num = QString::number(qRound(bytes)); }else if(bytes>=10){ //need 1 decimel place num = QString::number( (qRound(bytes*10)/10.0) ); }else if(bytes>=1){ //need 2 decimel places num = QString::number( (qRound(bytes*100)/100.0) ); }else{ //Fully decimel (3 places) num = "0."+QString::number(qRound(bytes*1000)); } //qDebug() << "Bytes to Human-readable:" << bytes << c << num << labs[c]; return (num+labs[c]); } QString LUtils::SecondsToDisplay(int secs){ if(secs < 0){ return "??"; } QString rem; //remaining if(secs > 3600){ int hours = secs/3600; rem.append( QString::number(hours)+"h "); secs = secs - (hours*3600); } if(secs > 60){ int min = secs/60; rem.append( QString::number(min)+"m "); secs = secs - (min*60); } if(secs > 0){ rem.append( QString::number(secs)+"s"); }else{ rem.append( "0s" ); } return rem; }