File: main.cpp

package info (click to toggle)
kde-workspace 4:4.11.13-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 67,756 kB
  • ctags: 47,705
  • sloc: cpp: 358,638; ansic: 34,695; xml: 5,231; perl: 1,598; sh: 1,307; ruby: 1,135; python: 651; asm: 566; makefile: 37
file content (464 lines) | stat: -rw-r--r-- 16,208 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*
 * main.cpp
 *
 * Copyright (c) 1999 Matthias Hoelzer-Kluepfel <hoelzer@kde.org>
 *
 * Requires the Qt widget libraries, available at no cost at
 * http://www.troll.no/
 *
 *  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.
 *
 *  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.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "main.h"

#include <config-workspace.h>

#include "background.h"
#include "kdm-gen.h"
#include "kdm-dlg.h"
#include "kdm-users.h"
#include "kdm-shut.h"
#include "kdm-conv.h"
#include "kdm-theme.h"

#include "helper.h"

#include <kaboutdata.h>
#include <kimageio.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kdebug.h>
#include <kmimetype.h>
#include <ktemporaryfile.h>
#include <kconfig.h>
#include <kpluginfactory.h>
#include <kpluginloader.h>

#include <QDropEvent>
#include <QFile>
#include <QLabel>
#include <QStackedWidget>
#include <QTabWidget>
#include <QVBoxLayout>

#include <sys/types.h>
#include <unistd.h>
#include <locale.h>
#include <pwd.h>
#include <grp.h>


K_PLUGIN_FACTORY(KDMFactory, registerPlugin<KDModule>();)
K_EXPORT_PLUGIN(KDMFactory("kdmconfig"))

int handleActionReply(QWidget *parent, const KAuth::ActionReply &reply)
{
    int code = 0;

    if (reply.failed()) {
        if (reply.type() == ActionReply::KAuthError)
            KMessageBox::error(parent,
                i18n("Unable to authenticate/execute the action: %1 (code %2)",
                     reply.errorDescription(), reply.errorCode()));
        else
            code = reply.errorCode();
    }

    return code;
}

KUrl *decodeImgDrop(QDropEvent *e, QWidget *wdg)
{
    KUrl::List uriList = KUrl::List::fromMimeData(e->mimeData());
    if (!uriList.isEmpty()) {
        KUrl *url = new KUrl(uriList.first());

        KMimeType::Ptr mime = KMimeType::findByUrl(*url);
        if (mime && KImageIO::isSupported(mime->name(), KImageIO::Reading))
            return url;

        QStringList qs = KImageIO::pattern().split('\n');
        qs.removeFirst();

        QString msg = i18n(
            "%1 does not appear to be an image file.\n"
            "Please use files with these extensions:\n"
            "%2",
            url->fileName(), qs.join("\n"));
        KMessageBox::sorry(wdg, msg);
        delete url;
    }
    return 0;
}

KConfig *config;

KDModule::KDModule(QWidget *parent, const QVariantList &)
    : KCModule(KDMFactory::componentData(), parent)
    , minshowuid(0)
    , maxshowuid(0)
    , updateOK(false)
{
    KAboutData *about =
        new KAboutData("kcmkdm", "kdmconfig", ki18n("KDE Login Manager Config Module"),
                       QByteArray(), KLocalizedString(), KAboutData::License_GPL,
                       ki18n("(c) 1996-2010 The KDM Authors"), KLocalizedString(),
                       "http://developer.kde.org/~ossi/sw/kdm.html");

    about->addAuthor(ki18n("Thomas Tanghus"), ki18n("Original author"), "tanghus@earthling.net");
    about->addAuthor(ki18n("Steffen Hansen"), KLocalizedString(), "hansen@kde.org");
    about->addAuthor(ki18n("Oswald Buddenhagen"), ki18n("Current maintainer"), "ossi@kde.org");
    about->addAuthor(ki18n("Stephen Leaf"), KLocalizedString(), "smileaf@smileaf.org");
    about->addAuthor(ki18n("Igor Krivenko"), KLocalizedString(), "igor@shg.ru");

    setQuickHelp(i18n(
        "<h1>Login Manager</h1> In this module you can configure the "
        "various aspects of the KDE Login Manager. This includes "
        "the look and feel as well as the users that can be "
        "selected for login. Note that you can only make changes "
        "if you run the module with superuser rights."
        "<h2>General</h2> On this tab page, you can configure parts of "
        "the Login Manager's look, and which language it should use. "
        "The language settings made here have no influence on "
        "the user's language settings."
        "<h2>Dialog</h2>Here you can configure the look of the \"classical\" "
        "dialog based mode if you have chosen to use it. "
        "<h2>Background</h2>If you want to set a special background for the dialog-based "
        "login screen, this is where to do it."
        "<h2>Themes</h2> Here you can specify the theme to be used by the Login Manager."
        "<h2>Shutdown</h2> Here you can specify who is allowed to shutdown/reboot the machine "
        "and whether a boot manager should be used."
        "<h2>Users</h2>On this tab page, you can select which users the Login Manager "
        "will offer you for logging in."
        "<h2>Convenience</h2> Here you can specify a user to be logged in automatically, "
        "users not needing to provide a password to log in, and other convenience features.<br/>"
        "Note that by their nature, these settings are security holes, so use them very carefully."));

    setAboutData(about);

    setlocale(LC_COLLATE, "C");

    KGlobal::locale()->insertCatalog("kcmbackground");

    QStringList sl;
    QMap<gid_t, QStringList> tgmap;
    QMap<gid_t, QStringList>::Iterator tgmapi;
    QMap<gid_t, QStringList>::ConstIterator tgmapci;
    QMap<QString, QPair<int, QStringList> >::Iterator umapi;

    struct passwd *ps;
    for (setpwent(); (ps = getpwent());) {
        QString un(QFile::decodeName(ps->pw_name));
        if (usermap.find(un) == usermap.end()) {
            usermap.insert(un, QPair<int, QStringList>(ps->pw_uid, sl));
            if ((tgmapi = tgmap.find(ps->pw_gid)) != tgmap.end())
                (*tgmapi).append(un);
            else
                tgmap[ps->pw_gid] = QStringList(un);
        }
    }
    endpwent();

    struct group *grp;
    for (setgrent(); (grp = getgrent());) {
        QString gn(QFile::decodeName(grp->gr_name));
        bool delme = false;
        if ((tgmapi = tgmap.find(grp->gr_gid)) != tgmap.end()) {
            if ((*tgmapi).count() == 1 && (*tgmapi).first() == gn)
                delme = true;
            else
                for (QStringList::ConstIterator it = (*tgmapi).constBegin();
                        it != (*tgmapi).constEnd(); ++it)
                    usermap[*it].second.append(gn);
            tgmap.erase(tgmapi);
        }
        if (!*grp->gr_mem ||
                (delme && !grp->gr_mem[1] && gn == QFile::decodeName(*grp->gr_mem)))
            continue;
        do {
            QString un(QFile::decodeName(*grp->gr_mem));
            if ((umapi = usermap.find(un)) != usermap.end()) {
                if (!(*umapi).second.contains(gn))
                    (*umapi).second.append(gn);
            } else
                kWarning() << "group '" << gn << "' contains unknown user '" << un << "'" ;
        } while (*++grp->gr_mem);
    }
    endgrent();

    for (tgmapci = tgmap.constBegin(); tgmapci != tgmap.constEnd(); ++tgmapci)
        kWarning() << "user(s) '" << tgmapci.value().join(",")
                   << "' have unknown GID " << tgmapci.key() << endl;

    config = createTempConfig();

    QVBoxLayout *top = new QVBoxLayout(this);
    top->setMargin(0);
    tab = new QTabWidget(this);

    general = new KDMGeneralWidget(this);
    tab->addTab(general, i18n("&General"));
    connect(general, SIGNAL(changed()), SLOT(changed()));
    connect(general, SIGNAL(useThemeChanged(bool)),
            SLOT(slotUseThemeChanged(bool)));

    dialog_stack = new QStackedWidget(this);
    tab->addTab(dialog_stack, i18n("&Dialog"));
    dialog = new KDMDialogWidget(dialog_stack);
    dialog_stack->addWidget(dialog);
    connect(dialog, SIGNAL(changed()), SLOT(changed()));
    QLabel *lbl = new QLabel(
        i18n("There is no login dialog window in themed mode."),
        dialog_stack);
    lbl->setAlignment(Qt::AlignCenter);
    dialog_stack->addWidget(lbl);

    background_stack = new QStackedWidget(this);
    tab->addTab(background_stack, i18n("&Background"));
    background = new KBackground(createBackgroundTempConfig(), background_stack);
    background_stack->addWidget(background);
    connect(background, SIGNAL(changed()), SLOT(changed()));
    lbl = new QLabel(
        i18n("The background cannot be configured separately in themed mode."),
        background_stack);
    lbl->setAlignment(Qt::AlignCenter);
    background_stack->addWidget(lbl);

    theme_stack = new QStackedWidget(this);
    tab->addTab(theme_stack, i18n("&Theme"));
    lbl = new QLabel(
        i18n("Themed mode is disabled. See \"General\" tab."),
        theme_stack);
    lbl->setAlignment(Qt::AlignCenter);
    theme_stack->addWidget(lbl);
    theme = new KDMThemeWidget(theme_stack);
    theme_stack->addWidget(theme);
    connect(theme, SIGNAL(changed()), SLOT(changed()));

    sessions = new KDMSessionsWidget(this);
    tab->addTab(sessions, i18n("&Shutdown"));
    connect(sessions, SIGNAL(changed()), SLOT(changed()));

    users = new KDMUsersWidget(this);
    tab->addTab(users, i18n("&Users"));
    connect(users, SIGNAL(changed()), SLOT(changed()));
    connect(users, SIGNAL(setMinMaxUID(int,int)), SLOT(slotMinMaxUID(int,int)));
    connect(this, SIGNAL(addUsers(QMap<QString,int>)),
            users, SLOT(slotAddUsers(QMap<QString,int>)));
    connect(this, SIGNAL(delUsers(QMap<QString,int>)),
            users, SLOT(slotDelUsers(QMap<QString,int>)));
    connect(this, SIGNAL(clearUsers()), users, SLOT(slotClearUsers()));

    convenience = new KDMConvenienceWidget(this);
    tab->addTab(convenience, i18n("&Convenience"));
    connect(convenience, SIGNAL(changed()), SLOT(changed()));
    connect(this, SIGNAL(addUsers(QMap<QString,int>)),
            convenience, SLOT(slotAddUsers(QMap<QString,int>)));
    connect(this, SIGNAL(delUsers(QMap<QString,int>)),
            convenience, SLOT(slotDelUsers(QMap<QString,int>)));
    connect(this, SIGNAL(clearUsers()), convenience, SLOT(slotClearUsers()));

    top->addWidget(tab);

    setNeedsAuthorization(true);
}

KConfig *KDModule::createTempConfig()
{
    pTempConfigFile = new KTemporaryFile;
    pTempConfigFile->open();
    QString tempConfigName = pTempConfigFile->fileName();

    KConfig *pSystemKDMConfig = new KConfig(
        QString::fromLatin1(KDE_DEBIAN_CONFDIR "/kdm/kdmrc"), KConfig::SimpleConfig);

    KConfig *pTempConfig = pSystemKDMConfig->copyTo(tempConfigName);
    pTempConfig->sync();

    QFile::setPermissions(tempConfigName,
                          pTempConfigFile->permissions() | QFile::ReadOther);

    return pTempConfig;
}

KSharedConfigPtr KDModule::createBackgroundTempConfig()
{
    pBackgroundTempConfigFile = new KTemporaryFile;
    pBackgroundTempConfigFile->open();
    QString tempBackgroundConfigName = pBackgroundTempConfigFile->fileName();

    QString systemBackgroundConfigName =
        config->group("X-*-Greeter").readEntry(
            "BackgroundCfg", KDE_DEBIAN_CONFDIR "/kdm/backgroundrc");

    KConfig systemBackgroundConfig(systemBackgroundConfigName, KConfig::SimpleConfig);
    KSharedConfigPtr pTempConfig = KSharedConfig::openConfig(tempBackgroundConfigName);
    systemBackgroundConfig.copyTo(tempBackgroundConfigName, pTempConfig.data());
    pTempConfig->sync();

    QFile::setPermissions(tempBackgroundConfigName,
                          pBackgroundTempConfigFile->permissions() | QFile::ReadOther);

    return pTempConfig;
}

KDModule::~KDModule()
{
    delete config;
    delete pBackgroundTempConfigFile;
    delete pTempConfigFile;
}

void KDModule::load()
{
    general->load();
    dialog->load();
    background->load();
    theme->load();
    users->load();
    sessions->load();
    convenience->load();
    propagateUsers();

    emit changed(false);
}


void KDModule::save()
{
    general->save();
    dialog->save();
    background->save();
    theme->save();
    users->save();
    sessions->save();
    convenience->save();
    config->sync();

    QVariantMap helperargs;
    helperargs["tempkdmrcfile"] = config->name();
    helperargs["tempbackgroundrcfile"] = pBackgroundTempConfigFile->fileName();

    KAuth::Action *pAction = authAction();
    pAction->setArguments(helperargs);
    KAuth::ActionReply reply = pAction->execute();

    switch (handleActionReply(this, reply)) {
    case Helper::KdmrcInstallError:
        KMessageBox::error(this,
            i18n("Unable to install new kdmrc file from\n%1",
                 config->name()));
        break;
    case Helper::BackgroundrcInstallError:
        KMessageBox::error(this,
            i18n("Unable to install new backgroundrc file from\n%1",
                 pBackgroundTempConfigFile->fileName()));
        break;
    case Helper::KdmrcInstallError | Helper::BackgroundrcInstallError:
        KMessageBox::error(this,
            i18n("Unable to install new kdmrc file from\n%1"
                 "\nand new backgroundrc file from\n%2",
                 config->name(), pBackgroundTempConfigFile->fileName()));
        break;
    }

    emit changed(false);
}

void KDModule::defaults()
{
    general->defaults();
    dialog->defaults();
    background->defaults();
    theme->defaults();
    users->defaults();
    sessions->defaults();
    convenience->defaults();
    propagateUsers();
}

void KDModule::propagateUsers()
{
    groupmap.clear();
    emit clearUsers();
    QMap<QString, int> lusers;
    QMap<QString, QPair<int, QStringList> >::const_iterator it;
    QStringList::ConstIterator jt;
    QMap<QString, int>::Iterator gmapi;
    for (it = usermap.constBegin(); it != usermap.constEnd(); ++it) {
        int uid = it.value().first;
        if (!uid || (uid >= minshowuid && uid <= maxshowuid)) {
            lusers[it.key()] = uid;
            for (jt = it.value().second.begin(); jt != it.value().second.end(); ++jt)
                if ((gmapi = groupmap.find(*jt)) == groupmap.end()) {
                    groupmap[*jt] = 1;
                    lusers['@' + *jt] = -uid;
                } else
                    (*gmapi)++;
        }
    }
    emit addUsers(lusers);
    updateOK = true;
}

void KDModule::slotMinMaxUID(int min, int max)
{
    if (updateOK) {
        QMap<QString, int> alusers, dlusers;
        QMap<QString, QPair<int, QStringList> >::const_iterator it;
        QStringList::ConstIterator jt;
        QMap<QString, int>::Iterator gmapi;
        for (it = usermap.constBegin(); it != usermap.constEnd(); ++it) {
            int uid = it.value().first;
            if (!uid)
                continue;
            if ((uid >= minshowuid && uid <= maxshowuid) &&
                    !(uid >= min && uid <= max)) {
                dlusers[it.key()] = uid;
                for (jt = it.value().second.begin();
                        jt != it.value().second.end(); ++jt) {
                    gmapi = groupmap.find(*jt);
                    if (!--(*gmapi)) {
                        groupmap.erase(gmapi);
                        dlusers['@' + *jt] = -uid;
                    }
                }
            } else if ((uid >= min && uid <= max) &&
                       !(uid >= minshowuid && uid <= maxshowuid)) {
                alusers[it.key()] = uid;
                for (jt = it.value().second.begin();
                        jt != it.value().second.end(); ++jt)
                    if ((gmapi = groupmap.find(*jt)) == groupmap.end()) {
                        groupmap[*jt] = 1;
                        alusers['@' + *jt] = -uid;
                    } else
                        (*gmapi)++;
            }
        }
        emit delUsers(dlusers);
        emit addUsers(alusers);
    }
    minshowuid = min;
    maxshowuid = max;
}

void KDModule::slotUseThemeChanged(bool use)
{
    dialog_stack->setCurrentIndex(use);
    background_stack->setCurrentIndex(use);
    theme_stack->setCurrentIndex(use);
}

#include "main.moc"