summaryrefslogblamecommitdiffstats
path: root/src/main.cpp
blob: 44efa473c50349a2a291fa06c58b8eb16d7fe53d (plain) (tree)
1
2
3
4
5
6
7
8
                       
                    





                   





                                                            



                                                                                                          

                       



                                
                                                          
                                                 
                                         
                              
                                                 




                                                    

                              
                               
                      
                                            


                                              
                                                      











                                                                       

                                                  
                     

                                                     
                     

                                                   
         





                                                                       



                                            


                                                                                               
 





                                                                
        
                                          
                                     
                                                          

                                                                     
                                                                                                
     
        
                                              
                                                                      
                   
                    
                                               
               
                      
 
#include <QApplication>
#include <QSettings>
#include <QtCore>
#include <getopt.h>
#include <cstdlib>
#include <iostream>
#include "fbgui.h"

void printHelp()
{
    // Prints usage information.
    QTextStream qout(stdout);
    qout << QObject::tr("Usage: ./fbgui [OPTIONS]") << endl;
    qout << QObject::tr("Options:") << endl;
    qout << "-u <URL>, --url=<URL>           " << QObject::tr("Set which URL to load.") << endl;
    qout << "-d <dir>, --downloaddir <dir>   " << QObject::tr("Specifiy the download directory.") << endl;
    qout << "-D, --debug                     " << QObject::tr("Activate debug mode.") << endl;
    qout << "-h, --help                      " << QObject::tr("Prints usage information.") << endl;
    qout.flush();
    exit(EXIT_SUCCESS);
}

int main(int argc, char *argv[])
{
    QApplication app(argc, argv, QApplication::GuiServer);
    app.setOrganizationName("team_projekt_2011");
    app.setApplicationName("prebootGUI");
    app.setObjectName("test");
    binPath = QApplication::applicationDirPath();

    // Translator, for later (maybe).
    QTranslator translator;
    translator.load(":" + QLocale::system().name());
    app.installTranslator(&translator);

    /* Parse cmdline argus. */
    QMap<QString, QString> clo;
    int longIndex = 0;
    static const char *optString = "u:hDd:";
    static const struct option longOpts[] =
    {
    	{"url", required_argument, NULL, 'u'},
    	{"downloaddir", required_argument, NULL, 'd'},
    	{"debug", no_argument, NULL, 'D'},
	{"help", no_argument, NULL, 'h'}    
    };
    int opt = getopt_long(argc, argv, optString, longOpts, &longIndex);
    while (opt != -1)
    {
    	switch(opt)
        {
            case 'u':
                clo.insert("url", optarg);
                break;
            case 'd':
            	clo.insert("downloadDir", optarg);
            	break;
	    case 'D':
	    		clo.insert("debug", "debug");
	    		break;
	    case 'h':
	    		clo.insert("help", "help");
	    		break;
	}
	opt = getopt_long(argc, argv, optString, longOpts, &longIndex);
    }
    // Print help
    if (clo.contains("help"))
        printHelp();    
    // Debug mode
    if (clo.contains("debug")){
    	debug = true;
    	qDebug() << "Debug mode activated.";
    }
    // Read the config file, for now hardcoded expected name.
    QSettings confFileSettings(app.applicationDirPath() + "/fbgui.conf", QSettings::IniFormat);
    confFileSettings.setIniCodec("UTF-8");

    if (clo.contains("url"))
    	baseURL = QUrl(clo.value("url"));
    else if (confFileSettings.contains("default/url"))
	baseURL = confFileSettings.value("default/url").toUrl();
    else
	baseURL = DEFAULT_URL;
	
    // Setting target downloads directory.
    if (clo.contains("downloadDir")){
    	downloadPath = binPath + clo.value("downloadDir");
    }
    else if (confFileSettings.contains("default/downloadDirectory")){
    	downloadPath = binPath + confFileSettings.value("default/downloadDirectory").toString();
    }
    else
    	downloadPath = binPath + "/downloads";
	if (debug) qDebug() << "Downloads saved in: " << downloadPath;
    // Start fbgui.
    fbgui gui(&app);
    gui.setAttribute(Qt::WA_QuitOnClose, true);
    gui.show();
    return app.exec();
}