~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to kdm/kcm/kdm-dlg.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (C) 1997-1998 Thomas Tanghus (tanghus@earthling.net)
 
3
 
 
4
  This program is free software; you can redistribute it and/or
 
5
  modify it under the terms of the GNU General Public
 
6
  License as published by the Free Software Foundation; either
 
7
  version 2 of the License, or (at your option) any later version.
 
8
 
 
9
  This program is distributed in the hope that it will be useful,
 
10
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
  General Public License for more details.
 
13
 
 
14
  You should have received a copy of the GNU Library General Public License
 
15
  along with this library; see the file COPYING.LIB.  If not, write to
 
16
  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
17
  Boston, MA 02110-1301, USA.
 
18
*/
 
19
 
 
20
#include "kdm-dlg.h"
 
21
 
 
22
#include "positioner.h"
 
23
 
 
24
#include <KDialog>
 
25
#include <KFileDialog>
 
26
#include <KIconLoader>
 
27
#include <KImageFilePreview>
 
28
#include <KImageIO>
 
29
#include <KLineEdit>
 
30
#include <KLocale>
 
31
#include <KMessageBox>
 
32
#include <KConfig>
 
33
#include <KConfigGroup>
 
34
#include <KStandardDirs>
 
35
#include <KIO/NetAccess>
 
36
 
 
37
#include <QButtonGroup>
 
38
#include <QDragEnterEvent>
 
39
#include <QEvent>
 
40
#include <QLabel>
 
41
#include <QPushButton>
 
42
#include <QRadioButton>
 
43
#include <QStyle>
 
44
#include <QGridLayout>
 
45
#include <QHBoxLayout>
 
46
#include <QVBoxLayout>
 
47
 
 
48
extern KConfig *config;
 
49
 
 
50
KDMDialogWidget::KDMDialogWidget(QWidget *parent)
 
51
    : QWidget(parent)
 
52
{
 
53
    QString wtstr;
 
54
 
 
55
    QGridLayout *grid = new QGridLayout(this);
 
56
    grid->setMargin(KDialog::marginHint());
 
57
    grid->setSpacing(KDialog::spacingHint());
 
58
    grid->setColumnStretch(1, 1);
 
59
 
 
60
    QHBoxLayout *hlay = new QHBoxLayout();
 
61
    hlay->setSpacing(KDialog::spacingHint());
 
62
    grid->addLayout(hlay, 0, 0, 1, 2);
 
63
    greetstr_lined = new KLineEdit(this);
 
64
    QLabel *label = new QLabel(i18n("&Greeting:"), this);
 
65
    label->setBuddy(greetstr_lined);
 
66
    hlay->addWidget(label);
 
67
    connect(greetstr_lined, SIGNAL(textChanged(const QString &)),
 
68
            SIGNAL(changed()));
 
69
    hlay->addWidget(greetstr_lined);
 
70
    wtstr = i18n(
 
71
        "<p>This is the \"headline\" for KDM's login window. You may want to "
 
72
        "put some nice greeting or information about the operating system here.</p>"
 
73
        "<p>KDM will substitute the following character pairs with the "
 
74
        "respective contents:</p>"
 
75
        "<ul>"
 
76
        "<li>%d -> current display</li>"
 
77
        "<li>%h -> host name, possibly with domain name</li>"
 
78
        "<li>%n -> node name, most probably the host name without domain name</li>"
 
79
        "<li>%s -> the operating system</li>"
 
80
        "<li>%r -> the operating system's version</li>"
 
81
        "<li>%m -> the machine (hardware) type</li>"
 
82
        "<li>%% -> a single %</li>"
 
83
        "</ul>");
 
84
    label->setWhatsThis(wtstr);
 
85
    greetstr_lined->setWhatsThis(wtstr);
 
86
 
 
87
 
 
88
    QGridLayout *hglay = new QGridLayout();
 
89
    hglay->setSpacing(KDialog::spacingHint());
 
90
    grid->addLayout(hglay, 1, 0);
 
91
 
 
92
    label = new QLabel(i18n("Logo area:"), this);
 
93
    hglay->addWidget(label, 0, 0);
 
94
    QVBoxLayout *vlay = new QVBoxLayout();
 
95
    vlay->setSpacing(KDialog::spacingHint());
 
96
    hglay->addLayout(vlay, 0, 1, 1, 2);
 
97
    noneRadio = new QRadioButton(i18nc("logo area", "&None"), this);
 
98
    clockRadio = new QRadioButton(i18n("Show cloc&k"), this);
 
99
    logoRadio = new QRadioButton(i18n("Sho&w logo"), this);
 
100
    QButtonGroup *buttonGroup = new QButtonGroup(this);
 
101
    connect(buttonGroup, SIGNAL(buttonClicked(int)),
 
102
            SLOT(slotAreaRadioClicked(int)));
 
103
    connect(buttonGroup, SIGNAL(buttonClicked(int)), SIGNAL(changed()));
 
104
    buttonGroup->addButton(noneRadio, KdmNone);
 
105
    buttonGroup->addButton(clockRadio, KdmClock);
 
106
    buttonGroup->addButton(logoRadio, KdmLogo);
 
107
    vlay->addWidget(noneRadio);
 
108
    vlay->addWidget(clockRadio);
 
109
    vlay->addWidget(logoRadio);
 
110
    wtstr = i18n("You can choose to display a custom logo (see below), a clock or no logo at all.");
 
111
    label->setWhatsThis(wtstr);
 
112
    noneRadio->setWhatsThis(wtstr);
 
113
    logoRadio->setWhatsThis(wtstr);
 
114
    clockRadio->setWhatsThis(wtstr);
 
115
 
 
116
    logoLabel = new QLabel(i18n("&Logo:"), this);
 
117
    logobutton = new QPushButton(this);
 
118
    logoLabel->setBuddy(logobutton);
 
119
    logobutton->setAutoDefault(false);
 
120
    logobutton->setAcceptDrops(true);
 
121
    logobutton->installEventFilter(this); // for drag and drop
 
122
    connect(logobutton, SIGNAL(clicked()), SLOT(slotLogoButtonClicked()));
 
123
    hglay->addWidget(logoLabel, 1, 0, Qt::AlignVCenter);
 
124
    hglay->addWidget(logobutton, 1, 1, Qt::AlignCenter);
 
125
    hglay->setRowMinimumHeight(1, 110);
 
126
    wtstr = i18n(
 
127
        "Click here to choose an image that KDM will display. "
 
128
        "You can also drag and drop an image onto this button "
 
129
        "(e.g. from Konqueror).");
 
130
    logoLabel->setWhatsThis(wtstr);
 
131
    logobutton->setWhatsThis(wtstr);
 
132
 
 
133
 
 
134
    vlay = new QVBoxLayout();
 
135
    grid->addLayout(vlay, 1, 1, 2, 1);
 
136
    vlay->setParent(grid);
 
137
 
 
138
    label = new QLabel(i18n("Dialog &position:"), this);
 
139
    vlay->addWidget(label);
 
140
    positioner = new Positioner(this);
 
141
    label->setBuddy(positioner);
 
142
    connect(positioner, SIGNAL(positionChanged()), SIGNAL(changed()));
 
143
    vlay->addWidget(positioner);
 
144
 
 
145
    grid->setRowStretch(3, 1);
 
146
 
 
147
}
 
148
 
 
149
bool KDMDialogWidget::setLogo(const QString &logo)
 
150
{
 
151
    QString flogo = logo.isEmpty() ?
 
152
        KStandardDirs::locate("data", QLatin1String("kdm/pics/kdelogo.png")) :
 
153
        logo;
 
154
    QImage p(flogo);
 
155
    if (p.isNull())
 
156
        return false;
 
157
    if (p.width() > 100 || p.height() > 100)
 
158
        p = p.scaled(100, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation);
 
159
    logobutton->setIcon(QPixmap::fromImage(p));
 
160
    uint bd = style()->pixelMetric(QStyle::PM_ButtonMargin) * 2;
 
161
    logobutton->setFixedSize(p.width() + bd, p.height() + bd);
 
162
    logopath = logo;
 
163
    return true;
 
164
}
 
165
 
 
166
void KDMDialogWidget::slotLogoButtonClicked()
 
167
{
 
168
    KFileDialog dialog(KStandardDirs::locate("data", QLatin1String("kdm/pics/")),
 
169
                       KImageIO::pattern(KImageIO::Reading),
 
170
                       this);
 
171
    dialog.setOperationMode(KFileDialog::Opening);
 
172
    dialog.setMode(KFile::File | KFile::LocalOnly);
 
173
 
 
174
    KImageFilePreview *imagePreview = new KImageFilePreview(&dialog);
 
175
    dialog.setPreviewWidget(imagePreview);
 
176
    if (dialog.exec() == QDialog::Accepted &&
 
177
            setLogo(dialog.selectedFile()))
 
178
        changed();
 
179
}
 
180
 
 
181
 
 
182
void KDMDialogWidget::slotAreaRadioClicked(int id)
 
183
{
 
184
    logobutton->setEnabled(id == KdmLogo);
 
185
    logoLabel->setEnabled(id == KdmLogo);
 
186
}
 
187
 
 
188
 
 
189
bool KDMDialogWidget::eventFilter(QObject *, QEvent *e)
 
190
{
 
191
    if (e->type() == QEvent::DragEnter) {
 
192
        iconLoaderDragEnterEvent((QDragEnterEvent *)e);
 
193
        return true;
 
194
    }
 
195
 
 
196
    if (e->type() == QEvent::Drop) {
 
197
        iconLoaderDropEvent((QDropEvent *)e);
 
198
        return true;
 
199
    }
 
200
 
 
201
    return false;
 
202
}
 
203
 
 
204
void KDMDialogWidget::iconLoaderDragEnterEvent(QDragEnterEvent *e)
 
205
{
 
206
    e->setAccepted(KUrl::List::canDecode(e->mimeData()));
 
207
}
 
208
 
 
209
 
 
210
KUrl *decodeImgDrop(QDropEvent *e, QWidget *wdg);
 
211
 
 
212
void KDMDialogWidget::iconLoaderDropEvent(QDropEvent *e)
 
213
{
 
214
    // Installation of a logo from a non-local file is not
 
215
    // such a prominent feature to introduce a new KAuth action
 
216
    // for it. So let us just ignore these drops.
 
217
 
 
218
    KUrl *url = decodeImgDrop(e, this);
 
219
    if (url && url->isLocalFile()) {
 
220
        if (!setLogo(url->toLocalFile())) {
 
221
            QString msg = i18n("There was an error loading the image:\n"
 
222
                               "%1\n"
 
223
                               "It will not be saved.",
 
224
                               url->toLocalFile());
 
225
            KMessageBox::sorry(this, msg);
 
226
        } else {
 
227
            changed();
 
228
        }
 
229
    }
 
230
    delete url;
 
231
}
 
232
 
 
233
 
 
234
void KDMDialogWidget::save()
 
235
{
 
236
    KConfigGroup configGrp = config->group("X-*-Greeter");
 
237
 
 
238
    configGrp.writeEntry("GreetString", greetstr_lined->text());
 
239
 
 
240
    configGrp.writeEntry("LogoArea", noneRadio->isChecked() ? "None" :
 
241
                         logoRadio->isChecked() ? "Logo" : "Clock");
 
242
 
 
243
    configGrp.writeEntry("LogoPixmap",
 
244
        KIconLoader::global()->iconPath(logopath, KIconLoader::Desktop, true));
 
245
 
 
246
    configGrp.writeEntry("GreeterPos",
 
247
        QString("%1,%2").arg(positioner->x()).arg(positioner->y()));
 
248
}
 
249
 
 
250
 
 
251
void KDMDialogWidget::load()
 
252
{
 
253
    KConfigGroup configGrp = config->group("X-*-Greeter");
 
254
 
 
255
    // Read the greeting string
 
256
    greetstr_lined->setText(configGrp.readEntry("GreetString",
 
257
                                                i18n("Welcome to %s at %n")));
 
258
 
 
259
    // Regular logo or clock
 
260
    QString logoArea = configGrp.readEntry("LogoArea", "Logo");
 
261
    if (logoArea == "Clock") {
 
262
        clockRadio->setChecked(true);
 
263
        slotAreaRadioClicked(KdmClock);
 
264
    } else if (logoArea == "Logo") {
 
265
        logoRadio->setChecked(true);
 
266
        slotAreaRadioClicked(KdmLogo);
 
267
    } else {
 
268
        noneRadio->setChecked(true);
 
269
        slotAreaRadioClicked(KdmNone);
 
270
    }
 
271
 
 
272
    // See if we use alternate logo
 
273
    setLogo(configGrp.readEntry("LogoPixmap"));
 
274
 
 
275
    QStringList sl = configGrp.readEntry("GreeterPos", QStringList());
 
276
    if (sl.count() != 2)
 
277
        positioner->setPosition(50, 50);
 
278
    else
 
279
        positioner->setPosition(sl.first().toInt(), sl.last().toInt());
 
280
}
 
281
 
 
282
 
 
283
void KDMDialogWidget::defaults()
 
284
{
 
285
    greetstr_lined->setText(i18n("Welcome to %s at %n"));
 
286
    logoRadio->setChecked(true);
 
287
    slotAreaRadioClicked(KdmLogo);
 
288
    setLogo("");
 
289
    positioner->setPosition(50, 50);
 
290
}
 
291
 
 
292
QString KDMDialogWidget::quickHelp() const
 
293
{
 
294
    return i18n(
 
295
        "<h1>KDM - Dialog</h1> Here you can configure the basic appearance of "
 
296
        "the KDM login manager in dialog mode, i.e. a greeting string, an icon etc.");
 
297
}
 
298
 
 
299
#include "kdm-dlg.moc"