summaryrefslogblamecommitdiffstats
path: root/src/main.cpp
blob: 8f2b6a59132ff6303a0630565accb4d6d0c406ea (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> clOpts;
    int longIndex = 0;
    static const char *optString = "u:d:c:Dh";
    static const struct option longOpts[] =
    {
    	{"url", required_argument, NULL, 'u'},
    	{"download", required_argument, NULL, 'd'},
    	{"config", required_argument, NULL, 'c'},
    	{"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':
            	clOpts.insert("url", optarg);
                break;
            case 'd':
            	clOpts.insert("downloadDir", optarg);
            	break;
            case 'c':
            	clOpts.insert("configFile", optarg);
            case 'D':
	    		clOpts.insert("debug", "debug");
	    		break;
            case 'h':
	    		clOpts.insert("help", "help");
	    		break;
	}
	opt = getopt_long(argc, argv, optString, longOpts, &longIndex);
    }
    // Print help
    if (clOpts.contains("help"))
        printHelp();    
    // Debug mode
    if (clOpts.contains("debug")){
    	debug = true;
    	qDebug() << "Debug mode activated.";
    }
    QString configFilePath;
    QFileInfo confInfo;
    if (clOpts.contains("configFile"))
    	configFilePath = clOpts.value("configFile");
    else {
    	confInfo = QFileInfo(QDir::home(),  ".fbgui.conf");
    	if (confInfo.exists())
    		configFilePath = confInfo.absoluteFilePath();
    	else {
    	    	confInfo = QFileInfo(QString("/etc/fbgui.conf"));
    	    	if (confInfo.exists())
    	    		configFilePath = QString("/etc/fbgui.conf");
    	        else
    	        	/* temporary */
    	        	configFilePath = QApplication::applicationDirPath() + "/fbgui.conf";
    	}
    }
    if (debug) qDebug() << "Config file is: " << configFilePath;

    // Read the config file, for now hardcoded expected name.
    QSettings confFileSettings(configFilePath, QSettings::IniFormat);
    confFileSettings.setIniCodec("UTF-8");

    if (clOpts.contains("url")) {
    	baseURL = QUrl(clOpts.value("url"));
    	if (debug) qDebug() << "URL loaded from cmdline.";
    }
    else if (confFileSettings.contains("default/url")) {
    	baseURL = confFileSettings.value("default/url").toUrl();
    	if (debug) qDebug() << "URL loaded from config file.";
    }
    else {
    	baseURL = DEFAULT_URL;
    	if (debug) qDebug() << "URL set by default.";
    }
    if (debug) qDebug() << "Base URL: " << baseURL.toString();
	
    // Setting target downloads directory.
    if (clOpts.contains("downloadDir")){
    	downloadPath = clOpts.value("downloadDir");
    	if (debug) qDebug() << "Download directory loaded from cmdline.";
    }
    else if (confFileSettings.contains("default/downloadDirectory")){
    	downloadPath = confFileSettings.value("default/downloadDirectory").toString();
    	if (debug) qDebug() << "Download directory loaded from config file.";
    }
    else
    {
    	downloadPath = DEFAULT_DOWNLOAD_DIR; // Default download dir.
    	if (debug) qDebug() << "Download directory set by default.";
    }
	if (debug) qDebug() << "Download directory: " << downloadPath;

	if (confFileSettings.contains("default/updateInterval")){
		updateInterval = confFileSettings.value("default/updateInterval").toInt();
		if (debug) qDebug() << "Read updateInterval from confFile: " << updateInterval;
	}

    fbgui gui;
    return app.exec();
}