//=========================================== // Lumina-DE source code // Copyright (c) 2013-2015, Ken Moore // Available under the 3-clause BSD license // See the LICENSE file for full details //=========================================== #include "LuminaUtils.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include static QStringList fav; 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); }else{ proc.start(cmd,args); } while(!proc.waitForFinished(500)){ if(proc.state() == QProcess::NotRunning){ break; } //somehow missed the finished signal } out[0] = QString::number(proc.exitCode()); out[1] = QString(proc.readAllStandardOutput()); return out; } //============= // LUtils Functions //============= QString LUtils::LuminaDesktopVersion(){ return "0.8.8-devel"; } 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("_") ); } if( cTrans->load( appname+QString("_") + langCode, 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)); } 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; } //Various function for finding valid QtQuick plugins on the system bool LUtils::validQuickPlugin(QString ID){ return ( !LUtils::findQuickPluginFile(ID).isEmpty() ); } QString LUtils::findQuickPluginFile(QString ID){ if(ID.startsWith("quick-")){ ID = ID.section("-",1,50); } //just in case //Give preference to any user-supplied plugins (overwrites for system plugins) QString path = QDir::homePath()+"/.lumina/quickplugins/quick-"+ID+".qml"; if( QFile::exists(path) ){return path; } path = LOS::LuminaShare()+"quickplugins/quick-"+ID+".qml"; if( QFile::exists(path) ){return path; } return ""; //could not be found } QStringList LUtils::listQuickPlugins(){ QDir dir(QDir::homePath()+"/.lumina/quickplugins"); QStringList files = dir.entryList(QStringList() << "quick-*.qml", QDir::Files | QDir::NoDotAndDotDot, QDir::Name); dir.cd(LOS::LuminaShare()+"quickplugins"); files << dir.entryList(QStringList() << "quick-*.qml", QDir::Files | QDir::NoDotAndDotDot, QDir::Name); for(int i=0; i, sym-links in the ~/.lumina/favorites dir} //Include 0.8.4-devel versions in this upgrade (need to distinguish b/w devel and release versions later somehow) QDir favdir(QDir::homePath()+"/.lumina/favorites"); QFileInfoList symlinks = favdir.entryInfoList(QDir::Files | QDir::Dirs | QDir::System | QDir::NoDotAndDotDot); QStringList favfile = LUtils::listFavorites(); //just in case some already exist bool newentry = false; for(int i=0; iscreenCount(); i++){ if(desk->screenGeometry(i).x()==0){ screen = QString::number(i); screenGeom = desk->screenGeometry(i); break; } } //Now setup the default "desktopsettings.conf" and "sessionsettings.conf" files QStringList deskset, sesset, lopenset; // -- SESSION SETTINGS -- QStringList tmp = sysDefaults.filter("session_"); if(tmp.isEmpty()){ tmp = sysDefaults.filter("session."); }//for backwards compat sesset << "[General]"; //everything is in this section sesset << "DesktopVersion="+LUtils::LuminaDesktopVersion(); for(int i=0; isetContentsMargins(1,1,1,1); resizeSide = NONE; cAct = new QWidgetAction(this); contents = 0; connect(this, SIGNAL(aboutToShow()), this, SLOT(clearFlags()) ); connect(this, SIGNAL(aboutToHide()), this, SLOT(clearFlags()) ); } ResizeMenu::~ResizeMenu(){ } void ResizeMenu::setContents(QWidget *con){ this->clear(); cAct->setDefaultWidget(con); this->addAction(cAct); contents = con; //save for later contents->setCursor(Qt::ArrowCursor); } void ResizeMenu::mouseMoveEvent(QMouseEvent *ev){ QRect geom = this->geometry(); //Note: The exact position does not matter as much as the size // since the window will be moved again the next time it is shown // The "-2" in the sizing below accounts for the menu margins switch(resizeSide){ case TOP: geom.setTop(this->mapToGlobal(ev->pos()).y()); this->setGeometry(geom); if(contents!=0){ contents->setFixedSize(QSize(geom.width()-2, geom.height()-2)); } break; case BOTTOM: geom.setBottom( this->mapToGlobal(ev->pos()).y()); this->setGeometry(geom); if(contents!=0){ contents->setFixedSize(QSize(geom.width()-2, geom.height()-2)); } break; case LEFT: geom.setLeft(this->mapToGlobal(ev->pos()).x()); this->setGeometry(geom); if(contents!=0){ contents->setFixedSize(QSize(geom.width()-2, geom.height()-2)); } break; case RIGHT: geom.setRight(this->mapToGlobal(ev->pos()).x()); this->setGeometry(geom); if(contents!=0){ contents->setFixedSize(QSize(geom.width()-2, geom.height()-2)); } break; default: //NONE qDebug() << " - Mouse At:" << ev->pos(); //Just adjust the mouse cursor which is shown if(ev->pos().x()<=1 && ev->pos().x() >= -1){ this->setCursor(Qt::SizeHorCursor); } else if(ev->pos().x() >= this->width()-1 && ev->pos().x() <= this->width()+1){ this->setCursor(Qt::SizeHorCursor); } else if(ev->pos().y()<=1 && ev->pos().y() >= -1){ this->setCursor(Qt::SizeVerCursor); } else if(ev->pos().y() >= this->height()-1 && ev->pos().y() <= this->height()+1){ this->setCursor(Qt::SizeVerCursor); } else{ this->setCursor(Qt::ArrowCursor); } } QMenu::mouseMoveEvent(ev); //do normal processing as well } void ResizeMenu::mousePressEvent(QMouseEvent *ev){ bool used = false; if(ev->buttons().testFlag(Qt::LeftButton) && resizeSide==NONE){ //qDebug() << "Mouse Press Event:" << ev->pos() << resizeSide; if(ev->pos().x()<=1 && ev->pos().x() >= -1){resizeSide = LEFT; used = true;} else if(ev->pos().x() >= this->width()-1 && ev->pos().x() <= this->width()+1){ resizeSide = RIGHT; used = true;} else if(ev->pos().y()<=1 && ev->pos().y() >= -1){ resizeSide = TOP; used = true; } else if(ev->pos().y() >= this->height()-1 && ev->pos().y() <= this->height()+1){ resizeSide = BOTTOM; used = true; } } if(used){ ev->accept(); } else{ QMenu::mousePressEvent(ev); } //do normal processing } void ResizeMenu::mouseReleaseEvent(QMouseEvent *ev){ if(ev->button() == Qt::LeftButton && resizeSide!=NONE ){ //qDebug() << "Mouse Release Event:" << ev->pos() << resizeSide; resizeSide = NONE; emit MenuResized(contents->size()); ev->accept(); }else{ QMenu::mouseReleaseEvent(ev); //do normal processing } }