source: src-qt4/PCDM/src/main.cpp @ 1234f7c4

9.2-releasereleng/10.0releng/10.0.1releng/10.0.2releng/10.0.3
Last change on this file since 1234f7c4 was 1234f7c4, checked in by Ken Moore <ken@…>, 11 months ago

Turn off the PCDM debug mode

  • Property mode set to 100644
File size: 7.0 KB
Line 
1/* PCDM Login Manager:
2*  Written by Ken Moore (ken@pcbsd.org) 2012/2013
3*  Copyright(c) 2013 by the PC-BSD Project
4*  Available under the 3-clause BSD license
5*/
6
7#include <QtGui/QApplication>
8#include <QTranslator>
9#include <QLocale>
10#include <QDesktopWidget>
11#include <QFile>
12#include <QSplashScreen>
13#include <QTime>
14#include <QDebug>
15#include <QX11Info>
16
17#include <sys/types.h>
18#include <sys/wait.h>
19#include <unistd.h>
20
21#include "pcdm-gui.h"
22#include "pcdm-backend.h"
23#include "pcdm-config.h"
24#include "pcdm-xprocess.h"
25
26#define TMPLANGFILE QString("/tmp/.PCDMLang")
27#define TMPAUTOLOGINFILE QString("/tmp/.PCDMAutoLogin")
28#define TMPAUTHFILE QString("/tmp/.PCDMAuth")
29//Make sure that prefix is set
30//#ifndef prefix
31//#define prefix "/usr/local/"
32//#endif
33
34bool USECLIBS=false;
35
36int runSingleSession(int argc, char *argv[]){
37  //QTime clock;
38  //clock.start();
39  Backend::checkLocalDirs();  // Create and fill "/usr/local/share/PCDM" if needed
40  Backend::openLogFile("/var/log/PCDM.log"); 
41  //qDebug() << "Backend Checks Finished:" << QString::number(clock.elapsed())+" ms";
42  //Check for the flag to try and auto-login
43  bool ALtriggered = FALSE;
44  if(QFile::exists(TMPAUTOLOGINFILE)){ ALtriggered=TRUE; QFile::remove(TMPAUTOLOGINFILE); }
45 
46  QString changeLang; 
47  // Load the configuration file
48  QString confFile = "/usr/local/etc/pcdm.conf";
49  if(!QFile::exists(confFile)){ 
50    qDebug() << "PCDM: Configuration file missing:"<<confFile<<"\n  - Using default configuration";
51    confFile = ":samples/pcdm.conf"; 
52  }
53 
54  Config::loadConfigFile(confFile);
55  //qDebug() << "Config File Loaded:" << QString::number(clock.elapsed())+" ms";
56  // Startup the main application
57  QApplication a(argc,argv); 
58 
59  // Show our splash screen, so the user doesn't freak that that it takes a few seconds to show up
60  QSplashScreen splash;
61  if(!Config::splashscreen().isEmpty()){
62    splash.setPixmap( QPixmap(Config::splashscreen()) ); //load the splashscreen file
63  }
64  splash.show();
65  QCoreApplication::processEvents(); //Process the splash screen event immediately
66  //qDebug() << "SplashScreen Started:" << QString::number(clock.elapsed())+" ms";
67  //Initialize the xprocess
68  XProcess desktop;
69 
70  //*** STARTUP THE PROGRAM ***
71  bool goodAL = FALSE; //Flag for whether the autologin was successful
72  // Start the autologin procedure if applicable
73  if( ALtriggered && Config::useAutoLogin() ){
74    //Setup the Auto Login
75    QString user = Backend::getALUsername();
76    QString pwd = Backend::getALPassword();
77    QString dsk = Backend::getLastDE(user);
78    if( user.isEmpty() || dsk.isEmpty() ){
79         goodAL=FALSE;   
80    }else{
81        desktop.loginToXSession(user,pwd, dsk);
82        splash.close();
83        if(desktop.isRunning()){
84          goodAL=TRUE; //flag this as a good login to skip the GUI
85        }
86    }
87  }
88  //qDebug() << "AutoLogin Finished:" << QString::number(clock.elapsed())+" ms";
89  if(!goodAL){
90    // ------START THE PCDM GUI-------
91   
92    // Check what directory our app is in
93    QString appDir = "/usr/local/share/PCDM";
94    // Load the translator
95    QTranslator translator;
96    QLocale mylocale;
97    QString langCode = mylocale.name();
98    //Check for a language change detected
99    if ( ! changeLang.isEmpty() )       
100       langCode = changeLang;
101    //Load the proper locale for the translator
102    if ( QFile::exists(appDir + "/i18n/PCDM_" + langCode + ".qm" ) ) {
103      translator.load( QString("PCDM_") + langCode, appDir + "/i18n/" );
104      a.installTranslator(&translator);
105      Backend::log("Loaded Translation:" + appDir + "/i18n/PCDM_" + langCode + ".qm");
106    } else {
107      Backend::log("Could not find: " + appDir + "/i18n/PCDM_" + langCode + ".qm");
108      langCode = "";
109    }
110    //qDebug() << "Translation Finished:" << QString::number(clock.elapsed())+" ms";
111
112    Backend::log("Starting up PCDM interface");
113    PCDMgui w;
114    //qDebug() << "Main GUI Created:" << QString::number(clock.elapsed())+" ms";
115    splash.finish(&w); //close the splash when the GUI starts up
116
117    // Set full-screen dimensions
118    QRect dimensions = QApplication::desktop()->screenGeometry();
119    int wid = dimensions.width();     // returns desktop width
120    int hig = dimensions.height();    // returns desktop height
121    w.setGeometry(0, 0, wid, hig);
122
123    //Set the proper size on the Application
124    w.setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowStaysOnBottomHint);
125    w.setWindowState(Qt::WindowMaximized); //Qt::WindowFullScreen);
126
127    //Setup the signals/slots to startup the desktop session
128    if(USECLIBS){ QObject::connect( &w,SIGNAL(xLoginAttempt(QString,QString,QString)), &desktop,SLOT(setupDesktop(QString,QString,QString))); }
129    else{ QObject::connect( &w,SIGNAL(xLoginAttempt(QString,QString,QString)), &desktop,SLOT(loginToXSession(QString,QString,QString)) ); }
130    //Setup the signals/slots for return information for the GUI
131    QObject::connect( &desktop, SIGNAL(InvalidLogin()), &w, SLOT(slotLoginFailure()) );
132    QObject::connect( &desktop, SIGNAL(started()), &w, SLOT(slotLoginSuccess()) );
133    QObject::connect( &desktop, SIGNAL(ValidLogin()), &w, SLOT(slotLoginSuccess()) );
134   
135    //qDebug() << "Showing GUI:" << QString::number(clock.elapsed())+" ms";
136    w.show();
137    a.exec();
138  }  // end of PCDM GUI running
139  int retcode = 0;
140  //Wait for the desktop session to finish before exiting
141  if(USECLIBS){ desktop.startDesktop(); }
142  else{ desktop.waitForSessionClosed(); }
143  splash.show(); //show the splash screen again
144  splash.showMessage(QObject::tr("System Shutting Down"), Qt::AlignHCenter | Qt::AlignBottom, Qt::white);
145  //check for shutdown process
146  if(QFile::exists("/var/run/nologin")){
147    //Pause for a few seconds to prevent starting a new session during a shutdown
148    QTime wTime = QTime::currentTime().addSecs(30);
149    while( QTime::currentTime() < wTime ){ 
150      //Keep processing events during the wait (for splashscreen)
151      QCoreApplication::processEvents(QEventLoop::AllEvents, 100); 
152    }
153    //set the return code for a shutdown
154    retcode = -1; //make sure it does not start a new session
155  }
156 
157  //Clean up Code
158  delete &desktop;
159  delete &a;
160  delete &splash;
161 
162 
163  return retcode;
164}
165
166int main(int argc, char *argv[])
167{
168 bool neverquit = TRUE;
169 bool runonce = FALSE;
170 if(argc==2){ if( QString(argv[1]) == "--once"){ runonce = TRUE; } }
171 
172 while(neverquit){
173  if(runonce){ neverquit = FALSE; }
174  qDebug() << " -- PCDM Session Starting...";
175  int sid = -1;
176  int pid = fork();
177  if(pid < 0){
178    qDebug() << "Error: Could not fork the PCDM session";
179    return -1;
180  }else if( pid ==0 ){
181    //New Child Process
182    sid = setsid(); //start a session
183    qDebug() << "-- Session ID:" << sid;
184    int retCode = runSingleSession(argc,argv);
185    qDebug() << "-- PCDM Session Ended --";
186    //check for special exit code
187    if(retCode != 0){ neverquit=FALSE; }
188    //Now kill the shild process (whole session)
189    qDebug() << "Exiting child process";
190    exit(3);
191  }else{ 
192    //Parent (calling) process
193    int status;
194    sleep(2);
195    waitpid(sid,&status,0); //wait for the child (session) to finish
196  }
197  qDebug() << "-- PCDM Session Ended --";
198  if(QFile::exists("/var/run/nologin")){ neverquit = FALSE; } 
199 }
200 return 0;
201}
Note: See TracBrowser for help on using the repository browser.