source: src-qt4/pc-dmconf/mainwindow.cpp

Last change on this file was c753246, checked in by Ken Moore <ken@…>, 3 months ago

Make sure that pc-dmconf ensures that the "show users" button is checked if the setting is not explicity set in the config file.

  • Property mode set to 100644
File size: 10.2 KB
Line 
1/**************************************************************************
2*   Copyright (C) 2012 by Yuri Momotyuk                                   *
3*   yurkis@gmail.com                                                      *
4*   Copyright (C) 2013 by Kris Moore                                      *
5*   kris@pcbsd.org                                                        *
6*                                                                         *
7*   Permission is hereby granted, free of charge, to any person obtaining *
8*   a copy of this software and associated documentation files (the       *
9*   "Software"), to deal in the Software without restriction, including   *
10*   without limitation the rights to use, copy, modify, merge, publish,   *
11*   distribute, sublicense, and/or sell copies of the Software, and to    *
12*   permit persons to whom the Software is furnished to do so, subject to *
13*   the following conditions:                                             *
14*                                                                         *
15*   The above copyright notice and this permission notice shall be        *
16*   included in all copies or substantial portions of the Software.       *
17*                                                                         *
18*   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       *
19*   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    *
20*   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*
21*   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR     *
22*   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
23*   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
24*   OTHER DEALINGS IN THE SOFTWARE.                                       *
25***************************************************************************/
26
27#include "mainwindow.h"
28#include "ui_mainwindow.h"
29#include "pcbsd-utils.h"
30
31#include <QInputDialog>
32#include <QMessageBox>
33#include <QTextStream>
34
35#define DM_CONFIG_FILE QString("/usr/local/etc/pcdm.conf")
36
37///////////////////////////////////////////////////////////////////////////////
38inline bool toBoolean(QString Str, bool Default=false)
39{
40    if (Str.trimmed().toLower() == "true")
41        return true;
42    if (Str.trimmed().toLower() == "false")
43        return false;
44    return Default;
45}
46
47///////////////////////////////////////////////////////////////////////////////
48inline QString fromBoolean(bool flag)
49{
50    return (flag)?QString("true"):QString("false");
51}
52
53///////////////////////////////////////////////////////////////////////////////
54MainWindow::MainWindow(QWidget *parent) :
55    QMainWindow(parent),
56    ui(new Ui::MainWindow)
57{
58    ui->setupUi(this);
59    initUI();
60}
61
62///////////////////////////////////////////////////////////////////////////////
63MainWindow::~MainWindow()
64{
65    delete ui;
66}
67
68///////////////////////////////////////////////////////////////////////////////
69void MainWindow::initUI()
70{
71    //Make sure the conf file exists
72    if(!QFile::exists(DM_CONFIG_FILE)){
73      qDebug() << "Copying over the default configuration file:" << DM_CONFIG_FILE;
74      if( !QFile::copy(DM_CONFIG_FILE+".dist", DM_CONFIG_FILE) ){
75        QMessageBox::warning(this,tr("Missing Config File"), QString(tr("The PCDM configuration file could not be found: %1")).arg(DM_CONFIG_FILE)+"\n\n"+tr("This application will now close"));
76        exit(1);
77      }
78    }
79    getUsers();
80    ui->UsersList->clear();
81    QString autoLogDelay = pcbsd::Utils::getValFromSHFile(DM_CONFIG_FILE, "AUTO_LOGIN_DELAY");
82    if(!autoLogDelay.isEmpty()){
83      ui->spin_autoLogDelay->setValue(autoLogDelay.toInt());
84    }
85   
86    QString autoLogUser = pcbsd::Utils::getValFromSHFile(DM_CONFIG_FILE, "AUTO_LOGIN_USER");
87    for (int  i=0; i<mvUsers.size(); i++)
88    {
89        ui->UsersList->addItem(mvUsers[i]);
90        if ( autoLogUser == mvUsers[i] )
91          ui->UsersList->setCurrentIndex(i);
92    }
93
94    QString autoLog = pcbsd::Utils::getValFromSHFile(DM_CONFIG_FILE, "ENABLE_AUTO_LOGIN");
95    if ( autoLog == "TRUE" ) {
96      ui->AutoLoginEnabledCB->setChecked(true);
97      ui->UsersList->setEnabled(true);
98    } else {
99      ui->AutoLoginEnabledCB->setChecked(false);
100      ui->UsersList->setEnabled(false);
101    }
102
103    ui->EnableVNC->setChecked(false);
104    QString vnc = pcbsd::Utils::getValFromSHFile(DM_CONFIG_FILE, "ALLOW_REMOTE_LOGIN");
105    if ( vnc == "TRUE" )
106      ui->EnableVNC->setChecked(true);
107
108    ui->checkShowPW->setChecked(false);
109    QString showpw = pcbsd::Utils::getValFromSHFile(DM_CONFIG_FILE, "ENABLE_VIEW_PASSWORD_BUTTON");
110    if ( showpw == "TRUE" )
111      ui->checkShowPW->setChecked(true);
112
113    ui->checkShowUsers->setChecked(true); //PCDM defaults to true
114    QString showusers = pcbsd::Utils::getValFromSHFile(DM_CONFIG_FILE, "SHOW_SYSTEM_USERS");
115    if( showusers != "TRUE" && !showusers.isEmpty() ){
116      ui->checkShowUsers->setChecked(false);
117    }
118   
119    //Update the UI appropriately
120    itemChanged();
121    ui->SaveButton->setEnabled(false); //re-disable the save button because nothing has changed yet
122   
123    //Now setup all the signals/slots for updating the UI appropriately
124    connect( ui->AutoLoginEnabledCB, SIGNAL(stateChanged(int)), this, SLOT(itemChanged()) );
125    connect( ui->UsersList, SIGNAL(currentIndexChanged(int)), this, SLOT(itemChanged()) );
126    connect( ui->spin_autoLogDelay, SIGNAL(valueChanged(int)), this, SLOT(itemChanged()) );
127    connect( ui->EnableVNC, SIGNAL(stateChanged(int)), this, SLOT(itemChanged()) );
128    connect( ui->checkShowPW, SIGNAL(stateChanged(int)), this, SLOT(itemChanged()) );
129    connect( ui->checkShowUsers, SIGNAL(stateChanged(int)), this, SLOT(itemChanged()) );
130   
131   
132}
133
134///////////////////////////////////////////////////////////////////////////////
135void MainWindow::getUsers()
136{
137    /*
138        here is modifyed code from pc-usermanager
139    */
140    mvUsers.clear();
141    QFile userFile("/etc/passwd");
142    if ( userFile.open(QIODevice::ReadOnly) ) {
143        QTextStream stream(&userFile);
144        stream.setCodec("UTF-8");
145        QString line;
146
147        while ( !stream.atEnd() ) {
148            line = stream.readLine();
149
150            if ((line.trimmed().indexOf("#") != 0) && (! line.isEmpty())) { //Make sure it isn't a comment or blank
151                QString username = line.section(":",0,0);
152                QString homedir = line.section(":",5,5);
153
154                // Ignore PEFS encrypted users
155                if ( QFile::exists(homedir + "/.pefs.db") )
156                  continue;
157
158                int uid = line.section(":",2,2).toInt();
159                if ((uid>1000)&&(uid<65534))
160                    mvUsers.push_back(username);
161            }//if gout user
162        }//for all lines
163    }//if can open file
164}
165
166///////////////////////////////////////////////////////////////////////////////
167void MainWindow::on_SaveButton_clicked()
168{
169    bool ok;
170    system("touch " + DM_CONFIG_FILE.toLatin1());
171    if ( ui->AutoLoginEnabledCB->isChecked() && !ui->UsersList->currentText().isEmpty() ) {
172       // First ask for password
173       QString pw = QInputDialog::getText(this, tr("Password Request"),
174                        tr("Please enter the login password for this user"), 
175                        QLineEdit::Password, "", &ok);
176       if ( ! ok )
177          return;
178     
179       pcbsd::Utils::setConfFileValue(DM_CONFIG_FILE, "ENABLE_AUTO_LOGIN", "ENABLE_AUTO_LOGIN=TRUE", -1);
180       pcbsd::Utils::setConfFileValue(DM_CONFIG_FILE, "AUTO_LOGIN_USER", "AUTO_LOGIN_USER=" + ui->UsersList->currentText(), -1);
181       pcbsd::Utils::setConfFileValue(DM_CONFIG_FILE, "AUTO_LOGIN_PASSWORD", "AUTO_LOGIN_PASSWORD=" + pw, -1);
182       pcbsd::Utils::setConfFileValue(DM_CONFIG_FILE, "AUTO_LOGIN_DELAY","AUTO_LOGIN_DELAY="+QString::number(ui->spin_autoLogDelay->value()), -1);
183    } else {
184       ui->AutoLoginEnabledCB->setChecked(false); //make sure this is not checked to reflect file contents
185       pcbsd::Utils::setConfFileValue(DM_CONFIG_FILE, "ENABLE_AUTO_LOGIN", "ENABLE_AUTO_LOGIN=FALSE", -1);
186       pcbsd::Utils::setConfFileValue(DM_CONFIG_FILE, "AUTO_LOGIN_USER", "", -1);
187       pcbsd::Utils::setConfFileValue(DM_CONFIG_FILE, "AUTO_LOGIN_PASSWORD", "", -1);
188       pcbsd::Utils::setConfFileValue(DM_CONFIG_FILE, "AUTO_LOGIN_DELAY","", -1);
189    }
190
191    if ( ui->EnableVNC->isChecked() ) {
192       // First ask for password
193       QString pw = QInputDialog::getText(this, tr("Password Request"),
194                        tr("Please enter the remote login password"), 
195                        QLineEdit::Password, "", &ok);
196       if ( ! ok )
197          return;
198
199       QFile file("/usr/local/etc/vncpass");
200       if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
201          return;
202
203       QTextStream out(&file);
204       out << pw;
205       file.close();
206       system("chmod 600 /usr/local/etc/vncpass");
207
208       pcbsd::Utils::setConfFileValue(DM_CONFIG_FILE, "ALLOW_REMOTE_LOGIN", "ALLOW_REMOTE_LOGIN=TRUE", -1);
209    } else {
210       pcbsd::Utils::setConfFileValue(DM_CONFIG_FILE, "ALLOW_REMOTE_LOGIN", "ALLOW_REMOTE_LOGIN=FALSE", -1);
211       system("rm /usr/local/etc/vncpass 2>/dev/null");
212    }
213
214    if ( ui->checkShowPW->isChecked() )
215       pcbsd::Utils::setConfFileValue(DM_CONFIG_FILE, "ENABLE_VIEW_PASSWORD_BUTTON", "ENABLE_VIEW_PASSWORD_BUTTON=TRUE", -1);
216    else
217       pcbsd::Utils::setConfFileValue(DM_CONFIG_FILE, "ENABLE_VIEW_PASSWORD_BUTTON", "ENABLE_VIEW_PASSWORD_BUTTON=FALSE", -1);
218
219    if(ui->checkShowUsers->isChecked()){
220        pcbsd::Utils::setConfFileValue(DM_CONFIG_FILE, "SHOW_SYSTEM_USERS", "SHOW_SYSTEM_USERS=TRUE", -1);
221    }else{
222        pcbsd::Utils::setConfFileValue(DM_CONFIG_FILE, "SHOW_SYSTEM_USERS", "SHOW_SYSTEM_USERS=FALSE", -1);
223    }
224   
225    // Lastly make sure we set perms
226    system("chmod 600 " + DM_CONFIG_FILE.toLatin1());
227    ui->SaveButton->setEnabled(false);
228}
229
230
231///////////////////////////////////////////////////////////////////////////////
232void MainWindow::slotSingleInstance()
233{
234   this->hide();
235   this->showNormal();
236   this->activateWindow();
237   this->raise();
238}
239
240/////////////////////////////////////////////////
241void MainWindow::itemChanged(){
242  ui->SaveButton->setEnabled(true);
243  //Double check the dependant options for whether they are possible to be changed
244  ui->UsersList->setEnabled( ui->AutoLoginEnabledCB->isChecked() );
245  ui->spin_autoLogDelay->setEnabled( ui->AutoLoginEnabledCB->isChecked() );
246}
Note: See TracBrowser for help on using the repository browser.