//=========================================== // 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 //============= //Return the path to one of the XDG standard directories QString LUtils::standardDirectory(StandardDir dir, bool createAsNeeded){ // enum StandardDir {Desktop, Documents, Downloads, Music, Pictures, PublicShare, Templates, Videos} QString var="XDG_%1_DIR"; QString defval="$HOME"; QString val; switch (dir){ case Desktop: var = var.arg("DESKTOP"); defval.append("/Desktop"); break; case Documents: var = var.arg("DOCUMENTS"); defval.append("/Documents"); break; case Downloads: var = var.arg("DOWNLOAD"); defval.append("/Downloads"); break; case Music: var = var.arg("MUSIC"); defval.append("/Music"); break; case Pictures: var = var.arg("PICTURES"); defval.append("/Pictures"); break; case PublicShare: var = var.arg("PUBLICSHARE"); break; case Templates: var = var.arg("TEMPLATES"); break; case Videos: var = var.arg("VIDEOS"); defval.append("/Videos"); break; } //Read the XDG user dirs file (if it exists) QString configdir = getenv("XDG_DATA_HOME"); if(configdir.isEmpty()){ configdir = QDir::homePath()+"/.config"; } QString conffile=configdir+"/user-dirs.dirs"; if(QFile::exists(conffile)){ static QStringList _contents; static QDateTime _lastread; if(_contents.isEmpty() || _lastread < QFileInfo(conffile).lastModified()){ _contents = LUtils::readFile(conffile); _lastread = QDateTime::currentDateTime(); } QStringList match = _contents.filter(var+"="); if(!match.isEmpty()){ val = match.first().section("=",-1).simplified(); if(val.startsWith("\"")){ val = val.remove(0,1); } if(val.endsWith("\"")){ val.chop(1); } } } //Now check the value and return it if(val.isEmpty()){ val = defval; }; //use the default value val = val.replace("$HOME", QDir::homePath()); if(createAsNeeded && !QFile::exists(val) ){ QDir dir; dir.mkpath(val); } return val; } QString LUtils::runCommand(bool &success, QString command, QStringList arguments, QString workdir, QStringList env){ QProcess proc; proc.setProcessChannelMode(QProcess::MergedChannels); //need output //First setup the process environment as necessary QProcessEnvironment PE = QProcessEnvironment::systemEnvironment(); if(!env.isEmpty()){ for(int i=0; i 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){ //Trim off any quotes if(bin.startsWith("\"") && bin.endsWith("\"")){ bin.chop(1); bin = bin.remove(0,1); } if(bin.startsWith("\'") && bin.endsWith("\'")){ bin.chop(1); bin = bin.remove(0,1); } //Now look for relative/absolute path 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; }