~ubuntu-branches/ubuntu/gutsy/kdebase-workspace/gutsy-backports

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2007-09-05 20:45:14 UTC
  • Revision ID: james.westby@ubuntu.com-20070905204514-632hhspl0nvrc84i
Tags: upstream-3.93.0
ImportĀ upstreamĀ versionĀ 3.93.0

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 <KApplication>
 
25
#include <K3URLDrag>
 
26
#include <KDialog>
 
27
#include <KFileDialog>
 
28
#include <KIconLoader>
 
29
#include <KImageFilePreview>
 
30
#include <KImageIO>
 
31
#include <KIO/NetAccess>
 
32
#include <KLineEdit>
 
33
#include <KLocale>
 
34
#include <KMessageBox>
 
35
#include <KConfig>
 
36
#include <KConfigGroup>
 
37
#include <KStandardDirs>
 
38
 
 
39
#include <QButtonGroup>
 
40
#include <QDragEnterEvent>
 
41
#include <QEvent>
 
42
#include <QGroupBox>
 
43
#include <QLabel>
 
44
#include <QPushButton>
 
45
#include <QRadioButton>
 
46
#include <QStyle>
 
47
#include <QGridLayout>
 
48
#include <QHBoxLayout>
 
49
#include <QVBoxLayout>
 
50
 
 
51
extern KConfig *config;
 
52
 
 
53
KDMDialogWidget::KDMDialogWidget( QWidget *parent )
 
54
        : QWidget( parent )
 
55
{
 
56
        QString wtstr;
 
57
 
 
58
        QGridLayout *grid = new QGridLayout( this );
 
59
        grid->setMargin( KDialog::marginHint() );
 
60
        grid->setSpacing( KDialog::spacingHint() );
 
61
        grid->setColumnStretch( 1, 1 );
 
62
 
 
63
        QHBoxLayout *hlay = new QHBoxLayout();
 
64
        hlay->setSpacing( KDialog::spacingHint() );
 
65
        grid->addLayout( hlay, 0, 0, 1, 2 );
 
66
        greetstr_lined = new KLineEdit( this );
 
67
        QLabel *label = new QLabel( i18n("&Greeting:"), this );
 
68
        label->setBuddy( greetstr_lined );
 
69
        hlay->addWidget( label );
 
70
        connect( greetstr_lined, SIGNAL(textChanged( const QString& )),
 
71
                 SIGNAL(changed()) );
 
72
        hlay->addWidget( greetstr_lined );
 
73
        wtstr = i18n("<p>This is the \"headline\" for KDM's login window. You may want to "
 
74
                     "put some nice greeting or information about the operating system here.</p>"
 
75
                     "<p>KDM will substitute the following character pairs with the "
 
76
                     "respective contents:</p>"
 
77
                     "<ul>"
 
78
                     "<li>%d -> current display</li>"
 
79
                     "<li>%h -> host name, possibly with domain name</li>"
 
80
                     "<li>%n -> node name, most probably the host name without domain name</li>"
 
81
                     "<li>%s -> the operating system</li>"
 
82
                     "<li>%r -> the operating system's version</li>"
 
83
                     "<li>%m -> the machine (hardware) type</li>"
 
84
                     "<li>%% -> a single %</li>"
 
85
                     "</ul>" );
 
86
        label->setWhatsThis( wtstr );
 
87
        greetstr_lined->setWhatsThis( wtstr );
 
88
 
 
89
 
 
90
        QGridLayout *hglay = new QGridLayout();
 
91
        hglay->setSpacing( KDialog::spacingHint() );
 
92
        grid->addLayout( hglay, 1, 0 );
 
93
 
 
94
        label = new QLabel( i18n("Logo area:"), this );
 
95
        hglay->addWidget( label, 0, 0 );
 
96
        QVBoxLayout *vlay = new QVBoxLayout();
 
97
        vlay->setSpacing( KDialog::spacingHint() );
 
98
        hglay->addLayout( vlay, 0, 1, 1, 2 );
 
99
        noneRadio = new QRadioButton( i18nc("logo area", "&None"), this );
 
100
        clockRadio = new QRadioButton( i18n("Show cloc&k"), this );
 
101
        logoRadio = new QRadioButton( i18n("Sho&w logo"), this );
 
102
        QButtonGroup *buttonGroup = new QButtonGroup( this );
 
103
        connect( buttonGroup, SIGNAL(buttonClicked( int )),
 
104
                 SLOT(slotAreaRadioClicked( int )) );
 
105
        connect( buttonGroup, SIGNAL(buttonClicked( int )), SIGNAL(changed()) );
 
106
        buttonGroup->addButton( noneRadio, KdmNone );
 
107
        buttonGroup->addButton( clockRadio, KdmClock );
 
108
        buttonGroup->addButton( logoRadio, KdmLogo );
 
109
        vlay->addWidget( noneRadio );
 
110
        vlay->addWidget( clockRadio );
 
111
        vlay->addWidget( logoRadio );
 
112
        wtstr = i18n("You can choose to display a custom logo (see below), a clock or no logo at all.");
 
113
        label->setWhatsThis( wtstr );
 
114
        noneRadio->setWhatsThis( wtstr );
 
115
        logoRadio->setWhatsThis( wtstr );
 
116
        clockRadio->setWhatsThis( wtstr );
 
117
 
 
118
        logoLabel = new QLabel( i18n("&Logo:"), this );
 
119
        logobutton = new QPushButton( this );
 
120
        logoLabel->setBuddy( logobutton );
 
121
        logobutton->setAutoDefault( false );
 
122
        logobutton->setAcceptDrops( true );
 
123
        logobutton->installEventFilter( this ); // for drag and drop
 
124
        connect( logobutton, SIGNAL(clicked()), SLOT(slotLogoButtonClicked()) );
 
125
        hglay->addWidget( logoLabel, 1, 0, Qt::AlignVCenter );
 
126
        hglay->addWidget( logobutton, 1, 1, Qt::AlignCenter );
 
127
        hglay->setRowMinimumHeight( 1, 110 );
 
128
        wtstr = i18n("Click here to choose an image that KDM will display. "
 
129
                     "You can also drag and drop an image onto this button "
 
130
                     "(e.g. from Konqueror).");
 
131
        logoLabel->setWhatsThis( wtstr );
 
132
        logobutton->setWhatsThis( wtstr );
 
133
 
 
134
 
 
135
        vlay = new QVBoxLayout();
 
136
        grid->addLayout( vlay, 1, 1, 2, 1 );
 
137
        vlay->setParent( grid );
 
138
 
 
139
        label = new QLabel( i18n("Dialog &position:"), this );
 
140
        vlay->addWidget( label );
 
141
        positioner = new Positioner( this );
 
142
        label->setBuddy( positioner );
 
143
        connect( positioner, SIGNAL(positionChanged()), SIGNAL(changed()) );
 
144
        vlay->addWidget( positioner );
 
145
 
 
146
        grid->setRowStretch( 3, 1 );
 
147
 
 
148
}
 
149
 
 
150
void KDMDialogWidget::makeReadOnly()
 
151
{
 
152
        disconnect( logobutton, SIGNAL(clicked()),
 
153
                    this, SLOT(slotLogoButtonClicked()) );
 
154
        logobutton->setAcceptDrops( false );
 
155
        greetstr_lined->setReadOnly( true );
 
156
        noneRadio->setEnabled( false );
 
157
        clockRadio->setEnabled( false );
 
158
        logoRadio->setEnabled( false );
 
159
        positioner->makeReadOnly();
 
160
}
 
161
 
 
162
bool KDMDialogWidget::setLogo(const QString &logo)
 
163
{
 
164
        QString flogo = logo.isEmpty() ?
 
165
                KStandardDirs::locate( "data", QLatin1String("kdm/pics/kdelogo.png") ) :
 
166
                logo;
 
167
        QImage p( flogo );
 
168
        if (p.isNull())
 
169
                return false;
 
170
        if (p.width() > 100 || p.height() > 100)
 
171
                p = p.scaled( 100, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation );
 
172
        logobutton->setPixmap( QPixmap::fromImage( p ) );
 
173
        uint bd = style()->pixelMetric( QStyle::PM_ButtonMargin ) * 2;
 
174
        logobutton->setFixedSize( p.width() + bd, p.height() + bd );
 
175
        logopath = logo;
 
176
        return true;
 
177
}
 
178
 
 
179
 
 
180
void KDMDialogWidget::slotLogoButtonClicked()
 
181
{
 
182
        KFileDialog dialog( KStandardDirs::locate( "data", QLatin1String("kdm/pics/") ),
 
183
                            KImageIO::pattern( KImageIO::Reading ),
 
184
                            this );
 
185
        dialog.setOperationMode( KFileDialog::Opening );
 
186
        dialog.setMode( KFile::File | KFile::LocalOnly );
 
187
 
 
188
        KImageFilePreview *imagePreview = new KImageFilePreview( &dialog );
 
189
        dialog.setPreviewWidget( imagePreview );
 
190
        if (dialog.exec() == QDialog::Accepted &&
 
191
            setLogo(dialog.selectedFile() ))
 
192
                changed();
 
193
}
 
194
 
 
195
 
 
196
void KDMDialogWidget::slotAreaRadioClicked( int id )
 
197
{
 
198
        logobutton->setEnabled( id == KdmLogo );
 
199
        logoLabel->setEnabled( id == KdmLogo );
 
200
}
 
201
 
 
202
 
 
203
bool KDMDialogWidget::eventFilter( QObject *, QEvent *e )
 
204
{
 
205
        if (e->type() == QEvent::DragEnter) {
 
206
                iconLoaderDragEnterEvent( (QDragEnterEvent *)e );
 
207
                return true;
 
208
        }
 
209
 
 
210
        if (e->type() == QEvent::Drop) {
 
211
                iconLoaderDropEvent( (QDropEvent *)e );
 
212
                return true;
 
213
        }
 
214
 
 
215
        return false;
 
216
}
 
217
 
 
218
void KDMDialogWidget::iconLoaderDragEnterEvent( QDragEnterEvent *e )
 
219
{
 
220
        e->setAccepted( K3URLDrag::canDecode( e ) );
 
221
}
 
222
 
 
223
 
 
224
KUrl *decodeImgDrop( QDropEvent *e, QWidget *wdg );
 
225
 
 
226
void KDMDialogWidget::iconLoaderDropEvent( QDropEvent *e )
 
227
{
 
228
        KUrl pixurl;
 
229
        bool istmp;
 
230
 
 
231
        KUrl *url = decodeImgDrop( e, this );
 
232
        if (url) {
 
233
 
 
234
                // we gotta check if it is a non-local file and make a tmp copy at the hd.
 
235
                if (!url->isLocalFile()) {
 
236
                        pixurl.setPath( KGlobal::dirs()->
 
237
                                resourceDirs("data").last() + "kdm/pics/" + url->fileName() );
 
238
                        KIO::NetAccess::file_copy( *url, pixurl, parentWidget() );
 
239
                        istmp = true;
 
240
                } else {
 
241
                        pixurl = *url;
 
242
                        istmp = false;
 
243
                }
 
244
 
 
245
                // By now url should be "file:/..."
 
246
                if (!setLogo( pixurl.path() )) {
 
247
                        KIO::NetAccess::del( pixurl, parentWidget() );
 
248
                        QString msg = i18n("There was an error loading the image:\n"
 
249
                                           "%1\n"
 
250
                                           "It will not be saved.",
 
251
                                           pixurl.path());
 
252
                        KMessageBox::sorry( this, msg );
 
253
                }
 
254
 
 
255
                delete url;
 
256
        }
 
257
}
 
258
 
 
259
 
 
260
void KDMDialogWidget::save()
 
261
{
 
262
        KConfigGroup configGrp = config->group( "X-*-Greeter" );
 
263
 
 
264
        configGrp.writeEntry( "GreetString", greetstr_lined->text() );
 
265
 
 
266
        configGrp.writeEntry( "LogoArea", noneRadio->isChecked() ? "None" :
 
267
                            logoRadio->isChecked() ? "Logo" : "Clock" );
 
268
 
 
269
        configGrp.writeEntry( "LogoPixmap", KIconLoader::global()->iconPath( logopath, K3Icon::Desktop, true ) );
 
270
 
 
271
        configGrp.writeEntry( "GreeterPos",
 
272
                QString("%1,%2").arg( positioner->x() ).arg( positioner->y() ) );
 
273
}
 
274
 
 
275
 
 
276
void KDMDialogWidget::load()
 
277
{
 
278
        KConfigGroup configGrp = config->group( "X-*-Greeter" );
 
279
 
 
280
        // Read the greeting string
 
281
        greetstr_lined->setText( configGrp.readEntry( "GreetString",
 
282
                                                    i18n("Welcome to %s at %n") ) );
 
283
 
 
284
        // Regular logo or clock
 
285
        QString logoArea = configGrp.readEntry( "LogoArea", "Logo" );
 
286
        if (logoArea == "Clock") {
 
287
                clockRadio->setChecked( true );
 
288
                slotAreaRadioClicked( KdmClock );
 
289
        } else if (logoArea == "Logo") {
 
290
                logoRadio->setChecked( true );
 
291
                slotAreaRadioClicked( KdmLogo );
 
292
        } else {
 
293
                noneRadio->setChecked( true );
 
294
                slotAreaRadioClicked( KdmNone );
 
295
        }
 
296
 
 
297
        // See if we use alternate logo
 
298
        setLogo( configGrp.readEntry( "LogoPixmap" ) );
 
299
 
 
300
        QStringList sl = configGrp.readEntry( "GreeterPos", QStringList() );
 
301
        if (sl.count() != 2)
 
302
                positioner->setPosition( 50, 50 );
 
303
        else
 
304
                positioner->setPosition( sl.first().toInt(), sl.last().toInt() );
 
305
}
 
306
 
 
307
 
 
308
void KDMDialogWidget::defaults()
 
309
{
 
310
        greetstr_lined->setText( i18n("Welcome to %s at %n") );
 
311
        logoRadio->setChecked( true );
 
312
        slotAreaRadioClicked( KdmLogo );
 
313
        setLogo( "" );
 
314
        positioner->setPosition( 50, 50 );
 
315
}
 
316
 
 
317
QString KDMDialogWidget::quickHelp() const
 
318
{
 
319
        return i18n("<h1>KDM - Dialog</h1> Here you can configure the basic appearance"
 
320
                    " of the KDM login manager in dialog mode, i.e. a greeting string, an icon etc.");
 
321
}
 
322
 
 
323
#include "kdm-dlg.moc"