<commit_before>//===========================================
//  Lumina-DE source code
//  Copyright (c) 2012, <NAME>
//  Available under the 3-clause BSD license
//  See the LICENSE file for full details
//===========================================

#include <QApplication>
#include <QX11Info>
#include <QProcess>
#include <QProcessEnvironment>
#include <QFile>
#include <QFileInfo>
#include <QString>
#include <QUrl>
#include <QDebug>
#include <QTranslator>
#include <QLocale>
#include <QMessageBox>
#include <QSplashScreen>
#include <QDateTime>
#include <QPixmap>
#include <QColor>
#include <QFont>
#include <QTextCodec>

#include "LFileDialog.h"

#include <LuminaXDG.h>
#include <LuminaOS.h>

#ifndef PREFIX
#define PREFIX QString("/usr/local")
#endif

static QApplication *App = 0;

void printUsageInfo(){
  qDebug() << "lumina-open: Application launcher for the Lumina Desktop Environment";
  qDebug() << "Description: Given a file (with absolute path) or URL, this utility will try to find the appropriate application with which to open the file. If the file is a *.desktop application shortcut, it will just start the application appropriately. It can also perform a few specific system operations if given special flags."; 	
  qDebug() << "Usage: lumina-open [-select] <absolute file path or URL>";
  qDebug() << "           lumina-open [-volumeup, -volumedown, -brightnessup, -brightnessdown]";
  qDebug() << "  [-select] (optional) flag to bypass any default application settings and show the application selector window";
  qDebug() << "Special Flags:";
  qDebug() << " \"-volume[up/down]\" Flag to increase/decrease audio volume by 5%";
  qDebug() << " \"-brightness[up/down]\" Flag to increase/decrease screen brightness by 5%";
  exit(1);	
}

void setupApplication(int argc, char **argv){
  App = new QApplication(argc, argv);
    QTranslator translator;
    QLocale mylocale;
    QString langCode = mylocale.name();
    
    if(!QFile::exists(PREFIX + "/share/Lumina-DE/i18n/lumina-open_" + langCode + ".qm") ){ 
      langCode.truncate( langCode.indexOf("_") ); 
    }
    translator.load( QString("lumina-open_") + langCode, PREFIX + "/share/Lumina-DE/i18n/" );
    App->installTranslator( &translator );
    qDebug() << "Locale:" << langCode;
    //Load current encoding for this locale
    QTextCodec::setCodecForTr( QTextCodec::codecForLocale() ); //make sure to use the same codec
    qDebug() << "Locale Encoding:" << QTextCodec::codecForLocale()->name();
}

void showOSD(int argc, char **argv, QString message){
  setupApplication(argc, argv);
  //qDebug() << "Display OSD";
  
  QPixmap pix(":/icons/OSD.png");
  QSplashScreen splash(pix, Qt::SplashScreen | Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint);
     splash.setWindowTitle("");
     QFont myfont;
	myfont.setBold(true);
	myfont.setPixelSize(13);
	splash.setFont(myfont);
  qDebug() << "Display OSD";
  splash.show();
  //qDebug() << " - show message";
  splash.showMessage(message, Qt::AlignCenter, Qt::white);
  //qDebug() << " - loop";
  QDateTime end = QDateTime::currentDateTime().addMSecs(800);
  while(QDateTime::currentDateTime() < end){ App->processEvents(); }
  splash.hide();
}

QString cmdFromUser(int argc, char **argv, QString inFile, QString extension, QString& path, bool showDLG=false){
    //First check to see if there is a default for this extension
    QString defApp = LFileDialog::getDefaultApp(extension);
    if(extension=="directory" && defApp.isEmpty() && !showDLG){
      //Just use the Lumina File Manager
      return "lumina-fm";
    }else if( !defApp.isEmpty() && !showDLG ){
      bool ok = false;
      XDGDesktop DF = LXDG::loadDesktopFile(defApp, ok);
      if(ok){
      	QString exec = LXDG::getDesktopExec(DF);
      	if(!exec.isEmpty()){
      	  qDebug() << "[lumina-open] Using default application:" << DF.name << "File:" << inFile; 
          if(!DF.path.isEmpty()){ path = DF.path; }
          return exec;
        }
      }
      //invalid default - reset it and continue on
      LFileDialog::setDefaultApp(extension, "");
    }

    //No default set -- Start up the application selection dialog
    setupApplication(argc,argv);
    LFileDialog w;
    if(inFile.startsWith(extension)){
      //URL
      w.setFileInfo(inFile, extension, false);
    }else{
      //File
      if(inFile.endsWith("/")){ inFile.chop(1); }
      w.setFileInfo(inFile.section("/",-1), extension, true);
    }
    
    w.show();

    App->exec();
    if(!w.appSelected){ exit(1); }
    //Return the run path if appropriate
    if(!w.appPath.isEmpty()){ path = w.appPath; }
    //Just do the default application registration here for now
    //  might move it to the runtime phase later after seeing that the app has successfully started
    if(w.setDefault){ LFileDialog::setDefaultApp(extension, w.appFile); }
    else{ LFileDialog::setDefaultApp(extension, ""); }
    //Now return the resulting application command
    return w.appExec;
}

void getCMD(int argc, char ** argv, QString& binary, QString& args, QString& path){
  //Get the input file
  QString inFile;
  bool showDLG = false; //flag to bypass any default application setting
  if(argc > 1){ <commit_msg>Adjust how the QApplication is initialized in lumina-open to try and prevent a seg fault when showing the OSD on FreeBSD 10-Stable. It appears that a QApplication is a *very* special type of class and should not be initialized to a static pointer for the application.<commit_after>//===========================================
//  Lumina-DE source code
//  Copyright (c) 2012, <NAME>
//  Available under the 3-clause BSD license
//  See the LICENSE file for full details
//===========================================

#include <QApplication>
#include <QX11Info>
#include <QProcess>
#include <QProcessEnvironment>
#include <QFile>
#include <QFileInfo>
#include <QString>
#include <QUrl>
#include <QDebug>
#include <QTranslator>
#include <QLocale>
#include <QMessageBox>
#include <QSplashScreen>
#include <QDateTime>
#include <QPixmap>
#include <QColor>
#include <QFont>
#include <QTextCodec>

#include "LFileDialog.h"

#include <LuminaXDG.h>
#include <LuminaOS.h>

#ifndef PREFIX
#define PREFIX QString("/usr/local")
#endif

void printUsageInfo(){
  qDebug() << "lumina-open: Application launcher for the Lumina Desktop Environment";
  qDebug() << "Description: Given a file (with absolute path) or URL, this utility will try to find the appropriate application with which to open the file. If the file is a *.desktop application shortcut, it will just start the application appropriately. It can also perform a few specific system operations if given special flags."; 	
  qDebug() << "Usage: lumina-open [-select] <absolute file path or URL>";
  qDebug() << "           lumina-open [-volumeup, -volumedown, -brightnessup, -brightnessdown]";
  qDebug() << "  [-select] (optional) flag to bypass any default application settings and show the application selector window";
  qDebug() << "Special Flags:";
  qDebug() << " \"-volume[up/down]\" Flag to increase/decrease audio volume by 5%";
  qDebug() << " \"-brightness[up/down]\" Flag to increase/decrease screen brightness by 5%";
  exit(1);	
}

/*QApplication setupApplication(int argc, char **argv){
  QApplication App(argc, argv);
    QTranslator translator;
    QLocale mylocale;
    QString langCode = mylocale.name();
    
    if(!QFile::exists(PREFIX + "/share/Lumina-DE/i18n/lumina-open_" + langCode + ".qm") ){ 
      langCode.truncate( langCode.indexOf("_") ); 
    }
    translator.load( QString("lumina-open_") + langCode, PREFIX + "/share/Lumina-DE/i18n/" );
    App.installTranslator( &translator );
    qDebug() << "Locale:" << langCode;
    //Load current encoding for this locale
    QTextCodec::setCodecForTr( QTextCodec::codecForLocale() ); //make sure to use the same codec
    qDebug() << "Locale Encoding:" << QTextCodec::codecForLocale()->name();
    return App;
}*/

void showOSD(int argc, char **argv, QString message){
  //Setup the application
  QApplication App(argc, argv);
    QTranslator translator;
    QLocale mylocale;
    QString langCode = mylocale.name();
    
    if(!QFile::exists(PREFIX + "/share/Lumina-DE/i18n/lumina-open_" + langCode + ".qm") ){ 
      langCode.truncate( langCode.indexOf("_") ); 
    }
    translator.load( QString("lumina-open_") + langCode, PREFIX + "/share/Lumina-DE/i18n/" );
    App.installTranslator( &translator );

  //Display the OSD
  QPixmap pix(":/icons/OSD.png");
  QSplashScreen splash(pix, Qt::SplashScreen | Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint);
     splash.setWindowTitle("");
     QFont myfont;
	myfont.setBold(true);
	myfont.setPixelSize(13);
	splash.setFont(myfont);
  qDebug() << "Display OSD";
  splash.show();
  //qDebug() << " - show message";
  splash.showMessage(message, Qt::AlignCenter, Qt::white);
  //qDebug() << " - loop";
  QDateTime end = QDateTime::currentDateTime().addMSecs(800);
  while(QDateTime::currentDateTime() < end){ App.processEvents(); }
  splash.hide();
}

QString cmdFromUser(int argc, char **argv, QString inFile, QString extension, QString& path, bool showDLG=false){
    //First check to see if there is a default for this extension
    QString defApp = LFileDialog::getDefaultApp(extension);
    if(extension=="directory" && defApp.isEmpty() && !showDLG){
      //Just use the Lumina File Manager
      return "lumina-fm";
    }else if( !defApp.isEmpty() && !showDLG ){
      bool ok = false;
      XDGDesktop DF = LXDG::loadDesktopFile(defApp, ok);
      if(ok){
      	QString exec = LXDG::getDesktopExec(DF);
      	if(!exec.isEmpty()){
      	  qDebug() << "[lumina-open] Using default application:" << DF.name << "File:" << inFile; 
          if(!DF.path.isEmpty()){ path = DF.path; }
          return exec;
        }
      }
      //invalid default - reset it and continue on
      LFileDialog::setDefaultApp(extension, "");
    }

    //No default set -- Start up the application selection dialog
    QApplication App(argc, argv);
    QTranslator translator;
    QLocale mylocale;
    QString langCode = mylocale.name();
    
    if(!QFile::exists(PREFIX + "/share/Lumina-DE/i18n/lumina-open_" + langCode + ".qm") ){ 
      langCode.truncate( langCode.indexOf("_") ); 
    }
    translator.load( QString("lumina-open_") + langCode, PREFIX + "/share/Lumina-DE/i18n/" );
    App.installTranslator( &translator );
    qDebug() << "Locale:" << langCode;
    
    //Load current encoding for this locale
    QTextCodec::setCodecForTr( QTextCodec::codecForLocale() ); //make sure to use the same codec
    qDebug() << "Locale Encoding:" << QTextCodec::codecForLocale()->name();
    
    LFileDialog w;
    if(inFile.startsWith(extension)){
      //URL
      w.setFileInfo(inFile, extension, false);
    }else{
      //File
      if(inFile.endsWith("/")){ inFile.chop(1); }
      w.setFileInfo(inFile.section("/",-1), extension, true);
    }
    
    w.show();

    App.exec();
    if(!w.appSelected){ exit(1); }
    //Return the run path if appropriate
    if(!w.appPath.isEmpty()){ path = w.appPath; }
    //Just do the default application registration here for now
    //  might move it to the runtime phase later after seeing that the app has successfully started
    if(w.setDefault){ LFileDialog::setDefaultApp(extension, w.appFile); }
    else{ LFileDialog::setDefaultApp(extension, ""); }
    //Now return the resulting application command
    return w.appExec;
}

void getCMD(int argc, char ** argv, QString& binary, QString& args, QString& path){
  //Get the input file
  QString inFile;
  bool showDLG = false; //flag to bypass any default application setting
  if(argc > 1){ 
{'fraction_non_alphanumeric': 0.15440041173443128, 'fraction_numerical': 0.005232458397666838, 'mean_word_length': 3.7999176615891312, 'pattern_counts': {'":': 2, '<': 135, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5962787', 'n_tokens_mistral': 3755, 'n_tokens_neox': 3622, 'n_words': 1040}