advanceconfig.cpp 6 KB
Newer Older
1 2
/*
    Copyright 2013 by Reza Fatahilah Shah <rshah0385@kireihana.com>
3

4 5 6 7
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.
8

9 10 11 12
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
13

14 15 16 17 18 19
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "advanceconfig.h"
#include "ui_advanceconfig.h"

20 21
#include <QIntValidator>

David Edmundson's avatar
David Edmundson committed
22
#include <QDebug>
David Edmundson's avatar
David Edmundson committed
23
#include <KConfigGroup>
Reza Shah's avatar
Reza Shah committed
24 25 26
#include <KUser>

#include "usersmodel.h"
27
#include "sessionmodel.h"
28
#include "config.h"
29 30 31
#include "cursortheme/thememodel.h"
#include "cursortheme/sortproxymodel.h"
#include "cursortheme/cursortheme.h"
Reza Shah's avatar
Reza Shah committed
32

33 34 35
const int MIN_UID = 1000;
const int MAX_UID = 65000;

36 37 38
AdvanceConfig::AdvanceConfig(const KSharedConfigPtr &config, QWidget *parent) :
    QWidget(parent),
    mConfig(config)
39 40 41
{
    configUi = new Ui::AdvanceConfig();
    configUi->setupUi(this);
42

Reza Shah's avatar
Reza Shah committed
43
    load();
44

Reza Shah's avatar
Reza Shah committed
45
    connect(configUi->userList, SIGNAL(activated(int)), SIGNAL(changed()));
46
    connect(configUi->sessionList, SIGNAL(activated(int)), SIGNAL(changed()));
47 48
    connect(configUi->haltCommand, SIGNAL(textChanged(QString)), SIGNAL(changed()));
    connect(configUi->rebootCommand, SIGNAL(textChanged(QString)), SIGNAL(changed()));
49
    connect(configUi->cursorList, SIGNAL(activated(int)), SIGNAL(changed()));
50 51 52 53
    connect(configUi->minimumUid, SIGNAL(textChanged(QString)), SIGNAL(changed()));
    connect(configUi->minimumUid, SIGNAL(textChanged(QString)), SLOT(slotUidRangeChanged()));
    connect(configUi->maximumUid, SIGNAL(textChanged(QString)), SIGNAL(changed()));
    connect(configUi->maximumUid, SIGNAL(textChanged(QString)), SLOT(slotUidRangeChanged()));
54 55
    connect(configUi->autoLogin, SIGNAL(clicked()), SIGNAL(changed()));
    connect(configUi->reloginAfterQuit, SIGNAL(clicked()), SIGNAL(changed()));
56 57 58 59 60 61 62 63 64
}

AdvanceConfig::~AdvanceConfig()
{
    delete configUi;
}

void AdvanceConfig::load()
{
65
    //Cursor themes
66
    CursorThemeModel *cursorModel = new CursorThemeModel(this);
67 68 69
    proxyCursorModel = new SortProxyModel(this);
    proxyCursorModel->setSourceModel(cursorModel);
    proxyCursorModel->setFilterCaseSensitivity(Qt::CaseSensitive);
70
    proxyCursorModel->sort(Qt::DisplayRole, Qt::AscendingOrder);
71 72
    
    configUi->cursorList->setModel(proxyCursorModel);
73
    QString currentCursor = mConfig->group("Theme").readEntry("CursorTheme", "");
74 75
    QModelIndex cursorIndex = proxyCursorModel->findIndex(currentCursor);
    configUi->cursorList->setCurrentIndex(cursorIndex.row() < 0 ? 0 : cursorIndex.row());
76

77
    //User list
78
    int minUid, maxUid;
79 80
    minUid = mConfig->group("Users").readEntry("MinimumUid", MIN_UID);
    maxUid = mConfig->group("Users").readEntry("MaximumUid", MAX_UID);
81 82
    
    userModel = new UsersModel(this);
83
    configUi->userList->setModel(userModel);
84
    userModel->populate( minUid, maxUid );
85

86 87 88 89
    sessionModel = new SessionModel(this);
    configUi->sessionList->setModel(sessionModel);

    const QString currentUser = mConfig->group("Autologin").readEntry("User", "");
90
    configUi->userList->setCurrentIndex(userModel->indexOf(currentUser));
91 92 93 94

    const QString autologinSession = mConfig->group("Autologin").readEntry("Session", "");
    configUi->sessionList->setCurrentIndex(sessionModel->indexOf(autologinSession));

95
    configUi->autoLogin->setChecked(!currentUser.isEmpty());
96
    configUi->reloginAfterQuit->setChecked(mConfig->group("Autologin").readEntry("Relogin", false));
97 98 99 100 101 102 103 104
    
    QValidator *uidValidator = new QIntValidator(MIN_UID, MAX_UID, configUi->minimumUid);
    configUi->minimumUid->setValidator(uidValidator);
    configUi->minimumUid->setText(QString::number(minUid));
    
    configUi->maximumUid->setValidator(uidValidator);
    configUi->maximumUid->setText(QString::number(maxUid));
    
105

106
    //Commands
107 108
    configUi->haltCommand->setUrl(QUrl::fromLocalFile(mConfig->group("General").readEntry("HaltCommand")));
    configUi->rebootCommand->setUrl(QUrl::fromLocalFile(mConfig->group("General").readEntry("RebootCommand")));
109 110 111 112 113
}

QVariantMap AdvanceConfig::save()
{
    QVariantMap args;
114

David Edmundson's avatar
David Edmundson committed
115
    qDebug() << "idx:" << configUi->cursorList->currentIndex();
116

117
    QModelIndex cursorIndex = configUi->cursorList->model()->index(configUi->cursorList->currentIndex(),0);
118 119 120
    if (cursorIndex.isValid()) {
        const CursorTheme *cursorTheme = proxyCursorModel->theme(cursorIndex);
        if (cursorTheme)
121
            args["sddm.conf/Theme/CursorTheme"] = cursorTheme->name();
122 123
    }

124
    args["sddm.conf/Autologin/User"] = ( configUi->autoLogin->isChecked() ) ? configUi->userList->currentText() : "";
125 126
    args["sddm.conf/Autologin/Session"] = ( configUi->autoLogin->isChecked() ) ? configUi->sessionList->currentData() : "";

127 128
    args["sddm.conf/Autologin/Relogin"] = configUi->reloginAfterQuit->isChecked();
    //TODO session
129 130 131 132

    int minUid = configUi->minimumUid->text().toInt();
    int maxUid = configUi->maximumUid->text().toInt();
    if (isUidRangeValid(minUid, maxUid)) {
133 134
        args["sddm.conf/Users/MinimumUid"] = configUi->minimumUid->text();
        args["sddm.conf/Users/MaximumUid"] = configUi->maximumUid->text();
135 136
    }

137 138
    args["sddm.conf/General/HaltCommand"] = configUi->haltCommand->url().toLocalFile();
    args["sddm.conf/General/RebootCommand"] = configUi->rebootCommand->url().toLocalFile();
139

140
    return args;
141
}
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

void AdvanceConfig::slotUidRangeChanged()
{
    int minUid = configUi->minimumUid->text().toInt();
    int maxUid = configUi->maximumUid->text().toInt();

    if (!isUidRangeValid(minUid, maxUid)) {
        return;
    }

    userModel->populate(minUid, maxUid);
}

bool AdvanceConfig::isUidRangeValid(int minUid, int maxUid) const
{
157
    if (minUid < 0 || minUid > maxUid)
158 159 160
        return false;
    
    return true;
David Edmundson's avatar
David Edmundson committed
161
}