source: src-qt4/PCDM/src/main.cpp @ 999faef

9.2-releasereleng/10.0releng/10.0.1
Last change on this file since 999faef was 999faef, checked in by Ken Moore <ken@…>, 6 months ago

Remove the old PCDM-xloop script (no longer needed), and also clean up the testing functions for using pure C classes to start up the user session (the Qt session startup is working fine)

  • Property mode set to 100644
File size: 7.2 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
34//bool 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    QFile::copy(":samples/pcdm.conf", confFile);
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{
130            QObject::connect( &w,SIGNAL(xLoginAttempt(QString,QString,QString)), &desktop,SLOT(loginToXSession(QString,QString,QString)) ); 
131    //}
132    //Setup the signals/slots for return information for the GUI
133    QObject::connect( &desktop, SIGNAL(InvalidLogin()), &w, SLOT(slotLoginFailure()) );
134    QObject::connect( &desktop, SIGNAL(started()), &w, SLOT(slotLoginSuccess()) );
135    QObject::connect( &desktop, SIGNAL(ValidLogin()), &w, SLOT(slotLoginSuccess()) );
136   
137    //qDebug() << "Showing GUI:" << QString::number(clock.elapsed())+" ms";
138    w.show();
139    a.exec();
140  }  // end of PCDM GUI running
141  int retcode = 0;
142  //Wait for the desktop session to finish before exiting
143  //if(USECLIBS){ desktop.startDesktop(); }
144  //else{
145    desktop.waitForSessionClosed(); 
146  //}
147  splash.show(); //show the splash screen again
148  QCoreApplication::processEvents();
149  //check for shutdown process
150  if(QFile::exists("/var/run/nologin")){
151    splash.showMessage(QObject::tr("System Shutting Down"), Qt::AlignHCenter | Qt::AlignBottom, Qt::white);
152    QCoreApplication::processEvents();
153    //Pause for a few seconds to prevent starting a new session during a shutdown
154    QTime wTime = QTime::currentTime().addSecs(30);
155    while( QTime::currentTime() < wTime ){ 
156      //Keep processing events during the wait (for splashscreen)
157      QCoreApplication::processEvents(QEventLoop::AllEvents, 100); 
158    }
159    //set the return code for a shutdown
160    retcode = -1; //make sure it does not start a new session
161  }
162 
163  //Clean up Code
164  delete &desktop;
165  delete &a;
166  delete &splash;
167 
168 
169  return retcode;
170}
171
172int main(int argc, char *argv[])
173{
174 bool neverquit = TRUE;
175 bool runonce = FALSE;
176 if(argc==2){ if( QString(argv[1]) == "-once"){ runonce = TRUE; } }
177 
178 while(neverquit){
179  if(runonce){ neverquit = FALSE; }
180  qDebug() << " -- PCDM Session Starting...";
181  int sid = -1;
182  int pid = fork();
183  if(pid < 0){
184    qDebug() << "Error: Could not fork the PCDM session";
185    return -1;
186  }else if( pid ==0 ){
187    //New Child Process
188    sid = setsid(); //start a session
189    qDebug() << "-- Session ID:" << sid;
190    int retCode = runSingleSession(argc,argv);
191    qDebug() << "-- PCDM Session Ended --";
192    //check for special exit code
193    if(retCode != 0){ neverquit=FALSE; }
194    //Now kill the shild process (whole session)
195    qDebug() << "Exiting child process";
196    exit(3);
197  }else{ 
198    //Parent (calling) process
199    int status;
200    sleep(2);
201    waitpid(sid,&status,0); //wait for the child (session) to finish
202  }
203  qDebug() << "-- PCDM Session Ended --";
204  if(QFile::exists("/var/run/nologin")){ neverquit = FALSE; } 
205 }
206 return 0;
207}
Note: See TracBrowser for help on using the repository browser.